1 /* $OpenBSD: evutil.c,v 1.11 2019/06/28 13:32:42 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Niels Provos <provos@citi.umich.edu> 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 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/types.h> 31 #include <sys/socket.h> 32 #include <sys/queue.h> 33 34 #include <unistd.h> 35 #include <fcntl.h> 36 #include <stdlib.h> 37 #include <errno.h> 38 #include <stdio.h> 39 #include <signal.h> 40 41 #include "event.h" 42 #include "event-internal.h" 43 #include "evutil.h" 44 #include "log.h" 45 46 int 47 evutil_socketpair(int family, int type, int protocol, int fd[2]) 48 { 49 return socketpair(family, type, protocol, fd); 50 } 51 52 int 53 evutil_make_socket_nonblocking(int fd) 54 { 55 int flags; 56 57 if ((flags = fcntl(fd, F_GETFL)) == -1) { 58 event_warn("fcntl(%d, F_GETFL)", fd); 59 return -1; 60 } 61 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { 62 event_warn("fcntl(%d, F_SETFL)", fd); 63 return -1; 64 } 65 return 0; 66 } 67 68 ev_int64_t 69 evutil_strtoll(const char *s, char **endptr, int base) 70 { 71 return (ev_int64_t)strtoll(s, endptr, base); 72 } 73 74 int 75 evutil_snprintf(char *buf, size_t buflen, const char *format, ...) 76 { 77 int r; 78 va_list ap; 79 va_start(ap, format); 80 r = evutil_vsnprintf(buf, buflen, format, ap); 81 va_end(ap); 82 return r; 83 } 84 85 int 86 evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap) 87 { 88 int r = vsnprintf(buf, buflen, format, ap); 89 buf[buflen-1] = '\0'; 90 return r; 91 } 92 93 static int 94 evutil_issetugid(void) 95 { 96 return issetugid(); 97 } 98 99 const char * 100 evutil_getenv(const char *varname) 101 { 102 if (evutil_issetugid()) 103 return NULL; 104 105 return getenv(varname); 106 } 107