ctdb-common: Add async version of shutdown in sock_daemon
[samba.git] / ctdb / common / sock_daemon.c
1 /*
2    A server based on unix domain socket
3
4    Copyright (C) Amitay Isaacs  2016
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
24
25 #include <talloc.h>
26 #include <tevent.h>
27
28 #include "lib/async_req/async_sock.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/blocking.h"
31 #include "lib/util/dlinklist.h"
32 #include "lib/util/tevent_unix.h"
33 #include "lib/util/become_daemon.h"
34
35 #include "common/logging.h"
36 #include "common/reqid.h"
37 #include "common/comm.h"
38 #include "common/pidfile.h"
39 #include "common/sock_daemon.h"
40
41 struct sock_socket {
42         struct sock_socket *prev, *next;
43
44         const char *sockpath;
45         struct sock_socket_funcs *funcs;
46         void *private_data;
47
48         int fd;
49         struct tevent_req *req;
50 };
51
52 struct sock_client {
53         struct sock_client *prev, *next;
54
55         struct tevent_req *req;
56         struct sock_client_context *client_ctx;
57 };
58
59 struct sock_client_context {
60         struct tevent_context *ev;
61         struct sock_socket *sock;
62         int fd;
63         struct comm_context *comm;
64
65         struct sock_client *client;
66 };
67
68 struct sock_daemon_context {
69         struct sock_daemon_funcs *funcs;
70         void *private_data;
71
72         struct pidfile_context *pid_ctx;
73         struct sock_socket *socket_list;
74 };
75
76 /*
77  * Process a single client
78  */
79
80 static void sock_client_read_handler(uint8_t *buf, size_t buflen,
81                                      void *private_data);
82 static void sock_client_read_done(struct tevent_req *subreq);
83 static void sock_client_dead_handler(void *private_data);
84 static int sock_client_context_destructor(
85                                 struct sock_client_context *client_ctx);
86
87 static int sock_client_context_init(TALLOC_CTX *mem_ctx,
88                                     struct tevent_context *ev,
89                                     struct sock_socket *sock,
90                                     int client_fd,
91                                     struct sock_client *client,
92                                     struct sock_client_context **result)
93 {
94         struct sock_client_context *client_ctx;
95         int ret;
96
97         client_ctx = talloc_zero(mem_ctx, struct sock_client_context);
98         if (client_ctx == NULL) {
99                 return ENOMEM;
100         }
101
102         client_ctx->ev = ev;
103         client_ctx->sock = sock;
104         client_ctx->fd = client_fd;
105         client_ctx->client = client;
106
107         ret = comm_setup(client_ctx, ev, client_fd,
108                          sock_client_read_handler, client_ctx,
109                          sock_client_dead_handler, client_ctx,
110                          &client_ctx->comm);
111         if (ret != 0) {
112                 talloc_free(client_ctx);
113                 return ret;
114         }
115
116         if (sock->funcs->connect != NULL) {
117                 bool status;
118
119                 status = sock->funcs->connect(client_ctx, sock->private_data);
120                 if (! status) {
121                         talloc_free(client_ctx);
122                         close(client_fd);
123                         return 0;
124                 }
125         }
126
127         talloc_set_destructor(client_ctx, sock_client_context_destructor);
128
129         *result = client_ctx;
130         return 0;
131 }
132
133 static void sock_client_read_handler(uint8_t *buf, size_t buflen,
134                                      void *private_data)
135 {
136         struct sock_client_context *client_ctx = talloc_get_type_abort(
137                 private_data, struct sock_client_context);
138         struct sock_socket *sock = client_ctx->sock;
139         struct tevent_req *subreq;
140
141         subreq = sock->funcs->read_send(client_ctx, client_ctx->ev,
142                                         client_ctx, buf, buflen,
143                                         sock->private_data);
144         if (subreq == NULL) {
145                 talloc_free(client_ctx);
146                 return;
147         }
148         tevent_req_set_callback(subreq, sock_client_read_done, client_ctx);
149 }
150
151 static void sock_client_read_done(struct tevent_req *subreq)
152 {
153         struct sock_client_context *client_ctx = tevent_req_callback_data(
154                 subreq, struct sock_client_context);
155         struct sock_socket *sock = client_ctx->sock;
156         int ret;
157         bool status;
158
159         status = sock->funcs->read_recv(subreq, &ret);
160         if (! status) {
161                 D_ERR("client read failed with ret=%d\n", ret);
162                 talloc_free(client_ctx);
163         }
164 }
165
166 static void sock_client_dead_handler(void *private_data)
167 {
168         struct sock_client_context *client_ctx = talloc_get_type_abort(
169                 private_data, struct sock_client_context);
170         struct sock_socket *sock = client_ctx->sock;
171
172         if (sock->funcs->disconnect != NULL) {
173                 sock->funcs->disconnect(client_ctx, sock->private_data);
174         }
175
176         talloc_free(client_ctx);
177 }
178
179 static int sock_client_context_destructor(
180                                 struct sock_client_context *client_ctx)
181 {
182         TALLOC_FREE(client_ctx->client);
183         TALLOC_FREE(client_ctx->comm);
184         if (client_ctx->fd != -1) {
185                 close(client_ctx->fd);
186                 client_ctx->fd = -1;
187         }
188
189         return 0;
190 }
191
192 /*
193  * Process a single listening socket
194  */
195
196 static int socket_setup(const char *sockpath, bool remove_before_use)
197 {
198         struct sockaddr_un addr;
199         size_t len;
200         int ret, fd;
201
202         memset(&addr, 0, sizeof(addr));
203         addr.sun_family = AF_UNIX;
204
205         len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
206         if (len >= sizeof(addr.sun_path)) {
207                 D_ERR("socket path too long: %s\n", sockpath);
208                 return -1;
209         }
210
211         fd = socket(AF_UNIX, SOCK_STREAM, 0);
212         if (fd == -1) {
213                 D_ERR("socket create failed - %s\n", sockpath);
214                 return -1;
215         }
216
217         ret = set_blocking(fd, false);
218         if (ret != 0) {
219                 D_ERR("socket set nonblocking failed - %s\n", sockpath);
220                 close(fd);
221                 return -1;
222         }
223
224         if (remove_before_use) {
225                 unlink(sockpath);
226         }
227
228         ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
229         if (ret != 0) {
230                 D_ERR("socket bind failed - %s\n", sockpath);
231                 close(fd);
232                 return -1;
233         }
234
235         ret = listen(fd, 10);
236         if (ret != 0) {
237                 D_ERR("socket listen failed - %s\n", sockpath);
238                 close(fd);
239                 return -1;
240         }
241
242         D_NOTICE("listening on %s\n", sockpath);
243
244         return fd;
245 }
246
247 static int sock_socket_destructor(struct sock_socket *sock);
248
249 static int sock_socket_init(TALLOC_CTX *mem_ctx, const char *sockpath,
250                             struct sock_socket_funcs *funcs,
251                             void *private_data,
252                             struct sock_socket **result)
253 {
254         struct sock_socket *sock;
255
256         if (funcs == NULL) {
257                 return EINVAL;
258         }
259         if (funcs->read_send == NULL || funcs->read_recv == NULL) {
260                 return EINVAL;
261         }
262
263         sock = talloc_zero(mem_ctx, struct sock_socket);
264         if (sock == NULL) {
265                 return ENOMEM;
266         }
267
268         sock->sockpath = talloc_strdup(sock, sockpath);
269         if (sock->sockpath == NULL) {
270                 talloc_free(sock);
271                 return ENOMEM;
272         }
273         sock->funcs = funcs;
274         sock->private_data = private_data;
275         sock->fd = -1;
276
277         talloc_set_destructor(sock, sock_socket_destructor);
278
279         *result = sock;
280         return 0;
281 }
282
283 static int sock_socket_destructor(struct sock_socket *sock)
284 {
285         TALLOC_FREE(sock->req);
286
287         if (sock->fd != -1) {
288                 close(sock->fd);
289                 sock->fd = -1;
290         }
291
292         unlink(sock->sockpath);
293         return 0;
294 }
295
296
297 struct sock_socket_start_state {
298         struct tevent_context *ev;
299         struct sock_socket *sock;
300
301         struct sock_client *client_list;
302 };
303
304 static int sock_socket_start_state_destructor(
305                                 struct sock_socket_start_state *state);
306 static void sock_socket_start_new_client(struct tevent_req *subreq);
307 static int sock_socket_start_client_destructor(struct sock_client *client);
308
309 static struct tevent_req *sock_socket_start_send(TALLOC_CTX *mem_ctx,
310                                                  struct tevent_context *ev,
311                                                  struct sock_socket *sock,
312                                                  bool remove_before_use)
313 {
314         struct tevent_req *req, *subreq;
315         struct sock_socket_start_state *state;
316
317         req = tevent_req_create(mem_ctx, &state,
318                                 struct sock_socket_start_state);
319         if (req == NULL) {
320                 return NULL;
321         }
322
323         state->ev = ev;
324         state->sock = sock;
325
326         sock->fd = socket_setup(sock->sockpath, remove_before_use);
327         if (sock->fd == -1) {
328                 tevent_req_error(req, EIO);
329                 return tevent_req_post(req, ev);
330         }
331
332         talloc_set_destructor(state, sock_socket_start_state_destructor);
333
334         subreq = accept_send(state, ev, sock->fd);
335         if (tevent_req_nomem(subreq, req)) {
336                 return tevent_req_post(req, ev);
337         }
338         tevent_req_set_callback(subreq, sock_socket_start_new_client, req);
339
340         sock->req = req;
341
342         return req;
343 }
344
345 static int sock_socket_start_state_destructor(
346                                 struct sock_socket_start_state *state)
347 {
348         struct sock_client *client;
349
350         while ((client = state->client_list) != NULL) {
351                 talloc_free(client);
352         }
353
354         return 0;
355 }
356
357 static void sock_socket_start_new_client(struct tevent_req *subreq)
358 {
359         struct tevent_req *req = tevent_req_callback_data(
360                 subreq, struct tevent_req);
361         struct sock_socket_start_state *state = tevent_req_data(
362                 req, struct sock_socket_start_state);
363         struct sock_client *client;
364         int client_fd, ret;
365
366         client_fd = accept_recv(subreq, NULL, NULL, &ret);
367         TALLOC_FREE(subreq);
368         if (client_fd == -1) {
369                 D_ERR("failed to accept new connection\n");
370         }
371
372         subreq = accept_send(state, state->ev, state->sock->fd);
373         if (tevent_req_nomem(subreq, req)) {
374                 return;
375         }
376         tevent_req_set_callback(subreq, sock_socket_start_new_client, req);
377
378         if (client_fd == -1) {
379                 return;
380         }
381
382         client = talloc_zero(state, struct sock_client);
383         if (tevent_req_nomem(client, req)) {
384                 close(client_fd);
385                 return;
386         }
387
388         client->req = req;
389
390         ret = sock_client_context_init(client, state->ev, state->sock,
391                                        client_fd, client, &client->client_ctx);
392         if (ret != 0) {
393                 talloc_free(client);
394                 return;
395         }
396
397         talloc_set_destructor(client, sock_socket_start_client_destructor);
398         DLIST_ADD(state->client_list, client);
399 }
400
401 static int sock_socket_start_client_destructor(struct sock_client *client)
402 {
403         struct sock_socket_start_state *state = tevent_req_data(
404                 client->req, struct sock_socket_start_state);
405
406         DLIST_REMOVE(state->client_list, client);
407         TALLOC_FREE(client->client_ctx);
408
409         return 0;
410 }
411
412 static bool sock_socket_start_recv(struct tevent_req *req, int *perr,
413                                    TALLOC_CTX *mem_ctx, const char **sockpath)
414 {
415         struct sock_socket_start_state *state = tevent_req_data(
416                 req, struct sock_socket_start_state);
417         int ret;
418
419         state->sock->req = NULL;
420
421         if (tevent_req_is_unix_error(req, &ret)) {
422                 if (perr != NULL) {
423                         *perr = ret;
424                 }
425                 return false;
426         }
427
428         if (sockpath != NULL) {
429                 *sockpath = talloc_steal(mem_ctx, state->sock->sockpath);
430         }
431
432         return true;
433 }
434
435 /*
436  * Send message to a client
437  */
438
439 struct tevent_req *sock_socket_write_send(TALLOC_CTX *mem_ctx,
440                                          struct tevent_context *ev,
441                                          struct sock_client_context *client_ctx,
442                                          uint8_t *buf, size_t buflen)
443 {
444         struct tevent_req *req;
445
446         req = comm_write_send(mem_ctx, ev, client_ctx->comm, buf, buflen);
447
448         return req;
449 }
450
451 bool sock_socket_write_recv(struct tevent_req *req, int *perr)
452 {
453         int ret;
454         bool status;
455
456         status = comm_write_recv(req, &ret);
457         if (! status) {
458                 if (perr != NULL) {
459                         *perr = ret;
460                 }
461         }
462
463         return status;
464 }
465
466 /*
467  * Socket daemon
468  */
469
470 int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
471                       const char *logging, const char *debug_level,
472                       struct sock_daemon_funcs *funcs,
473                       void *private_data,
474                       struct sock_daemon_context **out)
475 {
476         struct sock_daemon_context *sockd;
477         int ret;
478
479         sockd = talloc_zero(mem_ctx, struct sock_daemon_context);
480         if (sockd == NULL) {
481                 return ENOMEM;
482         }
483
484         sockd->funcs = funcs;
485         sockd->private_data = private_data;
486
487         ret = logging_init(sockd, logging, debug_level, daemon_name);
488         if (ret != 0) {
489                 fprintf(stderr,
490                         "Failed to initialize logging, logging=%s, debug=%s\n",
491                         logging, debug_level);
492                 return ret;
493         }
494
495         *out = sockd;
496         return 0;
497 }
498
499 int sock_daemon_add_unix(struct sock_daemon_context *sockd,
500                          const char *sockpath,
501                          struct sock_socket_funcs *funcs,
502                          void *private_data)
503 {
504         struct sock_socket *sock;
505         int ret;
506
507         ret = sock_socket_init(sockd, sockpath, funcs, private_data, &sock);
508         if (ret != 0) {
509                 return ret;
510         }
511
512
513         DLIST_ADD(sockd->socket_list, sock);
514         return 0;
515 }
516
517 /*
518  * Run socket daemon
519  */
520
521 struct sock_daemon_run_state {
522         struct tevent_context *ev;
523         struct sock_daemon_context *sockd;
524         pid_t pid_watch;
525
526         int fd;
527         int exit_code;
528 };
529
530 static void sock_daemon_run_started(struct tevent_req *subreq);
531 static void sock_daemon_run_startup_done(struct tevent_req *subreq);
532 static void sock_daemon_run_signal_handler(struct tevent_context *ev,
533                                            struct tevent_signal *se,
534                                            int signum, int count, void *siginfo,
535                                            void *private_data);
536 static void sock_daemon_run_reconfigure(struct tevent_req *req);
537 static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq);
538 static void sock_daemon_run_shutdown(struct tevent_req *req);
539 static void sock_daemon_run_shutdown_done(struct tevent_req *subreq);
540 static void sock_daemon_run_exit(struct tevent_req *req);
541 static bool sock_daemon_run_socket_listen(struct tevent_req *req);
542 static void sock_daemon_run_socket_fail(struct tevent_req *subreq);
543 static void sock_daemon_run_watch_pid(struct tevent_req *subreq);
544 static void sock_daemon_run_wait(struct tevent_req *req);
545 static void sock_daemon_run_wait_done(struct tevent_req *subreq);
546
547 struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
548                                         struct tevent_context *ev,
549                                         struct sock_daemon_context *sockd,
550                                         const char *pidfile,
551                                         bool do_fork, bool create_session,
552                                         pid_t pid_watch)
553 {
554         struct tevent_req *req, *subreq;
555         struct sock_daemon_run_state *state;
556         struct tevent_signal *se;
557
558         req = tevent_req_create(mem_ctx, &state,
559                                 struct sock_daemon_run_state);
560         if (req == NULL) {
561                 return NULL;
562         }
563
564         become_daemon(do_fork, !create_session, false);
565
566         if (pidfile != NULL) {
567                 int ret = pidfile_context_create(sockd, pidfile,
568                                                  &sockd->pid_ctx);
569                 if (ret != 0) {
570                         tevent_req_error(req, EEXIST);
571                         return tevent_req_post(req, ev);
572                 }
573         }
574
575         state->ev = ev;
576         state->sockd = sockd;
577         state->pid_watch = pid_watch;
578         state->fd  = -1;
579
580         subreq = tevent_wakeup_send(state, ev,
581                                     tevent_timeval_current_ofs(0, 0));
582         if (tevent_req_nomem(subreq, req)) {
583                 return tevent_req_post(req, ev);
584         }
585         tevent_req_set_callback(subreq, sock_daemon_run_started, req);
586
587         se = tevent_add_signal(ev, state, SIGHUP, 0,
588                                sock_daemon_run_signal_handler, req);
589         if (tevent_req_nomem(se, req)) {
590                 return tevent_req_post(req, ev);
591         }
592
593         se = tevent_add_signal(ev, state, SIGUSR1, 0,
594                                sock_daemon_run_signal_handler, req);
595         if (tevent_req_nomem(se, req)) {
596                 return tevent_req_post(req, ev);
597         }
598
599         se = tevent_add_signal(ev, state, SIGINT, 0,
600                                sock_daemon_run_signal_handler, req);
601         if (tevent_req_nomem(se, req)) {
602                 return tevent_req_post(req, ev);
603         }
604
605         se = tevent_add_signal(ev, state, SIGTERM, 0,
606                                sock_daemon_run_signal_handler, req);
607         if (tevent_req_nomem(se, req)) {
608                 return tevent_req_post(req, ev);
609         }
610
611         if (pid_watch > 1) {
612                 subreq = tevent_wakeup_send(state, ev,
613                                             tevent_timeval_current_ofs(1,0));
614                 if (tevent_req_nomem(subreq, req)) {
615                         return tevent_req_post(req, ev);
616                 }
617                 tevent_req_set_callback(subreq, sock_daemon_run_watch_pid,
618                                         req);
619         }
620
621         return req;
622 }
623
624 static void sock_daemon_run_started(struct tevent_req *subreq)
625 {
626         struct tevent_req *req = tevent_req_callback_data(
627                 subreq, struct tevent_req);
628         struct sock_daemon_run_state *state = tevent_req_data(
629                 req, struct sock_daemon_run_state);
630         struct sock_daemon_context *sockd = state->sockd;
631         bool status;
632
633         status = tevent_wakeup_recv(subreq);
634         TALLOC_FREE(subreq);
635         if (! status) {
636                 tevent_req_error(req, EIO);
637                 return;
638         }
639
640         D_NOTICE("daemon started, pid=%u\n", getpid());
641
642         if (sockd->funcs != NULL && sockd->funcs->startup_send != NULL &&
643             sockd->funcs->startup_recv != NULL) {
644                 subreq = sockd->funcs->startup_send(state, state->ev,
645                                                     sockd->private_data);
646                 if (tevent_req_nomem(subreq, req)) {
647                         return;
648                 }
649                 tevent_req_set_callback(subreq, sock_daemon_run_startup_done,
650                                         req);
651                 return;
652         }
653
654         if (sockd->funcs != NULL && sockd->funcs->startup != NULL) {
655                 int ret;
656
657                 ret = sockd->funcs->startup(sockd->private_data);
658                 if (ret != 0) {
659                         D_ERR("startup failed, ret=%d\n", ret);
660                         tevent_req_error(req, EIO);
661                         return;
662                 }
663
664                 D_NOTICE("startup completed successfully\n");
665         }
666
667         status = sock_daemon_run_socket_listen(req);
668         if (! status) {
669                 return;
670         }
671         sock_daemon_run_wait(req);
672 }
673
674 static void sock_daemon_run_startup_done(struct tevent_req *subreq)
675 {
676         struct tevent_req *req = tevent_req_callback_data(
677                 subreq, struct tevent_req);
678         struct sock_daemon_run_state *state = tevent_req_data(
679                 req, struct sock_daemon_run_state);
680         struct sock_daemon_context *sockd = state->sockd;
681         int ret;
682         bool status;
683
684         status = sockd->funcs->startup_recv(subreq, &ret);
685         TALLOC_FREE(subreq);
686         if (! status) {
687                 D_ERR("startup failed, ret=%d\n", ret);
688                 tevent_req_error(req, EIO);
689                 return;
690         }
691
692         D_NOTICE("startup completed succesfully\n");
693
694         status = sock_daemon_run_socket_listen(req);
695         if (! status) {
696                 return;
697         }
698         sock_daemon_run_wait(req);
699 }
700
701 static void sock_daemon_run_signal_handler(struct tevent_context *ev,
702                                            struct tevent_signal *se,
703                                            int signum, int count, void *siginfo,
704                                            void *private_data)
705 {
706         struct tevent_req *req = talloc_get_type_abort(
707                 private_data, struct tevent_req);
708         struct sock_daemon_run_state *state = tevent_req_data(
709                 req, struct sock_daemon_run_state);
710
711         D_NOTICE("Received signal %d\n", signum);
712
713         if (signum == SIGHUP || signum == SIGUSR1) {
714                 sock_daemon_run_reconfigure(req);
715                 return;
716         }
717
718         if (signum == SIGINT || signum == SIGTERM) {
719                 state->exit_code = EINTR;
720                 sock_daemon_run_shutdown(req);
721         }
722 }
723
724 static void sock_daemon_run_reconfigure(struct tevent_req *req)
725 {
726         struct tevent_req *subreq;
727         struct sock_daemon_run_state *state = tevent_req_data(
728                 req, struct sock_daemon_run_state);
729         struct sock_daemon_context *sockd = state->sockd;
730
731         if (sockd->funcs != NULL && sockd->funcs->reconfigure_send != NULL &&
732             sockd->funcs->reconfigure_recv != NULL) {
733                 subreq = sockd->funcs->reconfigure_send(state, state->ev,
734                                                         sockd->private_data);
735                 if (tevent_req_nomem(subreq, req)) {
736                         return;
737                 }
738                 tevent_req_set_callback(subreq,
739                                         sock_daemon_run_reconfigure_done, req);
740                 return;
741         }
742
743         if (sockd->funcs != NULL && sockd->funcs->reconfigure != NULL) {
744                 int ret;
745
746                 ret = sockd->funcs->reconfigure(sockd->private_data);
747                 if (ret != 0) {
748                         D_ERR("reconfigure failed, ret=%d\n", ret);
749                         return;
750                 }
751
752                 D_NOTICE("reconfigure completed successfully\n");
753         }
754 }
755
756 static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq)
757 {
758         struct tevent_req *req = tevent_req_callback_data(
759                 subreq, struct tevent_req);
760         struct sock_daemon_run_state *state = tevent_req_data(
761                 req, struct sock_daemon_run_state);
762         struct sock_daemon_context *sockd = state->sockd;
763         int ret;
764         bool status;
765
766         status = sockd->funcs->reconfigure_recv(subreq, &ret);
767         TALLOC_FREE(subreq);
768         if (! status) {
769                 D_ERR("reconfigure failed, ret=%d\n", ret);
770                 return;
771         }
772
773         D_NOTICE("reconfigure completed successfully\n");
774 }
775
776 static void sock_daemon_run_shutdown(struct tevent_req *req)
777 {
778         struct tevent_req *subreq;
779         struct sock_daemon_run_state *state = tevent_req_data(
780                 req, struct sock_daemon_run_state);
781         struct sock_daemon_context *sockd = state->sockd;
782         struct sock_socket *sock;
783
784         D_NOTICE("Shutting down\n");
785
786         while ((sock = sockd->socket_list) != NULL) {
787                 DLIST_REMOVE(sockd->socket_list, sock);
788                 TALLOC_FREE(sock);
789         }
790
791         if (sockd->funcs != NULL && sockd->funcs->shutdown_send != NULL &&
792             sockd->funcs->shutdown_recv != NULL) {
793                 subreq = sockd->funcs->shutdown_send(state, state->ev,
794                                                      sockd->private_data);
795                 if (subreq == NULL) {
796                         sock_daemon_run_exit(req);
797                         return;
798                 }
799                 tevent_req_set_callback(subreq, sock_daemon_run_shutdown_done,
800                                                 req);
801                 return;
802         }
803
804         if (sockd->funcs != NULL && sockd->funcs->shutdown != NULL) {
805                 sockd->funcs->shutdown(sockd->private_data);
806         }
807
808         sock_daemon_run_exit(req);
809 }
810
811 static void sock_daemon_run_shutdown_done(struct tevent_req *subreq)
812 {
813         struct tevent_req *req = tevent_req_callback_data(
814                 subreq, struct tevent_req);
815         struct sock_daemon_run_state *state = tevent_req_data(
816                 req, struct sock_daemon_run_state);
817         struct sock_daemon_context *sockd = state->sockd;
818
819         sockd->funcs->shutdown_recv(subreq);
820         TALLOC_FREE(subreq);
821
822         sock_daemon_run_exit(req);
823 }
824
825 static void sock_daemon_run_exit(struct tevent_req *req)
826 {
827         struct sock_daemon_run_state *state = tevent_req_data(
828                 req, struct sock_daemon_run_state);
829         struct sock_daemon_context *sockd = state->sockd;
830
831         TALLOC_FREE(sockd->pid_ctx);
832
833         if (state->exit_code == 0) {
834                 tevent_req_done(req);
835         } else {
836                 tevent_req_error(req, state->exit_code);
837         }
838 }
839
840 static bool sock_daemon_run_socket_listen(struct tevent_req *req)
841 {
842         struct tevent_req *subreq;
843         struct sock_daemon_run_state *state = tevent_req_data(
844                 req, struct sock_daemon_run_state);
845         struct sock_daemon_context *sockd = state->sockd;
846         struct sock_socket *sock;
847         bool remove_before_use = false;
848
849         if (sockd->pid_ctx != NULL) {
850                 remove_before_use = true;
851         }
852         for (sock = sockd->socket_list; sock != NULL; sock = sock->next) {
853                 subreq = sock_socket_start_send(state, state->ev, sock,
854                                                 remove_before_use);
855                 if (tevent_req_nomem(subreq, req)) {
856                         return false;
857                 }
858                 tevent_req_set_callback(subreq, sock_daemon_run_socket_fail,
859                                         req);
860         }
861
862         return true;
863 }
864
865 static void sock_daemon_run_socket_fail(struct tevent_req *subreq)
866 {
867         struct tevent_req *req = tevent_req_callback_data(
868                 subreq, struct tevent_req);
869         struct sock_daemon_run_state *state = tevent_req_data(
870                 req, struct sock_daemon_run_state);
871         const char *sockpath = NULL;
872         int ret = 0;
873         bool status;
874
875         status = sock_socket_start_recv(subreq, &ret, state, &sockpath);
876         TALLOC_FREE(subreq);
877         if (! status) {
878                 D_ERR("socket %s closed unexpectedly\n", sockpath);
879                 state->exit_code = ret;
880         } else {
881                 state->exit_code = 0;
882         }
883
884         sock_daemon_run_shutdown(req);
885 }
886
887 static void sock_daemon_run_watch_pid(struct tevent_req *subreq)
888 {
889         struct tevent_req *req = tevent_req_callback_data(
890                 subreq, struct tevent_req);
891         struct sock_daemon_run_state *state = tevent_req_data(
892                 req, struct sock_daemon_run_state);
893         int ret;
894         bool status;
895
896         status = tevent_wakeup_recv(subreq);
897         TALLOC_FREE(subreq);
898         if (! status) {
899                 tevent_req_error(req, EIO);
900                 return;
901         }
902
903         ret = kill(state->pid_watch, 0);
904         if (ret == -1) {
905                 if (errno == ESRCH) {
906                         D_ERR("PID %d gone away, exiting\n", state->pid_watch);
907                         state->exit_code = ESRCH;
908                         sock_daemon_run_shutdown(req);
909                         return;
910                 } else {
911                         D_ERR("Failed to check PID status %d, ret=%d\n",
912                               state->pid_watch, errno);
913                 }
914         }
915
916         subreq = tevent_wakeup_send(state, state->ev,
917                                     tevent_timeval_current_ofs(5,0));
918         if (tevent_req_nomem(subreq, req)) {
919                 return;
920         }
921         tevent_req_set_callback(subreq, sock_daemon_run_watch_pid, req);
922 }
923
924 static void sock_daemon_run_wait(struct tevent_req *req)
925 {
926         struct tevent_req *subreq;
927         struct sock_daemon_run_state *state = tevent_req_data(
928                 req, struct sock_daemon_run_state);
929         struct sock_daemon_context *sockd = state->sockd;
930
931         if (sockd->funcs != NULL && sockd->funcs->wait_send != NULL &&
932             sockd->funcs->wait_recv != NULL) {
933                 subreq = sockd->funcs->wait_send(state, state->ev,
934                                                  sockd->private_data);
935                 if (tevent_req_nomem(subreq, req)) {
936                         return;
937                 }
938                 tevent_req_set_callback(subreq, sock_daemon_run_wait_done,
939                                         req);
940         }
941 }
942
943 static void sock_daemon_run_wait_done(struct tevent_req *subreq)
944 {
945         struct tevent_req *req = tevent_req_callback_data(
946                 subreq, struct tevent_req);
947         struct sock_daemon_run_state *state = tevent_req_data(
948                 req, struct sock_daemon_run_state);
949         struct sock_daemon_context *sockd = state->sockd;
950         int ret = 0;
951         bool status;
952
953         status = sockd->funcs->wait_recv(subreq, &ret);
954         TALLOC_FREE(subreq);
955         if (! status) {
956                 state->exit_code = ret;
957         } else {
958                 state->exit_code = 0;
959         }
960
961         sock_daemon_run_shutdown(req);
962 }
963
964 bool sock_daemon_run_recv(struct tevent_req *req, int *perr)
965 {
966         int ret;
967
968         if (tevent_req_is_unix_error(req, &ret)) {
969                 if (perr != NULL) {
970                         *perr = ret;
971                 }
972                 return false;
973         }
974
975         return true;
976 }
977
978 int sock_daemon_run(struct tevent_context *ev,
979                     struct sock_daemon_context *sockd,
980                     const char *pidfile,
981                     bool do_fork, bool create_session,
982                     pid_t pid_watch)
983 {
984         struct tevent_req *req;
985         int ret;
986         bool status;
987
988         req = sock_daemon_run_send(ev, ev, sockd,
989                                    pidfile, do_fork, create_session, pid_watch);
990         if (req == NULL) {
991                 return ENOMEM;
992         }
993
994         tevent_req_poll(req, ev);
995
996         status = sock_daemon_run_recv(req, &ret);
997         TALLOC_FREE(req);
998         if (! status) {
999                 return ret;
1000         }
1001
1002         return 0;
1003 }