1 /* $OpenBSD: log.c,v 1.11 2015/01/08 05:34:21 bcook 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/socket.h> 20 21 #include <errno.h> 22 #include <netdb.h> 23 #include <pwd.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 31 #include "log.h" 32 33 #define TRACE_DEBUG 0x1 34 35 static int foreground; 36 static int verbose; 37 38 void vlog(int, const char *, va_list); 39 void logit(int, const char *, ...) 40 __attribute__((format (printf, 2, 3))); 41 42 43 void 44 log_init(int n_foreground) 45 { 46 extern char *__progname; 47 48 foreground = n_foreground; 49 if (! foreground) 50 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 51 52 tzset(); 53 } 54 55 void 56 log_verbose(int v) 57 { 58 verbose = v; 59 } 60 61 void 62 logit(int pri, const char *fmt, ...) 63 { 64 va_list ap; 65 66 va_start(ap, fmt); 67 vlog(pri, fmt, ap); 68 va_end(ap); 69 } 70 71 void 72 vlog(int pri, const char *fmt, va_list ap) 73 { 74 char *nfmt; 75 76 if (foreground) { 77 /* best effort in out of mem situations */ 78 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 79 vfprintf(stderr, fmt, ap); 80 fprintf(stderr, "\n"); 81 } else { 82 vfprintf(stderr, nfmt, ap); 83 free(nfmt); 84 } 85 fflush(stderr); 86 } else 87 vsyslog(pri, fmt, ap); 88 } 89 90 91 void 92 log_warn(const char *emsg, ...) 93 { 94 char *nfmt; 95 va_list ap; 96 97 /* best effort to even work in out of memory situations */ 98 if (emsg == NULL) 99 logit(LOG_CRIT, "%s", strerror(errno)); 100 else { 101 va_start(ap, emsg); 102 103 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 104 /* we tried it... */ 105 vlog(LOG_CRIT, emsg, ap); 106 logit(LOG_CRIT, "%s", strerror(errno)); 107 } else { 108 vlog(LOG_CRIT, nfmt, ap); 109 free(nfmt); 110 } 111 va_end(ap); 112 } 113 } 114 115 void 116 log_warnx(const char *emsg, ...) 117 { 118 va_list ap; 119 120 va_start(ap, emsg); 121 vlog(LOG_CRIT, emsg, ap); 122 va_end(ap); 123 } 124 125 void 126 log_info(const char *emsg, ...) 127 { 128 va_list ap; 129 130 va_start(ap, emsg); 131 vlog(LOG_INFO, emsg, ap); 132 va_end(ap); 133 } 134 135 void 136 log_debug(const char *emsg, ...) 137 { 138 va_list ap; 139 140 if (verbose & TRACE_DEBUG) { 141 va_start(ap, emsg); 142 vlog(LOG_DEBUG, emsg, ap); 143 va_end(ap); 144 } 145 } 146 147 void 148 log_trace(int mask, const char *emsg, ...) 149 { 150 va_list ap; 151 152 if (verbose & mask) { 153 va_start(ap, emsg); 154 vlog(LOG_DEBUG, emsg, ap); 155 va_end(ap); 156 } 157 } 158 159 static void 160 fatal_arg(const char *emsg, va_list ap) 161 { 162 #define FATALBUFSIZE 1024 163 static char ebuffer[FATALBUFSIZE]; 164 165 if (emsg == NULL) 166 (void)strlcpy(ebuffer, strerror(errno), sizeof ebuffer); 167 else { 168 if (errno) { 169 (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); 170 (void)strlcat(ebuffer, ": ", sizeof ebuffer); 171 (void)strlcat(ebuffer, strerror(errno), sizeof ebuffer); 172 } 173 else 174 (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); 175 } 176 logit(LOG_CRIT, "fatal: %s", ebuffer); 177 } 178 179 void 180 fatal(const char *emsg, ...) 181 { 182 va_list ap; 183 184 va_start(ap, emsg); 185 fatal_arg(emsg, ap); 186 va_end(ap); 187 exit(1); 188 } 189 190 void 191 fatalx(const char *emsg, ...) 192 { 193 va_list ap; 194 195 errno = 0; 196 va_start(ap, emsg); 197 fatal_arg(emsg, ap); 198 va_end(ap); 199 exit(1); 200 } 201 202 const char * 203 log_sockaddr(struct sockaddr *sa) 204 { 205 static char buf[NI_MAXHOST]; 206 207 if (getnameinfo(sa, SA_LEN(sa), buf, sizeof(buf), NULL, 0, 208 NI_NUMERICHOST)) 209 return ("(unknown)"); 210 else 211 return (buf); 212 } 213