blob: 837a670de1c63f08057eb115cd36d2d4950bfbbd [file] [log] [blame]
Damien Miller5ce662a1999-11-11 17:57:39 +11001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Shared versions of debug(), log(), etc.
Damien Miller5428f641999-11-25 11:54:57 +11003 */
Damien Miller5ce662a1999-11-11 17:57:39 +11004
5#include "includes.h"
Damien Miller5428f641999-11-25 11:54:57 +11006RCSID("$OpenBSD: log.c,v 1.6 1999/11/24 19:53:47 markus Exp $");
Damien Miller5ce662a1999-11-11 17:57:39 +11007
8#include "ssh.h"
9#include "xmalloc.h"
10
11/* Fatal messages. This function never returns. */
12
13void
Damien Miller95def091999-11-25 00:26:21 +110014fatal(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +110015{
Damien Miller95def091999-11-25 00:26:21 +110016 va_list args;
17 va_start(args, fmt);
18 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
19 va_end(args);
20 fatal_cleanup();
Damien Miller5ce662a1999-11-11 17:57:39 +110021}
22
23/* Error messages that should be logged. */
24
25void
Damien Miller95def091999-11-25 00:26:21 +110026error(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +110027{
Damien Miller95def091999-11-25 00:26:21 +110028 va_list args;
29 va_start(args, fmt);
30 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
31 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +110032}
33
34/* Log this message (information that usually should go to the log). */
35
36void
Damien Miller95def091999-11-25 00:26:21 +110037log(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +110038{
Damien Miller95def091999-11-25 00:26:21 +110039 va_list args;
40 va_start(args, fmt);
41 do_log(SYSLOG_LEVEL_INFO, fmt, args);
42 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +110043}
44
45/* More detailed messages (information that does not need to go to the log). */
46
47void
Damien Miller95def091999-11-25 00:26:21 +110048verbose(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +110049{
Damien Miller95def091999-11-25 00:26:21 +110050 va_list args;
51 va_start(args, fmt);
52 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
53 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +110054}
55
56/* Debugging messages that should not be logged during normal operation. */
57
58void
Damien Miller95def091999-11-25 00:26:21 +110059debug(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +110060{
Damien Miller95def091999-11-25 00:26:21 +110061 va_list args;
62 va_start(args, fmt);
63 do_log(SYSLOG_LEVEL_DEBUG, fmt, args);
64 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +110065}
66
67/* Fatal cleanup */
68
Damien Miller95def091999-11-25 00:26:21 +110069struct fatal_cleanup {
70 struct fatal_cleanup *next;
71 void (*proc) (void *);
72 void *context;
Damien Miller5ce662a1999-11-11 17:57:39 +110073};
74
75static struct fatal_cleanup *fatal_cleanups = NULL;
76
77/* Registers a cleanup function to be called by fatal() before exiting. */
78
79void
Damien Miller95def091999-11-25 00:26:21 +110080fatal_add_cleanup(void (*proc) (void *), void *context)
Damien Miller5ce662a1999-11-11 17:57:39 +110081{
Damien Miller95def091999-11-25 00:26:21 +110082 struct fatal_cleanup *cu;
Damien Miller5ce662a1999-11-11 17:57:39 +110083
Damien Miller95def091999-11-25 00:26:21 +110084 cu = xmalloc(sizeof(*cu));
85 cu->proc = proc;
86 cu->context = context;
87 cu->next = fatal_cleanups;
88 fatal_cleanups = cu;
Damien Miller5ce662a1999-11-11 17:57:39 +110089}
90
91/* Removes a cleanup frunction to be called at fatal(). */
92
93void
Damien Miller95def091999-11-25 00:26:21 +110094fatal_remove_cleanup(void (*proc) (void *context), void *context)
Damien Miller5ce662a1999-11-11 17:57:39 +110095{
Damien Miller95def091999-11-25 00:26:21 +110096 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 }
Damien Miller5ce662a1999-11-11 17:57:39 +1100105 }
Damien Miller95def091999-11-25 00:26:21 +1100106 fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n",
107 (unsigned long) proc, (unsigned long) context);
Damien Miller5ce662a1999-11-11 17:57:39 +1100108}
109
110/* Cleanup and exit */
111void
112fatal_cleanup(void)
113{
Damien Miller95def091999-11-25 00:26:21 +1100114 struct fatal_cleanup *cu, *next_cu;
115 static int called = 0;
Damien Miller5ce662a1999-11-11 17:57:39 +1100116
Damien Miller95def091999-11-25 00:26:21 +1100117 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);
Damien Miller5ce662a1999-11-11 17:57:39 +1100128}
Damien Miller6162d121999-11-21 13:23:52 +1100129
130/* textual representation of log-facilities/levels */
131
Damien Miller95def091999-11-25 00:26:21 +1100132static 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 }
Damien Miller6162d121999-11-21 13:23:52 +1100148};
149
Damien Miller95def091999-11-25 00:26:21 +1100150static struct {
151 const char *name;
152 LogLevel val;
Damien Miller6162d121999-11-21 13:23:52 +1100153} log_levels[] =
154{
Damien Miller95def091999-11-25 00:26:21 +1100155 { "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 }
Damien Miller6162d121999-11-21 13:23:52 +1100162};
163
164SyslogFacility
165log_facility_number(char *name)
166{
Damien Miller95def091999-11-25 00:26:21 +1100167 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;
Damien Miller6162d121999-11-21 13:23:52 +1100173}
174
175LogLevel
176log_level_number(char *name)
177{
Damien Miller95def091999-11-25 00:26:21 +1100178 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;
Damien Miller6162d121999-11-21 13:23:52 +1100184}