summaryrefslogtreecommitdiff
path: root/ctdb/common/sock_daemon.c
blob: 7554cd6da0762791bcc6d954825401e0833acbeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
/*
   A server based on unix domain socket

   Copyright (C) Amitay Isaacs  2016

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/filesys.h"
#include "system/network.h"
#include "system/wait.h"

#include <talloc.h>
#include <tevent.h>

#include "lib/async_req/async_sock.h"
#include "lib/util/debug.h"
#include "lib/util/blocking.h"
#include "lib/util/dlinklist.h"
#include "lib/util/tevent_unix.h"
#include "lib/util/become_daemon.h"

#include "common/logging.h"
#include "common/reqid.h"
#include "common/comm.h"
#include "common/pidfile.h"
#include "common/sock_daemon.h"

struct sock_socket {
	struct sock_socket *prev, *next;

	const char *sockpath;
	struct sock_socket_funcs *funcs;
	void *private_data;

	int fd;
	struct tevent_req *req;
};

struct sock_client {
	struct sock_client *prev, *next;

	struct tevent_req *req;
	struct sock_client_context *client_ctx;
};

struct sock_client_context {
	struct tevent_context *ev;
	struct sock_socket *sock;
	int fd;
	struct comm_context *comm;

	struct sock_client *client;
};

struct sock_daemon_context {
	struct sock_daemon_funcs *funcs;
	void *private_data;

	struct pidfile_context *pid_ctx;
	struct sock_socket *socket_list;
};

/*
 * Process a single client
 */

static void sock_client_read_handler(uint8_t *buf, size_t buflen,
				     void *private_data);
static void sock_client_read_done(struct tevent_req *subreq);
static void sock_client_dead_handler(void *private_data);
static int sock_client_context_destructor(
				struct sock_client_context *client_ctx);

static int sock_client_context_init(TALLOC_CTX *mem_ctx,
				    struct tevent_context *ev,
				    struct sock_socket *sock,
				    int client_fd,
				    struct sock_client *client,
				    struct sock_client_context **result)
{
	struct sock_client_context *client_ctx;
	int ret;

	client_ctx = talloc_zero(mem_ctx, struct sock_client_context);
	if (client_ctx == NULL) {
		return ENOMEM;
	}

	client_ctx->ev = ev;
	client_ctx->sock = sock;
	client_ctx->fd = client_fd;
	client_ctx->client = client;

	ret = comm_setup(client_ctx, ev, client_fd,
			 sock_client_read_handler, client_ctx,
			 sock_client_dead_handler, client_ctx,
			 &client_ctx->comm);
	if (ret != 0) {
		talloc_free(client_ctx);
		return ret;
	}

	if (sock->funcs->connect != NULL) {
		bool status;

		status = sock->funcs->connect(client_ctx, sock->private_data);
		if (! status) {
			talloc_free(client_ctx);
			close(client_fd);
			return 0;
		}
	}

	talloc_set_destructor(client_ctx, sock_client_context_destructor);

	*result = client_ctx;
	return 0;
}

static void sock_client_read_handler(uint8_t *buf, size_t buflen,
				     void *private_data)
{
	struct sock_client_context *client_ctx = talloc_get_type_abort(
		private_data, struct sock_client_context);
	struct sock_socket *sock = client_ctx->sock;
	struct tevent_req *subreq;

	subreq = sock->funcs->read_send(client_ctx, client_ctx->ev,
					client_ctx, buf, buflen,
					sock->private_data);
	if (subreq == NULL) {
		talloc_free(client_ctx);
		return;
	}
	tevent_req_set_callback(subreq, sock_client_read_done, client_ctx);
}

static void sock_client_read_done(struct tevent_req *subreq)
{
	struct sock_client_context *client_ctx = tevent_req_callback_data(
		subreq, struct sock_client_context);
	struct sock_socket *sock = client_ctx->sock;
	int ret;
	bool status;

	status = sock->funcs->read_recv(subreq, &ret);
	if (! status) {
		D_ERR("client read failed with ret=%d\n", ret);
		talloc_free(client_ctx);
	}
}

static void sock_client_dead_handler(void *private_data)
{
	struct sock_client_context *client_ctx = talloc_get_type_abort(
		private_data, struct sock_client_context);
	struct sock_socket *sock = client_ctx->sock;

	if (sock->funcs->disconnect != NULL) {
		sock->funcs->disconnect(client_ctx, sock->private_data);
	}

	talloc_free(client_ctx);
}

static int sock_client_context_destructor(
				struct sock_client_context *client_ctx)
{
	TALLOC_FREE(client_ctx->client);
	TALLOC_FREE(client_ctx->comm);
	if (client_ctx->fd != -1) {
		close(client_ctx->fd);
		client_ctx->fd = -1;
	}

	return 0;
}

/*
 * Process a single listening socket
 */

static int socket_setup(const char *sockpath, bool remove_before_use)
{
	struct sockaddr_un addr;
	size_t len;
	int ret, fd;

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;

	len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
	if (len >= sizeof(addr.sun_path)) {
		D_ERR("socket path too long: %s\n", sockpath);
		return -1;
	}

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1) {
		D_ERR("socket create failed - %s\n", sockpath);
		return -1;
	}

	ret = set_blocking(fd, false);
	if (ret != 0) {
		D_ERR("socket set nonblocking failed - %s\n", sockpath);
		close(fd);
		return -1;
	}

	if (remove_before_use) {
		unlink(sockpath);
	}

	ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
	if (ret != 0) {
		D_ERR("socket bind failed - %s\n", sockpath);
		close(fd);
		return -1;
	}

	ret = listen(fd, 10);
	if (ret != 0) {
		D_ERR("socket listen failed - %s\n", sockpath);
		close(fd);
		return -1;
	}

	D_NOTICE("listening on %s\n", sockpath);

	return fd;
}

static int sock_socket_destructor(struct sock_socket *sock);

static int sock_socket_init(TALLOC_CTX *mem_ctx, const char *sockpath,
			    struct sock_socket_funcs *funcs,
			    void *private_data,
			    struct sock_socket **result)
{
	struct sock_socket *sock;

	if (funcs == NULL) {
		return EINVAL;
	}
	if (funcs->read_send == NULL || funcs->read_recv == NULL) {
		return EINVAL;
	}

	sock = talloc_zero(mem_ctx, struct sock_socket);
	if (sock == NULL) {
		return ENOMEM;
	}

	sock->sockpath = talloc_strdup(sock, sockpath);
	if (sock->sockpath == NULL) {
		talloc_free(sock);
		return ENOMEM;
	}
	sock->funcs = funcs;
	sock->private_data = private_data;
	sock->fd = -1;

	talloc_set_destructor(sock, sock_socket_destructor);

	*result = sock;
	return 0;
}

static int sock_socket_destructor(struct sock_socket *sock)
{
	TALLOC_FREE(sock->req);

	if (sock->fd != -1) {
		close(sock->fd);
		sock->fd = -1;
	}

	unlink(sock->sockpath);
	return 0;
}


struct sock_socket_start_state {
	struct tevent_context *ev;
	struct sock_socket *sock;

	struct sock_client *client_list;
};

static int sock_socket_start_state_destructor(
				struct sock_socket_start_state *state);
static void sock_socket_start_new_client(struct tevent_req *subreq);
static int sock_socket_start_client_destructor(struct sock_client *client);

static struct tevent_req *sock_socket_start_send(TALLOC_CTX *mem_ctx,
						 struct tevent_context *ev,
						 struct sock_socket *sock,
						 bool remove_before_use)
{
	struct tevent_req *req, *subreq;
	struct sock_socket_start_state *state;

	req = tevent_req_create(mem_ctx, &state,
				struct sock_socket_start_state);
	if (req == NULL) {
		return NULL;
	}

	state->ev = ev;
	state->sock = sock;

	sock->fd = socket_setup(sock->sockpath, remove_before_use);
	if (sock->fd == -1) {
		tevent_req_error(req, EIO);
		return tevent_req_post(req, ev);
	}

	talloc_set_destructor(state, sock_socket_start_state_destructor);

	subreq = accept_send(state, ev, sock->fd);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, sock_socket_start_new_client, req);

	sock->req = req;

	return req;
}

static int sock_socket_start_state_destructor(
				struct sock_socket_start_state *state)
{
	struct sock_client *client;

	while ((client = state->client_list) != NULL) {
		talloc_free(client);
	}

	return 0;
}

static void sock_socket_start_new_client(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_socket_start_state *state = tevent_req_data(
		req, struct sock_socket_start_state);
	struct sock_client *client;
	int client_fd, ret;

	client_fd = accept_recv(subreq, NULL, NULL, &ret);
	TALLOC_FREE(subreq);
	if (client_fd == -1) {
		D_ERR("failed to accept new connection\n");
	}

	subreq = accept_send(state, state->ev, state->sock->fd);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, sock_socket_start_new_client, req);

	if (client_fd == -1) {
		return;
	}

	client = talloc_zero(state, struct sock_client);
	if (tevent_req_nomem(client, req)) {
		close(client_fd);
		return;
	}

	client->req = req;

	ret = sock_client_context_init(client, state->ev, state->sock,
				       client_fd, client, &client->client_ctx);
	if (ret != 0) {
		talloc_free(client);
		return;
	}

	talloc_set_destructor(client, sock_socket_start_client_destructor);
	DLIST_ADD(state->client_list, client);
}

static int sock_socket_start_client_destructor(struct sock_client *client)
{
	struct sock_socket_start_state *state = tevent_req_data(
		client->req, struct sock_socket_start_state);

	DLIST_REMOVE(state->client_list, client);
	TALLOC_FREE(client->client_ctx);

	return 0;
}

static bool sock_socket_start_recv(struct tevent_req *req, int *perr,
				   TALLOC_CTX *mem_ctx, const char **sockpath)
{
	struct sock_socket_start_state *state = tevent_req_data(
		req, struct sock_socket_start_state);
	int ret;

	state->sock->req = NULL;

	if (tevent_req_is_unix_error(req, &ret)) {
		if (perr != NULL) {
			*perr = ret;
		}
		return false;
	}

	if (sockpath != NULL) {
		*sockpath = talloc_steal(mem_ctx, state->sock->sockpath);
	}

	return true;
}

/*
 * Send message to a client
 */

struct tevent_req *sock_socket_write_send(TALLOC_CTX *mem_ctx,
					 struct tevent_context *ev,
					 struct sock_client_context *client_ctx,
					 uint8_t *buf, size_t buflen)
{
	struct tevent_req *req;

	req = comm_write_send(mem_ctx, ev, client_ctx->comm, buf, buflen);

	return req;
}

bool sock_socket_write_recv(struct tevent_req *req, int *perr)
{
	int ret;
	bool status;

	status = comm_write_recv(req, &ret);
	if (! status) {
		if (perr != NULL) {
			*perr = ret;
		}
	}

	return status;
}

/*
 * Socket daemon
 */

int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
		      const char *logging, const char *debug_level,
		      struct sock_daemon_funcs *funcs,
		      void *private_data,
		      struct sock_daemon_context **out)
{
	struct sock_daemon_context *sockd;
	int ret;

	sockd = talloc_zero(mem_ctx, struct sock_daemon_context);
	if (sockd == NULL) {
		return ENOMEM;
	}

	sockd->funcs = funcs;
	sockd->private_data = private_data;

	ret = logging_init(sockd, logging, debug_level, daemon_name);
	if (ret != 0) {
		fprintf(stderr,
			"Failed to initialize logging, logging=%s, debug=%s\n",
			logging, debug_level);
		return ret;
	}

	*out = sockd;
	return 0;
}

int sock_daemon_add_unix(struct sock_daemon_context *sockd,
			 const char *sockpath,
			 struct sock_socket_funcs *funcs,
			 void *private_data)
{
	struct sock_socket *sock;
	int ret;

	ret = sock_socket_init(sockd, sockpath, funcs, private_data, &sock);
	if (ret != 0) {
		return ret;
	}


	DLIST_ADD(sockd->socket_list, sock);
	return 0;
}

/*
 * Run socket daemon
 */

struct sock_daemon_run_state {
	struct tevent_context *ev;
	struct sock_daemon_context *sockd;
	pid_t pid_watch;

	int fd;
	int exit_code;
};

static void sock_daemon_run_started(struct tevent_req *subreq);
static void sock_daemon_run_startup_done(struct tevent_req *subreq);
static void sock_daemon_run_signal_handler(struct tevent_context *ev,
					   struct tevent_signal *se,
					   int signum, int count, void *siginfo,
					   void *private_data);
static void sock_daemon_run_reconfigure(struct tevent_req *req);
static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq);
static void sock_daemon_run_shutdown(struct tevent_req *req);
static void sock_daemon_run_shutdown_done(struct tevent_req *subreq);
static void sock_daemon_run_exit(struct tevent_req *req);
static bool sock_daemon_run_socket_listen(struct tevent_req *req);
static void sock_daemon_run_socket_fail(struct tevent_req *subreq);
static void sock_daemon_run_watch_pid(struct tevent_req *subreq);
static void sock_daemon_run_wait(struct tevent_req *req);
static void sock_daemon_run_wait_done(struct tevent_req *subreq);

struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					struct sock_daemon_context *sockd,
					const char *pidfile,
					bool do_fork, bool create_session,
					pid_t pid_watch)
{
	struct tevent_req *req, *subreq;
	struct sock_daemon_run_state *state;
	struct tevent_signal *se;

	req = tevent_req_create(mem_ctx, &state,
				struct sock_daemon_run_state);
	if (req == NULL) {
		return NULL;
	}

	become_daemon(do_fork, !create_session, false);

	if (pidfile != NULL) {
		int ret = pidfile_context_create(sockd, pidfile,
						 &sockd->pid_ctx);
		if (ret != 0) {
			tevent_req_error(req, EEXIST);
			return tevent_req_post(req, ev);
		}
	}

	state->ev = ev;
	state->sockd = sockd;
	state->pid_watch = pid_watch;
	state->fd  = -1;

	subreq = tevent_wakeup_send(state, ev,
				    tevent_timeval_current_ofs(0, 0));
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, sock_daemon_run_started, req);

	se = tevent_add_signal(ev, state, SIGHUP, 0,
			       sock_daemon_run_signal_handler, req);
	if (tevent_req_nomem(se, req)) {
		return tevent_req_post(req, ev);
	}

	se = tevent_add_signal(ev, state, SIGUSR1, 0,
			       sock_daemon_run_signal_handler, req);
	if (tevent_req_nomem(se, req)) {
		return tevent_req_post(req, ev);
	}

	se = tevent_add_signal(ev, state, SIGINT, 0,
			       sock_daemon_run_signal_handler, req);
	if (tevent_req_nomem(se, req)) {
		return tevent_req_post(req, ev);
	}

	se = tevent_add_signal(ev, state, SIGTERM, 0,
			       sock_daemon_run_signal_handler, req);
	if (tevent_req_nomem(se, req)) {
		return tevent_req_post(req, ev);
	}

	if (pid_watch > 1) {
		subreq = tevent_wakeup_send(state, ev,
					    tevent_timeval_current_ofs(1,0));
		if (tevent_req_nomem(subreq, req)) {
			return tevent_req_post(req, ev);
		}
		tevent_req_set_callback(subreq, sock_daemon_run_watch_pid,
					req);
	}

	return req;
}

static void sock_daemon_run_started(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;
	bool status;

	status = tevent_wakeup_recv(subreq);
	TALLOC_FREE(subreq);
	if (! status) {
		tevent_req_error(req, EIO);
		return;
	}

	D_NOTICE("daemon started, pid=%u\n", getpid());

	if (sockd->funcs != NULL && sockd->funcs->startup_send != NULL &&
	    sockd->funcs->startup_recv != NULL) {
		subreq = sockd->funcs->startup_send(state, state->ev,
						    sockd->private_data);
		if (tevent_req_nomem(subreq, req)) {
			return;
		}
		tevent_req_set_callback(subreq, sock_daemon_run_startup_done,
					req);
		return;
	}

	if (sockd->funcs != NULL && sockd->funcs->startup != NULL) {
		int ret;

		ret = sockd->funcs->startup(sockd->private_data);
		if (ret != 0) {
			D_ERR("startup failed, ret=%d\n", ret);
			tevent_req_error(req, EIO);
			return;
		}

		D_NOTICE("startup completed successfully\n");
	}

	status = sock_daemon_run_socket_listen(req);
	if (! status) {
		return;
	}
	sock_daemon_run_wait(req);
}

static void sock_daemon_run_startup_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;
	int ret;
	bool status;

	status = sockd->funcs->startup_recv(subreq, &ret);
	TALLOC_FREE(subreq);
	if (! status) {
		D_ERR("startup failed, ret=%d\n", ret);
		tevent_req_error(req, EIO);
		return;
	}

	D_NOTICE("startup completed succesfully\n");

	status = sock_daemon_run_socket_listen(req);
	if (! status) {
		return;
	}
	sock_daemon_run_wait(req);
}

static void sock_daemon_run_signal_handler(struct tevent_context *ev,
					   struct tevent_signal *se,
					   int signum, int count, void *siginfo,
					   void *private_data)
{
	struct tevent_req *req = talloc_get_type_abort(
		private_data, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);

	D_NOTICE("Received signal %d\n", signum);

	if (signum == SIGHUP || signum == SIGUSR1) {
		sock_daemon_run_reconfigure(req);
		return;
	}

	if (signum == SIGINT || signum == SIGTERM) {
		state->exit_code = EINTR;
		sock_daemon_run_shutdown(req);
	}
}

static void sock_daemon_run_reconfigure(struct tevent_req *req)
{
	struct tevent_req *subreq;
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;

	if (sockd->funcs != NULL && sockd->funcs->reconfigure_send != NULL &&
	    sockd->funcs->reconfigure_recv != NULL) {
		subreq = sockd->funcs->reconfigure_send(state, state->ev,
							sockd->private_data);
		if (tevent_req_nomem(subreq, req)) {
			return;
		}
		tevent_req_set_callback(subreq,
					sock_daemon_run_reconfigure_done, req);
		return;
	}

	if (sockd->funcs != NULL && sockd->funcs->reconfigure != NULL) {
		int ret;

		ret = sockd->funcs->reconfigure(sockd->private_data);
		if (ret != 0) {
			D_ERR("reconfigure failed, ret=%d\n", ret);
			return;
		}

		D_NOTICE("reconfigure completed successfully\n");
	}
}

static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;
	int ret;
	bool status;

	status = sockd->funcs->reconfigure_recv(subreq, &ret);
	TALLOC_FREE(subreq);
	if (! status) {
		D_ERR("reconfigure failed, ret=%d\n", ret);
		return;
	}

	D_NOTICE("reconfigure completed successfully\n");
}

static void sock_daemon_run_shutdown(struct tevent_req *req)
{
	struct tevent_req *subreq;
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;
	struct sock_socket *sock;

	D_NOTICE("Shutting down\n");

	while ((sock = sockd->socket_list) != NULL) {
		DLIST_REMOVE(sockd->socket_list, sock);
		TALLOC_FREE(sock);
	}

	if (sockd->funcs != NULL && sockd->funcs->shutdown_send != NULL &&
	    sockd->funcs->shutdown_recv != NULL) {
		subreq = sockd->funcs->shutdown_send(state, state->ev,
						     sockd->private_data);
		if (subreq == NULL) {
			sock_daemon_run_exit(req);
			return;
		}
		tevent_req_set_callback(subreq, sock_daemon_run_shutdown_done,
						req);
		return;
	}

	if (sockd->funcs != NULL && sockd->funcs->shutdown != NULL) {
		sockd->funcs->shutdown(sockd->private_data);
	}

	sock_daemon_run_exit(req);
}

static void sock_daemon_run_shutdown_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;

	sockd->funcs->shutdown_recv(subreq);
	TALLOC_FREE(subreq);

	sock_daemon_run_exit(req);
}

static void sock_daemon_run_exit(struct tevent_req *req)
{
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;

	TALLOC_FREE(sockd->pid_ctx);

	if (state->exit_code == 0) {
		tevent_req_done(req);
	} else {
		tevent_req_error(req, state->exit_code);
	}
}

static bool sock_daemon_run_socket_listen(struct tevent_req *req)
{
	struct tevent_req *subreq;
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;
	struct sock_socket *sock;
	bool remove_before_use = false;

	if (sockd->pid_ctx != NULL) {
		remove_before_use = true;
	}
	for (sock = sockd->socket_list; sock != NULL; sock = sock->next) {
		subreq = sock_socket_start_send(state, state->ev, sock,
						remove_before_use);
		if (tevent_req_nomem(subreq, req)) {
			return false;
		}
		tevent_req_set_callback(subreq, sock_daemon_run_socket_fail,
					req);
	}

	return true;
}

static void sock_daemon_run_socket_fail(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	const char *sockpath = NULL;
	int ret = 0;
	bool status;

	status = sock_socket_start_recv(subreq, &ret, state, &sockpath);
	TALLOC_FREE(subreq);
	if (! status) {
		D_ERR("socket %s closed unexpectedly\n", sockpath);
		state->exit_code = ret;
	} else {
		state->exit_code = 0;
	}

	sock_daemon_run_shutdown(req);
}

static void sock_daemon_run_watch_pid(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	int ret;
	bool status;

	status = tevent_wakeup_recv(subreq);
	TALLOC_FREE(subreq);
	if (! status) {
		tevent_req_error(req, EIO);
		return;
	}

	ret = kill(state->pid_watch, 0);
	if (ret == -1) {
		if (errno == ESRCH) {
			D_ERR("PID %d gone away, exiting\n", state->pid_watch);
			state->exit_code = ESRCH;
			sock_daemon_run_shutdown(req);
			return;
		} else {
			D_ERR("Failed to check PID status %d, ret=%d\n",
			      state->pid_watch, errno);
		}
	}

	subreq = tevent_wakeup_send(state, state->ev,
				    tevent_timeval_current_ofs(5,0));
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, sock_daemon_run_watch_pid, req);
}

static void sock_daemon_run_wait(struct tevent_req *req)
{
	struct tevent_req *subreq;
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;

	if (sockd->funcs != NULL && sockd->funcs->wait_send != NULL &&
	    sockd->funcs->wait_recv != NULL) {
		subreq = sockd->funcs->wait_send(state, state->ev,
						 sockd->private_data);
		if (tevent_req_nomem(subreq, req)) {
			return;
		}
		tevent_req_set_callback(subreq, sock_daemon_run_wait_done,
					req);
	}
}

static void sock_daemon_run_wait_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct sock_daemon_run_state *state = tevent_req_data(
		req, struct sock_daemon_run_state);
	struct sock_daemon_context *sockd = state->sockd;
	int ret = 0;
	bool status;

	status = sockd->funcs->wait_recv(subreq, &ret);
	TALLOC_FREE(subreq);
	if (! status) {
		state->exit_code = ret;
	} else {
		state->exit_code = 0;
	}

	sock_daemon_run_shutdown(req);
}

bool sock_daemon_run_recv(struct tevent_req *req, int *perr)
{
	int ret;

	if (tevent_req_is_unix_error(req, &ret)) {
		if (perr != NULL) {
			*perr = ret;
		}
		return false;
	}

	return true;
}

int sock_daemon_run(struct tevent_context *ev,
		    struct sock_daemon_context *sockd,
		    const char *pidfile,
		    bool do_fork, bool create_session,
		    pid_t pid_watch)
{
	struct tevent_req *req;
	int ret;
	bool status;

	req = sock_daemon_run_send(ev, ev, sockd,
				   pidfile, do_fork, create_session, pid_watch);
	if (req == NULL) {
		return ENOMEM;
	}

	tevent_req_poll(req, ev);

	status = sock_daemon_run_recv(req, &ret);
	TALLOC_FREE(req);
	if (! status) {
		return ret;
	}

	return 0;
}