ldb_tdb: Use braces in ltdb_dn_list_find_val()
[Samba.git] / ctdb / common / sock_daemon.c
blob56205d019ecc4c2980c8a9336c216d6699081df5
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"
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"
41 struct sock_socket {
42 struct sock_socket *prev, *next;
44 const char *sockpath;
45 struct sock_socket_funcs *funcs;
46 void *private_data;
48 int fd;
49 struct tevent_req *req;
52 struct sock_client {
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;
62 int fd;
63 struct comm_context *comm;
65 struct sock_client *client;
68 struct sock_daemon_context {
69 struct sock_daemon_funcs *funcs;
70 void *private_data;
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,
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);
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)
94 struct sock_client_context *client_ctx;
95 int ret;
97 client_ctx = talloc_zero(mem_ctx, struct sock_client_context);
98 if (client_ctx == NULL) {
99 return ENOMEM;
102 client_ctx->ev = ev;
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,
110 &client_ctx->comm);
111 if (ret != 0) {
112 talloc_free(client_ctx);
113 return ret;
116 if (sock->funcs->connect != NULL) {
117 bool status;
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;
127 talloc_set_destructor(client_ctx, sock_client_context_destructor);
129 *result = client_ctx;
130 return 0;
133 static void sock_client_read_handler(uint8_t *buf, size_t buflen,
134 void *private_data)
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,
143 sock->private_data);
144 if (subreq == NULL) {
145 talloc_free(client_ctx);
146 return;
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;
156 int ret;
157 bool status;
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);
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);
186 client_ctx->fd = -1;
189 return 0;
193 * Process a single listening socket
196 static int socket_setup(const char *sockpath, bool remove_before_use)
198 struct sockaddr_un addr;
199 size_t len;
200 int ret, fd;
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);
208 return -1;
211 fd = socket(AF_UNIX, SOCK_STREAM, 0);
212 if (fd == -1) {
213 D_ERR("socket create failed - %s\n", sockpath);
214 return -1;
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;
224 if (remove_before_use) {
225 unlink(sockpath);
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;
235 ret = listen(fd, 10);
236 if (ret != 0) {
237 D_ERR("socket listen failed - %s\n", sockpath);
238 close(fd);
239 return -1;
242 return fd;
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,
249 void *private_data,
250 bool remove_before_use,
251 struct sock_socket **result)
253 struct sock_socket *sock;
255 if (funcs == NULL) {
256 return EINVAL;
258 if (funcs->read_send == NULL || funcs->read_recv == NULL) {
259 return EINVAL;
262 sock = talloc_zero(mem_ctx, struct sock_socket);
263 if (sock == NULL) {
264 return ENOMEM;
267 sock->sockpath = sockpath;
268 sock->funcs = funcs;
269 sock->private_data = private_data;
271 sock->fd = socket_setup(sockpath, remove_before_use);
272 if (sock->fd == -1) {
273 talloc_free(sock);
274 return EIO;
277 talloc_set_destructor(sock, sock_socket_destructor);
279 *result = sock;
280 return 0;
283 static int sock_socket_destructor(struct sock_socket *sock)
285 if (sock->fd != -1) {
286 close(sock->fd);
287 sock->fd = -1;
290 unlink(sock->sockpath);
291 return 0;
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);
316 if (req == NULL) {
317 return NULL;
320 state->ev = ev;
321 state->sock = sock;
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);
331 return 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) {
340 talloc_free(client);
343 return 0;
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;
353 int client_fd, ret;
355 client_fd = accept_recv(subreq, NULL, NULL, &ret);
356 TALLOC_FREE(subreq);
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)) {
363 return;
365 tevent_req_set_callback(subreq, sock_socket_start_new_client, req);
367 if (client_fd == -1) {
368 return;
371 client = talloc_zero(state, struct sock_client);
372 if (tevent_req_nomem(client, req)) {
373 close(client_fd);
374 return;
377 client->req = req;
379 ret = sock_client_context_init(client, state->ev, state->sock,
380 client_fd, client, &client->client_ctx);
381 if (ret != 0) {
382 talloc_free(client);
383 return;
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);
398 return 0;
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);
405 int ret;
407 state->sock->req = NULL;
409 if (tevent_req_is_unix_error(req, &ret)) {
410 if (perr != NULL) {
411 *perr = ret;
413 return false;
416 return true;
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);
432 return req;
435 bool sock_socket_write_recv(struct tevent_req *req, int *perr)
437 int ret;
438 bool status;
440 status = comm_write_recv(req, &ret);
441 if (! status) {
442 if (perr != NULL) {
443 *perr = ret;
447 return status;
451 * Socket daemon
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,
457 void *private_data,
458 struct sock_daemon_context **out)
460 struct sock_daemon_context *sockd;
461 int ret;
463 sockd = talloc_zero(mem_ctx, struct sock_daemon_context);
464 if (sockd == NULL) {
465 return ENOMEM;
468 sockd->funcs = funcs;
469 sockd->private_data = private_data;
471 ret = logging_init(sockd, logging, debug_level, daemon_name);
472 if (ret != 0) {
473 fprintf(stderr,
474 "Failed to initialize logging, logging=%s, debug=%s\n",
475 logging, debug_level);
476 return ret;
479 *out = sockd;
480 return 0;
483 int sock_daemon_add_unix(struct sock_daemon_context *sockd,
484 const char *sockpath,
485 struct sock_socket_funcs *funcs,
486 void *private_data)
488 struct sock_socket *sock;
489 int ret;
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);
496 if (ret != 0) {
497 return ret;
500 D_NOTICE("listening on %s\n", sockpath);
502 DLIST_ADD(sockd->socket_list, sock);
503 return 0;
507 * Run socket daemon
510 struct sock_daemon_run_state {
511 struct tevent_context *ev;
512 struct sock_daemon_context *sockd;
513 pid_t pid_watch;
515 int fd;
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,
522 void *private_data);
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,
532 const char *pidfile,
533 bool do_fork, bool create_session,
534 pid_t pid_watch)
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);
543 if (req == NULL) {
544 return NULL;
547 become_daemon(do_fork, !create_session, false);
549 if (pidfile != NULL) {
550 int ret = pidfile_context_create(sockd, pidfile,
551 &sockd->pid_ctx);
552 if (ret != 0) {
553 tevent_req_error(req, EEXIST);
554 return tevent_req_post(req, ev);
558 state->ev = ev;
559 state->sockd = sockd;
560 state->pid_watch = pid_watch;
561 state->fd = -1;
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,
600 req);
602 sock->req = subreq;
605 if (pid_watch > 1) {
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,
612 req);
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,
623 req);
626 return req;
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,
647 void *private_data)
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);
656 return;
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);
688 TALLOC_FREE(sock);
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);
702 int ret = 0;
703 bool status;
705 status = sock_socket_start_recv(subreq, &ret);
706 TALLOC_FREE(subreq);
707 sock_daemon_run_shutdown(req);
708 if (! status) {
709 tevent_req_error(req, ret);
710 } else {
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);
721 int ret;
722 bool status;
724 status = tevent_wakeup_recv(subreq);
725 TALLOC_FREE(subreq);
726 if (! status) {
727 tevent_req_error(req, EIO);
728 return;
731 ret = kill(state->pid_watch, 0);
732 if (ret == -1) {
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);
737 return;
738 } else {
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)) {
747 return;
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;
759 int ret;
760 bool status;
762 status = sockd->funcs->wait_recv(subreq, &ret);
763 TALLOC_FREE(subreq);
764 sock_daemon_run_shutdown(req);
765 if (! status) {
766 tevent_req_error(req, ret);
767 } else {
768 tevent_req_done(req);
772 bool sock_daemon_run_recv(struct tevent_req *req, int *perr)
774 int ret;
776 if (tevent_req_is_unix_error(req, &ret)) {
777 if (perr != NULL) {
778 *perr = ret;
780 return false;
783 return true;
786 int sock_daemon_run(struct tevent_context *ev,
787 struct sock_daemon_context *sockd,
788 const char *pidfile,
789 bool do_fork, bool create_session,
790 pid_t pid_watch)
792 struct tevent_req *req;
793 int ret;
794 bool status;
796 req = sock_daemon_run_send(ev, ev, sockd,
797 pidfile, do_fork, create_session, pid_watch);
798 if (req == NULL) {
799 return ENOMEM;
802 tevent_req_poll(req, ev);
804 status = sock_daemon_run_recv(req, &ret);
805 TALLOC_FREE(req);
806 if (! status) {
807 return ret;
810 return 0;