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 "pathconfig.h"
35 #include <sys/utsname.h>
37 #define ENSC_WRAPPERS_FCNTL 1
38 #define ENSC_WRAPPERS_IO 1
39 #define ENSC_WRAPPERS_UNISTD 1
42 typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
43 tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
45 tgINITPID, tgINITPID_PID,
46 tgXID, tgUTS, tgSYSINFO,
51 char const * const tag;
53 char const * const descr;
55 { "CONTEXT", tgCONTEXT, "the current and/or assigned context" },
56 { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
57 { "VDIR", tgVDIR, "gives out the root-directory of the vserver" },
58 { "NAME", tgNAME, "gives out the name of the vserver" },
59 { "CFGDIR", tgCFGDIR, "gives out the configuration directory of the vserver" },
60 { "APPDIR", tgAPPDIR, "gives out the name of the toplevel application cfgdir" },
61 { "INITPID", tgINITPID, "gives out the initpid of the given context" },
62 { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
63 { "XID", tgXID, "gives out the context-id of the given pid" },
64 { "APIVER", tgAPIVER, "gives out the version of the kernel API" },
65 { "UTS", tgUTS, ("gives out an uts-entry; possible entries are "
66 "context, sysname, nodename, release, version, "
67 "machine and domainname") },
68 { "SYSINFO", tgSYSINFO, "gives out information about the systen" },
69 { "FEATURE", tgFEATURE, "returns 0 iff the queried feature is supported" },
72 int wrapper_exit_code = 1;
74 static struct option const
76 { "help", no_argument, 0, 'h' },
77 { "version", no_argument, 0, 'v' },
83 showHelp(int fd, char const *cmd, int res)
85 WRITE_MSG(fd, "Usage: ");
88 " [-ql] <vserver>|<pid>|<context> <tag>\n"
89 "Please report bugs to " PACKAGE_BUGREPORT "\n");
97 "vserver-info " VERSION " -- returns information about vservers\n"
98 "This program is part of " PACKAGE_STRING "\n\n"
99 "Copyright (C) 2003 Enrico Scholz\n"
100 VERSION_COPYRIGHT_DISCLAIMER);
107 char const * delim = "";
110 WRITE_MSG(1, "Valid tags are: ");
111 for (i=0; i<DIM_OF(TAGS); ++i) {
113 WRITE_STR(1, TAGS[i].tag);
122 stringToTag(char const *str)
125 for (i=0; i<DIM_OF(TAGS); ++i)
126 if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
132 utsText2Tag(char const *str)
134 if (strcmp(str, "context") ==0) return vcVHI_CONTEXT;
135 else if (strcmp(str, "sysname") ==0) return vcVHI_SYSNAME;
136 else if (strcmp(str, "nodename") ==0) return vcVHI_NODENAME;
137 else if (strcmp(str, "release") ==0) return vcVHI_RELEASE;
138 else if (strcmp(str, "version") ==0) return vcVHI_VERSION;
139 else if (strcmp(str, "machine") ==0) return vcVHI_MACHINE;
140 else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
142 WRITE_MSG(2, "Unknown UTS tag\n");
150 int v = vc_get_version();
156 l = utilvserver_fmt_xulong(0, (unsigned int)v);
157 memcpy(buf, "0x00000000", 10);
158 utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
164 getXid(char *buf, char const *vserver)
166 pid_t pid = atoi(vserver);
167 xid_t xid = vc_get_task_xid(pid);
169 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
171 utilvserver_fmt_long(buf, xid);
179 getInitPid(char *buf, xid_t xid)
181 struct vc_vx_info info;
183 if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
185 utilvserver_fmt_long(buf, info.initpid);
193 getInitPidPid(char *buf, char const *vserver)
195 struct vc_vx_info info;
196 pid_t pid = atoi(vserver);
197 xid_t xid = vc_get_task_xid(pid);
199 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
200 else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
202 utilvserver_fmt_long(buf, info.initpid);
210 getUTS(char *buf, xid_t xid, size_t argc, char * argv[])
213 vc_uts_type type = utsText2Tag(argv[0]);
214 if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
215 perror("vc_get_vhi_name()");
220 bool is_passed = false;
222 #define APPEND_UTS(TYPE) \
223 (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
224 (strcat(buf, "??? ")))
226 if (APPEND_UTS(vcVHI_CONTEXT) &&
227 APPEND_UTS(vcVHI_SYSNAME) &&
228 APPEND_UTS(vcVHI_NODENAME) &&
229 APPEND_UTS(vcVHI_RELEASE) &&
230 APPEND_UTS(vcVHI_VERSION) &&
231 APPEND_UTS(vcVHI_MACHINE) &&
232 APPEND_UTS(vcVHI_DOMAINNAME) &&
236 perror("vc_get_vhi_name()");
244 printSysInfo(char *buf)
246 int fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY);
255 WRITE_STR(1, uts.release);
260 if (getAPIVer(buf)) WRITE_STR(1, buf);
261 else WRITE_MSG(1, "???");
264 " util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n"
269 WRITE_MSG(1, "FEATURES.txt not found\n");
271 off_t l = Elseek(fd, 0, SEEK_END);
272 Elseek(fd, 0, SEEK_SET);
275 EreadAll(fd, buf, l);
276 EwriteAll(1, buf, l);
285 getContext(char *buf, char const *vserver)
287 xid_t xid = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
288 if (xid==VC_NOCTX) return 0;
290 utilvserver_fmt_long(buf, xid);
295 testFeature(int argc, char *argv[])
297 return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE;
301 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
303 char const * res = 0;
304 char buf[sizeof(xid_t)*4 + 1024];
305 xid_t xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
307 memset(buf, 0, sizeof buf);
309 case tgNAME : res = vc_getVserverName(vserver, vcCFG_AUTO); break;
311 res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
313 case tgCFGDIR : res = vc_getVserverCfgDir(vserver, vcCFG_AUTO); break;
315 res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
319 res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
322 case tgCONTEXT : res = getContext(buf, vserver); break;
323 case tgXID : res = getXid(buf, vserver); break;
324 case tgINITPID : res = getInitPid(buf, xid); break;
325 case tgINITPID_PID : res = getInitPidPid(buf, vserver); break;
326 case tgAPIVER : res = getAPIVer(buf); break;
327 case tgUTS : res = getUTS(buf, xid, argc, argv); break;
328 case tgSYSINFO : return printSysInfo(buf); break;
329 case tgFEATURE : return testFeature(argc,argv); break;
331 default : assert(false); abort(); // TODO
334 if (res==0) return EXIT_FAILURE;
340 int main(int argc, char *argv[])
343 char const * vserver;
347 int c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
351 case 'h' : showHelp(1, argv[0], 0);
352 case 'v' : showVersion();
353 case 'l' : showTags();
354 case 'q' : quiet = true; break;
356 WRITE_MSG(2, "Try '");
357 WRITE_STR(2, argv[0]);
358 WRITE_MSG(2, " --help\" for more information.\n");
365 WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
369 vserver = argv[optind];
370 tag = stringToTag(argv[optind+1]);
373 WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
378 int fd = Eopen("/dev/null", O_WRONLY, 0644);
383 return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);