1 | /* |
---|
2 | |
---|
3 | log-client.c |
---|
4 | |
---|
5 | Author: Tatu Ylonen <ylo@cs.hut.fi> |
---|
6 | |
---|
7 | Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
---|
8 | All rights reserved |
---|
9 | |
---|
10 | Created: Mon Mar 20 21:13:40 1995 ylo |
---|
11 | |
---|
12 | Client-side versions of debug(), log_msg(), etc. These print to stderr. |
---|
13 | |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | * $Id: log-client.c,v 1.1.1.2 1999-03-08 17:43:24 danw Exp $ |
---|
18 | * $Log: not supported by cvs2svn $ |
---|
19 | * Revision 1.6 1998/05/23 20:21:37 kivinen |
---|
20 | * Changed () -> (void). |
---|
21 | * |
---|
22 | * Revision 1.5 1996/12/04 18:16:15 ttsalo |
---|
23 | * debug() can now prefix every message with local hostname |
---|
24 | * |
---|
25 | * Revision 1.4 1996/10/29 22:37:59 kivinen |
---|
26 | * log -> log_msg. |
---|
27 | * |
---|
28 | * Revision 1.3 1996/04/26 00:40:12 ylo |
---|
29 | * Changed \n\r to \r\n. They are always written in this order, |
---|
30 | * and deviating from this is likely to cause problems. |
---|
31 | * |
---|
32 | * Revision 1.2 1996/04/22 23:46:53 huima |
---|
33 | * Changed '\n' to '\n\r'. \n alone isn't always enough. |
---|
34 | * |
---|
35 | * Revision 1.1.1.1 1996/02/18 21:38:12 ylo |
---|
36 | * Imported ssh-1.2.13. |
---|
37 | * |
---|
38 | * Revision 1.3 1995/08/21 23:24:44 ylo |
---|
39 | * Added support for log_quiet. |
---|
40 | * |
---|
41 | * Revision 1.2 1995/07/13 01:25:51 ylo |
---|
42 | * Removed "Last modified" header. |
---|
43 | * Added cvs log. |
---|
44 | * |
---|
45 | * $Endlog$ |
---|
46 | */ |
---|
47 | |
---|
48 | #include "includes.h" |
---|
49 | #include "xmalloc.h" |
---|
50 | #include "ssh.h" |
---|
51 | |
---|
52 | static int log_debug = 0; |
---|
53 | static int log_quiet = 0; |
---|
54 | |
---|
55 | /* Name of the host we are running on. This is used in debug() |
---|
56 | and initialized in log_init() */ |
---|
57 | #ifdef HAVE_GETHOSTNAME |
---|
58 | static char local_hostname[257]; |
---|
59 | #endif |
---|
60 | |
---|
61 | void log_init(char *av0, int on_stderr, int debug, int quiet, |
---|
62 | SyslogFacility facility) |
---|
63 | { |
---|
64 | log_debug = debug; |
---|
65 | log_quiet = quiet; |
---|
66 | |
---|
67 | /* Get our own hostname */ |
---|
68 | #ifdef HAVE_GETHOSTNAME |
---|
69 | if (gethostname(local_hostname, sizeof(local_hostname)) < 0) |
---|
70 | fatal("gethostname: %.100s", strerror(errno)); |
---|
71 | #endif |
---|
72 | } |
---|
73 | |
---|
74 | void log_msg(const char *fmt, ...) |
---|
75 | { |
---|
76 | va_list args; |
---|
77 | |
---|
78 | if (log_quiet) |
---|
79 | return; |
---|
80 | va_start(args, fmt); |
---|
81 | vfprintf(stderr, fmt, args); |
---|
82 | fprintf(stderr, "\r\n"); |
---|
83 | va_end(args); |
---|
84 | } |
---|
85 | |
---|
86 | void log_severity(SyslogSeverity severity, const char *fmt, ...) |
---|
87 | { |
---|
88 | va_list args; |
---|
89 | |
---|
90 | if (log_quiet) |
---|
91 | return; |
---|
92 | va_start(args, fmt); |
---|
93 | vfprintf(stderr, fmt, args); |
---|
94 | fprintf(stderr, "\r\n"); |
---|
95 | va_end(args); |
---|
96 | } |
---|
97 | |
---|
98 | void debug(const char *fmt, ...) |
---|
99 | { |
---|
100 | va_list args; |
---|
101 | if (log_quiet || !log_debug) |
---|
102 | return; |
---|
103 | #if defined(HAVE_GETHOSTNAME) && defined(LOCAL_HOSTNAME_IN_DEBUG) |
---|
104 | fprintf(stderr, "%s: ", local_hostname); |
---|
105 | #endif |
---|
106 | va_start(args, fmt); |
---|
107 | vfprintf(stderr, fmt, args); |
---|
108 | fprintf(stderr, "\r\n"); |
---|
109 | va_end(args); |
---|
110 | } |
---|
111 | |
---|
112 | void error(const char *fmt, ...) |
---|
113 | { |
---|
114 | va_list args; |
---|
115 | if (log_quiet) |
---|
116 | return; |
---|
117 | va_start(args, fmt); |
---|
118 | vfprintf(stderr, fmt, args); |
---|
119 | fprintf(stderr, "\r\n"); |
---|
120 | va_end(args); |
---|
121 | } |
---|
122 | |
---|
123 | struct fatal_cleanup |
---|
124 | { |
---|
125 | struct fatal_cleanup *next; |
---|
126 | void (*proc)(void *); |
---|
127 | void *context; |
---|
128 | }; |
---|
129 | |
---|
130 | static struct fatal_cleanup *fatal_cleanups = NULL; |
---|
131 | |
---|
132 | /* Registers a cleanup function to be called by fatal() before exiting. */ |
---|
133 | |
---|
134 | void fatal_add_cleanup(void (*proc)(void *), void *context) |
---|
135 | { |
---|
136 | struct fatal_cleanup *cu; |
---|
137 | |
---|
138 | cu = xmalloc(sizeof(*cu)); |
---|
139 | cu->proc = proc; |
---|
140 | cu->context = context; |
---|
141 | cu->next = fatal_cleanups; |
---|
142 | fatal_cleanups = cu; |
---|
143 | } |
---|
144 | |
---|
145 | /* Removes a cleanup frunction to be called at fatal(). */ |
---|
146 | |
---|
147 | void fatal_remove_cleanup(void (*proc)(void *context), void *context) |
---|
148 | { |
---|
149 | struct fatal_cleanup **cup, *cu; |
---|
150 | |
---|
151 | for (cup = &fatal_cleanups; *cup; cup = &cu->next) |
---|
152 | { |
---|
153 | cu = *cup; |
---|
154 | if (cu->proc == proc && cu->context == context) |
---|
155 | { |
---|
156 | *cup = cu->next; |
---|
157 | xfree(cu); |
---|
158 | return; |
---|
159 | } |
---|
160 | } |
---|
161 | fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n", |
---|
162 | (unsigned long)proc, (unsigned long)context); |
---|
163 | } |
---|
164 | |
---|
165 | /* Executes fatal() cleanups. */ |
---|
166 | |
---|
167 | static void do_fatal_cleanups(void) |
---|
168 | { |
---|
169 | struct fatal_cleanup *cu, *next_cu; |
---|
170 | static int fatal_called = 0; |
---|
171 | |
---|
172 | if (!fatal_called) |
---|
173 | { |
---|
174 | fatal_called = 1; |
---|
175 | |
---|
176 | /* Call cleanup functions. */ |
---|
177 | for (cu = fatal_cleanups; cu; cu = next_cu) |
---|
178 | { |
---|
179 | next_cu = cu->next; |
---|
180 | (*cu->proc)(cu->context); |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | /* Function to display an error message and exit. This is in this file because |
---|
186 | this needs to restore terminal modes before exiting. See log-client.c |
---|
187 | for other related functions. */ |
---|
188 | |
---|
189 | void fatal(const char *fmt, ...) |
---|
190 | { |
---|
191 | va_list args; |
---|
192 | |
---|
193 | do_fatal_cleanups(); |
---|
194 | |
---|
195 | va_start(args, fmt); |
---|
196 | vfprintf(stderr, fmt, args); |
---|
197 | fprintf(stderr, "\n"); |
---|
198 | va_end(args); |
---|
199 | exit(255); |
---|
200 | } |
---|
201 | |
---|
202 | void fatal_severity(SyslogSeverity severity, const char *fmt, ...) |
---|
203 | { |
---|
204 | va_list args; |
---|
205 | |
---|
206 | do_fatal_cleanups(); |
---|
207 | |
---|
208 | va_start(args, fmt); |
---|
209 | vfprintf(stderr, fmt, args); |
---|
210 | fprintf(stderr, "\n"); |
---|
211 | va_end(args); |
---|
212 | exit(255); |
---|
213 | } |
---|