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