2 Unix SMB/CIFS implementation.
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/>.
25 #include "smbd/globals.h"
26 #include "smbd/scavenger.h"
27 #include "locking/proto.h"
28 #include "lib/util/util_process.h"
29 #include "lib/sys_rw.h"
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
;
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
;
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
)));
59 TALLOC_CTX
*frame
= talloc_stackframe();
62 ret
= tevent_loop_once(state
->ev
);
64 DEBUG(2, ("tevent_loop_once failed: %s\n",
70 DEBUG(10, ("scavenger: %s event loop iteration\n",
71 server_id_str_buf(*state
->scavenger_id
, &tmp1
)));
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
,
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
,
123 scavenger_sig_term_handler
,
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
) {
136 return serverid_exists(state
->scavenger_id
);
139 static int smbd_scavenger_server_id_destructor(struct server_id
*id
)
141 serverid_deregister(*id
);
145 static bool scavenger_say_hello(int fd
, struct server_id self
)
147 const uint8_t *msg
= (const uint8_t *)&self
;
148 size_t remaining
= sizeof(self
);
150 struct server_id_buf tmp
;
152 while (remaining
> 0) {
155 ret
= sys_write(fd
, msg
+ ofs
, remaining
);
157 DEBUG(2, ("Failed to write to pipe: %s\n",
164 DEBUG(4, ("scavenger_say_hello: self[%s]\n",
165 server_id_str_buf(self
, &tmp
)));
169 static bool scavenger_wait_hello(int fd
, struct server_id
*child
)
171 uint8_t *msg
= (uint8_t *)child
;
172 size_t remaining
= sizeof(*child
);
174 struct server_id_buf tmp
;
176 while (remaining
> 0) {
179 ret
= sys_read(fd
, msg
+ ofs
, remaining
);
181 DEBUG(2, ("Failed to read from pipe: %s\n",
188 DEBUG(4, ("scavenger_say_hello: child[%s]\n",
189 server_id_str_buf(*child
, &tmp
)));
193 static bool smbd_scavenger_start(struct smbd_scavenger_state
*state
)
195 struct server_id self
= messaging_server_id(state
->msg
);
196 struct tevent_fd
*fde
= NULL
;
202 SMB_ASSERT(server_id_equal(&state
->parent_id
, &self
));
204 if (smbd_scavenger_running(state
)) {
205 struct server_id_buf tmp
;
206 DEBUG(10, ("scavenger %s already running\n",
207 server_id_str_buf(*state
->scavenger_id
,
212 if (state
->scavenger_id
!= NULL
) {
213 struct server_id_buf tmp
;
214 DEBUG(10, ("scavenger zombie %s, cleaning up\n",
215 server_id_str_buf(*state
->scavenger_id
,
217 TALLOC_FREE(state
->scavenger_id
);
220 state
->scavenger_id
= talloc_zero(state
, struct server_id
);
221 if (state
->scavenger_id
== NULL
) {
222 DEBUG(2, ("Out of memory\n"));
225 talloc_set_destructor(state
->scavenger_id
,
226 smbd_scavenger_server_id_destructor
);
228 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
230 DEBUG(2, ("socketpair failed: %s", strerror(errno
)));
234 smb_set_close_on_exec(fds
[0]);
235 smb_set_close_on_exec(fds
[1]);
237 unique_id
= serverid_get_random_unique_id();
244 DEBUG(0, ("fork failed: %s", strerror(err
)));
255 set_my_unique_id(unique_id
);
257 status
= smbd_reinit_after_fork(state
->msg
, state
->ev
,
258 true, "smbd-scavenger");
259 if (!NT_STATUS_IS_OK(status
)) {
260 DEBUG(2, ("reinit_after_fork failed: %s\n",
262 exit_server("reinit_after_fork failed");
266 state
->am_scavenger
= true;
267 *state
->scavenger_id
= messaging_server_id(state
->msg
);
269 scavenger_setup_sig_term_handler(state
->ev
);
271 if (!serverid_register(*state
->scavenger_id
,
273 DBG_WARNING("serverid_register failed");
274 exit_server("serverid_register failed");
278 ok
= scavenger_say_hello(fds
[1], *state
->scavenger_id
);
280 DEBUG(2, ("scavenger_say_hello failed\n"));
281 exit_server("scavenger_say_hello failed");
285 fde
= tevent_add_fd(state
->ev
, state
->scavenger_id
,
286 fds
[1], TEVENT_FD_READ
,
287 smbd_scavenger_parent_dead
, state
);
289 DEBUG(2, ("tevent_add_fd(smbd_scavenger_parent_dead) "
291 exit_server("tevent_add_fd(smbd_scavenger_parent_dead) "
295 tevent_fd_set_auto_close(fde
);
297 ret
= smbd_scavenger_main(state
);
299 DEBUG(10, ("scavenger ended: %d\n", ret
));
300 exit_server_cleanly("scavenger ended");
307 ok
= scavenger_wait_hello(fds
[0], state
->scavenger_id
);
313 fde
= tevent_add_fd(state
->ev
, state
->scavenger_id
,
314 fds
[0], TEVENT_FD_READ
,
315 smbd_scavenger_done
, state
);
320 tevent_fd_set_auto_close(fde
);
324 TALLOC_FREE(state
->scavenger_id
);
328 static void scavenger_add_timer(struct smbd_scavenger_state
*state
,
329 struct scavenger_message
*msg
);
331 static void smbd_scavenger_msg(struct messaging_context
*msg_ctx
,
334 struct server_id src
,
337 struct smbd_scavenger_state
*state
=
338 talloc_get_type_abort(private_data
,
339 struct smbd_scavenger_state
);
340 TALLOC_CTX
*frame
= talloc_stackframe();
341 struct server_id self
= messaging_server_id(msg_ctx
);
342 struct scavenger_message
*msg
= NULL
;
343 struct server_id_buf tmp1
, tmp2
;
345 DEBUG(10, ("smbd_scavenger_msg: %s got message from %s\n",
346 server_id_str_buf(self
, &tmp1
),
347 server_id_str_buf(src
, &tmp2
)));
349 if (server_id_equal(&state
->parent_id
, &self
)) {
352 if (!smbd_scavenger_running(state
) &&
353 !smbd_scavenger_start(state
))
355 DEBUG(2, ("Failed to start scavenger\n"));
358 DEBUG(10, ("forwarding message to scavenger\n"));
360 status
= messaging_send(msg_ctx
,
361 *state
->scavenger_id
, msg_type
, data
);
362 if (!NT_STATUS_IS_OK(status
)) {
363 DEBUG(2, ("forwarding message to scavenger failed: "
364 "%s\n", nt_errstr(status
)));
370 if (!state
->am_scavenger
) {
371 DEBUG(10, ("im not the scavenger: ignore message\n"));
375 if (!server_id_equal(&state
->parent_id
, &src
)) {
376 DEBUG(10, ("scavenger: ignore spurious message\n"));
380 DEBUG(10, ("scavenger: got a message\n"));
381 msg
= (struct scavenger_message
*)data
->data
;
382 scavenger_add_timer(state
, msg
);
387 bool smbd_scavenger_init(TALLOC_CTX
*mem_ctx
,
388 struct messaging_context
*msg
,
389 struct tevent_context
*ev
)
391 struct smbd_scavenger_state
*state
;
394 if (smbd_scavenger_state
) {
395 DEBUG(10, ("smbd_scavenger_init called again\n"));
399 state
= talloc_zero(mem_ctx
, struct smbd_scavenger_state
);
401 DEBUG(2, ("Out of memory\n"));
407 state
->parent_id
= messaging_server_id(msg
);
409 status
= messaging_register(msg
, state
, MSG_SMB_SCAVENGER
,
411 if (!NT_STATUS_IS_OK(status
)) {
412 DEBUG(2, ("failed to register message handler: %s\n",
417 smbd_scavenger_state
= state
;
424 void scavenger_schedule_disconnected(struct files_struct
*fsp
)
427 struct server_id self
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
428 struct timeval disconnect_time
, until
;
429 uint64_t timeout_usec
;
430 struct scavenger_message msg
;
432 struct server_id_buf tmp
;
434 if (fsp
->op
== NULL
) {
437 nttime_to_timeval(&disconnect_time
, fsp
->op
->global
->disconnect_time
);
438 timeout_usec
= 1000 * fsp
->op
->global
->durable_timeout_msec
;
439 until
= timeval_add(&disconnect_time
,
440 timeout_usec
/ 1000000,
441 timeout_usec
% 1000000);
444 msg
.file_id
= fsp
->file_id
;
445 msg
.open_persistent_id
= fsp
->op
->global
->open_persistent_id
;
446 msg
.until
= timeval_to_nttime(&until
);
448 DEBUG(10, ("smbd: %s mark file %s as disconnected at %s with timeout "
450 server_id_str_buf(self
, &tmp
),
451 file_id_string_tos(&fsp
->file_id
),
452 timeval_string(talloc_tos(), &disconnect_time
, true),
453 timeval_string(talloc_tos(), &until
, true),
454 fsp
->op
->global
->durable_timeout_msec
/1000.0));
456 SMB_ASSERT(server_id_is_disconnected(&fsp
->op
->global
->server_id
));
457 SMB_ASSERT(!server_id_equal(&self
, &smbd_scavenger_state
->parent_id
));
458 SMB_ASSERT(!smbd_scavenger_state
->am_scavenger
);
460 msg_blob
= data_blob_const(&msg
, sizeof(msg
));
461 DEBUG(10, ("send message to scavenger\n"));
463 status
= messaging_send(smbd_scavenger_state
->msg
,
464 smbd_scavenger_state
->parent_id
,
467 if (!NT_STATUS_IS_OK(status
)) {
468 struct server_id_buf tmp1
, tmp2
;
469 DEBUG(2, ("Failed to send message to parent smbd %s "
471 server_id_str_buf(smbd_scavenger_state
->parent_id
,
473 server_id_str_buf(self
, &tmp2
),
478 struct scavenger_timer_context
{
479 struct smbd_scavenger_state
*state
;
480 struct scavenger_message msg
;
483 static void scavenger_timer(struct tevent_context
*ev
,
484 struct tevent_timer
*te
,
485 struct timeval t
, void *data
)
487 struct scavenger_timer_context
*ctx
=
488 talloc_get_type_abort(data
, struct scavenger_timer_context
);
492 DEBUG(10, ("scavenger: do cleanup for file %s at %s\n",
493 file_id_string_tos(&ctx
->msg
.file_id
),
494 timeval_string(talloc_tos(), &t
, true)));
496 ok
= share_mode_cleanup_disconnected(ctx
->msg
.file_id
,
497 ctx
->msg
.open_persistent_id
);
499 DEBUG(2, ("Failed to cleanup share modes and byte range locks "
500 "for file %s open %llu\n",
501 file_id_string_tos(&ctx
->msg
.file_id
),
502 (unsigned long long)ctx
->msg
.open_persistent_id
));
505 status
= smbXsrv_open_cleanup(ctx
->msg
.open_persistent_id
);
506 if (!NT_STATUS_IS_OK(status
)) {
507 DEBUG(2, ("Failed to cleanup open global for file %s open %llu:"
508 " %s\n", file_id_string_tos(&ctx
->msg
.file_id
),
509 (unsigned long long)ctx
->msg
.open_persistent_id
,
514 static void scavenger_add_timer(struct smbd_scavenger_state
*state
,
515 struct scavenger_message
*msg
)
517 struct tevent_timer
*te
;
518 struct scavenger_timer_context
*ctx
;
519 struct timeval until
;
521 nttime_to_timeval(&until
, msg
->until
);
523 DEBUG(10, ("scavenger: schedule file %s for cleanup at %s\n",
524 file_id_string_tos(&msg
->file_id
),
525 timeval_string(talloc_tos(), &until
, true)));
527 ctx
= talloc_zero(state
, struct scavenger_timer_context
);
529 DEBUG(2, ("Failed to talloc_zero(scavenger_timer_context)\n"));
536 te
= tevent_add_timer(state
->ev
,
542 DEBUG(2, ("Failed to add scavenger_timer event\n"));
547 /* delete context after handler was running */
548 talloc_steal(te
, ctx
);