f21944f1fd6bb0cf5ff73383246e8b6738ace175
[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.8.4.2.4.1.2.1 2005/09/20 21:31:52 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
73     if (geteuid() != 0) {
74         error("error [must be setuid root]");
75     }
76 #endif  /* FORCE_USERID */
77
78 #if !defined (DONT_SUID_ROOT)
79     setuid(0);
80 #endif
81
82     if (AM_GETPGRP() != getpid()) {
83         error("error [must be the process group leader]");
84     }
85
86     signal(SIGTERM, term_kill_soft);
87
88     while (getchar() != EOF) {
89         /* wait until EOF */
90     }
91
92     term_kill_soft(0);
93
94     for(;;) {
95         if (wait(&status) != -1)
96             break;
97         if (errno != EINTR) {
98             error("error [wait() failed: %s]", strerror(errno));
99             return -1;
100         }
101     }
102
103     dbprintf(("child process exited with status %d\n", WEXITSTATUS(status)));
104
105     return WEXITSTATUS(status);
106 }
107
108 static void term_kill_soft(sig)
109 int sig;
110 {
111     pid_t dumppid = getpid();
112     int killerr;
113
114     signal(SIGTERM, SIG_IGN);
115     signal(SIGALRM, term_kill_hard);
116     alarm(3);
117     /*
118      * First, try to kill the dump process nicely.  If it ignores us
119      * for three seconds, hit it harder.
120      */
121     dbprintf(("sending SIGTERM to process group %ld\n", (long) dumppid));
122     killerr = kill(-dumppid, SIGTERM);
123     if (killerr == -1) {
124         dbprintf(("kill failed: %s\n", strerror(errno)));
125     }
126 }
127
128 static void term_kill_hard(sig)
129 int sig;
130 {
131     pid_t dumppid = getpid();
132     int killerr;
133
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? */
138     if (killerr == -1) {
139         dbprintf(("kill failed: %s\n", strerror(errno)));
140         dbprintf(("waiting until child terminates\n"));
141     }
142 }