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