s4:torture: FinderInfo conversion test with AppleDouble without xattr data
[Samba.git] / ctdb / common / sock_daemon.c
blob90f6bce2fd394df8cf1486334a9e85789b045d28
1 /*
2 A server based on unix domain socket
4 Copyright (C) Amitay Isaacs 2016
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.
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.
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/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
25 #include <talloc.h>
26 #include <tevent.h>
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 #include "lib/util/sys_rw.h"
36 #include "common/logging.h"
37 #include "common/reqid.h"
38 #include "common/comm.h"
39 #include "common/pidfile.h"
40 #include "common/system.h"
41 #include "common/sock_daemon.h"
43 struct sock_socket {
44 struct sock_socket *prev, *next;
46 const char *sockpath;
47 struct sock_socket_funcs *funcs;
48 void *private_data;
50 int fd;
51 struct tevent_req *req;
54 struct sock_client {
55 struct sock_client *prev, *next;
57 struct tevent_req *req;
58 struct sock_client_context *client_ctx;
61 struct sock_client_context {
62 struct tevent_context *ev;
63 struct sock_socket *sock;
64 int fd;
65 struct comm_context *comm;
67 struct sock_client *client;
70 struct sock_daemon_context {
71 struct sock_daemon_funcs *funcs;
72 void *private_data;
74 struct pidfile_context *pid_ctx;
75 struct sock_socket *socket_list;
76 int startup_fd;
80 * Process a single client
83 static void sock_client_read_handler(uint8_t *buf, size_t buflen,
84 void *private_data);
85 static void sock_client_read_done(struct tevent_req *subreq);
86 static void sock_client_dead_handler(void *private_data);
87 static int sock_client_context_destructor(
88 struct sock_client_context *client_ctx);
90 static int sock_client_context_init(TALLOC_CTX *mem_ctx,
91 struct tevent_context *ev,
92 struct sock_socket *sock,
93 int client_fd,
94 struct sock_client *client,
95 struct sock_client_context **result)
97 struct sock_client_context *client_ctx;
98 int ret;
100 client_ctx = talloc_zero(mem_ctx, struct sock_client_context);
101 if (client_ctx == NULL) {
102 return ENOMEM;
105 client_ctx->ev = ev;
106 client_ctx->sock = sock;
107 client_ctx->fd = client_fd;
108 client_ctx->client = client;
110 ret = comm_setup(client_ctx, ev, client_fd,
111 sock_client_read_handler, client_ctx,
112 sock_client_dead_handler, client_ctx,
113 &client_ctx->comm);
114 if (ret != 0) {
115 talloc_free(client_ctx);
116 return ret;
119 if (sock->funcs->connect != NULL) {
120 pid_t pid;
121 bool status;
123 (void) ctdb_get_peer_pid(client_fd, &pid);
125 status = sock->funcs->connect(client_ctx,
126 pid,
127 sock->private_data);
128 if (! status) {
129 talloc_free(client_ctx);
130 close(client_fd);
131 return 0;
135 talloc_set_destructor(client_ctx, sock_client_context_destructor);
137 *result = client_ctx;
138 return 0;
141 static void sock_client_read_handler(uint8_t *buf, size_t buflen,
142 void *private_data)
144 struct sock_client_context *client_ctx = talloc_get_type_abort(
145 private_data, struct sock_client_context);
146 struct sock_socket *sock = client_ctx->sock;
147 struct tevent_req *subreq;
149 subreq = sock->funcs->read_send(client_ctx, client_ctx->ev,
150 client_ctx, buf, buflen,
151 sock->private_data);
152 if (subreq == NULL) {
153 talloc_free(client_ctx);
154 return;
156 tevent_req_set_callback(subreq, sock_client_read_done, client_ctx);
159 static void sock_client_read_done(struct tevent_req *subreq)
161 struct sock_client_context *client_ctx = tevent_req_callback_data(
162 subreq, struct sock_client_context);
163 struct sock_socket *sock = client_ctx->sock;
164 int ret;
165 bool status;
167 status = sock->funcs->read_recv(subreq, &ret);
168 if (! status) {
169 D_ERR("client read failed with ret=%d\n", ret);
170 talloc_free(client_ctx);
174 static void sock_client_dead_handler(void *private_data)
176 struct sock_client_context *client_ctx = talloc_get_type_abort(
177 private_data, struct sock_client_context);
178 struct sock_socket *sock = client_ctx->sock;
180 if (sock->funcs->disconnect != NULL) {
181 sock->funcs->disconnect(client_ctx, sock->private_data);
184 talloc_free(client_ctx);
187 static int sock_client_context_destructor(
188 struct sock_client_context *client_ctx)
190 TALLOC_FREE(client_ctx->client);
191 TALLOC_FREE(client_ctx->comm);
192 if (client_ctx->fd != -1) {
193 close(client_ctx->fd);
194 client_ctx->fd = -1;
197 return 0;
201 * Process a single listening socket
204 static int socket_setup(const char *sockpath, bool remove_before_use)
206 struct sockaddr_un addr;
207 size_t len;
208 int ret, fd;
210 memset(&addr, 0, sizeof(addr));
211 addr.sun_family = AF_UNIX;
213 len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
214 if (len >= sizeof(addr.sun_path)) {
215 D_ERR("socket path too long: %s\n", sockpath);
216 return -1;
219 fd = socket(AF_UNIX, SOCK_STREAM, 0);
220 if (fd == -1) {
221 D_ERR("socket create failed - %s\n", sockpath);
222 return -1;
225 ret = set_blocking(fd, false);
226 if (ret != 0) {
227 D_ERR("socket set nonblocking failed - %s\n", sockpath);
228 close(fd);
229 return -1;
232 if (remove_before_use) {
233 unlink(sockpath);
236 ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
237 if (ret != 0) {
238 D_ERR("socket bind failed - %s\n", sockpath);
239 close(fd);
240 return -1;
243 ret = listen(fd, 10);
244 if (ret != 0) {
245 D_ERR("socket listen failed - %s\n", sockpath);
246 close(fd);
247 return -1;
250 D_NOTICE("listening on %s\n", sockpath);
252 return fd;
255 static int sock_socket_destructor(struct sock_socket *sock);
257 static int sock_socket_init(TALLOC_CTX *mem_ctx, const char *sockpath,
258 struct sock_socket_funcs *funcs,
259 void *private_data,
260 struct sock_socket **result)
262 struct sock_socket *sock;
264 if (funcs == NULL) {
265 return EINVAL;
267 if (funcs->read_send == NULL || funcs->read_recv == NULL) {
268 return EINVAL;
271 sock = talloc_zero(mem_ctx, struct sock_socket);
272 if (sock == NULL) {
273 return ENOMEM;
276 sock->sockpath = talloc_strdup(sock, sockpath);
277 if (sock->sockpath == NULL) {
278 talloc_free(sock);
279 return ENOMEM;
281 sock->funcs = funcs;
282 sock->private_data = private_data;
283 sock->fd = -1;
285 talloc_set_destructor(sock, sock_socket_destructor);
287 *result = sock;
288 return 0;
291 static int sock_socket_destructor(struct sock_socket *sock)
293 TALLOC_FREE(sock->req);
295 if (sock->fd != -1) {
296 close(sock->fd);
297 sock->fd = -1;
300 unlink(sock->sockpath);
301 return 0;
305 struct sock_socket_start_state {
306 struct tevent_context *ev;
307 struct sock_socket *sock;
309 struct sock_client *client_list;
312 static int sock_socket_start_state_destructor(
313 struct sock_socket_start_state *state);
314 static void sock_socket_start_new_client(struct tevent_req *subreq);
315 static int sock_socket_start_client_destructor(struct sock_client *client);
317 static struct tevent_req *sock_socket_start_send(TALLOC_CTX *mem_ctx,
318 struct tevent_context *ev,
319 struct sock_socket *sock,
320 bool remove_before_use)
322 struct tevent_req *req, *subreq;
323 struct sock_socket_start_state *state;
325 req = tevent_req_create(mem_ctx, &state,
326 struct sock_socket_start_state);
327 if (req == NULL) {
328 return NULL;
331 state->ev = ev;
332 state->sock = sock;
334 sock->fd = socket_setup(sock->sockpath, remove_before_use);
335 if (sock->fd == -1) {
336 tevent_req_error(req, EIO);
337 return tevent_req_post(req, ev);
340 talloc_set_destructor(state, sock_socket_start_state_destructor);
342 subreq = accept_send(state, ev, sock->fd);
343 if (tevent_req_nomem(subreq, req)) {
344 return tevent_req_post(req, ev);
346 tevent_req_set_callback(subreq, sock_socket_start_new_client, req);
348 sock->req = req;
350 return req;
353 static int sock_socket_start_state_destructor(
354 struct sock_socket_start_state *state)
356 struct sock_client *client;
358 while ((client = state->client_list) != NULL) {
359 talloc_free(client);
362 return 0;
365 static void sock_socket_start_new_client(struct tevent_req *subreq)
367 struct tevent_req *req = tevent_req_callback_data(
368 subreq, struct tevent_req);
369 struct sock_socket_start_state *state = tevent_req_data(
370 req, struct sock_socket_start_state);
371 struct sock_client *client;
372 int client_fd, ret;
374 client_fd = accept_recv(subreq, NULL, NULL, &ret);
375 TALLOC_FREE(subreq);
376 if (client_fd == -1) {
377 D_ERR("failed to accept new connection\n");
380 subreq = accept_send(state, state->ev, state->sock->fd);
381 if (tevent_req_nomem(subreq, req)) {
382 return;
384 tevent_req_set_callback(subreq, sock_socket_start_new_client, req);
386 if (client_fd == -1) {
387 return;
390 client = talloc_zero(state, struct sock_client);
391 if (tevent_req_nomem(client, req)) {
392 close(client_fd);
393 return;
396 client->req = req;
398 ret = sock_client_context_init(client, state->ev, state->sock,
399 client_fd, client, &client->client_ctx);
400 if (ret != 0) {
401 talloc_free(client);
402 return;
405 talloc_set_destructor(client, sock_socket_start_client_destructor);
406 DLIST_ADD(state->client_list, client);
409 static int sock_socket_start_client_destructor(struct sock_client *client)
411 struct sock_socket_start_state *state = tevent_req_data(
412 client->req, struct sock_socket_start_state);
414 DLIST_REMOVE(state->client_list, client);
415 TALLOC_FREE(client->client_ctx);
417 return 0;
420 static bool sock_socket_start_recv(struct tevent_req *req, int *perr,
421 TALLOC_CTX *mem_ctx, const char **sockpath)
423 struct sock_socket_start_state *state = tevent_req_data(
424 req, struct sock_socket_start_state);
425 int ret;
427 state->sock->req = NULL;
429 if (tevent_req_is_unix_error(req, &ret)) {
430 if (perr != NULL) {
431 *perr = ret;
433 return false;
436 if (sockpath != NULL) {
437 *sockpath = talloc_steal(mem_ctx, state->sock->sockpath);
440 return true;
444 * Send message to a client
447 struct tevent_req *sock_socket_write_send(TALLOC_CTX *mem_ctx,
448 struct tevent_context *ev,
449 struct sock_client_context *client_ctx,
450 uint8_t *buf, size_t buflen)
452 struct tevent_req *req;
454 req = comm_write_send(mem_ctx, ev, client_ctx->comm, buf, buflen);
456 return req;
459 bool sock_socket_write_recv(struct tevent_req *req, int *perr)
461 int ret;
462 bool status;
464 status = comm_write_recv(req, &ret);
465 if (! status) {
466 if (perr != NULL) {
467 *perr = ret;
471 return status;
475 * Socket daemon
478 int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
479 const char *logging, const char *debug_level,
480 struct sock_daemon_funcs *funcs,
481 void *private_data,
482 struct sock_daemon_context **out)
484 struct sock_daemon_context *sockd;
485 int ret;
487 sockd = talloc_zero(mem_ctx, struct sock_daemon_context);
488 if (sockd == NULL) {
489 return ENOMEM;
492 sockd->funcs = funcs;
493 sockd->private_data = private_data;
494 sockd->startup_fd = -1;
496 ret = logging_init(sockd, logging, debug_level, daemon_name);
497 if (ret != 0) {
498 fprintf(stderr,
499 "Failed to initialize logging, logging=%s, debug=%s\n",
500 logging, debug_level);
501 return ret;
504 *out = sockd;
505 return 0;
508 int sock_daemon_add_unix(struct sock_daemon_context *sockd,
509 const char *sockpath,
510 struct sock_socket_funcs *funcs,
511 void *private_data)
513 struct sock_socket *sock;
514 int ret;
516 ret = sock_socket_init(sockd, sockpath, funcs, private_data, &sock);
517 if (ret != 0) {
518 return ret;
522 DLIST_ADD(sockd->socket_list, sock);
523 return 0;
526 void sock_daemon_set_startup_fd(struct sock_daemon_context *sockd, int fd)
528 sockd->startup_fd = fd;
532 * Run socket daemon
535 struct sock_daemon_run_state {
536 struct tevent_context *ev;
537 struct sock_daemon_context *sockd;
538 pid_t pid_watch;
540 int fd;
541 int exit_code;
544 static void sock_daemon_run_started(struct tevent_req *subreq);
545 static void sock_daemon_run_startup_done(struct tevent_req *subreq);
546 static void sock_daemon_run_signal_handler(struct tevent_context *ev,
547 struct tevent_signal *se,
548 int signum, int count, void *siginfo,
549 void *private_data);
550 static void sock_daemon_run_reconfigure(struct tevent_req *req);
551 static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq);
552 static void sock_daemon_run_shutdown(struct tevent_req *req);
553 static void sock_daemon_run_shutdown_done(struct tevent_req *subreq);
554 static void sock_daemon_run_exit(struct tevent_req *req);
555 static bool sock_daemon_run_socket_listen(struct tevent_req *req);
556 static void sock_daemon_run_socket_fail(struct tevent_req *subreq);
557 static void sock_daemon_run_watch_pid(struct tevent_req *subreq);
558 static void sock_daemon_run_wait(struct tevent_req *req);
559 static void sock_daemon_run_wait_done(struct tevent_req *subreq);
560 static void sock_daemon_startup_notify(struct sock_daemon_context *sockd);
562 struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
563 struct tevent_context *ev,
564 struct sock_daemon_context *sockd,
565 const char *pidfile,
566 bool do_fork, bool create_session,
567 pid_t pid_watch)
569 struct tevent_req *req, *subreq;
570 struct sock_daemon_run_state *state;
571 struct tevent_signal *se;
573 req = tevent_req_create(mem_ctx, &state,
574 struct sock_daemon_run_state);
575 if (req == NULL) {
576 return NULL;
579 become_daemon(do_fork, !create_session, false);
581 if (pidfile != NULL) {
582 int ret = pidfile_context_create(sockd, pidfile,
583 &sockd->pid_ctx);
584 if (ret != 0) {
585 tevent_req_error(req, EEXIST);
586 return tevent_req_post(req, ev);
590 state->ev = ev;
591 state->sockd = sockd;
592 state->pid_watch = pid_watch;
593 state->fd = -1;
595 subreq = tevent_wakeup_send(state, ev,
596 tevent_timeval_current_ofs(0, 0));
597 if (tevent_req_nomem(subreq, req)) {
598 return tevent_req_post(req, ev);
600 tevent_req_set_callback(subreq, sock_daemon_run_started, req);
602 se = tevent_add_signal(ev, state, SIGHUP, 0,
603 sock_daemon_run_signal_handler, req);
604 if (tevent_req_nomem(se, req)) {
605 return tevent_req_post(req, ev);
608 se = tevent_add_signal(ev, state, SIGUSR1, 0,
609 sock_daemon_run_signal_handler, req);
610 if (tevent_req_nomem(se, req)) {
611 return tevent_req_post(req, ev);
614 se = tevent_add_signal(ev, state, SIGINT, 0,
615 sock_daemon_run_signal_handler, req);
616 if (tevent_req_nomem(se, req)) {
617 return tevent_req_post(req, ev);
620 se = tevent_add_signal(ev, state, SIGTERM, 0,
621 sock_daemon_run_signal_handler, req);
622 if (tevent_req_nomem(se, req)) {
623 return tevent_req_post(req, ev);
626 if (pid_watch > 1) {
627 subreq = tevent_wakeup_send(state, ev,
628 tevent_timeval_current_ofs(1,0));
629 if (tevent_req_nomem(subreq, req)) {
630 return tevent_req_post(req, ev);
632 tevent_req_set_callback(subreq, sock_daemon_run_watch_pid,
633 req);
636 return req;
639 static void sock_daemon_run_started(struct tevent_req *subreq)
641 struct tevent_req *req = tevent_req_callback_data(
642 subreq, struct tevent_req);
643 struct sock_daemon_run_state *state = tevent_req_data(
644 req, struct sock_daemon_run_state);
645 struct sock_daemon_context *sockd = state->sockd;
646 bool status;
648 status = tevent_wakeup_recv(subreq);
649 TALLOC_FREE(subreq);
650 if (! status) {
651 tevent_req_error(req, EIO);
652 return;
655 D_NOTICE("daemon started, pid=%u\n", getpid());
657 if (sockd->funcs != NULL && sockd->funcs->startup_send != NULL &&
658 sockd->funcs->startup_recv != NULL) {
659 subreq = sockd->funcs->startup_send(state, state->ev,
660 sockd->private_data);
661 if (tevent_req_nomem(subreq, req)) {
662 return;
664 tevent_req_set_callback(subreq, sock_daemon_run_startup_done,
665 req);
666 return;
669 if (sockd->funcs != NULL && sockd->funcs->startup != NULL) {
670 int ret;
672 ret = sockd->funcs->startup(sockd->private_data);
673 if (ret != 0) {
674 D_ERR("startup failed, ret=%d\n", ret);
675 tevent_req_error(req, EIO);
676 return;
679 D_NOTICE("startup completed successfully\n");
682 status = sock_daemon_run_socket_listen(req);
683 if (! status) {
684 return;
686 sock_daemon_run_wait(req);
688 sock_daemon_startup_notify(sockd);
691 static void sock_daemon_run_startup_done(struct tevent_req *subreq)
693 struct tevent_req *req = tevent_req_callback_data(
694 subreq, struct tevent_req);
695 struct sock_daemon_run_state *state = tevent_req_data(
696 req, struct sock_daemon_run_state);
697 struct sock_daemon_context *sockd = state->sockd;
698 int ret;
699 bool status;
701 status = sockd->funcs->startup_recv(subreq, &ret);
702 TALLOC_FREE(subreq);
703 if (! status) {
704 D_ERR("startup failed, ret=%d\n", ret);
705 tevent_req_error(req, EIO);
706 return;
709 D_NOTICE("startup completed successfully\n");
711 status = sock_daemon_run_socket_listen(req);
712 if (! status) {
713 return;
715 sock_daemon_run_wait(req);
717 sock_daemon_startup_notify(sockd);
720 static void sock_daemon_run_signal_handler(struct tevent_context *ev,
721 struct tevent_signal *se,
722 int signum, int count, void *siginfo,
723 void *private_data)
725 struct tevent_req *req = talloc_get_type_abort(
726 private_data, struct tevent_req);
727 struct sock_daemon_run_state *state = tevent_req_data(
728 req, struct sock_daemon_run_state);
730 D_NOTICE("Received signal %d\n", signum);
732 if (signum == SIGHUP || signum == SIGUSR1) {
733 sock_daemon_run_reconfigure(req);
734 return;
737 if (signum == SIGINT || signum == SIGTERM) {
738 state->exit_code = EINTR;
739 sock_daemon_run_shutdown(req);
743 static void sock_daemon_run_reconfigure(struct tevent_req *req)
745 struct tevent_req *subreq;
746 struct sock_daemon_run_state *state = tevent_req_data(
747 req, struct sock_daemon_run_state);
748 struct sock_daemon_context *sockd = state->sockd;
750 if (sockd->funcs != NULL && sockd->funcs->reconfigure_send != NULL &&
751 sockd->funcs->reconfigure_recv != NULL) {
752 subreq = sockd->funcs->reconfigure_send(state, state->ev,
753 sockd->private_data);
754 if (tevent_req_nomem(subreq, req)) {
755 return;
757 tevent_req_set_callback(subreq,
758 sock_daemon_run_reconfigure_done, req);
759 return;
762 if (sockd->funcs != NULL && sockd->funcs->reconfigure != NULL) {
763 int ret;
765 ret = sockd->funcs->reconfigure(sockd->private_data);
766 if (ret != 0) {
767 D_ERR("reconfigure failed, ret=%d\n", ret);
768 return;
771 D_NOTICE("reconfigure completed successfully\n");
775 static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq)
777 struct tevent_req *req = tevent_req_callback_data(
778 subreq, struct tevent_req);
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 int ret;
783 bool status;
785 status = sockd->funcs->reconfigure_recv(subreq, &ret);
786 TALLOC_FREE(subreq);
787 if (! status) {
788 D_ERR("reconfigure failed, ret=%d\n", ret);
789 return;
792 D_NOTICE("reconfigure completed successfully\n");
795 static void sock_daemon_run_shutdown(struct tevent_req *req)
797 struct tevent_req *subreq;
798 struct sock_daemon_run_state *state = tevent_req_data(
799 req, struct sock_daemon_run_state);
800 struct sock_daemon_context *sockd = state->sockd;
801 struct sock_socket *sock;
803 D_NOTICE("Shutting down\n");
805 while ((sock = sockd->socket_list) != NULL) {
806 DLIST_REMOVE(sockd->socket_list, sock);
807 TALLOC_FREE(sock);
810 if (sockd->funcs != NULL && sockd->funcs->shutdown_send != NULL &&
811 sockd->funcs->shutdown_recv != NULL) {
812 subreq = sockd->funcs->shutdown_send(state, state->ev,
813 sockd->private_data);
814 if (subreq == NULL) {
815 sock_daemon_run_exit(req);
816 return;
818 tevent_req_set_callback(subreq, sock_daemon_run_shutdown_done,
819 req);
820 return;
823 if (sockd->funcs != NULL && sockd->funcs->shutdown != NULL) {
824 sockd->funcs->shutdown(sockd->private_data);
827 sock_daemon_run_exit(req);
830 static void sock_daemon_run_shutdown_done(struct tevent_req *subreq)
832 struct tevent_req *req = tevent_req_callback_data(
833 subreq, struct tevent_req);
834 struct sock_daemon_run_state *state = tevent_req_data(
835 req, struct sock_daemon_run_state);
836 struct sock_daemon_context *sockd = state->sockd;
838 sockd->funcs->shutdown_recv(subreq);
839 TALLOC_FREE(subreq);
841 sock_daemon_run_exit(req);
844 static void sock_daemon_run_exit(struct tevent_req *req)
846 struct sock_daemon_run_state *state = tevent_req_data(
847 req, struct sock_daemon_run_state);
848 struct sock_daemon_context *sockd = state->sockd;
850 TALLOC_FREE(sockd->pid_ctx);
852 if (state->exit_code == 0) {
853 tevent_req_done(req);
854 } else {
855 tevent_req_error(req, state->exit_code);
859 static bool sock_daemon_run_socket_listen(struct tevent_req *req)
861 struct tevent_req *subreq;
862 struct sock_daemon_run_state *state = tevent_req_data(
863 req, struct sock_daemon_run_state);
864 struct sock_daemon_context *sockd = state->sockd;
865 struct sock_socket *sock;
866 bool remove_before_use = false;
868 if (sockd->pid_ctx != NULL) {
869 remove_before_use = true;
871 for (sock = sockd->socket_list; sock != NULL; sock = sock->next) {
872 subreq = sock_socket_start_send(state, state->ev, sock,
873 remove_before_use);
874 if (tevent_req_nomem(subreq, req)) {
875 return false;
877 tevent_req_set_callback(subreq, sock_daemon_run_socket_fail,
878 req);
881 return true;
884 static void sock_daemon_run_socket_fail(struct tevent_req *subreq)
886 struct tevent_req *req = tevent_req_callback_data(
887 subreq, struct tevent_req);
888 struct sock_daemon_run_state *state = tevent_req_data(
889 req, struct sock_daemon_run_state);
890 const char *sockpath = NULL;
891 int ret = 0;
892 bool status;
894 status = sock_socket_start_recv(subreq, &ret, state, &sockpath);
895 TALLOC_FREE(subreq);
896 if (! status) {
897 D_ERR("socket %s closed unexpectedly\n", sockpath);
898 state->exit_code = ret;
899 } else {
900 state->exit_code = 0;
903 sock_daemon_run_shutdown(req);
906 static void sock_daemon_run_watch_pid(struct tevent_req *subreq)
908 struct tevent_req *req = tevent_req_callback_data(
909 subreq, struct tevent_req);
910 struct sock_daemon_run_state *state = tevent_req_data(
911 req, struct sock_daemon_run_state);
912 int ret;
913 bool status;
915 status = tevent_wakeup_recv(subreq);
916 TALLOC_FREE(subreq);
917 if (! status) {
918 tevent_req_error(req, EIO);
919 return;
922 ret = kill(state->pid_watch, 0);
923 if (ret == -1) {
924 if (errno == ESRCH) {
925 D_ERR("PID %d gone away, exiting\n", state->pid_watch);
926 state->exit_code = ESRCH;
927 sock_daemon_run_shutdown(req);
928 return;
929 } else {
930 D_ERR("Failed to check PID status %d, ret=%d\n",
931 state->pid_watch, errno);
935 subreq = tevent_wakeup_send(state, state->ev,
936 tevent_timeval_current_ofs(5,0));
937 if (tevent_req_nomem(subreq, req)) {
938 return;
940 tevent_req_set_callback(subreq, sock_daemon_run_watch_pid, req);
943 static void sock_daemon_run_wait(struct tevent_req *req)
945 struct tevent_req *subreq;
946 struct sock_daemon_run_state *state = tevent_req_data(
947 req, struct sock_daemon_run_state);
948 struct sock_daemon_context *sockd = state->sockd;
950 if (sockd->funcs != NULL && sockd->funcs->wait_send != NULL &&
951 sockd->funcs->wait_recv != NULL) {
952 subreq = sockd->funcs->wait_send(state, state->ev,
953 sockd->private_data);
954 if (tevent_req_nomem(subreq, req)) {
955 return;
957 tevent_req_set_callback(subreq, sock_daemon_run_wait_done,
958 req);
962 static void sock_daemon_run_wait_done(struct tevent_req *subreq)
964 struct tevent_req *req = tevent_req_callback_data(
965 subreq, struct tevent_req);
966 struct sock_daemon_run_state *state = tevent_req_data(
967 req, struct sock_daemon_run_state);
968 struct sock_daemon_context *sockd = state->sockd;
969 int ret = 0;
970 bool status;
972 status = sockd->funcs->wait_recv(subreq, &ret);
973 TALLOC_FREE(subreq);
974 if (! status) {
975 state->exit_code = ret;
976 } else {
977 state->exit_code = 0;
980 sock_daemon_run_shutdown(req);
983 static void sock_daemon_startup_notify(struct sock_daemon_context *sockd)
985 if (sockd->startup_fd != -1) {
986 unsigned int zero = 0;
987 ssize_t num;
989 num = sys_write(sockd->startup_fd, &zero, sizeof(zero));
990 if (num != sizeof(zero)) {
991 D_WARNING("Failed to write zero to pipe FD\n");
996 bool sock_daemon_run_recv(struct tevent_req *req, int *perr)
998 int ret;
1000 if (tevent_req_is_unix_error(req, &ret)) {
1001 if (perr != NULL) {
1002 *perr = ret;
1004 return false;
1007 return true;
1010 int sock_daemon_run(struct tevent_context *ev,
1011 struct sock_daemon_context *sockd,
1012 const char *pidfile,
1013 bool do_fork, bool create_session,
1014 pid_t pid_watch)
1016 struct tevent_req *req;
1017 int ret;
1018 bool status;
1020 req = sock_daemon_run_send(ev, ev, sockd,
1021 pidfile, do_fork, create_session, pid_watch);
1022 if (req == NULL) {
1023 return ENOMEM;
1026 tevent_req_poll(req, ev);
1028 status = sock_daemon_run_recv(req, &ret);
1029 TALLOC_FREE(req);
1030 if (! status) {
1031 return ret;
1034 return 0;