lib: Remove server_id_str()
[samba.git] / source3 / smbd / scavenger.c
1 /*
2    Unix SMB/CIFS implementation.
3    smbd scavenger daemon
4
5    Copyright (C) Gregor Beck                    2013
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22
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"
29 #include "lib/sys_rw.h"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_SCAVENGER
33
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;
40 };
41
42 static struct smbd_scavenger_state *smbd_scavenger_state = NULL;
43
44 struct scavenger_message {
45         struct file_id file_id;
46         uint64_t open_persistent_id;
47         NTTIME until;
48 };
49
50 static int smbd_scavenger_main(struct smbd_scavenger_state *state)
51 {
52         struct server_id_buf tmp1, tmp2;
53
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)));
57
58         while (true) {
59                 TALLOC_CTX *frame = talloc_stackframe();
60                 int ret;
61
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;
68                 }
69
70                 DEBUG(10, ("scavenger: %s event loop iteration\n",
71                            server_id_str_buf(*state->scavenger_id, &tmp1)));
72                 TALLOC_FREE(frame);
73         }
74
75         return 0;
76 }
77
78 static void smbd_scavenger_done(struct tevent_context *event_ctx, struct tevent_fd *fde,
79                                 uint16_t flags, void *private_data)
80 {
81         struct smbd_scavenger_state *state = talloc_get_type_abort(
82                 private_data, struct smbd_scavenger_state);
83         struct server_id_buf tmp;
84
85         DEBUG(2, ("scavenger: %s died\n",
86                   server_id_str_buf(*state->scavenger_id, &tmp)));
87
88         TALLOC_FREE(state->scavenger_id);
89 }
90
91 static void smbd_scavenger_parent_dead(struct tevent_context *event_ctx,
92                                        struct tevent_fd *fde,
93                                        uint16_t flags, void *private_data)
94 {
95         struct smbd_scavenger_state *state = talloc_get_type_abort(
96                 private_data, struct smbd_scavenger_state);
97         struct server_id_buf tmp1, tmp2;
98
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)));
102
103         exit_server("smbd_scavenger_parent_dead");
104 }
105
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)
112 {
113         exit_server_cleanly("termination signal");
114 }
115
116 static void scavenger_setup_sig_term_handler(struct tevent_context *ev_ctx)
117 {
118         struct tevent_signal *se;
119
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");
127         }
128 }
129
130 static bool smbd_scavenger_running(struct smbd_scavenger_state *state)
131 {
132         if (state->scavenger_id == NULL) {
133                 return false;
134         }
135
136         return serverid_exists(state->scavenger_id);
137 }
138
139 static int smbd_scavenger_server_id_destructor(struct server_id *id)
140 {
141         serverid_deregister(*id);
142         return 0;
143 }
144
145 static bool scavenger_say_hello(int fd, struct server_id self)
146 {
147         const uint8_t *msg = (const uint8_t *)&self;
148         size_t remaining = sizeof(self);
149         size_t ofs = 0;
150         struct server_id_buf tmp;
151
152         while (remaining > 0) {
153                 ssize_t ret;
154
155                 ret = sys_write(fd, msg + ofs, remaining);
156                 if (ret == -1) {
157                         DEBUG(2, ("Failed to write to pipe: %s\n",
158                                   strerror(errno)));
159                         return false;
160                 }
161                 remaining -= ret;
162         }
163
164         DEBUG(4, ("scavenger_say_hello: self[%s]\n",
165                   server_id_str_buf(self, &tmp)));
166         return true;
167 }
168
169 static bool scavenger_wait_hello(int fd, struct server_id *child)
170 {
171         uint8_t *msg = (uint8_t *)child;
172         size_t remaining = sizeof(*child);
173         size_t ofs = 0;
174         struct server_id_buf tmp;
175
176         while (remaining > 0) {
177                 ssize_t ret;
178
179                 ret = sys_read(fd, msg + ofs, remaining);
180                 if (ret == -1) {
181                         DEBUG(2, ("Failed to read from pipe: %s\n",
182                                   strerror(errno)));
183                         return false;
184                 }
185                 remaining -= ret;
186         }
187
188         DEBUG(4, ("scavenger_say_hello: child[%s]\n",
189                   server_id_str_buf(*child, &tmp)));
190         return true;
191 }
192
193 static bool smbd_scavenger_start(struct smbd_scavenger_state *state)
194 {
195         struct server_id self = messaging_server_id(state->msg);
196         struct tevent_fd *fde = NULL;
197         int fds[2];
198         int ret;
199         uint64_t unique_id;
200         bool ok;
201
202         SMB_ASSERT(server_id_equal(&state->parent_id, &self));
203
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,
208                                              &tmp)));
209                 return true;
210         }
211
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,
216                                              &tmp)));
217                 TALLOC_FREE(state->scavenger_id);
218         }
219
220         state->scavenger_id = talloc_zero(state, struct server_id);
221         if (state->scavenger_id == NULL) {
222                 DEBUG(2, ("Out of memory\n"));
223                 goto fail;
224         }
225         talloc_set_destructor(state->scavenger_id,
226                               smbd_scavenger_server_id_destructor);
227
228         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
229         if (ret == -1) {
230                 DEBUG(2, ("socketpair failed: %s", strerror(errno)));
231                 goto fail;
232         }
233
234         smb_set_close_on_exec(fds[0]);
235         smb_set_close_on_exec(fds[1]);
236
237         unique_id = serverid_get_random_unique_id();
238
239         ret = fork();
240         if (ret == -1) {
241                 int err = errno;
242                 close(fds[0]);
243                 close(fds[1]);
244                 DEBUG(0, ("fork failed: %s", strerror(err)));
245                 goto fail;
246         }
247
248         if (ret == 0) {
249                 /* child */
250
251                 NTSTATUS status;
252
253                 close(fds[0]);
254
255                 set_my_unique_id(unique_id);
256
257                 status = smbd_reinit_after_fork(state->msg, state->ev, true);
258                 if (!NT_STATUS_IS_OK(status)) {
259                         DEBUG(2, ("reinit_after_fork failed: %s\n",
260                                   nt_errstr(status)));
261                         exit_server("reinit_after_fork failed");
262                         return false;
263                 }
264
265                 prctl_set_comment("smbd-scavenger");
266
267                 state->am_scavenger = true;
268                 *state->scavenger_id = messaging_server_id(state->msg);
269
270                 scavenger_setup_sig_term_handler(state->ev);
271
272                 serverid_register(*state->scavenger_id, FLAG_MSG_GENERAL);
273
274                 ok = scavenger_say_hello(fds[1], *state->scavenger_id);
275                 if (!ok) {
276                         DEBUG(2, ("scavenger_say_hello failed\n"));
277                         exit_server("scavenger_say_hello failed");
278                         return false;
279                 }
280
281                 fde = tevent_add_fd(state->ev, state->scavenger_id,
282                                     fds[1], TEVENT_FD_READ,
283                                     smbd_scavenger_parent_dead, state);
284                 if (fde == NULL) {
285                         DEBUG(2, ("tevent_add_fd(smbd_scavenger_parent_dead) "
286                                   "failed\n"));
287                         exit_server("tevent_add_fd(smbd_scavenger_parent_dead) "
288                                     "failed");
289                         return false;
290                 }
291                 tevent_fd_set_auto_close(fde);
292
293                 ret = smbd_scavenger_main(state);
294
295                 DEBUG(10, ("scavenger ended: %d\n", ret));
296                 exit_server_cleanly("scavenger ended");
297                 return false;
298         }
299
300         /* parent */
301         close(fds[1]);
302
303         ok = scavenger_wait_hello(fds[0], state->scavenger_id);
304         if (!ok) {
305                 close(fds[0]);
306                 goto fail;
307         }
308
309         fde = tevent_add_fd(state->ev, state->scavenger_id,
310                             fds[0], TEVENT_FD_READ,
311                             smbd_scavenger_done, state);
312         if (fde == NULL) {
313                 close(fds[0]);
314                 goto fail;
315         }
316         tevent_fd_set_auto_close(fde);
317
318         return true;
319 fail:
320         TALLOC_FREE(state->scavenger_id);
321         return false;
322 }
323
324 static void scavenger_add_timer(struct smbd_scavenger_state *state,
325                                 struct scavenger_message *msg);
326
327 static void smbd_scavenger_msg(struct messaging_context *msg_ctx,
328                                void *private_data,
329                                uint32_t msg_type,
330                                struct server_id src,
331                                DATA_BLOB *data)
332 {
333         struct smbd_scavenger_state *state =
334                 talloc_get_type_abort(private_data,
335                                       struct smbd_scavenger_state);
336         TALLOC_CTX *frame = talloc_stackframe();
337         struct server_id self = messaging_server_id(msg_ctx);
338         struct scavenger_message *msg = NULL;
339         struct server_id_buf tmp1, tmp2;
340
341         DEBUG(10, ("smbd_scavenger_msg: %s got message from %s\n",
342                    server_id_str_buf(self, &tmp1),
343                    server_id_str_buf(src, &tmp2)));
344
345         if (server_id_equal(&state->parent_id, &self)) {
346                 NTSTATUS status;
347
348                 if (!smbd_scavenger_running(state) &&
349                     !smbd_scavenger_start(state))
350                 {
351                         DEBUG(2, ("Failed to start scavenger\n"));
352                         goto done;
353                 }
354                 DEBUG(10, ("forwarding message to scavenger\n"));
355
356                 status = messaging_send(msg_ctx,
357                                         *state->scavenger_id, msg_type, data);
358                 if (!NT_STATUS_IS_OK(status)) {
359                         DEBUG(2, ("forwarding message to scavenger failed: "
360                                   "%s\n", nt_errstr(status)));
361                         goto done;
362                 }
363                 goto done;
364         }
365
366         if (!state->am_scavenger) {
367                 DEBUG(10, ("im not the scavenger: ignore message\n"));
368                 goto done;
369         }
370
371         if (!server_id_equal(&state->parent_id, &src)) {
372                 DEBUG(10, ("scavenger: ignore spurious message\n"));
373                 goto done;
374         }
375
376         DEBUG(10, ("scavenger: got a message\n"));
377         msg = (struct scavenger_message*)data->data;
378         scavenger_add_timer(state, msg);
379 done:
380         talloc_free(frame);
381 }
382
383 bool smbd_scavenger_init(TALLOC_CTX *mem_ctx,
384                          struct messaging_context *msg,
385                          struct tevent_context *ev)
386 {
387         struct smbd_scavenger_state *state;
388         NTSTATUS status;
389
390         if (smbd_scavenger_state) {
391                 DEBUG(10, ("smbd_scavenger_init called again\n"));
392                 return true;
393         }
394
395         state = talloc_zero(mem_ctx, struct smbd_scavenger_state);
396         if (state == NULL) {
397                 DEBUG(2, ("Out of memory\n"));
398                 return false;
399         }
400
401         state->msg = msg;
402         state->ev = ev;
403         state->parent_id = messaging_server_id(msg);
404
405         status = messaging_register(msg, state, MSG_SMB_SCAVENGER,
406                                     smbd_scavenger_msg);
407         if (!NT_STATUS_IS_OK(status)) {
408                 DEBUG(2, ("failed to register message handler: %s\n",
409                           nt_errstr(status)));
410                 goto fail;
411         }
412
413         smbd_scavenger_state = state;
414         return true;
415 fail:
416         talloc_free(state);
417         return false;
418 }
419
420 void scavenger_schedule_disconnected(struct files_struct *fsp)
421 {
422         NTSTATUS status;
423         struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
424         struct timeval disconnect_time, until;
425         uint64_t timeout_usec;
426         struct scavenger_message msg;
427         DATA_BLOB msg_blob;
428         struct server_id_buf tmp;
429
430         if (fsp->op == NULL) {
431                 return;
432         }
433         nttime_to_timeval(&disconnect_time, fsp->op->global->disconnect_time);
434         timeout_usec = 1000 * fsp->op->global->durable_timeout_msec;
435         until = timeval_add(&disconnect_time,
436                             timeout_usec / 1000000,
437                             timeout_usec % 1000000);
438
439         ZERO_STRUCT(msg);
440         msg.file_id = fsp->file_id;
441         msg.open_persistent_id = fsp->op->global->open_persistent_id;
442         msg.until = timeval_to_nttime(&until);
443
444         DEBUG(10, ("smbd: %s mark file %s as disconnected at %s with timeout "
445                    "at %s in %fs\n",
446                    server_id_str_buf(self, &tmp),
447                    file_id_string_tos(&fsp->file_id),
448                    timeval_string(talloc_tos(), &disconnect_time, true),
449                    timeval_string(talloc_tos(), &until, true),
450                    fsp->op->global->durable_timeout_msec/1000.0));
451
452         SMB_ASSERT(server_id_is_disconnected(&fsp->op->global->server_id));
453         SMB_ASSERT(!server_id_equal(&self, &smbd_scavenger_state->parent_id));
454         SMB_ASSERT(!smbd_scavenger_state->am_scavenger);
455
456         msg_blob = data_blob_const(&msg, sizeof(msg));
457         DEBUG(10, ("send message to scavenger\n"));
458
459         status = messaging_send(smbd_scavenger_state->msg,
460                                 smbd_scavenger_state->parent_id,
461                                 MSG_SMB_SCAVENGER,
462                                 &msg_blob);
463         if (!NT_STATUS_IS_OK(status)) {
464                 struct server_id_buf tmp1, tmp2;
465                 DEBUG(2, ("Failed to send message to parent smbd %s "
466                           "from %s: %s\n",
467                           server_id_str_buf(smbd_scavenger_state->parent_id,
468                                             &tmp1),
469                           server_id_str_buf(self, &tmp2),
470                           nt_errstr(status)));
471         }
472 }
473
474 struct scavenger_timer_context {
475         struct smbd_scavenger_state *state;
476         struct scavenger_message msg;
477 };
478
479 static void scavenger_timer(struct tevent_context *ev,
480                             struct tevent_timer *te,
481                             struct timeval t, void *data)
482 {
483         struct scavenger_timer_context *ctx =
484                 talloc_get_type_abort(data, struct scavenger_timer_context);
485         NTSTATUS status;
486         bool ok;
487
488         DEBUG(10, ("scavenger: do cleanup for file %s at %s\n",
489                   file_id_string_tos(&ctx->msg.file_id),
490                   timeval_string(talloc_tos(), &t, true)));
491
492         ok = share_mode_cleanup_disconnected(ctx->msg.file_id,
493                                              ctx->msg.open_persistent_id);
494         if (!ok) {
495                 DEBUG(2, ("Failed to cleanup share modes and byte range locks "
496                           "for file %s open %llu\n",
497                           file_id_string_tos(&ctx->msg.file_id),
498                           (unsigned long long)ctx->msg.open_persistent_id));
499         }
500
501         status = smbXsrv_open_cleanup(ctx->msg.open_persistent_id);
502         if (!NT_STATUS_IS_OK(status)) {
503                 DEBUG(2, ("Failed to cleanup open global for file %s open %llu:"
504                           " %s\n", file_id_string_tos(&ctx->msg.file_id),
505                           (unsigned long long)ctx->msg.open_persistent_id,
506                           nt_errstr(status)));
507         }
508 }
509
510 static void scavenger_add_timer(struct smbd_scavenger_state *state,
511                                 struct scavenger_message *msg)
512 {
513         struct tevent_timer *te;
514         struct scavenger_timer_context *ctx;
515         struct timeval until;
516
517         nttime_to_timeval(&until, msg->until);
518
519         DEBUG(10, ("scavenger: schedule file %s for cleanup at %s\n",
520                    file_id_string_tos(&msg->file_id),
521                    timeval_string(talloc_tos(), &until, true)));
522
523         ctx = talloc_zero(state, struct scavenger_timer_context);
524         if (ctx == NULL) {
525                 DEBUG(2, ("Failed to talloc_zero(scavenger_timer_context)\n"));
526                 return;
527         }
528
529         ctx->state = state;
530         ctx->msg = *msg;
531
532         te = tevent_add_timer(state->ev,
533                               state,
534                               until,
535                               scavenger_timer,
536                               ctx);
537         if (te == NULL) {
538                 DEBUG(2, ("Failed to add scavenger_timer event\n"));
539                 talloc_free(ctx);
540                 return;
541         }
542
543         /* delete context after handler was running */
544         talloc_steal(te, ctx);
545 }