2 * Copyright (c) 1999-2004 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 SM_RCSID("@(#)$Id: signal.c,v 8.42 2004/08/20 21:10:30 ca Exp $")
14 #include "libmilter.h"
17 ** thread to handle signals
20 static smutex_t M_Mutex;
22 static int MilterStop = MILTER_CONT;
24 static void *mi_signal_thread __P((void *));
25 static int mi_spawn_signal_thread __P((char *));
28 ** MI_STOP -- return value of MilterStop
34 ** value of MilterStop
43 ** MI_STOP_MILTERS -- set value of MilterStop
46 ** v -- new value for MilterStop.
56 (void) smutex_lock(&M_Mutex);
60 /* close listen socket */
62 (void) smutex_unlock(&M_Mutex);
65 ** MI_CLEAN_SIGNALS -- clean up signal handler thread
77 (void) smutex_destroy(&M_Mutex);
80 ** MI_SIGNAL_THREAD -- thread to deal with signals
83 ** name -- name of milter
90 mi_signal_thread(name)
96 (void) sigemptyset(&set);
97 (void) sigaddset(&set, SIGHUP);
98 (void) sigaddset(&set, SIGTERM);
100 /* Handle Ctrl-C gracefully for debugging */
101 (void) sigaddset(&set, SIGINT);
107 #if defined(SOLARIS) || defined(__svr5__)
108 if ((sig = sigwait(&set)) < 0)
109 #else /* defined(SOLARIS) || defined(__svr5__) */
110 if (sigwait(&set, &sig) != 0)
111 #endif /* defined(SOLARIS) || defined(__svr5__) */
113 /* this can happen on OSF/1 (at least) */
117 "%s: sigwait returned error: %d",
118 (char *)name, errno);
119 if (++errs > MAX_FAILS_T)
121 mi_stop_milters(MILTER_ABRT);
132 mi_stop_milters(MILTER_STOP);
135 mi_stop_milters(MILTER_ABRT);
139 "%s: sigwait returned unmasked signal: %d",
147 ** MI_SPAWN_SIGNAL_THREAD -- spawn thread to handle signals
150 ** name -- name of milter
153 ** MI_SUCCESS/MI_FAILURE
157 mi_spawn_signal_thread(name)
164 /* Mask HUP and KILL signals */
165 (void) sigemptyset(&set);
166 (void) sigaddset(&set, SIGHUP);
167 (void) sigaddset(&set, SIGTERM);
168 (void) sigaddset(&set, SIGINT);
170 if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0)
173 "%s: Couldn't mask HUP and KILL signals", name);
176 r = thread_create(&tid, mi_signal_thread, (void *)name);
180 "%s: Couldn't start signal thread: %d",
187 ** MI_CONTROL_STARTUP -- startup for thread to handle signals
190 ** name -- name of milter
193 ** MI_SUCCESS/MI_FAILURE
197 mi_control_startup(name)
201 if (!smutex_init(&M_Mutex))
204 "%s: Couldn't initialize control pipe mutex", name);
209 ** spawn_signal_thread must happen before other threads are spawned
210 ** off so that it can mask the right signals and other threads
211 ** will inherit that mask.
213 if (mi_spawn_signal_thread(name) == MI_FAILURE)
216 "%s: Couldn't spawn signal thread", name);
217 (void) smutex_destroy(&M_Mutex);