2 * Copyright (c) 1999-2004, 2006 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.
11 #pragma ident "%Z%%M% %I% %E% SMI"
14 SM_RCSID("@(#)$Id: signal.c,v 8.44 2006/03/03 03:42:04 ca Exp $")
16 #include "libmilter.h"
19 ** thread to handle signals
22 static smutex_t M_Mutex
;
24 static int MilterStop
= MILTER_CONT
;
26 static void *mi_signal_thread
__P((void *));
27 static int mi_spawn_signal_thread
__P((char *));
30 ** MI_STOP -- return value of MilterStop
36 ** value of MilterStop
45 ** MI_STOP_MILTERS -- set value of MilterStop
48 ** v -- new value for MilterStop.
58 (void) smutex_lock(&M_Mutex
);
62 /* close listen socket */
64 (void) smutex_unlock(&M_Mutex
);
67 ** MI_CLEAN_SIGNALS -- clean up signal handler thread
79 (void) smutex_destroy(&M_Mutex
);
82 ** MI_SIGNAL_THREAD -- thread to deal with signals
85 ** name -- name of milter
92 mi_signal_thread(name
)
95 int sig
, errs
, sigerr
;
98 (void) sigemptyset(&set
);
99 (void) sigaddset(&set
, SIGHUP
);
100 (void) sigaddset(&set
, SIGTERM
);
102 /* Handle Ctrl-C gracefully for debugging */
103 (void) sigaddset(&set
, SIGINT
);
109 #if defined(SOLARIS) || defined(__svr5__)
110 if ((sig
= sigwait(&set
)) < 0)
111 #else /* defined(SOLARIS) || defined(__svr5__) */
112 if ((sigerr
= sigwait(&set
, &sig
)) != 0)
113 #endif /* defined(SOLARIS) || defined(__svr5__) */
115 /* some OS return -1 and set errno: copy it */
119 /* this can happen on OSF/1 (at least) */
123 "%s: sigwait returned error: %d",
124 (char *)name
, sigerr
);
125 if (++errs
> MAX_FAILS_T
)
127 mi_stop_milters(MILTER_ABRT
);
138 mi_stop_milters(MILTER_STOP
);
141 mi_stop_milters(MILTER_ABRT
);
145 "%s: sigwait returned unmasked signal: %d",
153 ** MI_SPAWN_SIGNAL_THREAD -- spawn thread to handle signals
156 ** name -- name of milter
159 ** MI_SUCCESS/MI_FAILURE
163 mi_spawn_signal_thread(name
)
170 /* Mask HUP and KILL signals */
171 (void) sigemptyset(&set
);
172 (void) sigaddset(&set
, SIGHUP
);
173 (void) sigaddset(&set
, SIGTERM
);
174 (void) sigaddset(&set
, SIGINT
);
176 if (pthread_sigmask(SIG_BLOCK
, &set
, NULL
) != 0)
179 "%s: Couldn't mask HUP and KILL signals", name
);
182 r
= thread_create(&tid
, mi_signal_thread
, (void *)name
);
186 "%s: Couldn't start signal thread: %d",
193 ** MI_CONTROL_STARTUP -- startup for thread to handle signals
196 ** name -- name of milter
199 ** MI_SUCCESS/MI_FAILURE
203 mi_control_startup(name
)
207 if (!smutex_init(&M_Mutex
))
210 "%s: Couldn't initialize control pipe mutex", name
);
215 ** spawn_signal_thread must happen before other threads are spawned
216 ** off so that it can mask the right signals and other threads
217 ** will inherit that mask.
219 if (mi_spawn_signal_thread(name
) == MI_FAILURE
)
222 "%s: Couldn't spawn signal thread", name
);
223 (void) smutex_destroy(&M_Mutex
);