3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "lib/utils-legacy.h"
24 #include "pathconfig.h"
36 #include <sys/utsname.h>
40 #define ENSC_WRAPPERS_FCNTL 1
41 #define ENSC_WRAPPERS_IO 1
42 #define ENSC_WRAPPERS_UNISTD 1
43 #define ENSC_WRAPPERS_VSERVER 1
47 #include "capability-compat.h"
49 typedef enum { tgNONE,tgCONTEXT, tgID, tgRUNNING,
50 tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
52 tgINITPID, tgINITPID_PID,
53 tgXID, tgUTS, tgSYSINFO,
54 tgFEATURE, tgCANONIFY,
55 tgVERIFYCAP, tgXIDTYPE, tgVERIFYPROC,
60 char const * const tag;
62 char const * const descr;
64 { "CONTEXT", tgCONTEXT, ("the current and/or assigned context; when an optinal argument "
65 "evaluates to false,only the current context will be printed") },
66 { "ID", tgID, "gives out the vserver-id for the context-xid" },
67 { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
68 { "VDIR", tgVDIR, "gives out the root-directory of the vserver" },
69 { "NAME", tgNAME, "gives out the name of the vserver" },
70 { "CFGDIR", tgCFGDIR, "gives out the configuration directory of the vserver" },
71 { "APPDIR", tgAPPDIR, "gives out the name of the toplevel application cfgdir" },
72 { "INITPID", tgINITPID, "gives out the initpid of the given context" },
73 { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
74 { "XID", tgXID, "gives out the context-id of the given pid" },
75 { "APIVER", tgAPIVER, "gives out the version of the kernel API" },
76 { "UTS", tgUTS, ("gives out an uts-entry; possible entries are "
77 "context, sysname, nodename, release, version, "
78 "machine and domainname") },
79 { "SYSINFO", tgSYSINFO, "gives out information about the systen" },
80 { "FEATURE", tgFEATURE, "returns 0 iff the queried feature is supported" },
81 { "PXID", tgPXID, "returns the xid of the parent context" },
82 { "CANONIFY", tgCANONIFY, "canonifies the vserver-name and removes dangerous characters" },
83 { "VERIFYCAP", tgVERIFYCAP, "test if the kernel supports linux capabilities" },
84 { "VERIFYPROC", tgVERIFYPROC, "test if /proc can be read by contexts!=0" },
85 { "XIDTYPE", tgXIDTYPE, "returns the type of the given XID" },
86 { "NID", tgNID, "outputs the network context-id of the given pid" },
87 { "TAG", tgTAG, "outputs the filesystem tag of the given pid" },
90 int wrapper_exit_code = 1;
92 static struct option const
94 { "help", no_argument, 0, 'h' },
95 { "version", no_argument, 0, 'v' },
101 showHelp(int fd, char const *cmd, int res)
103 WRITE_MSG(fd, "Usage: ");
106 " [-ql] <vserver>|<pid>|<context> <tag>\n"
107 "Please report bugs to " PACKAGE_BUGREPORT "\n");
115 "vserver-info " VERSION " -- returns information about vservers\n"
116 "This program is part of " PACKAGE_STRING "\n\n"
117 "Copyright (C) 2003 Enrico Scholz\n"
118 VERSION_COPYRIGHT_DISCLAIMER);
125 char const * delim = "";
128 WRITE_MSG(1, "Valid tags are: ");
129 for (i=0; i<DIM_OF(TAGS); ++i) {
131 WRITE_STR(1, TAGS[i].tag);
140 stringToTag(char const *str)
143 for (i=0; i<DIM_OF(TAGS); ++i)
144 if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
150 utsText2Tag(char const *str)
152 if (strcmp(str, "context") ==0) return vcVHI_CONTEXT;
153 else if (strcmp(str, "sysname") ==0) return vcVHI_SYSNAME;
154 else if (strcmp(str, "nodename") ==0) return vcVHI_NODENAME;
155 else if (strcmp(str, "release") ==0) return vcVHI_RELEASE;
156 else if (strcmp(str, "version") ==0) return vcVHI_VERSION;
157 else if (strcmp(str, "machine") ==0) return vcVHI_MACHINE;
158 else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
160 WRITE_MSG(2, "Unknown UTS tag\n");
170 if (!switchToWatchXid(&errptr)) {
175 if (access("/proc/uptime", R_OK)==-1) {
177 perror("access(\"/proc/uptime\")");
189 struct __user_cap_header_struct header;
190 struct __user_cap_data_struct user[2];
192 header.version = _LINUX_CAPABILITY_VERSION_2;
196 WRITE_MSG(2, "'VERIFYCAP' can be executed as root only\n");
200 // if( prctl( PR_SET_KEEPCAPS, 1,0,0,0 ) < 0 ) {
201 // perror( "prctl:" );
206 if (capget(&header, user)==-1) {
208 header.version != _LINUX_CAPABILITY_VERSION_2) {
209 header.version = _LINUX_CAPABILITY_VERSION_1;
217 user[0].effective = user[1].effective = 0;
218 user[0].permitted = user[1].permitted = 0;
219 user[0].inheritable = user[1].inheritable = 0;
221 if (capset(&header, user)==-1) {
226 return chroot("/")==-1;
232 int v = vc_get_version();
238 l = utilvserver_fmt_xulong(0, (unsigned int)v);
239 memcpy(buf, "0x00000000", 10);
240 utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
246 getCtxId(char *buf, const char *pid_str, xid_t (*get_id)(pid_t pid), const char *err_str)
248 pid_t pid = atoi(pid_str);
249 xid_t xid = get_id(pid);
251 if (xid==VC_NOCTX) perror(err_str);
253 utilvserver_fmt_long(buf, xid);
261 getXid(char *buf, char const *pid_str)
263 return getCtxId(buf, pid_str, vc_get_task_xid, "vc_get_task_xid()");
267 getNid(char *buf, const char *pid_str)
269 return getCtxId(buf, pid_str, vc_get_task_nid, "vc_get_task_nid()");
273 getTag(char *buf, const char *pid_str)
275 return getCtxId(buf, pid_str, vc_get_task_tag, "vc_get_task_tag()");
279 getPXid(char UNUSED *buf, char const UNUSED *vserver)
281 // TODO: implement me when available
286 getInitPid_native(char *buf, xid_t xid)
288 struct vc_vx_info info;
290 if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
292 utilvserver_fmt_long(buf, info.initpid);
299 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_V11)
301 selectPid(struct dirent const *ent)
303 return atoi(ent->d_name)!=0;
307 getInitPid_internal(pid_t pid, xid_t xid, pid_t *res)
312 size_t bufsize = utilvserver_getProcEntryBufsize();
316 pos = utilvserver_getProcEntry(pid, "\ns_context: ", buf, bufsize);
317 if (pos==0 && errno==EAGAIN) continue;
319 if (pos==0 || (xid_t)atoi(pos)!=xid) return false;
322 pos = strstr(buf, "\ninitpid: ");
325 pos += sizeof("\ninitpid: ")-1;
326 if (strncmp(pos, "none", 4)==0) *res = -1;
327 else *res = atoi(pos);
335 getInitPid_emulated(char *buf, xid_t xid)
337 struct dirent **namelist;
340 switchToWatchXid(0); // ignore errors silently...
341 n = scandir("/proc", &namelist, selectPid, alphasort);
342 if (n<0) perror("scandir()");
345 if (!getInitPid_internal(atoi(namelist[n]->d_name), xid, &pid)) continue;
347 utilvserver_fmt_long(buf, pid);
353 #else // VC_ENABLE_API_COMPAT
355 getInitPid_emulated(char UNUSED *buf, xid_t UNUSED xid)
357 WRITE_MSG(2, "tools were built without compat API, getInitPid() not available\n");
360 #endif // VC_ENABLE_API_COMPAT
363 getInitPid(char *buf, xid_t xid)
365 if (vc_isSupported(vcFEATURE_VINFO))
366 return getInitPid_native(buf, xid);
368 return getInitPid_emulated(buf, xid);
372 getInitPidPid(char *buf, char const *vserver)
374 struct vc_vx_info info;
375 pid_t pid = atoi(vserver);
376 xid_t xid = vc_get_task_xid(pid);
378 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
379 else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
381 utilvserver_fmt_long(buf, info.initpid);
389 getUTS(char *buf, xid_t xid, size_t argc, char * argv[])
392 vc_uts_type type = utsText2Tag(argv[0]);
393 if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
394 perror("vc_get_vhi_name()");
399 bool is_passed = false;
401 #define APPEND_UTS(TYPE) \
402 (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
403 (strcat(buf, "??? ")))
405 if (APPEND_UTS(vcVHI_CONTEXT) &&
406 APPEND_UTS(vcVHI_SYSNAME) &&
407 APPEND_UTS(vcVHI_NODENAME) &&
408 APPEND_UTS(vcVHI_RELEASE) &&
409 APPEND_UTS(vcVHI_VERSION) &&
410 APPEND_UTS(vcVHI_MACHINE) &&
411 APPEND_UTS(vcVHI_DOMAINNAME) &&
415 perror("vc_get_vhi_name()");
423 printSysInfo(char *buf)
425 int fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY);
434 WRITE_STR(1, uts.release);
439 if (getAPIVer(buf)) WRITE_STR(1, buf);
440 else WRITE_MSG(1, "???");
443 " util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n"
448 WRITE_MSG(1, "FEATURES.txt not found\n");
450 off_t l = Elseek(fd, 0, SEEK_END);
451 Elseek(fd, 0, SEEK_SET);
454 EreadAll(fd, buf, l);
455 EwriteAll(1, buf, l);
464 getContext(char *buf, char const *vserver, bool allow_only_static)
466 xid_t xid = vc_getVserverCtx(vserver, vcCFG_AUTO,
467 allow_only_static, 0, vcCTX_XID);
468 if (xid==VC_NOCTX) return 0;
470 utilvserver_fmt_long(buf, xid);
475 getXIDType(xid_t xid, int argc, char *argv[])
479 switch (vc_getXIDType(xid)) {
480 case vcTYPE_INVALID : tp = "invalid"; break;
481 case vcTYPE_MAIN : tp = "main"; break;
482 case vcTYPE_WATCH : tp = "watch"; break;
483 case vcTYPE_STATIC : tp = "static"; break;
484 case vcTYPE_DYNAMIC : tp = "dynamic"; break;
485 default : tp = 0; break;
488 if (argc==0 || tp==0)
493 if (strcasecmp(argv[argc], tp)==0) return tp;
500 testFeature(int argc, char *argv[])
502 return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE;
506 str2bool(char const *str)
508 return atoi(str)!=0 || strchr("yYtT", str[0])!=0 || strcasecmp("true", str)==0;
512 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
514 char const * res = 0;
515 char buf[sizeof(xid_t)*4 + 1024 + strlen(vserver)];
517 memset(buf, 0, sizeof buf);
519 case tgNAME : res = vc_getVserverName(vserver, vcCFG_AUTO); break;
521 res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
523 case tgCFGDIR : res = vc_getVserverCfgDir(vserver, vcCFG_AUTO); break;
525 res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
529 signed long xid; // type is a small hack, but should be ok...
530 struct vc_vx_info info;
532 if (isNumber(vserver, &xid, true) && xid>=0)
533 res = (vc_get_vx_info(xid, &info)==-1) ? 0 : "1";
535 res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0, vcCTX_XID)==VC_NOCTX) ? 0 : "1";
541 strcpy(buf, vserver);
542 if (canonifyVserverName(buf)>0) res = buf;
545 case tgCONTEXT : res = getContext(buf, vserver,
546 argc==0 || str2bool(argv[0])); break;
547 case tgINITPID_PID : res = getInitPidPid(buf, vserver); break;
548 case tgAPIVER : res = getAPIVer(buf); break;
549 case tgXID : res = getXid(buf, vserver); break;
550 case tgPXID : res = getPXid(buf, vserver); break;
551 case tgSYSINFO : return printSysInfo(buf); break;
552 case tgFEATURE : return testFeature(argc,argv); break;
553 case tgVERIFYCAP : return verifyCap() ? 0 : 1; break;
554 case tgVERIFYPROC : return verifyProc() ? 0 : 1; break;
555 case tgNID : res = getNid(buf, vserver); break;
556 case tgTAG : res = getTag(buf, vserver); break;
560 xid_t xid = *vserver!='\0' ? vc_xidopt2xid(vserver,true,0) : VC_SAMECTX;
563 case tgID : res = vc_getVserverByCtx(xid,0,0); break;
564 case tgINITPID : res = getInitPid(buf, xid); break;
565 case tgUTS : res = getUTS(buf, xid, argc, argv); break;
566 case tgXIDTYPE : res = getXIDType(xid, argc, argv); break;
568 default : assert(false); abort(); // TODO
573 if (res==0) return EXIT_FAILURE;
579 int main(int argc, char *argv[])
582 char const * vserver;
586 int c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
590 case 'h' : showHelp(1, argv[0], 0);
591 case 'v' : showVersion();
592 case 'l' : showTags();
593 case 'q' : quiet = true; break;
595 WRITE_MSG(2, "Try '");
596 WRITE_STR(2, argv[0]);
597 WRITE_MSG(2, " --help' for more information.\n");
604 execQuery("-", tgSYSINFO, 0, 0);
605 WRITE_MSG(2, "\nAssumed 'SYSINFO' as no other option given; try '--help' for more information.\n");
609 vserver = argv[optind];
610 tag = stringToTag(argv[optind+1]);
613 WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
618 int fd = Eopen("/dev/null", O_WRONLY, 0644);
623 return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);