1/* -*- C -*- */ 2/* 3 * Copyright (c) 1995-2005 Kungliga Tekniska Högskolan 4 * (Royal Institute of Technology, Stockholm, Sweden). 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * 3. Neither the name of the Institute nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35#include <stdio.h> 36#include <stdlib.h> 37#include <stdarg.h> 38#ifdef HAVE_STDINT_H 39#include <stdint.h> 40#endif 41#include <string.h> 42#include <signal.h> 43 44#ifndef ROKEN_LIB_FUNCTION 45#ifdef _WIN32 46# define ROKEN_LIB_CALL __cdecl 47# ifdef ROKEN_LIB_DYNAMIC 48# define ROKEN_LIB_FUNCTION __declspec(dllimport) 49# define ROKEN_LIB_VARIABLE __declspec(dllimport) 50# else 51# define ROKEN_LIB_FUNCTION 52# define ROKEN_LIB_VARIABLE 53# endif 54#else 55#define ROKEN_LIB_FUNCTION 56#define ROKEN_LIB_CALL 57#define ROKEN_LIB_VARIABLE 58#endif 59#endif 60 61#ifdef HAVE_WINSOCK 62/* Declarations for Microsoft Windows */ 63 64#include <winsock2.h> 65#include <ws2tcpip.h> 66 67/* 68 * error codes for inet_ntop/inet_pton 69 */ 70#define EAFNOSUPPORT WSAEAFNOSUPPORT 71 72typedef SOCKET rk_socket_t; 73 74#define rk_closesocket(x) closesocket(x) 75#define rk_INVALID_SOCKET INVALID_SOCKET 76#define rk_IS_BAD_SOCKET(s) ((s) == INVALID_SOCKET) 77#define rk_IS_SOCKET_ERROR(rv) ((rv) == SOCKET_ERROR) 78#define rk_SOCK_ERRNO WSAGetLastError() 79 80ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_SOCK_IOCTL(SOCKET s, long cmd, int * argp); 81 82#define ETIMEDOUT WSAETIMEDOUT 83#define EWOULDBLOCK WSAEWOULDBLOCK 84#define ENOTSOCK WSAENOTSOCK 85 86#define rk_SOCK_INIT() rk_WSAStartup() 87#define rk_SOCK_EXIT() rk_WSACleanup() 88 89ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_WSAStartup(void); 90ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_WSACleanup(void); 91 92#else /* not WinSock */ 93 94typedef int rk_socket_t; 95 96#define rk_closesocket(x) close(x) 97#define rk_SOCK_IOCTL(s,c,a) ioctl((s),(c),(a)) 98#define rk_IS_BAD_SOCKET(s) ((s) < 0) 99#define rk_IS_SOCKET_ERROR(rv) ((rv) < 0) 100#define rk_SOCK_ERRNO errno 101#define rk_INVALID_SOCKET (-1) 102 103#define rk_SOCK_INIT() 0 104#define rk_SOCK_EXIT() do { } while(0) 105 106#endif 107 108#ifdef _MSC_VER 109/* Declarations for Microsoft Visual C runtime on Windows */ 110 111#include<process.h> 112 113#include<io.h> 114 115#ifndef __BIT_TYPES_DEFINED__ 116#define __BIT_TYPES_DEFINED__ 117 118typedef __int8 int8_t; 119typedef __int16 int16_t; 120typedef __int32 int32_t; 121typedef __int64 int64_t; 122typedef unsigned __int8 uint8_t; 123typedef unsigned __int16 uint16_t; 124typedef unsigned __int32 uint32_t; 125typedef unsigned __int64 uint64_t; 126typedef uint8_t u_int8_t; 127typedef uint16_t u_int16_t; 128typedef uint32_t u_int32_t; 129typedef uint64_t u_int64_t; 130 131#endif /* __BIT_TYPES_DEFINED__ */ 132 133#define UNREACHABLE(x) x 134#define UNUSED_ARGUMENT(x) ((void) x) 135 136#define RETSIGTYPE void 137 138#define VOID_RETSIGTYPE 1 139 140#ifdef VOID_RETSIGTYPE 141#define SIGRETURN(x) return 142#else 143#define SIGRETURN(x) return (RETSIGTYPE)(x) 144#endif 145 146#ifndef CPP_ONLY 147 148typedef int pid_t; 149 150typedef unsigned int gid_t; 151 152typedef unsigned int uid_t; 153 154typedef unsigned short mode_t; 155 156#endif 157 158#ifndef __cplusplus 159#define inline __inline 160#endif 161 162#else 163 164#define UNREACHABLE(x) 165#define UNUSED_ARGUMENT(x) 166 167#endif 168 169#ifdef _AIX 170struct ether_addr; 171struct sockaddr_dl; 172#endif 173#ifdef HAVE_SYS_PARAM_H 174#include <sys/param.h> 175#endif 176#ifdef HAVE_INTTYPES_H 177#include <inttypes.h> 178#endif 179#ifdef HAVE_SYS_TYPES_H 180#include <sys/types.h> 181#endif 182#ifdef HAVE_SYS_BITYPES_H 183#include <sys/bitypes.h> 184#endif 185#ifdef HAVE_BIND_BITYPES_H 186#include <bind/bitypes.h> 187#endif 188#ifdef HAVE_NETINET_IN6_MACHTYPES_H 189#include <netinet/in6_machtypes.h> 190#endif 191#ifdef HAVE_UNISTD_H 192#include <unistd.h> 193#endif 194#ifdef HAVE_SYS_SOCKET_H 195#include <sys/socket.h> 196#endif 197#ifdef HAVE_SYS_UIO_H 198#include <sys/uio.h> 199#endif 200#ifdef HAVE_GRP_H 201#include <grp.h> 202#endif 203#ifdef HAVE_SYS_STAT_H 204#include <sys/stat.h> 205#endif 206#ifdef HAVE_NETINET_IN_H 207#include <netinet/in.h> 208#endif 209#ifdef HAVE_NETINET_IN6_H 210#include <netinet/in6.h> 211#endif 212#ifdef HAVE_NETINET6_IN6_H 213#include <netinet6/in6.h> 214#endif 215#ifdef HAVE_ARPA_INET_H 216#include <arpa/inet.h> 217#endif 218#ifdef HAVE_NETDB_H 219#include <netdb.h> 220#endif 221#ifdef HAVE_ARPA_NAMESER_H 222#include <arpa/nameser.h> 223#endif 224#ifdef HAVE_RESOLV_H 225#include <resolv.h> 226#endif 227#ifdef HAVE_SYSLOG_H 228#include <syslog.h> 229#endif 230#ifdef HAVE_FCNTL_H 231#include <fcntl.h> 232#endif 233#ifdef HAVE_ERRNO_H 234#include <errno.h> 235#endif 236#include <err.h> 237#ifdef HAVE_TERMIOS_H 238#include <termios.h> 239#endif 240#ifdef HAVE_SYS_IOCTL_H 241#include <sys/ioctl.h> 242#endif 243#ifdef TIME_WITH_SYS_TIME 244#include <sys/time.h> 245#include <time.h> 246#elif defined(HAVE_SYS_TIME_H) 247#include <sys/time.h> 248#else 249#include <time.h> 250#endif 251 252#ifdef HAVE_PATHS_H 253#include <paths.h> 254#endif 255 256#ifdef HAVE_DIRENT_H 257#include <dirent.h> 258#endif 259 260#ifdef BACKSLASH_PATH_DELIM 261#define rk_PATH_DELIM '\\' 262#endif 263 264#ifndef HAVE_SSIZE_T 265#ifndef SSIZE_T_DEFINED 266#ifdef ssize_t 267#undef ssize_t 268#endif 269#ifdef _WIN64 270typedef __int64 ssize_t; 271#else 272typedef int ssize_t; 273#endif 274#define SSIZE_T_DEFINED 275#endif /* SSIZE_T_DEFINED */ 276#endif /* HAVE_SSIZE_T */ 277 278#include <roken-common.h> 279 280ROKEN_CPP_START 281 282#ifdef HAVE_UINTPTR_T 283#define rk_UNCONST(x) ((void *)(uintptr_t)(const void *)(x)) 284#else 285#define rk_UNCONST(x) ((void *)(unsigned long)(const void *)(x)) 286#endif 287 288#if !defined(HAVE_SETSID) && defined(HAVE__SETSID) 289#define setsid _setsid 290#endif 291 292#ifdef _MSC_VER 293/* Additional macros for Visual C/C++ runtime */ 294 295#define close _close 296 297#define getpid _getpid 298 299#define open _open 300 301#define chdir _chdir 302 303#define fsync _commit 304 305/* The MSVC implementation of snprintf is not C99 compliant. */ 306#define snprintf rk_snprintf 307#define vsnprintf rk_vsnprintf 308#define vasnprintf rk_vasnprintf 309#define vasprintf rk_vasprintf 310#define asnprintf rk_asnprintf 311#define asprintf rk_asprintf 312 313#define _PIPE_BUFFER_SZ 8192 314#define pipe(fds) _pipe((fds), _PIPE_BUFFER_SZ, O_BINARY); 315 316#define ftruncate(fd, sz) _chsize((fd), (sz)) 317 318ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 319rk_snprintf (char *str, size_t sz, const char *format, ...); 320 321ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 322rk_asprintf (char **ret, const char *format, ...); 323 324ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 325rk_asnprintf (char **ret, size_t max_sz, const char *format, ...); 326 327ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 328rk_vasprintf (char **ret, const char *format, va_list args); 329 330ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 331rk_vasnprintf (char **ret, size_t max_sz, const char *format, va_list args); 332 333ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 334rk_vsnprintf (char *str, size_t sz, const char *format, va_list args); 335 336/* missing stat.h predicates */ 337 338#define S_ISREG(m) (((m) & _S_IFREG) == _S_IFREG) 339 340#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR) 341 342#define S_ISCHR(m) (((m) & _S_IFCHR) == _S_IFCHR) 343 344#define S_ISFIFO(m) (((m) & _S_IFIFO) == _S_IFIFO) 345 346/* The following are not implemented: 347 348 S_ISLNK(m) 349 S_ISSOCK(m) 350 S_ISBLK(m) 351*/ 352 353#endif /* _MSC_VER */ 354 355#ifdef HAVE_WINSOCK 356 357/* While we are at it, define WinSock specific scatter gather socket 358 I/O. */ 359 360#define iovec _WSABUF 361#define iov_base buf 362#define iov_len len 363 364struct msghdr { 365 void *msg_name; 366 socklen_t msg_namelen; 367 struct iovec *msg_iov; 368 size_t msg_iovlen; 369 void *msg_control; 370 socklen_t msg_controllen; 371 int msg_flags; 372}; 373 374#define sendmsg sendmsg_w32 375 376ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 377sendmsg_w32(rk_socket_t s, const struct msghdr * msg, int flags); 378 379#endif /* HAVE_WINSOCK */ 380 381#ifndef HAVE_PUTENV 382#define putenv rk_putenv 383ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL putenv(const char *); 384#endif 385 386#if !defined(HAVE_SETENV) || defined(NEED_SETENV_PROTO) 387#ifndef HAVE_SETENV 388#define setenv rk_setenv 389#endif 390ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL setenv(const char *, const char *, int); 391#endif 392 393#if !defined(HAVE_UNSETENV) || defined(NEED_UNSETENV_PROTO) 394#ifndef HAVE_UNSETENV 395#define unsetenv rk_unsetenv 396#endif 397ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL unsetenv(const char *); 398#endif 399 400#if !defined(HAVE_GETUSERSHELL) || defined(NEED_GETUSERSHELL_PROTO) 401#ifndef HAVE_GETUSERSHELL 402#define getusershell rk_getusershell 403#define endusershell rk_endusershell 404#endif 405ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL getusershell(void); 406ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL endusershell(void); 407#endif 408 409#if !defined(HAVE_SNPRINTF) || defined(NEED_SNPRINTF_PROTO) 410#ifndef HAVE_SNPRINTF 411#define snprintf rk_snprintf 412#endif 413ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 414 rk_snprintf (char *, size_t, const char *, ...) 415 __attribute__ ((format (printf, 3, 4))); 416#endif 417 418#if !defined(HAVE_VSNPRINTF) || defined(NEED_VSNPRINTF_PROTO) 419#ifndef HAVE_VSNPRINTF 420#define vsnprintf rk_vsnprintf 421#endif 422ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 423 rk_vsnprintf (char *, size_t, const char *, va_list) 424 __attribute__((format (printf, 3, 0))); 425#endif 426 427#if !defined(HAVE_ASPRINTF) || defined(NEED_ASPRINTF_PROTO) 428#ifndef HAVE_ASPRINTF 429#define asprintf rk_asprintf 430#endif 431ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 432 rk_asprintf (char **, const char *, ...) 433 __attribute__ ((format (printf, 2, 3))); 434#endif 435 436#if !defined(HAVE_VASPRINTF) || defined(NEED_VASPRINTF_PROTO) 437#ifndef HAVE_VASPRINTF 438#define vasprintf rk_vasprintf 439#endif 440ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 441 rk_vasprintf (char **, const char *, va_list) 442 __attribute__((format (printf, 2, 0))); 443#endif 444 445#if !defined(HAVE_ASNPRINTF) || defined(NEED_ASNPRINTF_PROTO) 446#ifndef HAVE_ASNPRINTF 447#define asnprintf rk_asnprintf 448#endif 449ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 450 rk_asnprintf (char **, size_t, const char *, ...) 451 __attribute__ ((format (printf, 3, 4))); 452#endif 453 454#if !defined(HAVE_VASNPRINTF) || defined(NEED_VASNPRINTF_PROTO) 455#ifndef HAVE_VASNPRINTF 456#define vasnprintf rk_vasnprintf 457#endif 458ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 459 vasnprintf (char **, size_t, const char *, va_list) 460 __attribute__((format (printf, 3, 0))); 461#endif 462 463#ifndef HAVE_STRDUP 464#define strdup rk_strdup 465ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL strdup(const char *); 466#endif 467 468#if !defined(HAVE_STRNDUP) || defined(NEED_STRNDUP_PROTO) 469#ifndef HAVE_STRNDUP 470#define strndup rk_strndup 471#endif 472ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL strndup(const char *, size_t); 473#endif 474 475#ifndef HAVE_STRLWR 476#define strlwr rk_strlwr 477ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL strlwr(char *); 478#endif 479 480#ifndef HAVE_STRNLEN 481#define strnlen rk_strnlen 482ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL strnlen(const char*, size_t); 483#endif 484 485#if !defined(HAVE_STRSEP) || defined(NEED_STRSEP_PROTO) 486#ifndef HAVE_STRSEP 487#define strsep rk_strsep 488#endif 489ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL strsep(char**, const char*); 490#endif 491 492#if !defined(HAVE_STRSEP_COPY) || defined(NEED_STRSEP_COPY_PROTO) 493#ifndef HAVE_STRSEP_COPY 494#define strsep_copy rk_strsep_copy 495#endif 496ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL strsep_copy(const char**, const char*, char*, size_t); 497#endif 498 499#ifndef HAVE_STRCASECMP 500#define strcasecmp rk_strcasecmp 501ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL strcasecmp(const char *, const char *); 502#endif 503 504#ifdef NEED_FCLOSE_PROTO 505ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL fclose(FILE *); 506#endif 507 508#ifdef NEED_STRTOK_R_PROTO 509ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL strtok_r(char *, const char *, char **); 510#endif 511 512#ifndef HAVE_STRUPR 513#define strupr rk_strupr 514ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL strupr(char *); 515#endif 516 517#ifndef HAVE_STRLCPY 518#define strlcpy rk_strlcpy 519ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL strlcpy (char *, const char *, size_t); 520#endif 521 522#ifndef HAVE_STRLCAT 523#define strlcat rk_strlcat 524ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL strlcat (char *, const char *, size_t); 525#endif 526 527#ifndef HAVE_GETDTABLESIZE 528#define getdtablesize rk_getdtablesize 529ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL getdtablesize(void); 530#endif 531 532#if !defined(HAVE_STRERROR) && !defined(strerror) 533#define strerror rk_strerror 534ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL strerror(int); 535#endif 536 537#if (!defined(HAVE_STRERROR_R) && !defined(strerror_r)) || (!defined(STRERROR_R_PROTO_COMPATIBLE) && defined(HAVE_STRERROR_R)) 538int ROKEN_LIB_FUNCTION rk_strerror_r(int, char *, size_t); 539#else 540#define rk_strerror_r strerror_r 541#endif 542 543#if !defined(HAVE_HSTRERROR) || defined(NEED_HSTRERROR_PROTO) 544#ifndef HAVE_HSTRERROR 545#define hstrerror rk_hstrerror 546#endif 547/* This causes a fatal error under Psoriasis */ 548#ifndef SunOS 549ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL hstrerror(int); 550#endif 551#endif 552 553#if !HAVE_DECL_H_ERRNO 554extern int h_errno; 555#endif 556 557#if !defined(HAVE_INET_ATON) || defined(NEED_INET_ATON_PROTO) 558#ifndef HAVE_INET_ATON 559#define inet_aton rk_inet_aton 560#endif 561ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL inet_aton(const char *, struct in_addr *); 562#endif 563 564#ifndef HAVE_INET_NTOP 565#define inet_ntop rk_inet_ntop 566ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL 567inet_ntop(int af, const void *src, char *dst, size_t size); 568#endif 569 570#ifndef HAVE_INET_PTON 571#define inet_pton rk_inet_pton 572ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 573inet_pton(int, const char *, void *); 574#endif 575 576#ifndef HAVE_GETCWD 577#define getcwd rk_getcwd 578ROKEN_LIB_FUNCTION char* ROKEN_LIB_CALL getcwd(char *, size_t); 579#endif 580 581#ifdef HAVE_PWD_H 582#include <pwd.h> 583ROKEN_LIB_FUNCTION struct passwd * ROKEN_LIB_CALL k_getpwnam (const char *); 584ROKEN_LIB_FUNCTION struct passwd * ROKEN_LIB_CALL k_getpwuid (uid_t); 585#endif 586 587ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL get_default_username (void); 588 589#ifndef HAVE_SETEUID 590#define seteuid rk_seteuid 591ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL seteuid(uid_t); 592#endif 593 594#ifndef HAVE_SETEGID 595#define setegid rk_setegid 596ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL setegid(gid_t); 597#endif 598 599#ifndef HAVE_LSTAT 600#define lstat rk_lstat 601ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL lstat(const char *, struct stat *); 602#endif 603 604#if !defined(HAVE_MKSTEMP) || defined(NEED_MKSTEMP_PROTO) 605#ifndef HAVE_MKSTEMP 606#define mkstemp rk_mkstemp 607#endif 608ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL mkstemp(char *); 609#endif 610 611#ifndef HAVE_CGETENT 612#define cgetent rk_cgetent 613#define cgetstr rk_cgetstr 614ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetent(char **, char **, const char *); 615ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetstr(char *, const char *, char **); 616#endif 617 618#ifndef HAVE_INITGROUPS 619#define initgroups rk_initgroups 620ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL initgroups(const char *, gid_t); 621#endif 622 623#ifndef HAVE_FCHOWN 624#define fchown rk_fchown 625ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL fchown(int, uid_t, gid_t); 626#endif 627 628#ifdef RENAME_DOES_NOT_UNLINK 629ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_rename(const char *, const char *); 630#else 631#define rk_rename(__rk_rn_from,__rk_rn_to) rename(__rk_rn_from,__rk_rn_to) 632#endif 633 634#if !defined(HAVE_DAEMON) || defined(NEED_DAEMON_PROTO) 635#ifndef HAVE_DAEMON 636#define daemon rk_daemon 637#endif 638ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL daemon(int, int); 639#endif 640 641#ifndef HAVE_CHOWN 642#define chown rk_chown 643ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL chown(const char *, uid_t, gid_t); 644#endif 645 646#ifndef HAVE_RCMD 647#define rcmd rk_rcmd 648ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 649 rcmd(char **, unsigned short, const char *, 650 const char *, const char *, int *); 651#endif 652 653#if !defined(HAVE_INNETGR) || defined(NEED_INNETGR_PROTO) 654#ifndef HAVE_INNETGR 655#define innetgr rk_innetgr 656#endif 657ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL innetgr(const char*, const char*, 658 const char*, const char*); 659#endif 660 661#ifndef HAVE_IRUSEROK 662#define iruserok rk_iruserok 663ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL iruserok(unsigned, int, 664 const char *, const char *); 665#endif 666 667#if !defined(HAVE_GETHOSTNAME) || defined(NEED_GETHOSTNAME_PROTO) 668#ifndef HAVE_GETHOSTNAME 669#define gethostname rk_gethostname 670#endif 671ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL gethostname(char *, int); 672#endif 673 674#ifndef HAVE_WRITEV 675#define writev rk_writev 676ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 677writev(int, const struct iovec *, int); 678#endif 679 680#ifndef HAVE_READV 681#define readv rk_readv 682ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 683readv(int, const struct iovec *, int); 684#endif 685 686#ifndef HAVE_PIDFILE 687#ifdef NO_PIDFILES 688#define pidfile(x) ((void) 0) 689#else 690#define pidfile rk_pidfile 691ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL pidfile (const char*); 692#endif 693#endif 694 695#ifndef HAVE_BSWAP32 696#define bswap32 rk_bswap32 697ROKEN_LIB_FUNCTION unsigned int ROKEN_LIB_CALL bswap32(unsigned int); 698#endif 699 700#ifndef HAVE_BSWAP16 701#define bswap16 rk_bswap16 702ROKEN_LIB_FUNCTION unsigned short ROKEN_LIB_CALL bswap16(unsigned short); 703#endif 704 705#ifndef HAVE_FLOCK 706#ifndef LOCK_SH 707#define LOCK_SH 1 /* Shared lock */ 708#endif 709#ifndef LOCK_EX 710#define LOCK_EX 2 /* Exclusive lock */ 711#endif 712#ifndef LOCK_NB 713#define LOCK_NB 4 /* Don't block when locking */ 714#endif 715#ifndef LOCK_UN 716#define LOCK_UN 8 /* Unlock */ 717#endif 718 719#define flock(_x,_y) rk_flock(_x,_y) 720int rk_flock(int fd, int operation); 721#endif /* HAVE_FLOCK */ 722 723#ifndef HAVE_DIRFD 724#ifdef HAVE_DIR_DD_FD 725#define dirfd(x) ((x)->dd_fd) 726#else 727#ifndef _WIN32 /* Windows code never calls dirfd */ 728#error Missing dirfd() and ->dd_fd 729#endif 730#endif 731#endif 732 733ROKEN_LIB_FUNCTION time_t ROKEN_LIB_CALL tm2time (struct tm, int); 734 735ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL unix_verify_user(char *, char *); 736 737ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL roken_concat (char *, size_t, ...); 738 739ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL roken_mconcat (char **, size_t, ...); 740 741ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL roken_vconcat (char *, size_t, va_list); 742 743ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL 744 roken_vmconcat (char **, size_t, va_list); 745 746ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 747 net_write (rk_socket_t, const void *, size_t); 748 749ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 750 net_read (rk_socket_t, void *, size_t); 751 752ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 753 issuid(void); 754 755#ifndef HAVE_STRUCT_WINSIZE 756struct winsize { 757 unsigned short ws_row, ws_col; 758 unsigned short ws_xpixel, ws_ypixel; 759}; 760#endif 761 762ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL get_window_size(int fd, int *, int *); 763 764#ifndef HAVE_VSYSLOG 765#define vsyslog rk_vsyslog 766ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL vsyslog(int, const char *, va_list); 767#endif 768 769#ifndef HAVE_GETOPT 770#define getopt rk_getopt 771#define optarg rk_optarg 772#define optind rk_optind 773#define opterr rk_opterr 774ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 775getopt(int nargc, char * const *nargv, const char *ostr); 776#endif 777 778#if !HAVE_DECL_OPTARG 779ROKEN_LIB_VARIABLE extern char *optarg; 780#endif 781#if !HAVE_DECL_OPTIND 782ROKEN_LIB_VARIABLE extern int optind; 783#endif 784#if !HAVE_DECL_OPTERR 785ROKEN_LIB_VARIABLE extern int opterr; 786#endif 787 788#ifndef HAVE_GETIPNODEBYNAME 789#define getipnodebyname rk_getipnodebyname 790ROKEN_LIB_FUNCTION struct hostent * ROKEN_LIB_CALL 791getipnodebyname (const char *, int, int, int *); 792#endif 793 794#ifndef HAVE_GETIPNODEBYADDR 795#define getipnodebyaddr rk_getipnodebyaddr 796ROKEN_LIB_FUNCTION struct hostent * ROKEN_LIB_CALL 797getipnodebyaddr (const void *, size_t, int, int *); 798#endif 799 800#ifndef HAVE_FREEHOSTENT 801#define freehostent rk_freehostent 802ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 803freehostent (struct hostent *); 804#endif 805 806#ifndef HAVE_COPYHOSTENT 807#define copyhostent rk_copyhostent 808ROKEN_LIB_FUNCTION struct hostent * ROKEN_LIB_CALL 809copyhostent (const struct hostent *); 810#endif 811 812#ifndef HAVE_SOCKLEN_T 813typedef int socklen_t; 814#endif 815 816#ifndef HAVE_STRUCT_SOCKADDR_STORAGE 817 818#ifndef HAVE_SA_FAMILY_T 819typedef unsigned short sa_family_t; 820#endif 821 822#ifdef HAVE_IPV6 823#define _SS_MAXSIZE sizeof(struct sockaddr_in6) 824#else 825#define _SS_MAXSIZE sizeof(struct sockaddr_in) 826#endif 827 828#define _SS_ALIGNSIZE sizeof(unsigned long) 829 830#if HAVE_STRUCT_SOCKADDR_SA_LEN 831 832typedef unsigned char roken_sa_family_t; 833 834#define _SS_PAD1SIZE ((2 * _SS_ALIGNSIZE - sizeof (roken_sa_family_t) - sizeof(unsigned char)) % _SS_ALIGNSIZE) 835#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + sizeof(unsigned char) + _SS_PAD1SIZE + _SS_ALIGNSIZE)) 836 837struct sockaddr_storage { 838 unsigned char ss_len; 839 roken_sa_family_t ss_family; 840 char __ss_pad1[_SS_PAD1SIZE]; 841 unsigned long __ss_align[_SS_PAD2SIZE / sizeof(unsigned long) + 1]; 842}; 843 844#else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */ 845 846typedef unsigned short roken_sa_family_t; 847 848#define _SS_PAD1SIZE ((2 * _SS_ALIGNSIZE - sizeof (roken_sa_family_t)) % _SS_ALIGNSIZE) 849#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + _SS_PAD1SIZE + _SS_ALIGNSIZE)) 850 851struct sockaddr_storage { 852 roken_sa_family_t ss_family; 853 char __ss_pad1[_SS_PAD1SIZE]; 854 unsigned long __ss_align[_SS_PAD2SIZE / sizeof(unsigned long) + 1]; 855}; 856 857#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ 858 859#endif /* HAVE_STRUCT_SOCKADDR_STORAGE */ 860 861#ifndef HAVE_STRUCT_ADDRINFO 862struct addrinfo { 863 int ai_flags; 864 int ai_family; 865 int ai_socktype; 866 int ai_protocol; 867 size_t ai_addrlen; 868 char *ai_canonname; 869 struct sockaddr *ai_addr; 870 struct addrinfo *ai_next; 871}; 872#endif 873 874#ifndef HAVE_GETADDRINFO 875#define getaddrinfo rk_getaddrinfo 876ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 877getaddrinfo(const char *, 878 const char *, 879 const struct addrinfo *, 880 struct addrinfo **); 881#endif 882 883#ifndef HAVE_GETNAMEINFO 884#define getnameinfo rk_getnameinfo 885ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 886getnameinfo(const struct sockaddr *, socklen_t, 887 char *, size_t, 888 char *, size_t, 889 int); 890#endif 891 892#ifndef HAVE_FREEADDRINFO 893#define freeaddrinfo rk_freeaddrinfo 894ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 895freeaddrinfo(struct addrinfo *); 896#endif 897 898#ifndef HAVE_GAI_STRERROR 899#define gai_strerror rk_gai_strerror 900ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL 901gai_strerror(int); 902#endif 903 904#ifdef NO_SLEEP 905 906ROKEN_LIB_FUNCTION unsigned int ROKEN_LIB_CALL 907sleep(unsigned int seconds); 908 909#endif 910 911ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 912getnameinfo_verified(const struct sockaddr *, socklen_t, 913 char *, size_t, 914 char *, size_t, 915 int); 916 917ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 918roken_getaddrinfo_hostspec(const char *, int, struct addrinfo **); 919ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 920roken_getaddrinfo_hostspec2(const char *, int, int, struct addrinfo **); 921 922#ifndef HAVE_STRFTIME 923#define strftime rk_strftime 924ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL 925strftime (char *, size_t, const char *, const struct tm *); 926#endif 927 928#ifndef HAVE_STRPTIME 929#define strptime rk_strptime 930ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL 931strptime (const char *, const char *, struct tm *); 932#endif 933 934#ifndef HAVE_GETTIMEOFDAY 935#define gettimeofday rk_gettimeofday 936ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 937gettimeofday (struct timeval *, void *); 938#endif 939 940#ifndef HAVE_EMALLOC 941#define emalloc rk_emalloc 942ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL emalloc (size_t); 943#endif 944#ifndef HAVE_ECALLOC 945#define ecalloc rk_ecalloc 946ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL ecalloc(size_t, size_t); 947#endif 948#ifndef HAVE_EREALLOC 949#define erealloc rk_erealloc 950ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL erealloc (void *, size_t); 951#endif 952#ifndef HAVE_ESTRDUP 953#define estrdup rk_estrdup 954ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL estrdup (const char *); 955#endif 956 957/* 958 * kludges and such 959 */ 960 961ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 962roken_gethostby_setup(const char*, const char*); 963ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL 964roken_gethostbyname(const char*); 965ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL 966roken_gethostbyaddr(const void*, size_t, int); 967 968#ifdef GETSERVBYNAME_PROTO_COMPATIBLE 969#define roken_getservbyname(x,y) getservbyname(x,y) 970#else 971#define roken_getservbyname(x,y) getservbyname((char *)x, (char *)y) 972#endif 973 974#ifdef OPENLOG_PROTO_COMPATIBLE 975#define roken_openlog(a,b,c) openlog(a,b,c) 976#else 977#define roken_openlog(a,b,c) openlog((char *)a,b,c) 978#endif 979 980#ifdef GETSOCKNAME_PROTO_COMPATIBLE 981#define roken_getsockname(a,b,c) getsockname(a,b,c) 982#else 983#define roken_getsockname(a,b,c) getsockname(a, b, (void*)c) 984#endif 985 986#ifndef HAVE_SETPROGNAME 987#define setprogname rk_setprogname 988ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL setprogname(const char *); 989#endif 990 991#ifndef HAVE_GETPROGNAME 992#define getprogname rk_getprogname 993ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL getprogname(void); 994#endif 995 996#if !defined(HAVE_SETPROGNAME) && !defined(HAVE_GETPROGNAME) && !HAVE_DECL___PROGNAME 997extern const char *__progname; 998#endif 999 1000ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 1001mini_inetd_addrinfo (struct addrinfo*, rk_socket_t *); 1002 1003ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 1004mini_inetd (int, rk_socket_t *); 1005 1006#ifndef HAVE_LOCALTIME_R 1007#define localtime_r rk_localtime_r 1008ROKEN_LIB_FUNCTION struct tm * ROKEN_LIB_CALL 1009localtime_r(const time_t *, struct tm *); 1010#endif 1011 1012#if !defined(HAVE_STRSVIS) || defined(NEED_STRSVIS_PROTO) 1013#ifndef HAVE_STRSVIS 1014#define strsvis rk_strsvis 1015#endif 1016ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1017strsvis(char *, const char *, int, const char *); 1018#endif 1019 1020#if !defined(HAVE_STRSVISX) || defined(NEED_STRSVISX_PROTO) 1021#ifndef HAVE_STRSVISX 1022#define strsvisx rk_strsvisx 1023#endif 1024ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1025strsvisx(char *, const char *, size_t, int, const char *); 1026#endif 1027 1028#if !defined(HAVE_STRUNVIS) || defined(NEED_STRUNVIS_PROTO) 1029#ifndef HAVE_STRUNVIS 1030#define strunvis rk_strunvis 1031#endif 1032ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1033strunvis(char *, const char *); 1034#endif 1035 1036#if !defined(HAVE_STRVIS) || defined(NEED_STRVIS_PROTO) 1037#ifndef HAVE_STRVIS 1038#define strvis rk_strvis 1039#endif 1040ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1041strvis(char *, const char *, int); 1042#endif 1043 1044#if !defined(HAVE_STRVISX) || defined(NEED_STRVISX_PROTO) 1045#ifndef HAVE_STRVISX 1046#define strvisx rk_strvisx 1047#endif 1048ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1049strvisx(char *, const char *, size_t, int); 1050#endif 1051 1052#if !defined(HAVE_SVIS) || defined(NEED_SVIS_PROTO) 1053#ifndef HAVE_SVIS 1054#define svis rk_svis 1055#endif 1056ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL 1057svis(char *, int, int, int, const char *); 1058#endif 1059 1060#if !defined(HAVE_UNVIS) || defined(NEED_UNVIS_PROTO) 1061#ifndef HAVE_UNVIS 1062#define unvis rk_unvis 1063#endif 1064ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1065unvis(char *, int, int *, int); 1066#endif 1067 1068#if !defined(HAVE_VIS) || defined(NEED_VIS_PROTO) 1069#ifndef HAVE_VIS 1070#define vis rk_vis 1071#endif 1072ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL 1073vis(char *, int, int, int); 1074#endif 1075 1076#if !defined(HAVE_CLOSEFROM) 1077#define closefrom rk_closefrom 1078ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1079closefrom(int); 1080#endif 1081 1082#if !defined(HAVE_TIMEGM) 1083#define timegm rk_timegm 1084ROKEN_LIB_FUNCTION time_t ROKEN_LIB_CALL 1085rk_timegm(struct tm *tm); 1086#endif 1087 1088#ifdef NEED_QSORT 1089#define qsort rk_qsort 1090void 1091rk_qsort(void *, size_t, size_t, int (*)(const void *, const void *)); 1092#endif 1093 1094#if defined(HAVE_ARC4RANDOM) 1095#define rk_random() arc4random() 1096#elif defined(HAVE_RANDOM) 1097#define rk_random() random() 1098#else 1099#define rk_random() rand() 1100#endif 1101 1102#ifndef HAVE_TDELETE 1103#define tdelete(a,b,c) rk_tdelete(a,b,c) 1104#endif 1105#ifndef HAVE_TFIND 1106#define tfind(a,b,c) rk_tfind(a,b,c) 1107#endif 1108#ifndef HAVE_TSEARCH 1109#define tsearch(a,b,c) rk_tsearch(a,b,c) 1110#endif 1111#ifndef HAVE_TWALK 1112#define twalk(a,b) rk_twalk(a,b) 1113#endif 1114 1115#if defined(__linux__) && defined(SOCK_CLOEXEC) && !defined(SOCKET_WRAPPER_REPLACE) && !defined(__SOCKET_WRAPPER_H__) 1116#undef socket 1117#define socket(_fam,_type,_prot) rk_socket(_fam,_type,_prot) 1118int ROKEN_LIB_FUNCTION rk_socket(int, int, int); 1119#endif 1120 1121#ifdef SOCKET_WRAPPER_REPLACE 1122#include <socket_wrapper.h> 1123#endif 1124 1125ROKEN_CPP_END 1126