1 /* $OpenBSD: log.c,v 1.1 2009/06/01 20:59:45 michele 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 "ldpd.h" 28 #include "log.h" 29 30 static const char * const procnames[] = { 31 "parent", 32 "ldpe", 33 "lde" 34 }; 35 36 int debug; 37 38 void logit(int, const char *, ...); 39 40 void 41 log_init(int n_debug) 42 { 43 extern char *__progname; 44 45 debug = n_debug; 46 47 if (!debug) 48 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 49 50 tzset(); 51 } 52 53 void 54 logit(int pri, const char *fmt, ...) 55 { 56 va_list ap; 57 58 va_start(ap, fmt); 59 vlog(pri, fmt, ap); 60 va_end(ap); 61 } 62 63 void 64 vlog(int pri, const char *fmt, va_list ap) 65 { 66 char *nfmt; 67 68 if (debug) { 69 /* best effort in out of mem situations */ 70 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 71 vfprintf(stderr, fmt, ap); 72 fprintf(stderr, "\n"); 73 } else { 74 vfprintf(stderr, nfmt, ap); 75 free(nfmt); 76 } 77 fflush(stderr); 78 } else 79 vsyslog(pri, fmt, ap); 80 } 81 82 void 83 log_warn(const char *emsg, ...) 84 { 85 char *nfmt; 86 va_list ap; 87 88 /* best effort to even work in out of memory situations */ 89 if (emsg == NULL) 90 logit(LOG_CRIT, "%s", strerror(errno)); 91 else { 92 va_start(ap, emsg); 93 94 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 95 /* we tried it... */ 96 vlog(LOG_CRIT, emsg, ap); 97 logit(LOG_CRIT, "%s", strerror(errno)); 98 } else { 99 vlog(LOG_CRIT, nfmt, ap); 100 free(nfmt); 101 } 102 va_end(ap); 103 } 104 } 105 106 void 107 log_warnx(const char *emsg, ...) 108 { 109 va_list ap; 110 111 va_start(ap, emsg); 112 vlog(LOG_CRIT, emsg, ap); 113 va_end(ap); 114 } 115 116 void 117 log_info(const char *emsg, ...) 118 { 119 va_list ap; 120 121 va_start(ap, emsg); 122 vlog(LOG_INFO, emsg, ap); 123 va_end(ap); 124 } 125 126 void 127 log_debug(const char *emsg, ...) 128 { 129 va_list ap; 130 131 if (debug) { 132 va_start(ap, emsg); 133 vlog(LOG_DEBUG, emsg, ap); 134 va_end(ap); 135 } 136 } 137 138 void 139 fatal(const char *emsg) 140 { 141 if (emsg == NULL) 142 logit(LOG_CRIT, "fatal in %s: %s", procnames[ldpd_process], 143 strerror(errno)); 144 else 145 if (errno) 146 logit(LOG_CRIT, "fatal in %s: %s: %s", 147 procnames[ldpd_process], emsg, strerror(errno)); 148 else 149 logit(LOG_CRIT, "fatal in %s: %s", 150 procnames[ldpd_process], emsg); 151 152 if (ldpd_process == PROC_MAIN) 153 exit(1); 154 else /* parent copes via SIGCHLD */ 155 _exit(1); 156 } 157 158 void 159 fatalx(const char *emsg) 160 { 161 errno = 0; 162 fatal(emsg); 163 } 164 165 /* names */ 166 const char * 167 nbr_state_name(int state) 168 { 169 switch (state) { 170 case NBR_STA_DOWN: 171 return ("DOWN"); 172 case NBR_STA_PRESENT: 173 return ("PRESENT"); 174 case NBR_STA_INITIAL: 175 return ("INITIALIZED"); 176 case NBR_STA_OPENREC: 177 return ("OPENREC"); 178 case NBR_STA_OPENSENT: 179 return ("OPENSENT"); 180 case NBR_STA_OPER: 181 return ("OPERATIONAL"); 182 case NBR_STA_ACTIVE: 183 return ("ACTIVE"); 184 default: 185 return ("UNKNW"); 186 } 187 } 188 189 const char * 190 if_state_name(int state) 191 { 192 switch (state) { 193 case IF_STA_DOWN: 194 return ("DOWN"); 195 case IF_STA_LOOPBACK: 196 return ("LOOP"); 197 case IF_STA_POINTTOPOINT: 198 return ("P2P"); 199 case IF_STA_DROTHER: 200 return ("OTHER"); 201 case IF_STA_ACTIVE: 202 return ("ACTIVE"); 203 default: 204 return ("UNKNW"); 205 } 206 } 207 208 const char * 209 if_type_name(enum iface_type type) 210 { 211 switch (type) { 212 case IF_TYPE_POINTOPOINT: 213 return ("POINTOPOINT"); 214 case IF_TYPE_BROADCAST: 215 return ("BROADCAST"); 216 case IF_TYPE_NBMA: 217 return ("NBMA"); 218 case IF_TYPE_POINTOMULTIPOINT: 219 return ("POINTOMULTIPOINT"); 220 case IF_TYPE_VIRTUALLINK: 221 return ("VIRTUALLINK"); 222 } 223 /* NOTREACHED */ 224 return ("UNKNOWN"); 225 } 226