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/>.
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.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"
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"
42 struct sock_socket *prev, *next;
45 struct sock_socket_funcs *funcs;
49 struct tevent_req *req;
53 struct sock_client *prev, *next;
55 struct tevent_req *req;
56 struct sock_client_context *client_ctx;
59 struct sock_client_context {
60 struct tevent_context *ev;
61 struct sock_socket *sock;
63 struct comm_context *comm;
65 struct sock_client *client;
68 struct sock_daemon_context {
69 struct sock_daemon_funcs *funcs;
72 struct pidfile_context *pid_ctx;
73 struct sock_socket *socket_list;
77 * Process a single client
80 static void sock_client_read_handler(uint8_t *buf, size_t buflen,
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);
87 static int sock_client_context_init(TALLOC_CTX *mem_ctx,
88 struct tevent_context *ev,
89 struct sock_socket *sock,
91 struct sock_client *client,
92 struct sock_client_context **result)
94 struct sock_client_context *client_ctx;
97 client_ctx = talloc_zero(mem_ctx, struct sock_client_context);
98 if (client_ctx == NULL) {
103 client_ctx->sock = sock;
104 client_ctx->fd = client_fd;
105 client_ctx->client = client;
107 ret = comm_setup(client_ctx, ev, client_fd,
108 sock_client_read_handler, client_ctx,
109 sock_client_dead_handler, client_ctx,
112 talloc_free(client_ctx);
116 if (sock->funcs->connect != NULL) {
119 status = sock->funcs->connect(client_ctx, sock->private_data);
121 talloc_free(client_ctx);
127 talloc_set_destructor(client_ctx, sock_client_context_destructor);
129 *result = client_ctx;
133 static void sock_client_read_handler(uint8_t *buf, size_t buflen,
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;
141 subreq = sock->funcs->read_send(client_ctx, client_ctx->ev,
142 client_ctx, buf, buflen,
144 if (subreq == NULL) {
145 talloc_free(client_ctx);
148 tevent_req_set_callback(subreq, sock_client_read_done, client_ctx);
151 static void sock_client_read_done(struct tevent_req *subreq)
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;
159 status = sock->funcs->read_recv(subreq, &ret);
161 D_ERR("client read failed with ret=%d\n", ret);
162 talloc_free(client_ctx);
166 static void sock_client_dead_handler(void *private_data)
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;
172 if (sock->funcs->disconnect != NULL) {
173 sock->funcs->disconnect(client_ctx, sock->private_data);
176 talloc_free(client_ctx);
179 static int sock_client_context_destructor(
180 struct sock_client_context *client_ctx)
182 TALLOC_FREE(client_ctx->client);
183 TALLOC_FREE(client_ctx->comm);
184 if (client_ctx->fd != -1) {
185 close(client_ctx->fd);
193 * Process a single listening socket
196 static int socket_setup(const char *sockpath, bool remove_before_use)
198 struct sockaddr_un addr;
202 memset(&addr, 0, sizeof(addr));
203 addr.sun_family = AF_UNIX;
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);
211 fd = socket(AF_UNIX, SOCK_STREAM, 0);
213 D_ERR("socket create failed - %s\n", sockpath);
217 ret = set_blocking(fd, false);
219 D_ERR("socket set nonblocking failed - %s\n", sockpath);
224 if (remove_before_use) {
228 ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
230 D_ERR("socket bind failed - %s\n", sockpath);
235 ret = listen(fd, 10);
237 D_ERR("socket listen failed - %s\n", sockpath);
242 D_NOTICE("listening on %s\n", sockpath);
247 static int sock_socket_destructor(struct sock_socket *sock);
249 static int sock_socket_init(TALLOC_CTX *mem_ctx, const char *sockpath,
250 struct sock_socket_funcs *funcs,
252 struct sock_socket **result)
254 struct sock_socket *sock;
259 if (funcs->read_send == NULL || funcs->read_recv == NULL) {
263 sock = talloc_zero(mem_ctx, struct sock_socket);
268 sock->sockpath = talloc_strdup(sock, sockpath);
269 if (sock->sockpath == NULL) {
274 sock->private_data = private_data;
277 talloc_set_destructor(sock, sock_socket_destructor);
283 static int sock_socket_destructor(struct sock_socket *sock)
285 TALLOC_FREE(sock->req);
287 if (sock->fd != -1) {
292 unlink(sock->sockpath);
297 struct sock_socket_start_state {
298 struct tevent_context *ev;
299 struct sock_socket *sock;
301 struct sock_client *client_list;
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);
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)
314 struct tevent_req *req, *subreq;
315 struct sock_socket_start_state *state;
317 req = tevent_req_create(mem_ctx, &state,
318 struct sock_socket_start_state);
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);
332 talloc_set_destructor(state, sock_socket_start_state_destructor);
334 subreq = accept_send(state, ev, sock->fd);
335 if (tevent_req_nomem(subreq, req)) {
336 return tevent_req_post(req, ev);
338 tevent_req_set_callback(subreq, sock_socket_start_new_client, req);
345 static int sock_socket_start_state_destructor(
346 struct sock_socket_start_state *state)
348 struct sock_client *client;
350 while ((client = state->client_list) != NULL) {
357 static void sock_socket_start_new_client(struct tevent_req *subreq)
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;
366 client_fd = accept_recv(subreq, NULL, NULL, &ret);
368 if (client_fd == -1) {
369 D_ERR("failed to accept new connection\n");
372 subreq = accept_send(state, state->ev, state->sock->fd);
373 if (tevent_req_nomem(subreq, req)) {
376 tevent_req_set_callback(subreq, sock_socket_start_new_client, req);
378 if (client_fd == -1) {
382 client = talloc_zero(state, struct sock_client);
383 if (tevent_req_nomem(client, req)) {
390 ret = sock_client_context_init(client, state->ev, state->sock,
391 client_fd, client, &client->client_ctx);
397 talloc_set_destructor(client, sock_socket_start_client_destructor);
398 DLIST_ADD(state->client_list, client);
401 static int sock_socket_start_client_destructor(struct sock_client *client)
403 struct sock_socket_start_state *state = tevent_req_data(
404 client->req, struct sock_socket_start_state);
406 DLIST_REMOVE(state->client_list, client);
407 TALLOC_FREE(client->client_ctx);
412 static bool sock_socket_start_recv(struct tevent_req *req, int *perr,
413 TALLOC_CTX *mem_ctx, const char **sockpath)
415 struct sock_socket_start_state *state = tevent_req_data(
416 req, struct sock_socket_start_state);
419 state->sock->req = NULL;
421 if (tevent_req_is_unix_error(req, &ret)) {
428 if (sockpath != NULL) {
429 *sockpath = talloc_steal(mem_ctx, state->sock->sockpath);
436 * Send message to a client
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)
444 struct tevent_req *req;
446 req = comm_write_send(mem_ctx, ev, client_ctx->comm, buf, buflen);
451 bool sock_socket_write_recv(struct tevent_req *req, int *perr)
456 status = comm_write_recv(req, &ret);
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,
474 struct sock_daemon_context **out)
476 struct sock_daemon_context *sockd;
479 sockd = talloc_zero(mem_ctx, struct sock_daemon_context);
484 sockd->funcs = funcs;
485 sockd->private_data = private_data;
487 ret = logging_init(sockd, logging, debug_level, daemon_name);
490 "Failed to initialize logging, logging=%s, debug=%s\n",
491 logging, debug_level);
499 int sock_daemon_add_unix(struct sock_daemon_context *sockd,
500 const char *sockpath,
501 struct sock_socket_funcs *funcs,
504 struct sock_socket *sock;
507 ret = sock_socket_init(sockd, sockpath, funcs, private_data, &sock);
513 DLIST_ADD(sockd->socket_list, sock);
521 struct sock_daemon_run_state {
522 struct tevent_context *ev;
523 struct sock_daemon_context *sockd;
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,
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);
547 struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
548 struct tevent_context *ev,
549 struct sock_daemon_context *sockd,
551 bool do_fork, bool create_session,
554 struct tevent_req *req, *subreq;
555 struct sock_daemon_run_state *state;
556 struct tevent_signal *se;
558 req = tevent_req_create(mem_ctx, &state,
559 struct sock_daemon_run_state);
564 become_daemon(do_fork, !create_session, false);
566 if (pidfile != NULL) {
567 int ret = pidfile_context_create(sockd, pidfile,
570 tevent_req_error(req, EEXIST);
571 return tevent_req_post(req, ev);
576 state->sockd = sockd;
577 state->pid_watch = pid_watch;
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);
585 tevent_req_set_callback(subreq, sock_daemon_run_started, req);
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);
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);
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);
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);
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);
617 tevent_req_set_callback(subreq, sock_daemon_run_watch_pid,
624 static void sock_daemon_run_started(struct tevent_req *subreq)
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;
633 status = tevent_wakeup_recv(subreq);
636 tevent_req_error(req, EIO);
640 D_NOTICE("daemon started, pid=%u\n", getpid());
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)) {
649 tevent_req_set_callback(subreq, sock_daemon_run_startup_done,
654 if (sockd->funcs != NULL && sockd->funcs->startup != NULL) {
657 ret = sockd->funcs->startup(sockd->private_data);
659 D_ERR("startup failed, ret=%d\n", ret);
660 tevent_req_error(req, EIO);
664 D_NOTICE("startup completed successfully\n");
667 status = sock_daemon_run_socket_listen(req);
671 sock_daemon_run_wait(req);
674 static void sock_daemon_run_startup_done(struct tevent_req *subreq)
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;
684 status = sockd->funcs->startup_recv(subreq, &ret);
687 D_ERR("startup failed, ret=%d\n", ret);
688 tevent_req_error(req, EIO);
692 D_NOTICE("startup completed succesfully\n");
694 status = sock_daemon_run_socket_listen(req);
698 sock_daemon_run_wait(req);
701 static void sock_daemon_run_signal_handler(struct tevent_context *ev,
702 struct tevent_signal *se,
703 int signum, int count, void *siginfo,
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);
711 D_NOTICE("Received signal %d\n", signum);
713 if (signum == SIGHUP || signum == SIGUSR1) {
714 sock_daemon_run_reconfigure(req);
718 if (signum == SIGINT || signum == SIGTERM) {
719 state->exit_code = EINTR;
720 sock_daemon_run_shutdown(req);
724 static void sock_daemon_run_reconfigure(struct tevent_req *req)
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;
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)) {
738 tevent_req_set_callback(subreq,
739 sock_daemon_run_reconfigure_done, req);
743 if (sockd->funcs != NULL && sockd->funcs->reconfigure != NULL) {
746 ret = sockd->funcs->reconfigure(sockd->private_data);
748 D_ERR("reconfigure failed, ret=%d\n", ret);
752 D_NOTICE("reconfigure completed successfully\n");
756 static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq)
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;
766 status = sockd->funcs->reconfigure_recv(subreq, &ret);
769 D_ERR("reconfigure failed, ret=%d\n", ret);
773 D_NOTICE("reconfigure completed successfully\n");
776 static void sock_daemon_run_shutdown(struct tevent_req *req)
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;
784 D_NOTICE("Shutting down\n");
786 while ((sock = sockd->socket_list) != NULL) {
787 DLIST_REMOVE(sockd->socket_list, sock);
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);
799 tevent_req_set_callback(subreq, sock_daemon_run_shutdown_done,
804 if (sockd->funcs != NULL && sockd->funcs->shutdown != NULL) {
805 sockd->funcs->shutdown(sockd->private_data);
808 sock_daemon_run_exit(req);
811 static void sock_daemon_run_shutdown_done(struct tevent_req *subreq)
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;
819 sockd->funcs->shutdown_recv(subreq);
822 sock_daemon_run_exit(req);
825 static void sock_daemon_run_exit(struct tevent_req *req)
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;
831 TALLOC_FREE(sockd->pid_ctx);
833 if (state->exit_code == 0) {
834 tevent_req_done(req);
836 tevent_req_error(req, state->exit_code);
840 static bool sock_daemon_run_socket_listen(struct tevent_req *req)
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;
849 if (sockd->pid_ctx != NULL) {
850 remove_before_use = true;
852 for (sock = sockd->socket_list; sock != NULL; sock = sock->next) {
853 subreq = sock_socket_start_send(state, state->ev, sock,
855 if (tevent_req_nomem(subreq, req)) {
858 tevent_req_set_callback(subreq, sock_daemon_run_socket_fail,
865 static void sock_daemon_run_socket_fail(struct tevent_req *subreq)
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;
875 status = sock_socket_start_recv(subreq, &ret, state, &sockpath);
878 D_ERR("socket %s closed unexpectedly\n", sockpath);
879 state->exit_code = ret;
881 state->exit_code = 0;
884 sock_daemon_run_shutdown(req);
887 static void sock_daemon_run_watch_pid(struct tevent_req *subreq)
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);
896 status = tevent_wakeup_recv(subreq);
899 tevent_req_error(req, EIO);
903 ret = kill(state->pid_watch, 0);
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);
911 D_ERR("Failed to check PID status %d, ret=%d\n",
912 state->pid_watch, errno);
916 subreq = tevent_wakeup_send(state, state->ev,
917 tevent_timeval_current_ofs(5,0));
918 if (tevent_req_nomem(subreq, req)) {
921 tevent_req_set_callback(subreq, sock_daemon_run_watch_pid, req);
924 static void sock_daemon_run_wait(struct tevent_req *req)
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;
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)) {
938 tevent_req_set_callback(subreq, sock_daemon_run_wait_done,
943 static void sock_daemon_run_wait_done(struct tevent_req *subreq)
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;
953 status = sockd->funcs->wait_recv(subreq, &ret);
956 state->exit_code = ret;
958 state->exit_code = 0;
961 sock_daemon_run_shutdown(req);
964 bool sock_daemon_run_recv(struct tevent_req *req, int *perr)
968 if (tevent_req_is_unix_error(req, &ret)) {
978 int sock_daemon_run(struct tevent_context *ev,
979 struct sock_daemon_context *sockd,
981 bool do_fork, bool create_session,
984 struct tevent_req *req;
988 req = sock_daemon_run_send(ev, ev, sockd,
989 pidfile, do_fork, create_session, pid_watch);
994 tevent_req_poll(req, ev);
996 status = sock_daemon_run_recv(req, &ret);