-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathserver.c
2114 lines (1916 loc) · 58 KB
/
server.c
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
/*
* server.c -- nsd(8) network input/output
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#ifndef SHUT_WR
#define SHUT_WR 1
#endif
#include "axfr.h"
#include "namedb.h"
#include "netio.h"
#include "xfrd.h"
#include "xfrd-tcp.h"
#include "difffile.h"
#include "nsec3.h"
#include "ipc.h"
#include "udb.h"
#include "remote.h"
#define RELOAD_SYNC_TIMEOUT 25 /* seconds */
/*
* Data for the UDP handlers.
*/
struct udp_handler_data
{
struct nsd *nsd;
struct nsd_socket *socket;
query_type *query;
};
/*
* Data for the TCP accept handlers. Most data is simply passed along
* to the TCP connection handler.
*/
struct tcp_accept_handler_data {
struct nsd *nsd;
struct nsd_socket *socket;
size_t tcp_accept_handler_count;
netio_handler_type *tcp_accept_handlers;
};
int slowaccept;
struct timespec slowaccept_timeout;
/*
* Data for the TCP connection handlers.
*
* The TCP handlers use non-blocking I/O. This is necessary to avoid
* blocking the entire server on a slow TCP connection, but does make
* reading from and writing to the socket more complicated.
*
* Basically, whenever a read/write would block (indicated by the
* EAGAIN errno variable) we remember the position we were reading
* from/writing to and return from the TCP reading/writing event
* handler. When the socket becomes readable/writable again we
* continue from the same position.
*/
struct tcp_handler_data
{
/*
* The region used to allocate all TCP connection related
* data, including this structure. This region is destroyed
* when the connection is closed.
*/
region_type* region;
/*
* The global nsd structure.
*/
struct nsd* nsd;
/*
* The current query data for this TCP connection.
*/
query_type* query;
/*
* These fields are used to enable the TCP accept handlers
* when the number of TCP connection drops below the maximum
* number of TCP connections.
*/
size_t tcp_accept_handler_count;
netio_handler_type* tcp_accept_handlers;
/*
* The query_state is used to remember if we are performing an
* AXFR, if we're done processing, or if we should discard the
* query and connection.
*/
query_state_type query_state;
/*
* The bytes_transmitted field is used to remember the number
* of bytes transmitted when receiving or sending a DNS
* packet. The count includes the two additional bytes used
* to specify the packet length on a TCP connection.
*/
size_t bytes_transmitted;
/*
* The number of queries handled by this specific TCP connection.
*/
int query_count;
};
/*
* Handle incoming queries on the UDP server sockets.
*/
static void handle_udp(netio_type *netio,
netio_handler_type *handler,
netio_event_types_type event_types);
/*
* Handle incoming connections on the TCP sockets. These handlers
* usually wait for the NETIO_EVENT_READ event (indicating an incoming
* connection) but are disabled when the number of current TCP
* connections is equal to the maximum number of TCP connections.
* Disabling is done by changing the handler to wait for the
* NETIO_EVENT_NONE type. This is done using the function
* configure_tcp_accept_handlers.
*/
static void handle_tcp_accept(netio_type *netio,
netio_handler_type *handler,
netio_event_types_type event_types);
/*
* Handle incoming queries on a TCP connection. The TCP connections
* are configured to be non-blocking and the handler may be called
* multiple times before a complete query is received.
*/
static void handle_tcp_reading(netio_type *netio,
netio_handler_type *handler,
netio_event_types_type event_types);
/*
* Handle outgoing responses on a TCP connection. The TCP connections
* are configured to be non-blocking and the handler may be called
* multiple times before a complete response is sent.
*/
static void handle_tcp_writing(netio_type *netio,
netio_handler_type *handler,
netio_event_types_type event_types);
/*
* Send all children the quit nonblocking, then close pipe.
*/
static void send_children_quit(struct nsd* nsd);
/* set childrens flags to send NSD_STATS to them */
#ifdef BIND8_STATS
static void set_children_stats(struct nsd* nsd);
#endif /* BIND8_STATS */
/*
* Change the event types the HANDLERS are interested in to
* EVENT_TYPES.
*/
static void configure_handler_event_types(size_t count,
netio_handler_type *handlers,
netio_event_types_type event_types);
static uint16_t *compressed_dname_offsets = 0;
static uint32_t compression_table_capacity = 0;
static uint32_t compression_table_size = 0;
/*
* Remove the specified pid from the list of child pids. Returns -1 if
* the pid is not in the list, child_num otherwise. The field is set to 0.
*/
static int
delete_child_pid(struct nsd *nsd, pid_t pid)
{
size_t i;
for (i = 0; i < nsd->child_count; ++i) {
if (nsd->children[i].pid == pid) {
nsd->children[i].pid = 0;
if(!nsd->children[i].need_to_exit) {
if(nsd->children[i].child_fd != -1)
close(nsd->children[i].child_fd);
nsd->children[i].child_fd = -1;
if(nsd->children[i].handler)
nsd->children[i].handler->fd = -1;
}
return i;
}
}
return -1;
}
/*
* Restart child servers if necessary.
*/
static int
restart_child_servers(struct nsd *nsd, region_type* region, netio_type* netio,
int* xfrd_sock_p)
{
struct main_ipc_handler_data *ipc_data;
size_t i;
int sv[2];
/* Fork the child processes... */
for (i = 0; i < nsd->child_count; ++i) {
if (nsd->children[i].pid <= 0) {
if (nsd->children[i].child_fd != -1)
close(nsd->children[i].child_fd);
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
log_msg(LOG_ERR, "socketpair: %s",
strerror(errno));
return -1;
}
nsd->children[i].child_fd = sv[0];
nsd->children[i].parent_fd = sv[1];
nsd->children[i].pid = fork();
switch (nsd->children[i].pid) {
default: /* SERVER MAIN */
close(nsd->children[i].parent_fd);
nsd->children[i].parent_fd = -1;
if(!nsd->children[i].handler)
{
ipc_data = (struct main_ipc_handler_data*) region_alloc(
region, sizeof(struct main_ipc_handler_data));
ipc_data->nsd = nsd;
ipc_data->child = &nsd->children[i];
ipc_data->child_num = i;
ipc_data->xfrd_sock = xfrd_sock_p;
ipc_data->packet = buffer_create(region, QIOBUFSZ);
ipc_data->forward_mode = 0;
ipc_data->got_bytes = 0;
ipc_data->total_bytes = 0;
ipc_data->acl_num = 0;
nsd->children[i].handler = (struct netio_handler*) region_alloc(
region, sizeof(struct netio_handler));
nsd->children[i].handler->fd = nsd->children[i].child_fd;
nsd->children[i].handler->timeout = NULL;
nsd->children[i].handler->user_data = ipc_data;
nsd->children[i].handler->event_types = NETIO_EVENT_READ;
nsd->children[i].handler->event_handler = parent_handle_child_command;
netio_add_handler(netio, nsd->children[i].handler);
}
/* clear any ongoing ipc */
ipc_data = (struct main_ipc_handler_data*)
nsd->children[i].handler->user_data;
ipc_data->forward_mode = 0;
/* restart - update fd */
nsd->children[i].handler->fd = nsd->children[i].child_fd;
break;
case 0: /* CHILD */
/* the child need not be able to access the
* nsd.db file */
namedb_close_udb(nsd->db);
nsd->pid = 0;
nsd->child_count = 0;
nsd->server_kind = nsd->children[i].kind;
nsd->this_child = &nsd->children[i];
/* remove signal flags inherited from parent
the parent will handle them. */
nsd->signal_hint_reload_hup = 0;
nsd->signal_hint_reload = 0;
nsd->signal_hint_child = 0;
nsd->signal_hint_quit = 0;
nsd->signal_hint_shutdown = 0;
nsd->signal_hint_stats = 0;
nsd->signal_hint_statsusr = 0;
close(nsd->this_child->child_fd);
nsd->this_child->child_fd = -1;
server_child(nsd);
/* NOTREACH */
exit(0);
case -1:
log_msg(LOG_ERR, "fork failed: %s",
strerror(errno));
return -1;
}
}
}
return 0;
}
#ifdef BIND8_STATS
static void set_bind8_alarm(struct nsd* nsd)
{
/* resync so that the next alarm is on the next whole minute */
if(nsd->st.period > 0) /* % by 0 gives divbyzero error */
alarm(nsd->st.period - (time(NULL) % nsd->st.period));
}
#endif
static void
cleanup_dname_compression_tables(void *ptr)
{
free(ptr);
compressed_dname_offsets = NULL;
compression_table_capacity = 0;
}
static void
initialize_dname_compression_tables(struct nsd *nsd)
{
size_t needed = domain_table_count(nsd->db->domains) + 1;
needed += EXTRA_DOMAIN_NUMBERS;
if(compression_table_capacity < needed) {
if(compressed_dname_offsets) {
region_remove_cleanup(nsd->db->region,
cleanup_dname_compression_tables,
compressed_dname_offsets);
free(compressed_dname_offsets);
}
compressed_dname_offsets = (uint16_t *) xalloc(
needed * sizeof(uint16_t));
region_add_cleanup(nsd->db->region, cleanup_dname_compression_tables,
compressed_dname_offsets);
compression_table_capacity = needed;
compression_table_size=domain_table_count(nsd->db->domains)+1;
}
memset(compressed_dname_offsets, 0, needed * sizeof(uint16_t));
compressed_dname_offsets[0] = QHEADERSZ; /* The original query name */
}
/*
* Initialize the server, create and bind the sockets.
*
*/
int
server_init(struct nsd *nsd)
{
size_t i;
#if defined(SO_REUSEADDR) || (defined(INET6) && (defined(IPV6_V6ONLY) || defined(IPV6_USE_MIN_MTU) || defined(IPV6_MTU)))
int on = 1;
#endif
/* UDP */
/* Make a socket... */
for (i = 0; i < nsd->ifs; i++) {
if (!nsd->udp[i].addr) {
nsd->udp[i].s = -1;
continue;
}
if ((nsd->udp[i].s = socket(nsd->udp[i].addr->ai_family, nsd->udp[i].addr->ai_socktype, 0)) == -1) {
#if defined(INET6)
if (nsd->udp[i].addr->ai_family == AF_INET6 &&
errno == EAFNOSUPPORT && nsd->grab_ip6_optional) {
log_msg(LOG_WARNING, "fallback to UDP4, no IPv6: not supported");
continue;
}
#endif /* INET6 */
log_msg(LOG_ERR, "can't create a socket: %s", strerror(errno));
return -1;
}
#if defined(INET6)
if (nsd->udp[i].addr->ai_family == AF_INET6) {
# if defined(IPV6_V6ONLY)
if (setsockopt(nsd->udp[i].s,
IPPROTO_IPV6, IPV6_V6ONLY,
&on, sizeof(on)) < 0)
{
log_msg(LOG_ERR, "setsockopt(..., IPV6_V6ONLY, ...) failed: %s",
strerror(errno));
return -1;
}
# endif
# if defined(IPV6_USE_MIN_MTU)
/*
* There is no fragmentation of IPv6 datagrams
* during forwarding in the network. Therefore
* we do not send UDP datagrams larger than
* the minimum IPv6 MTU of 1280 octets. The
* EDNS0 message length can be larger if the
* network stack supports IPV6_USE_MIN_MTU.
*/
if (setsockopt(nsd->udp[i].s,
IPPROTO_IPV6, IPV6_USE_MIN_MTU,
&on, sizeof(on)) < 0)
{
log_msg(LOG_ERR, "setsockopt(..., IPV6_USE_MIN_MTU, ...) failed: %s",
strerror(errno));
return -1;
}
# elif defined(IPV6_MTU)
/*
* On Linux, PMTUD is disabled by default for datagrams
* so set the MTU equal to the MIN MTU to get the same.
*/
on = IPV6_MIN_MTU;
if (setsockopt(nsd->udp[i].s, IPPROTO_IPV6, IPV6_MTU,
&on, sizeof(on)) < 0)
{
log_msg(LOG_ERR, "setsockopt(..., IPV6_MTU, ...) failed: %s",
strerror(errno));
return -1;
}
on = 1;
# endif
}
#endif
#if defined(AF_INET)
if (nsd->udp[i].addr->ai_family == AF_INET) {
# if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
int action = IP_PMTUDISC_DONT;
if (setsockopt(nsd->udp[i].s, IPPROTO_IP,
IP_MTU_DISCOVER, &action, sizeof(action)) < 0)
{
log_msg(LOG_ERR, "setsockopt(..., IP_MTU_DISCOVER, IP_PMTUDISC_DONT...) failed: %s",
strerror(errno));
return -1;
}
# elif defined(IP_DONTFRAG)
int off = 0;
if (setsockopt(nsd->udp[i].s, IPPROTO_IP, IP_DONTFRAG,
&off, sizeof(off)) < 0)
{
log_msg(LOG_ERR, "setsockopt(..., IP_DONTFRAG, ...) failed: %s",
strerror(errno));
return -1;
}
# endif
}
#endif
/* set it nonblocking */
/* otherwise, on OSes with thundering herd problems, the
UDP recv could block NSD after select returns readable. */
if (fcntl(nsd->udp[i].s, F_SETFL, O_NONBLOCK) == -1) {
log_msg(LOG_ERR, "cannot fcntl udp: %s", strerror(errno));
}
/* Bind it... */
if (bind(nsd->udp[i].s, (struct sockaddr *) nsd->udp[i].addr->ai_addr, nsd->udp[i].addr->ai_addrlen) != 0) {
log_msg(LOG_ERR, "can't bind udp socket: %s", strerror(errno));
return -1;
}
}
/* TCP */
/* Make a socket... */
for (i = 0; i < nsd->ifs; i++) {
if (!nsd->tcp[i].addr) {
nsd->tcp[i].s = -1;
continue;
}
if ((nsd->tcp[i].s = socket(nsd->tcp[i].addr->ai_family, nsd->tcp[i].addr->ai_socktype, 0)) == -1) {
#if defined(INET6)
if (nsd->tcp[i].addr->ai_family == AF_INET6 &&
errno == EAFNOSUPPORT && nsd->grab_ip6_optional) {
log_msg(LOG_WARNING, "fallback to TCP4, no IPv6: not supported");
continue;
}
#endif /* INET6 */
log_msg(LOG_ERR, "can't create a socket: %s", strerror(errno));
return -1;
}
#ifdef SO_REUSEADDR
if (setsockopt(nsd->tcp[i].s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
log_msg(LOG_ERR, "setsockopt(..., SO_REUSEADDR, ...) failed: %s", strerror(errno));
}
#endif /* SO_REUSEADDR */
#if defined(INET6) && defined(IPV6_V6ONLY)
if (nsd->tcp[i].addr->ai_family == AF_INET6 &&
setsockopt(nsd->tcp[i].s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
{
log_msg(LOG_ERR, "setsockopt(..., IPV6_V6ONLY, ...) failed: %s", strerror(errno));
return -1;
}
#endif
/* set it nonblocking */
/* (StevensUNP p463), if tcp listening socket is blocking, then
it may block in accept, even if select() says readable. */
if (fcntl(nsd->tcp[i].s, F_SETFL, O_NONBLOCK) == -1) {
log_msg(LOG_ERR, "cannot fcntl tcp: %s", strerror(errno));
}
/* Bind it... */
if (bind(nsd->tcp[i].s, (struct sockaddr *) nsd->tcp[i].addr->ai_addr, nsd->tcp[i].addr->ai_addrlen) != 0) {
log_msg(LOG_ERR, "can't bind tcp socket: %s", strerror(errno));
return -1;
}
/* Listen to it... */
if (listen(nsd->tcp[i].s, TCP_BACKLOG) == -1) {
log_msg(LOG_ERR, "can't listen: %s", strerror(errno));
return -1;
}
}
return 0;
}
/*
* Prepare the server for take off.
*
*/
int
server_prepare(struct nsd *nsd)
{
/* Open the database... */
if ((nsd->db = namedb_open(nsd->dbfile, nsd->options)) == NULL) {
log_msg(LOG_ERR, "unable to open the database %s: %s",
nsd->dbfile, strerror(errno));
return -1;
}
/* check if zone files have been modified */
/* NULL for taskudb because we send soainfo in a moment, batched up,
* for all zones */
namedb_check_zonefiles(nsd->db, nsd->options, NULL, NULL);
/* Read diff file */
if(!diff_read_file(nsd->db, nsd->options, NULL, NULL, NULL)) {
log_msg(LOG_ERR, "The diff file contains errors. Will continue "
"without it");
}
compression_table_capacity = 0;
initialize_dname_compression_tables(nsd);
#ifdef BIND8_STATS
/* Initialize times... */
time(&nsd->st.boot);
set_bind8_alarm(nsd);
#endif /* BIND8_STATS */
return 0;
}
/*
* Fork the required number of servers.
*/
static int
server_start_children(struct nsd *nsd, region_type* region, netio_type* netio,
int* xfrd_sock_p)
{
size_t i;
/* Start all child servers initially. */
for (i = 0; i < nsd->child_count; ++i) {
nsd->children[i].pid = 0;
}
return restart_child_servers(nsd, region, netio, xfrd_sock_p);
}
static void
close_all_sockets(struct nsd_socket sockets[], size_t n)
{
size_t i;
/* Close all the sockets... */
for (i = 0; i < n; ++i) {
if (sockets[i].s != -1) {
close(sockets[i].s);
freeaddrinfo(sockets[i].addr);
sockets[i].s = -1;
}
}
}
/*
* Close the sockets, shutdown the server and exit.
* Does not return.
*
*/
static void
server_shutdown(struct nsd *nsd)
{
size_t i;
close_all_sockets(nsd->udp, nsd->ifs);
close_all_sockets(nsd->tcp, nsd->ifs);
/* CHILD: close command channel to parent */
if(nsd->this_child && nsd->this_child->parent_fd != -1)
{
close(nsd->this_child->parent_fd);
nsd->this_child->parent_fd = -1;
}
/* SERVER: close command channels to children */
if(!nsd->this_child)
{
for(i=0; i < nsd->child_count; ++i)
if(nsd->children[i].child_fd != -1)
{
close(nsd->children[i].child_fd);
nsd->children[i].child_fd = -1;
}
}
#ifdef HAVE_SSL
daemon_remote_close(nsd->rc); /* close sockets of rc */
#endif
log_finalize();
tsig_finalize();
#ifdef HAVE_SSL
daemon_remote_delete(nsd->rc); /* ssl-delete secret keys */
#endif
#if 0 /* OS collects memory pages */
nsd_options_destroy(nsd->options);
region_destroy(nsd->region);
#endif
exit(0);
}
void
server_prepare_xfrd(struct nsd* nsd)
{
char tmpfile[256];
/* create task mmaps */
nsd->mytask = 0;
snprintf(tmpfile, sizeof(tmpfile), "/tmp/nsd.%u.task.0",
(unsigned)getpid());
nsd->task[0] = task_file_create(tmpfile);
snprintf(tmpfile, sizeof(tmpfile), "/tmp/nsd.%u.task.1",
(unsigned)getpid());
nsd->task[1] = task_file_create(tmpfile);
assert(udb_base_get_userdata(nsd->task[0])->data == 0);
assert(udb_base_get_userdata(nsd->task[1])->data == 0);
/* create xfrd listener structure */
nsd->xfrd_pid = -1;
nsd->xfrd_listener = region_alloc(nsd->region,
sizeof(netio_handler_type));
nsd->xfrd_listener->user_data = (struct ipc_handler_conn_data*)
region_alloc(nsd->region, sizeof(struct ipc_handler_conn_data));
nsd->xfrd_listener->fd = -1;
((struct ipc_handler_conn_data*)nsd->xfrd_listener->user_data)->nsd =
nsd;
((struct ipc_handler_conn_data*)nsd->xfrd_listener->user_data)->conn =
xfrd_tcp_create(nsd->region);
}
void
server_start_xfrd(struct nsd *nsd, int del_db, int reload_active)
{
pid_t pid;
int sockets[2] = {0,0};
struct ipc_handler_conn_data *data;
if(nsd->xfrd_listener->fd != -1)
close(nsd->xfrd_listener->fd);
if(del_db) {
/* recreate taskdb that xfrd was using, it may be corrupt */
/* we (or reload) use nsd->mytask, and xfrd uses the other */
char* tmpfile = nsd->task[1-nsd->mytask]->fname;
nsd->task[1-nsd->mytask]->fname = NULL;
/* free alloc already, so udb does not shrink itself */
udb_alloc_delete(nsd->task[1-nsd->mytask]->alloc);
nsd->task[1-nsd->mytask]->alloc = NULL;
udb_base_free(nsd->task[1-nsd->mytask]);
/* create new file, overwrite the old one */
nsd->task[1-nsd->mytask] = task_file_create(tmpfile);
free(tmpfile);
}
/* old xfrd may have crashed, snip off garbage off the diff file */
diff_snip_garbage(nsd->db, nsd->options);
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
log_msg(LOG_ERR, "startxfrd failed on socketpair: %s", strerror(errno));
nsd->xfrd_pid = -1;
return;
}
pid = fork();
switch (pid) {
case -1:
log_msg(LOG_ERR, "fork xfrd failed: %s", strerror(errno));
break;
case 0:
/* CHILD: close first socket, use second one */
close(sockets[0]);
if(del_db) xfrd_free_namedb(nsd);
/* use other task than I am using, since if xfrd died and is
* restarted, the reload is using nsd->mytask */
nsd->mytask = 1 - nsd->mytask;
xfrd_init(sockets[1], nsd, del_db, reload_active);
/* ENOTREACH */
break;
default:
/* PARENT: close second socket, use first one */
close(sockets[1]);
nsd->xfrd_listener->fd = sockets[0];
break;
}
/* PARENT only */
nsd->xfrd_listener->timeout = NULL;
nsd->xfrd_listener->event_types = NETIO_EVENT_READ;
nsd->xfrd_listener->event_handler = parent_handle_xfrd_command;
/* clear ongoing ipc reads */
data = (struct ipc_handler_conn_data *) nsd->xfrd_listener->user_data;
data->conn->is_reading = 0;
nsd->xfrd_pid = pid;
}
/** add all soainfo to taskdb */
static void
add_all_soa_to_task(struct nsd* nsd, struct udb_base* taskudb)
{
struct radnode* n;
udb_ptr task_last; /* last task, mytask is empty so NULL */
/* add all SOA INFO to mytask */
udb_ptr_init(&task_last, taskudb);
for(n=radix_first(nsd->db->zonetree); n; n=radix_next(n)) {
task_new_soainfo(taskudb, &task_last, (zone_type*)n->elem);
}
udb_ptr_unlink(&task_last, taskudb);
}
void
server_send_soa_xfrd(struct nsd* nsd, int shortsoa)
{
/* normally this exchanges the SOA from nsd->xfrd and the expire back.
* parent fills one taskdb with soas, xfrd fills other with expires.
* then they exchange and process.
* shortsoa: xfrd crashes and needs to be restarted and one taskdb
* may be in use by reload. Fill SOA in taskdb and give to xfrd.
* expire notifications can be sent back via a normal reload later
* (xfrd will wait for current running reload to finish if any).
*/
sig_atomic_t cmd = 0;
int xfrd_sock = nsd->xfrd_listener->fd;
struct udb_base* taskudb = nsd->task[nsd->mytask];
udb_ptr t;
if(shortsoa) {
/* put SOA in xfrd task because mytask may be in use */
taskudb = nsd->task[1-nsd->mytask];
}
add_all_soa_to_task(nsd, taskudb);
if(!shortsoa) {
/* wait for xfrd to signal task is ready, RELOAD signal */
if(block_read(nsd, xfrd_sock, &cmd, sizeof(cmd), -1) != sizeof(cmd) ||
cmd != NSD_RELOAD) {
log_msg(LOG_ERR, "did not get start signal from xfrd");
exit(1);
}
}
/* give xfrd our task, signal it with RELOAD_DONE */
task_process_sync(taskudb);
cmd = NSD_RELOAD_DONE;
if(!write_socket(xfrd_sock, &cmd, sizeof(cmd))) {
log_msg(LOG_ERR, "problems sending soa end from reload %d to xfrd: %s",
(int)nsd->pid, strerror(errno));
}
if(!shortsoa) {
/* process the xfrd task works (expiry data) */
nsd->mytask = 1 - nsd->mytask;
taskudb = nsd->task[nsd->mytask];
task_remap(taskudb);
udb_ptr_new(&t, taskudb, udb_base_get_userdata(taskudb));
while(!udb_ptr_is_null(&t)) {
task_process_expire(nsd->db, TASKLIST(&t));
udb_ptr_set_rptr(&t, taskudb, &TASKLIST(&t)->next);
}
udb_ptr_unlink(&t, taskudb);
task_clear(taskudb);
/* tell xfrd that the task is emptied, signal with RELOAD_DONE */
cmd = NSD_RELOAD_DONE;
if(!write_socket(xfrd_sock, &cmd, sizeof(cmd))) {
log_msg(LOG_ERR, "problems sending soa end from reload %d to xfrd: %s",
(int)nsd->pid, strerror(errno));
}
}
}
/* pass timeout=-1 for blocking. Returns size, 0, -1(err), or -2(timeout) */
ssize_t
block_read(struct nsd* nsd, int s, void* p, ssize_t sz, int timeout)
{
uint8_t* buf = (uint8_t*) p;
ssize_t total = 0;
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
while( total < sz) {
ssize_t ret;
FD_SET(s, &rfds);
tv.tv_sec = timeout;
tv.tv_usec = 0;
ret = select(s+1, &rfds, NULL, NULL, timeout==-1?NULL:&tv);
if(ret == -1) {
if(errno == EAGAIN)
/* blocking read */
continue;
if(errno == EINTR) {
if(nsd && (nsd->signal_hint_quit || nsd->signal_hint_shutdown))
return -1;
/* other signals can be handled later */
continue;
}
/* some error */
return -1;
}
if(ret == 0) {
/* operation timed out */
return -2;
}
ret = read(s, buf+total, sz-total);
if(ret == -1) {
if(errno == EAGAIN)
/* blocking read */
continue;
if(errno == EINTR) {
if(nsd && (nsd->signal_hint_quit || nsd->signal_hint_shutdown))
return -1;
/* other signals can be handled later */
continue;
}
/* some error */
return -1;
}
if(ret == 0) {
/* closed connection! */
return 0;
}
total += ret;
}
return total;
}
static void
reload_process_tasks(struct nsd* nsd, udb_ptr* last_task)
{
udb_ptr t, next;
udb_base* u = nsd->task[nsd->mytask];
udb_ptr_init(&next, u);
udb_ptr_new(&t, u, udb_base_get_userdata(u));
udb_base_set_userdata(u, 0);
while(!udb_ptr_is_null(&t)) {
/* store next in list so this one can be deleted or reused */
udb_ptr_set_rptr(&next, u, &TASKLIST(&t)->next);
udb_rptr_zero(&TASKLIST(&t)->next, u);
/* process task t */
/* append results for task t and update last_task */
task_process_in_reload(nsd, u, last_task, &t);
/* go to next */
udb_ptr_set_ptr(&t, u, &next);
}
udb_ptr_unlink(&t, u);
udb_ptr_unlink(&next, u);
}
#ifdef BIND8_STATS
static void
parent_send_stats(struct nsd* nsd, int cmdfd)
{
size_t i;
if(!write_socket(cmdfd, &nsd->st, sizeof(nsd->st))) {
log_msg(LOG_ERR, "could not write stats to reload");
return;
}
for(i=0; i<nsd->child_count; i++)
if(!write_socket(cmdfd, &nsd->children[i].query_count,
sizeof(stc_t))) {
log_msg(LOG_ERR, "could not write stats to reload");
return;
}
}
static void
reload_do_stats(int cmdfd, struct nsd* nsd, udb_ptr* last)
{
struct nsdst s;
stc_t* p;
size_t i;
if(block_read(nsd, cmdfd, &s, sizeof(s),
RELOAD_SYNC_TIMEOUT) != sizeof(s)) {
log_msg(LOG_ERR, "could not read stats from oldpar");
return;
}
s.db_disk = nsd->db->udb->base_size;
s.db_mem = region_get_mem(nsd->db->region);
p = (stc_t*)task_new_stat_info(nsd->task[nsd->mytask], last, &s,
nsd->child_count);
if(!p) return;
for(i=0; i<nsd->child_count; i++) {
if(block_read(nsd, cmdfd, p++, sizeof(stc_t), 1)!=sizeof(stc_t))
return;
}
}
#endif /* BIND8_STATS */
/*
* Reload the database, stop parent, re-fork children and continue.
* as server_main.
*/
static void
server_reload(struct nsd *nsd, region_type* server_region, netio_type* netio,
int cmdsocket)
{
pid_t old_pid;
sig_atomic_t cmd = NSD_QUIT_SYNC;
int ret;
udb_ptr last_task;
/* see what tasks we got from xfrd */
task_remap(nsd->task[nsd->mytask]);
udb_ptr_init(&last_task, nsd->task[nsd->mytask]);
reload_process_tasks(nsd, &last_task);
if(!diff_read_file(nsd->db, nsd->options, NULL,
nsd->task[nsd->mytask], &last_task)) {
log_msg(LOG_ERR, "unable to load the diff file: %s", nsd->options->difffile);
exit(1);
}
#ifndef NDEBUG
if(nsd_debug_level >= 1)
region_log_stats(nsd->db->region);
#endif /* NDEBUG */
/* sync to disk (if needed) */
udb_base_sync(nsd->db->udb, 0);
initialize_dname_compression_tables(nsd);
/* Get our new process id */
old_pid = nsd->pid;
nsd->pid = getpid();
#ifdef BIND8_STATS
/* Restart dumping stats if required. */
time(&nsd->st.boot);
set_bind8_alarm(nsd);
#endif
/* Start new child processes */
if (server_start_children(nsd, server_region, netio, &nsd->
xfrd_listener->fd) != 0) {
send_children_quit(nsd);
exit(1);
}
/* if the parent has quit, we must quit too, poll the fd for cmds */
if(block_read(nsd, cmdsocket, &cmd, sizeof(cmd), 0) == sizeof(cmd)) {
DEBUG(DEBUG_IPC,1, (LOG_INFO, "reload: ipc command from main %d", (int)cmd));
if(cmd == NSD_QUIT) {
DEBUG(DEBUG_IPC,1, (LOG_INFO, "reload: quit to follow nsd"));
send_children_quit(nsd);
exit(0);
}
}
/* Overwrite pid before closing old parent, to avoid race condition:
* - parent process already closed
* - pidfile still contains old_pid
* - control script contacts parent process, using contents of pidfile
*/
if (writepid(nsd) == -1) {
log_msg(LOG_ERR, "cannot overwrite the pidfile %s: %s", nsd->pidfile, strerror(errno));
}
/* Send quit command to parent: blocking, wait for receipt. */
do {
DEBUG(DEBUG_IPC,1, (LOG_INFO, "reload: ipc send quit to main"));
if (write_socket(cmdsocket, &cmd, sizeof(cmd)) == -1)
{
log_msg(LOG_ERR, "problems sending command from reload %d to oldnsd %d: %s",
(int)nsd->pid, (int)old_pid, strerror(errno));
}
/* blocking: wait for parent to really quit. (it sends RELOAD as ack) */
DEBUG(DEBUG_IPC,1, (LOG_INFO, "reload: ipc wait for ack main"));
ret = block_read(nsd, cmdsocket, &cmd, sizeof(cmd),
RELOAD_SYNC_TIMEOUT);
if(ret == -2) {
DEBUG(DEBUG_IPC, 1, (LOG_ERR, "reload timeout QUITSYNC. retry"));
}
} while (ret == -2);
if(ret == -1) {
log_msg(LOG_ERR, "reload: could not wait for parent to quit: %s",
strerror(errno));
}