added yum-2.6.0-chroot.patch
[util-vserver.git] / util-vserver / src / vserver-info.c
index 26f3d70..d695fee 100644 (file)
 #include <dirent.h>
 #include <strings.h>
 
-#include <sys/capability.h>
-
-
 #define ENSC_WRAPPERS_FCNTL    1
 #define ENSC_WRAPPERS_IO       1
 #define ENSC_WRAPPERS_UNISTD   1
 #define ENSC_WRAPPERS_VSERVER  1
 #include <wrappers.h>
 
+#undef _POSIX_SOURCE
+#include "capability-compat.h"
+
 typedef enum { tgNONE,tgCONTEXT, tgID, tgRUNNING,
               tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
               tgAPIVER, tgPXID,
               tgINITPID, tgINITPID_PID,
               tgXID, tgUTS, tgSYSINFO,
               tgFEATURE, tgCANONIFY,
-              tgVERIFYCAP,
+              tgVERIFYCAP, tgXIDTYPE, tgVERIFYPROC,
 }      VserverTag;
 
 static struct {
@@ -80,6 +80,8 @@ static struct {
   { "PXID",        tgPXID,        "returns the xid of the parent context" },
   { "CANONIFY",    tgCANONIFY,    "canonifies the vserver-name and removes dangerous characters" },
   { "VERIFYCAP",   tgVERIFYCAP,   "test if the kernel supports linux capabilities" },
+  { "VERIFYPROC",  tgVERIFYPROC,  "test if /proc can be read by contexts!=0" },
+  { "XIDTYPE",     tgXIDTYPE,     "returns the type of the given XID" },
 };
 
 int wrapper_exit_code = 1;
@@ -158,6 +160,26 @@ utsText2Tag(char const *str)
 }
 
 static bool
+verifyProc()
+{
+  char const           *errptr;
+  
+  if (!switchToWatchXid(&errptr)) {
+    perror(errptr);
+    return false;
+  }
+
+  if (access("/proc/uptime", R_OK)==-1) {
+    if (errno!=ENOENT)
+      perror("access(\"/proc/uptime\")");
+    
+    return false;
+  }
+
+  return true;
+}
+
+static bool
 verifyCap()
 {
   struct __user_cap_header_struct header;
@@ -209,9 +231,9 @@ getAPIVer(char *buf)
 }
 
 static char *
-getXid(char *buf, char const *vserver)
+getXid(char *buf, char const *pid_str)
 {
-  pid_t                pid = atoi(vserver);
+  pid_t                pid = atoi(pid_str);
   xid_t                xid = vc_get_task_xid(pid);
 
   if (xid==VC_NOCTX) perror("vc_get_task_xid()");
@@ -298,6 +320,13 @@ getInitPid_emulated(char *buf, xid_t xid)
 
   return 0;
 }
+#else // VC_ENABLE_API_COMPAT
+static char *
+getInitPid_emulated(char UNUSED *buf, xid_t UNUSED xid)
+{
+  WRITE_MSG(2, "tools were built without compat API, getInitPid() not available\n");
+  return 0;
+}
 #endif // VC_ENABLE_API_COMPAT
 
 static char *
@@ -412,6 +441,31 @@ getContext(char *buf, char const *vserver, bool allow_only_static)
   return buf;
 }
 
+static char const *
+getXIDType(xid_t xid, int argc, char *argv[])
+{
+  char const *         tp;
+  
+  switch (vc_getXIDType(xid)) {
+    case vcTYPE_INVALID                :  tp = "invalid"; break;
+    case vcTYPE_MAIN           :  tp = "main";    break;
+    case vcTYPE_WATCH          :  tp = "watch";   break;
+    case vcTYPE_STATIC         :  tp = "static";  break;
+    case vcTYPE_DYNAMIC                :  tp = "dynamic"; break;
+    default                    :  tp = 0;         break;
+  }
+
+  if (argc==0 || tp==0)
+    return tp;
+
+  while (argc>0) {
+    --argc;
+    if (strcasecmp(argv[argc], tp)==0) return tp;
+  }
+
+  return 0;
+}
+
 static int
 testFeature(int argc, char *argv[])
 {
@@ -421,7 +475,7 @@ testFeature(int argc, char *argv[])
 static bool
 str2bool(char const *str)
 {
-  return atoi(str)!=0 || strchr("yYtY", str[0])!=0 || strcasecmp("true", str)==0;
+  return atoi(str)!=0 || strchr("yYtT", str[0])!=0 || strcasecmp("true", str)==0;
 }
 
 static int
@@ -441,9 +495,17 @@ execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
       break;
       
-    case tgRUNNING     :
-      res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
+    case tgRUNNING     : {
+      signed long              xid;    // type is a small hack, but should be ok...
+      struct vc_vx_info                info;
+       
+      if (isNumber(vserver, &xid, true) && xid>=0)
+       res = (vc_get_vx_info(xid, &info)==-1) ? 0 : "1";
+      else
+       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
+      
       break;
+    }
 
     case tgCANONIFY    :
       strcpy(buf, vserver);
@@ -459,6 +521,7 @@ execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
     case tgSYSINFO     :  return printSysInfo(buf);           break;
     case tgFEATURE     :  return testFeature(argc,argv);      break;
     case tgVERIFYCAP   :  return verifyCap() ? 0 : 1;         break;
+    case tgVERIFYPROC  :  return verifyProc() ? 0 : 1;        break;
 
 
     default            : {
@@ -468,6 +531,7 @@ execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
        case tgID       :  res = vc_getVserverByCtx(xid,0,0);  break;
        case tgINITPID  :  res = getInitPid(buf, xid);         break;
        case tgUTS      :  res = getUTS(buf, xid, argc, argv); break;
+       case tgXIDTYPE  :  res = getXIDType(xid, argc, argv);  break;
     
        default         :  assert(false); abort();  // TODO
       }