]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/mDNSUNP.c
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
24 Change History (most recent first):
27 Revision 1.26 2005/04/08 21:43:59 ksekar
28 <rdar://problem/4083426> mDNSPosix (v98) retrieve interface list bug on AMD64 architecture
29 Submitted by Andrew de Quincey
31 Revision 1.25 2005/04/08 21:37:57 ksekar
32 <rdar://problem/3792767> get_ifi_info doesn't return IPv6 interfaces on Linux
34 Revision 1.24 2005/04/08 21:30:16 ksekar
35 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
36 Patch submitted by Bernd Kuhls
38 Revision 1.23 2004/12/01 04:25:05 cheshire
39 <rdar://problem/3872803> Darwin patches for Solaris and Suse
40 Provide daemon() for platforms that don't have it
42 Revision 1.22 2004/11/30 22:37:01 cheshire
43 Update copyright dates and add "Mode: C; tab-width: 4" headers
45 Revision 1.21 2004/11/08 22:13:59 rpantos
46 Create sockf6 lazily when v6 interface found.
48 Revision 1.20 2004/10/16 00:17:01 cheshire
49 <rdar://problem/3770558> Replace IP TTL 255 check with local subnet source address check
51 Revision 1.19 2004/07/20 01:47:36 rpantos
52 NOT_HAVE_SA_LEN applies to v6, too. And use more-portable s6_addr.
54 Revision 1.18 2004/07/08 21:30:21 rpantos
56 Revision 1.17 2004/06/25 00:26:27 rpantos
57 Changes to fix the Posix build on Solaris.
59 Revision 1.16 2004/03/20 05:37:09 cheshire
60 Fix contributed by Terry Lambert & Alfred Perlstein:
61 Don't use uint8_t -- it requires stdint.h, which doesn't exist on FreeBSD 4.x
63 Revision 1.15 2004/02/14 01:09:45 rpantos
64 Just use HAVE_IPV6 rather than defined(HAVE_IPV6).
66 Revision 1.14 2003/12/11 18:53:40 cheshire
67 Fix compiler warning reported by Paul Guyot
69 Revision 1.13 2003/12/08 20:47:02 rpantos
70 Add support for mDNSResponder on Linux.
72 Revision 1.12 2003/09/02 20:47:13 cheshire
73 Fix signed/unsigned warning
75 Revision 1.11 2003/08/12 19:56:26 cheshire
78 Revision 1.10 2003/08/06 18:20:51 cheshire
81 Revision 1.9 2003/07/14 18:11:54 cheshire
82 Fix stricter compiler warnings
84 Revision 1.8 2003/07/02 21:19:59 cheshire
85 <rdar://problem/3313413> Update copyright notices, etc., in source code comments
87 Revision 1.7 2003/03/20 21:10:31 cheshire
88 Fixes done at IETF 56 to make mDNSProxyResponderPosix run on Solaris
90 Revision 1.6 2003/03/13 03:46:21 cheshire
91 Fixes to make the code build on Linux
93 Revision 1.5 2003/02/07 03:02:02 cheshire
94 Submitted by: Mitsutaka Watanabe
95 The code saying "index += 1;" was effectively making up random interface index values.
96 The right way to find the correct interface index is if_nametoindex();
98 Revision 1.4 2002/12/23 22:13:31 jgraessl
100 Reviewed by: Stuart Cheshire
101 Initial IPv6 support for mDNSResponder.
103 Revision 1.3 2002/09/21 20:44:53 zarzycki
106 Revision 1.2 2002/09/19 04:20:44 cheshire
107 Remove high-ascii characters that confuse some systems
109 Revision 1.1 2002/09/17 06:24:34 cheshire
121 #include <sys/ioctl.h>
125 /* Solaris defined SIOCGIFCONF etc in <sys/sockio.h> but
126 other platforms don't even have that include file. So,
127 if we haven't yet got a definition, let's try to find
132 #include <sys/sockio.h>
135 /* sockaddr_dl is only referenced if we're using IP_RECVIF,
136 so only include the header in that case.
140 #include <net/if_dl.h>
143 #if defined(AF_INET6) && HAVE_IPV6 && !HAVE_LINUX
144 #include <netinet6/in6_var.h>
147 #if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
149 #include <arpa/inet.h>
151 /* Converts a prefix length to IPv6 network mask */
152 void plen_to_mask(int plen
, char *addr
) {
154 int colons
=7; /* Number of colons in IPv6 address */
155 int bits_in_block
=16; /* Bits per IPv6 block */
156 for(i
=0;i
<=colons
;i
++) {
157 int block
, ones
=0xffff, ones_in_block
;
158 if(plen
>bits_in_block
) ones_in_block
=bits_in_block
;
159 else ones_in_block
=plen
;
160 block
= ones
& (ones
<< (bits_in_block
-ones_in_block
));
161 i
==0 ? sprintf(addr
, "%x", block
) :
162 sprintf(addr
, "%s:%x", addr
, block
);
163 plen
-= ones_in_block
;
167 /* Gets IPv6 interface information from the /proc filesystem in linux*/
168 struct ifi_info
*get_ifi_info_linuxv6(int family
, int doaliases
)
170 struct ifi_info
*ifi
, *ifihead
, **ifipnext
;
173 int flags
, myflags
, index
, plen
, scope
;
174 char ifname
[8], lastname
[IFNAMSIZ
];
176 struct addrinfo hints
, *res0
;
177 struct sockaddr_in6
*sin6
;
178 struct in6_addr
*addrptr
;
186 if ((fp
= fopen(PROC_IFINET6_PATH
, "r")) != NULL
) {
188 "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %8s\n",
189 addr
[0],addr
[1],addr
[2],addr
[3],
190 addr
[4],addr
[5],addr
[6],addr
[7],
191 &index
, &plen
, &scope
, &flags
, ifname
) != EOF
) {
194 if (strncmp(lastname
, ifname
, IFNAMSIZ
) == 0) {
196 continue; /* already processed this interface */
199 memcpy(lastname
, ifname
, IFNAMSIZ
);
200 ifi
= (struct ifi_info
*)calloc(1, sizeof(struct ifi_info
));
205 *ifipnext
= ifi
; /* prev points to this new one */
206 ifipnext
= &ifi
->ifi_next
; /* pointer to next one goes here */
208 sprintf(addr6
, "%s:%s:%s:%s:%s:%s:%s:%s",
209 addr
[0],addr
[1],addr
[2],addr
[3],
210 addr
[4],addr
[5],addr
[6],addr
[7]);
212 /* Add address of the interface */
213 memset(&hints
, 0, sizeof(hints
));
214 hints
.ai_family
= AF_INET6
;
215 hints
.ai_flags
= AI_NUMERICHOST
;
216 err
= getaddrinfo(addr6
, NULL
, &hints
, &res0
);
220 ifi
->ifi_addr
= calloc(1, sizeof(struct sockaddr_in6
));
221 if (ifi
->ifi_addr
== NULL
) {
224 memcpy(ifi
->ifi_addr
, res0
->ai_addr
, sizeof(struct sockaddr_in6
));
226 /* Add netmask of the interface */
227 char ipv6addr
[INET6_ADDRSTRLEN
];
228 plen_to_mask(plen
, ipv6addr
);
229 ifi
->ifi_netmask
= calloc(1, sizeof(struct sockaddr_in6
));
230 if (ifi
->ifi_addr
== NULL
) {
233 sin6
=calloc(1, sizeof(struct sockaddr_in6
));
234 addrptr
=calloc(1, sizeof(struct in6_addr
));
235 inet_pton(family
, ipv6addr
, addrptr
);
236 sin6
->sin6_family
=family
;
237 sin6
->sin6_addr
=*addrptr
;
238 sin6
->sin6_scope_id
=scope
;
239 memcpy(ifi
->ifi_netmask
, sin6
, sizeof(struct sockaddr_in6
));
243 /* Add interface name */
244 memcpy(ifi
->ifi_name
, ifname
, IFI_NAME
);
246 /* Add interface index */
247 ifi
->ifi_index
= index
;
249 /* If interface is in /proc then it is up*/
250 ifi
->ifi_flags
= IFF_UP
;
259 if (ifihead
!= NULL
) {
260 free_ifi_info(ifihead
);
268 return(ifihead
); /* pointer to first structure in linked list */
270 #endif // defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
272 struct ifi_info
*get_ifi_info(int family
, int doaliases
)
275 struct ifi_info
*ifi
, *ifihead
, **ifipnext
;
276 int sockfd
, sockf6
, len
, lastlen
, flags
, myflags
;
277 #ifdef NOT_HAVE_IF_NAMETOINDEX
280 char *ptr
, *buf
, lastname
[IFNAMSIZ
], *cptr
;
282 struct ifreq
*ifr
, ifrcopy
;
283 struct sockaddr_in
*sinptr
;
285 #if defined(AF_INET6) && HAVE_IPV6
286 struct sockaddr_in6
*sinptr6
;
289 #if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
290 if(family
== AF_INET6
) return get_ifi_info_linuxv6(family
, doaliases
);
298 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
304 len
= 100 * sizeof(struct ifreq
); /* initial buffer size guess */
306 buf
= (char*)malloc(len
);
312 if (ioctl(sockfd
, SIOCGIFCONF
, &ifc
) < 0) {
313 if (errno
!= EINVAL
|| lastlen
!= 0) {
317 if (ifc
.ifc_len
== lastlen
)
318 break; /* success, len has not changed */
319 lastlen
= ifc
.ifc_len
;
321 len
+= 10 * sizeof(struct ifreq
); /* increment */
327 /* end get_ifi_info1 */
329 /* include get_ifi_info2 */
330 for (ptr
= buf
; ptr
< buf
+ ifc
.ifc_len
; ) {
331 ifr
= (struct ifreq
*) ptr
;
333 len
= GET_SA_LEN(ifr
->ifr_addr
);
334 ptr
+= sizeof(struct ifreq
); /* for next one in buffer */
336 // fprintf(stderr, "intf %d name=%s AF=%d\n", index, ifr->ifr_name, ifr->ifr_addr.sa_family);
338 if (ifr
->ifr_addr
.sa_family
!= family
)
339 continue; /* ignore if not desired address family */
342 if ( (cptr
= strchr(ifr
->ifr_name
, ':')) != NULL
)
343 *cptr
= 0; /* replace colon will null */
344 if (strncmp(lastname
, ifr
->ifr_name
, IFNAMSIZ
) == 0) {
346 continue; /* already processed this interface */
349 memcpy(lastname
, ifr
->ifr_name
, IFNAMSIZ
);
352 if (ioctl(sockfd
, SIOCGIFFLAGS
, &ifrcopy
) < 0) {
356 flags
= ifrcopy
.ifr_flags
;
357 if ((flags
& IFF_UP
) == 0)
358 continue; /* ignore if interface not up */
360 ifi
= (struct ifi_info
*)calloc(1, sizeof(struct ifi_info
));
364 *ifipnext
= ifi
; /* prev points to this new one */
365 ifipnext
= &ifi
->ifi_next
; /* pointer to next one goes here */
367 ifi
->ifi_flags
= flags
; /* IFF_xxx values */
368 ifi
->ifi_myflags
= myflags
; /* IFI_xxx values */
369 #ifndef NOT_HAVE_IF_NAMETOINDEX
370 ifi
->ifi_index
= if_nametoindex(ifr
->ifr_name
);
373 if ( 0 >= ioctl(sockfd
, SIOCGIFINDEX
, &ifrcopy
))
374 ifi
->ifi_index
= ifrcopy
.ifr_index
;
376 ifi
->ifi_index
= index
++; /* SIOCGIFINDEX is broken on Solaris 2.5ish, so fake it */
378 memcpy(ifi
->ifi_name
, ifr
->ifr_name
, IFI_NAME
);
379 ifi
->ifi_name
[IFI_NAME
-1] = '\0';
380 /* end get_ifi_info2 */
381 /* include get_ifi_info3 */
382 switch (ifr
->ifr_addr
.sa_family
) {
384 sinptr
= (struct sockaddr_in
*) &ifr
->ifr_addr
;
385 if (ifi
->ifi_addr
== NULL
) {
386 ifi
->ifi_addr
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
387 if (ifi
->ifi_addr
== NULL
) {
390 memcpy(ifi
->ifi_addr
, sinptr
, sizeof(struct sockaddr_in
));
392 #ifdef SIOCGIFNETMASK
393 if (ioctl(sockfd
, SIOCGIFNETMASK
, &ifrcopy
) < 0) goto gotError
;
394 ifi
->ifi_netmask
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
395 if (ifi
->ifi_netmask
== NULL
) goto gotError
;
396 sinptr
= (struct sockaddr_in
*) &ifrcopy
.ifr_addr
;
397 memcpy(ifi
->ifi_netmask
, sinptr
, sizeof(struct sockaddr_in
));
400 #ifdef SIOCGIFBRDADDR
401 if (flags
& IFF_BROADCAST
) {
402 if (ioctl(sockfd
, SIOCGIFBRDADDR
, &ifrcopy
) < 0) {
405 sinptr
= (struct sockaddr_in
*) &ifrcopy
.ifr_broadaddr
;
406 ifi
->ifi_brdaddr
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
407 if (ifi
->ifi_brdaddr
== NULL
) {
410 memcpy(ifi
->ifi_brdaddr
, sinptr
, sizeof(struct sockaddr_in
));
414 #ifdef SIOCGIFDSTADDR
415 if (flags
& IFF_POINTOPOINT
) {
416 if (ioctl(sockfd
, SIOCGIFDSTADDR
, &ifrcopy
) < 0) {
419 sinptr
= (struct sockaddr_in
*) &ifrcopy
.ifr_dstaddr
;
420 ifi
->ifi_dstaddr
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
421 if (ifi
->ifi_dstaddr
== NULL
) {
424 memcpy(ifi
->ifi_dstaddr
, sinptr
, sizeof(struct sockaddr_in
));
430 #if defined(AF_INET6) && HAVE_IPV6
432 sinptr6
= (struct sockaddr_in6
*) &ifr
->ifr_addr
;
433 if (ifi
->ifi_addr
== NULL
) {
434 ifi
->ifi_addr
= calloc(1, sizeof(struct sockaddr_in6
));
435 if (ifi
->ifi_addr
== NULL
) {
439 /* Some platforms (*BSD) inject the prefix in IPv6LL addresses */
440 /* We need to strip that out */
441 if (IN6_IS_ADDR_LINKLOCAL(&sinptr6
->sin6_addr
))
442 sinptr6
->sin6_addr
.s6_addr
[2] = sinptr6
->sin6_addr
.s6_addr
[3] = 0;
443 memcpy(ifi
->ifi_addr
, sinptr6
, sizeof(struct sockaddr_in6
));
445 #ifdef SIOCGIFNETMASK_IN6
447 struct in6_ifreq ifr6
;
449 sockf6
= socket(AF_INET6
, SOCK_DGRAM
, 0);
450 bzero(&ifr6
, sizeof(ifr6
));
451 memcpy(&ifr6
.ifr_name
, &ifr
->ifr_name
, sizeof(ifr6
.ifr_name
));
452 memcpy(&ifr6
.ifr_ifru
.ifru_addr
, &ifr
->ifr_addr
, sizeof(ifr6
.ifr_ifru
.ifru_addr
));
453 if (ioctl(sockf6
, SIOCGIFNETMASK_IN6
, &ifr6
) < 0) goto gotError
;
454 ifi
->ifi_netmask
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in6
));
455 if (ifi
->ifi_netmask
== NULL
) goto gotError
;
456 sinptr6
= (struct sockaddr_in6
*) &ifr6
.ifr_ifru
.ifru_addr
;
457 memcpy(ifi
->ifi_netmask
, sinptr6
, sizeof(struct sockaddr_in6
));
471 if (ifihead
!= NULL
) {
472 free_ifi_info(ifihead
);
481 junk
= close(sockfd
);
485 junk
= close(sockf6
);
488 return(ifihead
); /* pointer to first structure in linked list */
490 /* end get_ifi_info3 */
492 /* include free_ifi_info */
494 free_ifi_info(struct ifi_info
*ifihead
)
496 struct ifi_info
*ifi
, *ifinext
;
498 for (ifi
= ifihead
; ifi
!= NULL
; ifi
= ifinext
) {
499 if (ifi
->ifi_addr
!= NULL
)
501 if (ifi
->ifi_brdaddr
!= NULL
)
502 free(ifi
->ifi_brdaddr
);
503 if (ifi
->ifi_dstaddr
!= NULL
)
504 free(ifi
->ifi_dstaddr
);
505 ifinext
= ifi
->ifi_next
; /* can't fetch ifi_next after free() */
506 free(ifi
); /* the ifi_info{} itself */
509 /* end free_ifi_info */
512 recvfrom_flags(int fd
, void *ptr
, size_t nbytes
, int *flagsp
,
513 struct sockaddr
*sa
, socklen_t
*salenptr
, struct my_in_pktinfo
*pktp
, u_char
*ttl
)
520 struct cmsghdr
*cmptr
;
526 *ttl
= 255; // If kernel fails to provide TTL data then assume the TTL was 255 as it should be
528 msg
.msg_control
= control_un
.control
;
529 msg
.msg_controllen
= sizeof(control_un
.control
);
532 memset(&msg
, 0, sizeof(msg
)); /* make certain msg_accrightslen = 0 */
533 #endif /* CMSG_FIRSTHDR */
535 msg
.msg_name
= (char *) sa
;
536 msg
.msg_namelen
= *salenptr
;
537 iov
[0].iov_base
= (char *)ptr
;
538 iov
[0].iov_len
= nbytes
;
542 if ( (n
= recvmsg(fd
, &msg
, *flagsp
)) < 0)
545 *salenptr
= msg
.msg_namelen
; /* pass back results */
547 /* 0.0.0.0, i/f = -1 */
548 /* We set the interface to -1 so that the caller can
549 tell whether we returned a meaningful value or
550 just some default. Previously this code just
551 set the value to 0, but I'm concerned that 0
552 might be a valid interface value.
554 memset(pktp
, 0, sizeof(struct my_in_pktinfo
));
555 pktp
->ipi_ifindex
= -1;
557 /* end recvfrom_flags1 */
559 /* include recvfrom_flags2 */
560 #ifndef CMSG_FIRSTHDR
561 #warning CMSG_FIRSTHDR not defined. Will not be able to determine destination address, received interface, etc.
562 *flagsp
= 0; /* pass back results */
566 *flagsp
= msg
.msg_flags
; /* pass back results */
567 if (msg
.msg_controllen
< (socklen_t
)sizeof(struct cmsghdr
) ||
568 (msg
.msg_flags
& MSG_CTRUNC
) || pktp
== NULL
)
571 for (cmptr
= CMSG_FIRSTHDR(&msg
); cmptr
!= NULL
;
572 cmptr
= CMSG_NXTHDR(&msg
, cmptr
)) {
575 #if in_pktinfo_definition_is_missing
579 struct in_addr ipi_spec_dst
;
580 struct in_addr ipi_addr
;
583 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
584 cmptr
->cmsg_type
== IP_PKTINFO
) {
585 struct in_pktinfo
*tmp
;
586 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&pktp
->ipi_addr
;
588 tmp
= (struct in_pktinfo
*) CMSG_DATA(cmptr
);
589 sin
->sin_family
= AF_INET
;
590 sin
->sin_addr
= tmp
->ipi_addr
;
592 pktp
->ipi_ifindex
= tmp
->ipi_ifindex
;
597 #ifdef IP_RECVDSTADDR
598 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
599 cmptr
->cmsg_type
== IP_RECVDSTADDR
) {
600 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&pktp
->ipi_addr
;
602 sin
->sin_family
= AF_INET
;
603 sin
->sin_addr
= *(struct in_addr
*)CMSG_DATA(cmptr
);
610 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
611 cmptr
->cmsg_type
== IP_RECVIF
) {
612 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*) CMSG_DATA(cmptr
);
613 #ifndef HAVE_BROKEN_RECVIF_NAME
614 int nameLen
= (sdl
->sdl_nlen
< IFI_NAME
- 1) ? sdl
->sdl_nlen
: (IFI_NAME
- 1);
615 strncpy(pktp
->ipi_ifname
, sdl
->sdl_data
, nameLen
);
617 pktp
->ipi_ifindex
= sdl
->sdl_index
;
618 assert(pktp
->ipi_ifname
[IFI_NAME
- 1] == 0);
619 // null terminated because of memset above
625 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
626 cmptr
->cmsg_type
== IP_RECVTTL
) {
627 *ttl
= *(u_char
*)CMSG_DATA(cmptr
);
630 else if (cmptr
->cmsg_level
== IPPROTO_IP
&&
631 cmptr
->cmsg_type
== IP_TTL
) { // some implementations seem to send IP_TTL instead of IP_RECVTTL
632 *ttl
= *(int*)CMSG_DATA(cmptr
);
637 #if defined(IPV6_PKTINFO) && HAVE_IPV6
638 if (cmptr
->cmsg_level
== IPPROTO_IPV6
&&
639 cmptr
->cmsg_type
== IPV6_PKTINFO
) {
640 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&pktp
->ipi_addr
;
641 struct in6_pktinfo
*ip6_info
= (struct in6_pktinfo
*)CMSG_DATA(cmptr
);
643 sin6
->sin6_family
= AF_INET6
;
644 #ifndef NOT_HAVE_SA_LEN
645 sin6
->sin6_len
= sizeof(*sin6
);
647 sin6
->sin6_addr
= ip6_info
->ipi6_addr
;
648 sin6
->sin6_flowinfo
= 0;
649 sin6
->sin6_scope_id
= 0;
651 pktp
->ipi_ifindex
= ip6_info
->ipi6_ifindex
;
656 #if defined(IPV6_HOPLIMIT) && HAVE_IPV6
657 if (cmptr
->cmsg_level
== IPPROTO_IPV6
&&
658 cmptr
->cmsg_type
== IPV6_HOPLIMIT
) {
659 *ttl
= *(int*)CMSG_DATA(cmptr
);
663 assert(0); // unknown ancillary data
666 #endif /* CMSG_FIRSTHDR */
669 // **********************************************************************************************
671 // daemonize the process. Adapted from "Unix Network Programming" vol 1 by Stevens, section 12.4.
672 // Returns 0 on success, -1 on failure.
674 #ifdef NOT_HAVE_DAEMON
676 #include <sys/stat.h>
679 int daemon(int nochdir
, int noclose
)
683 case -1: return (-1); // Fork failed
684 case 0: break; // Child -- continue
685 default: _exit(0); // Parent -- exit
688 if (setsid() == -1) return(-1);
690 signal(SIGHUP
, SIG_IGN
);
692 switch (fork()) // Fork again, primarily for reasons of Unix trivia
694 case -1: return (-1); // Fork failed
695 case 0: break; // Child -- continue
696 default: _exit(0); // Parent -- exit
699 if (!nochdir
) (void)chdir("/");
704 int fd
= open("/dev/null", O_RDWR
, 0);
707 // Avoid unnecessarily duplicating a file descriptor to itself
708 if (fd
!= STDIN_FILENO
) (void)dup2(fd
, STDIN_FILENO
);
709 if (fd
!= STDOUT_FILENO
) (void)dup2(fd
, STDOUT_FILENO
);
710 if (fd
!= STDERR_FILENO
) (void)dup2(fd
, STDERR_FILENO
);
711 if (fd
!= STDIN_FILENO
&& fd
!= STDOUT_FILENO
&& fd
!= STDERR_FILENO
)
717 #endif /* NOT_HAVE_DAEMON */