r4819: its just not my day today ....
[kai/samba-autobuild/.git] / source4 / 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    Copyright (C) James J Myers                  2003 <myersjj@samba.org>
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "version.h"
26 #include "lib/cmdline/popt_common.h"
27
28 /****************************************************************************
29  main server.
30 ****************************************************************************/
31 static int binary_smbd_main(int argc,const char *argv[])
32 {
33         BOOL is_daemon = False;
34         BOOL interactive = False;
35         BOOL Fork = True;
36         BOOL log_stdout = False;
37         int opt;
38         poptContext pc;
39         struct server_context *server;
40         const char *model = "standard";
41         struct poptOption long_options[] = {
42         POPT_AUTOHELP
43         POPT_COMMON_SAMBA
44         {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" , NULL },
45         {"interactive", 'i', POPT_ARG_VAL, &interactive, True, "Run interactive (not a daemon)", NULL},
46         {"foreground", 'F', POPT_ARG_VAL, &Fork, True, "Run daemon in foreground (for daemontools & etc)" , NULL },
47         {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout", NULL },
48         {"port", 'p', POPT_ARG_STRING, NULL, 0, "Listen on the specified ports", "PORTS"},
49         {"model", 'M', POPT_ARG_STRING, &model, True, "Select process model", "MODEL"},
50         POPT_COMMON_VERSION
51         POPT_TABLEEND
52         };
53         
54         pc = poptGetContext("smbd", argc, argv, long_options, 0);
55         
56         while((opt = poptGetNextOpt(pc)) != -1) {
57                 switch (opt)  {
58                 case 'p':
59                         lp_set_cmdline("smb ports", poptGetOptArg(pc));
60                         break;
61                 }
62         }
63         poptFreeContext(pc);
64
65         if (interactive) {
66                 Fork = False;
67                 log_stdout = True;
68         }
69
70         if (log_stdout && Fork) {
71                 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
72                 exit(1);
73         }
74         setup_logging(argv[0], log_stdout?DEBUG_STDOUT:DEBUG_FILE);
75
76         fault_setup(NULL);
77         
78         /* we are never interested in SIGPIPE */
79         BlockSignals(True,SIGPIPE);
80
81 #if defined(SIGFPE)
82         /* we are never interested in SIGFPE */
83         BlockSignals(True,SIGFPE);
84 #endif
85
86 #if defined(SIGUSR2)
87         /* We are no longer interested in USR2 */
88         BlockSignals(True,SIGUSR2);
89 #endif
90
91         /* POSIX demands that signals are inherited. If the invoking process has
92          * these signals masked, we will have problems, as we won't recieve them. */
93         BlockSignals(False, SIGHUP);
94         BlockSignals(False, SIGUSR1);
95         BlockSignals(False, SIGTERM);
96
97         /* we want total control over the permissions on created files,
98            so set our umask to 0 */
99         umask(0);
100
101         reopen_logs();
102
103         DEBUG(0,("smbd version %s started.\n", SAMBA_VERSION_STRING));
104         DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2005\n"));
105
106         if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4 || sizeof(uint64_t) < 8) {
107                 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
108                 exit(1);
109         }
110
111         if (!reload_services(NULL, False))
112                 return(-1);     
113
114         if (!is_daemon && !is_a_socket(0)) {
115                 if (!interactive)
116                         DEBUG(0,("standard input is not a socket, assuming -D option\n"));
117
118                 /*
119                  * Setting is_daemon here prevents us from eventually calling
120                  * the open_sockets_inetd()
121                  */
122
123                 is_daemon = True;
124         }
125
126         if (is_daemon && !interactive) {
127                 DEBUG(3,("Becoming a daemon.\n"));
128                 become_daemon(Fork);
129         }
130
131         if (!directory_exist(lp_lockdir(), NULL)) {
132                 mkdir(lp_lockdir(), 0755);
133         }
134
135         if (is_daemon) {
136                 pidfile_create("smbd");
137         }
138
139         init_subsystems();
140
141         smbd_process_init();
142
143         DEBUG(0,("Using %s process model\n", model));
144         server = server_service_startup(model, lp_server_services());
145         if (!server) {
146                 DEBUG(0,("Starting Services failed.\n"));
147                 return 1;
148         }
149
150         /* wait for events */
151         event_loop_wait(server->event.ctx);
152
153         server_service_shutdown(server, "exit");
154
155         return 0;
156 }
157
158  int main(int argc, const char *argv[])
159 {
160         return binary_smbd_main(argc, argv);
161 }