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.
34 typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
35 tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
37 tgINITPID, tgINITPID_PID,
42 char const * const tag;
44 char const * const descr;
46 { "CONTEXT", tgCONTEXT, "the current and/or assigned context" },
47 { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
48 { "VDIR", tgVDIR, "gives out the root-directory of the vserver" },
49 { "NAME", tgNAME, "gives out the name of the vserver" },
50 { "CFGDIR", tgCFGDIR, "gives out the configuration directory of the vserver" },
51 { "APPDIR", tgAPPDIR, "gives out the name of the toplevel application cfgdir" },
52 { "INITPID", tgINITPID, "gives out the initpid of the given context" },
53 { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
54 { "XID", tgXID, "gives out the context-id of the given pid" },
55 { "APIVER", tgAPIVER, "gives out the version of the kernel API" }
58 int wrapper_exit_code = 1;
60 static struct option const
62 { "help", no_argument, 0, 'h' },
63 { "version", no_argument, 0, 'v' },
69 showHelp(int fd, char const *cmd, int res)
71 WRITE_MSG(fd, "Usage: ");
74 " [-ql] <vserver>|<pid>|<context> <tag>\n"
75 "Please report bugs to " PACKAGE_BUGREPORT "\n");
83 "vserver-info " VERSION " -- returns information about vservers\n"
84 "This program is part of " PACKAGE_STRING "\n\n"
85 "Copyright (C) 2003 Enrico Scholz\n"
86 VERSION_COPYRIGHT_DISCLAIMER);
93 char const * delim = "";
96 WRITE_MSG(1, "Valid tags are: ");
97 for (i=0; i<DIM_OF(TAGS); ++i) {
99 WRITE_STR(1, TAGS[i].tag);
108 stringToTag(char const *str)
111 for (i=0; i<DIM_OF(TAGS); ++i)
112 if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
118 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
120 char const * res = 0;
121 char buf[sizeof(xid_t)*4 + 16];
124 memset(buf, 0, sizeof buf);
126 case tgNAME : res = vc_getVserverName(vserver, vcCFG_AUTO); break;
128 res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
130 case tgCFGDIR : res = vc_getVserverCfgDir(vserver, vcCFG_AUTO); break;
132 res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
136 ctx = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
138 utilvserver_fmt_long(buf, ctx);
144 res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
149 pid_t pid = atoi(vserver);
150 xid_t xid = vc_get_task_xid(pid);
151 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
153 utilvserver_fmt_long(buf, xid);
161 xid_t xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
162 struct vc_vx_info info;
163 if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
165 utilvserver_fmt_long(buf, info.xid);
173 pid_t pid = atoi(vserver);
174 xid_t xid = vc_get_task_xid(pid);
175 struct vc_vx_info info;
177 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
178 else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
180 utilvserver_fmt_long(buf, info.xid);
188 int v = vc_get_version();
190 size_t l = utilvserver_fmt_xulong(0, (unsigned int)v);
191 memcpy(buf, "0x00000000", 10);
192 utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
197 default : assert(false); abort(); // TODO
200 if (res==0) return EXIT_FAILURE;
206 int main(int argc, char *argv[])
209 char const * vserver;
213 int c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
217 case 'h' : showHelp(1, argv[0], 0);
218 case 'v' : showVersion();
219 case 'l' : showTags();
220 case 'q' : quiet = true; break;
222 WRITE_MSG(2, "Try '");
223 WRITE_STR(2, argv[0]);
224 WRITE_MSG(2, " --help\" for more information.\n");
231 WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
235 vserver = argv[optind];
236 tag = stringToTag(argv[optind+1]);
239 WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
244 int fd = Eopen("/dev/null", O_WRONLY, 0644);
249 return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);