bc58dd3a2814c0f6475fc4a3b72a6bd257db7b04
[samba.git] / source / nmbd / nmbd.c
1 /*
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 1997-2002
6    Copyright (C) Jelmer Vernooij 2002,2003 (Conversion to popt)
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #include "includes.h"
25
26 int ClientNMB       = -1;
27 int ClientDGRAM     = -1;
28 int global_nmb_port = -1;
29
30 extern BOOL rescan_listen_set;
31 extern struct in_addr loopback_ip;
32 extern BOOL global_in_nmbd;
33
34 extern BOOL override_logfile;
35
36 /* are we running as a daemon ? */
37 static BOOL is_daemon;
38
39 /* fork or run in foreground ? */
40 static BOOL Fork = True;
41
42 /* log to standard output ? */
43 static BOOL log_stdout;
44
45 /* have we found LanMan clients yet? */
46 BOOL found_lm_clients = False;
47
48 /* what server type are we currently */
49
50 time_t StartupTime = 0;
51
52 /**************************************************************************** **
53  Handle a SIGTERM in band.
54  **************************************************************************** */
55
56 static void terminate(void)
57 {
58         DEBUG(0,("Got SIGTERM: going down...\n"));
59   
60         /* Write out wins.dat file if samba is a WINS server */
61         wins_write_database(False);
62   
63         /* Remove all SELF registered names from WINS */
64         release_wins_names();
65   
66         /* Announce all server entries as 0 time-to-live, 0 type. */
67         announce_my_servers_removed();
68
69         /* If there was an async dns child - kill it. */
70         kill_async_dns_child();
71
72         exit(0);
73 }
74
75 /**************************************************************************** **
76  Handle a SHUTDOWN message from smbcontrol.
77  **************************************************************************** */
78
79 static void nmbd_terminate(int msg_type, pid_t src, void *buf, size_t len)
80 {
81         terminate();
82 }
83
84 /**************************************************************************** **
85  Catch a SIGTERM signal.
86  **************************************************************************** */
87
88 static SIG_ATOMIC_T got_sig_term;
89
90 static void sig_term(int sig)
91 {
92         got_sig_term = 1;
93         sys_select_signal(SIGTERM);
94 }
95
96 /**************************************************************************** **
97  Catch a SIGHUP signal.
98  **************************************************************************** */
99
100 static SIG_ATOMIC_T reload_after_sighup;
101
102 static void sig_hup(int sig)
103 {
104         reload_after_sighup = 1;
105         sys_select_signal(SIGHUP);
106 }
107
108 #if DUMP_CORE
109 /**************************************************************************** **
110  Prepare to dump a core file - carefully!
111  **************************************************************************** */
112
113 static BOOL dump_core(void)
114 {
115         char *p;
116         pstring dname;
117         pstrcpy( dname, lp_logfile() );
118         if ((p=strrchr_m(dname,'/')))
119                 *p=0;
120         pstrcat( dname, "/corefiles" );
121         mkdir( dname, 0700 );
122         sys_chown( dname, getuid(), getgid() );
123         chmod( dname, 0700 );
124         if ( chdir(dname) )
125                 return( False );
126         umask( ~(0700) );
127
128 #ifdef HAVE_GETRLIMIT
129 #ifdef RLIMIT_CORE
130         {
131                 struct rlimit rlp;
132                 getrlimit( RLIMIT_CORE, &rlp );
133                 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
134                 setrlimit( RLIMIT_CORE, &rlp );
135                 getrlimit( RLIMIT_CORE, &rlp );
136                 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
137         }
138 #endif
139 #endif
140
141
142         DEBUG(0,("Dumping core in %s\n",dname));
143         abort();
144         return( True );
145 }
146 #endif
147
148 /**************************************************************************** **
149  Possibly continue after a fault.
150  **************************************************************************** */
151
152 static void fault_continue(void)
153 {
154 #if DUMP_CORE
155         dump_core();
156 #endif
157 }
158
159 /**************************************************************************** **
160  Expire old names from the namelist and server list.
161  **************************************************************************** */
162
163 static void expire_names_and_servers(time_t t)
164 {
165         static time_t lastrun = 0;
166   
167         if ( !lastrun )
168                 lastrun = t;
169         if ( t < (lastrun + 5) )
170                 return;
171         lastrun = t;
172
173         /*
174          * Expire any timed out names on all the broadcast
175          * subnets and those registered with the WINS server.
176          * (nmbd_namelistdb.c)
177          */
178
179         expire_names(t);
180
181         /*
182          * Go through all the broadcast subnets and for each
183          * workgroup known on that subnet remove any expired
184          * server names. If a workgroup has an empty serverlist
185          * and has itself timed out then remove the workgroup.
186          * (nmbd_workgroupdb.c)
187          */
188
189         expire_workgroups_and_servers(t);
190 }
191
192 /************************************************************************** **
193  Reload the list of network interfaces.
194  ************************************************************************** */
195
196 static BOOL reload_interfaces(time_t t)
197 {
198         static time_t lastt;
199         int n;
200         struct subnet_record *subrec;
201
202         if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) return False;
203         lastt = t;
204
205         if (!interfaces_changed()) return False;
206
207         /* the list of probed interfaces has changed, we may need to add/remove
208            some subnets */
209         load_interfaces();
210
211         /* find any interfaces that need adding */
212         for (n=iface_count() - 1; n >= 0; n--) {
213                 struct interface *iface = get_interface(n);
214
215                 /*
216                  * We don't want to add a loopback interface, in case
217                  * someone has added 127.0.0.1 for smbd, nmbd needs to
218                  * ignore it here. JRA.
219                  */
220
221                 if (ip_equal(iface->ip, loopback_ip)) {
222                         DEBUG(2,("reload_interfaces: Ignoring loopback interface %s\n", inet_ntoa(iface->ip)));
223                         continue;
224                 }
225
226                 for (subrec=subnetlist; subrec; subrec=subrec->next) {
227                         if (ip_equal(iface->ip, subrec->myip) &&
228                             ip_equal(iface->nmask, subrec->mask_ip)) break;
229                 }
230
231                 if (!subrec) {
232                         /* it wasn't found! add it */
233                         DEBUG(2,("Found new interface %s\n", 
234                                  inet_ntoa(iface->ip)));
235                         subrec = make_normal_subnet(iface);
236                         if (subrec)
237                                 register_my_workgroup_one_subnet(subrec);
238                 }
239         }
240
241         /* find any interfaces that need deleting */
242         for (subrec=subnetlist; subrec; subrec=subrec->next) {
243                 for (n=iface_count() - 1; n >= 0; n--) {
244                         struct interface *iface = get_interface(n);
245                         if (ip_equal(iface->ip, subrec->myip) &&
246                             ip_equal(iface->nmask, subrec->mask_ip)) break;
247                 }
248                 if (n == -1) {
249                         /* oops, an interface has disapeared. This is
250                          tricky, we don't dare actually free the
251                          interface as it could be being used, so
252                          instead we just wear the memory leak and
253                          remove it from the list of interfaces without
254                          freeing it */
255                         DEBUG(2,("Deleting dead interface %s\n", 
256                                  inet_ntoa(subrec->myip)));
257                         close_subnet(subrec);
258                 }
259         }
260         
261         rescan_listen_set = True;
262
263         /* We need to shutdown if there are no subnets... */
264         if (FIRST_SUBNET == NULL) {
265                 DEBUG(0,("reload_interfaces: No subnets to listen to. Shutting down...\n"));
266                 return True;
267         }
268         return False;
269 }
270
271 /**************************************************************************** **
272  Reload the services file.
273  **************************************************************************** */
274
275 static BOOL reload_nmbd_services(BOOL test)
276 {
277         BOOL ret;
278
279         set_remote_machine_name("nmbd", False);
280
281         if ( lp_loaded() ) {
282                 pstring fname;
283                 pstrcpy( fname,lp_configfile());
284                 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
285                         pstrcpy(dyn_CONFIGFILE,fname);
286                         test = False;
287                 }
288         }
289
290         if ( test && !lp_file_list_changed() )
291                 return(True);
292
293         ret = lp_load( dyn_CONFIGFILE, True , False, False);
294
295         /* perhaps the config filename is now set */
296         if ( !test ) {
297                 DEBUG( 3, ( "services not loaded\n" ) );
298                 reload_nmbd_services( True );
299         }
300
301         return(ret);
302 }
303
304 /**************************************************************************** **
305  * React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP
306  * We use buf here to return BOOL result to process() when reload_interfaces()
307  * detects that there are no subnets.
308  **************************************************************************** */
309
310 static void msg_reload_nmbd_services(int msg_type, pid_t src, void *buf, size_t len)
311 {
312         write_browse_list( 0, True );
313         dump_all_namelists();
314         reload_nmbd_services( True );
315         reopen_logs();
316         
317         if(buf) {
318                 /* We were called from process() */
319                 /* If reload_interfaces() returned True */
320                 /* we need to shutdown if there are no subnets... */
321                 /* pass this info back to process() */
322                 *((BOOL*)buf) = reload_interfaces(0);  
323         }
324 }
325
326 static void msg_nmbd_send_packet(int msg_type, pid_t src,
327                                  void *buf, size_t len)
328 {
329         struct packet_struct *p = (struct packet_struct *)buf;
330         struct subnet_record *subrec;
331         struct in_addr *local_ip;
332
333         DEBUG(10, ("Received send_packet from %d\n", src));
334
335         if (len != sizeof(struct packet_struct)) {
336                 DEBUG(2, ("Discarding invalid packet length from %d\n", src));
337                 return;
338         }
339
340         if ((p->packet_type != NMB_PACKET) &&
341             (p->packet_type != DGRAM_PACKET)) {
342                 DEBUG(2, ("Discarding invalid packet type from %d: %d\n",
343                           src, p->packet_type));
344                 return;
345         }
346
347         local_ip = iface_ip(p->ip);
348
349         if (local_ip == NULL) {
350                 DEBUG(2, ("Could not find ip for packet from %d\n", src));
351                 return;
352         }
353
354         subrec = FIRST_SUBNET;
355
356         p->fd = (p->packet_type == NMB_PACKET) ?
357                 subrec->nmb_sock : subrec->dgram_sock;
358
359         for (subrec = FIRST_SUBNET; subrec != NULL;
360              subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
361                 if (ip_equal(*local_ip, subrec->myip)) {
362                         p->fd = (p->packet_type == NMB_PACKET) ?
363                                 subrec->nmb_sock : subrec->dgram_sock;
364                         break;
365                 }
366         }
367
368         if (p->packet_type == DGRAM_PACKET) {
369                 p->port = 138;
370                 p->packet.dgram.header.source_ip.s_addr = local_ip->s_addr;
371                 p->packet.dgram.header.source_port = 138;
372         }
373
374         send_packet(p);
375 }
376
377 /**************************************************************************** **
378  The main select loop.
379  **************************************************************************** */
380
381 static void process(void)
382 {
383         BOOL run_election;
384         BOOL no_subnets;
385
386         while( True ) {
387                 time_t t = time(NULL);
388
389                 /* Check for internal messages */
390
391                 message_dispatch();
392
393                 /*
394                  * Check all broadcast subnets to see if
395                  * we need to run an election on any of them.
396                  * (nmbd_elections.c)
397                  */
398
399                 run_election = check_elections();
400
401                 /*
402                  * Read incoming UDP packets.
403                  * (nmbd_packets.c)
404                  */
405
406                 if(listen_for_packets(run_election))
407                         return;
408
409                 /*
410                  * Handle termination inband.
411                  */
412
413                 if (got_sig_term) {
414                         got_sig_term = 0;
415                         terminate();
416                 }
417
418                 /*
419                  * Process all incoming packets
420                  * read above. This calls the success and
421                  * failure functions registered when response
422                  * packets arrrive, and also deals with request
423                  * packets from other sources.
424                  * (nmbd_packets.c)
425                  */
426
427                 run_packet_queue();
428
429                 /*
430                  * Run any elections - initiate becoming
431                  * a local master browser if we have won.
432                  * (nmbd_elections.c)
433                  */
434
435                 run_elections(t);
436
437                 /*
438                  * Send out any broadcast announcements
439                  * of our server names. This also announces
440                  * the workgroup name if we are a local
441                  * master browser.
442                  * (nmbd_sendannounce.c)
443                  */
444
445                 announce_my_server_names(t);
446
447                 /*
448                  * Send out any LanMan broadcast announcements
449                  * of our server names.
450                  * (nmbd_sendannounce.c)
451                  */
452
453                 announce_my_lm_server_names(t);
454
455                 /*
456                  * If we are a local master browser, periodically
457                  * announce ourselves to the domain master browser.
458                  * This also deals with syncronising the domain master
459                  * browser server lists with ourselves as a local
460                  * master browser.
461                  * (nmbd_sendannounce.c)
462                  */
463
464                 announce_myself_to_domain_master_browser(t);
465
466                 /*
467                  * Fullfill any remote announce requests.
468                  * (nmbd_sendannounce.c)
469                  */
470
471                 announce_remote(t);
472
473                 /*
474                  * Fullfill any remote browse sync announce requests.
475                  * (nmbd_sendannounce.c)
476                  */
477
478                 browse_sync_remote(t);
479
480                 /*
481                  * Scan the broadcast subnets, and WINS client
482                  * namelists and refresh any that need refreshing.
483                  * (nmbd_mynames.c)
484                  */
485
486                 refresh_my_names(t);
487
488                 /*
489                  * Scan the subnet namelists and server lists and
490                  * expire thos that have timed out.
491                  * (nmbd.c)
492                  */
493
494                 expire_names_and_servers(t);
495
496                 /*
497                  * Write out a snapshot of our current browse list into
498                  * the browse.dat file. This is used by smbd to service
499                  * incoming NetServerEnum calls - used to synchronise
500                  * browse lists over subnets.
501                  * (nmbd_serverlistdb.c)
502                  */
503
504                 write_browse_list(t, False);
505
506                 /*
507                  * If we are a domain master browser, we have a list of
508                  * local master browsers we should synchronise browse
509                  * lists with (these are added by an incoming local
510                  * master browser announcement packet). Expire any of
511                  * these that are no longer current, and pull the server
512                  * lists from each of these known local master browsers.
513                  * (nmbd_browsesync.c)
514                  */
515
516                 dmb_expire_and_sync_browser_lists(t);
517
518                 /*
519                  * Check that there is a local master browser for our
520                  * workgroup for all our broadcast subnets. If one
521                  * is not found, start an election (which we ourselves
522                  * may or may not participate in, depending on the
523                  * setting of the 'local master' parameter.
524                  * (nmbd_elections.c)
525                  */
526
527                 check_master_browser_exists(t);
528
529                 /*
530                  * If we are configured as a logon server, attempt to
531                  * register the special NetBIOS names to become such
532                  * (WORKGROUP<1c> name) on all broadcast subnets and
533                  * with the WINS server (if used). If we are configured
534                  * to become a domain master browser, attempt to register
535                  * the special NetBIOS name (WORKGROUP<1b> name) to
536                  * become such.
537                  * (nmbd_become_dmb.c)
538                  */
539
540                 add_domain_names(t);
541
542                 /*
543                  * If we are a WINS server, do any timer dependent
544                  * processing required.
545                  * (nmbd_winsserver.c)
546                  */
547
548                 initiate_wins_processing(t);
549
550                 /*
551                  * If we are a domain master browser, attempt to contact the
552                  * WINS server to get a list of all known WORKGROUPS/DOMAINS.
553                  * This will only work to a Samba WINS server.
554                  * (nmbd_browsesync.c)
555                  */
556
557                 if (lp_enhanced_browsing())
558                         collect_all_workgroup_names_from_wins_server(t);
559
560                 /*
561                  * Go through the response record queue and time out or re-transmit
562                  * and expired entries.
563                  * (nmbd_packets.c)
564                  */
565
566                 retransmit_or_expire_response_records(t);
567
568                 /*
569                  * check to see if any remote browse sync child processes have completed
570                  */
571
572                 sync_check_completion();
573
574                 /*
575                  * regularly sync with any other DMBs we know about 
576                  */
577
578                 if (lp_enhanced_browsing())
579                         sync_all_dmbs(t);
580
581                 /*
582                  * clear the unexpected packet queue 
583                  */
584
585                 clear_unexpected(t);
586
587                 /*
588                  * Reload the services file if we got a sighup.
589                  */
590
591                 if(reload_after_sighup) {
592                         DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
593                         msg_reload_nmbd_services(MSG_SMB_CONF_UPDATED, (pid_t) 0, (void*) &no_subnets, 0);
594                         if(no_subnets)
595                                 return;
596                         reload_after_sighup = 0;
597                 }
598
599                 /* check for new network interfaces */
600
601                 if(reload_interfaces(t))
602                         return;
603
604                 /* free up temp memory */
605                         lp_talloc_free();
606         }
607 }
608
609 /**************************************************************************** **
610  Open the socket communication.
611  **************************************************************************** */
612
613 static BOOL open_sockets(BOOL isdaemon, int port)
614 {
615         /*
616          * The sockets opened here will be used to receive broadcast
617          * packets *only*. Interface specific sockets are opened in
618          * make_subnet() in namedbsubnet.c. Thus we bind to the
619          * address "0.0.0.0". The parameter 'socket address' is
620          * now deprecated.
621          */
622
623         if ( isdaemon )
624                 ClientNMB = open_socket_in(SOCK_DGRAM, port,
625                                            0, interpret_addr(lp_socket_address()),
626                                            True);
627         else
628                 ClientNMB = 0;
629   
630         ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,0,True);
631
632         if ( ClientNMB == -1 )
633                 return( False );
634
635         /* we are never interested in SIGPIPE */
636         BlockSignals(True,SIGPIPE);
637
638         set_socket_options( ClientNMB,   "SO_BROADCAST" );
639         set_socket_options( ClientDGRAM, "SO_BROADCAST" );
640
641         DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
642         return( True );
643 }
644
645 /**************************************************************************** **
646  main program
647  **************************************************************************** */
648  int main(int argc, const char *argv[])
649 {
650         pstring logfile;
651         static BOOL opt_interactive;
652         poptContext pc;
653         struct poptOption long_options[] = {
654         POPT_AUTOHELP
655         {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon(default)" },
656         {"interactive", 'i', POPT_ARG_VAL, &opt_interactive, True, "Run interactive (not a daemon)" },
657         {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },
658         {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
659         {"hosts", 'H', POPT_ARG_STRING, dyn_LMHOSTSFILE, 'H', "Load a netbios hosts file"},
660         {"port", 'p', POPT_ARG_INT, &global_nmb_port, NMB_PORT, "Listen on the specified port" },
661         POPT_COMMON_SAMBA
662         { NULL }
663         };
664
665         global_nmb_port = NMB_PORT;
666
667         pc = poptGetContext("nmbd", argc, argv, long_options, 0);
668         while (poptGetNextOpt(pc) != -1) {};
669         poptFreeContext(pc);
670
671         global_in_nmbd = True;
672         
673         StartupTime = time(NULL);
674         
675         sys_srandom(time(NULL) ^ sys_getpid());
676         
677         if (!override_logfile) {
678                 slprintf(logfile, sizeof(logfile)-1, "%s/log.nmbd", dyn_LOGFILEBASE);
679                 lp_set_logfile(logfile);
680         }
681         
682         fault_setup((void (*)(void *))fault_continue );
683         
684         /* POSIX demands that signals are inherited. If the invoking process has
685          * these signals masked, we will have problems, as we won't receive them. */
686         BlockSignals(False, SIGHUP);
687         BlockSignals(False, SIGUSR1);
688         BlockSignals(False, SIGTERM);
689         
690         CatchSignal( SIGHUP,  SIGNAL_CAST sig_hup );
691         CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
692         
693 #if defined(SIGFPE)
694         /* we are never interested in SIGFPE */
695         BlockSignals(True,SIGFPE);
696 #endif
697
698         /* We no longer use USR2... */
699 #if defined(SIGUSR2)
700         BlockSignals(True, SIGUSR2);
701 #endif
702
703         if ( opt_interactive ) {
704                 Fork = False;
705                 log_stdout = True;
706         }
707
708         if ( log_stdout && Fork ) {
709                 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
710                 exit(1);
711         }
712
713         setup_logging( argv[0], log_stdout );
714
715         reopen_logs();
716
717         DEBUG( 0, ( "Netbios nameserver version %s started.\n", SAMBA_VERSION_STRING) );
718         DEBUGADD( 0, ( "Copyright Andrew Tridgell and the Samba Team 1994-2004\n" ) );
719
720         if ( !reload_nmbd_services(False) )
721                 return(-1);
722
723         if(!init_names())
724                 return -1;
725
726         reload_nmbd_services( True );
727
728         if (strequal(lp_workgroup(),"*")) {
729                 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
730                 exit(1);
731         }
732
733         set_samba_nb_type();
734
735         if (!is_daemon && !is_a_socket(0)) {
736                 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
737                 is_daemon = True;
738         }
739   
740         if (is_daemon && !opt_interactive) {
741                 DEBUG( 2, ( "Becoming a daemon.\n" ) );
742                 become_daemon(Fork);
743         }
744
745 #if HAVE_SETPGID
746         /*
747          * If we're interactive we want to set our own process group for 
748          * signal management.
749          */
750         if (opt_interactive)
751                 setpgid( (pid_t)0, (pid_t)0 );
752 #endif
753
754 #ifndef SYNC_DNS
755         /* Setup the async dns. We do it here so it doesn't have all the other
756                 stuff initialised and thus chewing memory and sockets */
757         if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
758                 start_async_dns();
759         }
760 #endif
761
762         if (!directory_exist(lp_lockdir(), NULL)) {
763                 mkdir(lp_lockdir(), 0755);
764         }
765
766         pidfile_create("nmbd");
767         message_init();
768         message_register(MSG_FORCE_ELECTION, nmbd_message_election);
769         message_register(MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry);
770         message_register(MSG_SHUTDOWN, nmbd_terminate);
771         message_register(MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services);
772         message_register(MSG_SEND_PACKET, msg_nmbd_send_packet);
773
774         DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
775
776         if ( !open_sockets( is_daemon, global_nmb_port ) ) {
777                 kill_async_dns_child();
778                 return 1;
779         }
780
781         /* Determine all the IP addresses we have. */
782         load_interfaces();
783
784         /* Create an nmbd subnet record for each of the above. */
785         if( False == create_subnets() ) {
786                 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
787                 kill_async_dns_child();
788                 exit(1);
789         }
790
791         /* Load in any static local names. */ 
792         load_lmhosts_file(dyn_LMHOSTSFILE);
793         DEBUG(3,("Loaded hosts file %s\n", dyn_LMHOSTSFILE));
794
795         /* If we are acting as a WINS server, initialise data structures. */
796         if( !initialise_wins() ) {
797                 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
798                 kill_async_dns_child();
799                 exit(1);
800         }
801
802         /* 
803          * Register nmbd primary workgroup and nmbd names on all
804          * the broadcast subnets, and on the WINS server (if specified).
805          * Also initiate the startup of our primary workgroup (start
806          * elections if we are setup as being able to be a local
807          * master browser.
808          */
809
810         if( False == register_my_workgroup_and_names() ) {
811                 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
812                 kill_async_dns_child();
813                 exit(1);
814         }
815
816         /* We can only take signals in the select. */
817         BlockSignals( True, SIGTERM );
818
819         process();
820
821         if (dbf)
822                 x_fclose(dbf);
823         kill_async_dns_child();
824         return(0);
825 }