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
46 typedef enum { tgNONE,tgCONTEXT, tgID, tgRUNNING,
47 tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
49 tgINITPID, tgINITPID_PID,
50 tgXID, tgUTS, tgSYSINFO,
55 char const * const tag;
57 char const * const descr;
59 { "CONTEXT", tgCONTEXT, ("the current and/or assigned context; when an optinal argument "
60 "evaluates to false,only the current context will be printed") },
61 { "ID", tgID, "gives out the vserver-id for the context-xid" },
62 { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
63 { "VDIR", tgVDIR, "gives out the root-directory of the vserver" },
64 { "NAME", tgNAME, "gives out the name of the vserver" },
65 { "CFGDIR", tgCFGDIR, "gives out the configuration directory of the vserver" },
66 { "APPDIR", tgAPPDIR, "gives out the name of the toplevel application cfgdir" },
67 { "INITPID", tgINITPID, "gives out the initpid of the given context" },
68 { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
69 { "XID", tgXID, "gives out the context-id of the given pid" },
70 { "APIVER", tgAPIVER, "gives out the version of the kernel API" },
71 { "UTS", tgUTS, ("gives out an uts-entry; possible entries are "
72 "context, sysname, nodename, release, version, "
73 "machine and domainname") },
74 { "SYSINFO", tgSYSINFO, "gives out information about the systen" },
75 { "FEATURE", tgFEATURE, "returns 0 iff the queried feature is supported" },
76 { "PXID", tgPXID, "returns the xid of the parent context" },
79 int wrapper_exit_code = 1;
81 static struct option const
83 { "help", no_argument, 0, 'h' },
84 { "version", no_argument, 0, 'v' },
90 showHelp(int fd, char const *cmd, int res)
92 WRITE_MSG(fd, "Usage: ");
95 " [-ql] <vserver>|<pid>|<context> <tag>\n"
96 "Please report bugs to " PACKAGE_BUGREPORT "\n");
104 "vserver-info " VERSION " -- returns information about vservers\n"
105 "This program is part of " PACKAGE_STRING "\n\n"
106 "Copyright (C) 2003 Enrico Scholz\n"
107 VERSION_COPYRIGHT_DISCLAIMER);
114 char const * delim = "";
117 WRITE_MSG(1, "Valid tags are: ");
118 for (i=0; i<DIM_OF(TAGS); ++i) {
120 WRITE_STR(1, TAGS[i].tag);
129 stringToTag(char const *str)
132 for (i=0; i<DIM_OF(TAGS); ++i)
133 if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
139 utsText2Tag(char const *str)
141 if (strcmp(str, "context") ==0) return vcVHI_CONTEXT;
142 else if (strcmp(str, "sysname") ==0) return vcVHI_SYSNAME;
143 else if (strcmp(str, "nodename") ==0) return vcVHI_NODENAME;
144 else if (strcmp(str, "release") ==0) return vcVHI_RELEASE;
145 else if (strcmp(str, "version") ==0) return vcVHI_VERSION;
146 else if (strcmp(str, "machine") ==0) return vcVHI_MACHINE;
147 else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
149 WRITE_MSG(2, "Unknown UTS tag\n");
157 int v = vc_get_version();
163 l = utilvserver_fmt_xulong(0, (unsigned int)v);
164 memcpy(buf, "0x00000000", 10);
165 utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
171 getXid(char *buf, char const *vserver)
173 pid_t pid = atoi(vserver);
174 xid_t xid = vc_get_task_xid(pid);
176 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
178 utilvserver_fmt_long(buf, xid);
186 getPXid(char UNUSED *buf, char const UNUSED *vserver)
188 // TODO: implement me when available
193 getInitPid_native(char *buf, xid_t xid)
195 struct vc_vx_info info;
197 if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
199 utilvserver_fmt_long(buf, info.initpid);
206 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_V11)
208 selectPid(struct dirent const *ent)
210 return atoi(ent->d_name)!=0;
214 getInitPid_internal(pid_t pid, xid_t xid, pid_t *res)
219 size_t bufsize = utilvserver_getProcEntryBufsize();
223 pos = utilvserver_getProcEntry(pid, "\ns_context: ", buf, bufsize);
224 if (pos==0 && errno==EAGAIN) continue;
226 if (pos==0 || (xid_t)atoi(pos)!=xid) return false;
229 pos = strstr(buf, "\ninitpid: ");
232 pos += sizeof("\ninitpid: ")-1;
233 if (strncmp(pos, "none", 4)==0) *res = -1;
234 else *res = atoi(pos);
242 getInitPid_emulated(char *buf, xid_t xid)
244 struct dirent **namelist;
247 switchToWatchXid(0); // ignore errors silently...
248 n = scandir("/proc", &namelist, selectPid, alphasort);
249 if (n<0) perror("scandir()");
252 if (!getInitPid_internal(atoi(namelist[n]->d_name), xid, &pid)) continue;
254 utilvserver_fmt_long(buf, pid);
260 #endif // VC_ENABLE_API_COMPAT
263 getInitPid(char *buf, xid_t xid)
265 if (vc_isSupported(vcFEATURE_VINFO))
266 return getInitPid_native(buf, xid);
268 return getInitPid_emulated(buf, xid);
272 getInitPidPid(char *buf, char const *vserver)
274 struct vc_vx_info info;
275 pid_t pid = atoi(vserver);
276 xid_t xid = vc_get_task_xid(pid);
278 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
279 else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
281 utilvserver_fmt_long(buf, info.initpid);
289 getUTS(char *buf, xid_t xid, size_t argc, char * argv[])
292 vc_uts_type type = utsText2Tag(argv[0]);
293 if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
294 perror("vc_get_vhi_name()");
299 bool is_passed = false;
301 #define APPEND_UTS(TYPE) \
302 (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
303 (strcat(buf, "??? ")))
305 if (APPEND_UTS(vcVHI_CONTEXT) &&
306 APPEND_UTS(vcVHI_SYSNAME) &&
307 APPEND_UTS(vcVHI_NODENAME) &&
308 APPEND_UTS(vcVHI_RELEASE) &&
309 APPEND_UTS(vcVHI_VERSION) &&
310 APPEND_UTS(vcVHI_MACHINE) &&
311 APPEND_UTS(vcVHI_DOMAINNAME) &&
315 perror("vc_get_vhi_name()");
323 printSysInfo(char *buf)
325 int fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY);
334 WRITE_STR(1, uts.release);
339 if (getAPIVer(buf)) WRITE_STR(1, buf);
340 else WRITE_MSG(1, "???");
343 " util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n"
348 WRITE_MSG(1, "FEATURES.txt not found\n");
350 off_t l = Elseek(fd, 0, SEEK_END);
351 Elseek(fd, 0, SEEK_SET);
354 EreadAll(fd, buf, l);
355 EwriteAll(1, buf, l);
364 getContext(char *buf, char const *vserver, bool allow_only_static)
366 xid_t xid = vc_getVserverCtx(vserver, vcCFG_AUTO,
367 allow_only_static, 0);
368 if (xid==VC_NOCTX) return 0;
370 utilvserver_fmt_long(buf, xid);
375 testFeature(int argc, char *argv[])
377 return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE;
381 str2bool(char const *str)
383 return atoi(str)!=0 || strchr("yYtY", str[0])!=0 || strcasecmp("true", str)==0;
387 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
389 char const * res = 0;
390 char buf[sizeof(xid_t)*4 + 1024];
392 memset(buf, 0, sizeof buf);
394 case tgNAME : res = vc_getVserverName(vserver, vcCFG_AUTO); break;
396 res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
398 case tgCFGDIR : res = vc_getVserverCfgDir(vserver, vcCFG_AUTO); break;
400 res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
404 res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
407 case tgCONTEXT : res = getContext(buf, vserver,
408 argc==0 || str2bool(argv[0])); break;
409 case tgINITPID_PID : res = getInitPidPid(buf, vserver); break;
410 case tgAPIVER : res = getAPIVer(buf); break;
411 case tgXID : res = getXid(buf, vserver); break;
412 case tgPXID : res = getPXid(buf, vserver); break;
413 case tgSYSINFO : return printSysInfo(buf); break;
414 case tgFEATURE : return testFeature(argc,argv); break;
417 xid_t xid = *vserver!='\0' ? vc_xidopt2xid(vserver,true,0) : VC_SAMECTX;
420 case tgID : res = vc_getVserverByCtx(xid,0,0); break;
421 case tgINITPID : res = getInitPid(buf, xid); break;
422 case tgUTS : res = getUTS(buf, xid, argc, argv); break;
424 default : assert(false); abort(); // TODO
429 if (res==0) return EXIT_FAILURE;
435 int main(int argc, char *argv[])
438 char const * vserver;
442 int c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
446 case 'h' : showHelp(1, argv[0], 0);
447 case 'v' : showVersion();
448 case 'l' : showTags();
449 case 'q' : quiet = true; break;
451 WRITE_MSG(2, "Try '");
452 WRITE_STR(2, argv[0]);
453 WRITE_MSG(2, " --help\" for more information.\n");
460 WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
464 vserver = argv[optind];
465 tag = stringToTag(argv[optind+1]);
468 WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
473 int fd = Eopen("/dev/null", O_WRONLY, 0644);
478 return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);