Cleanups!
[kai/samba.git] / source3 / smbd / server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main SMB server routines
4    Copyright (C) Andrew Tridgell                1992-1998
5    Copyright (C) Martin Pool                    2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern fstring global_myworkgroup;
25 extern pstring global_myname;
26
27 int am_parent = 1;
28
29 /* the last message the was processed */
30 int last_message = -1;
31
32 /* a useful macro to debug the last message processed */
33 #define LAST_MESSAGE() smb_fn_name(last_message)
34
35 extern pstring user_socket_options;
36
37 #ifdef WITH_DFS
38 extern int dcelogin_atmost_once;
39 #endif /* WITH_DFS */
40
41 extern fstring remote_machine;
42
43 /* really we should have a top level context structure that has the
44    client file descriptor as an element. That would require a major rewrite :(
45
46    the following 2 functions are an alternative - they make the file
47    descriptor private to smbd
48  */
49 static int server_fd = -1;
50
51 int smbd_server_fd(void)
52 {
53         return server_fd;
54 }
55
56 static void smbd_set_server_fd(int fd)
57 {
58         server_fd = fd;
59         client_setfd(fd);
60 }
61
62 /****************************************************************************
63  Terminate signal.
64 ****************************************************************************/
65
66 VOLATILE sig_atomic_t got_sig_term = 0;
67
68 static void sig_term(void)
69 {
70         got_sig_term = 1;
71         sys_select_signal();
72 }
73
74 /****************************************************************************
75  Catch a sighup.
76 ****************************************************************************/
77
78 VOLATILE sig_atomic_t reload_after_sighup = 0;
79
80 static void sig_hup(int sig)
81 {
82         reload_after_sighup = 1;
83         sys_select_signal();
84 }
85
86 /****************************************************************************
87   Send a SIGTERM to our process group.
88 *****************************************************************************/
89
90 static void  killkids(void)
91 {
92         if(am_parent) kill(0,SIGTERM);
93 }
94
95 /****************************************************************************
96  Process a sam sync message - not sure whether to do this here or
97  somewhere else.
98 ****************************************************************************/
99
100 static void msg_sam_sync(int UNUSED(msg_type), pid_t UNUSED(pid),
101                          void *UNUSED(buf), size_t UNUSED(len))
102 {
103         DEBUG(10, ("** sam sync message received, ignoring\n"));
104 }
105
106 /****************************************************************************
107  Process a sam sync replicate message - not sure whether to do this here or
108  somewhere else.
109 ****************************************************************************/
110
111 static void msg_sam_repl(int msg_type, pid_t pid, void *buf, size_t len)
112 {
113         uint32 low_serial;
114
115         if (len != sizeof(uint32))
116                 return;
117
118         low_serial = *((uint32 *)buf);
119
120         DEBUG(3, ("received sam replication message, serial = 0x%04x\n",
121                   low_serial));
122 }
123
124 /****************************************************************************
125  Open the socket communication - inetd.
126 ****************************************************************************/
127
128 static BOOL open_sockets_inetd(void)
129 {
130         /* Started from inetd. fd 0 is the socket. */
131         /* We will abort gracefully when the client or remote system 
132            goes away */
133         smbd_set_server_fd(dup(0));
134         
135         /* close our standard file descriptors */
136         close_low_fds();
137         
138         set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
139         set_socket_options(smbd_server_fd(), user_socket_options);
140
141         return True;
142 }
143
144 static void msg_exit_server(int msg_type, pid_t src, void *buf, size_t len)
145 {
146         exit_server("Got a SHUTDOWN message");
147 }
148
149
150 /****************************************************************************
151  Open the socket communication.
152 ****************************************************************************/
153
154 static BOOL open_sockets(BOOL is_daemon,int port)
155 {
156         int num_interfaces = iface_count();
157         int fd_listenset[FD_SETSIZE];
158         fd_set listen_set;
159         int s;
160         int i;
161
162         if (!is_daemon) {
163                 return open_sockets_inetd();
164         }
165
166                 
167 #ifdef HAVE_ATEXIT
168         {
169                 static int atexit_set;
170                 if(atexit_set == 0) {
171                         atexit_set=1;
172                         atexit(killkids);
173                 }
174         }
175 #endif
176
177         /* Stop zombies */
178         CatchChild();
179                 
180                 
181         FD_ZERO(&listen_set);
182
183         if(lp_interfaces() && lp_bind_interfaces_only()) {
184                 /* We have been given an interfaces line, and been 
185                    told to only bind to those interfaces. Create a
186                    socket per interface and bind to only these.
187                 */
188                 
189                 if(num_interfaces > FD_SETSIZE) {
190                         DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
191 max can be %d\n", 
192                                  num_interfaces, FD_SETSIZE));
193                         return False;
194                 }
195                 
196                 /* Now open a listen socket for each of the
197                    interfaces. */
198                 for(i = 0; i < num_interfaces; i++) {
199                         struct in_addr *ifip = iface_n_ip(i);
200                         
201                         if(ifip == NULL) {
202                                 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
203                                 continue;
204                         }
205                         s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
206                         if(s == -1)
207                                 return False;
208
209                         /* ready to listen */
210                         set_socket_options(s,"SO_KEEPALIVE"); 
211                         set_socket_options(s,user_socket_options);
212       
213                         if (listen(s, 5) == -1) {
214                                 DEBUG(0,("listen: %s\n",strerror(errno)));
215                                 close(s);
216                                 return False;
217                         }
218                         FD_SET(s,&listen_set);
219                 }
220         } else {
221                 /* Just bind to 0.0.0.0 - accept connections
222                    from anywhere. */
223                 num_interfaces = 1;
224                 
225                 /* open an incoming socket */
226                 s = open_socket_in(SOCK_STREAM, port, 0,
227                                    interpret_addr(lp_socket_address()),True);
228                 if (s == -1)
229                         return(False);
230                 
231                 /* ready to listen */
232                 set_socket_options(s,"SO_KEEPALIVE"); 
233                 set_socket_options(s,user_socket_options);
234
235                 if (listen(s, 5) == -1) {
236                         DEBUG(0,("open_sockets: listen: %s\n",
237                                  strerror(errno)));
238                         close(s);
239                         return False;
240                 }
241                 
242                 fd_listenset[0] = s;
243                 FD_SET(s,&listen_set);
244         } 
245
246         /* Listen to messages */
247
248         message_register(MSG_SMB_SAM_SYNC, msg_sam_sync);
249         message_register(MSG_SMB_SAM_REPL, msg_sam_repl);
250         message_register(MSG_SHUTDOWN, msg_exit_server);
251
252         /* now accept incoming connections - forking a new process
253            for each incoming connection */
254         DEBUG(2,("waiting for a connection\n"));
255         while (1) {
256                 fd_set lfds;
257                 int num;
258                 
259                 /* Free up temporary memory from the main smbd. */
260                 lp_talloc_free();
261
262                 /* Ensure we respond to PING and DEBUG messages from the main smbd. */
263                 message_dispatch();
264
265                 memcpy((char *)&lfds, (char *)&listen_set, 
266                        sizeof(listen_set));
267                 
268                 num = sys_select(FD_SETSIZE,&lfds,NULL,NULL,NULL);
269                 
270                 if (num == -1 && errno == EINTR) {
271                         if (got_sig_term) {
272                                 exit_server("Caught TERM signal");
273                         }
274
275                         /* check for sighup processing */
276                         if (reload_after_sighup) {
277                                 change_to_root_user();
278                                 DEBUG(1,("Reloading services after SIGHUP\n"));
279                                 reload_services(False);
280                                 reload_after_sighup = 0;
281                         }
282
283                         continue;
284                 }
285                 
286                 /* check if we need to reload services */
287                 check_reload(time(NULL));
288
289                 /* Find the sockets that are read-ready -
290                    accept on these. */
291                 for( ; num > 0; num--) {
292                         struct sockaddr addr;
293                         socklen_t in_addrlen = sizeof(addr);
294                         
295                         s = -1;
296                         for(i = 0; i < num_interfaces; i++) {
297                                 if(FD_ISSET(fd_listenset[i],&lfds)) {
298                                         s = fd_listenset[i];
299                                         /* Clear this so we don't look
300                                            at it again. */
301                                         FD_CLR(fd_listenset[i],&lfds);
302                                         break;
303                                 }
304                         }
305
306                         smbd_set_server_fd(accept(s,&addr,&in_addrlen));
307                         
308                         if (smbd_server_fd() == -1 && errno == EINTR)
309                                 continue;
310                         
311                         if (smbd_server_fd() == -1) {
312                                 DEBUG(0,("open_sockets: accept: %s\n",
313                                          strerror(errno)));
314                                 continue;
315                         }
316                         
317                         if (smbd_server_fd() != -1 && sys_fork()==0) {
318                                 /* Child code ... */
319                                 
320                                 /* close the listening socket(s) */
321                                 for(i = 0; i < num_interfaces; i++)
322                                         close(fd_listenset[i]);
323                                 
324                                 /* close our standard file
325                                    descriptors */
326                                 close_low_fds();
327                                 am_parent = 0;
328                                 
329                                 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
330                                 set_socket_options(smbd_server_fd(),user_socket_options);
331                                 
332                                 /* Reset global variables in util.c so
333                                    that client substitutions will be
334                                    done correctly in the process.  */
335                                 reset_globals_after_fork();
336
337                                 /* tdb needs special fork handling */
338                                 tdb_reopen_all();
339
340                                 return True; 
341                         }
342                         /* The parent doesn't need this socket */
343                         close(smbd_server_fd()); 
344
345                         /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
346                                 Clear the closed fd info out of server_fd --
347                                 and more importantly, out of client_fd in
348                                 util_sock.c, to avoid a possible
349                                 getpeername failure if we reopen the logs
350                                 and use %I in the filename.
351                         */
352
353                         smbd_set_server_fd(-1);
354
355                         /* Force parent to check log size after
356                          * spawning child.  Fix from
357                          * klausr@ITAP.Physik.Uni-Stuttgart.De.  The
358                          * parent smbd will log to logserver.smb.  It
359                          * writes only two messages for each child
360                          * started/finished. But each child writes,
361                          * say, 50 messages also in logserver.smb,
362                          * begining with the debug_count of the
363                          * parent, before the child opens its own log
364                          * file logserver.client. In a worst case
365                          * scenario the size of logserver.smb would be
366                          * checked after about 50*50=2500 messages
367                          * (ca. 100kb).
368                          * */
369                         force_check_log_size();
370  
371                 } /* end for num */
372         } /* end while 1 */
373
374 /* NOTREACHED   return True; */
375 }
376
377 /****************************************************************************
378  Reload the services file.
379 **************************************************************************/
380
381 BOOL reload_services(BOOL test)
382 {
383         BOOL ret;
384         
385         if (lp_loaded()) {
386                 pstring fname;
387                 pstrcpy(fname,lp_configfile());
388                 if (file_exist(fname, NULL) &&
389                     !strcsequal(fname, dyn_CONFIGFILE)) {
390                         pstrcpy(dyn_CONFIGFILE, fname);
391                         test = False;
392                 }
393         }
394
395         reopen_logs();
396
397         if (test && !lp_file_list_changed())
398                 return(True);
399
400         lp_killunused(conn_snum_used);
401         
402         ret = lp_load(dyn_CONFIGFILE, False, False, True);
403
404         load_printers();
405
406         /* perhaps the config filename is now set */
407         if (!test)
408                 reload_services(True);
409
410         reopen_logs();
411
412         load_interfaces();
413
414         {
415                 if (smbd_server_fd() != -1) {      
416                         set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
417                         set_socket_options(smbd_server_fd(), user_socket_options);
418                 }
419         }
420
421         mangle_reset_cache();
422         reset_stat_cache();
423
424         /* this forces service parameters to be flushed */
425         set_current_service(NULL,True);
426
427         return(ret);
428 }
429
430 #if DUMP_CORE
431 /*******************************************************************
432 prepare to dump a core file - carefully!
433 ********************************************************************/
434 static BOOL dump_core(void)
435 {
436         char *p;
437         pstring dname;
438         
439         pstrcpy(dname,lp_logfile());
440         if ((p=strrchr_m(dname,'/'))) *p=0;
441         pstrcat(dname,"/corefiles");
442         mkdir(dname,0700);
443         sys_chown(dname,getuid(),getgid());
444         chmod(dname,0700);
445         if (chdir(dname)) return(False);
446         umask(~(0700));
447
448 #ifdef HAVE_GETRLIMIT
449 #ifdef RLIMIT_CORE
450         {
451                 struct rlimit rlp;
452                 getrlimit(RLIMIT_CORE, &rlp);
453                 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
454                 setrlimit(RLIMIT_CORE, &rlp);
455                 getrlimit(RLIMIT_CORE, &rlp);
456                 DEBUG(3,("Core limits now %d %d\n",
457                          (int)rlp.rlim_cur,(int)rlp.rlim_max));
458         }
459 #endif
460 #endif
461
462
463         DEBUG(0,("Dumping core in %s\n", dname));
464         abort();
465         return(True);
466 }
467 #endif
468
469 /****************************************************************************
470 update the current smbd process count
471 ****************************************************************************/
472
473 static void decrement_smbd_process_count(void)
474 {
475         int32 total_smbds;
476
477         if (lp_max_smbd_processes()) {
478                 total_smbds = 0;
479                 tdb_change_int32_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds, -1);
480         }
481 }
482
483 /****************************************************************************
484  Exit the server.
485 ****************************************************************************/
486
487 void exit_server(char *reason)
488 {
489         static int firsttime=1;
490         extern char *last_inbuf;
491         extern struct auth_context *negprot_global_auth_context;
492
493         if (!firsttime)
494                 exit(0);
495         firsttime = 0;
496
497         change_to_root_user();
498         DEBUG(2,("Closing connections\n"));
499
500         if (negprot_global_auth_context) {
501                 (negprot_global_auth_context->free)(&negprot_global_auth_context);
502         }
503
504         conn_close_all();
505
506         invalidate_all_vuids();
507
508         /* delete our entry in the connections database. */
509         yield_connection(NULL,"");
510
511         respond_to_all_remaining_local_messages();
512         decrement_smbd_process_count();
513
514 #ifdef WITH_DFS
515         if (dcelogin_atmost_once) {
516                 dfs_unlogin();
517         }
518 #endif
519
520         if (!reason) {   
521                 int oldlevel = DEBUGLEVEL;
522                 DEBUGLEVEL = 10;
523                 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
524                 if (last_inbuf)
525                         show_msg(last_inbuf);
526                 DEBUGLEVEL = oldlevel;
527                 DEBUG(0,("===============================================================\n"));
528 #if DUMP_CORE
529                 if (dump_core()) return;
530 #endif
531         }    
532
533         locking_end();
534
535         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
536         exit(0);
537 }
538
539 /****************************************************************************
540  Initialise connect, service and file structs.
541 ****************************************************************************/
542
543 static void init_structs(void )
544 {
545         /*
546          * Set the machine NETBIOS name if not already
547          * set from the config file.
548          */
549
550         if (!*global_myname) {
551                 char *p;
552                 pstrcpy( global_myname, myhostname() );
553                 p = strchr_m(global_myname, '.' );
554                 if (p) 
555                         *p = 0;
556         }
557
558         strupper(global_myname);
559
560         conn_init();
561
562         file_init();
563
564         /* for RPC pipes */
565         init_rpc_pipe_hnd();
566
567         init_dptrs();
568
569         secrets_init();
570
571 }
572
573 /****************************************************************************
574  Usage on the program.
575 ****************************************************************************/
576
577 static void usage(char *pname)
578 {
579
580         d_printf("Usage: %s [-DaioPh?Vb] [-d debuglevel] [-l log basename] [-p port]\n", pname);
581         d_printf("       [-O socket options] [-s services file]\n");
582         d_printf("\t-D                    Become a daemon (default)\n");
583         d_printf("\t-a                    Append to log file (default)\n");
584         d_printf("\t-i                    Run interactive (not a daemon)\n" );
585         d_printf("\t-o                    Overwrite log file, don't append\n");
586         d_printf("\t-h                    Print usage\n");
587         d_printf("\t-?                    Print usage\n");
588         d_printf("\t-V                    Print version\n");
589         d_printf("\t-b                    Print build options\n");
590         d_printf("\t-d debuglevel         Set the debuglevel\n");
591         d_printf("\t-l log basename.      Basename for log/debug files\n");
592         d_printf("\t-p port               Listen on the specified port\n");
593         d_printf("\t-O socket options     Socket options\n");
594         d_printf("\t-s services file.     Filename of services file\n");
595         d_printf("\n");
596 }
597
598 /****************************************************************************
599  main program.
600 ****************************************************************************/
601
602  int main(int argc,char *argv[])
603 {
604         extern BOOL append_log;
605         extern BOOL AllowDebugChange;
606         extern char *optarg;
607         /* shall I run as a daemon */
608         BOOL is_daemon = False;
609         BOOL interactive = False;
610         BOOL specified_logfile = False;
611         int port = SMB_PORT;
612         int opt;
613         pstring logfile;
614
615 #ifdef HAVE_SET_AUTH_PARAMETERS
616         set_auth_parameters(argc,argv);
617 #endif
618
619         /* this is for people who can't start the program correctly */
620         while (argc > 1 && (*argv[1] != '-')) {
621                 argv++;
622                 argc--;
623         }
624
625         while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?bVaiof:")) )
626                 switch (opt)  {
627                 case 'O':
628                         pstrcpy(user_socket_options,optarg);
629                         break;
630
631                 case 's':
632                         pstrcpy(dyn_CONFIGFILE,optarg);
633                         break;
634
635                 case 'l':
636                         specified_logfile = True;
637                         pstr_sprintf(logfile, "%s/log.smbd", optarg);
638                         lp_set_logfile(logfile);
639                         break;
640
641                 case 'a':
642                         append_log = True;
643                         break;
644
645                 case 'i':
646                         interactive = True;
647                         break;
648
649                 case 'o':
650                         append_log = False;
651                         break;
652
653                 case 'D':
654                         is_daemon = True;
655                         break;
656
657                 case 'd':
658                         if (*optarg == 'A')
659                                 DEBUGLEVEL = 10000;
660                         else
661                                 DEBUGLEVEL = atoi(optarg);
662                         AllowDebugChange = False;
663                         break;
664
665                 case 'p':
666                         port = atoi(optarg);
667                         break;
668
669                 case 'h':
670                 case '?':
671                         usage(argv[0]);
672                         exit(0);
673                         break;
674
675                 case 'V':
676                         d_printf("Version %s\n",VERSION);
677                         exit(0);
678                         break;
679                 case 'b':
680                         build_options(True); /* Display output to screen as well as debug */ 
681                         exit(0);
682                         break;
683                 default:
684                         DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
685                         usage(argv[0]);
686                         exit(1);
687                 }
688
689 #ifdef HAVE_SETLUID
690         /* needed for SecureWare on SCO */
691         setluid(0);
692 #endif
693
694         sec_init();
695
696         load_case_tables();
697
698         append_log = True;
699
700         if(!specified_logfile) {
701                 pstr_sprintf(logfile, "%s/log.smbd", dyn_LOGFILEBASE);
702                 lp_set_logfile(logfile);
703         }
704
705         fstrcpy(remote_machine, "smbd");
706
707         setup_logging(argv[0],interactive);
708
709         /* we want to re-seed early to prevent time delays causing
710            client problems at a later date. (tridge) */
711         generate_random_buffer(NULL, 0, False);
712
713         /* make absolutely sure we run as root - to handle cases where people
714            are crazy enough to have it setuid */
715
716         gain_root_privilege();
717         gain_root_group_privilege();
718
719         fault_setup((void (*)(void *))exit_server);
720         CatchSignal(SIGTERM , SIGNAL_CAST sig_term);
721         CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
722         
723         /* we are never interested in SIGPIPE */
724         BlockSignals(True,SIGPIPE);
725
726 #if defined(SIGFPE)
727         /* we are never interested in SIGFPE */
728         BlockSignals(True,SIGFPE);
729 #endif
730
731 #if defined(SIGUSR2)
732         /* We are no longer interested in USR2 */
733         BlockSignals(True,SIGUSR2);
734 #endif
735
736         /* POSIX demands that signals are inherited. If the invoking process has
737          * these signals masked, we will have problems, as we won't recieve them. */
738         BlockSignals(False, SIGHUP);
739         BlockSignals(False, SIGUSR1);
740         BlockSignals(False, SIGTERM);
741
742         /* we want total control over the permissions on created files,
743            so set our umask to 0 */
744         umask(0);
745
746         init_sec_ctx();
747
748         reopen_logs();
749
750         DEBUG(0,( "smbd version %s started.\n", VERSION));
751         DEBUGADD(0,( "Copyright Andrew Tridgell and the Samba Team 1992-2002\n"));
752
753         DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
754                  (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
755
756         /* Output the build options to the debug log */ 
757         build_options(False);
758
759         if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
760                 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
761                 exit(1);
762         }
763
764         /*
765          * Do this before reload_services.
766          */
767
768         if (!reload_services(False))
769                 return(-1);     
770
771         init_structs();
772
773         /* don't call winbind for our domain if we are the DC */
774         if (lp_domain_logons()) {
775                 winbind_exclude_domain(lp_workgroup());
776         }
777         
778 #ifdef WITH_PROFILE
779         if (!profile_setup(False)) {
780                 DEBUG(0,("ERROR: failed to setup profiling\n"));
781                 return -1;
782         }
783 #endif
784
785         fstrcpy(global_myworkgroup, lp_workgroup());
786
787         DEBUG(3,( "loaded services\n"));
788
789         if (!is_daemon && !is_a_socket(0)) {
790                 if (!interactive)
791                         DEBUG(0,("standard input is not a socket, assuming -D option\n"));
792
793                 /*
794                  * Setting is_daemon here prevents us from eventually calling
795                  * the open_sockets_inetd()
796                  */
797
798                 is_daemon = True;
799         }
800
801         if (is_daemon && !interactive) {
802                 DEBUG( 3, ( "Becoming a daemon.\n" ) );
803                 become_daemon();
804         }
805
806 #if HAVE_SETPGID
807         /*
808          * If we're interactive we want to set our own process group for
809          * signal management.
810          */
811         if (interactive)
812                 setpgid( (pid_t)0, (pid_t)0);
813 #endif
814
815         if (!directory_exist(lp_lockdir(), NULL)) {
816                 mkdir(lp_lockdir(), 0755);
817         }
818
819         if (is_daemon) {
820                 pidfile_create("smbd");
821         }
822
823         if (!message_init()) {
824                 exit(1);
825         }
826         register_msg_pool_usage();
827         register_dmalloc_msgs();
828
829         /* Setup the main smbd so that we can get messages. */
830         claim_connection(NULL,"",0,True);
831
832         /* 
833            DO NOT ENABLE THIS TILL YOU COPE WITH KILLING THESE TASKS AND INETD
834            THIS *killed* LOTS OF BUILD FARM MACHINES. IT CREATED HUNDREDS OF 
835            smbd PROCESSES THAT NEVER DIE
836            start_background_queue(); 
837         */
838
839         if (!open_sockets(is_daemon,port))
840                 exit(1);
841
842         /*
843          * everything after this point is run after the fork()
844          */ 
845
846         if (!locking_init(0))
847                 exit(1);
848
849         if (!print_backend_init())
850                 exit(1);
851
852         if (!share_info_db_init())
853                 exit(1);
854
855         if(!initialize_password_db(False))
856                 exit(1);
857
858         uni_group_cache_init(); /* Non-critical */
859         
860         /* possibly reload the services file. */
861         reload_services(True);
862
863         if(!pdb_generate_sam_sid()) {
864                 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
865                 exit(1);
866         }
867
868         if (!init_account_policy()) {
869                 DEBUG(0,("Could not open account policy tdb.\n"));
870                 exit(1);
871         }
872
873         if (*lp_rootdir()) {
874                 if (sys_chroot(lp_rootdir()) == 0)
875                         DEBUG(2,("Changed root to %s\n", lp_rootdir()));
876         }
877
878         /* Setup oplocks */
879         if (!init_oplocks())
880                 exit(1);
881         
882         /* Setup change notify */
883         if (!init_change_notify())
884                 exit(1);
885
886         smbd_process();
887         
888         uni_group_cache_shutdown();
889         exit_server("normal exit");
890         return(0);
891 }