MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / contrib / sendmail-8.14 / libmilter / signal.c
blobad684692ea63f41c96b9df752f13551f1b11cbad
1 /*
2 * Copyright (c) 1999-2004, 2006 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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.
9 */
11 #include <sm/gen.h>
12 SM_RCSID("@(#)$Id: signal.c,v 8.44 2006/03/03 03:42:04 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
30 ** Parameters:
31 ** none.
33 ** Returns:
34 ** value of MilterStop
37 int
38 mi_stop()
40 return MilterStop;
43 ** MI_STOP_MILTERS -- set value of MilterStop
45 ** Parameters:
46 ** v -- new value for MilterStop.
48 ** Returns:
49 ** none.
52 void
53 mi_stop_milters(v)
54 int v;
56 (void) smutex_lock(&M_Mutex);
57 if (MilterStop < v)
58 MilterStop = v;
60 /* close listen socket */
61 mi_closener();
62 (void) smutex_unlock(&M_Mutex);
65 ** MI_CLEAN_SIGNALS -- clean up signal handler thread
67 ** Parameters:
68 ** none.
70 ** Returns:
71 ** none.
74 void
75 mi_clean_signals()
77 (void) smutex_destroy(&M_Mutex);
80 ** MI_SIGNAL_THREAD -- thread to deal with signals
82 ** Parameters:
83 ** name -- name of milter
85 ** Returns:
86 ** NULL
89 static void *
90 mi_signal_thread(name)
91 void *name;
93 int sig, errs, sigerr;
94 sigset_t set;
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);
102 errs = 0;
104 for (;;)
106 sigerr = sig = 0;
107 #if defined(SOLARIS) || defined(__svr5__)
108 if ((sig = sigwait(&set)) < 0)
109 #else /* defined(SOLARIS) || defined(__svr5__) */
110 if ((sigerr = sigwait(&set, &sig)) != 0)
111 #endif /* defined(SOLARIS) || defined(__svr5__) */
113 /* some OS return -1 and set errno: copy it */
114 if (sigerr <= 0)
115 sigerr = errno;
117 /* this can happen on OSF/1 (at least) */
118 if (sigerr == EINTR)
119 continue;
120 smi_log(SMI_LOG_ERR,
121 "%s: sigwait returned error: %d",
122 (char *)name, sigerr);
123 if (++errs > MAX_FAILS_T)
125 mi_stop_milters(MILTER_ABRT);
126 return NULL;
128 continue;
130 errs = 0;
132 switch (sig)
134 case SIGHUP:
135 case SIGTERM:
136 mi_stop_milters(MILTER_STOP);
137 return NULL;
138 case SIGINT:
139 mi_stop_milters(MILTER_ABRT);
140 return NULL;
141 default:
142 smi_log(SMI_LOG_ERR,
143 "%s: sigwait returned unmasked signal: %d",
144 (char *)name, sig);
145 break;
148 /* NOTREACHED */
151 ** MI_SPAWN_SIGNAL_THREAD -- spawn thread to handle signals
153 ** Parameters:
154 ** name -- name of milter
156 ** Returns:
157 ** MI_SUCCESS/MI_FAILURE
160 static int
161 mi_spawn_signal_thread(name)
162 char *name;
164 sthread_t tid;
165 int r;
166 sigset_t set;
168 /* Mask HUP and KILL signals */
169 (void) sigemptyset(&set);
170 (void) sigaddset(&set, SIGHUP);
171 (void) sigaddset(&set, SIGTERM);
172 (void) sigaddset(&set, SIGINT);
174 if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0)
176 smi_log(SMI_LOG_ERR,
177 "%s: Couldn't mask HUP and KILL signals", name);
178 return MI_FAILURE;
180 r = thread_create(&tid, mi_signal_thread, (void *)name);
181 if (r != 0)
183 smi_log(SMI_LOG_ERR,
184 "%s: Couldn't start signal thread: %d",
185 name, r);
186 return MI_FAILURE;
188 return MI_SUCCESS;
191 ** MI_CONTROL_STARTUP -- startup for thread to handle signals
193 ** Parameters:
194 ** name -- name of milter
196 ** Returns:
197 ** MI_SUCCESS/MI_FAILURE
201 mi_control_startup(name)
202 char *name;
205 if (!smutex_init(&M_Mutex))
207 smi_log(SMI_LOG_ERR,
208 "%s: Couldn't initialize control pipe mutex", name);
209 return MI_FAILURE;
213 ** spawn_signal_thread must happen before other threads are spawned
214 ** off so that it can mask the right signals and other threads
215 ** will inherit that mask.
217 if (mi_spawn_signal_thread(name) == MI_FAILURE)
219 smi_log(SMI_LOG_ERR,
220 "%s: Couldn't spawn signal thread", name);
221 (void) smutex_destroy(&M_Mutex);
222 return MI_FAILURE;
224 return MI_SUCCESS;