1 /* $OpenBSD: log.c,v 1.4 2015/01/16 06:39:58 deraadt 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/queue.h> 20 #include <sys/socket.h> 21 #include <sys/tree.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 <event.h> 30 31 #include "iked.h" 32 33 int debug; 34 int verbose; 35 36 void vlog(int, const char *, va_list); 37 void logit(int, const char *, ...); 38 39 void 40 log_init(int n_debug) 41 { 42 extern char *__progname; 43 44 debug = n_debug; 45 verbose = n_debug; 46 47 if (!debug) 48 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 49 50 tzset(); 51 } 52 53 void 54 log_verbose(int v) 55 { 56 verbose = v; 57 } 58 59 void 60 logit(int pri, const char *fmt, ...) 61 { 62 va_list ap; 63 64 va_start(ap, fmt); 65 vlog(pri, fmt, ap); 66 va_end(ap); 67 } 68 69 void 70 vlog(int pri, const char *fmt, va_list ap) 71 { 72 char *nfmt; 73 74 if (debug) { 75 /* best effort in out of mem situations */ 76 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 77 vfprintf(stderr, fmt, ap); 78 fprintf(stderr, "\n"); 79 } else { 80 vfprintf(stderr, nfmt, ap); 81 free(nfmt); 82 } 83 fflush(stderr); 84 } else 85 vsyslog(pri, fmt, ap); 86 } 87 88 89 void 90 log_warn(const char *emsg, ...) 91 { 92 char *nfmt; 93 va_list ap; 94 95 /* best effort to even work in out of memory situations */ 96 if (emsg == NULL) 97 logit(LOG_CRIT, "%s", strerror(errno)); 98 else { 99 va_start(ap, emsg); 100 101 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 102 /* we tried it... */ 103 vlog(LOG_CRIT, emsg, ap); 104 logit(LOG_CRIT, "%s", strerror(errno)); 105 } else { 106 vlog(LOG_CRIT, nfmt, ap); 107 free(nfmt); 108 } 109 va_end(ap); 110 } 111 } 112 113 void 114 log_warnx(const char *emsg, ...) 115 { 116 va_list ap; 117 118 va_start(ap, emsg); 119 vlog(LOG_CRIT, emsg, ap); 120 va_end(ap); 121 } 122 123 void 124 log_info(const char *emsg, ...) 125 { 126 va_list ap; 127 128 va_start(ap, emsg); 129 vlog(LOG_INFO, emsg, ap); 130 va_end(ap); 131 } 132 133 void 134 log_debug(const char *emsg, ...) 135 { 136 va_list ap; 137 138 if (verbose > 1) { 139 va_start(ap, emsg); 140 vlog(LOG_DEBUG, emsg, ap); 141 va_end(ap); 142 } 143 } 144 145 void 146 print_debug(const char *emsg, ...) 147 { 148 va_list ap; 149 150 if (debug && verbose > 2) { 151 va_start(ap, emsg); 152 vfprintf(stderr, emsg, ap); 153 va_end(ap); 154 } 155 } 156 157 void 158 print_verbose(const char *emsg, ...) 159 { 160 va_list ap; 161 162 if (verbose) { 163 va_start(ap, emsg); 164 vfprintf(stderr, emsg, ap); 165 va_end(ap); 166 } 167 } 168 169 void 170 fatal(const char *emsg) 171 { 172 if (emsg == NULL) 173 logit(LOG_CRIT, "fatal: %s", strerror(errno)); 174 else { 175 if (errno) 176 logit(LOG_CRIT, "fatal: %s: %s", 177 emsg, strerror(errno)); 178 else 179 logit(LOG_CRIT, "fatal: %s", emsg); 180 } 181 182 exit(1); 183 } 184 185 void 186 fatalx(const char *emsg) 187 { 188 errno = 0; 189 fatal(emsg); 190 } 191