]>
Commit | Line | Data |
---|---|---|
6a17f9c2 | 1 | /* |
5260325f | 2 | * Shared versions of debug(), log(), etc. |
aa3378df | 3 | */ |
6a17f9c2 | 4 | |
5 | #include "includes.h" | |
aa3378df | 6 | RCSID("$OpenBSD: log.c,v 1.6 1999/11/24 19:53:47 markus Exp $"); |
6a17f9c2 | 7 | |
8 | #include "ssh.h" | |
9 | #include "xmalloc.h" | |
10 | ||
11 | /* Fatal messages. This function never returns. */ | |
12 | ||
13 | void | |
5260325f | 14 | fatal(const char *fmt,...) |
6a17f9c2 | 15 | { |
5260325f | 16 | va_list args; |
17 | va_start(args, fmt); | |
18 | do_log(SYSLOG_LEVEL_FATAL, fmt, args); | |
19 | va_end(args); | |
20 | fatal_cleanup(); | |
6a17f9c2 | 21 | } |
22 | ||
23 | /* Error messages that should be logged. */ | |
24 | ||
25 | void | |
5260325f | 26 | error(const char *fmt,...) |
6a17f9c2 | 27 | { |
5260325f | 28 | va_list args; |
29 | va_start(args, fmt); | |
30 | do_log(SYSLOG_LEVEL_ERROR, fmt, args); | |
31 | va_end(args); | |
6a17f9c2 | 32 | } |
33 | ||
34 | /* Log this message (information that usually should go to the log). */ | |
35 | ||
36 | void | |
5260325f | 37 | log(const char *fmt,...) |
6a17f9c2 | 38 | { |
5260325f | 39 | va_list args; |
40 | va_start(args, fmt); | |
41 | do_log(SYSLOG_LEVEL_INFO, fmt, args); | |
42 | va_end(args); | |
6a17f9c2 | 43 | } |
44 | ||
45 | /* More detailed messages (information that does not need to go to the log). */ | |
46 | ||
47 | void | |
5260325f | 48 | verbose(const char *fmt,...) |
6a17f9c2 | 49 | { |
5260325f | 50 | va_list args; |
51 | va_start(args, fmt); | |
52 | do_log(SYSLOG_LEVEL_VERBOSE, fmt, args); | |
53 | va_end(args); | |
6a17f9c2 | 54 | } |
55 | ||
56 | /* Debugging messages that should not be logged during normal operation. */ | |
57 | ||
58 | void | |
5260325f | 59 | debug(const char *fmt,...) |
6a17f9c2 | 60 | { |
5260325f | 61 | va_list args; |
62 | va_start(args, fmt); | |
63 | do_log(SYSLOG_LEVEL_DEBUG, fmt, args); | |
64 | va_end(args); | |
6a17f9c2 | 65 | } |
66 | ||
67 | /* Fatal cleanup */ | |
68 | ||
5260325f | 69 | struct fatal_cleanup { |
70 | struct fatal_cleanup *next; | |
71 | void (*proc) (void *); | |
72 | void *context; | |
6a17f9c2 | 73 | }; |
74 | ||
75 | static struct fatal_cleanup *fatal_cleanups = NULL; | |
76 | ||
77 | /* Registers a cleanup function to be called by fatal() before exiting. */ | |
78 | ||
79 | void | |
5260325f | 80 | fatal_add_cleanup(void (*proc) (void *), void *context) |
6a17f9c2 | 81 | { |
5260325f | 82 | struct fatal_cleanup *cu; |
6a17f9c2 | 83 | |
5260325f | 84 | cu = xmalloc(sizeof(*cu)); |
85 | cu->proc = proc; | |
86 | cu->context = context; | |
87 | cu->next = fatal_cleanups; | |
88 | fatal_cleanups = cu; | |
6a17f9c2 | 89 | } |
90 | ||
91 | /* Removes a cleanup frunction to be called at fatal(). */ | |
92 | ||
93 | void | |
5260325f | 94 | fatal_remove_cleanup(void (*proc) (void *context), void *context) |
6a17f9c2 | 95 | { |
5260325f | 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 | } | |
6a17f9c2 | 105 | } |
5260325f | 106 | fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n", |
107 | (unsigned long) proc, (unsigned long) context); | |
6a17f9c2 | 108 | } |
109 | ||
110 | /* Cleanup and exit */ | |
111 | void | |
112 | fatal_cleanup(void) | |
113 | { | |
5260325f | 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); | |
6a17f9c2 | 128 | } |
9d6b7add | 129 | |
130 | /* textual representation of log-facilities/levels */ | |
131 | ||
5260325f | 132 | static 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 } | |
9d6b7add | 148 | }; |
149 | ||
5260325f | 150 | static struct { |
151 | const char *name; | |
152 | LogLevel val; | |
9d6b7add | 153 | } log_levels[] = |
154 | { | |
5260325f | 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 } | |
9d6b7add | 162 | }; |
163 | ||
164 | SyslogFacility | |
165 | log_facility_number(char *name) | |
166 | { | |
5260325f | 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; | |
9d6b7add | 173 | } |
174 | ||
175 | LogLevel | |
176 | log_level_number(char *name) | |
177 | { | |
5260325f | 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; | |
9d6b7add | 184 | } |