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