summaryrefslogtreecommitdiff
path: root/source4/smbd/server.c
blob: f5f37a55cfbd1e10963c2ba84880c627858d7608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* 
   Unix SMB/CIFS implementation.

   Main SMB server routines

   Copyright (C) Andrew Tridgell		1992-2005
   Copyright (C) Martin Pool			2002
   Copyright (C) Jelmer Vernooij		2002
   Copyright (C) James J Myers 			2003 <myersjj@samba.org>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/events/events.h"
#include "version.h"
#include "dynconfig.h"
#include "lib/cmdline/popt_common.h"
#include "system/dir.h"
#include "system/filesys.h"


/*
  recursively delete a directory tree
*/
static void recursive_delete(const char *path)
{
	DIR *dir;
	struct dirent *de;

	dir = opendir(path);
	if (!dir) {
		return;
	}

	for (de=readdir(dir);de;de=readdir(dir)) {
		char *fname;
		struct stat st;

		if (strcmp(de->d_name, ".") == 0 ||
		    strcmp(de->d_name, "..") == 0) {
			continue;
		}

		fname = talloc_asprintf(path, "%s/%s", path, de->d_name);
		if (stat(fname, &st) != 0) {
			continue;
		}
		if (S_ISDIR(st.st_mode)) {
			recursive_delete(fname);
			talloc_free(fname);
			continue;
		}
		if (unlink(fname) != 0) {
			DEBUG(0,("Unabled to delete '%s' - %s\n", 
				 fname, strerror(errno)));
			smb_panic("unable to cleanup tmp files");
		}
		talloc_free(fname);
	}
	closedir(dir);
}

/*
  cleanup temporary files. This is the new alternative to
  TDB_CLEAR_IF_FIRST. Unfortunately TDB_CLEAR_IF_FIRST is not
  efficient on unix systems due to the lack of scaling of the byte
  range locking system. So instead of putting the burden on tdb to
  cleanup tmp files, this function deletes them. 
*/
static void cleanup_tmp_files(void)
{
	char *path;
	TALLOC_CTX *mem_ctx = talloc_new(NULL);

	path = smbd_tmp_path(mem_ctx, NULL);

	recursive_delete(path);
	talloc_free(mem_ctx);
}

/*
  setup signal masks
*/
static void setup_signals(void)
{
	fault_setup(NULL);
	
	/* we are never interested in SIGPIPE */
	BlockSignals(True,SIGPIPE);

#if defined(SIGFPE)
	/* we are never interested in SIGFPE */
	BlockSignals(True,SIGFPE);
#endif

#if defined(SIGUSR2)
	/* We are no longer interested in USR2 */
	BlockSignals(True,SIGUSR2);
#endif

	/* POSIX demands that signals are inherited. If the invoking process has
	 * these signals masked, we will have problems, as we won't recieve them. */
	BlockSignals(False, SIGHUP);
	BlockSignals(False, SIGUSR1);
	BlockSignals(False, SIGTERM);
}


/*
  handle io on stdin
*/
static void server_stdin_handler(struct event_context *event_ctx, struct fd_event *fde, 
				 uint16_t flags, void *private)
{
	uint8_t c;
	if (read(0, &c, 1) == 0) {
		DEBUG(0,("smbd: EOF on stdin - terminating\n"));
		exit(0);
	}
}


/*
  die if the user selected maximum runtime is exceeded
*/
static void max_runtime_handler(struct event_context *ev, struct timed_event *te, 
				struct timeval t, void *private)
{
	DEBUG(0,("smbd maximum runtime exceeded - terminating\n"));
	exit(0);
}


/*
 main server.
*/
static int binary_smbd_main(int argc, const char *argv[])
{
	BOOL interactive = False;
	int opt;
	poptContext pc;
	struct event_context *event_ctx;
	NTSTATUS status;
	const char *model = "standard";
	int max_runtime = 0;
	struct poptOption long_options[] = {
		POPT_AUTOHELP
		{"interactive", 'i', POPT_ARG_VAL, &interactive, True, 
		 "Run interactive (not a daemon)", NULL},
		{"model", 'M', POPT_ARG_STRING, &model, True, 
		 "Select process model", "MODEL"},
		{"maximum-runtime", 0, POPT_ARG_INT, &max_runtime, True, 
		 "set maximum time for smbd to live", "seconds"},
		POPT_COMMON_SAMBA
		POPT_COMMON_VERSION
		POPT_TABLEEND
	};
	
	pc = poptGetContext("smbd", argc, argv, long_options, 0);
	
	while((opt = poptGetNextOpt(pc)) != -1) /* noop */ ;

	poptFreeContext(pc);

	setup_logging(NULL, interactive?DEBUG_STDOUT:DEBUG_FILE);
	setup_signals();

	/* we want total control over the permissions on created files,
	   so set our umask to 0 */
	umask(0);

	DEBUG(0,("smbd version %s started.\n", SAMBA_VERSION_STRING));
	DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2005\n"));

	if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4 || sizeof(uint64_t) < 8) {
		DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
		exit(1);
	}

	if (!interactive) {
		DEBUG(3,("Becoming a daemon.\n"));
		become_daemon(True);
	}

	cleanup_tmp_files();

	if (!directory_exist(lp_lockdir())) {
		mkdir(lp_lockdir(), 0755);
	}

	pidfile_create("smbd");

	/* Do *not* remove this, until you have removed
	 * passdb/secrets.c, and proved that Samba still builds... */
	/* Setup the SECRETS subsystem */
	if (!secrets_init()) {
		exit(1);
	}

	smbd_init_subsystems;

	/* the event context is the top level structure in smbd. Everything else
	   should hang off that */
	event_ctx = event_context_init(NULL);

	if (interactive) {
		/* catch EOF on stdin */
#ifdef SIGTTIN
		signal(SIGTTIN, SIG_IGN);
#endif
		event_add_fd(event_ctx, event_ctx, 0, EVENT_FD_READ, 
			     server_stdin_handler, NULL);
	}


	if (max_runtime) {
		event_add_timed(event_ctx, event_ctx, 
				timeval_current_ofs(max_runtime, 0), 
				max_runtime_handler, NULL);
	}

	DEBUG(0,("Using %s process model\n", model));
	status = server_service_startup(event_ctx, model, lp_server_services());
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("Starting Services failed - %s\n", nt_errstr(status)));
		return 1;
	}

	/* wait for events - this is where smbd sits for most of its
	   life */
	event_loop_wait(event_ctx);

	/* as everything hangs off this event context, freeing it
	   should initiate a clean shutdown of all services */
	talloc_free(event_ctx);

	return 0;
}

 int main(int argc, const char *argv[])
{
	return binary_smbd_main(argc, argv);
}