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 #define ENSC_WRAPPERS_FCNTL 1
35 #define ENSC_WRAPPERS_UNISTD 1
38 typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
39 tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
41 tgINITPID, tgINITPID_PID,
46 char const * const tag;
48 char const * const descr;
50 { "CONTEXT", tgCONTEXT, "the current and/or assigned context" },
51 { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
52 { "VDIR", tgVDIR, "gives out the root-directory of the vserver" },
53 { "NAME", tgNAME, "gives out the name of the vserver" },
54 { "CFGDIR", tgCFGDIR, "gives out the configuration directory of the vserver" },
55 { "APPDIR", tgAPPDIR, "gives out the name of the toplevel application cfgdir" },
56 { "INITPID", tgINITPID, "gives out the initpid of the given context" },
57 { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
58 { "XID", tgXID, "gives out the context-id of the given pid" },
59 { "APIVER", tgAPIVER, "gives out the version of the kernel API" },
60 { "UTS", tgUTS, ("gives out an uts-entry; possible entries are "
61 "context, sysname, nodename, release, version, "
62 "machine and domainname") },
65 int wrapper_exit_code = 1;
67 static struct option const
69 { "help", no_argument, 0, 'h' },
70 { "version", no_argument, 0, 'v' },
76 showHelp(int fd, char const *cmd, int res)
78 WRITE_MSG(fd, "Usage: ");
81 " [-ql] <vserver>|<pid>|<context> <tag>\n"
82 "Please report bugs to " PACKAGE_BUGREPORT "\n");
90 "vserver-info " VERSION " -- returns information about vservers\n"
91 "This program is part of " PACKAGE_STRING "\n\n"
92 "Copyright (C) 2003 Enrico Scholz\n"
93 VERSION_COPYRIGHT_DISCLAIMER);
100 char const * delim = "";
103 WRITE_MSG(1, "Valid tags are: ");
104 for (i=0; i<DIM_OF(TAGS); ++i) {
106 WRITE_STR(1, TAGS[i].tag);
115 stringToTag(char const *str)
118 for (i=0; i<DIM_OF(TAGS); ++i)
119 if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
125 utsText2Tag(char const *str)
127 if (strcmp(str, "context") ==0) return vcVHI_CONTEXT;
128 else if (strcmp(str, "sysname") ==0) return vcVHI_SYSNAME;
129 else if (strcmp(str, "nodename") ==0) return vcVHI_NODENAME;
130 else if (strcmp(str, "release") ==0) return vcVHI_RELEASE;
131 else if (strcmp(str, "version") ==0) return vcVHI_VERSION;
132 else if (strcmp(str, "machine") ==0) return vcVHI_MACHINE;
133 else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
135 WRITE_MSG(2, "Unknown UTS tag\n");
141 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
143 char const * res = 0;
144 char buf[sizeof(xid_t)*4 + 1024];
145 xid_t xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
147 memset(buf, 0, sizeof buf);
149 case tgNAME : res = vc_getVserverName(vserver, vcCFG_AUTO); break;
151 res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
153 case tgCFGDIR : res = vc_getVserverCfgDir(vserver, vcCFG_AUTO); break;
155 res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
159 xid = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
161 utilvserver_fmt_long(buf, xid);
167 res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
172 pid_t pid = atoi(vserver);
174 xid = vc_get_task_xid(pid);
175 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
177 utilvserver_fmt_long(buf, xid);
185 struct vc_vx_info info;
186 if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
188 utilvserver_fmt_long(buf, info.xid);
196 pid_t pid = atoi(vserver);
197 struct vc_vx_info info;
199 xid = vc_get_task_xid(pid);
200 if (xid==VC_NOCTX) perror("vc_get_task_xid()");
201 else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
203 utilvserver_fmt_long(buf, info.xid);
211 int v = vc_get_version();
213 size_t l = utilvserver_fmt_xulong(0, (unsigned int)v);
214 memcpy(buf, "0x00000000", 10);
215 utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
224 vc_uts_type type = utsText2Tag(argv[0]);
225 if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
226 perror("vc_get_vhi_name()");
231 bool is_passed = false;
233 #define APPEND_UTS(TYPE) \
234 (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
235 (strcat(buf, "??? ")))
237 APPEND_UTS(vcVHI_CONTEXT) &&
238 APPEND_UTS(vcVHI_SYSNAME) &&
239 APPEND_UTS(vcVHI_NODENAME) &&
240 APPEND_UTS(vcVHI_RELEASE) &&
241 APPEND_UTS(vcVHI_VERSION) &&
242 APPEND_UTS(vcVHI_MACHINE) &&
243 APPEND_UTS(vcVHI_DOMAINNAME) &&
248 perror("vc_get_vhi_name()");
255 default : assert(false); abort(); // TODO
258 if (res==0) return EXIT_FAILURE;
264 int main(int argc, char *argv[])
267 char const * vserver;
271 int c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
275 case 'h' : showHelp(1, argv[0], 0);
276 case 'v' : showVersion();
277 case 'l' : showTags();
278 case 'q' : quiet = true; break;
280 WRITE_MSG(2, "Try '");
281 WRITE_STR(2, argv[0]);
282 WRITE_MSG(2, " --help\" for more information.\n");
289 WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
293 vserver = argv[optind];
294 tag = stringToTag(argv[optind+1]);
297 WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
302 int fd = Eopen("/dev/null", O_WRONLY, 0644);
307 return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);