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
42 #define AM_GETPGRP() getpgrp()
44 #define AM_GETPGRP() getpgrp(getpid())
47 /* we cannot check it, so let us assume it is ok */
48 #define AM_GETPGRP() getpid()
51 int main(int argc, char **argv);
52 static void term_kill_soft(int sig);
53 static void term_kill_hard(int sig);
65 * Configure program for internationalization:
66 * 1) Only set the message locale for now.
67 * 2) Set textdomain for all amanda related programs to "amanda"
68 * We don't want to be forced to support dozens of message catalogs.
70 setlocale(LC_MESSAGES, "C");
76 set_pname("killpgrp");
78 dbopen(DBG_SUBDIR_CLIENT);
79 config_init(CONFIG_INIT_CLIENT, NULL);
82 error("Need at least 2 arguments\n");
85 dbprintf(_("version %s\n"), VERSION);
86 dbprintf(_("config: %s\n"), argv[1]);
87 if (strcmp(argv[1], "NOCONFIG") != 0)
88 dbrename(argv[1], DBG_SUBDIR_CLIENT);
90 #ifdef WANT_SETUID_CLIENT
91 check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY);
93 error(_("error [%s could not become root (is the setuid bit set?)]\n"), get_pname());
97 check_running_as(RUNNING_AS_CLIENT_LOGIN);
100 if (AM_GETPGRP() != getpid()) {
101 error(_("error [must be the process group leader]"));
105 signal(SIGTERM, term_kill_soft);
107 /* Consume any extranious input */
116 if (wait(&status) != -1)
118 if (errno != EINTR) {
119 error(_("error [wait() failed: %s]"), strerror(errno));
123 exitstr = str_exit_status("child", status);
124 dbprintf("%s\n", exitstr);
128 return WIFEXITED(status)?WEXITSTATUS(status):1;
132 static void term_kill_soft(
135 pid_t dumppid = getpid();
138 (void)sig; /* Quiet unused parameter warning */
140 signal(SIGTERM, SIG_IGN);
141 signal(SIGALRM, term_kill_hard);
144 * First, try to kill the dump process nicely. If it ignores us
145 * for three seconds, hit it harder.
147 dbprintf(_("sending SIGTERM to process group %ld\n"), (long) dumppid);
148 killerr = kill(-dumppid, SIGTERM);
150 dbprintf(_("kill failed: %s\n"), strerror(errno));
154 static void term_kill_hard(
157 pid_t dumppid = getpid();
160 (void)sig; /* Quiet unused parameter warning */
162 dbprintf(_("It won\'t die with SIGTERM, but SIGKILL should do.\n"));
163 dbprintf(_("Don't expect any further output, this will be suicide.\n"));
164 killerr = kill(-dumppid, SIGKILL);
165 /* should never reach this point, but so what? */
167 dbprintf(_("kill failed: %s\n"), strerror(errno));
168 dbprintf(_("waiting until child terminates\n"));