Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / bounce / bounce_cleanup.c
blob772e6c1235def8309ae3323b0a38d31df1a662c7
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* bounce_cleanup 3
6 /* SUMMARY
7 /* cleanup logfile upon error
8 /* SYNOPSIS
9 /* #include "bounce_service.h"
11 /* int bounce_cleanup_registered()
13 /* void bounce_cleanup_register(queue_id)
14 /* char *queue_id;
16 /* void bounce_cleanup_log(void)
18 /* void bounce_cleanup_unregister(void)
19 /* DESCRIPTION
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
33 /* bounce logfile.
35 /* bounce_cleanup_registered() returns non-zero when a cleanup
36 /* trap has been set.
37 /* DIAGNOSTICS
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()
41 /* call.
42 /* BUGS
43 /* SEE ALSO
44 /* master(8) process manager
45 /* LICENSE
46 /* .ad
47 /* .fi
48 /* The Secure Mailer license must be distributed with this software.
49 /* AUTHOR(S)
50 /* Wietse Venema
51 /* IBM T.J. Watson Research
52 /* P.O. Box 704
53 /* Yorktown Heights, NY 10598, USA
54 /*--*/
56 /* System library. */
58 #include <sys_defs.h>
59 #include <unistd.h>
60 #include <signal.h>
61 #include <stdlib.h>
63 /* Utility library. */
65 #include <msg.h>
66 #include <mymalloc.h>
67 #include <vstring.h>
69 /* Global 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
82 * trap was set up.
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)
93 * Remove the logfile.
95 if (bounce_cleanup_path)
96 bounce_cleanup_log();
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";
112 * Sanity checks.
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));
134 _exit(sig);
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";
144 * Sanity checks.
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";
166 * Sanity checks.
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;