1 /* $OpenBSD: log.c,v 1.6 2015/08/20 13:00:23 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <sys/time.h> 22 23 #include <errno.h> 24 #include <stdarg.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <syslog.h> 29 #include <time.h> 30 #include <netdb.h> 31 #include <ctype.h> 32 33 #include "httpd.h" 34 35 int debug; 36 int verbose; 37 38 void vlog(int, const char *, va_list) 39 __attribute__((__format__ (printf, 2, 0))); 40 void logit(int, const char *, ...) 41 __attribute__((__format__ (printf, 2, 3))); 42 43 void 44 log_init(int n_debug) 45 { 46 extern char *__progname; 47 48 debug = n_debug; 49 verbose = n_debug; 50 51 if (!debug) 52 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 53 54 tzset(); 55 } 56 57 void 58 log_verbose(int v) 59 { 60 verbose = v; 61 } 62 63 void 64 logit(int pri, const char *fmt, ...) 65 { 66 va_list ap; 67 68 va_start(ap, fmt); 69 vlog(pri, fmt, ap); 70 va_end(ap); 71 } 72 73 void 74 vlog(int pri, const char *fmt, va_list ap) 75 { 76 char *nfmt; 77 78 if (debug) { 79 /* best effort in out of mem situations */ 80 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 81 vfprintf(stderr, fmt, ap); 82 fprintf(stderr, "\n"); 83 } else { 84 vfprintf(stderr, nfmt, ap); 85 free(nfmt); 86 } 87 fflush(stderr); 88 } else 89 vsyslog(pri, fmt, ap); 90 } 91 92 93 void 94 log_warn(const char *emsg, ...) 95 { 96 char *nfmt; 97 va_list ap; 98 99 /* best effort to even work in out of memory situations */ 100 if (emsg == NULL) 101 logit(LOG_CRIT, "%s", strerror(errno)); 102 else { 103 va_start(ap, emsg); 104 105 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 106 /* we tried it... */ 107 vlog(LOG_CRIT, emsg, ap); 108 logit(LOG_CRIT, "%s", strerror(errno)); 109 } else { 110 vlog(LOG_CRIT, nfmt, ap); 111 free(nfmt); 112 } 113 va_end(ap); 114 } 115 } 116 117 void 118 log_warnx(const char *emsg, ...) 119 { 120 va_list ap; 121 122 va_start(ap, emsg); 123 vlog(LOG_CRIT, emsg, ap); 124 va_end(ap); 125 } 126 127 void 128 log_info(const char *emsg, ...) 129 { 130 va_list ap; 131 132 va_start(ap, emsg); 133 vlog(LOG_INFO, emsg, ap); 134 va_end(ap); 135 } 136 137 void 138 log_debug(const char *emsg, ...) 139 { 140 va_list ap; 141 142 if (verbose > 1) { 143 va_start(ap, emsg); 144 vlog(LOG_DEBUG, emsg, ap); 145 va_end(ap); 146 } 147 } 148 149 void 150 fatal(const char *emsg) 151 { 152 if (emsg == NULL) 153 logit(LOG_CRIT, "fatal: %s", strerror(errno)); 154 else 155 if (errno) 156 logit(LOG_CRIT, "fatal: %s: %s", 157 emsg, strerror(errno)); 158 else 159 logit(LOG_CRIT, "fatal: %s", emsg); 160 161 exit(1); 162 } 163 164 void 165 fatalx(const char *emsg) 166 { 167 errno = 0; 168 fatal(emsg); 169 } 170 171 const char * 172 print_host(struct sockaddr_storage *ss, char *buf, size_t len) 173 { 174 if (getnameinfo((struct sockaddr *)ss, ss->ss_len, 175 buf, len, NULL, 0, NI_NUMERICHOST) != 0) { 176 buf[0] = '\0'; 177 return (NULL); 178 } 179 return (buf); 180 } 181 182 const char * 183 print_time(struct timeval *a, struct timeval *b, char *buf, size_t len) 184 { 185 struct timeval tv; 186 unsigned long h, sec, min; 187 188 timerclear(&tv); 189 timersub(a, b, &tv); 190 sec = tv.tv_sec % 60; 191 min = tv.tv_sec / 60 % 60; 192 h = tv.tv_sec / 60 / 60; 193 194 snprintf(buf, len, "%.2lu:%.2lu:%.2lu", h, min, sec); 195 return (buf); 196 } 197 198 const char * 199 printb_flags(const uint32_t v, const char *bits) 200 { 201 static char buf[2][BUFSIZ]; 202 static int idx = 0; 203 int i, any = 0; 204 char c, *p, *r; 205 206 p = r = buf[++idx % 2]; 207 memset(p, 0, BUFSIZ); 208 209 if (bits) { 210 bits++; 211 while ((i = *bits++)) { 212 if (v & (1 << (i - 1))) { 213 if (any) { 214 *p++ = ','; 215 *p++ = ' '; 216 } 217 any = 1; 218 for (; (c = *bits) > 32; bits++) { 219 if (c == '_') 220 *p++ = ' '; 221 else 222 *p++ = 223 tolower((unsigned char)c); 224 } 225 } else 226 for (; *bits > 32; bits++) 227 ; 228 } 229 } 230 231 return (r); 232 } 233 234 void 235 getmonotime(struct timeval *tv) 236 { 237 struct timespec ts; 238 239 if (clock_gettime(CLOCK_MONOTONIC, &ts)) 240 fatal("clock_gettime"); 241 242 TIMESPEC_TO_TIMEVAL(tv, &ts); 243 } 244