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