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)) || (!defined(STRERROR_R_PROTO_COMPATIBLE) && defined(HAVE_STRERROR_R)) 555int ROKEN_LIB_FUNCTION rk_strerror_r(int, char *, size_t); 556#else 557#define rk_strerror_r strerror_r 558#endif 559 560#if !defined(HAVE_HSTRERROR) || defined(NEED_HSTRERROR_PROTO) 561#ifndef HAVE_HSTRERROR 562#define hstrerror rk_hstrerror 563#endif 564/* This causes a fatal error under Psoriasis */ 565#ifndef SunOS 566ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL hstrerror(int); 567#endif 568#endif 569 570#if !HAVE_DECL_H_ERRNO 571extern int h_errno; 572#endif 573 574#if !defined(HAVE_INET_ATON) || defined(NEED_INET_ATON_PROTO) 575#ifndef HAVE_INET_ATON 576#define inet_aton rk_inet_aton 577#endif 578ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL inet_aton(const char *, struct in_addr *); 579#endif 580 581#ifndef HAVE_INET_NTOP 582#define inet_ntop rk_inet_ntop 583ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL 584inet_ntop(int af, const void *src, char *dst, size_t size); 585#endif 586 587#ifndef HAVE_INET_PTON 588#define inet_pton rk_inet_pton 589ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 590inet_pton(int, const char *, void *); 591#endif 592 593#ifndef HAVE_GETCWD 594#define getcwd rk_getcwd 595ROKEN_LIB_FUNCTION char* ROKEN_LIB_CALL getcwd(char *, size_t); 596#endif 597 598#ifdef HAVE_PWD_H 599#include <pwd.h> 600ROKEN_LIB_FUNCTION struct passwd * ROKEN_LIB_CALL k_getpwnam (const char *); 601ROKEN_LIB_FUNCTION struct passwd * ROKEN_LIB_CALL k_getpwuid (uid_t); 602#endif 603 604ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL get_default_username (void); 605 606#ifndef HAVE_SETEUID 607#define seteuid rk_seteuid 608ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL seteuid(uid_t); 609#endif 610 611#ifndef HAVE_SETEGID 612#define setegid rk_setegid 613ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL setegid(gid_t); 614#endif 615 616#ifndef HAVE_LSTAT 617#define lstat rk_lstat 618ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL lstat(const char *, struct stat *); 619#endif 620 621#if !defined(HAVE_MKSTEMP) || defined(NEED_MKSTEMP_PROTO) 622#ifndef HAVE_MKSTEMP 623#define mkstemp rk_mkstemp 624#endif 625ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL mkstemp(char *); 626#endif 627 628#ifndef HAVE_CGETENT 629#define cgetent rk_cgetent 630#define cgetstr rk_cgetstr 631ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetent(char **, char **, const char *); 632ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetstr(char *, const char *, char **); 633#endif 634 635#ifndef HAVE_INITGROUPS 636#define initgroups rk_initgroups 637ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL initgroups(const char *, gid_t); 638#endif 639 640#ifndef HAVE_FCHOWN 641#define fchown rk_fchown 642ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL fchown(int, uid_t, gid_t); 643#endif 644 645#ifdef RENAME_DOES_NOT_UNLINK 646ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_rename(const char *, const char *); 647#else 648#define rk_rename(__rk_rn_from,__rk_rn_to) rename(__rk_rn_from,__rk_rn_to) 649#endif 650 651#if !defined(HAVE_DAEMON) || defined(NEED_DAEMON_PROTO) 652#ifndef HAVE_DAEMON 653#define daemon rk_daemon 654#endif 655ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL daemon(int, int); 656#endif 657 658#ifndef HAVE_CHOWN 659#define chown rk_chown 660ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL chown(const char *, uid_t, gid_t); 661#endif 662 663#ifndef HAVE_RCMD 664#define rcmd rk_rcmd 665ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 666 rcmd(char **, unsigned short, const char *, 667 const char *, const char *, int *); 668#endif 669 670#if !defined(HAVE_INNETGR) || defined(NEED_INNETGR_PROTO) 671#ifndef HAVE_INNETGR 672#define innetgr rk_innetgr 673#endif 674ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL innetgr(const char*, const char*, 675 const char*, const char*); 676#endif 677 678#ifndef HAVE_IRUSEROK 679#define iruserok rk_iruserok 680ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL iruserok(unsigned, int, 681 const char *, const char *); 682#endif 683 684#if !defined(HAVE_GETHOSTNAME) || defined(NEED_GETHOSTNAME_PROTO) 685#ifndef HAVE_GETHOSTNAME 686#define gethostname rk_gethostname 687#endif 688ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL gethostname(char *, int); 689#endif 690 691#ifndef HAVE_WRITEV 692#define writev rk_writev 693ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 694writev(int, const struct iovec *, int); 695#endif 696 697#ifndef HAVE_READV 698#define readv rk_readv 699ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 700readv(int, const struct iovec *, int); 701#endif 702 703#ifndef HAVE_PIDFILE 704#ifdef NO_PIDFILES 705#define pidfile(x) ((void) 0) 706#else 707#define pidfile rk_pidfile 708ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL pidfile (const char*); 709#endif 710#endif 711 712#ifndef HAVE_BSWAP32 713#define bswap32 rk_bswap32 714ROKEN_LIB_FUNCTION unsigned int ROKEN_LIB_CALL bswap32(unsigned int); 715#endif 716 717#ifndef HAVE_BSWAP16 718#define bswap16 rk_bswap16 719ROKEN_LIB_FUNCTION unsigned short ROKEN_LIB_CALL bswap16(unsigned short); 720#endif 721 722#ifndef HAVE_FLOCK 723#ifndef LOCK_SH 724#define LOCK_SH 1 /* Shared lock */ 725#endif 726#ifndef LOCK_EX 727#define LOCK_EX 2 /* Exclusive lock */ 728#endif 729#ifndef LOCK_NB 730#define LOCK_NB 4 /* Don't block when locking */ 731#endif 732#ifndef LOCK_UN 733#define LOCK_UN 8 /* Unlock */ 734#endif 735 736#define flock(_x,_y) rk_flock(_x,_y) 737int rk_flock(int fd, int operation); 738#endif /* HAVE_FLOCK */ 739 740#ifndef HAVE_DIRFD 741#ifdef HAVE_DIR_DD_FD 742#define dirfd(x) ((x)->dd_fd) 743#else 744#ifndef _WIN32 /* Windows code never calls dirfd */ 745#error Missing dirfd() and ->dd_fd 746#endif 747#endif 748#endif 749 750ROKEN_LIB_FUNCTION time_t ROKEN_LIB_CALL tm2time (struct tm, int); 751 752ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL unix_verify_user(char *, char *); 753 754ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL roken_concat (char *, size_t, ...); 755 756ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL roken_mconcat (char **, size_t, ...); 757 758ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL roken_vconcat (char *, size_t, va_list); 759 760ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL 761 roken_vmconcat (char **, size_t, va_list); 762 763ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 764 net_write (rk_socket_t, const void *, size_t); 765 766ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL 767 net_read (rk_socket_t, void *, size_t); 768 769ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 770 issuid(void); 771 772#ifndef HAVE_STRUCT_WINSIZE 773struct winsize { 774 unsigned short ws_row, ws_col; 775 unsigned short ws_xpixel, ws_ypixel; 776}; 777#endif 778 779ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL get_window_size(int fd, int *, int *); 780 781#ifndef HAVE_VSYSLOG 782#define vsyslog rk_vsyslog 783ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL vsyslog(int, const char *, va_list); 784#endif 785 786#ifndef HAVE_GETOPT 787#define getopt rk_getopt 788#define optarg rk_optarg 789#define optind rk_optind 790#define opterr rk_opterr 791ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 792getopt(int nargc, char * const *nargv, const char *ostr); 793#endif 794 795#if !HAVE_DECL_OPTARG 796ROKEN_LIB_VARIABLE extern char *optarg; 797#endif 798#if !HAVE_DECL_OPTIND 799ROKEN_LIB_VARIABLE extern int optind; 800#endif 801#if !HAVE_DECL_OPTERR 802ROKEN_LIB_VARIABLE extern int opterr; 803#endif 804 805#ifndef HAVE_GETIPNODEBYNAME 806#define getipnodebyname rk_getipnodebyname 807ROKEN_LIB_FUNCTION struct hostent * ROKEN_LIB_CALL 808getipnodebyname (const char *, int, int, int *); 809#endif 810 811#ifndef HAVE_GETIPNODEBYADDR 812#define getipnodebyaddr rk_getipnodebyaddr 813ROKEN_LIB_FUNCTION struct hostent * ROKEN_LIB_CALL 814getipnodebyaddr (const void *, size_t, int, int *); 815#endif 816 817#ifndef HAVE_FREEHOSTENT 818#define freehostent rk_freehostent 819ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 820freehostent (struct hostent *); 821#endif 822 823#ifndef HAVE_COPYHOSTENT 824#define copyhostent rk_copyhostent 825ROKEN_LIB_FUNCTION struct hostent * ROKEN_LIB_CALL 826copyhostent (const struct hostent *); 827#endif 828 829#ifndef HAVE_SOCKLEN_T 830typedef int socklen_t; 831#endif 832 833#ifndef HAVE_STRUCT_SOCKADDR_STORAGE 834 835#ifndef HAVE_SA_FAMILY_T 836typedef unsigned short sa_family_t; 837#endif 838 839#ifdef HAVE_IPV6 840#define _SS_MAXSIZE sizeof(struct sockaddr_in6) 841#else 842#define _SS_MAXSIZE sizeof(struct sockaddr_in) 843#endif 844 845#define _SS_ALIGNSIZE sizeof(unsigned long) 846 847#if HAVE_STRUCT_SOCKADDR_SA_LEN 848 849typedef unsigned char roken_sa_family_t; 850 851#define _SS_PAD1SIZE ((2 * _SS_ALIGNSIZE - sizeof (roken_sa_family_t) - sizeof(unsigned char)) % _SS_ALIGNSIZE) 852#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + sizeof(unsigned char) + _SS_PAD1SIZE + _SS_ALIGNSIZE)) 853 854struct sockaddr_storage { 855 unsigned char ss_len; 856 roken_sa_family_t ss_family; 857 char __ss_pad1[_SS_PAD1SIZE]; 858 unsigned long __ss_align[_SS_PAD2SIZE / sizeof(unsigned long) + 1]; 859}; 860 861#else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */ 862 863typedef unsigned short roken_sa_family_t; 864 865#define _SS_PAD1SIZE ((2 * _SS_ALIGNSIZE - sizeof (roken_sa_family_t)) % _SS_ALIGNSIZE) 866#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + _SS_PAD1SIZE + _SS_ALIGNSIZE)) 867 868struct sockaddr_storage { 869 roken_sa_family_t ss_family; 870 char __ss_pad1[_SS_PAD1SIZE]; 871 unsigned long __ss_align[_SS_PAD2SIZE / sizeof(unsigned long) + 1]; 872}; 873 874#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ 875 876#endif /* HAVE_STRUCT_SOCKADDR_STORAGE */ 877 878#ifndef HAVE_STRUCT_ADDRINFO 879struct addrinfo { 880 int ai_flags; 881 int ai_family; 882 int ai_socktype; 883 int ai_protocol; 884 size_t ai_addrlen; 885 char *ai_canonname; 886 struct sockaddr *ai_addr; 887 struct addrinfo *ai_next; 888}; 889#endif 890 891#ifndef HAVE_GETADDRINFO 892#define getaddrinfo rk_getaddrinfo 893ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 894getaddrinfo(const char *, 895 const char *, 896 const struct addrinfo *, 897 struct addrinfo **); 898#endif 899 900#ifndef HAVE_GETNAMEINFO 901#define getnameinfo rk_getnameinfo 902ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 903getnameinfo(const struct sockaddr *, socklen_t, 904 char *, size_t, 905 char *, size_t, 906 int); 907#endif 908 909#ifndef HAVE_FREEADDRINFO 910#define freeaddrinfo rk_freeaddrinfo 911ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 912freeaddrinfo(struct addrinfo *); 913#endif 914 915#ifndef HAVE_GAI_STRERROR 916#define gai_strerror rk_gai_strerror 917ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL 918gai_strerror(int); 919#endif 920 921#ifdef NO_SLEEP 922 923ROKEN_LIB_FUNCTION unsigned int ROKEN_LIB_CALL 924sleep(unsigned int seconds); 925 926#endif 927 928ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 929getnameinfo_verified(const struct sockaddr *, socklen_t, 930 char *, size_t, 931 char *, size_t, 932 int); 933 934ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 935roken_getaddrinfo_hostspec(const char *, int, struct addrinfo **); 936ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 937roken_getaddrinfo_hostspec2(const char *, int, int, struct addrinfo **); 938 939#ifndef HAVE_STRFTIME 940#define strftime rk_strftime 941ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL 942strftime (char *, size_t, const char *, const struct tm *); 943#endif 944 945#ifndef HAVE_STRPTIME 946#define strptime rk_strptime 947ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL 948strptime (const char *, const char *, struct tm *); 949#endif 950 951#ifndef HAVE_GETTIMEOFDAY 952#define gettimeofday rk_gettimeofday 953ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 954gettimeofday (struct timeval *, void *); 955#endif 956 957#ifndef HAVE_EMALLOC 958#define emalloc rk_emalloc 959ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL emalloc (size_t); 960#endif 961#ifndef HAVE_ECALLOC 962#define ecalloc rk_ecalloc 963ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL ecalloc(size_t, size_t); 964#endif 965#ifndef HAVE_EREALLOC 966#define erealloc rk_erealloc 967ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL erealloc (void *, size_t); 968#endif 969#ifndef HAVE_ESTRDUP 970#define estrdup rk_estrdup 971ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL estrdup (const char *); 972#endif 973 974/* 975 * kludges and such 976 */ 977 978ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 979roken_gethostby_setup(const char*, const char*); 980ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL 981roken_gethostbyname(const char*); 982ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL 983roken_gethostbyaddr(const void*, size_t, int); 984 985#ifdef GETSERVBYNAME_PROTO_COMPATIBLE 986#define roken_getservbyname(x,y) getservbyname(x,y) 987#else 988#define roken_getservbyname(x,y) getservbyname((char *)x, (char *)y) 989#endif 990 991#ifdef OPENLOG_PROTO_COMPATIBLE 992#define roken_openlog(a,b,c) openlog(a,b,c) 993#else 994#define roken_openlog(a,b,c) openlog((char *)a,b,c) 995#endif 996 997#ifdef GETSOCKNAME_PROTO_COMPATIBLE 998#define roken_getsockname(a,b,c) getsockname(a,b,c) 999#else 1000#define roken_getsockname(a,b,c) getsockname(a, b, (void*)c) 1001#endif 1002 1003#ifndef HAVE_SETPROGNAME 1004#define setprogname rk_setprogname 1005ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL setprogname(const char *); 1006#endif 1007 1008#ifndef HAVE_GETPROGNAME 1009#define getprogname rk_getprogname 1010ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL getprogname(void); 1011#endif 1012 1013#if !defined(HAVE_SETPROGNAME) && !defined(HAVE_GETPROGNAME) && !HAVE_DECL___PROGNAME 1014extern const char *__progname; 1015#endif 1016 1017ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 1018mini_inetd_addrinfo (struct addrinfo*, rk_socket_t *); 1019 1020ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 1021mini_inetd (int, rk_socket_t *); 1022 1023#ifndef HAVE_LOCALTIME_R 1024#define localtime_r rk_localtime_r 1025ROKEN_LIB_FUNCTION struct tm * ROKEN_LIB_CALL 1026localtime_r(const time_t *, struct tm *); 1027#endif 1028 1029#if !defined(HAVE_STRSVIS) || defined(NEED_STRSVIS_PROTO) 1030#ifndef HAVE_STRSVIS 1031#define strsvis rk_strsvis 1032#endif 1033ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1034strsvis(char *, const char *, int, const char *); 1035#endif 1036 1037#if !defined(HAVE_STRSVISX) || defined(NEED_STRSVISX_PROTO) 1038#ifndef HAVE_STRSVISX 1039#define strsvisx rk_strsvisx 1040#endif 1041ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1042strsvisx(char *, const char *, size_t, int, const char *); 1043#endif 1044 1045#if !defined(HAVE_STRUNVIS) || defined(NEED_STRUNVIS_PROTO) 1046#ifndef HAVE_STRUNVIS 1047#define strunvis rk_strunvis 1048#endif 1049ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1050strunvis(char *, const char *); 1051#endif 1052 1053#if !defined(HAVE_STRVIS) || defined(NEED_STRVIS_PROTO) 1054#ifndef HAVE_STRVIS 1055#define strvis rk_strvis 1056#endif 1057ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1058strvis(char *, const char *, int); 1059#endif 1060 1061#if !defined(HAVE_STRVISX) || defined(NEED_STRVISX_PROTO) 1062#ifndef HAVE_STRVISX 1063#define strvisx rk_strvisx 1064#endif 1065ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1066strvisx(char *, const char *, size_t, int); 1067#endif 1068 1069#if !defined(HAVE_SVIS) || defined(NEED_SVIS_PROTO) 1070#ifndef HAVE_SVIS 1071#define svis rk_svis 1072#endif 1073ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL 1074svis(char *, int, int, int, const char *); 1075#endif 1076 1077#if !defined(HAVE_UNVIS) || defined(NEED_UNVIS_PROTO) 1078#ifndef HAVE_UNVIS 1079#define unvis rk_unvis 1080#endif 1081ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1082unvis(char *, int, int *, int); 1083#endif 1084 1085#if !defined(HAVE_VIS) || defined(NEED_VIS_PROTO) 1086#ifndef HAVE_VIS 1087#define vis rk_vis 1088#endif 1089ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL 1090vis(char *, int, int, int); 1091#endif 1092 1093#if !defined(HAVE_CLOSEFROM) 1094#define closefrom rk_closefrom 1095ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 1096closefrom(int); 1097#endif 1098 1099#if !defined(HAVE_TIMEGM) 1100#define timegm rk_timegm 1101ROKEN_LIB_FUNCTION time_t ROKEN_LIB_CALL 1102rk_timegm(struct tm *tm); 1103#endif 1104 1105#ifdef NEED_QSORT 1106#define qsort rk_qsort 1107void 1108rk_qsort(void *, size_t, size_t, int (*)(const void *, const void *)); 1109#endif 1110 1111#if defined(HAVE_ARC4RANDOM) 1112#define rk_random() arc4random() 1113#elif defined(HAVE_RANDOM) 1114#define rk_random() random() 1115#else 1116#define rk_random() rand() 1117#endif 1118 1119#ifndef HAVE_TDELETE 1120#define tdelete(a,b,c) rk_tdelete(a,b,c) 1121#endif 1122#ifndef HAVE_TFIND 1123#define tfind(a,b,c) rk_tfind(a,b,c) 1124#endif 1125#ifndef HAVE_TSEARCH 1126#define tsearch(a,b,c) rk_tsearch(a,b,c) 1127#endif 1128#ifndef HAVE_TWALK 1129#define twalk(a,b) rk_twalk(a,b) 1130#endif 1131 1132#if defined(__linux__) && defined(SOCK_CLOEXEC) && !defined(SOCKET_WRAPPER_REPLACE) && !defined(__SOCKET_WRAPPER_H__) 1133#undef socket 1134#define socket(_fam,_type,_prot) rk_socket(_fam,_type,_prot) 1135int ROKEN_LIB_FUNCTION rk_socket(int, int, int); 1136#endif 1137 1138#ifdef SOCKET_WRAPPER_REPLACE 1139#include <socket_wrapper.h> 1140#endif 1141 1142ROKEN_CPP_END 1143