5
/* cleanup logfile upon error
7
/* #include "bounce_service.h"
9
/* int bounce_cleanup_registered()
11
/* void bounce_cleanup_register(queue_id)
14
/* void bounce_cleanup_log(void)
16
/* void bounce_cleanup_unregister(void)
18
/* This module implements support for deleting the current
19
/* bounce logfile in case of errors, and upon the arrival
20
/* of a SIGTERM signal (shutdown).
22
/* bounce_cleanup_register() registers a callback routine with the
23
/* run-time error handler, for automatic logfile removal in case
24
/* of a fatal run-time error.
26
/* bounce_cleanup_unregister() cleans up storage used by
27
/* bounce_cleanup_register().
29
/* In-between bounce_cleanup_register() and bounce_cleanup_unregister()
30
/* calls, a call of bounce_cleanup_log() will delete the registered
33
/* bounce_cleanup_registered() returns non-zero when a cleanup
36
/* Fatal error: all file access errors. Panic: nested calls of
37
/* bounce_cleanup_register(); any calls of bounce_cleanup_unregister()
38
/* or bounce_cleanup_log() without preceding bounce_cleanup_register()
42
/* master(8) process manager
46
/* The Secure Mailer license must be distributed with this software.
49
/* IBM T.J. Watson Research
51
/* Yorktown Heights, NY 10598, USA
61
/* Utility library. */
69
#include <mail_queue.h>
71
/* Application-specific. */
73
#include "bounce_service.h"
76
* Support for removing a logfile when an update fails. In order to do this,
77
* we save a copy of the currently-open logfile name, and register a
78
* callback function pointer with the run-time error handler. The saved
79
* pathname is made global so that the application can see whether or not a
82
static MSG_CLEANUP_FN bounce_cleanup_func; /* saved callback */
83
VSTRING *bounce_cleanup_path; /* saved path name */
85
/* bounce_cleanup_callback - run-time callback to cleanup logfile */
87
static void bounce_cleanup_callback(void)
93
if (bounce_cleanup_path)
97
* Execute the saved cleanup action.
99
if (bounce_cleanup_func)
100
bounce_cleanup_func();
103
/* bounce_cleanup_log - clean up the logfile */
105
void bounce_cleanup_log(void)
107
char *myname = "bounce_cleanup_log";
112
if (bounce_cleanup_path == 0)
113
msg_panic("%s: no cleanup context", myname);
116
* This function may be called before a logfile is created or after it
117
* has been deleted, so do not complain.
119
(void) unlink(vstring_str(bounce_cleanup_path));
122
/* bounce_cleanup_sig - signal handler */
124
static void bounce_cleanup_sig(int sig)
128
* Running as a signal handler - don't do complicated stuff.
130
if (bounce_cleanup_path)
131
(void) unlink(vstring_str(bounce_cleanup_path));
135
/* bounce_cleanup_register - register logfile to clean up */
137
void bounce_cleanup_register(char *service, char *queue_id)
139
char *myname = "bounce_cleanup_register";
144
if (bounce_cleanup_path)
145
msg_panic("%s: nested call", myname);
148
* Save a copy of the logfile path, and of the last callback function
149
* pointer registered with the run-time error handler.
151
bounce_cleanup_path = vstring_alloc(10);
152
(void) mail_queue_path(bounce_cleanup_path, service, queue_id);
153
bounce_cleanup_func = msg_cleanup(bounce_cleanup_callback);
154
signal(SIGTERM, bounce_cleanup_sig);
157
/* bounce_cleanup_unregister - unregister logfile to clean up */
159
void bounce_cleanup_unregister(void)
161
char *myname = "bounce_cleanup_unregister";
166
if (bounce_cleanup_path == 0)
167
msg_panic("%s: no cleanup context", myname);
170
* Restore the saved callback function pointer, and release storage for
171
* the saved logfile pathname.
173
signal(SIGTERM, SIG_DFL);
174
(void) msg_cleanup(bounce_cleanup_func);
175
vstring_free(bounce_cleanup_path);
176
bounce_cleanup_path = 0;