shadow_copy2: add comment block explaining shadow_copy2_convert()
[Samba.git] / source3 / smbd / scavenger.c
blobe6e287880656b0a9d505a7c490d53d0a7a3392ec
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"
23 #include "messages.h"
24 #include "serverid.h"
25 #include "smbd/globals.h"
26 #include "smbd/scavenger.h"
27 #include "locking/proto.h"
28 #include "lib/util/util_process.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_SCAVENGER
33 struct smbd_scavenger_state {
34 struct tevent_context *ev;
35 struct messaging_context *msg;
36 struct server_id parent_id;
37 struct server_id *scavenger_id;
38 bool am_scavenger;
41 static struct smbd_scavenger_state *smbd_scavenger_state = NULL;
43 struct scavenger_message {
44 struct file_id file_id;
45 uint64_t open_persistent_id;
46 NTTIME until;
49 static int smbd_scavenger_main(struct smbd_scavenger_state *state)
51 DEBUG(10, ("scavenger: %s started, parent: %s\n",
52 server_id_str(talloc_tos(), state->scavenger_id),
53 server_id_str(talloc_tos(), &state->parent_id)));
55 while (true) {
56 TALLOC_CTX *frame = talloc_stackframe();
57 int ret;
59 ret = tevent_loop_once(state->ev);
60 if (ret != 0) {
61 DEBUG(2, ("tevent_loop_once failed: %s\n",
62 strerror(errno)));
63 TALLOC_FREE(frame);
64 return 1;
67 DEBUG(10, ("scavenger: %s event loop iteration\n",
68 server_id_str(talloc_tos(), state->scavenger_id)));
69 TALLOC_FREE(frame);
72 return 0;
75 static void smbd_scavenger_done(struct tevent_context *event_ctx, struct tevent_fd *fde,
76 uint16_t flags, void *private_data)
78 struct smbd_scavenger_state *state = talloc_get_type_abort(
79 private_data, struct smbd_scavenger_state);
81 DEBUG(2, ("scavenger: %s died\n",
82 server_id_str(talloc_tos(), state->scavenger_id)));
84 TALLOC_FREE(state->scavenger_id);
87 static void smbd_scavenger_parent_dead(struct tevent_context *event_ctx,
88 struct tevent_fd *fde,
89 uint16_t flags, void *private_data)
91 struct smbd_scavenger_state *state = talloc_get_type_abort(
92 private_data, struct smbd_scavenger_state);
94 DEBUG(2, ("scavenger: %s parent %s died\n",
95 server_id_str(talloc_tos(), state->scavenger_id),
96 server_id_str(talloc_tos(), &state->parent_id)));
98 exit_server("smbd_scavenger_parent_dead");
101 static void scavenger_sig_term_handler(struct tevent_context *ev,
102 struct tevent_signal *se,
103 int signum,
104 int count,
105 void *siginfo,
106 void *private_data)
108 exit_server_cleanly("termination signal");
111 static void scavenger_setup_sig_term_handler(struct tevent_context *ev_ctx)
113 struct tevent_signal *se;
115 se = tevent_add_signal(ev_ctx,
116 ev_ctx,
117 SIGTERM, 0,
118 scavenger_sig_term_handler,
119 NULL);
120 if (se == NULL) {
121 exit_server("failed to setup SIGTERM handler");
125 static bool smbd_scavenger_running(struct smbd_scavenger_state *state)
127 if (state->scavenger_id == NULL) {
128 return false;
131 return serverid_exists(state->scavenger_id);
134 static int smbd_scavenger_server_id_destructor(struct server_id *id)
136 serverid_deregister(*id);
137 return 0;
140 static bool scavenger_say_hello(int fd, struct server_id self)
142 const uint8_t *msg = (const uint8_t *)&self;
143 size_t remaining = sizeof(self);
144 size_t ofs = 0;
146 while (remaining > 0) {
147 ssize_t ret;
149 ret = sys_write(fd, msg + ofs, remaining);
150 if (ret == -1) {
151 DEBUG(2, ("Failed to write to pipe: %s\n",
152 strerror(errno)));
153 return false;
155 remaining -= ret;
158 DEBUG(4, ("scavenger_say_hello: self[%s]\n",
159 server_id_str(talloc_tos(), &self)));
160 return true;
163 static bool scavenger_wait_hello(int fd, struct server_id *child)
165 uint8_t *msg = (uint8_t *)child;
166 size_t remaining = sizeof(*child);
167 size_t ofs = 0;
169 while (remaining > 0) {
170 ssize_t ret;
172 ret = sys_read(fd, msg + ofs, remaining);
173 if (ret == -1) {
174 DEBUG(2, ("Failed to read from pipe: %s\n",
175 strerror(errno)));
176 return false;
178 remaining -= ret;
181 DEBUG(4, ("scavenger_say_hello: child[%s]\n",
182 server_id_str(talloc_tos(), child)));
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 uint64_t unique_id;
193 bool ok;
195 SMB_ASSERT(server_id_equal(&state->parent_id, &self));
197 if (smbd_scavenger_running(state)) {
198 DEBUG(10, ("scavenger %s already running\n",
199 server_id_str(talloc_tos(),
200 state->scavenger_id)));
201 return true;
204 if (state->scavenger_id != NULL) {
205 DEBUG(10, ("scavenger zombie %s, cleaning up\n",
206 server_id_str(talloc_tos(),
207 state->scavenger_id)));
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 unique_id = serverid_get_random_unique_id();
230 ret = fork();
231 if (ret == -1) {
232 int err = errno;
233 close(fds[0]);
234 close(fds[1]);
235 DEBUG(0, ("fork failed: %s", strerror(err)));
236 goto fail;
239 if (ret == 0) {
240 /* child */
242 NTSTATUS status;
244 close(fds[0]);
246 am_parent = NULL;
248 set_my_unique_id(unique_id);
250 status = reinit_after_fork(state->msg, state->ev, true);
251 if (!NT_STATUS_IS_OK(status)) {
252 DEBUG(2, ("reinit_after_fork failed: %s\n",
253 nt_errstr(status)));
254 exit_server("reinit_after_fork failed");
255 return false;
258 prctl_set_comment("smbd-scavenger");
260 state->am_scavenger = true;
261 *state->scavenger_id = messaging_server_id(state->msg);
263 scavenger_setup_sig_term_handler(state->ev);
265 serverid_register(*state->scavenger_id, FLAG_MSG_GENERAL);
267 ok = scavenger_say_hello(fds[1], *state->scavenger_id);
268 if (!ok) {
269 DEBUG(2, ("scavenger_say_hello failed\n"));
270 exit_server("scavenger_say_hello failed");
271 return false;
274 fde = tevent_add_fd(state->ev, state->scavenger_id,
275 fds[1], TEVENT_FD_READ,
276 smbd_scavenger_parent_dead, state);
277 if (fde == NULL) {
278 DEBUG(2, ("tevent_add_fd(smbd_scavenger_parent_dead) "
279 "failed\n"));
280 exit_server("tevent_add_fd(smbd_scavenger_parent_dead) "
281 "failed");
282 return false;
284 tevent_fd_set_auto_close(fde);
286 ret = smbd_scavenger_main(state);
288 DEBUG(10, ("scavenger ended: %d\n", ret));
289 exit_server_cleanly("scavenger ended");
290 return false;
293 /* parent */
294 close(fds[1]);
296 ok = scavenger_wait_hello(fds[0], state->scavenger_id);
297 if (!ok) {
298 close(fds[0]);
299 goto fail;
302 fde = tevent_add_fd(state->ev, state->scavenger_id,
303 fds[0], TEVENT_FD_READ,
304 smbd_scavenger_done, state);
305 if (fde == NULL) {
306 close(fds[0]);
307 goto fail;
309 tevent_fd_set_auto_close(fde);
311 return true;
312 fail:
313 TALLOC_FREE(state->scavenger_id);
314 return false;
317 static void scavenger_add_timer(struct smbd_scavenger_state *state,
318 struct scavenger_message *msg);
320 static void smbd_scavenger_msg(struct messaging_context *msg_ctx,
321 void *private_data,
322 uint32_t msg_type,
323 struct server_id src,
324 DATA_BLOB *data)
326 struct smbd_scavenger_state *state =
327 talloc_get_type_abort(private_data,
328 struct smbd_scavenger_state);
329 TALLOC_CTX *frame = talloc_stackframe();
330 struct server_id self = messaging_server_id(msg_ctx);
331 struct scavenger_message *msg = NULL;
333 DEBUG(10, ("smbd_scavenger_msg: %s got message from %s\n",
334 server_id_str(talloc_tos(), &self),
335 server_id_str(talloc_tos(), &src)));
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;
421 nttime_to_timeval(&disconnect_time, fsp->op->global->disconnect_time);
422 timeout_usec = 1000 * fsp->op->global->durable_timeout_msec;
423 until = timeval_add(&disconnect_time,
424 timeout_usec / 1000000,
425 timeout_usec % 1000000);
427 ZERO_STRUCT(msg);
428 msg.file_id = fsp->file_id;
429 msg.open_persistent_id = fsp->op->global->open_persistent_id;
430 msg.until = timeval_to_nttime(&until);
432 DEBUG(10, ("smbd: %s mark file %s as disconnected at %s with timeout "
433 "at %s in %fs\n",
434 server_id_str(talloc_tos(), &self),
435 file_id_string_tos(&fsp->file_id),
436 timeval_string(talloc_tos(), &disconnect_time, true),
437 timeval_string(talloc_tos(), &until, true),
438 fsp->op->global->durable_timeout_msec/1000.0));
440 SMB_ASSERT(server_id_is_disconnected(&fsp->op->global->server_id));
441 SMB_ASSERT(!server_id_equal(&self, &smbd_scavenger_state->parent_id));
442 SMB_ASSERT(!smbd_scavenger_state->am_scavenger);
444 msg_blob = data_blob_const(&msg, sizeof(msg));
445 DEBUG(10, ("send message to scavenger\n"));
447 status = messaging_send(smbd_scavenger_state->msg,
448 smbd_scavenger_state->parent_id,
449 MSG_SMB_SCAVENGER,
450 &msg_blob);
451 if (!NT_STATUS_IS_OK(status)) {
452 DEBUG(2, ("Failed to send message to parent smbd %s "
453 "from %s: %s\n",
454 server_id_str(talloc_tos(),
455 &smbd_scavenger_state->parent_id),
456 server_id_str(talloc_tos(), &self),
457 nt_errstr(status)));
461 struct scavenger_timer_context {
462 struct smbd_scavenger_state *state;
463 struct scavenger_message msg;
466 static void scavenger_timer(struct tevent_context *ev,
467 struct tevent_timer *te,
468 struct timeval t, void *data)
470 struct scavenger_timer_context *ctx =
471 talloc_get_type_abort(data, struct scavenger_timer_context);
472 NTSTATUS status;
473 bool ok;
475 DEBUG(10, ("scavenger: do cleanup for file %s at %s\n",
476 file_id_string_tos(&ctx->msg.file_id),
477 timeval_string(talloc_tos(), &t, true)));
479 ok = share_mode_cleanup_disconnected(ctx->msg.file_id,
480 ctx->msg.open_persistent_id);
481 if (!ok) {
482 DEBUG(2, ("Failed to cleanup share modes and byte range locks "
483 "for file %s open %llu\n",
484 file_id_string_tos(&ctx->msg.file_id),
485 (unsigned long long)ctx->msg.open_persistent_id));
488 status = smbXsrv_open_cleanup(ctx->msg.open_persistent_id);
489 if (!NT_STATUS_IS_OK(status)) {
490 DEBUG(2, ("Failed to cleanup open global for file %s open %llu:"
491 " %s\n", file_id_string_tos(&ctx->msg.file_id),
492 (unsigned long long)ctx->msg.open_persistent_id,
493 nt_errstr(status)));
497 static void scavenger_add_timer(struct smbd_scavenger_state *state,
498 struct scavenger_message *msg)
500 struct tevent_timer *te;
501 struct scavenger_timer_context *ctx;
502 struct timeval until;
504 nttime_to_timeval(&until, msg->until);
506 DEBUG(10, ("scavenger: schedule file %s for cleanup at %s\n",
507 file_id_string_tos(&msg->file_id),
508 timeval_string(talloc_tos(), &until, true)));
510 ctx = talloc_zero(state, struct scavenger_timer_context);
511 if (ctx == NULL) {
512 DEBUG(2, ("Failed to talloc_zero(scavenger_timer_context)\n"));
513 return;
516 ctx->state = state;
517 ctx->msg = *msg;
519 te = tevent_add_timer(state->ev,
520 state,
521 until,
522 scavenger_timer,
523 ctx);
524 if (te == NULL) {
525 DEBUG(2, ("Failed to add scavenger_timer event\n"));
526 talloc_free(ctx);
527 return;
530 /* delete context after handler was running */
531 talloc_steal(te, ctx);