11 |
|
|
12 |
#include "config.h" |
#include "config.h" |
13 |
#include "imapfilter.h" |
#include "imapfilter.h" |
14 |
|
#include "pathnames.h" |
15 |
|
|
16 |
|
|
17 |
extern conn_t connpri, connaux; |
extern conn_t connpri, connaux; |
19 |
extern unsigned int options; |
extern unsigned int options; |
20 |
extern unsigned int flags; |
extern unsigned int flags; |
21 |
|
|
22 |
|
static FILE *debugfp = NULL; /* Pointer to debug file. */ |
23 |
static FILE *logfp = NULL; /* Pointer to logfile. */ |
static FILE *logfp = NULL; /* Pointer to logfile. */ |
24 |
|
|
25 |
|
|
61 |
|
|
62 |
|
|
63 |
/* |
/* |
64 |
|
* Write message to debug file. |
65 |
|
*/ |
66 |
|
void |
67 |
|
debug(const char *debug,...) |
68 |
|
{ |
69 |
|
va_list args; |
70 |
|
|
71 |
|
if ((options & OPTION_DEBUG) && debugfp) { |
72 |
|
va_start(args, debug); |
73 |
|
vfprintf(debugfp, debug, args); |
74 |
|
fflush(debugfp); |
75 |
|
va_end(args); |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
/* |
81 |
* Print error message and write it into logfile. |
* Print error message and write it into logfile. |
82 |
*/ |
*/ |
83 |
void |
void |
115 |
close_connection(&connaux); |
close_connection(&connaux); |
116 |
secmem_clear(); |
secmem_clear(); |
117 |
tty_restore(); |
tty_restore(); |
118 |
close_logfile(); |
log_stop(); |
119 |
lockfile_remove(); |
lockfile_remove(); |
120 |
|
debug_stop(); |
121 |
|
|
122 |
exit(errnum); |
exit(errnum); |
123 |
} |
} |
124 |
|
|
125 |
|
|
126 |
/* |
/* |
127 |
* Catch signals that cause program's termination. |
* Catch signals that cause rereading of the configuration file or program's |
128 |
|
* termination. |
129 |
*/ |
*/ |
130 |
void |
void |
131 |
catch_signals(void) |
catch_signals(void) |
138 |
|
|
139 |
|
|
140 |
/* |
/* |
141 |
* Signal handler for signals that cause termination of program. |
* Signal handler for signals that cause rereading of the configuration file or |
142 |
|
* termination of program. |
143 |
*/ |
*/ |
144 |
void |
void |
145 |
signal_handler(int sig) |
signal_handler(int sig) |
152 |
|
|
153 |
|
|
154 |
/* |
/* |
155 |
|
* Open temporary debug file and associate a stream with the returned file |
156 |
|
* descriptor. |
157 |
|
*/ |
158 |
|
int |
159 |
|
debug_start(void) |
160 |
|
{ |
161 |
|
static char dt[] = PATHNAME_DEBUG_FILE; |
162 |
|
int fd; |
163 |
|
|
164 |
|
if (!(options & OPTION_DEBUG)) |
165 |
|
return 0; |
166 |
|
|
167 |
|
fd = mkstemp(dt); |
168 |
|
|
169 |
|
if (fd != -1) { |
170 |
|
debugfp = fdopen(fd, "w"); |
171 |
|
if (debugfp == NULL) { |
172 |
|
error("opening debug file %s: %s\n", logfile, |
173 |
|
strerror(errno)); |
174 |
|
return ERROR_TRIVIAL; |
175 |
|
} |
176 |
|
} |
177 |
|
return 0; |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
/* |
182 |
|
* Close temporary debug file. |
183 |
|
*/ |
184 |
|
int |
185 |
|
debug_stop(void) |
186 |
|
{ |
187 |
|
if (debugfp == NULL) |
188 |
|
return 0; |
189 |
|
else |
190 |
|
return fclose(debugfp); |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
/* |
195 |
* Open the file for saving of logging information. |
* Open the file for saving of logging information. |
196 |
*/ |
*/ |
197 |
int |
int |
198 |
open_logfile(void) |
log_start(void) |
199 |
{ |
{ |
200 |
if (*logfile == '\0') |
if (*logfile == '\0') |
201 |
return 0; /* Logging not enabled. */ |
return 0; /* Logging not enabled. */ |
202 |
|
|
203 |
#ifdef DEBUG |
debug("log file: '%s'\n", logfile); |
|
fprintf(stderr, "debug: logfile: '%s'\n", logfile); |
|
|
#endif |
|
204 |
|
|
205 |
if (create_file(logfile, S_IRUSR | S_IWUSR)) |
if (create_file(logfile, S_IRUSR | S_IWUSR)) |
206 |
return ERROR_TRIVIAL; |
return ERROR_TRIVIAL; |
207 |
|
|
208 |
logfp = fopen(logfile, "a"); |
logfp = fopen(logfile, "a"); |
209 |
if (logfp == NULL) { |
if (logfp == NULL) { |
210 |
error("opening logfile %s: %s\n", logfile, strerror(errno)); |
error("opening log file %s: %s\n", logfile, strerror(errno)); |
211 |
return ERROR_TRIVIAL; |
return ERROR_TRIVIAL; |
212 |
} |
} |
213 |
return 0; |
return 0; |
215 |
|
|
216 |
|
|
217 |
/* |
/* |
218 |
* Close the logfile. |
* Close the log file. |
219 |
*/ |
*/ |
220 |
int |
int |
221 |
close_logfile(void) |
log_stop(void) |
222 |
{ |
{ |
223 |
if (logfp == NULL) |
if (logfp == NULL) |
224 |
return 0; |
return 0; |