8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / libmilter / signal.c
bloba91d0ba58402dd10271f15e4dc630bc743d6e032
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 #pragma ident "%Z%%M% %I% %E% SMI"
13 #include <sm/gen.h>
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
32 ** Parameters:
33 ** none.
35 ** Returns:
36 ** value of MilterStop
39 int
40 mi_stop()
42 return MilterStop;
45 ** MI_STOP_MILTERS -- set value of MilterStop
47 ** Parameters:
48 ** v -- new value for MilterStop.
50 ** Returns:
51 ** none.
54 void
55 mi_stop_milters(v)
56 int v;
58 (void) smutex_lock(&M_Mutex);
59 if (MilterStop < v)
60 MilterStop = v;
62 /* close listen socket */
63 mi_closener();
64 (void) smutex_unlock(&M_Mutex);
67 ** MI_CLEAN_SIGNALS -- clean up signal handler thread
69 ** Parameters:
70 ** none.
72 ** Returns:
73 ** none.
76 void
77 mi_clean_signals()
79 (void) smutex_destroy(&M_Mutex);
82 ** MI_SIGNAL_THREAD -- thread to deal with signals
84 ** Parameters:
85 ** name -- name of milter
87 ** Returns:
88 ** NULL
91 static void *
92 mi_signal_thread(name)
93 void *name;
95 int sig, errs, sigerr;
96 sigset_t set;
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);
104 errs = 0;
106 for (;;)
108 sigerr = sig = 0;
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 */
116 if (sigerr <= 0)
117 sigerr = errno;
119 /* this can happen on OSF/1 (at least) */
120 if (sigerr == EINTR)
121 continue;
122 smi_log(SMI_LOG_ERR,
123 "%s: sigwait returned error: %d",
124 (char *)name, sigerr);
125 if (++errs > MAX_FAILS_T)
127 mi_stop_milters(MILTER_ABRT);
128 return NULL;
130 continue;
132 errs = 0;
134 switch (sig)
136 case SIGHUP:
137 case SIGTERM:
138 mi_stop_milters(MILTER_STOP);
139 return NULL;
140 case SIGINT:
141 mi_stop_milters(MILTER_ABRT);
142 return NULL;
143 default:
144 smi_log(SMI_LOG_ERR,
145 "%s: sigwait returned unmasked signal: %d",
146 (char *)name, sig);
147 break;
150 /* NOTREACHED */
153 ** MI_SPAWN_SIGNAL_THREAD -- spawn thread to handle signals
155 ** Parameters:
156 ** name -- name of milter
158 ** Returns:
159 ** MI_SUCCESS/MI_FAILURE
162 static int
163 mi_spawn_signal_thread(name)
164 char *name;
166 sthread_t tid;
167 int r;
168 sigset_t set;
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)
178 smi_log(SMI_LOG_ERR,
179 "%s: Couldn't mask HUP and KILL signals", name);
180 return MI_FAILURE;
182 r = thread_create(&tid, mi_signal_thread, (void *)name);
183 if (r != 0)
185 smi_log(SMI_LOG_ERR,
186 "%s: Couldn't start signal thread: %d",
187 name, r);
188 return MI_FAILURE;
190 return MI_SUCCESS;
193 ** MI_CONTROL_STARTUP -- startup for thread to handle signals
195 ** Parameters:
196 ** name -- name of milter
198 ** Returns:
199 ** MI_SUCCESS/MI_FAILURE
203 mi_control_startup(name)
204 char *name;
207 if (!smutex_init(&M_Mutex))
209 smi_log(SMI_LOG_ERR,
210 "%s: Couldn't initialize control pipe mutex", name);
211 return MI_FAILURE;
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)
221 smi_log(SMI_LOG_ERR,
222 "%s: Couldn't spawn signal thread", name);
223 (void) smutex_destroy(&M_Mutex);
224 return MI_FAILURE;
226 return MI_SUCCESS;