2 * Copyright (c) 1999-2000 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 static char id[] = "@(#)$Id: signal.c,v 8.10.4.7 2000/09/01 00:49:04 ca Exp $";
16 #include "libmilter.h"
18 typedef pthread_mutex_t smutex_t;
19 # define smutex_init(mp) (pthread_mutex_init(mp, NULL) == 0)
20 # define smutex_destroy(mp) (pthread_mutex_destroy(mp) == 0)
21 # define smutex_lock(mp) (pthread_mutex_lock(mp) == 0)
22 # define smutex_unlock(mp) (pthread_mutex_unlock(mp) == 0)
23 # define smutex_trylock(mp) (pthread_mutex_trylock(mp) == 0)
26 ** thread to handle signals
29 static smutex_t M_Mutex;
31 static int MilterStop = MILTER_CONT;
34 ** MI_STOP -- return value of MilterStop
40 ** value of MilterStop
49 ** MI_STOP_MILTERS -- set value of MilterStop
52 ** v -- new value for MilterStop.
62 (void) smutex_lock(&M_Mutex);
66 /* close listen socket */
68 (void) smutex_unlock(&M_Mutex);
71 ** MI_CLEAN_SIGNALS -- clean up signal handler thread
83 (void) smutex_destroy(&M_Mutex);
86 ** MI_SIGNAL_THREAD -- thread to deal with signals
89 ** name -- name of milter
96 mi_signal_thread(name)
103 sigaddset(&set, SIGHUP);
104 sigaddset(&set, SIGTERM);
106 /* Handle Ctrl-C gracefully for debugging */
107 sigaddset(&set, SIGINT);
114 if ((sig = sigwait(&set)) < 0)
116 if (sigwait(&set, &sig) != 0)
120 "%s: sigwait returned error: %s",
121 (char *)name, strerror(errno));
122 if (++errs > MAX_FAILS_T)
124 mi_stop_milters(MILTER_ABRT);
135 mi_stop_milters(MILTER_STOP);
138 mi_stop_milters(MILTER_ABRT);
142 "%s: sigwait returned unmasked signal: %d",
149 ** MI_SPAWN_SIGNAL_THREAD -- spawn thread to handle signals
152 ** name -- name of milter
155 ** MI_SUCCESS/MI_FAILURE
159 mi_spawn_signal_thread(name)
165 /* Mask HUP and KILL signals */
167 sigaddset(&set, SIGHUP);
168 sigaddset(&set, SIGTERM);
169 sigaddset(&set, SIGINT);
171 if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0)
174 "%s: Couldn't mask HUP and KILL signals", name);
177 if (thread_create(&tid, mi_signal_thread,
178 (void *)name) != MI_SUCCESS)
181 "%s: Couldn't start signal thread", name);
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);
222 #endif /* _FFR_MILTER */