summaryrefslogtreecommitdiffstats
path: root/libasyncns/asyncns.c
blob: 4088ce7385cca30f34477d6a1ac921c728a5d2d1 (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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
/***
  This file is part of libasyncns.

  Copyright 2005-2008 Lennart Poettering

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

  libasyncns 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
  Lesser General Public License for more details.

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

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* #undef HAVE_PTHREAD */

#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <pwd.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <dirent.h>

#if HAVE_ARPA_NAMESER_COMPAT_H
#include <arpa/nameser_compat.h>
#endif

#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif

#if HAVE_PTHREAD
#include <pthread.h>
#endif

#include "asyncns.h"

#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif

#define MAX_WORKERS 16
#define MAX_QUERIES 256
#define BUFSIZE (10240)

typedef enum {
    REQUEST_ADDRINFO,
    RESPONSE_ADDRINFO,
    REQUEST_NAMEINFO,
    RESPONSE_NAMEINFO,
    REQUEST_RES_QUERY,
    REQUEST_RES_SEARCH,
    RESPONSE_RES,
    REQUEST_TERMINATE,
    RESPONSE_DIED
} query_type_t;

enum {
    REQUEST_RECV_FD = 0,
    REQUEST_SEND_FD = 1,
    RESPONSE_RECV_FD = 2,
    RESPONSE_SEND_FD = 3,
    MESSAGE_FD_MAX = 4
};

struct asyncns {
    int fds[4];

#ifndef HAVE_PTHREAD
    pid_t workers[MAX_WORKERS];
#else
    pthread_t workers[MAX_WORKERS];
#endif
    unsigned valid_workers;

    unsigned current_id, current_index;
    asyncns_query_t* queries[MAX_QUERIES];

    asyncns_query_t *done_head, *done_tail;

    int n_queries;
    int dead;
};

struct asyncns_query {
    asyncns_t *asyncns;
    int done;
    unsigned id;
    query_type_t type;
    asyncns_query_t *done_next, *done_prev;
    int ret;
    int _errno;
    int _h_errno;
    struct addrinfo *addrinfo;
    char *serv, *host;
    void *userdata;
};

typedef struct rheader {
    query_type_t type;
    unsigned id;
    size_t length;
} rheader_t;

typedef struct addrinfo_request {
    struct rheader header;
    int hints_is_null;
    int ai_flags;
    int ai_family;
    int ai_socktype;
    int ai_protocol;
    size_t node_len, service_len;
} addrinfo_request_t;

typedef struct addrinfo_response {
    struct rheader header;
    int ret;
    int _errno;
    int _h_errno;
    /* followed by addrinfo_serialization[] */
} addrinfo_response_t;

typedef struct addrinfo_serialization {
    int ai_flags;
    int ai_family;
    int ai_socktype;
    int ai_protocol;
    size_t ai_addrlen;
    size_t canonname_len;
    /* Followed by ai_addr amd ai_canonname with variable lengths */
} addrinfo_serialization_t;

typedef struct nameinfo_request {
    struct rheader header;
    int flags;
    socklen_t sockaddr_len;
    int gethost, getserv;
} nameinfo_request_t;

typedef struct nameinfo_response {
    struct rheader header;
    size_t hostlen, servlen;
    int ret;
    int _errno;
    int _h_errno;
} nameinfo_response_t;

typedef struct res_query_request {
    struct rheader header;
    int class;
    int type;
    size_t dname_len;
} res_request_t;

typedef struct res_query_response {
    struct rheader header;
    int ret;
    int _errno;
    int _h_errno;
} res_response_t;

#ifndef HAVE_STRNDUP

static char *strndup(const char *s, size_t l) {
    size_t a;
    char *n;

    a = strlen(s);
    if (a > l)
        a = l;

    if (!(n = malloc(a+1)))
        return NULL;

    memcpy(n, s, a);
    n[a] = 0;

    return n;
}

#endif

#ifndef HAVE_PTHREAD

static int close_allv(const int except_fds[]) {
    struct rlimit rl;
    int fd;

#ifdef __linux__

    DIR *d;

    assert(except_fds);

    if ((d = opendir("/proc/self/fd"))) {

        struct dirent *de;

        while ((de = readdir(d))) {
            int found;
            long l;
            char *e = NULL;
            int i;

            if (de->d_name[0] == '.')
                continue;

            errno = 0;
            l = strtol(de->d_name, &e, 10);
            if (errno != 0 || !e || *e) {
                closedir(d);
                errno = EINVAL;
                return -1;
            }

            fd = (int) l;

            if ((long) fd != l) {
                closedir(d);
                errno = EINVAL;
                return -1;
            }

            if (fd < 3)
                continue;

            if (fd == dirfd(d))
                continue;

            found = 0;
            for (i = 0; except_fds[i] >= 0; i++)
                if (except_fds[i] == fd) {
                    found = 1;
                    break;
                }

            if (found)
                continue;

            if (close(fd) < 0) {
                int saved_errno;

                saved_errno = errno;
                closedir(d);
                errno = saved_errno;

                return -1;
            }
        }

        closedir(d);
        return 0;
    }

#endif

    if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
        return -1;

    for (fd = 0; fd < (int) rl.rlim_max; fd++) {
        int i;

        if (fd <= 3)
            continue;

        for (i = 0; except_fds[i] >= 0; i++)
            if (except_fds[i] == fd)
                continue;

        if (close(fd) < 0 && errno != EBADF)
            return -1;
    }

    return 0;
}

static int reset_sigsv(const int except[]) {
    int sig;
    assert(except);

    for (sig = 1; sig < NSIG; sig++) {
        int reset = 1;

        switch (sig) {
            case SIGKILL:
            case SIGSTOP:
                reset = 0;
                break;

            default: {
                int i;

                for (i = 0; except[i] > 0; i++) {
                    if (sig == except[i]) {
                        reset = 0;
                        break;
                    }
                }
            }
        }

        if (reset) {
            struct sigaction sa;

            memset(&sa, 0, sizeof(sa));
            sa.sa_handler = SIG_DFL;

            /* On Linux the first two RT signals are reserved by
             * glibc, and sigaction() will return EINVAL for them. */
            if ((sigaction(sig, &sa, NULL) < 0))
                if (errno != EINVAL)
                    return -1;
        }
    }

    return 0;
}

static int ignore_sigsv(const int ignore[]) {
    int i;
    assert(ignore);

    for (i = 0; ignore[i] > 0; i++) {
        struct sigaction sa;

        memset(&sa, 0, sizeof(sa));
        sa.sa_handler = SIG_IGN;

        if ((sigaction(ignore[i], &sa, NULL) < 0))
            return -1;
    }

    return 0;
}

#endif

static int fd_nonblock(int fd) {
    int i;
    assert(fd >= 0);

    if ((i = fcntl(fd, F_GETFL, 0)) < 0)
        return -1;

    if (i & O_NONBLOCK)
        return 0;

    return fcntl(fd, F_SETFL, i | O_NONBLOCK);
}

static int fd_cloexec(int fd) {
    int v;
    assert(fd >= 0);

    if ((v = fcntl(fd, F_GETFD, 0)) < 0)
        return -1;

    if (v & FD_CLOEXEC)
        return 0;

    return fcntl(fd, F_SETFD, v | FD_CLOEXEC);
}

static int send_died(int out_fd) {
    rheader_t rh;
    assert(out_fd > 0);

    memset(&rh, 0, sizeof(rh));
    rh.type = RESPONSE_DIED;
    rh.id = 0;
    rh.length = sizeof(rh);

    return send(out_fd, &rh, rh.length, MSG_NOSIGNAL);
}

static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) {
    addrinfo_serialization_t s;
    size_t cnl, l;
    assert(p);
    assert(ai);
    assert(length);
    assert(*length <= maxlength);

    cnl = (ai->ai_canonname ? strlen(ai->ai_canonname)+1 : 0);
    l = sizeof(addrinfo_serialization_t) + ai->ai_addrlen + cnl;

    if (*length + l > maxlength)
        return NULL;

    s.ai_flags = ai->ai_flags;
    s.ai_family = ai->ai_family;
    s.ai_socktype = ai->ai_socktype;
    s.ai_protocol = ai->ai_protocol;
    s.ai_addrlen = ai->ai_addrlen;
    s.canonname_len = cnl;

    memcpy((uint8_t*) p, &s, sizeof(addrinfo_serialization_t));
    memcpy((uint8_t*) p + sizeof(addrinfo_serialization_t), ai->ai_addr, ai->ai_addrlen);

    if (ai->ai_canonname)
        strcpy((char*) p + sizeof(addrinfo_serialization_t) + ai->ai_addrlen, ai->ai_canonname);

    *length += l;
    return (uint8_t*) p + l;
}

static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo *ai, int _errno, int _h_errno) {
    addrinfo_response_t data[BUFSIZE/sizeof(addrinfo_response_t) + 1];
    addrinfo_response_t *resp = data;
    assert(out_fd >= 0);

    memset(data, 0, sizeof(data));
    resp->header.type = RESPONSE_ADDRINFO;
    resp->header.id = id;
    resp->header.length = sizeof(addrinfo_response_t);
    resp->ret = ret;
    resp->_errno = _errno;
    resp->_h_errno = _h_errno;

    if (ret == 0 && ai) {
        void *p = data + 1;
        struct addrinfo *k;

        for (k = ai; k; k = k->ai_next) {

            if (!(p = serialize_addrinfo(p, k, &resp->header.length, (char*) data + BUFSIZE - (char*) p))) {
                resp->ret = EAI_MEMORY;
                break;
            }
        }
    }

    if (ai)
        freeaddrinfo(ai);

    return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
}

static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) {
    nameinfo_response_t data[BUFSIZE/sizeof(nameinfo_response_t) + 1];
    size_t hl, sl;
    nameinfo_response_t *resp = data;

    assert(out_fd >= 0);

    sl = serv ? strlen(serv)+1 : 0;
    hl = host ? strlen(host)+1 : 0;

    memset(data, 0, sizeof(data));
    resp->header.type = RESPONSE_NAMEINFO;
    resp->header.id = id;
    resp->header.length = sizeof(nameinfo_response_t) + hl + sl;
    resp->ret = ret;
    resp->_errno = _errno;
    resp->_h_errno = _h_errno;
    resp->hostlen = hl;
    resp->servlen = sl;

    assert(sizeof(data) >= resp->header.length);

    if (host)
        memcpy((uint8_t *)data + sizeof(nameinfo_response_t), host, hl);

    if (serv)
        memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl);

    return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
}

static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) {
    res_response_t data[BUFSIZE/sizeof(res_response_t) + 1];
    res_response_t *resp = data;

    assert(out_fd >= 0);

    memset(data, 0, sizeof(data));
    resp->header.type = RESPONSE_RES;
    resp->header.id = id;
    resp->header.length = sizeof(res_response_t) + (ret < 0 ? 0 : ret);
    resp->ret = ret;
    resp->_errno = _errno;
    resp->_h_errno = _h_errno;

    assert(sizeof(data) >= resp->header.length);

    if (ret > 0)
        memcpy((uint8_t *)data + sizeof(res_response_t), answer, ret);

    return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
}

static int handle_request(int out_fd, const rheader_t *req, size_t length) {
    assert(out_fd >= 0);
    assert(req);
    assert(length >= sizeof(rheader_t));
    assert(length == req->length);

    switch (req->type) {
        case REQUEST_ADDRINFO: {
            struct addrinfo ai, *result = NULL;
            const addrinfo_request_t *ai_req = (const addrinfo_request_t*) req;
            const char *node, *service;
            int ret;

            assert(length >= sizeof(addrinfo_request_t));
            assert(length == sizeof(addrinfo_request_t) + ai_req->node_len + ai_req->service_len);

            memset(&ai, 0, sizeof(ai));
            ai.ai_flags = ai_req->ai_flags;
            ai.ai_family = ai_req->ai_family;
            ai.ai_socktype = ai_req->ai_socktype;
            ai.ai_protocol = ai_req->ai_protocol;

            node = ai_req->node_len ? (const char*) req + sizeof(addrinfo_request_t) : NULL;
            service = ai_req->service_len ? (const char*) req + sizeof(addrinfo_request_t) + ai_req->node_len : NULL;

            ret = getaddrinfo(node, service,
                              ai_req->hints_is_null ? NULL : &ai,
                              &result);

            /* send_addrinfo_reply() frees result */
            return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno);
        }

        case REQUEST_NAMEINFO: {
            int ret;
            const nameinfo_request_t *ni_req = (const nameinfo_request_t*) req;
            char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
            struct sockaddr_storage sa;

            assert(length >= sizeof(nameinfo_request_t));
            assert(length == sizeof(nameinfo_request_t) + ni_req->sockaddr_len);

            memcpy(&sa, (const uint8_t *)req + sizeof(nameinfo_request_t), ni_req->sockaddr_len);

            ret = getnameinfo((struct sockaddr *)&sa, ni_req->sockaddr_len,
                              ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0,
                              ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0,
                              ni_req->flags);

            return send_nameinfo_reply(out_fd, req->id, ret,
                                       ret == 0 && ni_req->gethost ? hostbuf : NULL,
                                       ret == 0 && ni_req->getserv ? servbuf : NULL,
                                       errno, h_errno);
        }

        case REQUEST_RES_QUERY:
        case REQUEST_RES_SEARCH: {
            int ret;
            HEADER answer[BUFSIZE/sizeof(HEADER) + 1];
            const res_request_t *res_req = (const res_request_t *)req;
            const char *dname;

            assert(length >= sizeof(res_request_t));
            assert(length == sizeof(res_request_t) + res_req->dname_len);

            dname = (const char *) req + sizeof(res_request_t);

            if (req->type == REQUEST_RES_QUERY)
                ret = res_query(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);
            else
                ret = res_search(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);

            return send_res_reply(out_fd, req->id, (unsigned char *) answer, ret, errno, h_errno);
        }

        case REQUEST_TERMINATE:
            /* Quit */
            return -1;

        default:
            ;
    }

    return 0;
}

#ifndef HAVE_PTHREAD

static int process_worker(int in_fd, int out_fd) {
    int have_death_sig = 0;
    int good_fds[3];
    int ret = 1;

    const int ignore_sigs[] = {
        SIGINT,
        SIGHUP,
        SIGPIPE,
        SIGUSR1,
        SIGUSR2,
        -1
    };

    assert(in_fd > 2);
    assert(out_fd > 2);

    close(0);
    close(1);
    close(2);

    if (open("/dev/null", O_RDONLY) != 0)
        goto fail;

    if (open("/dev/null", O_WRONLY) != 1)
        goto fail;

    if (open("/dev/null", O_WRONLY) != 2)
        goto fail;

    if (chdir("/") < 0)
        goto fail;

    if (geteuid() == 0) {
        struct passwd *pw;
        int r;

        if ((pw = getpwnam("nobody"))) {
#ifdef HAVE_SETRESUID
            r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
#elif HAVE_SETREUID
            r = setreuid(pw->pw_uid, pw->pw_uid);
#else
            if ((r = setuid(pw->pw_uid)) >= 0)
                r = seteuid(pw->pw_uid);
#endif
            if (r < 0)
                goto fail;
        }
    }

    if (reset_sigsv(ignore_sigs) < 0)
        goto fail;

    if (ignore_sigsv(ignore_sigs) < 0)
        goto fail;

    good_fds[0] = in_fd; good_fds[1] = out_fd; good_fds[2] = -1;
    if (close_allv(good_fds) < 0)
        goto fail;

#ifdef PR_SET_PDEATHSIG
    if (prctl(PR_SET_PDEATHSIG, SIGTERM) >= 0)
        have_death_sig = 1;
#endif

    if (!have_death_sig)
        fd_nonblock(in_fd);

    while (getppid() > 1) { /* if the parent PID is 1 our parent process died. */
        rheader_t buf[BUFSIZE/sizeof(rheader_t) + 1];
        ssize_t length;

        if (!have_death_sig) {
            fd_set fds;
            struct timeval tv = { 0, 500000 };

            FD_ZERO(&fds);
            FD_SET(in_fd, &fds);

            if (select(in_fd+1, &fds, NULL, NULL, &tv) < 0)
                break;

            if (getppid() == 1)
                break;
        }

        if ((length = recv(in_fd, buf, sizeof(buf), 0)) <= 0) {

            if (length < 0 &&
                (errno == EAGAIN || errno == EINTR))
                continue;

            break;
        }

        if (handle_request(out_fd, buf, (size_t) length) < 0)
            break;
    }

    ret = 0;

fail:
    send_died(out_fd);

    return ret;
}

#else

static void* thread_worker(void *p) {
    asyncns_t *asyncns = p;
    sigset_t fullset;

    /* No signals in this thread please */
    sigfillset(&fullset);
    pthread_sigmask(SIG_BLOCK, &fullset, NULL);

    while (!asyncns->dead) {
        rheader_t buf[BUFSIZE/sizeof(rheader_t) + 1];
        ssize_t length;

        if ((length = recv(asyncns->fds[REQUEST_RECV_FD], buf, sizeof(buf), 0)) <= 0) {

            if (length < 0 &&
                (errno == EAGAIN || errno == EINTR))
                continue;

            break;
        }

        if (asyncns->dead)
            break;

        if (handle_request(asyncns->fds[RESPONSE_SEND_FD], buf, (size_t) length) < 0)
            break;
    }

    send_died(asyncns->fds[RESPONSE_SEND_FD]);

    return NULL;
}

#endif

asyncns_t* asyncns_new(unsigned n_proc) {
    asyncns_t *asyncns = NULL;
    int i;
    assert(n_proc >= 1);

    if (n_proc > MAX_WORKERS)
        n_proc = MAX_WORKERS;

    if (!(asyncns = malloc(sizeof(asyncns_t)))) {
        errno = ENOMEM;
        goto fail;
    }

    asyncns->dead = 0;
    asyncns->valid_workers = 0;

    for (i = 0; i < MESSAGE_FD_MAX; i++)
        asyncns->fds[i] = -1;

    memset(asyncns->queries, 0, sizeof(asyncns->queries));

    if (socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds) < 0 ||
        socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds+2) < 0)
        goto fail;

    for (i = 0; i < MESSAGE_FD_MAX; i++)
        fd_cloexec(asyncns->fds[i]);

    for (asyncns->valid_workers = 0; asyncns->valid_workers < n_proc; asyncns->valid_workers++) {

#ifndef HAVE_PTHREAD
        if ((asyncns->workers[asyncns->valid_workers] = fork()) < 0)
            goto fail;
        else if (asyncns->workers[asyncns->valid_workers] == 0) {
            int ret;

            close(asyncns->fds[REQUEST_SEND_FD]);
            close(asyncns->fds[RESPONSE_RECV_FD]);
            ret = process_worker(asyncns->fds[REQUEST_RECV_FD], asyncns->fds[RESPONSE_SEND_FD]);
            close(asyncns->fds[REQUEST_RECV_FD]);
            close(asyncns->fds[RESPONSE_SEND_FD]);
            _exit(ret);
        }
#else
        int r;

        if ((r = pthread_create(&asyncns->workers[asyncns->valid_workers], NULL, thread_worker, asyncns)) != 0) {
            errno = r;
            goto fail;
        }
#endif
    }

#ifndef HAVE_PTHREAD
    close(asyncns->fds[REQUEST_RECV_FD]);
    close(asyncns->fds[RESPONSE_SEND_FD]);
    asyncns->fds[REQUEST_RECV_FD] = asyncns->fds[RESPONSE_SEND_FD] = -1;
#endif

    asyncns->current_index = asyncns->current_id = 0;
    asyncns->done_head = asyncns->done_tail = NULL;
    asyncns->n_queries = 0;

    fd_nonblock(asyncns->fds[RESPONSE_RECV_FD]);

    return asyncns;

fail:
    if (asyncns)
        asyncns_free(asyncns);

    return NULL;
}

void asyncns_free(asyncns_t *asyncns) {
    int i;
    int saved_errno = errno;
    unsigned p;

    assert(asyncns);

    asyncns->dead = 1;

    if (asyncns->fds[REQUEST_SEND_FD] >= 0) {
        rheader_t req;

        memset(&req, 0, sizeof(req));
        req.type = REQUEST_TERMINATE;
        req.length = sizeof(req);
        req.id = 0;

        /* Send one termination packet for each worker */
        for (p = 0; p < asyncns->valid_workers; p++)
            send(asyncns->fds[REQUEST_SEND_FD], &req, req.length, MSG_NOSIGNAL);
    }

    /* Now terminate them and wait until they are gone. */
    for (p = 0; p < asyncns->valid_workers; p++) {
#ifndef HAVE_PTHREAD
        kill(asyncns->workers[p], SIGTERM);
        for (;;) {
            if (waitpid(asyncns->workers[p], NULL, 0) >= 0 || errno != EINTR)
                break;
        }
#else
        for (;;) {
            if (pthread_join(asyncns->workers[p], NULL) != EINTR)
                break;
        }
#endif
    }

    /* Close all communication channels */
    for (i = 0; i < MESSAGE_FD_MAX; i++)
        if (asyncns->fds[i] >= 0)
            close(asyncns->fds[i]);

    for (p = 0; p < MAX_QUERIES; p++)
        if (asyncns->queries[p])
            asyncns_cancel(asyncns, asyncns->queries[p]);

    free(asyncns);

    errno = saved_errno;
}

int asyncns_fd(asyncns_t *asyncns) {
    assert(asyncns);

    return asyncns->fds[RESPONSE_RECV_FD];
}

static asyncns_query_t *lookup_query(asyncns_t *asyncns, unsigned id) {
    asyncns_query_t *q;
    assert(asyncns);

    if ((q = asyncns->queries[id % MAX_QUERIES]))
        if (q->id == id)
            return q;

    return NULL;
}

static void complete_query(asyncns_t *asyncns, asyncns_query_t *q) {
    assert(asyncns);
    assert(q);
    assert(!q->done);

    q->done = 1;

    if ((q->done_prev = asyncns->done_tail))
        asyncns->done_tail->done_next = q;
    else
        asyncns->done_head = q;

    asyncns->done_tail = q;
    q->done_next = NULL;
}

static void *unserialize_addrinfo(void *p, struct addrinfo **ret_ai, size_t *length) {
    addrinfo_serialization_t s;
    size_t l;
    struct addrinfo *ai;
    assert(p);
    assert(ret_ai);
    assert(length);

    if (*length < sizeof(addrinfo_serialization_t))
        return NULL;

    memcpy(&s, p, sizeof(s));

    l = sizeof(addrinfo_serialization_t) + s.ai_addrlen + s.canonname_len;
    if (*length < l)
        return NULL;

    if (!(ai = malloc(sizeof(struct addrinfo))))
        goto fail;

    ai->ai_addr = NULL;
    ai->ai_canonname = NULL;
    ai->ai_next = NULL;

    if (s.ai_addrlen && !(ai->ai_addr = malloc(s.ai_addrlen)))
        goto fail;

    if (s.canonname_len && !(ai->ai_canonname = malloc(s.canonname_len)))
        goto fail;

    ai->ai_flags = s.ai_flags;
    ai->ai_family = s.ai_family;
    ai->ai_socktype = s.ai_socktype;
    ai->ai_protocol = s.ai_protocol;
    ai->ai_addrlen = s.ai_addrlen;

    if (ai->ai_addr)
        memcpy(ai->ai_addr, (uint8_t*) p + sizeof(addrinfo_serialization_t), s.ai_addrlen);

    if (ai->ai_canonname)
        memcpy(ai->ai_canonname, (uint8_t*) p + sizeof(addrinfo_serialization_t) + s.ai_addrlen, s.canonname_len);

    *length -= l;
    *ret_ai = ai;

    return (uint8_t*) p + l;


fail:
    if (ai)
        asyncns_freeaddrinfo(ai);

    return NULL;
}

static int handle_response(asyncns_t *asyncns, rheader_t *resp, size_t length) {
    asyncns_query_t *q;
    assert(asyncns);
    assert(resp);
    assert(length >= sizeof(rheader_t));
    assert(length == resp->length);

    if (resp->type == RESPONSE_DIED) {
        asyncns->dead = 1;
        return 0;
    }

    if (!(q = lookup_query(asyncns, resp->id)))
        return 0;

    switch (resp->type) {
        case RESPONSE_ADDRINFO: {
            const addrinfo_response_t *ai_resp = (addrinfo_response_t*) resp;
            void *p;
            size_t l;
            struct addrinfo *prev = NULL;

            assert(length >= sizeof(addrinfo_response_t));
            assert(q->type == REQUEST_ADDRINFO);

            q->ret = ai_resp->ret;
            q->_errno = ai_resp->_errno;
            q->_h_errno = ai_resp->_h_errno;
            l = length - sizeof(addrinfo_response_t);
            p = (uint8_t*) resp + sizeof(addrinfo_response_t);

            while (l > 0 && p) {
                struct addrinfo *ai = NULL;
                p = unserialize_addrinfo(p, &ai, &l);

                if (!p || !ai) {
                    q->ret = EAI_MEMORY;
                    break;
                }

                if (prev)
                    prev->ai_next = ai;
                else
                    q->addrinfo = ai;

                prev = ai;
            }

            complete_query(asyncns, q);
            break;
        }

        case RESPONSE_NAMEINFO: {
            const nameinfo_response_t *ni_resp = (nameinfo_response_t*) resp;

            assert(length >= sizeof(nameinfo_response_t));
            assert(q->type == REQUEST_NAMEINFO);

            q->ret = ni_resp->ret;
            q->_errno = ni_resp->_errno;
            q->_h_errno = ni_resp->_h_errno;

            if (ni_resp->hostlen)
                if (!(q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1)))
                    q->ret = EAI_MEMORY;

            if (ni_resp->servlen)
                if (!(q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1)))
                    q->ret = EAI_MEMORY;

            complete_query(asyncns, q);
            break;
        }

        case RESPONSE_RES: {
            const res_response_t *res_resp = (res_response_t *)resp;

            assert(length >= sizeof(res_response_t));
            assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);

            q->ret = res_resp->ret;
            q->_errno = res_resp->_errno;
            q->_h_errno = res_resp->_h_errno;

            if (res_resp->ret >= 0)  {
                if (!(q->serv = malloc(res_resp->ret))) {
                    q->ret = -1;
                    q->_errno = ENOMEM;
                } else
                    memcpy(q->serv, (char *)resp + sizeof(res_response_t), res_resp->ret);
            }

            complete_query(asyncns, q);
            break;
        }

        default:
            ;
    }

    return 0;
}

int asyncns_wait(asyncns_t *asyncns, int block) {
    int handled = 0;
    assert(asyncns);

    for (;;) {
        rheader_t buf[BUFSIZE/sizeof(rheader_t) + 1];
        ssize_t l;

        if (asyncns->dead) {
            errno = ECHILD;
            return -1;
        }

        if (((l = recv(asyncns->fds[RESPONSE_RECV_FD], buf, sizeof(buf), 0)) < 0)) {
            fd_set fds;

            if (errno != EAGAIN)
                return -1;

            if (!block || handled)
                return 0;

            FD_ZERO(&fds);
            FD_SET(asyncns->fds[RESPONSE_RECV_FD], &fds);

            if (select(asyncns->fds[RESPONSE_RECV_FD]+1, &fds, NULL, NULL, NULL) < 0)
                return -1;

            continue;
        }

        if (handle_response(asyncns, buf, (size_t) l) < 0)
            return -1;

        handled = 1;
    }
}

static asyncns_query_t *alloc_query(asyncns_t *asyncns) {
    asyncns_query_t *q;
    assert(asyncns);

    if (asyncns->n_queries >= MAX_QUERIES) {
        errno = ENOMEM;
        return NULL;
    }

    while (asyncns->queries[asyncns->current_index]) {

        asyncns->current_index++;
        asyncns->current_id++;

        while (asyncns->current_index >= MAX_QUERIES)
            asyncns->current_index -= MAX_QUERIES;
    }

    if (!(q = asyncns->queries[asyncns->current_index] = malloc(sizeof(asyncns_query_t)))) {
        errno = ENOMEM;
        return NULL;
    }

    asyncns->n_queries++;

    q->asyncns = asyncns;
    q->done = 0;
    q->id = asyncns->current_id;
    q->done_next = q->done_prev = NULL;
    q->ret = 0;
    q->_errno = 0;
    q->_h_errno = 0;
    q->addrinfo = NULL;
    q->userdata = NULL;
    q->host = q->serv = NULL;

    return q;
}

asyncns_query_t* asyncns_getaddrinfo(asyncns_t *asyncns, const char *node, const char *service, const struct addrinfo *hints) {
    addrinfo_request_t data[BUFSIZE/sizeof(addrinfo_request_t) + 1];
    addrinfo_request_t *req = data;
    asyncns_query_t *q;
    assert(asyncns);
    assert(node || service);

    if (asyncns->dead) {
        errno = ECHILD;
        return NULL;
    }

    if (!(q = alloc_query(asyncns)))
        return NULL;

    memset(req, 0, sizeof(addrinfo_request_t));

    req->node_len = node ? strlen(node)+1 : 0;
    req->service_len = service ? strlen(service)+1 : 0;

    req->header.id = q->id;
    req->header.type = q->type = REQUEST_ADDRINFO;
    req->header.length = sizeof(addrinfo_request_t) + req->node_len + req->service_len;

    if (req->header.length > BUFSIZE) {
        errno = ENOMEM;
        goto fail;
    }

    if (!(req->hints_is_null = !hints)) {
        req->ai_flags = hints->ai_flags;
        req->ai_family = hints->ai_family;
        req->ai_socktype = hints->ai_socktype;
        req->ai_protocol = hints->ai_protocol;
    }

    if (node)
        strcpy((char*) req + sizeof(addrinfo_request_t), node);

    if (service)
        strcpy((char*) req + sizeof(addrinfo_request_t) + req->node_len, service);

    if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
        goto fail;

    return q;

fail:
    if (q)
        asyncns_cancel(asyncns, q);

    return NULL;
}

int asyncns_getaddrinfo_done(asyncns_t *asyncns, asyncns_query_t* q, struct addrinfo **ret_res) {
    int ret;
    assert(asyncns);
    assert(q);
    assert(q->asyncns == asyncns);
    assert(q->type == REQUEST_ADDRINFO);

    if (asyncns->dead) {
        errno = ECHILD;
        return EAI_SYSTEM;
    }

    if (!q->done)
        return EAI_AGAIN;

    *ret_res = q->addrinfo;
    q->addrinfo = NULL;

    ret = q->ret;

    if (ret == EAI_SYSTEM)
        errno = q->_errno;

    if (ret != 0)
        h_errno = q->_h_errno;

    asyncns_cancel(asyncns, q);

    return ret;
}

asyncns_query_t* asyncns_getnameinfo(asyncns_t *asyncns, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv) {
    nameinfo_request_t data[BUFSIZE/sizeof(nameinfo_request_t) + 1];
    nameinfo_request_t *req = data;
    asyncns_query_t *q;

    assert(asyncns);
    assert(sa);
    assert(salen > 0);

    if (asyncns->dead) {
        errno = ECHILD;
        return NULL;
    }

    if (!(q = alloc_query(asyncns)))
        return NULL;

    memset(req, 0, sizeof(nameinfo_request_t));

    req->header.id = q->id;
    req->header.type = q->type = REQUEST_NAMEINFO;
    req->header.length = sizeof(nameinfo_request_t) + salen;

    if (req->header.length > BUFSIZE) {
        errno = ENOMEM;
        goto fail;
    }

    req->flags = flags;
    req->sockaddr_len = salen;
    req->gethost = gethost;
    req->getserv = getserv;

    memcpy((uint8_t*) req + sizeof(nameinfo_request_t), sa, salen);

    if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
        goto fail;

    return q;

fail:
    if (q)
        asyncns_cancel(asyncns, q);

    return NULL;
}

int asyncns_getnameinfo_done(asyncns_t *asyncns, asyncns_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen) {
    int ret;
    assert(asyncns);
    assert(q);
    assert(q->asyncns == asyncns);
    assert(q->type == REQUEST_NAMEINFO);
    assert(!ret_host || hostlen);
    assert(!ret_serv || servlen);

    if (asyncns->dead) {
        errno = ECHILD;
        return EAI_SYSTEM;
    }

    if (!q->done)
        return EAI_AGAIN;

    if (ret_host && q->host) {
        strncpy(ret_host, q->host, hostlen);
        ret_host[hostlen-1] = 0;
    }

    if (ret_serv && q->serv) {
        strncpy(ret_serv, q->serv, servlen);
        ret_serv[servlen-1] = 0;
    }

    ret = q->ret;

    if (ret == EAI_SYSTEM)
        errno = q->_errno;

    if (ret != 0)
        h_errno = q->_h_errno;

    asyncns_cancel(asyncns, q);

    return ret;
}

static asyncns_query_t * asyncns_res(asyncns_t *asyncns, query_type_t qtype, const char *dname, int class, int type) {
    res_request_t data[BUFSIZE/sizeof(res_request_t) + 1];
    res_request_t *req = data;
    asyncns_query_t *q;

    assert(asyncns);
    assert(dname);

    if (asyncns->dead) {
        errno = ECHILD;
        return NULL;
    }

    if (!(q = alloc_query(asyncns)))
        return NULL;

    memset(req, 0, sizeof(res_request_t));

    req->dname_len = strlen(dname) + 1;

    req->header.id = q->id;
    req->header.type = q->type = qtype;
    req->header.length = sizeof(res_request_t) + req->dname_len;

    if (req->header.length > BUFSIZE) {
        errno = ENOMEM;
        goto fail;
    }

    req->class = class;
    req->type = type;

    strcpy((char*) req + sizeof(res_request_t), dname);

    if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
        goto fail;

    return q;

fail:
    if (q)
        asyncns_cancel(asyncns, q);

    return NULL;
}

asyncns_query_t* asyncns_res_query(asyncns_t *asyncns, const char *dname, int class, int type) {
    return asyncns_res(asyncns, REQUEST_RES_QUERY, dname, class, type);
}

asyncns_query_t* asyncns_res_search(asyncns_t *asyncns, const char *dname, int class, int type) {
    return asyncns_res(asyncns, REQUEST_RES_SEARCH, dname, class, type);
}

int asyncns_res_done(asyncns_t *asyncns, asyncns_query_t* q, unsigned char **answer) {
    int ret;
    assert(asyncns);
    assert(q);
    assert(q->asyncns == asyncns);
    assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
    assert(answer);

    if (asyncns->dead) {
        errno = ECHILD;
        return -ECHILD;
    }

    if (!q->done) {
        errno = EAGAIN;
        return -EAGAIN;
    }

    *answer = (unsigned char *)q->serv;
    q->serv = NULL;

    ret = q->ret;

    if (ret < 0) {
        errno = q->_errno;
        h_errno = q->_h_errno;
    }

    asyncns_cancel(asyncns, q);

    return ret < 0 ? -errno : ret;
}

asyncns_query_t* asyncns_getnext(asyncns_t *asyncns) {
    assert(asyncns);
    return asyncns->done_head;
}

int asyncns_getnqueries(asyncns_t *asyncns) {
    assert(asyncns);
    return asyncns->n_queries;
}

void asyncns_cancel(asyncns_t *asyncns, asyncns_query_t* q) {
    int i;
    int saved_errno = errno;

    assert(asyncns);
    assert(q);
    assert(q->asyncns == asyncns);
    assert(asyncns->n_queries > 0);

    if (q->done) {

        if (q->done_prev)
            q->done_prev->done_next = q->done_next;
        else
            asyncns->done_head = q->done_next;

        if (q->done_next)
            q->done_next->done_prev = q->done_prev;
        else
            asyncns->done_tail = q->done_prev;
    }

    i = q->id % MAX_QUERIES;
    assert(asyncns->queries[i] == q);
    asyncns->queries[i] = NULL;

    asyncns_freeaddrinfo(q->addrinfo);
    free(q->host);
    free(q->serv);

    asyncns->n_queries--;
    free(q);

    errno = saved_errno;
}

void asyncns_freeaddrinfo(struct addrinfo *ai) {
    int saved_errno = errno;

    while (ai) {
        struct addrinfo *next = ai->ai_next;

        free(ai->ai_addr);
        free(ai->ai_canonname);
        free(ai);

        ai = next;
    }

    errno = saved_errno;
}

void asyncns_freeanswer(unsigned char *answer) {
    int saved_errno = errno;

    if (!answer)
        return;

    /* Please note that this function is new in libasyncns 0.4. In
     * older versions you were supposed to free the answer directly
     * with free(). Hence, if this function is changed to do more than
     * just a simple free() this must be considered ABI/API breakage! */

    free(answer);

    errno = saved_errno;
}

int asyncns_isdone(asyncns_t *asyncns, asyncns_query_t*q) {
    assert(asyncns);
    assert(q);
    assert(q->asyncns == asyncns);

    return q->done;
}

void asyncns_setuserdata(asyncns_t *asyncns, asyncns_query_t *q, void *userdata) {
    assert(q);
    assert(asyncns);
    assert(q->asyncns = asyncns);

    q->userdata = userdata;
}

void* asyncns_getuserdata(asyncns_t *asyncns, asyncns_query_t *q) {
    assert(q);
    assert(asyncns);
    assert(q->asyncns = asyncns);

    return q->userdata;
}