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