Imported Upstream version 2.6.0
[debian/amanda] / client-src / killpgrp.c
index 9c12141a03ff4a5542e92104d64b3ec449890a32..d7472943dcc961b8437a7ce1faa7bebc0fb3c9b7 100644 (file)
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: killpgrp.c,v 1.8.4.2.4.1 2002/10/27 14:31:18 martinea Exp $
+ * $Id: killpgrp.c,v 1.17 2006/07/25 18:27:56 martinea Exp $
  *
  * if it is the process group leader, it kills all processes in its
  * process group when it is killed itself.
+ *
+ * argv[0] is the killpgrp program name
+ * argv[1] is the config name or NOCONFIG
+ *
  */
 #include "amanda.h"
 #include "version.h"
+#include "util.h"
 
 #ifdef HAVE_GETPGRP
 #ifdef GETPGRP_VOID
 #define AM_GETPGRP() getpid()
 #endif
  
-int main P((int argc, char **argv));
-static void term_kill_soft P((int sig));
-static void term_kill_hard P((int sig));
-
-int main(argc, argv)
-int argc;
-char **argv;
+int main(int argc, char **argv);
+static void term_kill_soft(int sig);
+static void term_kill_hard(int sig);
+
+int
+main(
+    int                argc,
+    char **    argv)
 {
+    int ch;
+    char *exitstr;
     amwait_t status;
-    int fd;
-
-    for(fd = 3; fd < FD_SETSIZE; fd++) {
-       /*
-        * Make sure nobody spoofs us with a lot of extra open files
-        * that would cause an open we do to get a very high file
-        * descriptor, which in turn might be used as an index into
-        * an array (e.g. an fd_set).
-        */
-       close(fd);
-    }
 
+    /*
+     * Configure program for internationalization:
+     *   1) Only set the message locale for now.
+     *   2) Set textdomain for all amanda related programs to "amanda"
+     *      We don't want to be forced to support dozens of message catalogs.
+     */  
+    setlocale(LC_MESSAGES, "C");
+    textdomain("amanda"); 
+
+    safe_fd(-1, 0);
     safe_cd();
 
     set_pname("killpgrp");
 
-    dbopen();
-    dbprintf(("%s: version %s\n", argv[0], version()));
-
-    if(client_uid == (uid_t) -1) {
-       error("error [cannot find user %s in passwd file]", CLIENT_LOGIN);
+    dbopen(DBG_SUBDIR_CLIENT);
+    if (argc < 2) {
+       error("Need at least 2 arguments\n");
+       /*NOTREACHED*/
     }
-
-#ifdef FORCE_USERID
-    if (getuid() != client_uid) {
-       error("error [must be invoked by %s]", CLIENT_LOGIN);
+    dbprintf(_("version %s\n"), version());
+    dbprintf(_("config: %s\n"), argv[1]);
+    if (strcmp(argv[1], "NOCONFIG") != 0)
+       dbrename(argv[1], DBG_SUBDIR_CLIENT);
+
+#ifdef WANT_SETUID_CLIENT
+    check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY);
+    if (!become_root()) {
+       error(_("error [%s could not become root (is the setuid bit set?)]\n"), get_pname());
+       /*NOTREACHED*/
     }
-
-    if (geteuid() != 0) {
-       error("error [must be setuid root]");
-    }
-#endif /* FORCE_USERID */
-
-#if !defined (DONT_SUID_ROOT)
-    setuid(0);
+#else
+    check_running_as(RUNNING_AS_CLIENT_LOGIN);
 #endif
 
     if (AM_GETPGRP() != getpid()) {
-       error("error [must be the process group leader]");
+       error(_("error [must be the process group leader]"));
+       /*NOTREACHED*/
     }
 
     signal(SIGTERM, term_kill_soft);
 
-    while (getchar() != EOF) {
+    /* Consume any extranious input */
+    do {
+       ch = getchar();
        /* wait until EOF */
-    }
+    } while (ch != EOF);
 
     term_kill_soft(0);
 
@@ -105,22 +114,27 @@ char **argv;
        if (wait(&status) != -1)
            break;
        if (errno != EINTR) {
-           error("error [wait() failed: %s]", strerror(errno));
-           return -1;
+           error(_("error [wait() failed: %s]"), strerror(errno));
+           /*NOTREACHED*/
        }
     }
+    exitstr = str_exit_status("child", status);
+    dbprintf("%s\n", exitstr);
+    amfree(exitstr);
 
-    dbprintf(("child process exited with status %d\n", WEXITSTATUS(status)));
-
-    return WEXITSTATUS(status);
+    /*@ignore@*/
+    return WIFEXITED(status)?WEXITSTATUS(status):1;
+    /*@end@*/
 }
 
-static void term_kill_soft(sig)
-int sig;
+static void term_kill_soft(
+    int sig)
 {
     pid_t dumppid = getpid();
     int killerr;
 
+    (void)sig; /* Quiet unused parameter warning */
+
     signal(SIGTERM, SIG_IGN);
     signal(SIGALRM, term_kill_hard);
     alarm(3);
@@ -128,25 +142,27 @@ int sig;
      * First, try to kill the dump process nicely.  If it ignores us
      * for three seconds, hit it harder.
      */
-    dbprintf(("sending SIGTERM to process group %ld\n", (long) dumppid));
+    dbprintf(_("sending SIGTERM to process group %ld\n"), (long) dumppid);
     killerr = kill(-dumppid, SIGTERM);
     if (killerr == -1) {
-       dbprintf(("kill failed: %s\n", strerror(errno)));
+       dbprintf(_("kill failed: %s\n"), strerror(errno));
     }
 }
 
-static void term_kill_hard(sig)
-int sig;
+static void term_kill_hard(
+    int sig)
 {
     pid_t dumppid = getpid();
     int killerr;
 
-    dbprintf(("it won\'t die with SIGTERM, but SIGKILL should do\n"));
-    dbprintf(("do\'t expect any further output, this will be suicide\n"));
+    (void)sig; /* Quiet unused parameter warning */
+
+    dbprintf(_("It won\'t die with SIGTERM, but SIGKILL should do.\n"));
+    dbprintf(_("Don't expect any further output, this will be suicide.\n"));
     killerr = kill(-dumppid, SIGKILL);
     /* should never reach this point, but so what? */
     if (killerr == -1) {
-       dbprintf(("kill failed: %s\n", strerror(errno)));
-       dbprintf(("waiting until child terminates\n"));
+       dbprintf(_("kill failed: %s\n"), strerror(errno));
+       dbprintf(_("waiting until child terminates\n"));
     }
 }