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.17 2006/07/25 18:27:56 martinea Exp $
29 * if it is the process group leader, it kills all processes in its
30 * process group when it is killed itself.
32 * argv[0] is the killpgrp program name
33 * argv[1] is the config name or NOCONFIG
41 #define AM_GETPGRP() getpgrp()
43 #define AM_GETPGRP() getpgrp(getpid())
46 /* we cannot check it, so let us assume it is ok */
47 #define AM_GETPGRP() getpid()
50 int main(int argc, char **argv);
51 static void term_kill_soft(int sig);
52 static void term_kill_hard(int sig);
64 set_pname("killpgrp");
66 dbopen(DBG_SUBDIR_CLIENT);
68 error("%s: Need at least 2 arguments\n", debug_prefix(NULL));
71 dbprintf(("%s: version %s\n", debug_prefix(NULL), version()));
72 dbprintf(("config: %s\n", argv[1]));
73 if (strcmp(argv[1], "NOCONFIG") != 0)
74 dbrename(argv[1], DBG_SUBDIR_CLIENT);
76 if(client_uid == (uid_t) -1) {
77 error("error [cannot find user %s in passwd file]", CLIENT_LOGIN);
82 if (getuid() != client_uid) {
83 error("error [must be invoked by %s]", CLIENT_LOGIN);
87 error("error [must be setuid root]");
90 #endif /* FORCE_USERID */
92 #if !defined (DONT_SUID_ROOT)
96 if (AM_GETPGRP() != getpid()) {
97 error("error [must be the process group leader]");
101 /* Consume any extranious input */
102 signal(SIGTERM, term_kill_soft);
112 if (wait(&status) != -1)
114 if (errno != EINTR) {
115 error("error [wait() failed: %s]", strerror(errno));
121 dbprintf(("child process exited with status %d\n", WEXITSTATUS(status)));
123 return WEXITSTATUS(status);
127 static void term_kill_soft(
130 pid_t dumppid = getpid();
133 (void)sig; /* Quiet unused parameter warning */
135 signal(SIGTERM, SIG_IGN);
136 signal(SIGALRM, term_kill_hard);
139 * First, try to kill the dump process nicely. If it ignores us
140 * for three seconds, hit it harder.
142 dbprintf(("sending SIGTERM to process group %ld\n", (long) dumppid));
143 killerr = kill(-dumppid, SIGTERM);
145 dbprintf(("kill failed: %s\n", strerror(errno)));
149 static void term_kill_hard(
152 pid_t dumppid = getpid();
155 (void)sig; /* Quiet unused parameter warning */
157 dbprintf(("it won\'t die with SIGTERM, but SIGKILL should do\n"));
158 dbprintf(("do\'t expect any further output, this will be suicide\n"));
159 killerr = kill(-dumppid, SIGKILL);
160 /* should never reach this point, but so what? */
162 dbprintf(("kill failed: %s\n", strerror(errno)));
163 dbprintf(("waiting until child terminates\n"));