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