X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fsrc%2Fvserver-info.c;h=eb63df4981672a7f1d01c68363370ec69a6cf71c;hb=e035f164c858115c488d0f2fc1e0d356a9096996;hp=10ea63b32a0c5a6a6353c20e4588c5c22772f8df;hpb=d889623dae96e7f4138b5c6946a1c53468025aed;p=util-vserver.git diff --git a/util-vserver/src/vserver-info.c b/util-vserver/src/vserver-info.c index 10ea63b..eb63df4 100644 --- a/util-vserver/src/vserver-info.c +++ b/util-vserver/src/vserver-info.c @@ -20,8 +20,8 @@ # include #endif +#include "pathconfig.h" #include "util.h" -#include "wrappers.h" #include "internal.h" #include "vserver.h" @@ -30,12 +30,21 @@ #include #include #include +#include +#include +#include + +#define ENSC_WRAPPERS_FCNTL 1 +#define ENSC_WRAPPERS_IO 1 +#define ENSC_WRAPPERS_UNISTD 1 +#include typedef enum { tgNONE,tgCONTEXT, tgRUNNING, tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR, tgAPIVER, tgINITPID, tgINITPID_PID, - tgXID, + tgXID, tgUTS, tgSYSINFO, + tgFEATURE, } VserverTag; static struct { @@ -52,7 +61,12 @@ static struct { { "INITPID", tgINITPID, "gives out the initpid of the given context" }, { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" }, { "XID", tgXID, "gives out the context-id of the given pid" }, - { "APIVER", tgAPIVER, "gives out the version of the kernel API" } + { "APIVER", tgAPIVER, "gives out the version of the kernel API" }, + { "UTS", tgUTS, ("gives out an uts-entry; possible entries are " + "context, sysname, nodename, release, version, " + "machine and domainname") }, + { "SYSINFO", tgSYSINFO, "gives out information about the systen" }, + { "FEATURE", tgFEATURE, "returns 0 iff the queried feature is supported" }, }; int wrapper_exit_code = 1; @@ -114,12 +128,181 @@ stringToTag(char const *str) return tgNONE; } +static vc_uts_type +utsText2Tag(char const *str) +{ + if (strcmp(str, "context") ==0) return vcVHI_CONTEXT; + else if (strcmp(str, "sysname") ==0) return vcVHI_SYSNAME; + else if (strcmp(str, "nodename") ==0) return vcVHI_NODENAME; + else if (strcmp(str, "release") ==0) return vcVHI_RELEASE; + else if (strcmp(str, "version") ==0) return vcVHI_VERSION; + else if (strcmp(str, "machine") ==0) return vcVHI_MACHINE; + else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME; + else { + WRITE_MSG(2, "Unknown UTS tag\n"); + exit(1); + } +} + +static char * +getAPIVer(char *buf) +{ + int v = vc_get_version(); + size_t l; + + if (v==-1) return 0; + + + l = utilvserver_fmt_xulong(0, (unsigned int)v); + memcpy(buf, "0x00000000", 10); + utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v); + + return buf; +} + +static char * +getXid(char *buf, char const *vserver) +{ + pid_t pid = atoi(vserver); + xid_t xid = vc_get_task_xid(pid); + + if (xid==VC_NOCTX) perror("vc_get_task_xid()"); + else { + utilvserver_fmt_long(buf, xid); + return buf; + } + + return 0; +} + +static char * +getInitPid(char *buf, xid_t xid) +{ + struct vc_vx_info info; + + if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()"); + else { + utilvserver_fmt_long(buf, info.initpid); + return buf; + } + + return 0; +} + +static char * +getInitPidPid(char *buf, char const *vserver) +{ + struct vc_vx_info info; + pid_t pid = atoi(vserver); + xid_t xid = vc_get_task_xid(pid); + + if (xid==VC_NOCTX) perror("vc_get_task_xid()"); + else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()"); + else { + utilvserver_fmt_long(buf, info.initpid); + return buf; + } + + return 0; +} + +static char * +getUTS(char *buf, xid_t xid, size_t argc, char * argv[]) +{ + if (argc>0) { + vc_uts_type type = utsText2Tag(argv[0]); + if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1) + perror("vc_get_vhi_name()"); + else + return buf; + } + else { + bool is_passed = false; + char tmp[128]; +#define APPEND_UTS(TYPE) \ + (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \ + (strcat(buf, "??? "))) + + if (APPEND_UTS(vcVHI_CONTEXT) && + APPEND_UTS(vcVHI_SYSNAME) && + APPEND_UTS(vcVHI_NODENAME) && + APPEND_UTS(vcVHI_RELEASE) && + APPEND_UTS(vcVHI_VERSION) && + APPEND_UTS(vcVHI_MACHINE) && + APPEND_UTS(vcVHI_DOMAINNAME) && + is_passed) + return buf; + + perror("vc_get_vhi_name()"); +#undef APPEND_UTS + } + + return 0; +} + +static int +printSysInfo(char *buf) +{ + int fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY); + struct utsname uts; + + if (uname(&uts)==-1) + perror("uname()"); + else { + WRITE_MSG(1, + "Versions:\n" + " Kernel: "); + WRITE_STR(1, uts.release); + WRITE_MSG(1, "\n" + " VS-API: "); + + memset(buf, 0, 128); + if (getAPIVer(buf)) WRITE_STR(1, buf); + else WRITE_MSG(1, "???"); + + WRITE_MSG(1, "\n" + " util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n" + "\n"); + } + + if (fd==-1) + WRITE_MSG(1, "FEATURES.txt not found\n"); + else { + off_t l = Elseek(fd, 0, SEEK_END); + Elseek(fd, 0, SEEK_SET); + { + char buf[l]; + EreadAll(fd, buf, l); + EwriteAll(1, buf, l); + } + Eclose(fd); + } + + return EXIT_SUCCESS; +} + +static char * +getContext(char *buf, char const *vserver) +{ + xid_t xid = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0); + if (xid==VC_NOCTX) return 0; + + utilvserver_fmt_long(buf, xid); + return buf; +} + +static int +testFeature(int argc, char *argv[]) +{ + return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE; +} + static int execQuery(char const *vserver, VserverTag tag, int argc, char *argv[]) { char const * res = 0; - char buf[sizeof(xid_t)*4 + 16]; - xid_t ctx; + char buf[sizeof(xid_t)*4 + 1024]; + xid_t xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX; memset(buf, 0, sizeof buf); switch (tag) { @@ -132,68 +315,19 @@ execQuery(char const *vserver, VserverTag tag, int argc, char *argv[]) res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]); break; - case tgCONTEXT : - ctx = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0); - if (ctx!=VC_NOCTX) { - utilvserver_fmt_long(buf, ctx); - res = buf; - } - break; - case tgRUNNING : res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1"; break; - case tgXID : - { - pid_t pid = atoi(vserver); - xid_t xid = vc_get_task_xid(pid); - if (xid==VC_NOCTX) perror("vc_get_task_xid()"); - else { - utilvserver_fmt_long(buf, xid); - res = buf; - } - break; - } - - case tgINITPID : - { - xid_t xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX; - struct vc_vx_info info; - if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()"); - else { - utilvserver_fmt_long(buf, info.xid); - res = buf; - } - break; - } - - case tgINITPID_PID : - { - pid_t pid = atoi(vserver); - xid_t xid = vc_get_task_xid(pid); - struct vc_vx_info info; - - if (xid==VC_NOCTX) perror("vc_get_task_xid()"); - else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()"); - else { - utilvserver_fmt_long(buf, info.xid); - res = buf; - } - break; - } - - case tgAPIVER : - { - int v = vc_get_version(); - if (v!=-1) { - size_t l = utilvserver_fmt_xulong(0, (unsigned int)v); - memcpy(buf, "0x00000000", 10); - utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v); - res = buf; - } - break; - } + case tgCONTEXT : res = getContext(buf, vserver); break; + case tgXID : res = getXid(buf, vserver); break; + case tgINITPID : res = getInitPid(buf, xid); break; + case tgINITPID_PID : res = getInitPidPid(buf, vserver); break; + case tgAPIVER : res = getAPIVer(buf); break; + case tgUTS : res = getUTS(buf, xid, argc, argv); break; + case tgSYSINFO : return printSysInfo(buf); break; + case tgFEATURE : return testFeature(argc,argv); break; + default : assert(false); abort(); // TODO }