2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: killpgrp.c,v 1.12 2005/09/20 21:32:25 jrjackson Exp $
29 * if it is the process group leader, it kills all processes in its
30 * process group when it is killed itself.
37 #define AM_GETPGRP() getpgrp()
39 #define AM_GETPGRP() getpgrp(getpid())
42 /* we cannot check it, so let us assume it is ok */
43 #define AM_GETPGRP() getpid()
46 int main P((int argc, char **argv));
47 static void term_kill_soft P((int sig));
48 static void term_kill_hard P((int sig));
59 set_pname("killpgrp");
62 dbprintf(("%s: version %s\n", argv[0], version()));
64 if(client_uid == (uid_t) -1) {
65 error("error [cannot find user %s in passwd file]", CLIENT_LOGIN);
69 if (getuid() != client_uid)
70 error("error [must be invoked by %s]", CLIENT_LOGIN);
73 error("error [must be setuid root]");
74 #endif /* FORCE_USERID */
76 #if !defined (DONT_SUID_ROOT)
80 if (AM_GETPGRP() != getpid()) {
81 error("error [must be the process group leader]");
84 signal(SIGTERM, term_kill_soft);
86 while (getchar() != EOF) {
93 if (wait(&status) != -1)
96 error("error [wait() failed: %s]", strerror(errno));
101 dbprintf(("child process exited with status %d\n", WEXITSTATUS(status)));
103 return WEXITSTATUS(status);
106 static void term_kill_soft(sig)
109 pid_t dumppid = getpid();
112 signal(SIGTERM, SIG_IGN);
113 signal(SIGALRM, term_kill_hard);
116 * First, try to kill the dump process nicely. If it ignores us
117 * for three seconds, hit it harder.
119 dbprintf(("sending SIGTERM to process group %ld\n", (long) dumppid));
120 killerr = kill(-dumppid, SIGTERM);
122 dbprintf(("kill failed: %s\n", strerror(errno)));
126 static void term_kill_hard(sig)
129 pid_t dumppid = getpid();
132 dbprintf(("it won\'t die with SIGTERM, but SIGKILL should do\n"));
133 dbprintf(("do\'t expect any further output, this will be suicide\n"));
134 killerr = kill(-dumppid, SIGKILL);
135 /* should never reach this point, but so what? */
137 dbprintf(("kill failed: %s\n", strerror(errno)));
138 dbprintf(("waiting until child terminates\n"));