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