File: log.c

package info (click to toggle)
deliver 2.1.14-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 488 kB
  • ctags: 914
  • sloc: ansic: 6,050; yacc: 405; makefile: 131; sh: 34
file content (475 lines) | stat: -rw-r--r-- 10,026 bytes parent folder | download | duplicates (3)
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
/* $Id: log.c,v 1.7 1993/10/28 16:49:51 chip Exp $
 *
 * Deliver logging.
 *
 * $Log: log.c,v $
 * Revision 1.7  1993/10/28 16:49:51  chip
 * Declare function return types, including void.
 *
 * Revision 1.6  1991/11/25  20:49:42  chip
 * Prettify "Undel.mail" message for home directory of root.
 *
 * Revision 1.5  1991/11/12  20:44:47  chip
 * Use new logsize() function.  Seek to end of log before
 * writing to it or testing its size, since a child process
 * might have grown it.
 *
 * Revision 1.4  1991/08/27  15:39:45  chip
 * Use tmzone().
 *
 * Revision 1.3  1991/08/21  22:15:33  chip
 * Careful creation for NFS.
 *
 * Revision 1.2  1991/06/04  18:16:28  chip
 * Feature-based configuration.
 *
 * Revision 1.1  1991/05/13  18:36:55  chip
 * Initial revision
 *
 */

#include "deliver.h"
#include <time.h>

static void logundel();
static void logheader();

/*----------------------------------------------------------------------
 * Open temporary log files.
 */

void
openlogs()
{
#ifdef ERRLOG
    /* If we're delivering and not being verbose, keep an error log. */

    if (!dryrun && !verbose)
    {
	t_errlogfile = tempfile();
	if ((errlog = ftcreate(t_errlogfile)) == NULL)
	    syserr("can't create %s", t_errlogfile);
    }
#endif

#ifdef LOG
    /* If we're delivering and the log file exists, keep data for it. */

    if (!dryrun && exists(f_logfile))
    {
	t_logfile = tempfile();
	if ((log = ftcreate(t_logfile)) == NULL)
	    syserr("can't create %s", t_logfile);
    }
#endif
}

/*------------------------------------------------------------------------
 * Discard temporary log files.
 */

void
tosslogs()
{
    if (t_logfile && unlink(t_logfile) == -1)
	syserr("can't remove %s", t_logfile);

    if (t_errlogfile && unlink(t_errlogfile) == -1)
	syserr("can't remove %s", t_errlogfile);
}

/*----------------------------------------------------------------------
 * Save contents of temporary logs in the real logfiles.
 */

void
savelogs()
{
    if (!log && !errlog)
	return;

    /* If temporary logs contain anything, append them to real logs. */

    if (logsize(log) || logsize(errlog))
    {
	if (log_lock() == 0)
	{
	    if (t_logfile)
		applog(&log, f_logfile);
	    errdone();
	    if (t_errlogfile)
		applog(&errlog, f_errlogfile);
	    (void) log_unlock();
	}
    }
}

/*----------------------------------------------------------------------
 * Append a temporary log file to a real logfile.
 * We pass a FILE **, so that it can be set to NULL when closed;
 * this is important, since errlog is used by syserr().
 *
 * Note:  It is assumed that the caller already called log_lock().
 * Nowever, on systems where kernel locking and modern open() are
 * available, log_lock() is just a nop.  We call k_lock() here,
 * which does the real locking on modern systems.
 */

void
applog(fpp, realfile)
FILE **fpp;
char *realfile;
{
    FILE *fp = fpp ? *fpp : NULL;
    int fd, realfd;

    /* If log data weren't kept, never mind. */

    if (fp == NULL)
	return;

    /* Flush buffered data. */

    (void) fflush(fp);

    /* If the file is empty, never mind. */

    if (logsize(fp) == 0)
    {
	(void) fclose(fp);
	*fpp = NULL;
	return;
    }

    /* Get an fd and close the stream. */

    if ((fd = dup(fileno(fp))) == -1)
    {
	syserr("can't dup log fd");
	(void) fclose(fp);
	*fpp = NULL;
	return;
    }
    (void) fclose(fp);
    *fpp = NULL;

    /*
     * Open the real logfile, creating it if necessary.
     *
     * Note that there is no race condition here, since if safe
     * creation isn't available, the logs are already locked.
     * See log_lock() and log_unlock() in lock.c.
     */

#ifdef SAFE_CREATE
    realfd = open(realfile, O_WRONLY | O_CREAT, 0666);
#else
    if ((realfd = open(realfile, O_WRONLY)) == -1)
	realfd = creat(realfile, 0666);
#endif
    if (realfd == -1)
	syserr("can't open %s for writing", realfile);
    else
    {
	if (k_lock(realfd) == -1)
	    syserr("can't lock %s", realfile);
	else
	{
	    /* Append the temporary log to the real log. */

	    (void) lseek(fd, 0L, 0);
	    (void) lseek(realfd, 0L, 2);
	    (void) copyfd(fd, realfd);
	    (void) k_unlock(realfd);
	}
	(void) close(realfd);
    }

    /* Close the temporary log. */

    (void) close(fd);
}

/*----------------------------------------------------------------------
 * Write a report to the log file.
 */

void
logreport(ac, av)
int ac;
char **av;
{
    int a;

    if (!log)
	return;

    logstart(log);

    if (local_sender)
	(void) fprintf(log, "local sender: %s\n", local_sender);
    if (orig_sender)
	(void) fprintf(log, "original sender: %s\n", orig_sender);
    if (boxdelivery)
	(void) fprintf(log, "mailbox%s:", (ac > 1) ? "es" : "");
    else
	(void) fprintf(log, "destination%s:", (ac > 1) ? "s" : "");
    for (a = 0; a < ac; ++a)
	(void) fprintf(log, " \"%s\"", av[a]);
    (void) fputc('\n', log);

    logstate("delivered", ST_DONE);
    logstate("failed", ST_ERROR);

    logdone(log);
}

/*----------------------------------------------------------------------
 * Log the destinations with the given state.
 * If any are found, the list is prefixed with the given description.
 */

void
logstate(desc, state)
char *desc;
DSTATE state;
{
    DEST *d;
    int dcount;

    dcount = 0;
    for (d = first_dest(); d; d = next_dest(d))
    {
	if (d->d_state != state)
	    continue;

	if (++dcount == 1)
	    (void) fprintf(log, "%s:", desc);
	(void) fprintf(log, " %s", d->d_name);
	if (d->d_class == CL_MBOX)
	    (void) fprintf(log, ":%s", d->d_param);
	else if (d->d_class == CL_PROG)
	    (void) fprintf(log, "|\"%s\"", d->d_param);
    }
    if (dcount)
	(void) fputc('\n', log);
}

/*----------------------------------------------------------------------
 * Record any interesting information in the error log file.
 */

void
logerrinfo()
{
    if (!errlog)
	return;

    /* Log undelivered mail. */

    logundel();

    /* If any errors have been logged, record the failed header. */

    if (logsize(errlog))
	logheader();
}

/*----------------------------------------------------------------------
 * Log undelivered mail.
 *
 * Note that this algorithm assumes that delivery to the MBX_UNDEL mailbox
 * is always worth reporting.
 */

static void
logundel()
{
    DEST *d;

    if (!errlog)
	return;

    for (d = first_dest(); d; d = next_dest(d))
    {
	if (d->d_state == ST_DONE
	    && d->d_class == CL_MBOX
	    && strcmp(d->d_param, MBX_UNDEL) == 0)
	{
	    CONTEXT *ct;
	    char *home;

	    if ((ct = name_context(d->d_name)) != NULL)
		home = ct->ct_home;
	    else
		home = "~";	/* should never happen */

	    errstart();
	    (void) fprintf(errlog, "Undelivered mail for %s put in %s/%s\n",
			   d->d_name, (strcmp(home, "/") == 0) ? "" : home,
			   MBX_UNDEL);
	}
    }
}

/*----------------------------------------------------------------------
 * Log the message header.
 */

static void
logheader()
{
    FILE *hfp;
    int hfd;

    if (!errlog)
	return;

    /* Copy the failed message's header (if any). */

    if (tfd[T_HDR] == -1)
	return;

    hfd = dup(tfd[T_HDR]);
    hfp = (hfd < 0) ? NULL : fdopen(hfd, "r");
    if (hfp == NULL)
    {
	(void) fprintf(errlog, "%s: can't open header file %s\n",
		       progname, tfile[T_HDR]);
    }
    else
    {
	int c, oc;

	(void) fprintf(errlog, "+ Header:\n");

	(void) fseek(hfp, 0L, 0);
	oc = '\n';
	while ((c = getc(hfp)) != EOF)
	{
	    if (oc != '\n' || c != '\n')
	    {
		if (oc == '\n')
		    (void) fputs("| ", errlog);
		(void) putc(c, errlog);
	    }
	    oc = c;
	}

	(void) fclose(hfp);
    }
}

/*----------------------------------------------------------------------
 * Record a time stamp in the error log file.
 */

void
errstart()
{
    if (!errlog)
	return;

    /* If we've already written a time stamp, don't do it again. */

    if (logsize(errlog))
	return;

    /* Write a time stamp and various useful info. */

    logstart(errlog);
    (void) fprintf(errlog, "process %d", getpid());
    if (rec_parent > 0)
	(void) fprintf(errlog, ", parent %d", rec_parent);
    (void) fprintf(errlog, ": %s %s\n", progname, version);
}

/*----------------------------------------------------------------------
 * Record the end of this process's error log entry.
 */

void
errdone()
{
    if (!errlog)
	return;

    /* If we never wrote to the error log file, do nothing. */

    if (logsize(errlog) == 0)
	return;

    /* Write a simple closing line for the error log entry. */

    (void) fprintf(errlog, "process %d", getpid());
    if (rec_parent > 0)
	(void) fprintf(errlog, ", parent %d", rec_parent);
    (void) fprintf(errlog, ": exit\n");

    logdone(errlog);
}

/*----------------------------------------------------------------------
 * Start a log entry.
 * Various useful info goes here -- especially a timestamp.
 */

void
logstart(fp)
FILE *fp;
{
    struct tm *lt;
    time_t now;
    static char month[12][4] =
    {
	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    };

    (void) time(&now);
    lt = localtime(&now);

    (void) fputc('\n', fp);
    if (rec_level)
	(void) fprintf(fp, "[%d]", rec_level);
    else
	(void) fputs("---", fp);
    (void) fputs("------------------------ ", fp);
    (void) fprintf(fp, "%d %s %d, %02d:%02d:%02d %s\n",
		   lt->tm_mday, month[lt->tm_mon], lt->tm_year + 1900,
		   lt->tm_hour, lt->tm_min, lt->tm_sec, tmzone(lt));
}

/*----------------------------------------------------------------------
 * Write a concluding marker to the given logfile.
 * This marker separates instances of Deliver at recursion level zero.
 */

void
logdone(fp)
FILE *fp;
{
    if (rec_level == 0)
	(void) fputs("===========================\n\n", fp);
}

/*----------------------------------------------------------------------
 * Report the size of an open log file.
 * Incidentally, seek to the end.
 */

long
logsize(fp)
FILE *fp;
{
    long pos;

    if (!fp)
	return 0L;

    if (fseek(fp, 0L, 2) == -1)
	return 0;

    pos = ftell(fp);
    return (pos == -1) ? 0 : pos;
}