source: branches/samba-3.3.x/source/nmbd/nmbd.c@ 273

Last change on this file since 273 was 273, checked in by Silvan Scherrer, 16 years ago

mutex lock fix

File size: 25.3 KB
Line 
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 3 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, see <http://www.gnu.org/licenses/>.
20
21*/
22
23#include "includes.h"
24
25int ClientNMB = -1;
26int ClientDGRAM = -1;
27int global_nmb_port = -1;
28
29extern bool rescan_listen_set;
30extern bool global_in_nmbd;
31
32#ifdef __OS2__
33extern int global_Sem32Add;
34#endif
35
36extern bool override_logfile;
37
38/* have we found LanMan clients yet? */
39bool found_lm_clients = False;
40
41/* what server type are we currently */
42
43time_t StartupTime = 0;
44
45struct event_context *nmbd_event_context(void)
46{
47 static struct event_context *ctx;
48
49 if (!ctx && !(ctx = event_context_init(NULL))) {
50 smb_panic("Could not init nmbd event context");
51 }
52 return ctx;
53}
54
55struct messaging_context *nmbd_messaging_context(void)
56{
57 static struct messaging_context *ctx;
58
59 if (ctx == NULL) {
60 ctx = messaging_init(NULL, server_id_self(),
61 nmbd_event_context());
62 }
63 if (ctx == NULL) {
64 DEBUG(0, ("Could not init nmbd messaging context.\n"));
65 }
66 return ctx;
67}
68
69/**************************************************************************** **
70 Handle a SIGTERM in band.
71 **************************************************************************** */
72
73static void terminate(void)
74{
75 DEBUG(0,("Got SIGTERM: going down...\n"));
76
77 /* Write out wins.dat file if samba is a WINS server */
78 wins_write_database(0,False);
79
80 /* Remove all SELF registered names from WINS */
81 release_wins_names();
82
83 /* Announce all server entries as 0 time-to-live, 0 type. */
84 announce_my_servers_removed();
85
86 /* If there was an async dns child - kill it. */
87 kill_async_dns_child();
88
89 exit(0);
90}
91
92/**************************************************************************** **
93 Handle a SHUTDOWN message from smbcontrol.
94 **************************************************************************** */
95
96static void nmbd_terminate(struct messaging_context *msg,
97 void *private_data,
98 uint32_t msg_type,
99 struct server_id server_id,
100 DATA_BLOB *data)
101{
102 terminate();
103}
104
105/**************************************************************************** **
106 Catch a SIGTERM signal.
107 **************************************************************************** */
108
109static SIG_ATOMIC_T got_sig_term;
110
111static void sig_term(int sig)
112{
113 got_sig_term = 1;
114 sys_select_signal(SIGTERM);
115}
116
117/**************************************************************************** **
118 Catch a SIGHUP signal.
119 **************************************************************************** */
120
121static SIG_ATOMIC_T reload_after_sighup;
122
123static void sig_hup(int sig)
124{
125 reload_after_sighup = 1;
126 sys_select_signal(SIGHUP);
127}
128
129/**************************************************************************** **
130 Possibly continue after a fault.
131 **************************************************************************** */
132
133static void fault_continue(void)
134{
135 dump_core();
136}
137
138/**************************************************************************** **
139 Expire old names from the namelist and server list.
140 **************************************************************************** */
141
142static void expire_names_and_servers(time_t t)
143{
144 static time_t lastrun = 0;
145
146 if ( !lastrun )
147 lastrun = t;
148 if ( t < (lastrun + 5) )
149 return;
150 lastrun = t;
151
152 /*
153 * Expire any timed out names on all the broadcast
154 * subnets and those registered with the WINS server.
155 * (nmbd_namelistdb.c)
156 */
157
158 expire_names(t);
159
160 /*
161 * Go through all the broadcast subnets and for each
162 * workgroup known on that subnet remove any expired
163 * server names. If a workgroup has an empty serverlist
164 * and has itself timed out then remove the workgroup.
165 * (nmbd_workgroupdb.c)
166 */
167
168 expire_workgroups_and_servers(t);
169}
170
171/************************************************************************** **
172 Reload the list of network interfaces.
173 Doesn't return until a network interface is up.
174 ************************************************************************** */
175
176static void reload_interfaces(time_t t)
177{
178 static time_t lastt;
179 int n;
180 bool print_waiting_msg = true;
181 struct subnet_record *subrec;
182
183 if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) {
184 return;
185 }
186
187 lastt = t;
188
189 if (!interfaces_changed()) {
190 return;
191 }
192
193 try_again:
194
195 /* the list of probed interfaces has changed, we may need to add/remove
196 some subnets */
197 load_interfaces();
198
199 /* find any interfaces that need adding */
200 for (n=iface_count() - 1; n >= 0; n--) {
201 char str[INET6_ADDRSTRLEN];
202 const struct interface *iface = get_interface(n);
203 struct in_addr ip, nmask;
204
205 if (!iface) {
206 DEBUG(2,("reload_interfaces: failed to get interface %d\n", n));
207 continue;
208 }
209
210 /* Ensure we're only dealing with IPv4 here. */
211 if (iface->ip.ss_family != AF_INET) {
212 DEBUG(2,("reload_interfaces: "
213 "ignoring non IPv4 interface.\n"));
214 continue;
215 }
216
217 ip = ((struct sockaddr_in *)&iface->ip)->sin_addr;
218 nmask = ((struct sockaddr_in *)&iface->netmask)->sin_addr;
219
220 /*
221 * We don't want to add a loopback interface, in case
222 * someone has added 127.0.0.1 for smbd, nmbd needs to
223 * ignore it here. JRA.
224 */
225
226 if (is_loopback_addr(&iface->ip)) {
227 DEBUG(2,("reload_interfaces: Ignoring loopback "
228 "interface %s\n",
229 print_sockaddr(str, sizeof(str), &iface->ip) ));
230 continue;
231 }
232
233 for (subrec=subnetlist; subrec; subrec=subrec->next) {
234 if (ip_equal_v4(ip, subrec->myip) &&
235 ip_equal_v4(nmask, subrec->mask_ip)) {
236 break;
237 }
238 }
239
240 if (!subrec) {
241 /* it wasn't found! add it */
242 DEBUG(2,("Found new interface %s\n",
243 print_sockaddr(str,
244 sizeof(str), &iface->ip) ));
245 subrec = make_normal_subnet(iface);
246 if (subrec)
247 register_my_workgroup_one_subnet(subrec);
248 }
249 }
250
251 /* find any interfaces that need deleting */
252 for (subrec=subnetlist; subrec; subrec=subrec->next) {
253 for (n=iface_count() - 1; n >= 0; n--) {
254 struct interface *iface = get_interface(n);
255 struct in_addr ip, nmask;
256 if (!iface) {
257 continue;
258 }
259 /* Ensure we're only dealing with IPv4 here. */
260 if (iface->ip.ss_family != AF_INET) {
261 DEBUG(2,("reload_interfaces: "
262 "ignoring non IPv4 interface.\n"));
263 continue;
264 }
265 ip = ((struct sockaddr_in *)&iface->ip)->sin_addr;
266 nmask = ((struct sockaddr_in *)&iface->netmask)->sin_addr;
267 if (ip_equal_v4(ip, subrec->myip) &&
268 ip_equal_v4(nmask, subrec->mask_ip)) {
269 break;
270 }
271 }
272 if (n == -1) {
273 /* oops, an interface has disapeared. This is
274 tricky, we don't dare actually free the
275 interface as it could be being used, so
276 instead we just wear the memory leak and
277 remove it from the list of interfaces without
278 freeing it */
279 DEBUG(2,("Deleting dead interface %s\n",
280 inet_ntoa(subrec->myip)));
281 close_subnet(subrec);
282 }
283 }
284
285 rescan_listen_set = True;
286
287 /* We need to wait if there are no subnets... */
288 if (FIRST_SUBNET == NULL) {
289
290 if (print_waiting_msg) {
291 DEBUG(0,("reload_interfaces: "
292 "No subnets to listen to. Waiting..\n"));
293 print_waiting_msg = false;
294 }
295
296 /*
297 * Whilst we're waiting for an interface, allow SIGTERM to
298 * cause us to exit.
299 */
300
301 BlockSignals(false, SIGTERM);
302
303 /* We only count IPv4, non-loopback interfaces here. */
304 while (iface_count_v4_nl() == 0 && !got_sig_term) {
305 sleep(5);
306 load_interfaces();
307 }
308
309 /*
310 * Handle termination inband.
311 */
312
313 if (got_sig_term) {
314 got_sig_term = 0;
315 terminate();
316 }
317
318 /*
319 * We got an interface, go back to blocking term.
320 */
321
322 BlockSignals(true, SIGTERM);
323 goto try_again;
324 }
325}
326
327/**************************************************************************** **
328 Reload the services file.
329 **************************************************************************** */
330
331static bool reload_nmbd_services(bool test)
332{
333 bool ret;
334
335 set_remote_machine_name("nmbd", False);
336
337 if ( lp_loaded() ) {
338 const char *fname = lp_configfile();
339 if (file_exist(fname,NULL) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
340 set_dyn_CONFIGFILE(fname);
341 test = False;
342 }
343 }
344
345 if ( test && !lp_file_list_changed() )
346 return(True);
347
348 ret = lp_load(get_dyn_CONFIGFILE(), True , False, False, True);
349
350 /* perhaps the config filename is now set */
351 if ( !test ) {
352 DEBUG( 3, ( "services not loaded\n" ) );
353 reload_nmbd_services( True );
354 }
355
356 return(ret);
357}
358
359/**************************************************************************** **
360 * React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP
361 **************************************************************************** */
362
363static void msg_reload_nmbd_services(struct messaging_context *msg,
364 void *private_data,
365 uint32_t msg_type,
366 struct server_id server_id,
367 DATA_BLOB *data)
368{
369 write_browse_list( 0, True );
370 dump_all_namelists();
371 reload_nmbd_services( True );
372 reopen_logs();
373 reload_interfaces(0);
374}
375
376static void msg_nmbd_send_packet(struct messaging_context *msg,
377 void *private_data,
378 uint32_t msg_type,
379 struct server_id src,
380 DATA_BLOB *data)
381{
382 struct packet_struct *p = (struct packet_struct *)data->data;
383 struct subnet_record *subrec;
384 struct sockaddr_storage ss;
385 const struct sockaddr_storage *pss;
386 const struct in_addr *local_ip;
387
388 DEBUG(10, ("Received send_packet from %u\n", (unsigned int)procid_to_pid(&src)));
389
390 if (data->length != sizeof(struct packet_struct)) {
391 DEBUG(2, ("Discarding invalid packet length from %u\n",
392 (unsigned int)procid_to_pid(&src)));
393 return;
394 }
395
396 if ((p->packet_type != NMB_PACKET) &&
397 (p->packet_type != DGRAM_PACKET)) {
398 DEBUG(2, ("Discarding invalid packet type from %u: %d\n",
399 (unsigned int)procid_to_pid(&src), p->packet_type));
400 return;
401 }
402
403 in_addr_to_sockaddr_storage(&ss, p->ip);
404 pss = iface_ip(&ss);
405
406 if (pss == NULL) {
407 DEBUG(2, ("Could not find ip for packet from %u\n",
408 (unsigned int)procid_to_pid(&src)));
409 return;
410 }
411
412 local_ip = &((const struct sockaddr_in *)pss)->sin_addr;
413 subrec = FIRST_SUBNET;
414
415 p->fd = (p->packet_type == NMB_PACKET) ?
416 subrec->nmb_sock : subrec->dgram_sock;
417
418 for (subrec = FIRST_SUBNET; subrec != NULL;
419 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
420 if (ip_equal_v4(*local_ip, subrec->myip)) {
421 p->fd = (p->packet_type == NMB_PACKET) ?
422 subrec->nmb_sock : subrec->dgram_sock;
423 break;
424 }
425 }
426
427 if (p->packet_type == DGRAM_PACKET) {
428 p->port = 138;
429 p->packet.dgram.header.source_ip.s_addr = local_ip->s_addr;
430 p->packet.dgram.header.source_port = 138;
431 }
432
433 send_packet(p);
434}
435
436/**************************************************************************** **
437 The main select loop.
438 **************************************************************************** */
439
440static void process(void)
441{
442 bool run_election;
443
444 while( True ) {
445 time_t t = time(NULL);
446 TALLOC_CTX *frame = talloc_stackframe();
447
448 /* Check for internal messages */
449
450 message_dispatch(nmbd_messaging_context());
451
452 /*
453 * Check all broadcast subnets to see if
454 * we need to run an election on any of them.
455 * (nmbd_elections.c)
456 */
457
458 run_election = check_elections();
459
460 /*
461 * Read incoming UDP packets.
462 * (nmbd_packets.c)
463 */
464
465 if(listen_for_packets(run_election)) {
466 TALLOC_FREE(frame);
467 return;
468 }
469
470 /*
471 * Handle termination inband.
472 */
473
474 if (got_sig_term) {
475 got_sig_term = 0;
476 terminate();
477 }
478
479 /*
480 * Process all incoming packets
481 * read above. This calls the success and
482 * failure functions registered when response
483 * packets arrrive, and also deals with request
484 * packets from other sources.
485 * (nmbd_packets.c)
486 */
487
488 run_packet_queue();
489
490 /*
491 * Run any elections - initiate becoming
492 * a local master browser if we have won.
493 * (nmbd_elections.c)
494 */
495
496 run_elections(t);
497
498 /*
499 * Send out any broadcast announcements
500 * of our server names. This also announces
501 * the workgroup name if we are a local
502 * master browser.
503 * (nmbd_sendannounce.c)
504 */
505
506 announce_my_server_names(t);
507
508 /*
509 * Send out any LanMan broadcast announcements
510 * of our server names.
511 * (nmbd_sendannounce.c)
512 */
513
514 announce_my_lm_server_names(t);
515
516 /*
517 * If we are a local master browser, periodically
518 * announce ourselves to the domain master browser.
519 * This also deals with syncronising the domain master
520 * browser server lists with ourselves as a local
521 * master browser.
522 * (nmbd_sendannounce.c)
523 */
524
525 announce_myself_to_domain_master_browser(t);
526
527 /*
528 * Fullfill any remote announce requests.
529 * (nmbd_sendannounce.c)
530 */
531
532 announce_remote(t);
533
534 /*
535 * Fullfill any remote browse sync announce requests.
536 * (nmbd_sendannounce.c)
537 */
538
539 browse_sync_remote(t);
540
541 /*
542 * Scan the broadcast subnets, and WINS client
543 * namelists and refresh any that need refreshing.
544 * (nmbd_mynames.c)
545 */
546
547 refresh_my_names(t);
548
549 /*
550 * Scan the subnet namelists and server lists and
551 * expire thos that have timed out.
552 * (nmbd.c)
553 */
554
555 expire_names_and_servers(t);
556
557 /*
558 * Write out a snapshot of our current browse list into
559 * the browse.dat file. This is used by smbd to service
560 * incoming NetServerEnum calls - used to synchronise
561 * browse lists over subnets.
562 * (nmbd_serverlistdb.c)
563 */
564
565 write_browse_list(t, False);
566
567 /*
568 * If we are a domain master browser, we have a list of
569 * local master browsers we should synchronise browse
570 * lists with (these are added by an incoming local
571 * master browser announcement packet). Expire any of
572 * these that are no longer current, and pull the server
573 * lists from each of these known local master browsers.
574 * (nmbd_browsesync.c)
575 */
576
577 dmb_expire_and_sync_browser_lists(t);
578
579 /*
580 * Check that there is a local master browser for our
581 * workgroup for all our broadcast subnets. If one
582 * is not found, start an election (which we ourselves
583 * may or may not participate in, depending on the
584 * setting of the 'local master' parameter.
585 * (nmbd_elections.c)
586 */
587
588 check_master_browser_exists(t);
589
590 /*
591 * If we are configured as a logon server, attempt to
592 * register the special NetBIOS names to become such
593 * (WORKGROUP<1c> name) on all broadcast subnets and
594 * with the WINS server (if used). If we are configured
595 * to become a domain master browser, attempt to register
596 * the special NetBIOS name (WORKGROUP<1b> name) to
597 * become such.
598 * (nmbd_become_dmb.c)
599 */
600
601 add_domain_names(t);
602
603 /*
604 * If we are a WINS server, do any timer dependent
605 * processing required.
606 * (nmbd_winsserver.c)
607 */
608
609 initiate_wins_processing(t);
610
611 /*
612 * If we are a domain master browser, attempt to contact the
613 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
614 * This will only work to a Samba WINS server.
615 * (nmbd_browsesync.c)
616 */
617
618 if (lp_enhanced_browsing())
619 collect_all_workgroup_names_from_wins_server(t);
620
621 /*
622 * Go through the response record queue and time out or re-transmit
623 * and expired entries.
624 * (nmbd_packets.c)
625 */
626
627 retransmit_or_expire_response_records(t);
628
629 /*
630 * check to see if any remote browse sync child processes have completed
631 */
632
633 sync_check_completion();
634
635 /*
636 * regularly sync with any other DMBs we know about
637 */
638
639 if (lp_enhanced_browsing())
640 sync_all_dmbs(t);
641
642 /*
643 * clear the unexpected packet queue
644 */
645
646 clear_unexpected(t);
647
648 /*
649 * Reload the services file if we got a sighup.
650 */
651
652 if(reload_after_sighup) {
653 DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
654 msg_reload_nmbd_services(nmbd_messaging_context(),
655 NULL, MSG_SMB_CONF_UPDATED,
656 procid_self(), NULL);
657
658 reload_after_sighup = 0;
659 }
660
661 /* check for new network interfaces */
662
663 reload_interfaces(t);
664
665 /* free up temp memory */
666 TALLOC_FREE(frame);
667 }
668}
669
670/**************************************************************************** **
671 Open the socket communication.
672 **************************************************************************** */
673
674static bool open_sockets(bool isdaemon, int port)
675{
676 struct sockaddr_storage ss;
677 const char *sock_addr = lp_socket_address();
678
679 /*
680 * The sockets opened here will be used to receive broadcast
681 * packets *only*. Interface specific sockets are opened in
682 * make_subnet() in namedbsubnet.c. Thus we bind to the
683 * address "0.0.0.0". The parameter 'socket address' is
684 * now deprecated.
685 */
686
687 if (!interpret_string_addr(&ss, sock_addr,
688 AI_NUMERICHOST|AI_PASSIVE)) {
689 DEBUG(0,("open_sockets: unable to get socket address "
690 "from string %s", sock_addr));
691 return false;
692 }
693 if (ss.ss_family != AF_INET) {
694 DEBUG(0,("open_sockets: unable to use IPv6 socket"
695 "%s in nmbd\n",
696 sock_addr));
697 return false;
698 }
699
700 if (isdaemon) {
701 ClientNMB = open_socket_in(SOCK_DGRAM, port,
702 0, &ss,
703 true);
704 } else {
705 ClientNMB = 0;
706 }
707
708 if (ClientNMB == -1) {
709 return false;
710 }
711
712 ClientDGRAM = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
713 3, &ss,
714 true);
715
716 if (ClientDGRAM == -1) {
717 if (ClientNMB != 0) {
718 close(ClientNMB);
719 }
720 return false;
721 }
722
723 /* we are never interested in SIGPIPE */
724 BlockSignals(True,SIGPIPE);
725
726 set_socket_options( ClientNMB, "SO_BROADCAST" );
727 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
728
729 /* Ensure we're non-blocking. */
730 set_blocking( ClientNMB, False);
731 set_blocking( ClientDGRAM, False);
732
733 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
734 return( True );
735}
736
737/**************************************************************************** **
738 main program
739 **************************************************************************** */
740
741 int main(int argc, const char *argv[])
742{
743 static bool is_daemon;
744 static bool opt_interactive;
745 static bool Fork = true;
746 static bool no_process_group;
747 static bool log_stdout;
748 poptContext pc;
749 char *p_lmhosts = NULL;
750 int opt;
751 enum {
752 OPT_DAEMON = 1000,
753 OPT_INTERACTIVE,
754 OPT_FORK,
755 OPT_NO_PROCESS_GROUP,
756 OPT_LOG_STDOUT
757 };
758 struct poptOption long_options[] = {
759 POPT_AUTOHELP
760 {"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon(default)" },
761 {"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon)" },
762 {"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools & etc)" },
763 {"no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
764 {"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
765 {"hosts", 'H', POPT_ARG_STRING, &p_lmhosts, 'H', "Load a netbios hosts file"},
766 {"port", 'p', POPT_ARG_INT, &global_nmb_port, NMB_PORT, "Listen on the specified port" },
767 POPT_COMMON_SAMBA
768 { NULL }
769 };
770 TALLOC_CTX *frame = talloc_stackframe(); /* Setup tos. */
771
772#ifdef __OS2__
773 global_Sem32Add = 1;
774#endif
775
776 load_case_tables();
777
778 global_nmb_port = NMB_PORT;
779
780 pc = poptGetContext("nmbd", argc, argv, long_options, 0);
781 while ((opt = poptGetNextOpt(pc)) != -1) {
782 switch (opt) {
783 case OPT_DAEMON:
784 is_daemon = true;
785 break;
786 case OPT_INTERACTIVE:
787 opt_interactive = true;
788 break;
789 case OPT_FORK:
790 Fork = false;
791 break;
792 case OPT_NO_PROCESS_GROUP:
793 no_process_group = true;
794 break;
795 case OPT_LOG_STDOUT:
796 log_stdout = true;
797 break;
798 default:
799 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
800 poptBadOption(pc, 0), poptStrerror(opt));
801 poptPrintUsage(pc, stderr, 0);
802 exit(1);
803 }
804 };
805 poptFreeContext(pc);
806
807 global_in_nmbd = true;
808
809 StartupTime = time(NULL);
810
811 sys_srandom(time(NULL) ^ sys_getpid());
812
813 if (!override_logfile) {
814 char *lfile = NULL;
815 if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
816 exit(1);
817 }
818 lp_set_logfile(lfile);
819 SAFE_FREE(lfile);
820 }
821
822 fault_setup((void (*)(void *))fault_continue );
823 dump_core_setup("nmbd");
824
825 /* POSIX demands that signals are inherited. If the invoking process has
826 * these signals masked, we will have problems, as we won't receive them. */
827 BlockSignals(False, SIGHUP);
828 BlockSignals(False, SIGUSR1);
829 BlockSignals(False, SIGTERM);
830
831 CatchSignal( SIGHUP, SIGNAL_CAST sig_hup );
832 CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
833
834#if defined(SIGFPE)
835 /* we are never interested in SIGFPE */
836 BlockSignals(True,SIGFPE);
837#endif
838
839 /* We no longer use USR2... */
840#if defined(SIGUSR2)
841 BlockSignals(True, SIGUSR2);
842#endif
843
844 if ( opt_interactive ) {
845 Fork = False;
846 log_stdout = True;
847 }
848
849 if ( log_stdout && Fork ) {
850 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
851 exit(1);
852 }
853
854 setup_logging( argv[0], log_stdout );
855
856 reopen_logs();
857
858 DEBUG(0,("nmbd version %s started.\n", SAMBA_VERSION_STRING));
859 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
860
861 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
862 DEBUG(0, ("error opening config file\n"));
863 exit(1);
864 }
865
866 if (nmbd_messaging_context() == NULL) {
867 return 1;
868 }
869
870 if ( !reload_nmbd_services(False) )
871 return(-1);
872
873 if(!init_names())
874 return -1;
875
876 reload_nmbd_services( True );
877
878 if (strequal(lp_workgroup(),"*")) {
879 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
880 exit(1);
881 }
882
883 set_samba_nb_type();
884
885 if (!is_daemon && !is_a_socket(0)) {
886 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
887 is_daemon = True;
888 }
889
890 if (is_daemon && !opt_interactive) {
891 DEBUG( 2, ( "Becoming a daemon.\n" ) );
892 become_daemon(Fork, no_process_group);
893 }
894
895#if HAVE_SETPGID
896 /*
897 * If we're interactive we want to set our own process group for
898 * signal management.
899 */
900 if (opt_interactive && !no_process_group)
901 setpgid( (pid_t)0, (pid_t)0 );
902#endif
903
904 if (nmbd_messaging_context() == NULL) {
905 return 1;
906 }
907
908#ifndef SYNC_DNS
909 /* Setup the async dns. We do it here so it doesn't have all the other
910 stuff initialised and thus chewing memory and sockets */
911 if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
912 start_async_dns();
913 }
914#endif
915
916 if (!directory_exist(lp_lockdir(), NULL)) {
917 mkdir(lp_lockdir(), 0755);
918 }
919
920 pidfile_create("nmbd");
921
922 if (!reinit_after_fork(nmbd_messaging_context(),
923 nmbd_event_context(), false)) {
924 DEBUG(0,("reinit_after_fork() failed\n"));
925 exit(1);
926 }
927
928 /* get broadcast messages */
929 claim_connection(NULL,"",FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP);
930
931 messaging_register(nmbd_messaging_context(), NULL,
932 MSG_FORCE_ELECTION, nmbd_message_election);
933#if 0
934 /* Until winsrepl is done. */
935 messaging_register(nmbd_messaging_context(), NULL,
936 MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry);
937#endif
938 messaging_register(nmbd_messaging_context(), NULL,
939 MSG_SHUTDOWN, nmbd_terminate);
940 messaging_register(nmbd_messaging_context(), NULL,
941 MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services);
942 messaging_register(nmbd_messaging_context(), NULL,
943 MSG_SEND_PACKET, msg_nmbd_send_packet);
944
945 TimeInit();
946
947 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
948
949 if ( !open_sockets( is_daemon, global_nmb_port ) ) {
950 kill_async_dns_child();
951 return 1;
952 }
953
954 /* Determine all the IP addresses we have. */
955 load_interfaces();
956
957 /* Create an nmbd subnet record for each of the above. */
958 if( False == create_subnets() ) {
959 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
960 kill_async_dns_child();
961 exit(1);
962 }
963
964 /* Load in any static local names. */
965 if (p_lmhosts) {
966 set_dyn_LMHOSTSFILE(p_lmhosts);
967 }
968 load_lmhosts_file(get_dyn_LMHOSTSFILE());
969 DEBUG(3,("Loaded hosts file %s\n", get_dyn_LMHOSTSFILE()));
970
971 /* If we are acting as a WINS server, initialise data structures. */
972 if( !initialise_wins() ) {
973 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
974 kill_async_dns_child();
975 exit(1);
976 }
977
978 /*
979 * Register nmbd primary workgroup and nmbd names on all
980 * the broadcast subnets, and on the WINS server (if specified).
981 * Also initiate the startup of our primary workgroup (start
982 * elections if we are setup as being able to be a local
983 * master browser.
984 */
985
986 if( False == register_my_workgroup_and_names() ) {
987 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
988 kill_async_dns_child();
989 exit(1);
990 }
991
992 /* We can only take signals in the select. */
993 BlockSignals( True, SIGTERM );
994
995 TALLOC_FREE(frame);
996 process();
997
998 if (dbf)
999 x_fclose(dbf);
1000 kill_async_dns_child();
1001 return(0);
1002}
Note: See TracBrowser for help on using the repository browser.