1 /* $OpenBSD: log.c,v 1.2 2010/11/10 07:32:50 martinh 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/socket.h> 22 #include <sys/time.h> 23 24 #include <errno.h> 25 #include <netdb.h> 26 #include <stdarg.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <syslog.h> 31 #include <time.h> 32 #include <unistd.h> 33 34 #include "ldapd.h" 35 36 int debug; 37 int verbose; 38 39 void 40 log_init(int n_debug) 41 { 42 extern char *__progname; 43 44 debug = n_debug; 45 46 if (!debug) 47 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 48 49 tzset(); 50 } 51 52 void 53 log_verbose(int v) 54 { 55 verbose = v; 56 } 57 58 void 59 logit(int pri, const char *fmt, ...) 60 { 61 va_list ap; 62 63 va_start(ap, fmt); 64 vlog(pri, fmt, ap); 65 va_end(ap); 66 } 67 68 void 69 vlog(int pri, const char *fmt, va_list ap) 70 { 71 char datebuf[24]; 72 struct timeval tv; 73 struct tm *tm; 74 char *nfmt; 75 size_t rc; 76 time_t now; 77 78 if (debug) { 79 gettimeofday(&tv, NULL); 80 now = tv.tv_sec; 81 tm = localtime(&now); 82 rc = strftime(datebuf, sizeof(datebuf), "%b %e %H:%M:%S", tm); 83 if (rc == 0) 84 datebuf[0] = 0; 85 fprintf(stderr, "%s.%03li [%i] ", datebuf, tv.tv_usec / 1000, getpid()); 86 87 /* best effort in out of mem situations */ 88 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 89 vfprintf(stderr, fmt, ap); 90 fprintf(stderr, "\n"); 91 } else { 92 vfprintf(stderr, nfmt, ap); 93 free(nfmt); 94 } 95 fflush(stderr); 96 } else 97 vsyslog(pri, fmt, ap); 98 } 99 100 void 101 log_warn(const char *emsg, ...) 102 { 103 char *nfmt; 104 va_list ap; 105 106 /* best effort to even work in out of memory situations */ 107 if (emsg == NULL) 108 logit(LOG_CRIT, "%s", strerror(errno)); 109 else { 110 va_start(ap, emsg); 111 112 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 113 /* we tried it... */ 114 vlog(LOG_CRIT, emsg, ap); 115 logit(LOG_CRIT, "%s", strerror(errno)); 116 } else { 117 vlog(LOG_CRIT, nfmt, ap); 118 free(nfmt); 119 } 120 va_end(ap); 121 } 122 } 123 124 void 125 log_warnx(const char *emsg, ...) 126 { 127 va_list ap; 128 129 va_start(ap, emsg); 130 vlog(LOG_CRIT, emsg, ap); 131 va_end(ap); 132 } 133 134 void 135 log_info(const char *emsg, ...) 136 { 137 va_list ap; 138 139 va_start(ap, emsg); 140 vlog(LOG_INFO, emsg, ap); 141 va_end(ap); 142 } 143 144 void 145 log_debug(const char *emsg, ...) 146 { 147 va_list ap; 148 149 if (verbose) { 150 va_start(ap, emsg); 151 vlog(LOG_DEBUG, emsg, ap); 152 va_end(ap); 153 } 154 } 155 156 void 157 fatal(const char *emsg) 158 { 159 if (emsg == NULL) 160 logit(LOG_CRIT, "fatal: %s", strerror(errno)); 161 else 162 if (errno) 163 logit(LOG_CRIT, "fatal: %s: %s", 164 emsg, strerror(errno)); 165 else 166 logit(LOG_CRIT, "fatal: %s", emsg); 167 168 exit(1); 169 } 170 171 void 172 fatalx(const char *emsg) 173 { 174 errno = 0; 175 fatal(emsg); 176 } 177 178 const char * 179 print_host(struct sockaddr_storage *ss, char *buf, size_t len) 180 { 181 if (getnameinfo((struct sockaddr *)ss, ss->ss_len, 182 buf, len, NULL, 0, NI_NUMERICHOST) != 0) { 183 buf[0] = '\0'; 184 return (NULL); 185 } 186 return (buf); 187 } 188