-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlog.c
More file actions
546 lines (430 loc) · 12.2 KB
/
Copy pathlog.c
File metadata and controls
546 lines (430 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
* FCRON - periodic command scheduler
*
* Copyright 2000-2026 Thibault Godouet <fcron@free.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The GNU General Public License can also be found in the file
* `LICENSE' that comes with the fcron source distribution.
*/
/* This code was originally inspired by Anacron's sources of
Itai Tzur <itzur@actcom.co.il> */
#include "fcron.h"
#include "log.h"
#include <sys/types.h>
#include <sys/socket.h>
#ifdef DEBUG
char debug_opt = 1; /* set to 1 if we are in debug mode */
#else
char debug_opt = 0; /* set to 1 if we are in debug mode */
#endif
int dosyslog = 1;
char *logfile_path = NULL;
char *make_msg(const char *append, char *fmt, va_list args);
void log_syslog_str(int priority, char *msg);
void log_file_str(int priority, char *msg);
void log_console_str(int priority, char *msg);
void log_fd_str(int fd, char *msg);
static void print_line_prefix(FILE * logfile, int priority);
static void xlog(int priority, int fd, char *fmt, va_list args);
static void xlog_e(int priority, int fd, char *fmt, va_list args);
#ifdef HAVE_LIBPAM
static void log_pame(int priority, pam_handle_t * pamh, int pamerrno,
char *fmt, va_list args);
#endif
static char truncated[] = " (truncated)";
static int log_open = 0;
static FILE *logfile = NULL;
/* Initialise logging to either syslog or a file specified in fcron.conf,
* or take no action if logging is suppressed.
* This function will be called automatically if you attempt to log something,
* however you may have to call it explicitely as it needs to run before the
* program becomes a daemon so as it can print an error on the console
* if it can't open the logs correctly. */
void
xopenlog(void)
{
if (log_open)
return;
/* we MUST set log_open to 1 before doing anything else. That way,
* if we call a function that logs something, which calls xopenlog,
* then we won't end up in a nasty loop */
log_open = 1;
// are we logging to a file or using syslog or not logging at all?
if (dosyslog) {
openlog(prog_name, LOG_PID, SYSLOG_FACILITY);
}
if (logfile_path != NULL) {
logfile = fopen(logfile_path, "a");
if (logfile == NULL) {
int saved_errno = errno;
if (dosyslog) {
/* we have already called openlog() which cannot fail */
syslog(COMPLAIN_LEVEL, "Could not fopen log file '%s': %s",
logfile_path, strerror(saved_errno));
}
print_line_prefix(stderr, COMPLAIN_LEVEL);
fprintf(stderr, "Could not fopen log file '%s': %s\n", logfile_path,
strerror(saved_errno));
}
}
}
void
xcloselog()
{
if (!log_open)
return;
// check whether we need to close syslog, or a file.
if (logfile != NULL) {
/* we must NOT use xfclose_check() in log.c to avoid infinite loops */
if (xfclose(&logfile) != 0) {
int saved_errno = errno;
syslog(COMPLAIN_LEVEL, "Error while closing log file '%s': %s",
logfile_path, strerror(saved_errno));
if (foreground == 1) {
print_line_prefix(stderr, COMPLAIN_LEVEL);
fprintf(stderr, "Error while closing log file '%s': %s\n",
logfile_path, strerror(saved_errno));
}
}
}
if (dosyslog) {
closelog();
}
log_open = 0;
}
/* Construct the message string from its parts, and append a string to it */
char *
make_msg(const char *append, char *fmt, va_list args)
{
int len;
char *msg = NULL;
if ((msg = calloc(1, MAX_MSG + 1)) == NULL)
return NULL;
/* There's some confusion in the documentation about what vsnprintf
* returns when the buffer overflows. Hmmm... */
len = vsnprintf(msg, MAX_MSG + 1, fmt, args);
if (append != NULL) {
size_t size_to_cat = ((MAX_MSG - len) > 0) ? (MAX_MSG - len) : 0;
strncat(msg, ": ", size_to_cat);
strncat(msg, append, size_to_cat);
len += 2 + strlen(append);
}
if (len >= MAX_MSG)
strcpy(msg + (MAX_MSG - 1) - sizeof(truncated), truncated);
return msg;
}
/* log a simple string to syslog if needed */
void
log_syslog_str(int priority, char *msg)
{
if (dosyslog) {
xopenlog();
syslog(priority, "%s", msg);
}
}
/* log a simple string to a log file if needed */
void
log_file_str(int priority, char *msg)
{
xopenlog();
/* we may have failed to open the logfile - check if
* it does exist *after* xopenlog() */
if (logfile != NULL) {
print_line_prefix(logfile, priority);
fprintf(logfile, "%s\n", msg);
fflush(logfile);
}
}
/* log a simple string to console if needed */
void
log_console_str(int priority, char *msg)
{
if (foreground == 1) {
print_line_prefix(stderr, priority);
fprintf(stderr, "%s\n", msg);
}
}
/* log a simple string to fd if needed */
void
log_fd_str(int fd, char *msg)
{
if (fd >= 0) {
send(fd, msg, strlen(msg), 0);
send(fd, "\n", strlen("\n"), 0);
}
}
/* Log a message, described by "fmt" and "args", with the specified
* "priority". */
/* write it also to fd if positive, and to stderr if foreground==1 */
static void
xlog(int priority, int fd, char *fmt, va_list args)
{
char *msg;
if ((msg = make_msg(NULL, fmt, args)) == NULL)
return;
log_syslog_str(priority, msg);
log_file_str(priority, msg);
log_console_str(priority, msg);
log_fd_str(fd, msg);
Free_safe(msg);
}
/* Same as xlog(), but also appends an error description corresponding
* to "errno". */
static void
xlog_e(int priority, int fd, char *fmt, va_list args)
{
int saved_errno;
char *msg;
saved_errno = errno;
if ((msg = make_msg(strerror(saved_errno), fmt, args)) == NULL)
return;
log_syslog_str(priority, msg);
log_file_str(priority, msg);
log_console_str(priority, msg);
log_fd_str(fd, msg);
Free_safe(msg);
}
/* write a message to the file specified by logfile. */
static void
print_line_prefix(FILE *logfile, int priority)
{
time_t t = time(NULL);
struct tm *ft;
char date[30];
char *type;
// print the current time as a string.
ft = localtime(&t);
date[0] = '\0';
strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", ft);
// is it an info/warning/error/debug message?
switch (priority) {
case EXPLAIN_LEVEL:
type = " INFO";
break;
case WARNING_LEVEL:
type = " WARN";
break;
case COMPLAIN_LEVEL:
type = "ERROR";
break;
case DEBUG_LEVEL:
type = "DEBUG";
break;
default:
type = "UNKNOWN_SEVERITY";
}
// print the log message.
fprintf(logfile, "%s %s ", date, type);
}
#ifdef HAVE_LIBPAM
/* Same as log_syslog(), but also appends an error description corresponding
* to the pam_error. */
static void
log_pame(int priority, pam_handle_t *pamh, int pamerrno, char *fmt,
va_list args)
{
char *msg;
if ((msg = make_msg(pam_strerror(pamh, pamerrno), fmt, args)) == NULL)
return;
log_syslog_str(priority, msg);
log_console_str(priority, msg);
xcloselog();
Free_safe(msg);
}
#endif
/* Log an "explain" level message */
void
explain(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(EXPLAIN_LEVEL, -1, fmt, args);
va_end(args);
}
/* as explain(), but also write message to fd if positive */
void
explain_fd(int fd, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(EXPLAIN_LEVEL, fd, fmt, args);
va_end(args);
}
/* Log an "explain" level message, with an error description */
void
explain_e(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog_e(EXPLAIN_LEVEL, -1, fmt, args);
va_end(args);
}
/* Log a "warning" level message */
void
warn(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(WARNING_LEVEL, -1, fmt, args);
va_end(args);
}
/* as warn(), but also write message to fd if positive */
void
warn_fd(int fd, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(WARNING_LEVEL, fd, fmt, args);
va_end(args);
}
/* Log a "warning" level message, with an error description */
void
warn_e(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog_e(WARNING_LEVEL, -1, fmt, args);
va_end(args);
}
/* Log a "complain" level message */
void
error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(COMPLAIN_LEVEL, -1, fmt, args);
va_end(args);
}
/* as error(), but also write message to fd if positive */
void
error_fd(int fd, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(COMPLAIN_LEVEL, fd, fmt, args);
va_end(args);
}
/* Log a "complain" level message, with an error description */
void
error_e(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog_e(COMPLAIN_LEVEL, -1, fmt, args);
va_end(args);
}
#ifdef HAVE_LIBPAM
/* Log a "complain" level message, with a PAM error description */
void
error_pame(pam_handle_t *pamh, int pamerrno, char *fmt, ...)
{
va_list args;
xcloselog(); /* PAM is likely to have used openlog() */
va_start(args, fmt);
log_pame(COMPLAIN_LEVEL, pamh, pamerrno, fmt, args);
va_end(args);
}
#endif
/* Log a "complain" level message, and exit */
void
die(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(COMPLAIN_LEVEL, -1, fmt, args);
va_end(args);
if (getpid() == daemon_pid) {
error("Aborted");
}
else {
error("fcron child aborted: this does not affect the main fcron daemon,"
" but this may prevent a job from being run or an email from being sent.");
}
exit(EXIT_ERR);
}
/* Log a "complain" level message, with an error description, and exit */
void
die_e(char *fmt, ...)
{
va_list args;
int err_no = 0;
err_no = errno;
va_start(args, fmt);
xlog_e(COMPLAIN_LEVEL, -1, fmt, args);
va_end(args);
if (getpid() == daemon_pid) {
error("Aborted");
}
else {
error("fcron child aborted: this does not affect the main fcron daemon,"
" but this may prevent a job from being run or an email from being sent.");
}
exit(err_no);
}
#ifdef HAVE_LIBPAM
/* Log a "complain" level message, with a PAM error description, and exit */
void
die_pame(pam_handle_t *pamh, int pamerrno, char *fmt, ...)
{
va_list args;
xcloselog(); /* PAM is likely to have used openlog() */
va_start(args, fmt);
log_pame(COMPLAIN_LEVEL, pamh, pamerrno, fmt, args);
va_end(args);
pam_end(pamh, pamerrno);
if (getpid() == daemon_pid)
error("Aborted");
exit(EXIT_ERR);
}
#endif
/* Log a "debug" level message */
void
Debug(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
xlog(DEBUG_LEVEL, -1, fmt, args);
va_end(args);
}
/* write message to fd, and to syslog in "debug" level message if debug_opt */
void
send_msg_fd_debug(int fd, char *fmt, ...)
{
char *msg;
va_list args;
va_start(args, fmt);
if ((msg = make_msg(NULL, fmt, args)) == NULL)
return;
if (debug_opt)
log_syslog_str(DEBUG_LEVEL, msg);
log_fd_str(fd, msg);
Free_safe(msg);
va_end(args);
}
/* write message to fd */
void
send_msg_fd(int fd, char *fmt, ...)
{
char *msg;
va_list args;
va_start(args, fmt);
if ((msg = make_msg(NULL, fmt, args)) == NULL)
return;
log_fd_str(fd, msg);
Free_safe(msg);
va_end(args);
}