2424 * POSSIBILITY OF SUCH DAMAGE.
2525 */
2626
27- #ifndef _GNU_SOURCE
28- #define _GNU_SOURCE
29- #endif
27+ #define _DEFAULT_SOURCE
3028
3129#include <sys/types.h>
3230#include <sys/socket.h>
4139#include <errno.h>
4240#include <stdarg.h>
4341#include <stdio.h>
44- #include <limits.h>
4542#include <stdlib.h>
4643#include <string.h>
4744#include <strings.h>
6865/* ---- portability ---- */
6966
7067#ifndef __dead
71- #if defined(__OpenBSD__ ) || defined(__FreeBSD__ ) || defined(__NetBSD__ ) || \
72- defined(__DragonFly__ )
73- /* provided by <sys/cdefs.h> */
74- #else
7568#define __dead __attribute__((__noreturn__))
7669#endif
77- #endif
7870
79- #ifndef HAVE_STRLCPY
80- #if defined(__OpenBSD__ ) || defined(__FreeBSD__ ) || defined(__APPLE__ ) || \
81- defined(__NetBSD__ ) || defined(__DragonFly__ ) || \
82- (defined(__GLIBC__ ) && (__GLIBC__ > 2 || \
83- (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 38 ))) || \
84- (defined(__linux__ ) && !defined(__GLIBC__ )) /* musl */
85- #define HAVE_STRLCPY
86- #endif
87- #endif
88-
89- #ifndef HAVE_CLOSEFROM
90- #if defined(__OpenBSD__ ) || defined(__FreeBSD__ ) || defined(__NetBSD__ ) || \
91- defined(__APPLE__ ) || \
92- (defined(__GLIBC__ ) && (__GLIBC__ > 2 || \
93- (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 34 )))
94- #define HAVE_CLOSEFROM
95- #endif
96- #endif
97-
98- #ifndef HAVE_STRTONUM
99- #if defined(__OpenBSD__ ) || defined(__FreeBSD__ ) || defined(__NetBSD__ ) || \
100- defined(__DragonFly__ ) || \
101- (defined(__GLIBC__ ) && (__GLIBC__ > 2 || \
102- (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 39 )))
103- #define HAVE_STRTONUM
104- #endif
105- #endif
106-
107- #ifndef HAVE_STRLCPY
108- static size_t
109- strlcpy (char * dst , const char * src , size_t dstsize )
110- {
111- size_t n ;
112-
113- for (n = 0 ; n + 1 < dstsize && src [n ] != '\0' ; n ++ )
114- dst [n ] = src [n ];
115- if (dstsize > 0 )
116- dst [n ] = '\0' ;
117- while (src [n ] != '\0' )
118- n ++ ;
119- return n ;
120- }
121- #endif
122-
123- #ifndef HAVE_CLOSEFROM
71+ /*
72+ * On non-OpenBSD, provide inline wrappers that use POSIX
73+ * equivalents instead of BSD functions, avoiding compat
74+ * function conflicts with varying system headers.
75+ */
76+ #ifndef __OpenBSD__
77+ #define strlcpy (d , s , n ) (size_t)snprintf((d), (n), "%s", (s))
12478static void
125- closefrom (int lowfd )
79+ closefrom_compat (int lowfd )
12680{
127- int fd , maxfd ;
128-
129- maxfd = (int )sysconf (_SC_OPEN_MAX );
130- if (maxfd < 0 )
131- maxfd = 256 ;
132- for (fd = lowfd ; fd < maxfd ; fd ++ )
81+ int fd ;
82+ for (fd = lowfd ; fd < MAX_FDS ; fd ++ )
13383 (void )close (fd );
13484}
135- #endif
136-
137- #ifndef HAVE_STRTONUM
85+ #define closefrom closefrom_compat
13886static long long
139- strtonum (const char * numstr , long long minval , long long maxval ,
87+ strtonum_compat (const char * numstr , long long minval , long long maxval ,
14088 const char * * errstrp )
14189{
14290 long long ll ;
@@ -146,16 +94,17 @@ strtonum(const char *numstr, long long minval, long long maxval,
14694 ll = strtoll (numstr , & ep , 10 );
14795 if (numstr == ep || * ep != '\0' )
14896 * errstrp = "invalid" ;
149- else if (( ll == LLONG_MIN && errno == ERANGE ) || ll < minval )
97+ else if (ll < minval )
15098 * errstrp = "too small" ;
151- else if (( ll == LLONG_MAX && errno == ERANGE ) || ll > maxval )
99+ else if (ll > maxval )
152100 * errstrp = "too large" ;
153101 else {
154102 * errstrp = NULL ;
155103 return ll ;
156104 }
157105 return 0 ;
158106}
107+ #define strtonum strtonum_compat
159108#endif
160109
161110#define ERR_400 "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n"
@@ -1574,11 +1523,7 @@ accept_conn(int lfd)
15741523 struct conn * c ;
15751524
15761525 sl = sizeof (ss );
1577- #ifdef SOCK_NONBLOCK
1578- fd = accept4 (lfd , (struct sockaddr * )& ss , & sl , SOCK_NONBLOCK );
1579- #else
15801526 fd = accept (lfd , (struct sockaddr * )& ss , & sl );
1581- #endif
15821527 if (fd == -1 ) {
15831528 if (errno == EMFILE || errno == ENFILE ) {
15841529 logmsg (LOG_ERR , "accept: %s" , strerror (errno ));
@@ -1610,12 +1555,10 @@ accept_conn(int lfd)
16101555 return ;
16111556 }
16121557
1613- #ifndef SOCK_NONBLOCK
16141558 if (set_nonblock (fd ) == -1 ) {
16151559 close (fd );
16161560 return ;
16171561 }
1618- #endif
16191562
16201563 on = 1 ;
16211564 (void )setsockopt (fd , SOL_SOCKET , SO_KEEPALIVE , & on , sizeof (on ));
0 commit comments