ctdb-tests: Avoid segfault by initializing logging
[Samba.git] / source3 / smbd / scavenger.c
bloba9fda7f06415c9db72553f87cdbee5190d85672e
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 return 0;
144 static bool scavenger_say_hello(int fd, struct server_id self)
146 ssize_t ret;
147 struct server_id_buf tmp;
149 ret = write_data(fd, &self, sizeof(self));
150 if (ret == -1) {
151 DEBUG(2, ("Failed to write to pipe: %s\n", strerror(errno)));
152 return false;
154 if (ret < sizeof(self)) {
155 DBG_WARNING("Could not write serverid\n");
156 return false;
159 DEBUG(4, ("scavenger_say_hello: self[%s]\n",
160 server_id_str_buf(self, &tmp)));
161 return true;
164 static bool scavenger_wait_hello(int fd, struct server_id *child)
166 struct server_id_buf tmp;
167 ssize_t ret;
169 ret = read_data(fd, child, sizeof(struct server_id));
170 if (ret == -1) {
171 DEBUG(2, ("Failed to read from pipe: %s\n",
172 strerror(errno)));
173 return false;
175 if (ret < sizeof(struct server_id)) {
176 DBG_WARNING("Could not read serverid\n");
177 return false;
180 DEBUG(4, ("scavenger_say_hello: child[%s]\n",
181 server_id_str_buf(*child, &tmp)));
182 return true;
185 static bool smbd_scavenger_start(struct smbd_scavenger_state *state)
187 struct server_id self = messaging_server_id(state->msg);
188 struct tevent_fd *fde = NULL;
189 int fds[2];
190 int ret;
191 bool ok;
193 SMB_ASSERT(server_id_equal(&state->parent_id, &self));
195 if (smbd_scavenger_running(state)) {
196 struct server_id_buf tmp;
197 DEBUG(10, ("scavenger %s already running\n",
198 server_id_str_buf(*state->scavenger_id,
199 &tmp)));
200 return true;
203 if (state->scavenger_id != NULL) {
204 struct server_id_buf tmp;
205 DEBUG(10, ("scavenger zombie %s, cleaning up\n",
206 server_id_str_buf(*state->scavenger_id,
207 &tmp)));
208 TALLOC_FREE(state->scavenger_id);
211 state->scavenger_id = talloc_zero(state, struct server_id);
212 if (state->scavenger_id == NULL) {
213 DEBUG(2, ("Out of memory\n"));
214 goto fail;
216 talloc_set_destructor(state->scavenger_id,
217 smbd_scavenger_server_id_destructor);
219 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
220 if (ret == -1) {
221 DEBUG(2, ("socketpair failed: %s", strerror(errno)));
222 goto fail;
225 smb_set_close_on_exec(fds[0]);
226 smb_set_close_on_exec(fds[1]);
228 ret = fork();
229 if (ret == -1) {
230 int err = errno;
231 close(fds[0]);
232 close(fds[1]);
233 DEBUG(0, ("fork failed: %s", strerror(err)));
234 goto fail;
237 if (ret == 0) {
238 /* child */
240 NTSTATUS status;
242 close(fds[0]);
244 status = smbd_reinit_after_fork(state->msg, state->ev,
245 true, "smbd-scavenger");
246 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(2, ("reinit_after_fork failed: %s\n",
248 nt_errstr(status)));
249 exit_server("reinit_after_fork failed");
250 return false;
253 state->am_scavenger = true;
254 *state->scavenger_id = messaging_server_id(state->msg);
256 scavenger_setup_sig_term_handler(state->ev);
258 ok = scavenger_say_hello(fds[1], *state->scavenger_id);
259 if (!ok) {
260 DEBUG(2, ("scavenger_say_hello failed\n"));
261 exit_server("scavenger_say_hello failed");
262 return false;
265 fde = tevent_add_fd(state->ev, state->scavenger_id,
266 fds[1], TEVENT_FD_READ,
267 smbd_scavenger_parent_dead, state);
268 if (fde == NULL) {
269 DEBUG(2, ("tevent_add_fd(smbd_scavenger_parent_dead) "
270 "failed\n"));
271 exit_server("tevent_add_fd(smbd_scavenger_parent_dead) "
272 "failed");
273 return false;
275 tevent_fd_set_auto_close(fde);
277 ret = smbd_scavenger_main(state);
279 DEBUG(10, ("scavenger ended: %d\n", ret));
280 exit_server_cleanly("scavenger ended");
281 return false;
284 /* parent */
285 close(fds[1]);
287 ok = scavenger_wait_hello(fds[0], state->scavenger_id);
288 if (!ok) {
289 close(fds[0]);
290 goto fail;
293 fde = tevent_add_fd(state->ev, state->scavenger_id,
294 fds[0], TEVENT_FD_READ,
295 smbd_scavenger_done, state);
296 if (fde == NULL) {
297 close(fds[0]);
298 goto fail;
300 tevent_fd_set_auto_close(fde);
302 return true;
303 fail:
304 TALLOC_FREE(state->scavenger_id);
305 return false;
308 static void scavenger_add_timer(struct smbd_scavenger_state *state,
309 struct scavenger_message *msg);
311 static void smbd_scavenger_msg(struct messaging_context *msg_ctx,
312 void *private_data,
313 uint32_t msg_type,
314 struct server_id src,
315 DATA_BLOB *data)
317 struct smbd_scavenger_state *state =
318 talloc_get_type_abort(private_data,
319 struct smbd_scavenger_state);
320 TALLOC_CTX *frame = talloc_stackframe();
321 struct server_id self = messaging_server_id(msg_ctx);
322 struct scavenger_message *msg = NULL;
323 struct server_id_buf tmp1, tmp2;
325 DEBUG(10, ("smbd_scavenger_msg: %s got message from %s\n",
326 server_id_str_buf(self, &tmp1),
327 server_id_str_buf(src, &tmp2)));
329 if (server_id_equal(&state->parent_id, &self)) {
330 NTSTATUS status;
332 if (!smbd_scavenger_running(state) &&
333 !smbd_scavenger_start(state))
335 DEBUG(2, ("Failed to start scavenger\n"));
336 goto done;
338 DEBUG(10, ("forwarding message to scavenger\n"));
340 status = messaging_send(msg_ctx,
341 *state->scavenger_id, msg_type, data);
342 if (!NT_STATUS_IS_OK(status)) {
343 DEBUG(2, ("forwarding message to scavenger failed: "
344 "%s\n", nt_errstr(status)));
345 goto done;
347 goto done;
350 if (!state->am_scavenger) {
351 DEBUG(10, ("im not the scavenger: ignore message\n"));
352 goto done;
355 if (!server_id_equal(&state->parent_id, &src)) {
356 DEBUG(10, ("scavenger: ignore spurious message\n"));
357 goto done;
360 DEBUG(10, ("scavenger: got a message\n"));
361 msg = (struct scavenger_message*)data->data;
362 scavenger_add_timer(state, msg);
363 done:
364 talloc_free(frame);
367 bool smbd_scavenger_init(TALLOC_CTX *mem_ctx,
368 struct messaging_context *msg,
369 struct tevent_context *ev)
371 struct smbd_scavenger_state *state;
372 NTSTATUS status;
374 if (smbd_scavenger_state) {
375 DEBUG(10, ("smbd_scavenger_init called again\n"));
376 return true;
379 state = talloc_zero(mem_ctx, struct smbd_scavenger_state);
380 if (state == NULL) {
381 DEBUG(2, ("Out of memory\n"));
382 return false;
385 state->msg = msg;
386 state->ev = ev;
387 state->parent_id = messaging_server_id(msg);
389 status = messaging_register(msg, state, MSG_SMB_SCAVENGER,
390 smbd_scavenger_msg);
391 if (!NT_STATUS_IS_OK(status)) {
392 DEBUG(2, ("failed to register message handler: %s\n",
393 nt_errstr(status)));
394 goto fail;
397 smbd_scavenger_state = state;
398 return true;
399 fail:
400 talloc_free(state);
401 return false;
404 void scavenger_schedule_disconnected(struct files_struct *fsp)
406 NTSTATUS status;
407 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
408 struct timeval disconnect_time, until;
409 uint64_t timeout_usec;
410 struct scavenger_message msg;
411 DATA_BLOB msg_blob;
412 struct server_id_buf tmp;
414 if (fsp->op == NULL) {
415 return;
417 nttime_to_timeval(&disconnect_time, fsp->op->global->disconnect_time);
418 timeout_usec = 1000 * fsp->op->global->durable_timeout_msec;
419 until = timeval_add(&disconnect_time,
420 timeout_usec / 1000000,
421 timeout_usec % 1000000);
423 ZERO_STRUCT(msg);
424 msg.file_id = fsp->file_id;
425 msg.open_persistent_id = fsp->op->global->open_persistent_id;
426 msg.until = timeval_to_nttime(&until);
428 DEBUG(10, ("smbd: %s mark file %s as disconnected at %s with timeout "
429 "at %s in %fs\n",
430 server_id_str_buf(self, &tmp),
431 file_id_string_tos(&fsp->file_id),
432 timeval_string(talloc_tos(), &disconnect_time, true),
433 timeval_string(talloc_tos(), &until, true),
434 fsp->op->global->durable_timeout_msec/1000.0));
436 SMB_ASSERT(server_id_is_disconnected(&fsp->op->global->server_id));
437 SMB_ASSERT(!server_id_equal(&self, &smbd_scavenger_state->parent_id));
438 SMB_ASSERT(!smbd_scavenger_state->am_scavenger);
440 msg_blob = data_blob_const(&msg, sizeof(msg));
441 DEBUG(10, ("send message to scavenger\n"));
443 status = messaging_send(smbd_scavenger_state->msg,
444 smbd_scavenger_state->parent_id,
445 MSG_SMB_SCAVENGER,
446 &msg_blob);
447 if (!NT_STATUS_IS_OK(status)) {
448 struct server_id_buf tmp1, tmp2;
449 DEBUG(2, ("Failed to send message to parent smbd %s "
450 "from %s: %s\n",
451 server_id_str_buf(smbd_scavenger_state->parent_id,
452 &tmp1),
453 server_id_str_buf(self, &tmp2),
454 nt_errstr(status)));
458 struct scavenger_timer_context {
459 struct smbd_scavenger_state *state;
460 struct scavenger_message msg;
463 static void scavenger_timer(struct tevent_context *ev,
464 struct tevent_timer *te,
465 struct timeval t, void *data)
467 struct scavenger_timer_context *ctx =
468 talloc_get_type_abort(data, struct scavenger_timer_context);
469 NTSTATUS status;
470 bool ok;
472 DEBUG(10, ("scavenger: do cleanup for file %s at %s\n",
473 file_id_string_tos(&ctx->msg.file_id),
474 timeval_string(talloc_tos(), &t, true)));
476 ok = share_mode_cleanup_disconnected(ctx->msg.file_id,
477 ctx->msg.open_persistent_id);
478 if (!ok) {
479 DEBUG(2, ("Failed to cleanup share modes and byte range locks "
480 "for file %s open %llu\n",
481 file_id_string_tos(&ctx->msg.file_id),
482 (unsigned long long)ctx->msg.open_persistent_id));
485 status = smbXsrv_open_cleanup(ctx->msg.open_persistent_id);
486 if (!NT_STATUS_IS_OK(status)) {
487 DEBUG(2, ("Failed to cleanup open global for file %s open %llu:"
488 " %s\n", file_id_string_tos(&ctx->msg.file_id),
489 (unsigned long long)ctx->msg.open_persistent_id,
490 nt_errstr(status)));
494 static void scavenger_add_timer(struct smbd_scavenger_state *state,
495 struct scavenger_message *msg)
497 struct tevent_timer *te;
498 struct scavenger_timer_context *ctx;
499 struct timeval until;
501 nttime_to_timeval(&until, msg->until);
503 DEBUG(10, ("scavenger: schedule file %s for cleanup at %s\n",
504 file_id_string_tos(&msg->file_id),
505 timeval_string(talloc_tos(), &until, true)));
507 ctx = talloc_zero(state, struct scavenger_timer_context);
508 if (ctx == NULL) {
509 DEBUG(2, ("Failed to talloc_zero(scavenger_timer_context)\n"));
510 return;
513 ctx->state = state;
514 ctx->msg = *msg;
516 te = tevent_add_timer(state->ev,
517 state,
518 until,
519 scavenger_timer,
520 ctx);
521 if (te == NULL) {
522 DEBUG(2, ("Failed to add scavenger_timer event\n"));
523 talloc_free(ctx);
524 return;
527 /* delete context after handler was running */
528 talloc_steal(te, ctx);