log.c revision 57429
1/* 2 * Shared versions of debug(), log(), etc. 3 */ 4 5#include "includes.h" 6RCSID("$OpenBSD: log.c,v 1.7 2000/01/04 00:07:59 markus Exp $"); 7 8#include "ssh.h" 9#include "xmalloc.h" 10 11/* Fatal messages. This function never returns. */ 12 13void 14fatal(const char *fmt,...) 15{ 16 va_list args; 17 va_start(args, fmt); 18 do_log(SYSLOG_LEVEL_FATAL, fmt, args); 19 va_end(args); 20 fatal_cleanup(); 21} 22 23/* Error messages that should be logged. */ 24 25void 26error(const char *fmt,...) 27{ 28 va_list args; 29 va_start(args, fmt); 30 do_log(SYSLOG_LEVEL_ERROR, fmt, args); 31 va_end(args); 32} 33 34/* Log this message (information that usually should go to the log). */ 35 36void 37log(const char *fmt,...) 38{ 39 va_list args; 40 va_start(args, fmt); 41 do_log(SYSLOG_LEVEL_INFO, fmt, args); 42 va_end(args); 43} 44 45/* More detailed messages (information that does not need to go to the log). */ 46 47void 48verbose(const char *fmt,...) 49{ 50 va_list args; 51 va_start(args, fmt); 52 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args); 53 va_end(args); 54} 55 56/* Debugging messages that should not be logged during normal operation. */ 57 58void 59debug(const char *fmt,...) 60{ 61 va_list args; 62 va_start(args, fmt); 63 do_log(SYSLOG_LEVEL_DEBUG, fmt, args); 64 va_end(args); 65} 66 67/* Fatal cleanup */ 68 69struct fatal_cleanup { 70 struct fatal_cleanup *next; 71 void (*proc) (void *); 72 void *context; 73}; 74 75static struct fatal_cleanup *fatal_cleanups = NULL; 76 77/* Registers a cleanup function to be called by fatal() before exiting. */ 78 79void 80fatal_add_cleanup(void (*proc) (void *), void *context) 81{ 82 struct fatal_cleanup *cu; 83 84 cu = xmalloc(sizeof(*cu)); 85 cu->proc = proc; 86 cu->context = context; 87 cu->next = fatal_cleanups; 88 fatal_cleanups = cu; 89} 90 91/* Removes a cleanup frunction to be called at fatal(). */ 92 93void 94fatal_remove_cleanup(void (*proc) (void *context), void *context) 95{ 96 struct fatal_cleanup **cup, *cu; 97 98 for (cup = &fatal_cleanups; *cup; cup = &cu->next) { 99 cu = *cup; 100 if (cu->proc == proc && cu->context == context) { 101 *cup = cu->next; 102 xfree(cu); 103 return; 104 } 105 } 106 fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n", 107 (unsigned long) proc, (unsigned long) context); 108} 109 110/* Cleanup and exit */ 111void 112fatal_cleanup(void) 113{ 114 struct fatal_cleanup *cu, *next_cu; 115 static int called = 0; 116 117 if (called) 118 exit(255); 119 called = 1; 120 /* Call cleanup functions. */ 121 for (cu = fatal_cleanups; cu; cu = next_cu) { 122 next_cu = cu->next; 123 debug("Calling cleanup 0x%lx(0x%lx)", 124 (unsigned long) cu->proc, (unsigned long) cu->context); 125 (*cu->proc) (cu->context); 126 } 127 exit(255); 128} 129 130/* textual representation of log-facilities/levels */ 131 132static struct { 133 const char *name; 134 SyslogFacility val; 135} log_facilities[] = { 136 { "DAEMON", SYSLOG_FACILITY_DAEMON }, 137 { "USER", SYSLOG_FACILITY_USER }, 138 { "AUTH", SYSLOG_FACILITY_AUTH }, 139 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 }, 140 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 }, 141 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 }, 142 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 }, 143 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 }, 144 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 }, 145 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 }, 146 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 }, 147 { NULL, 0 } 148}; 149 150static struct { 151 const char *name; 152 LogLevel val; 153} log_levels[] = 154{ 155 { "QUIET", SYSLOG_LEVEL_QUIET }, 156 { "FATAL", SYSLOG_LEVEL_FATAL }, 157 { "ERROR", SYSLOG_LEVEL_ERROR }, 158 { "INFO", SYSLOG_LEVEL_INFO }, 159 { "VERBOSE", SYSLOG_LEVEL_VERBOSE }, 160 { "DEBUG", SYSLOG_LEVEL_DEBUG }, 161 { NULL, 0 } 162}; 163 164SyslogFacility 165log_facility_number(char *name) 166{ 167 int i; 168 if (name != NULL) 169 for (i = 0; log_facilities[i].name; i++) 170 if (strcasecmp(log_facilities[i].name, name) == 0) 171 return log_facilities[i].val; 172 return (SyslogFacility) - 1; 173} 174 175LogLevel 176log_level_number(char *name) 177{ 178 int i; 179 if (name != NULL) 180 for (i = 0; log_levels[i].name; i++) 181 if (strcasecmp(log_levels[i].name, name) == 0) 182 return log_levels[i].val; 183 return (LogLevel) - 1; 184} 185