2 * Copyright (c) 1999-2002 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.37.2.2 2002/10/23 16:52:00 ca Exp $")
14 #include "libmilter.h"
17 ** thread to handle signals
20 static smutex_t M_Mutex;
22 static int MilterStop = MILTER_CONT;
25 ** MI_STOP -- return value of MilterStop
31 ** value of MilterStop
40 ** MI_STOP_MILTERS -- set value of MilterStop
43 ** v -- new value for MilterStop.
53 (void) smutex_lock(&M_Mutex);
57 /* close listen socket */
59 (void) smutex_unlock(&M_Mutex);
62 ** MI_CLEAN_SIGNALS -- clean up signal handler thread
74 (void) smutex_destroy(&M_Mutex);
77 ** MI_SIGNAL_THREAD -- thread to deal with signals
80 ** name -- name of milter
87 mi_signal_thread(name)
94 sigaddset(&set, SIGHUP);
95 sigaddset(&set, SIGTERM);
97 /* Handle Ctrl-C gracefully for debugging */
98 sigaddset(&set, SIGINT);
104 #if defined(SOLARIS) || defined(__svr5__)
105 if ((sig = sigwait(&set)) < 0)
106 #else /* defined(SOLARIS) || defined(__svr5__) */
107 if (sigwait(&set, &sig) != 0)
108 #endif /* defined(SOLARIS) || defined(__svr5__) */
110 /* this can happen on OSF/1 (at least) */
114 "%s: sigwait returned error: %d",
115 (char *)name, errno);
116 if (++errs > MAX_FAILS_T)
118 mi_stop_milters(MILTER_ABRT);
129 mi_stop_milters(MILTER_STOP);
132 mi_stop_milters(MILTER_ABRT);
136 "%s: sigwait returned unmasked signal: %d",
143 ** MI_SPAWN_SIGNAL_THREAD -- spawn thread to handle signals
146 ** name -- name of milter
149 ** MI_SUCCESS/MI_FAILURE
153 mi_spawn_signal_thread(name)
160 /* Mask HUP and KILL signals */
162 sigaddset(&set, SIGHUP);
163 sigaddset(&set, SIGTERM);
164 sigaddset(&set, SIGINT);
166 if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0)
169 "%s: Couldn't mask HUP and KILL signals", name);
172 r = thread_create(&tid, mi_signal_thread, (void *)name);
176 "%s: Couldn't start signal thread: %d",
183 ** MI_CONTROL_STARTUP -- startup for thread to handle signals
186 ** name -- name of milter
189 ** MI_SUCCESS/MI_FAILURE
193 mi_control_startup(name)
197 if (!smutex_init(&M_Mutex))
200 "%s: Couldn't initialize control pipe mutex", name);
205 ** spawn_signal_thread must happen before other threads are spawned
206 ** off so that it can mask the right signals and other threads
207 ** will inherit that mask.
209 if (mi_spawn_signal_thread(name) == MI_FAILURE)
212 "%s: Couldn't spawn signal thread", name);
213 (void) smutex_destroy(&M_Mutex);