merge with 0.30.213
[util-vserver.git] / src / vkill.c
index aeabb08..57077fa 100644 (file)
@@ -1,4 +1,4 @@
-// $Id: vkill.c,v 1.1.2.2 2003/12/30 13:49:17 ensc Exp $    --*- c -*--
+// $Id: vkill.c 2491 2007-02-05 20:59:03Z dhozac $    --*- c -*--
 
 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 //  
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
-#include "compat.h"
 
 #include "vserver.h"
-#include "linuxvirtual.h"
+#include "util.h"
+
+#include "lib/virtual.h"
 
 #include <getopt.h>
 #include <signal.h>
 #include <sys/resource.h>
 #include <sys/wait.h>
 
-#define VERSION_COPYRIGHT_DISCLAIMER
+#define ENSC_WRAPPERS_VSERVER  1
+#define ENSC_WRAPPERS_UNISTD   1
+#include <wrappers.h>
 
 #define CMD_HELP       0x8000
 #define CMD_VERSION    0x8001
 
-#define WRITE_MSG(FD,X)         (void)(write(FD,X,sizeof(X)-1))
-#define WRITE_STR(FD,X)         writeStr(FD,X)
+int            wrapper_exit_code = 1;
 
 static struct option const
 CMDLINE_OPTIONS[] = {
-  { "help",     no_argument,  0, CMD_HELP },
-  { "version",  no_argument,  0, CMD_VERSION },
+  { "help",     no_argument,        0, CMD_HELP },
+  { "version",  no_argument,        0, CMD_VERSION },
+  { "xid",      required_argument,  0, 'c' },
   { 0,0,0,0 }
 };
 
 struct Arguments
 {
-    xid_t              ctx;
+    xid_t              xid;
     int                        sig;
 };
 
@@ -65,19 +68,13 @@ static char const * const SIGNALS[] = {
   0,
 };
 
-inline static void UNUSED
-writeStr(int fd, char const *cmd)
-{
-  (void)write(fd, cmd, strlen(cmd));
-}
-
 static void
 showHelp(int fd, char const *cmd, int res)
 {
   WRITE_MSG(fd, "Usage:  ");
   WRITE_STR(fd, cmd);
   WRITE_MSG(fd,
-           " [-c <ctx>] [-s <signal>] [--] <pid>*\n"
+           " [--xid|-c <xid>] [-s <signal>] [--] <pid>*\n"
            "Please report bugs to " PACKAGE_BUGREPORT "\n");
   exit(res);
 }
@@ -115,14 +112,14 @@ str2sig(char const *str)
 
 #if defined(VC_ENABLE_API_LEGACY)
 inline static ALWAYSINLINE int
-kill_wrapper_legacy(xid_t ctx, char const *proc, int sig)
+kill_wrapper_legacy(xid_t UNUSED xid, char const *proc, int UNUSED sig)
 {
-  pid_t                pid = fork();
-  if (pid==-1) {
-    perror("fork()");
-    exit(1);
-  }
-  else if (pid==0) {
+  pid_t                pid;
+
+  signal(SIGCHLD, SIG_DFL);
+  pid = Efork();
+
+  if (pid==0) {
     int                status;
     int                res;
     while ((res=wait4(pid, &status, 0,0))==-1 &&
@@ -132,21 +129,21 @@ kill_wrapper_legacy(xid_t ctx, char const *proc, int sig)
   }
 
   execl(LEGACYDIR "/vkill", "legacy/vkill", proc, (void *)(0));
-  perror("execl()");
+  perror("vkill: execl()");
   exit(1);
 }
 
 static int
-kill_wrapper(xid_t ctx, char const *pid, int sig)
+kill_wrapper(xid_t xid, char const *pid, int sig)
 {
-  //printf("kill_wrapper(%u, %s, %i)\n", ctx, pid, sig);
-  if (vc_ctx_kill(ctx,atoi(pid),sig)==-1) {
+  //printf("kill_wrapper(%u, %s, %i)\n", xid, pid, sig);
+  if (vc_ctx_kill(xid,atoi(pid),sig)==-1) {
     int                err = errno;
     if (vc_get_version(VC_CAT_COMPAT)==-1)
-      return kill_wrapper_legacy(ctx, pid, sig);
+      return kill_wrapper_legacy(xid, pid, sig);
     else {
       errno = err;
-      perror("vc_ctx_kill()");
+      perror("vkill: vc_ctx_kill()");
       return 1;
     }
   }
@@ -155,10 +152,22 @@ kill_wrapper(xid_t ctx, char const *pid, int sig)
 }
 #else // VC_ENABLE_API_LEGACY
 inline static int
-kill_wrapper(xid_t ctx, char const *pid, int sig)
+kill_wrapper(xid_t xid, char const *pid_s, int sig)
 {
-  if (vc_ctx_kill(ctx,atoi(pid),sig)==-1) {
-    perror("vc_ctx_kill()");
+  pid_t        pid;
+  long tmp;
+
+  if (!isNumber(pid_s, &tmp, true)) {
+    WRITE_MSG(2, "vkill: '");
+    WRITE_STR(2, pid_s);
+    WRITE_MSG(2, "' is not a number\n");
+  }
+  pid = (pid_t) tmp;
+
+  if (xid==VC_NOCTX)
+    xid = vc_get_task_xid(pid);
+  if (vc_ctx_kill(xid,pid,sig)==-1) {
+    perror("vkill: vc_ctx_kill()");
     return 1;
   }
   return 0;
@@ -170,7 +179,7 @@ int main(int argc, char *argv[])
 {
   int                  fail = 0;
   struct Arguments     args = {
-    .ctx = VC_NOCTX,
+    .xid = VC_NOCTX,
     .sig = SIGTERM,
   };
   
@@ -181,12 +190,12 @@ int main(int argc, char *argv[])
     switch (c) {
       case CMD_HELP    :  showHelp(1, argv[0], 0);
       case CMD_VERSION :  showVersion();
-      case 'c'         :  args.ctx = atoi(optarg);    break;
-      case 's'         :  args.sig = str2sig(optarg); break;
+      case 'c'         :  args.xid = Evc_xidopt2xid(optarg,true); break;
+      case 's'         :  args.sig = str2sig(optarg);             break;
       default          :
        WRITE_MSG(2, "Try '");
        WRITE_STR(2, argv[0]);
-       WRITE_MSG(2, " --help\" for more information.\n");
+       WRITE_MSG(2, " --help' for more information.\n");
        return EXIT_FAILURE;
        break;
     }
@@ -197,15 +206,15 @@ int main(int argc, char *argv[])
     return EXIT_FAILURE;
   }
 
-  if (args.ctx==VC_NOCTX && optind==argc) {
+  if (args.xid==VC_NOCTX && optind==argc) {
     WRITE_MSG(2, "No pid specified\n");
     return EXIT_FAILURE;
   }
 
   if (optind==argc)
-    fail += kill_wrapper(args.ctx, "0", args.sig);
+    fail += kill_wrapper(args.xid, "0", args.sig);
   else for (;optind<argc;++optind)
-    fail += kill_wrapper(args.ctx, argv[optind], args.sig);
+    fail += kill_wrapper(args.xid, argv[optind], args.sig);
 
   return fail==0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }