Imported Upstream version 2.5.0
[debian/amanda] / client-src / killpgrp.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: killpgrp.c,v 1.12 2005/09/20 21:32:25 jrjackson Exp $
28  *
29  * if it is the process group leader, it kills all processes in its
30  * process group when it is killed itself.
31  */
32 #include "amanda.h"
33 #include "version.h"
34
35 #ifdef HAVE_GETPGRP
36 #ifdef GETPGRP_VOID
37 #define AM_GETPGRP() getpgrp()
38 #else
39 #define AM_GETPGRP() getpgrp(getpid())
40 #endif
41 #else
42 /* we cannot check it, so let us assume it is ok */
43 #define AM_GETPGRP() getpid()
44 #endif
45  
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));
49
50 int main(argc, argv)
51 int argc;
52 char **argv;
53 {
54     amwait_t status;
55
56     safe_fd(-1, 0);
57     safe_cd();
58
59     set_pname("killpgrp");
60
61     dbopen();
62     dbprintf(("%s: version %s\n", argv[0], version()));
63
64     if(client_uid == (uid_t) -1) {
65         error("error [cannot find user %s in passwd file]", CLIENT_LOGIN);
66     }
67
68 #ifdef FORCE_USERID
69     if (getuid() != client_uid)
70         error("error [must be invoked by %s]", CLIENT_LOGIN);
71
72     if (geteuid() != 0)
73         error("error [must be setuid root]");
74 #endif  /* FORCE_USERID */
75
76 #if !defined (DONT_SUID_ROOT)
77     setuid(0);
78 #endif
79
80     if (AM_GETPGRP() != getpid()) {
81         error("error [must be the process group leader]");
82     }
83
84     signal(SIGTERM, term_kill_soft);
85
86     while (getchar() != EOF) {
87         /* wait until EOF */
88     }
89
90     term_kill_soft(0);
91
92     for(;;) {
93         if (wait(&status) != -1)
94             break;
95         if (errno != EINTR) {
96             error("error [wait() failed: %s]", strerror(errno));
97             return -1;
98         }
99     }
100
101     dbprintf(("child process exited with status %d\n", WEXITSTATUS(status)));
102
103     return WEXITSTATUS(status);
104 }
105
106 static void term_kill_soft(sig)
107 int sig;
108 {
109     pid_t dumppid = getpid();
110     int killerr;
111
112     signal(SIGTERM, SIG_IGN);
113     signal(SIGALRM, term_kill_hard);
114     alarm(3);
115     /*
116      * First, try to kill the dump process nicely.  If it ignores us
117      * for three seconds, hit it harder.
118      */
119     dbprintf(("sending SIGTERM to process group %ld\n", (long) dumppid));
120     killerr = kill(-dumppid, SIGTERM);
121     if (killerr == -1) {
122         dbprintf(("kill failed: %s\n", strerror(errno)));
123     }
124 }
125
126 static void term_kill_hard(sig)
127 int sig;
128 {
129     pid_t dumppid = getpid();
130     int killerr;
131
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? */
136     if (killerr == -1) {
137         dbprintf(("kill failed: %s\n", strerror(errno)));
138         dbprintf(("waiting until child terminates\n"));
139     }
140 }