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"
34 #include "common/logging.h"
35 #include "common/reqid.h"
36 #include "common/comm.h"
37 #include "common/pidfile.h"
38 #include "common/sock_daemon.h"
41 struct sock_socket
*prev
, *next
;
44 struct sock_socket_funcs
*funcs
;
48 struct tevent_req
*req
;
52 struct sock_client
*prev
, *next
;
54 struct tevent_req
*req
;
55 struct sock_client_context
*client_ctx
;
58 struct sock_client_context
{
59 struct tevent_context
*ev
;
60 struct sock_socket
*sock
;
62 struct comm_context
*comm
;
64 struct sock_client
*client
;
67 struct sock_daemon_context
{
68 struct sock_daemon_funcs
*funcs
;
71 struct pidfile_context
*pid_ctx
;
72 struct sock_socket
*socket_list
;
76 * Process a single client
79 static void sock_client_read_handler(uint8_t *buf
, size_t buflen
,
81 static void sock_client_read_done(struct tevent_req
*subreq
);
82 static void sock_client_dead_handler(void *private_data
);
83 static int sock_client_context_destructor(
84 struct sock_client_context
*client_ctx
);
86 static int sock_client_context_init(TALLOC_CTX
*mem_ctx
,
87 struct tevent_context
*ev
,
88 struct sock_socket
*sock
,
90 struct sock_client
*client
,
91 struct sock_client_context
**result
)
93 struct sock_client_context
*client_ctx
;
96 client_ctx
= talloc_zero(mem_ctx
, struct sock_client_context
);
97 if (client_ctx
== NULL
) {
102 client_ctx
->sock
= sock
;
103 client_ctx
->fd
= client_fd
;
104 client_ctx
->client
= client
;
106 ret
= comm_setup(client_ctx
, ev
, client_fd
,
107 sock_client_read_handler
, client_ctx
,
108 sock_client_dead_handler
, client_ctx
,
111 talloc_free(client_ctx
);
115 if (sock
->funcs
->connect
!= NULL
) {
118 status
= sock
->funcs
->connect(client_ctx
, sock
->private_data
);
120 talloc_free(client_ctx
);
126 talloc_set_destructor(client_ctx
, sock_client_context_destructor
);
128 *result
= client_ctx
;
132 static void sock_client_read_handler(uint8_t *buf
, size_t buflen
,
135 struct sock_client_context
*client_ctx
= talloc_get_type_abort(
136 private_data
, struct sock_client_context
);
137 struct sock_socket
*sock
= client_ctx
->sock
;
138 struct tevent_req
*subreq
;
140 subreq
= sock
->funcs
->read_send(client_ctx
, client_ctx
->ev
,
141 client_ctx
, buf
, buflen
,
143 if (subreq
== NULL
) {
144 talloc_free(client_ctx
);
147 tevent_req_set_callback(subreq
, sock_client_read_done
, client_ctx
);
150 static void sock_client_read_done(struct tevent_req
*subreq
)
152 struct sock_client_context
*client_ctx
= tevent_req_callback_data(
153 subreq
, struct sock_client_context
);
154 struct sock_socket
*sock
= client_ctx
->sock
;
158 status
= sock
->funcs
->read_recv(subreq
, &ret
);
160 D_ERR("client read failed with ret=%d\n", ret
);
161 talloc_free(client_ctx
);
165 static void sock_client_dead_handler(void *private_data
)
167 struct sock_client_context
*client_ctx
= talloc_get_type_abort(
168 private_data
, struct sock_client_context
);
169 struct sock_socket
*sock
= client_ctx
->sock
;
171 if (sock
->funcs
->disconnect
!= NULL
) {
172 sock
->funcs
->disconnect(client_ctx
, sock
->private_data
);
175 talloc_free(client_ctx
);
178 static int sock_client_context_destructor(
179 struct sock_client_context
*client_ctx
)
181 TALLOC_FREE(client_ctx
->client
);
182 TALLOC_FREE(client_ctx
->comm
);
183 if (client_ctx
->fd
!= -1) {
184 close(client_ctx
->fd
);
192 * Process a single listening socket
195 static int socket_setup(const char *sockpath
, bool remove_before_use
)
197 struct sockaddr_un addr
;
201 memset(&addr
, 0, sizeof(addr
));
202 addr
.sun_family
= AF_UNIX
;
204 len
= strlcpy(addr
.sun_path
, sockpath
, sizeof(addr
.sun_path
));
205 if (len
>= sizeof(addr
.sun_path
)) {
206 D_ERR("socket path too long: %s\n", sockpath
);
210 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
212 D_ERR("socket create failed - %s\n", sockpath
);
216 ret
= set_blocking(fd
, false);
218 D_ERR("socket set nonblocking failed - %s\n", sockpath
);
223 if (remove_before_use
) {
227 ret
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
229 D_ERR("socket bind failed - %s\n", sockpath
);
234 ret
= listen(fd
, 10);
236 D_ERR("socket listen failed - %s\n", sockpath
);
244 static int sock_socket_destructor(struct sock_socket
*sock
);
246 static int sock_socket_init(TALLOC_CTX
*mem_ctx
, const char *sockpath
,
247 struct sock_socket_funcs
*funcs
,
249 bool remove_before_use
,
250 struct sock_socket
**result
)
252 struct sock_socket
*sock
;
257 if (funcs
->read_send
== NULL
|| funcs
->read_recv
== NULL
) {
261 sock
= talloc_zero(mem_ctx
, struct sock_socket
);
266 sock
->sockpath
= sockpath
;
268 sock
->private_data
= private_data
;
270 sock
->fd
= socket_setup(sockpath
, remove_before_use
);
271 if (sock
->fd
== -1) {
276 talloc_set_destructor(sock
, sock_socket_destructor
);
282 static int sock_socket_destructor(struct sock_socket
*sock
)
284 if (sock
->fd
!= -1) {
289 unlink(sock
->sockpath
);
294 struct sock_socket_start_state
{
295 struct tevent_context
*ev
;
296 struct sock_socket
*sock
;
298 struct sock_client
*client_list
;
301 static int sock_socket_start_state_destructor(
302 struct sock_socket_start_state
*state
);
303 static void sock_socket_start_new_client(struct tevent_req
*subreq
);
304 static int sock_socket_start_client_destructor(struct sock_client
*client
);
306 static struct tevent_req
*sock_socket_start_send(TALLOC_CTX
*mem_ctx
,
307 struct tevent_context
*ev
,
308 struct sock_socket
*sock
)
310 struct tevent_req
*req
, *subreq
;
311 struct sock_socket_start_state
*state
;
313 req
= tevent_req_create(mem_ctx
, &state
,
314 struct sock_socket_start_state
);
322 talloc_set_destructor(state
, sock_socket_start_state_destructor
);
324 subreq
= accept_send(state
, ev
, sock
->fd
);
325 if (tevent_req_nomem(subreq
, req
)) {
326 return tevent_req_post(req
, ev
);
328 tevent_req_set_callback(subreq
, sock_socket_start_new_client
, req
);
333 static int sock_socket_start_state_destructor(
334 struct sock_socket_start_state
*state
)
336 struct sock_client
*client
;
338 while ((client
= state
->client_list
) != NULL
) {
345 static void sock_socket_start_new_client(struct tevent_req
*subreq
)
347 struct tevent_req
*req
= tevent_req_callback_data(
348 subreq
, struct tevent_req
);
349 struct sock_socket_start_state
*state
= tevent_req_data(
350 req
, struct sock_socket_start_state
);
351 struct sock_client
*client
;
354 client_fd
= accept_recv(subreq
, NULL
, NULL
, &ret
);
356 if (client_fd
== -1) {
357 D_ERR("failed to accept new connection\n");
360 subreq
= accept_send(state
, state
->ev
, state
->sock
->fd
);
361 if (tevent_req_nomem(subreq
, req
)) {
364 tevent_req_set_callback(subreq
, sock_socket_start_new_client
, req
);
366 if (client_fd
== -1) {
370 client
= talloc_zero(state
, struct sock_client
);
371 if (tevent_req_nomem(client
, req
)) {
378 ret
= sock_client_context_init(client
, state
->ev
, state
->sock
,
379 client_fd
, client
, &client
->client_ctx
);
385 talloc_set_destructor(client
, sock_socket_start_client_destructor
);
386 DLIST_ADD(state
->client_list
, client
);
389 static int sock_socket_start_client_destructor(struct sock_client
*client
)
391 struct sock_socket_start_state
*state
= tevent_req_data(
392 client
->req
, struct sock_socket_start_state
);
394 DLIST_REMOVE(state
->client_list
, client
);
395 TALLOC_FREE(client
->client_ctx
);
400 static bool sock_socket_start_recv(struct tevent_req
*req
, int *perr
)
402 struct sock_socket_start_state
*state
= tevent_req_data(
403 req
, struct sock_socket_start_state
);
406 state
->sock
->req
= NULL
;
408 if (tevent_req_is_unix_error(req
, &ret
)) {
419 * Send message to a client
422 struct tevent_req
*sock_socket_write_send(TALLOC_CTX
*mem_ctx
,
423 struct tevent_context
*ev
,
424 struct sock_client_context
*client_ctx
,
425 uint8_t *buf
, size_t buflen
)
427 struct tevent_req
*req
;
429 req
= comm_write_send(mem_ctx
, ev
, client_ctx
->comm
, buf
, buflen
);
434 bool sock_socket_write_recv(struct tevent_req
*req
, int *perr
)
439 status
= comm_write_recv(req
, &ret
);
453 int sock_daemon_setup(TALLOC_CTX
*mem_ctx
, const char *daemon_name
,
454 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
);
479 if (pidfile
!= NULL
) {
480 ret
= pidfile_create(sockd
, pidfile
, &sockd
->pid_ctx
);
491 int sock_daemon_add_unix(struct sock_daemon_context
*sockd
,
492 const char *sockpath
,
493 struct sock_socket_funcs
*funcs
,
496 struct sock_socket
*sock
;
498 bool remove_before_use
= false;
500 remove_before_use
= (sockd
->pid_ctx
!= NULL
) ? true : false;
502 ret
= sock_socket_init(sockd
, sockpath
, funcs
, private_data
,
503 remove_before_use
, &sock
);
508 D_NOTICE("listening on %s\n", sockpath
);
510 DLIST_ADD(sockd
->socket_list
, sock
);
518 struct sock_daemon_run_state
{
519 struct tevent_context
*ev
;
520 struct sock_daemon_context
*sockd
;
526 static void sock_daemon_run_started(struct tevent_req
*subreq
);
527 static void sock_daemon_run_signal_handler(struct tevent_context
*ev
,
528 struct tevent_signal
*se
,
529 int signum
, int count
, void *siginfo
,
531 static void sock_daemon_run_reconfigure(struct tevent_req
*req
);
532 static void sock_daemon_run_shutdown(struct tevent_req
*req
);
533 static void sock_daemon_run_socket_fail(struct tevent_req
*subreq
);
534 static void sock_daemon_run_watch_pid(struct tevent_req
*subreq
);
535 static void sock_daemon_run_wait_done(struct tevent_req
*subreq
);
537 struct tevent_req
*sock_daemon_run_send(TALLOC_CTX
*mem_ctx
,
538 struct tevent_context
*ev
,
539 struct sock_daemon_context
*sockd
,
542 struct tevent_req
*req
, *subreq
;
543 struct sock_daemon_run_state
*state
;
544 struct tevent_signal
*se
;
545 struct sock_socket
*sock
;
547 req
= tevent_req_create(mem_ctx
, &state
,
548 struct sock_daemon_run_state
);
554 state
->sockd
= sockd
;
555 state
->pid_watch
= pid_watch
;
558 subreq
= tevent_wakeup_send(state
, ev
,
559 tevent_timeval_current_ofs(0, 0));
560 if (tevent_req_nomem(subreq
, req
)) {
561 return tevent_req_post(req
, ev
);
563 tevent_req_set_callback(subreq
, sock_daemon_run_started
, req
);
565 se
= tevent_add_signal(ev
, state
, SIGHUP
, 0,
566 sock_daemon_run_signal_handler
, req
);
567 if (tevent_req_nomem(se
, req
)) {
568 return tevent_req_post(req
, ev
);
571 se
= tevent_add_signal(ev
, state
, SIGUSR1
, 0,
572 sock_daemon_run_signal_handler
, req
);
573 if (tevent_req_nomem(se
, req
)) {
574 return tevent_req_post(req
, ev
);
577 se
= tevent_add_signal(ev
, state
, SIGINT
, 0,
578 sock_daemon_run_signal_handler
, req
);
579 if (tevent_req_nomem(se
, req
)) {
580 return tevent_req_post(req
, ev
);
583 se
= tevent_add_signal(ev
, state
, SIGTERM
, 0,
584 sock_daemon_run_signal_handler
, req
);
585 if (tevent_req_nomem(se
, req
)) {
586 return tevent_req_post(req
, ev
);
589 for (sock
= sockd
->socket_list
; sock
!= NULL
; sock
= sock
->next
) {
590 subreq
= sock_socket_start_send(state
, ev
, sock
);
591 if (tevent_req_nomem(subreq
, req
)) {
592 return tevent_req_post(req
, ev
);
594 tevent_req_set_callback(subreq
, sock_daemon_run_socket_fail
,
601 subreq
= tevent_wakeup_send(state
, ev
,
602 tevent_timeval_current_ofs(1,0));
603 if (tevent_req_nomem(subreq
, req
)) {
604 return tevent_req_post(req
, ev
);
606 tevent_req_set_callback(subreq
, sock_daemon_run_watch_pid
,
610 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->wait_send
!= NULL
&&
611 sockd
->funcs
->wait_recv
!= NULL
) {
612 subreq
= sockd
->funcs
->wait_send(state
, ev
,
613 sockd
->private_data
);
614 if (tevent_req_nomem(subreq
, req
)) {
615 return tevent_req_post(req
, ev
);
617 tevent_req_set_callback(subreq
, sock_daemon_run_wait_done
,
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
;
632 D_NOTICE("daemon started, pid=%u\n", getpid());
634 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->startup
!= NULL
) {
635 sockd
->funcs
->startup(sockd
->private_data
);
639 static void sock_daemon_run_signal_handler(struct tevent_context
*ev
,
640 struct tevent_signal
*se
,
641 int signum
, int count
, void *siginfo
,
644 struct tevent_req
*req
= talloc_get_type_abort(
645 private_data
, struct tevent_req
);
647 D_NOTICE("Received signal %d\n", signum
);
649 if (signum
== SIGHUP
|| signum
== SIGUSR1
) {
650 sock_daemon_run_reconfigure(req
);
654 if (signum
== SIGINT
|| signum
== SIGTERM
) {
655 sock_daemon_run_shutdown(req
);
656 tevent_req_error(req
, EINTR
);
660 static void sock_daemon_run_reconfigure(struct tevent_req
*req
)
662 struct sock_daemon_run_state
*state
= tevent_req_data(
663 req
, struct sock_daemon_run_state
);
664 struct sock_daemon_context
*sockd
= state
->sockd
;
666 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->reconfigure
!= NULL
) {
667 sockd
->funcs
->reconfigure(sockd
->private_data
);
671 static void sock_daemon_run_shutdown(struct tevent_req
*req
)
673 struct sock_daemon_run_state
*state
= tevent_req_data(
674 req
, struct sock_daemon_run_state
);
675 struct sock_daemon_context
*sockd
= state
->sockd
;
676 struct sock_socket
*sock
;
678 D_NOTICE("Shutting down\n");
680 while ((sock
= sockd
->socket_list
) != NULL
) {
681 DLIST_REMOVE(sockd
->socket_list
, sock
);
682 TALLOC_FREE(sock
->req
);
686 if (sockd
->funcs
!= NULL
&& sockd
->funcs
->shutdown
!= NULL
) {
687 sockd
->funcs
->shutdown(sockd
->private_data
);
690 TALLOC_FREE(sockd
->pid_ctx
);
693 static void sock_daemon_run_socket_fail(struct tevent_req
*subreq
)
695 struct tevent_req
*req
= tevent_req_callback_data(
696 subreq
, struct tevent_req
);
700 status
= sock_socket_start_recv(subreq
, &ret
);
702 sock_daemon_run_shutdown(req
);
704 tevent_req_error(req
, ret
);
706 tevent_req_done(req
);
710 static void sock_daemon_run_watch_pid(struct tevent_req
*subreq
)
712 struct tevent_req
*req
= tevent_req_callback_data(
713 subreq
, struct tevent_req
);
714 struct sock_daemon_run_state
*state
= tevent_req_data(
715 req
, struct sock_daemon_run_state
);
719 status
= tevent_wakeup_recv(subreq
);
722 tevent_req_error(req
, EIO
);
726 ret
= kill(state
->pid_watch
, 0);
728 if (errno
== ESRCH
) {
729 D_ERR("PID %d gone away, exiting\n", state
->pid_watch
);
730 sock_daemon_run_shutdown(req
);
731 tevent_req_error(req
, ESRCH
);
734 D_ERR("Failed to check PID status %d, ret=%d\n",
735 state
->pid_watch
, errno
);
739 subreq
= tevent_wakeup_send(state
, state
->ev
,
740 tevent_timeval_current_ofs(5,0));
741 if (tevent_req_nomem(subreq
, req
)) {
744 tevent_req_set_callback(subreq
, sock_daemon_run_watch_pid
, req
);
747 static void sock_daemon_run_wait_done(struct tevent_req
*subreq
)
749 struct tevent_req
*req
= tevent_req_callback_data(
750 subreq
, struct tevent_req
);
751 struct sock_daemon_run_state
*state
= tevent_req_data(
752 req
, struct sock_daemon_run_state
);
753 struct sock_daemon_context
*sockd
= state
->sockd
;
757 status
= sockd
->funcs
->wait_recv(subreq
, &ret
);
759 sock_daemon_run_shutdown(req
);
761 tevent_req_error(req
, ret
);
763 tevent_req_done(req
);
767 bool sock_daemon_run_recv(struct tevent_req
*req
, int *perr
)
771 if (tevent_req_is_unix_error(req
, &ret
)) {
781 int sock_daemon_run(struct tevent_context
*ev
,
782 struct sock_daemon_context
*sockd
,
785 struct tevent_req
*req
;
789 req
= sock_daemon_run_send(ev
, ev
, sockd
, pid_watch
);
794 tevent_req_poll(req
, ev
);
796 status
= sock_daemon_run_recv(req
, &ret
);