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"
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
;
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
;
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
)));
56 TALLOC_CTX
*frame
= talloc_stackframe();
59 ret
= tevent_loop_once(state
->ev
);
61 DEBUG(2, ("tevent_loop_once failed: %s\n",
67 DEBUG(10, ("scavenger: %s event loop iteration\n",
68 server_id_str(talloc_tos(), state
->scavenger_id
)));
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
,
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
,
118 scavenger_sig_term_handler
,
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
) {
131 return serverid_exists(state
->scavenger_id
);
134 static int smbd_scavenger_server_id_destructor(struct server_id
*id
)
136 serverid_deregister(*id
);
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
);
146 while (remaining
> 0) {
149 ret
= sys_write(fd
, msg
+ ofs
, remaining
);
151 DEBUG(2, ("Failed to write to pipe: %s\n",
158 DEBUG(4, ("scavenger_say_hello: self[%s]\n",
159 server_id_str(talloc_tos(), &self
)));
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
);
169 while (remaining
> 0) {
172 ret
= sys_read(fd
, msg
+ ofs
, remaining
);
174 DEBUG(2, ("Failed to read from pipe: %s\n",
181 DEBUG(4, ("scavenger_say_hello: child[%s]\n",
182 server_id_str(talloc_tos(), child
)));
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
;
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
)));
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"));
216 talloc_set_destructor(state
->scavenger_id
,
217 smbd_scavenger_server_id_destructor
);
219 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
221 DEBUG(2, ("socketpair failed: %s", strerror(errno
)));
225 smb_set_close_on_exec(fds
[0]);
226 smb_set_close_on_exec(fds
[1]);
228 unique_id
= serverid_get_random_unique_id();
235 DEBUG(0, ("fork failed: %s", strerror(err
)));
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",
254 exit_server("reinit_after_fork failed");
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
);
269 DEBUG(2, ("scavenger_say_hello failed\n"));
270 exit_server("scavenger_say_hello failed");
274 fde
= tevent_add_fd(state
->ev
, state
->scavenger_id
,
275 fds
[1], TEVENT_FD_READ
,
276 smbd_scavenger_parent_dead
, state
);
278 DEBUG(2, ("tevent_add_fd(smbd_scavenger_parent_dead) "
280 exit_server("tevent_add_fd(smbd_scavenger_parent_dead) "
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");
296 ok
= scavenger_wait_hello(fds
[0], state
->scavenger_id
);
302 fde
= tevent_add_fd(state
->ev
, state
->scavenger_id
,
303 fds
[0], TEVENT_FD_READ
,
304 smbd_scavenger_done
, state
);
309 tevent_fd_set_auto_close(fde
);
313 TALLOC_FREE(state
->scavenger_id
);
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
,
323 struct server_id src
,
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
)) {
340 if (!smbd_scavenger_running(state
) &&
341 !smbd_scavenger_start(state
))
343 DEBUG(2, ("Failed to start scavenger\n"));
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
)));
358 if (!state
->am_scavenger
) {
359 DEBUG(10, ("im not the scavenger: ignore message\n"));
363 if (!server_id_equal(&state
->parent_id
, &src
)) {
364 DEBUG(10, ("scavenger: ignore spurious message\n"));
368 DEBUG(10, ("scavenger: got a message\n"));
369 msg
= (struct scavenger_message
*)data
->data
;
370 scavenger_add_timer(state
, msg
);
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
;
382 if (smbd_scavenger_state
) {
383 DEBUG(10, ("smbd_scavenger_init called again\n"));
387 state
= talloc_zero(mem_ctx
, struct smbd_scavenger_state
);
389 DEBUG(2, ("Out of memory\n"));
395 state
->parent_id
= messaging_server_id(msg
);
397 status
= messaging_register(msg
, state
, MSG_SMB_SCAVENGER
,
399 if (!NT_STATUS_IS_OK(status
)) {
400 DEBUG(2, ("failed to register message handler: %s\n",
405 smbd_scavenger_state
= state
;
412 void scavenger_schedule_disconnected(struct files_struct
*fsp
)
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
;
421 if (fsp
->op
== NULL
) {
424 nttime_to_timeval(&disconnect_time
, fsp
->op
->global
->disconnect_time
);
425 timeout_usec
= 1000 * fsp
->op
->global
->durable_timeout_msec
;
426 until
= timeval_add(&disconnect_time
,
427 timeout_usec
/ 1000000,
428 timeout_usec
% 1000000);
431 msg
.file_id
= fsp
->file_id
;
432 msg
.open_persistent_id
= fsp
->op
->global
->open_persistent_id
;
433 msg
.until
= timeval_to_nttime(&until
);
435 DEBUG(10, ("smbd: %s mark file %s as disconnected at %s with timeout "
437 server_id_str(talloc_tos(), &self
),
438 file_id_string_tos(&fsp
->file_id
),
439 timeval_string(talloc_tos(), &disconnect_time
, true),
440 timeval_string(talloc_tos(), &until
, true),
441 fsp
->op
->global
->durable_timeout_msec
/1000.0));
443 SMB_ASSERT(server_id_is_disconnected(&fsp
->op
->global
->server_id
));
444 SMB_ASSERT(!server_id_equal(&self
, &smbd_scavenger_state
->parent_id
));
445 SMB_ASSERT(!smbd_scavenger_state
->am_scavenger
);
447 msg_blob
= data_blob_const(&msg
, sizeof(msg
));
448 DEBUG(10, ("send message to scavenger\n"));
450 status
= messaging_send(smbd_scavenger_state
->msg
,
451 smbd_scavenger_state
->parent_id
,
454 if (!NT_STATUS_IS_OK(status
)) {
455 DEBUG(2, ("Failed to send message to parent smbd %s "
457 server_id_str(talloc_tos(),
458 &smbd_scavenger_state
->parent_id
),
459 server_id_str(talloc_tos(), &self
),
464 struct scavenger_timer_context
{
465 struct smbd_scavenger_state
*state
;
466 struct scavenger_message msg
;
469 static void scavenger_timer(struct tevent_context
*ev
,
470 struct tevent_timer
*te
,
471 struct timeval t
, void *data
)
473 struct scavenger_timer_context
*ctx
=
474 talloc_get_type_abort(data
, struct scavenger_timer_context
);
478 DEBUG(10, ("scavenger: do cleanup for file %s at %s\n",
479 file_id_string_tos(&ctx
->msg
.file_id
),
480 timeval_string(talloc_tos(), &t
, true)));
482 ok
= share_mode_cleanup_disconnected(ctx
->msg
.file_id
,
483 ctx
->msg
.open_persistent_id
);
485 DEBUG(2, ("Failed to cleanup share modes and byte range locks "
486 "for file %s open %llu\n",
487 file_id_string_tos(&ctx
->msg
.file_id
),
488 (unsigned long long)ctx
->msg
.open_persistent_id
));
491 status
= smbXsrv_open_cleanup(ctx
->msg
.open_persistent_id
);
492 if (!NT_STATUS_IS_OK(status
)) {
493 DEBUG(2, ("Failed to cleanup open global for file %s open %llu:"
494 " %s\n", file_id_string_tos(&ctx
->msg
.file_id
),
495 (unsigned long long)ctx
->msg
.open_persistent_id
,
500 static void scavenger_add_timer(struct smbd_scavenger_state
*state
,
501 struct scavenger_message
*msg
)
503 struct tevent_timer
*te
;
504 struct scavenger_timer_context
*ctx
;
505 struct timeval until
;
507 nttime_to_timeval(&until
, msg
->until
);
509 DEBUG(10, ("scavenger: schedule file %s for cleanup at %s\n",
510 file_id_string_tos(&msg
->file_id
),
511 timeval_string(talloc_tos(), &until
, true)));
513 ctx
= talloc_zero(state
, struct scavenger_timer_context
);
515 DEBUG(2, ("Failed to talloc_zero(scavenger_timer_context)\n"));
522 te
= tevent_add_timer(state
->ev
,
528 DEBUG(2, ("Failed to add scavenger_timer event\n"));
533 /* delete context after handler was running */
534 talloc_steal(te
, ctx
);