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>
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.
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.
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.
28 called on a fatal error that should cause this server to terminate
30 void exit_server(struct server_context *smb, const char *reason)
32 smb->model_ops->terminate_connection(smb, reason);
37 add a socket address to the list of events, one event per port
39 static void add_socket(struct event_context *events,
40 struct model_ops *model_ops,
43 char *ports = lp_smb_ports();
45 const char *delim = ", ";
47 for (tok=strtok_r(ports, delim, &ptr);
49 tok=strtok_r(NULL, delim, &ptr)) {
50 unsigned port = atoi(tok);
53 if (port == 0) continue;
55 fde.fd = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
57 DEBUG(0,("Failed to open socket on %s:%u - %s\n",
58 inet_ntoa(*ifip), port, strerror(errno)));
63 set_socket_options(fde.fd, "SO_KEEPALIVE");
64 set_socket_options(fde.fd, lp_socket_options());
66 if (listen(fde.fd, 10) == -1) {
67 DEBUG(0,("Failed to listen on %s:%d - %s\n",
68 inet_ntoa(*ifip), port, strerror(errno)));
73 /* we are only interested in read events on the listen socket */
74 fde.flags = EVENT_FD_READ;
75 fde.private = model_ops;
76 fde.handler = model_ops->accept_connection;
78 event_add_fd(events, &fde);
82 /****************************************************************************
83 Open the socket communication.
84 ****************************************************************************/
85 static void open_sockets_smbd(struct event_context *events,
86 struct model_ops *model_ops)
88 if (lp_interfaces() && lp_bind_interfaces_only()) {
89 int num_interfaces = iface_count();
92 /* We have been given an interfaces line, and been
93 told to only bind to those interfaces. Create a
94 socket per interface and bind to only these.
96 for(i = 0; i < num_interfaces; i++) {
97 struct in_addr *ifip = iface_n_ip(i);
100 DEBUG(0,("open_sockets_smbd: interface %d has NULL IP address !\n", i));
104 add_socket(events, model_ops, ifip);
107 TALLOC_CTX *mem_ctx = talloc_init("open_sockets_smbd");
109 struct in_addr *ifip = interpret_addr2(mem_ctx, lp_socket_address());
110 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
112 smb_panic("No memory");
114 add_socket(events, model_ops, ifip);
115 talloc_destroy(mem_ctx);
119 /****************************************************************************
120 Reload the services file.
121 **************************************************************************/
122 BOOL reload_services(struct server_context *smb, BOOL test)
128 pstrcpy(fname,lp_configfile());
129 if (file_exist(fname, NULL) &&
130 !strcsequal(fname, dyn_CONFIGFILE)) {
131 pstrcpy(dyn_CONFIGFILE, fname);
138 if (test && !lp_file_list_changed())
142 lp_killunused(smb, conn_snum_used);
145 ret = lp_load(dyn_CONFIGFILE, False, False, True);
149 /* perhaps the config filename is now set */
151 reload_services(smb, True);
157 mangle_reset_cache();
160 /* this forces service parameters to be flushed */
161 set_current_service(NULL,True);
166 /****************************************************************************
167 Initialise connect, service and file structs.
168 ****************************************************************************/
169 static BOOL init_structs(void)
176 /* we want to re-seed early to prevent time delays causing
177 client problems at a later date. (tridge) */
178 generate_random_buffer(NULL, 0, False);
185 setup the events for the chosen process model
187 static void setup_process_model(struct event_context *events,
190 struct model_ops *ops;
192 process_model_init();
194 ops = process_model_byname(model);
196 DEBUG(0,("Unknown process model '%s'\n", model));
200 ops->model_startup();
202 /* now setup the listening sockets, adding
203 event handlers to the events structure */
204 open_sockets_smbd(events, ops);
207 /****************************************************************************
209 ****************************************************************************/
210 int main(int argc,const char *argv[])
212 BOOL is_daemon = False;
213 BOOL interactive = False;
215 BOOL log_stdout = False;
218 struct event_context *events;
219 const char *model = "standard";
220 struct poptOption long_options[] = {
222 {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" },
223 {"interactive", 'i', POPT_ARG_VAL, &interactive, True, "Run interactive (not a daemon)"},
224 {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },
225 {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
226 {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },
227 {"port", 'p', POPT_ARG_STRING, NULL, 0, "Listen on the specified ports"},
228 {"model", 'M', POPT_ARG_STRING, &model, 0, "select process model"},
233 pc = poptGetContext("smbd", argc, argv, long_options, 0);
235 while((opt = poptGetNextOpt(pc)) != -1) {
238 /* Display output to screen as well as debug */
243 lp_set_cmdline("smb ports", poptGetOptArg(pc));
249 events = event_context_init();
258 if (log_stdout && Fork) {
259 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
262 setup_logging(argv[0], log_stdout);
264 fault_setup((void (*)(void *))exit_server);
266 /* we are never interested in SIGPIPE */
267 BlockSignals(True,SIGPIPE);
270 /* we are never interested in SIGFPE */
271 BlockSignals(True,SIGFPE);
275 /* We are no longer interested in USR2 */
276 BlockSignals(True,SIGUSR2);
279 /* POSIX demands that signals are inherited. If the invoking process has
280 * these signals masked, we will have problems, as we won't recieve them. */
281 BlockSignals(False, SIGHUP);
282 BlockSignals(False, SIGUSR1);
283 BlockSignals(False, SIGTERM);
285 /* we want total control over the permissions on created files,
286 so set our umask to 0 */
291 DEBUG(0,("smbd version %s started.\n", SAMBA_VERSION));
292 DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2003\n"));
294 /* Output the build options to the debug log */
295 build_options(False);
297 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
298 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
301 DEBUG(0,("Using %s process model\n", model));
303 if (!reload_services(NULL, False))
308 if (!is_daemon && !is_a_socket(0)) {
310 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
313 * Setting is_daemon here prevents us from eventually calling
314 * the open_sockets_inetd()
320 if (is_daemon && !interactive) {
321 DEBUG(3,("Becoming a daemon.\n"));
325 if (!directory_exist(lp_lockdir(), NULL)) {
326 mkdir(lp_lockdir(), 0755);
330 pidfile_create("smbd");
333 register_msg_pool_usage();
334 register_dmalloc_msgs();
336 setup_process_model(events, model);
338 /* wait for events */
339 return event_loop_wait(events);