7 /* cleanup logfile upon error
9 /* #include "bounce_service.h"
11 /* int bounce_cleanup_registered()
13 /* void bounce_cleanup_register(queue_id)
16 /* void bounce_cleanup_log(void)
18 /* void bounce_cleanup_unregister(void)
20 /* This module implements support for deleting the current
21 /* bounce logfile in case of errors, and upon the arrival
22 /* of a SIGTERM signal (shutdown).
24 /* bounce_cleanup_register() registers a callback routine with the
25 /* run-time error handler, for automatic logfile removal in case
26 /* of a fatal run-time error.
28 /* bounce_cleanup_unregister() cleans up storage used by
29 /* bounce_cleanup_register().
31 /* In-between bounce_cleanup_register() and bounce_cleanup_unregister()
32 /* calls, a call of bounce_cleanup_log() will delete the registered
35 /* bounce_cleanup_registered() returns non-zero when a cleanup
38 /* Fatal error: all file access errors. Panic: nested calls of
39 /* bounce_cleanup_register(); any calls of bounce_cleanup_unregister()
40 /* or bounce_cleanup_log() without preceding bounce_cleanup_register()
44 /* master(8) process manager
48 /* The Secure Mailer license must be distributed with this software.
51 /* IBM T.J. Watson Research
53 /* Yorktown Heights, NY 10598, USA
63 /* Utility library. */
71 #include <mail_queue.h>
73 /* Application-specific. */
75 #include "bounce_service.h"
78 * Support for removing a logfile when an update fails. In order to do this,
79 * we save a copy of the currently-open logfile name, and register a
80 * callback function pointer with the run-time error handler. The saved
81 * pathname is made global so that the application can see whether or not a
84 static MSG_CLEANUP_FN bounce_cleanup_func
; /* saved callback */
85 VSTRING
*bounce_cleanup_path
; /* saved path name */
87 /* bounce_cleanup_callback - run-time callback to cleanup logfile */
89 static void bounce_cleanup_callback(void)
95 if (bounce_cleanup_path
)
99 * Execute the saved cleanup action.
101 if (bounce_cleanup_func
)
102 bounce_cleanup_func();
105 /* bounce_cleanup_log - clean up the logfile */
107 void bounce_cleanup_log(void)
109 const char *myname
= "bounce_cleanup_log";
114 if (bounce_cleanup_path
== 0)
115 msg_panic("%s: no cleanup context", myname
);
118 * This function may be called before a logfile is created or after it
119 * has been deleted, so do not complain.
121 (void) unlink(vstring_str(bounce_cleanup_path
));
124 /* bounce_cleanup_sig - signal handler */
126 static void bounce_cleanup_sig(int sig
)
130 * Running as a signal handler - don't do complicated stuff.
132 if (bounce_cleanup_path
)
133 (void) unlink(vstring_str(bounce_cleanup_path
));
137 /* bounce_cleanup_register - register logfile to clean up */
139 void bounce_cleanup_register(char *service
, char *queue_id
)
141 const char *myname
= "bounce_cleanup_register";
146 if (bounce_cleanup_path
)
147 msg_panic("%s: nested call", myname
);
150 * Save a copy of the logfile path, and of the last callback function
151 * pointer registered with the run-time error handler.
153 bounce_cleanup_path
= vstring_alloc(10);
154 (void) mail_queue_path(bounce_cleanup_path
, service
, queue_id
);
155 bounce_cleanup_func
= msg_cleanup(bounce_cleanup_callback
);
156 signal(SIGTERM
, bounce_cleanup_sig
);
159 /* bounce_cleanup_unregister - unregister logfile to clean up */
161 void bounce_cleanup_unregister(void)
163 const char *myname
= "bounce_cleanup_unregister";
168 if (bounce_cleanup_path
== 0)
169 msg_panic("%s: no cleanup context", myname
);
172 * Restore the saved callback function pointer, and release storage for
173 * the saved logfile pathname.
175 signal(SIGTERM
, SIG_DFL
);
176 (void) msg_cleanup(bounce_cleanup_func
);
177 vstring_free(bounce_cleanup_path
);
178 bounce_cleanup_path
= 0;