1 /* $OpenBSD: log.c,v 1.12 2016/09/02 14:06:35 benno Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <errno.h> 21 #include <stdarg.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <syslog.h> 26 #include <time.h> 27 #include <unistd.h> 28 29 #include "log.h" 30 #include "ospf6d.h" 31 32 int debug; 33 int verbose; 34 const char *log_procname; 35 36 void 37 log_init(int n_debug) 38 { 39 extern char *__progname; 40 41 debug = n_debug; 42 43 if (!debug) 44 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 45 46 tzset(); 47 } 48 49 void 50 log_verbose(int v) 51 { 52 verbose = v; 53 } 54 55 void 56 logit(int pri, const char *fmt, ...) 57 { 58 va_list ap; 59 60 va_start(ap, fmt); 61 vlog(pri, fmt, ap); 62 va_end(ap); 63 } 64 65 void 66 vlog(int pri, const char *fmt, va_list ap) 67 { 68 char *nfmt; 69 70 if (debug) { 71 /* best effort in out of mem situations */ 72 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 73 vfprintf(stderr, fmt, ap); 74 fprintf(stderr, "\n"); 75 } else { 76 vfprintf(stderr, nfmt, ap); 77 free(nfmt); 78 } 79 fflush(stderr); 80 } else 81 vsyslog(pri, fmt, ap); 82 } 83 84 void 85 log_warn(const char *emsg, ...) 86 { 87 char *nfmt; 88 va_list ap; 89 90 /* best effort to even work in out of memory situations */ 91 if (emsg == NULL) 92 logit(LOG_CRIT, "%s", strerror(errno)); 93 else { 94 va_start(ap, emsg); 95 96 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 97 /* we tried it... */ 98 vlog(LOG_CRIT, emsg, ap); 99 logit(LOG_CRIT, "%s", strerror(errno)); 100 } else { 101 vlog(LOG_CRIT, nfmt, ap); 102 free(nfmt); 103 } 104 va_end(ap); 105 } 106 } 107 108 void 109 log_warnx(const char *emsg, ...) 110 { 111 va_list ap; 112 113 va_start(ap, emsg); 114 vlog(LOG_CRIT, emsg, ap); 115 va_end(ap); 116 } 117 118 void 119 log_info(const char *emsg, ...) 120 { 121 va_list ap; 122 123 va_start(ap, emsg); 124 vlog(LOG_INFO, emsg, ap); 125 va_end(ap); 126 } 127 128 void 129 log_debug(const char *emsg, ...) 130 { 131 va_list ap; 132 133 if (verbose) { 134 va_start(ap, emsg); 135 vlog(LOG_DEBUG, emsg, ap); 136 va_end(ap); 137 } 138 } 139 140 void 141 fatal(const char *emsg) 142 { 143 if (emsg == NULL) 144 logit(LOG_CRIT, "fatal in %s: %s", log_procname, 145 strerror(errno)); 146 else 147 if (errno) 148 logit(LOG_CRIT, "fatal in %s: %s: %s", 149 log_procname, emsg, strerror(errno)); 150 else 151 logit(LOG_CRIT, "fatal in %s: %s", 152 log_procname, emsg); 153 154 if (ospfd_process == PROC_MAIN) 155 exit(1); 156 else /* parent copes via SIGCHLD */ 157 _exit(1); 158 } 159 160 void 161 fatalx(const char *emsg) 162 { 163 errno = 0; 164 fatal(emsg); 165 } 166