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