dcerpc.idl Add symbolic constant for /root/ncalrpc_as_system
[Samba.git] / source3 / smbd / scavenger.c
blob9b479a621e3b146cfed307751b98e95812e5abf0
1 /*
2 Unix SMB/CIFS implementation.
3 smbd scavenger daemon
5 Copyright (C) Gregor Beck 2013
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "messages.h"
23 #include "serverid.h"
24 #include "smbd/globals.h"
25 #include "smbd/scavenger.h"
26 #include "locking/proto.h"
27 #include "lib/util/server_id.h"
28 #include "lib/util/util_process.h"
29 #include "lib/util/sys_rw_data.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_SCAVENGER
34 struct smbd_scavenger_state {
35 struct tevent_context *ev;
36 struct messaging_context *msg;
37 struct server_id parent_id;
38 struct server_id *scavenger_id;
39 bool am_scavenger;
42 static struct smbd_scavenger_state *smbd_scavenger_state = NULL;
44 struct scavenger_message {
45 struct file_id file_id;
46 uint64_t open_persistent_id;
47 NTTIME until;
50 static int smbd_scavenger_main(struct smbd_scavenger_state *state)
52 struct server_id_buf tmp1, tmp2;
54 DEBUG(10, ("scavenger: %s started, parent: %s\n",
55 server_id_str_buf(*state->scavenger_id, &tmp1),
56 server_id_str_buf(state->parent_id, &tmp2)));
58 while (true) {
59 TALLOC_CTX *frame = talloc_stackframe();
60 int ret;
62 ret = tevent_loop_once(state->ev);
63 if (ret != 0) {
64 DEBUG(2, ("tevent_loop_once failed: %s\n",
65 strerror(errno)));
66 TALLOC_FREE(frame);
67 return 1;
70 DEBUG(10, ("scavenger: %s event loop iteration\n",
71 server_id_str_buf(*state->scavenger_id, &tmp1)));
72 TALLOC_FREE(frame);
75 return 0;
78 static void smbd_scavenger_done(struct tevent_context *event_ctx, struct tevent_fd *fde,
79 uint16_t flags, void *private_data)
81 struct smbd_scavenger_state *state = talloc_get_type_abort(
82 private_data, struct smbd_scavenger_state);
83 struct server_id_buf tmp;
85 DEBUG(2, ("scavenger: %s died\n",
86 server_id_str_buf(*state->scavenger_id, &tmp)));
88 TALLOC_FREE(state->scavenger_id);
91 static void smbd_scavenger_parent_dead(struct tevent_context *event_ctx,
92 struct tevent_fd *fde,
93 uint16_t flags, void *private_data)
95 struct smbd_scavenger_state *state = talloc_get_type_abort(
96 private_data, struct smbd_scavenger_state);
97 struct server_id_buf tmp1, tmp2;
99 DEBUG(2, ("scavenger: %s parent %s died\n",
100 server_id_str_buf(*state->scavenger_id, &tmp1),
101 server_id_str_buf(state->parent_id, &tmp2)));
103 exit_server("smbd_scavenger_parent_dead");
106 static void scavenger_sig_term_handler(struct tevent_context *ev,
107 struct tevent_signal *se,
108 int signum,
109 int count,
110 void *siginfo,
111 void *private_data)
113 exit_server_cleanly("termination signal");
116 static void scavenger_setup_sig_term_handler(struct tevent_context *ev_ctx)
118 struct tevent_signal *se;
120 se = tevent_add_signal(ev_ctx,
121 ev_ctx,
122 SIGTERM, 0,
123 scavenger_sig_term_handler,
124 NULL);
125 if (se == NULL) {
126 exit_server("failed to setup SIGTERM handler");
130 static bool smbd_scavenger_running(struct smbd_scavenger_state *state)
132 if (state->scavenger_id == NULL) {
133 return false;
136 return serverid_exists(state->scavenger_id);
139 static int smbd_scavenger_server_id_destructor(struct server_id *id)
141 serverid_deregister(*id);
142 return 0;
145 static bool scavenger_say_hello(int fd, struct server_id self)
147 ssize_t ret;
148 struct server_id_buf tmp;
150 ret = write_data(fd, &self, sizeof(self));
151 if (ret == -1) {
152 DEBUG(2, ("Failed to write to pipe: %s\n", strerror(errno)));
153 return false;
155 if (ret < sizeof(self)) {
156 DBG_WARNING("Could not write serverid\n");
157 return false;
160 DEBUG(4, ("scavenger_say_hello: self[%s]\n",
161 server_id_str_buf(self, &tmp)));
162 return true;
165 static bool scavenger_wait_hello(int fd, struct server_id *child)
167 struct server_id_buf tmp;
168 ssize_t ret;
170 ret = read_data(fd, child, sizeof(struct server_id));
171 if (ret == -1) {
172 DEBUG(2, ("Failed to read from pipe: %s\n",
173 strerror(errno)));
174 return false;
176 if (ret < sizeof(struct server_id)) {
177 DBG_WARNING("Could not read serverid\n");
178 return false;
181 DEBUG(4, ("scavenger_say_hello: child[%s]\n",
182 server_id_str_buf(*child, &tmp)));
183 return true;
186 static bool smbd_scavenger_start(struct smbd_scavenger_state *state)
188 struct server_id self = messaging_server_id(state->msg);
189 struct tevent_fd *fde = NULL;
190 int fds[2];
191 int ret;
192 bool ok;
194 SMB_ASSERT(server_id_equal(&state->parent_id, &self));
196 if (smbd_scavenger_running(state)) {
197 struct server_id_buf tmp;
198 DEBUG(10, ("scavenger %s already running\n",
199 server_id_str_buf(*state->scavenger_id,
200 &tmp)));
201 return true;
204 if (state->scavenger_id != NULL) {
205 struct server_id_buf tmp;
206 DEBUG(10, ("scavenger zombie %s, cleaning up\n",
207 server_id_str_buf(*state->scavenger_id,
208 &tmp)));
209 TALLOC_FREE(state->scavenger_id);
212 state->scavenger_id = talloc_zero(state, struct server_id);
213 if (state->scavenger_id == NULL) {
214 DEBUG(2, ("Out of memory\n"));
215 goto fail;
217 talloc_set_destructor(state->scavenger_id,
218 smbd_scavenger_server_id_destructor);
220 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
221 if (ret == -1) {
222 DEBUG(2, ("socketpair failed: %s", strerror(errno)));
223 goto fail;
226 smb_set_close_on_exec(fds[0]);
227 smb_set_close_on_exec(fds[1]);
229 ret = fork();
230 if (ret == -1) {
231 int err = errno;
232 close(fds[0]);
233 close(fds[1]);
234 DEBUG(0, ("fork failed: %s", strerror(err)));
235 goto fail;
238 if (ret == 0) {
239 /* child */
241 NTSTATUS status;
243 close(fds[0]);
245 status = smbd_reinit_after_fork(state->msg, state->ev,
246 true, "smbd-scavenger");
247 if (!NT_STATUS_IS_OK(status)) {
248 DEBUG(2, ("reinit_after_fork failed: %s\n",
249 nt_errstr(status)));
250 exit_server("reinit_after_fork failed");
251 return false;
254 state->am_scavenger = true;
255 *state->scavenger_id = messaging_server_id(state->msg);
257 scavenger_setup_sig_term_handler(state->ev);
259 if (!serverid_register(*state->scavenger_id,
260 FLAG_MSG_GENERAL)) {
261 DBG_WARNING("serverid_register failed");
262 exit_server("serverid_register failed");
263 return false;
266 ok = scavenger_say_hello(fds[1], *state->scavenger_id);
267 if (!ok) {
268 DEBUG(2, ("scavenger_say_hello failed\n"));
269 exit_server("scavenger_say_hello failed");
270 return false;
273 fde = tevent_add_fd(state->ev, state->scavenger_id,
274 fds[1], TEVENT_FD_READ,
275 smbd_scavenger_parent_dead, state);
276 if (fde == NULL) {
277 DEBUG(2, ("tevent_add_fd(smbd_scavenger_parent_dead) "
278 "failed\n"));
279 exit_server("tevent_add_fd(smbd_scavenger_parent_dead) "
280 "failed");
281 return false;
283 tevent_fd_set_auto_close(fde);
285 ret = smbd_scavenger_main(state);
287 DEBUG(10, ("scavenger ended: %d\n", ret));
288 exit_server_cleanly("scavenger ended");
289 return false;
292 /* parent */
293 close(fds[1]);
295 ok = scavenger_wait_hello(fds[0], state->scavenger_id);
296 if (!ok) {
297 close(fds[0]);
298 goto fail;
301 fde = tevent_add_fd(state->ev, state->scavenger_id,
302 fds[0], TEVENT_FD_READ,
303 smbd_scavenger_done, state);
304 if (fde == NULL) {
305 close(fds[0]);
306 goto fail;
308 tevent_fd_set_auto_close(fde);
310 return true;
311 fail:
312 TALLOC_FREE(state->scavenger_id);
313 return false;
316 static void scavenger_add_timer(struct smbd_scavenger_state *state,
317 struct scavenger_message *msg);
319 static void smbd_scavenger_msg(struct messaging_context *msg_ctx,
320 void *private_data,
321 uint32_t msg_type,
322 struct server_id src,
323 DATA_BLOB *data)
325 struct smbd_scavenger_state *state =
326 talloc_get_type_abort(private_data,
327 struct smbd_scavenger_state);
328 TALLOC_CTX *frame = talloc_stackframe();
329 struct server_id self = messaging_server_id(msg_ctx);
330 struct scavenger_message *msg = NULL;
331 struct server_id_buf tmp1, tmp2;
333 DEBUG(10, ("smbd_scavenger_msg: %s got message from %s\n",
334 server_id_str_buf(self, &tmp1),
335 server_id_str_buf(src, &tmp2)));
337 if (server_id_equal(&state->parent_id, &self)) {
338 NTSTATUS status;
340 if (!smbd_scavenger_running(state) &&
341 !smbd_scavenger_start(state))
343 DEBUG(2, ("Failed to start scavenger\n"));
344 goto done;
346 DEBUG(10, ("forwarding message to scavenger\n"));
348 status = messaging_send(msg_ctx,
349 *state->scavenger_id, msg_type, data);
350 if (!NT_STATUS_IS_OK(status)) {
351 DEBUG(2, ("forwarding message to scavenger failed: "
352 "%s\n", nt_errstr(status)));
353 goto done;
355 goto done;
358 if (!state->am_scavenger) {
359 DEBUG(10, ("im not the scavenger: ignore message\n"));
360 goto done;
363 if (!server_id_equal(&state->parent_id, &src)) {
364 DEBUG(10, ("scavenger: ignore spurious message\n"));
365 goto done;
368 DEBUG(10, ("scavenger: got a message\n"));
369 msg = (struct scavenger_message*)data->data;
370 scavenger_add_timer(state, msg);
371 done:
372 talloc_free(frame);
375 bool smbd_scavenger_init(TALLOC_CTX *mem_ctx,
376 struct messaging_context *msg,
377 struct tevent_context *ev)
379 struct smbd_scavenger_state *state;
380 NTSTATUS status;
382 if (smbd_scavenger_state) {
383 DEBUG(10, ("smbd_scavenger_init called again\n"));
384 return true;
387 state = talloc_zero(mem_ctx, struct smbd_scavenger_state);
388 if (state == NULL) {
389 DEBUG(2, ("Out of memory\n"));
390 return false;
393 state->msg = msg;
394 state->ev = ev;
395 state->parent_id = messaging_server_id(msg);
397 status = messaging_register(msg, state, MSG_SMB_SCAVENGER,
398 smbd_scavenger_msg);
399 if (!NT_STATUS_IS_OK(status)) {
400 DEBUG(2, ("failed to register message handler: %s\n",
401 nt_errstr(status)));
402 goto fail;
405 smbd_scavenger_state = state;
406 return true;
407 fail:
408 talloc_free(state);
409 return false;
412 void scavenger_schedule_disconnected(struct files_struct *fsp)
414 NTSTATUS status;
415 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
416 struct timeval disconnect_time, until;
417 uint64_t timeout_usec;
418 struct scavenger_message msg;
419 DATA_BLOB msg_blob;
420 struct server_id_buf tmp;
422 if (fsp->op == NULL) {
423 return;
425 nttime_to_timeval(&disconnect_time, fsp->op->global->disconnect_time);
426 timeout_usec = 1000 * fsp->op->global->durable_timeout_msec;
427 until = timeval_add(&disconnect_time,
428 timeout_usec / 1000000,
429 timeout_usec % 1000000);
431 ZERO_STRUCT(msg);
432 msg.file_id = fsp->file_id;
433 msg.open_persistent_id = fsp->op->global->open_persistent_id;
434 msg.until = timeval_to_nttime(&until);
436 DEBUG(10, ("smbd: %s mark file %s as disconnected at %s with timeout "
437 "at %s in %fs\n",
438 server_id_str_buf(self, &tmp),
439 file_id_string_tos(&fsp->file_id),
440 timeval_string(talloc_tos(), &disconnect_time, true),
441 timeval_string(talloc_tos(), &until, true),
442 fsp->op->global->durable_timeout_msec/1000.0));
444 SMB_ASSERT(server_id_is_disconnected(&fsp->op->global->server_id));
445 SMB_ASSERT(!server_id_equal(&self, &smbd_scavenger_state->parent_id));
446 SMB_ASSERT(!smbd_scavenger_state->am_scavenger);
448 msg_blob = data_blob_const(&msg, sizeof(msg));
449 DEBUG(10, ("send message to scavenger\n"));
451 status = messaging_send(smbd_scavenger_state->msg,
452 smbd_scavenger_state->parent_id,
453 MSG_SMB_SCAVENGER,
454 &msg_blob);
455 if (!NT_STATUS_IS_OK(status)) {
456 struct server_id_buf tmp1, tmp2;
457 DEBUG(2, ("Failed to send message to parent smbd %s "
458 "from %s: %s\n",
459 server_id_str_buf(smbd_scavenger_state->parent_id,
460 &tmp1),
461 server_id_str_buf(self, &tmp2),
462 nt_errstr(status)));
466 struct scavenger_timer_context {
467 struct smbd_scavenger_state *state;
468 struct scavenger_message msg;
471 static void scavenger_timer(struct tevent_context *ev,
472 struct tevent_timer *te,
473 struct timeval t, void *data)
475 struct scavenger_timer_context *ctx =
476 talloc_get_type_abort(data, struct scavenger_timer_context);
477 NTSTATUS status;
478 bool ok;
480 DEBUG(10, ("scavenger: do cleanup for file %s at %s\n",
481 file_id_string_tos(&ctx->msg.file_id),
482 timeval_string(talloc_tos(), &t, true)));
484 ok = share_mode_cleanup_disconnected(ctx->msg.file_id,
485 ctx->msg.open_persistent_id);
486 if (!ok) {
487 DEBUG(2, ("Failed to cleanup share modes and byte range locks "
488 "for file %s open %llu\n",
489 file_id_string_tos(&ctx->msg.file_id),
490 (unsigned long long)ctx->msg.open_persistent_id));
493 status = smbXsrv_open_cleanup(ctx->msg.open_persistent_id);
494 if (!NT_STATUS_IS_OK(status)) {
495 DEBUG(2, ("Failed to cleanup open global for file %s open %llu:"
496 " %s\n", file_id_string_tos(&ctx->msg.file_id),
497 (unsigned long long)ctx->msg.open_persistent_id,
498 nt_errstr(status)));
502 static void scavenger_add_timer(struct smbd_scavenger_state *state,
503 struct scavenger_message *msg)
505 struct tevent_timer *te;
506 struct scavenger_timer_context *ctx;
507 struct timeval until;
509 nttime_to_timeval(&until, msg->until);
511 DEBUG(10, ("scavenger: schedule file %s for cleanup at %s\n",
512 file_id_string_tos(&msg->file_id),
513 timeval_string(talloc_tos(), &until, true)));
515 ctx = talloc_zero(state, struct scavenger_timer_context);
516 if (ctx == NULL) {
517 DEBUG(2, ("Failed to talloc_zero(scavenger_timer_context)\n"));
518 return;
521 ctx->state = state;
522 ctx->msg = *msg;
524 te = tevent_add_timer(state->ev,
525 state,
526 until,
527 scavenger_timer,
528 ctx);
529 if (te == NULL) {
530 DEBUG(2, ("Failed to add scavenger_timer event\n"));
531 talloc_free(ctx);
532 return;
535 /* delete context after handler was running */
536 talloc_steal(te, ctx);