xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/kadm5/ipropd_common.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: ipropd_common.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include "iprop.h"
37b9d004c6Schristos 
38b9d004c6Schristos #if defined(HAVE_FORK) && defined(HAVE_WAITPID)
39b9d004c6Schristos #include <sys/types.h>
40b9d004c6Schristos #include <sys/wait.h>
41b9d004c6Schristos #endif
42ca1c9b0cSelric 
43ca1c9b0cSelric sig_atomic_t exit_flag;
44ca1c9b0cSelric 
45ca1c9b0cSelric static RETSIGTYPE
sigterm(int sig)46ca1c9b0cSelric sigterm(int sig)
47ca1c9b0cSelric {
48ca1c9b0cSelric     exit_flag = sig;
49ca1c9b0cSelric }
50ca1c9b0cSelric 
51ca1c9b0cSelric void
setup_signal(void)52ca1c9b0cSelric setup_signal(void)
53ca1c9b0cSelric {
54ca1c9b0cSelric #ifdef HAVE_SIGACTION
55ca1c9b0cSelric     {
56ca1c9b0cSelric 	struct sigaction sa;
57ca1c9b0cSelric 
58ca1c9b0cSelric 	sa.sa_flags = 0;
59ca1c9b0cSelric 	sa.sa_handler = sigterm;
60ca1c9b0cSelric 	sigemptyset(&sa.sa_mask);
61ca1c9b0cSelric 
62ca1c9b0cSelric 	sigaction(SIGINT, &sa, NULL);
63ca1c9b0cSelric 	sigaction(SIGTERM, &sa, NULL);
64ca1c9b0cSelric 	sigaction(SIGXCPU, &sa, NULL);
65ca1c9b0cSelric 
66ca1c9b0cSelric 	sa.sa_handler = SIG_IGN;
67ca1c9b0cSelric 	sigaction(SIGPIPE, &sa, NULL);
68ca1c9b0cSelric     }
69ca1c9b0cSelric #else
70ca1c9b0cSelric     signal(SIGINT, sigterm);
71ca1c9b0cSelric     signal(SIGTERM, sigterm);
72ca1c9b0cSelric #ifndef NO_SIGXCPU
73ca1c9b0cSelric     signal(SIGXCPU, sigterm);
74ca1c9b0cSelric #endif
75ca1c9b0cSelric #ifndef NO_SIGPIPE
76ca1c9b0cSelric     signal(SIGPIPE, SIG_IGN);
77ca1c9b0cSelric #endif
78ca1c9b0cSelric #endif
79ca1c9b0cSelric }
80b9d004c6Schristos 
81b9d004c6Schristos /*
82b9d004c6Schristos  * Fork a child to run the service, and restart it if it dies.
83b9d004c6Schristos  *
84b9d004c6Schristos  * Returns -1 if not supported, else a file descriptor that the service
85b9d004c6Schristos  * should select() for.  Any events on that file descriptor should cause
86b9d004c6Schristos  * the caller to exit immediately, as that means that the restarter
87b9d004c6Schristos  * exited.
88b9d004c6Schristos  *
89b9d004c6Schristos  * The service's normal exit status values should be should be taken
90b9d004c6Schristos  * from enum ipropd_exit_code.  IPROPD_FATAL causes the restarter to
91b9d004c6Schristos  * stop restarting the service and to exit.
92b9d004c6Schristos  *
93b9d004c6Schristos  * A count of restarts is output via the `countp' argument, if it is
94b9d004c6Schristos  * non-NULL.  This is useful for testing this function (e.g., kill the
95b9d004c6Schristos  * restarter after N restarts and check that the child gets the signal
96b9d004c6Schristos  * sent to it).
97b9d004c6Schristos  *
98b9d004c6Schristos  * This requires fork() and waitpid() (otherwise returns -1).  Ignoring
99b9d004c6Schristos  * SIGCHLD, of course, would be bad.
100b9d004c6Schristos  *
101b9d004c6Schristos  * We could support this on Windows by spawning a child with mostly the
102b9d004c6Schristos  * same arguments as the restarter process.
103b9d004c6Schristos  */
104b9d004c6Schristos int
restarter(krb5_context context,size_t * countp)105b9d004c6Schristos restarter(krb5_context context, size_t *countp)
106b9d004c6Schristos {
107b9d004c6Schristos #if defined(HAVE_FORK) && defined(HAVE_WAITPID)
108b9d004c6Schristos     struct timeval tmout;
109b9d004c6Schristos     pid_t pid = -1;
110b9d004c6Schristos     pid_t wpid = -1;
111b9d004c6Schristos     int status;
112b9d004c6Schristos     int fds[2];
113b9d004c6Schristos     int fds2[2];
114b9d004c6Schristos     size_t count = 0;
115b9d004c6Schristos     fd_set readset;
116b9d004c6Schristos 
117b9d004c6Schristos     fds[0] = -1;
118b9d004c6Schristos     fds[1] = -1;
119b9d004c6Schristos     fds2[0] = -1;
120b9d004c6Schristos     fds2[1] = -1;
121b9d004c6Schristos 
122b9d004c6Schristos     signal(SIGCHLD, SIG_DFL);
123b9d004c6Schristos 
124b9d004c6Schristos     while (!exit_flag) {
125b9d004c6Schristos         /* Close the pipe ends we keep open */
126b9d004c6Schristos         if (fds[1] != -1)
127b9d004c6Schristos             (void) close(fds[1]);
128b9d004c6Schristos         if (fds2[0] != -1)
129b9d004c6Schristos             (void) close(fds2[1]);
130b9d004c6Schristos 
131b9d004c6Schristos         /* A pipe so the child can detect the parent's death */
132b9d004c6Schristos         if (pipe(fds) == -1) {
133b9d004c6Schristos             krb5_err(context, 1, errno,
134b9d004c6Schristos                      "Could not setup pipes in service restarter");
135b9d004c6Schristos         }
136b9d004c6Schristos 
137b9d004c6Schristos         /* A pipe so the parent can detect the child's death */
138b9d004c6Schristos         if (pipe(fds2) == -1) {
139b9d004c6Schristos             krb5_err(context, 1, errno,
140b9d004c6Schristos                      "Could not setup pipes in service restarter");
141b9d004c6Schristos         }
142b9d004c6Schristos 
143b9d004c6Schristos         fflush(stdout);
144b9d004c6Schristos         fflush(stderr);
145b9d004c6Schristos 
146b9d004c6Schristos         pid = fork();
147b9d004c6Schristos         if (pid == -1)
148b9d004c6Schristos             krb5_err(context, 1, errno, "Could not fork in service restarter");
149b9d004c6Schristos         if (pid == 0) {
150b9d004c6Schristos             if (countp != NULL)
151b9d004c6Schristos                 *countp = count;
152b9d004c6Schristos             (void) close(fds[1]);
153b9d004c6Schristos             (void) close(fds2[0]);
154b9d004c6Schristos             return fds[0];
155b9d004c6Schristos         }
156b9d004c6Schristos 
157b9d004c6Schristos         count++;
158b9d004c6Schristos 
159b9d004c6Schristos         (void) close(fds[0]);
160b9d004c6Schristos         (void) close(fds2[1]);
161b9d004c6Schristos 
162b9d004c6Schristos         do {
163b9d004c6Schristos             wpid = waitpid(pid, &status, 0);
164b9d004c6Schristos         } while (wpid == -1 && errno == EINTR && !exit_flag);
165b9d004c6Schristos         if (wpid == -1 && errno == EINTR)
166b9d004c6Schristos             break; /* We were signaled; gotta kill the child and exit */
167b9d004c6Schristos         if (wpid == -1) {
168b9d004c6Schristos             if (errno != ECHILD) {
169b9d004c6Schristos                 warn("waitpid() failed; killing restarter's child process");
170b9d004c6Schristos                 kill(pid, SIGTERM);
171b9d004c6Schristos             }
172b9d004c6Schristos             krb5_err(context, 1, errno, "restarter failed waiting for child");
173b9d004c6Schristos         }
174b9d004c6Schristos 
175b9d004c6Schristos         assert(wpid == pid);
176b9d004c6Schristos         wpid = -1;
177b9d004c6Schristos         pid = -1;
178b9d004c6Schristos         if (WIFEXITED(status)) {
179b9d004c6Schristos             switch (WEXITSTATUS(status)) {
180b9d004c6Schristos             case IPROPD_DONE:
181b9d004c6Schristos                 exit(0);
182b9d004c6Schristos             case IPROPD_RESTART_SLOW:
183b9d004c6Schristos                 if (exit_flag)
184b9d004c6Schristos                     exit(1);
185b9d004c6Schristos                 krb5_warnx(context, "Waiting 2 minutes to restart");
186b9d004c6Schristos                 sleep(120);
187b9d004c6Schristos                 continue;
188b9d004c6Schristos             case IPROPD_FATAL:
189b9d004c6Schristos                 krb5_errx(context, WEXITSTATUS(status),
190b9d004c6Schristos                          "Sockets and pipes not supported for "
191b9d004c6Schristos                          "iprop log files");
192b9d004c6Schristos             case IPROPD_RESTART:
193b9d004c6Schristos             default:
194b9d004c6Schristos                 if (exit_flag)
195b9d004c6Schristos                     exit(1);
196b9d004c6Schristos                 /* Add exponential backoff (with max backoff)? */
197b9d004c6Schristos                 krb5_warnx(context, "Waiting 30 seconds to restart");
198b9d004c6Schristos                 sleep(30);
199b9d004c6Schristos                 continue;
200b9d004c6Schristos             }
201b9d004c6Schristos         }
202b9d004c6Schristos         /* else */
203b9d004c6Schristos         krb5_warnx(context, "Child was killed; waiting 30 seconds to restart");
204b9d004c6Schristos         sleep(30);
205b9d004c6Schristos     }
206b9d004c6Schristos 
207b9d004c6Schristos     if (pid == -1)
208b9d004c6Schristos         exit(0); /* No dead child to reap; done */
209b9d004c6Schristos 
210b9d004c6Schristos     assert(pid > 0);
211b9d004c6Schristos     if (wpid != pid) {
212b9d004c6Schristos         warnx("Interrupted; killing child (pid %ld) with %d",
213b9d004c6Schristos               (long)pid, exit_flag);
214b9d004c6Schristos         krb5_warnx(context, "Interrupted; killing child (pid %ld) with %d",
215b9d004c6Schristos                    (long)pid, exit_flag);
216b9d004c6Schristos         kill(pid, exit_flag);
217b9d004c6Schristos 
218b9d004c6Schristos         /* Wait up to one second for the child */
219b9d004c6Schristos         tmout.tv_sec = 1;
220b9d004c6Schristos         tmout.tv_usec = 0;
221b9d004c6Schristos         FD_ZERO(&readset);
222b9d004c6Schristos         FD_SET(fds2[0], &readset);
223b9d004c6Schristos         /* We don't care why select() returns */
224b9d004c6Schristos         (void) select(fds2[0] + 1, &readset, NULL, NULL, &tmout);
225b9d004c6Schristos         /*
226b9d004c6Schristos          * We haven't reaped the child yet; if it's a zombie, then
227b9d004c6Schristos          * SIGKILLing it won't hurt.  If it's not a zombie yet, well,
228b9d004c6Schristos          * we're out of patience.
229b9d004c6Schristos          */
230b9d004c6Schristos         kill(pid, SIGKILL);
231b9d004c6Schristos         do {
232b9d004c6Schristos             wpid = waitpid(pid, &status, 0);
233b9d004c6Schristos         } while (wpid != pid && errno == EINTR);
234b9d004c6Schristos         if (wpid == -1)
235b9d004c6Schristos             krb5_err(context, 1, errno, "restarter failed waiting for child");
236b9d004c6Schristos     }
237b9d004c6Schristos 
238b9d004c6Schristos     /* Finally, the child is dead and reaped */
239b9d004c6Schristos     if (WIFEXITED(status))
240b9d004c6Schristos         exit(WEXITSTATUS(status));
241b9d004c6Schristos     if (WIFSIGNALED(status)) {
242b9d004c6Schristos         switch (WTERMSIG(status)) {
243b9d004c6Schristos         case SIGTERM:
244b9d004c6Schristos         case SIGXCPU:
245b9d004c6Schristos         case SIGINT:
246b9d004c6Schristos             exit(0);
247b9d004c6Schristos         default:
248b9d004c6Schristos             /*
249b9d004c6Schristos              * Attempt to set the same exit status for the parent as for
250b9d004c6Schristos              * the child.
251b9d004c6Schristos              */
252b9d004c6Schristos             kill(getpid(), WTERMSIG(status));
253b9d004c6Schristos             /*
254b9d004c6Schristos              * We can get past the self-kill if we inherited a SIG_IGN
255b9d004c6Schristos              * disposition that the child reset to SIG_DFL.
256b9d004c6Schristos              */
257b9d004c6Schristos         }
258b9d004c6Schristos     }
259b9d004c6Schristos     exit(1);
260b9d004c6Schristos #else
261b9d004c6Schristos     if (countp != NULL)
262b9d004c6Schristos         *countp = 0;
263b9d004c6Schristos     errno = ENOTSUP;
264b9d004c6Schristos     return -1;
265b9d004c6Schristos #endif
266b9d004c6Schristos }
267b9d004c6Schristos 
268