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
);
245 static int sock_socket_destructor(struct sock_socket
*sock
);
247 static int sock_socket_init(TALLOC_CTX
*mem_ctx
, const char *sockpath
,
248 struct sock_socket_funcs
*funcs
,
250 bool remove_before_use
,
251 struct sock_socket
**result
)
253 struct sock_socket
*sock
;
258 if (funcs
->read_send
== NULL
|| funcs
->read_recv
== NULL
) {
262 sock
= talloc_zero(mem_ctx
, struct sock_socket
);
267 sock
->sockpath
= sockpath
;
269 sock
->private_data
= private_data
;
271 sock
->fd
= socket_setup(sockpath
, remove_before_use
);
272 if (sock
->fd
== -1) {
277 talloc_set_destructor(sock
, sock_socket_destructor
);
283 static int sock_socket_destructor(struct sock_socket
*sock
)
285 if (sock
->fd
!= -1) {
290 unlink(sock
->sockpath
);
295 struct sock_socket_start_state
{
296 struct tevent_context
*ev
;
297 struct sock_socket
*sock
;
299 struct sock_client
*client_list
;
302 static int sock_socket_start_state_destructor(
303 struct sock_socket_start_state
*state
);
304 static void sock_socket_start_new_client(struct tevent_req
*subreq
);
305 static int sock_socket_start_client_destructor(struct sock_client
*client
);
307 static struct tevent_req
*sock_socket_start_send(TALLOC_CTX
*mem_ctx
,
308 struct tevent_context
*ev
,
309 struct sock_socket
*sock
)
311 struct tevent_req
*req
, *subreq
;
312 struct sock_socket_start_state
*state
;
314 req
= tevent_req_create(mem_ctx
, &state
,
315 struct sock_socket_start_state
);
323 talloc_set_destructor(state
, sock_socket_start_state_destructor
);
325 subreq
= accept_send(state
, ev
, sock
->fd
);
326 if (tevent_req_nomem(subreq
, req
)) {
327 return tevent_req_post(req
, ev
);
329 tevent_req_set_callback(subreq
, sock_socket_start_new_client
, req
);
334 static int sock_socket_start_state_destructor(
335 struct sock_socket_start_state
*state
)
337 struct sock_client
*client
;
339 while ((client
= state
->client_list
) != NULL
) {
346 static void sock_socket_start_new_client(struct tevent_req
*subreq
)
348 struct tevent_req
*req
= tevent_req_callback_data(
349 subreq
, struct tevent_req
);
350 struct sock_socket_start_state
*state
= tevent_req_data(
351 req
, struct sock_socket_start_state
);
352 struct sock_client
*client
;
355 client_fd
= accept_recv(subreq
, NULL
, NULL
, &ret
);
357 if (client_fd
== -1) {
358 D_ERR("failed to accept new connection\n");
361 subreq
= accept_send(state
, state
->ev
, state
->sock
->fd
);
362 if (tevent_req_nomem(subreq
, req
)) {
365 tevent_req_set_callback(subreq
, sock_socket_start_new_client
, req
);
367 if (client_fd
== -1) {
371 client
= talloc_zero(state
, struct sock_client
);
372 if (tevent_req_nomem(client
, req
)) {
379 ret
= sock_client_context_init(client
, state
->ev
, state
->sock
,
380 client_fd
, client
, &client
->client_ctx
);
386 talloc_set_destructor(client
, sock_socket_start_client_destructor
);
387 DLIST_ADD(state
->client_list
, client
);
390 static int sock_socket_start_client_destructor(struct sock_client
*client
)
392 struct sock_socket_start_state
*state
= tevent_req_data(
393 client
->req
, struct sock_socket_start_state
);
395 DLIST_REMOVE(state
->client_list
, client
);
396 TALLOC_FREE(client
->client_ctx
);
401 static bool sock_socket_start_recv(struct tevent_req
*req
, int *perr
)
403 struct sock_socket_start_state
*state
= tevent_req_data(
404 req
, struct sock_socket_start_state
);
407 state
->sock
->req
= NULL
;
409 if (tevent_req_is_unix_error(req
, &ret
)) {
420 * Send message to a client
423 struct tevent_req
*sock_socket_write_send(TALLOC_CTX
*mem_ctx
,
424 struct tevent_context
*ev
,
425 struct sock_client_context
*client_ctx
,
426 uint8_t *buf
, size_t buflen
)
428 struct tevent_req
*req
;
430 req
= comm_write_send(mem_ctx
, ev
, client_ctx
->comm
, buf
, buflen
);
435 bool sock_socket_write_recv(struct tevent_req
*req
, int *perr
)
440 status
= comm_write_recv(req
, &ret
);
454 int sock_daemon_setup(TALLOC_CTX
*mem_ctx
, const char *daemon_name
,
455 const char *logging
, const char *debug_level
,
456 struct sock_daemon_funcs
*funcs
,
458 struct sock_daemon_context
**out
)
460 struct sock_daemon_context
*sockd
;
463 sockd
= talloc_zero(mem_ctx
, struct sock_daemon_context
);
468 sockd
->funcs
= funcs
;
469 sockd
->private_data
= private_data
;
471 ret
= logging_init(sockd
, logging
, debug_level
, daemon_name
);
474 "Failed to initialize logging, logging=%s, debug=%s\n",
475 logging
, debug_level
);
483 int sock_daemon_add_unix(struct sock_daemon_context
*sockd
,
484 const char *sockpath
,
485 struct sock_socket_funcs
*funcs
,
488 struct sock_socket
*sock
;
490 bool remove_before_use
= false;
492 remove_before_use
= (sockd
->pid_ctx
!= NULL
) ? true : false;
494 ret
= sock_socket_init(sockd
, sockpath
, funcs
, private_data
,
495 remove_before_use
, &sock
);
500 D_NOTICE("listening on %s\n", sockpath
);
502 DLIST_ADD(sockd
->socket_list
, sock
);
510 struct sock_daemon_run_state
{
511 struct tevent_context
*ev
;
512 struct sock_daemon_context
*sockd
;
518 static void sock_daemon_run_started(struct tevent_req
*subreq
);
519 static void sock_daemon_run_signal_handler(struct tevent_context
*ev
,
520 struct tevent_signal
*se
,
521 int signum
, int count
, void *siginfo
,
523 static void sock_daemon_run_reconfigure(struct tevent_req
*req
);
524 static void sock_daemon_run_shutdown(struct tevent_req
*req
);
525 static void sock_daemon_run_socket_fail(struct tevent_req
*subreq
);
526 static void sock_daemon_run_watch_pid(struct tevent_req
*subreq
);
527 static void sock_daemon_run_wait_done(struct tevent_req
*subreq
);
529 struct tevent_req
*sock_daemon_run_send(TALLOC_CTX
*mem_ctx
,
530 struct tevent_context
*ev
,
531 struct sock_daemon_context
*sockd
,
533 bool do_fork
, bool create_session
,
536 struct tevent_req
*req
, *subreq
;
537 struct sock_daemon_run_state
*state
;
538 struct tevent_signal
*se
;
539 struct sock_socket
*sock
;
541 req
= tevent_req_create(mem_ctx
, &state
,
542 struct sock_daemon_run_state
);
547 become_daemon(do_fork
, !create_session
, false);
549 if (pidfile
!= NULL
) {
550 int ret
= pidfile_context_create(sockd
, pidfile
,
553 tevent_req_error(req
, EEXIST
);
554 return tevent_req_post(req
, ev
);
559 state
->sockd
= sockd
;
560 state
->pid_watch
= pid_watch
;
563 subreq
= tevent_wakeup_send(state
, ev
,
564 tevent_timeval_current_ofs(0, 0));
565 if (tevent_req_nomem(subreq
, req
)) {
566 return tevent_req_post(req
, ev
);
568 tevent_req_set_callback(subreq
, sock_daemon_run_started
, req
);
570 se
= tevent_add_signal(ev
, state
, SIGHUP
, 0,
571 sock_daemon_run_signal_handler
, req
);
572 if (tevent_req_nomem(se
, req
)) {
573 return tevent_req_post(req
, ev
);
576 se
= tevent_add_signal(ev
, state
, SIGUSR1
, 0,
577 sock_daemon_run_signal_handler
, req
);
578 if (tevent_req_nomem(se
, req
)) {
579 return tevent_req_post(req
, ev
);
582 se
= tevent_add_signal(ev
, state
, SIGINT
, 0,
583 sock_daemon_run_signal_handler
, req
);
584 if (tevent_req_nomem(se
, req
)) {
585 return tevent_req_post(req
, ev
);
588 se
= tevent_add_signal(ev
, state
, SIGTERM
, 0,
589 sock_daemon_run_signal_handler
, req
);
590 if (tevent_req_nomem(se
, req
)) {
591 return tevent_req_post(req
, ev
);
594 for (sock
= sockd
->socket_list
; sock
!= NULL
; sock
= sock
->next
) {
595 subreq
= sock_socket_start_send(state
, ev
, sock
);
596 if (tevent_req_nomem(subreq
, req
)) {
597 return tevent_req_post(req
, ev
);
599 tevent_req_set_callback(subreq
, sock_daemon_run_socket_fail
,
606 subreq
= tevent_wakeup_send(state
, ev
,
607 tevent_timeval_current_ofs(1,0));
608 if (tevent_req_nomem(subreq
, req
)) {
609 return tevent_req_post(req
, ev
);
611 tevent_req_set_callback(subreq
, sock_daemon_run_watch_pid
,
615 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->wait_send
!= NULL
&&
616 sockd
->funcs
->wait_recv
!= NULL
) {
617 subreq
= sockd
->funcs
->wait_send(state
, ev
,
618 sockd
->private_data
);
619 if (tevent_req_nomem(subreq
, req
)) {
620 return tevent_req_post(req
, ev
);
622 tevent_req_set_callback(subreq
, sock_daemon_run_wait_done
,
629 static void sock_daemon_run_started(struct tevent_req
*subreq
)
631 struct tevent_req
*req
= tevent_req_callback_data(
632 subreq
, struct tevent_req
);
633 struct sock_daemon_run_state
*state
= tevent_req_data(
634 req
, struct sock_daemon_run_state
);
635 struct sock_daemon_context
*sockd
= state
->sockd
;
637 D_NOTICE("daemon started, pid=%u\n", getpid());
639 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->startup
!= NULL
) {
640 sockd
->funcs
->startup(sockd
->private_data
);
644 static void sock_daemon_run_signal_handler(struct tevent_context
*ev
,
645 struct tevent_signal
*se
,
646 int signum
, int count
, void *siginfo
,
649 struct tevent_req
*req
= talloc_get_type_abort(
650 private_data
, struct tevent_req
);
652 D_NOTICE("Received signal %d\n", signum
);
654 if (signum
== SIGHUP
|| signum
== SIGUSR1
) {
655 sock_daemon_run_reconfigure(req
);
659 if (signum
== SIGINT
|| signum
== SIGTERM
) {
660 sock_daemon_run_shutdown(req
);
661 tevent_req_error(req
, EINTR
);
665 static void sock_daemon_run_reconfigure(struct tevent_req
*req
)
667 struct sock_daemon_run_state
*state
= tevent_req_data(
668 req
, struct sock_daemon_run_state
);
669 struct sock_daemon_context
*sockd
= state
->sockd
;
671 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->reconfigure
!= NULL
) {
672 sockd
->funcs
->reconfigure(sockd
->private_data
);
676 static void sock_daemon_run_shutdown(struct tevent_req
*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 struct sock_socket
*sock
;
683 D_NOTICE("Shutting down\n");
685 while ((sock
= sockd
->socket_list
) != NULL
) {
686 DLIST_REMOVE(sockd
->socket_list
, sock
);
687 TALLOC_FREE(sock
->req
);
691 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->shutdown
!= NULL
) {
692 sockd
->funcs
->shutdown(sockd
->private_data
);
695 TALLOC_FREE(sockd
->pid_ctx
);
698 static void sock_daemon_run_socket_fail(struct tevent_req
*subreq
)
700 struct tevent_req
*req
= tevent_req_callback_data(
701 subreq
, struct tevent_req
);
705 status
= sock_socket_start_recv(subreq
, &ret
);
707 sock_daemon_run_shutdown(req
);
709 tevent_req_error(req
, ret
);
711 tevent_req_done(req
);
715 static void sock_daemon_run_watch_pid(struct tevent_req
*subreq
)
717 struct tevent_req
*req
= tevent_req_callback_data(
718 subreq
, struct tevent_req
);
719 struct sock_daemon_run_state
*state
= tevent_req_data(
720 req
, struct sock_daemon_run_state
);
724 status
= tevent_wakeup_recv(subreq
);
727 tevent_req_error(req
, EIO
);
731 ret
= kill(state
->pid_watch
, 0);
733 if (errno
== ESRCH
) {
734 D_ERR("PID %d gone away, exiting\n", state
->pid_watch
);
735 sock_daemon_run_shutdown(req
);
736 tevent_req_error(req
, ESRCH
);
739 D_ERR("Failed to check PID status %d, ret=%d\n",
740 state
->pid_watch
, errno
);
744 subreq
= tevent_wakeup_send(state
, state
->ev
,
745 tevent_timeval_current_ofs(5,0));
746 if (tevent_req_nomem(subreq
, req
)) {
749 tevent_req_set_callback(subreq
, sock_daemon_run_watch_pid
, req
);
752 static void sock_daemon_run_wait_done(struct tevent_req
*subreq
)
754 struct tevent_req
*req
= tevent_req_callback_data(
755 subreq
, struct tevent_req
);
756 struct sock_daemon_run_state
*state
= tevent_req_data(
757 req
, struct sock_daemon_run_state
);
758 struct sock_daemon_context
*sockd
= state
->sockd
;
762 status
= sockd
->funcs
->wait_recv(subreq
, &ret
);
764 sock_daemon_run_shutdown(req
);
766 tevent_req_error(req
, ret
);
768 tevent_req_done(req
);
772 bool sock_daemon_run_recv(struct tevent_req
*req
, int *perr
)
776 if (tevent_req_is_unix_error(req
, &ret
)) {
786 int sock_daemon_run(struct tevent_context
*ev
,
787 struct sock_daemon_context
*sockd
,
789 bool do_fork
, bool create_session
,
792 struct tevent_req
*req
;
796 req
= sock_daemon_run_send(ev
, ev
, sockd
,
797 pidfile
, do_fork
, create_session
, pid_watch
);
802 tevent_req_poll(req
, ev
);
804 status
= sock_daemon_run_recv(req
, &ret
);