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.8.4.2.4.1.2.1 2005/09/20 21:31:52 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);
74 error("error [must be setuid root]");
76 #endif /* FORCE_USERID */
78 #if !defined (DONT_SUID_ROOT)
82 if (AM_GETPGRP() != getpid()) {
83 error("error [must be the process group leader]");
86 signal(SIGTERM, term_kill_soft);
88 while (getchar() != EOF) {
95 if (wait(&status) != -1)
98 error("error [wait() failed: %s]", strerror(errno));
103 dbprintf(("child process exited with status %d\n", WEXITSTATUS(status)));
105 return WEXITSTATUS(status);
108 static void term_kill_soft(sig)
111 pid_t dumppid = getpid();
114 signal(SIGTERM, SIG_IGN);
115 signal(SIGALRM, term_kill_hard);
118 * First, try to kill the dump process nicely. If it ignores us
119 * for three seconds, hit it harder.
121 dbprintf(("sending SIGTERM to process group %ld\n", (long) dumppid));
122 killerr = kill(-dumppid, SIGTERM);
124 dbprintf(("kill failed: %s\n", strerror(errno)));
128 static void term_kill_hard(sig)
131 pid_t dumppid = getpid();
134 dbprintf(("it won\'t die with SIGTERM, but SIGKILL should do\n"));
135 dbprintf(("do\'t expect any further output, this will be suicide\n"));
136 killerr = kill(-dumppid, SIGKILL);
137 /* should never reach this point, but so what? */
139 dbprintf(("kill failed: %s\n", strerror(errno)));
140 dbprintf(("waiting until child terminates\n"));