1 /* $OpenBSD: log.c,v 1.3 2009/11/02 20:28:49 claudio 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 USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <errno.h> 20 #include <stdarg.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <syslog.h> 25 #include <unistd.h> 26 27 #include "ripd.h" 28 #include "log.h" 29 30 static const char * const procnames[] = { 31 "parent", 32 "ripe", 33 "rde" 34 }; 35 36 int debug; 37 int verbose; 38 39 void logit(int, const char *, ...); 40 41 void 42 log_init(int n_debug) 43 { 44 extern char *__progname; 45 46 debug = n_debug; 47 verbose = n_debug; 48 49 if (!debug) 50 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 51 52 tzset(); 53 } 54 55 void 56 log_verbose(int v) 57 { 58 verbose = v; 59 } 60 61 void 62 logit(int pri, const char *fmt, ...) 63 { 64 va_list ap; 65 66 va_start(ap, fmt); 67 vlog(pri, fmt, ap); 68 va_end(ap); 69 } 70 71 void 72 vlog(int pri, const char *fmt, va_list ap) 73 { 74 char *nfmt; 75 76 if (debug) { 77 /* best effort in out of mem situations */ 78 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 79 vfprintf(stderr, fmt, ap); 80 fprintf(stderr, "\n"); 81 } else { 82 vfprintf(stderr, nfmt, ap); 83 free(nfmt); 84 } 85 fflush(stderr); 86 } else 87 vsyslog(pri, fmt, ap); 88 } 89 90 void 91 log_warn(const char *emsg, ...) 92 { 93 char *nfmt; 94 va_list ap; 95 96 /* best effort to even work in out of memory situations */ 97 if (emsg == NULL) 98 logit(LOG_CRIT, "%s", strerror(errno)); 99 else { 100 va_start(ap, emsg); 101 102 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 103 /* we tried it... */ 104 vlog(LOG_CRIT, emsg, ap); 105 logit(LOG_CRIT, "%s", strerror(errno)); 106 } else { 107 vlog(LOG_CRIT, nfmt, ap); 108 free(nfmt); 109 } 110 va_end(ap); 111 } 112 } 113 114 void 115 log_warnx(const char *emsg, ...) 116 { 117 va_list ap; 118 119 va_start(ap, emsg); 120 vlog(LOG_CRIT, emsg, ap); 121 va_end(ap); 122 } 123 124 void 125 log_info(const char *emsg, ...) 126 { 127 va_list ap; 128 129 va_start(ap, emsg); 130 vlog(LOG_INFO, emsg, ap); 131 va_end(ap); 132 } 133 134 void 135 log_debug(const char *emsg, ...) 136 { 137 va_list ap; 138 139 if (verbose) { 140 va_start(ap, emsg); 141 vlog(LOG_DEBUG, emsg, ap); 142 va_end(ap); 143 } 144 } 145 146 void 147 fatal(const char *emsg) 148 { 149 if (emsg == NULL) 150 logit(LOG_CRIT, "fatal in %s: %s", procnames[ripd_process], 151 strerror(errno)); 152 else 153 if (errno) 154 logit(LOG_CRIT, "fatal in %s: %s: %s", 155 procnames[ripd_process], emsg, strerror(errno)); 156 else 157 logit(LOG_CRIT, "fatal in %s: %s", 158 procnames[ripd_process], emsg); 159 160 if (ripd_process == PROC_MAIN) 161 exit(1); 162 else /* parent copes via SIGCHLD */ 163 _exit(1); 164 } 165 166 void 167 fatalx(const char *emsg) 168 { 169 errno = 0; 170 fatal(emsg); 171 } 172 173 /* names */ 174 const char * 175 nbr_state_name(int state) 176 { 177 switch (state) { 178 case NBR_STA_DOWN: 179 return ("DOWN"); 180 case NBR_STA_REQ_RCVD: 181 return ("REQUEST RCVD"); 182 case NBR_STA_ACTIVE: 183 return ("ACTIVE"); 184 default: 185 return ("UNKNOWN"); 186 } 187 } 188 189 const char * 190 if_type_name(enum iface_type type) 191 { 192 switch (type) { 193 case IF_TYPE_POINTOPOINT: 194 return ("POINTOPOINT"); 195 case IF_TYPE_BROADCAST: 196 return ("BROADCAST"); 197 case IF_TYPE_NBMA: 198 return ("NBMA"); 199 case IF_TYPE_POINTOMULTIPOINT: 200 return ("POINTOMULTIPOINT"); 201 } 202 /* NOTREACHED */ 203 return ("UNKNOWN"); 204 } 205 206 const char * 207 if_auth_name(enum auth_type type) 208 { 209 switch (type) { 210 case AUTH_NONE: 211 return ("none"); 212 case AUTH_SIMPLE: 213 return ("simple"); 214 case AUTH_CRYPT: 215 return ("crypt"); 216 } 217 /* NOTREACHED */ 218 return ("unknown"); 219 } 220 221 const char * 222 if_state_name(int state) 223 { 224 switch (state) { 225 case IF_STA_DOWN: 226 return ("DOWN"); 227 case IF_STA_ACTIVE: 228 return ("ACTIVE"); 229 default: 230 return ("UNKNOWN"); 231 } 232 } 233