added 'FEATURE' tag
[util-vserver.git] / util-vserver / src / vserver-info.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
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.
8 //  
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.
13 //  
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.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "pathconfig.h"
24 #include "util.h"
25
26 #include "internal.h"
27 #include "vserver.h"
28
29 #include <stdlib.h>
30 #include <getopt.h>
31 #include <assert.h>
32 #include <stdbool.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <sys/utsname.h>
36
37 #define ENSC_WRAPPERS_FCNTL     1
38 #define ENSC_WRAPPERS_IO        1
39 #define ENSC_WRAPPERS_UNISTD    1
40 #include <wrappers.h>
41
42 typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
43                tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
44                tgAPIVER,
45                tgINITPID, tgINITPID_PID,
46                tgXID, tgUTS, tgSYSINFO,
47                tgFEATURE,
48 }       VserverTag;
49
50 static struct {
51     char const * const  tag;
52     VserverTag const    val;
53     char const * const  descr;
54 }  const TAGS[] = {
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" },
70 };
71
72 int wrapper_exit_code = 1;
73
74 static struct option const
75 CMDLINE_OPTIONS[] = {
76   { "help",     no_argument,  0, 'h' },
77   { "version",  no_argument,  0, 'v' },
78   { 0,0,0,0 }
79 };
80
81
82 static void
83 showHelp(int fd, char const *cmd, int res)
84 {
85   WRITE_MSG(fd, "Usage:  ");
86   WRITE_STR(fd, cmd);
87   WRITE_MSG(fd,
88             " [-ql] <vserver>|<pid>|<context> <tag>\n"
89             "Please report bugs to " PACKAGE_BUGREPORT "\n");
90   exit(res);
91 }
92
93 static void
94 showVersion()
95 {
96   WRITE_MSG(1,
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);
101   exit(0);
102 }
103
104 static void
105 showTags()
106 {
107   char const *          delim = "";
108   size_t        i;
109
110   WRITE_MSG(1, "Valid tags are: ");
111   for (i=0; i<DIM_OF(TAGS); ++i) {
112     WRITE_STR(1, delim);
113     WRITE_STR(1, TAGS[i].tag);
114
115     delim = ", ";
116   }
117   WRITE_MSG(1, "\n");
118   exit(0);
119 }
120
121 static VserverTag
122 stringToTag(char const *str)
123 {
124   size_t        i;
125   for (i=0; i<DIM_OF(TAGS); ++i)
126     if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
127
128   return tgNONE;
129 }
130
131 static vc_uts_type
132 utsText2Tag(char const *str)
133 {
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;
141   else {
142     WRITE_MSG(2, "Unknown UTS tag\n");
143     exit(1);
144   }
145 }
146
147 static char *
148 getAPIVer(char *buf)
149 {
150   int           v = vc_get_version();
151   size_t        l;
152   
153   if (v==-1) return 0;
154
155   
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);
159
160   return buf;
161 }
162
163 static char *
164 getXid(char *buf, char const *vserver)
165 {
166   pid_t         pid = atoi(vserver);
167   xid_t         xid = vc_get_task_xid(pid);
168
169   if (xid==VC_NOCTX) perror("vc_get_task_xid()");
170   else {
171     utilvserver_fmt_long(buf, xid);
172     return buf;
173   }
174
175   return 0;
176 }
177
178 static char *
179 getInitPid(char *buf, xid_t xid)
180 {
181   struct vc_vx_info             info;
182   
183   if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
184   else {
185     utilvserver_fmt_long(buf, info.initpid);
186     return buf;
187   }
188
189   return 0;
190 }
191
192 static char *
193 getInitPidPid(char *buf, char const *vserver)
194 {
195   struct vc_vx_info     info;
196   pid_t                 pid = atoi(vserver);
197   xid_t                 xid = vc_get_task_xid(pid);
198
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()");
201   else {
202     utilvserver_fmt_long(buf, info.initpid);
203     return buf;
204   }
205
206   return 0;
207 }
208
209 static char *
210 getUTS(char *buf, xid_t xid, size_t argc, char * argv[])
211 {
212   if (argc>0) {
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()");
216     else
217       return buf;
218   }
219   else {
220     bool                is_passed = false;
221     char                tmp[128];
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, "??? ")))
225
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) &&
233         is_passed)
234       return buf;
235
236     perror("vc_get_vhi_name()");
237 #undef APPEND_UTS
238   }
239
240   return 0;
241 }
242
243 static int
244 printSysInfo(char *buf)
245 {
246   int                   fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY);
247   struct utsname        uts;
248
249   if (uname(&uts)==-1)
250     perror("uname()");
251   else {
252     WRITE_MSG(1,
253               "Versions:\n"
254               "                   Kernel: ");
255     WRITE_STR(1, uts.release);
256     WRITE_MSG(1, "\n"
257               "                   VS-API: ");
258
259     memset(buf, 0, 128);
260     if (getAPIVer(buf)) WRITE_STR(1, buf);
261     else                WRITE_MSG(1, "???");
262     
263     WRITE_MSG(1, "\n"
264               "             util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n"
265               "\n");
266   }
267
268   if (fd==-1)
269     WRITE_MSG(1, "FEATURES.txt not found\n");
270   else {
271     off_t               l  = Elseek(fd, 0, SEEK_END);
272     Elseek(fd, 0, SEEK_SET);
273     {
274       char              buf[l];
275       EreadAll(fd, buf, l);
276       EwriteAll(1, buf, l);
277     }
278     Eclose(fd);
279   }
280
281   return EXIT_SUCCESS;
282 }
283
284 static char *
285 getContext(char *buf, char const *vserver)
286 {
287   xid_t         xid = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
288   if (xid==VC_NOCTX) return 0;
289   
290   utilvserver_fmt_long(buf, xid);
291   return buf;
292 }
293
294 static int
295 testFeature(int argc, char *argv[])
296 {
297   return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE;
298 }
299
300 static int
301 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
302 {
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;
306
307   memset(buf, 0, sizeof buf);
308   switch (tag) {
309     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
310     case tgVDIR         :
311       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
312       break;
313     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
314     case tgAPPDIR       :
315       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
316       break;
317       
318     case tgRUNNING      :
319       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
320       break;
321
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;
330     
331     default             :  assert(false); abort();  // TODO
332   }
333
334   if (res==0) return EXIT_FAILURE;
335   WRITE_STR(1, res);
336   WRITE_MSG(1, "\n");
337   return EXIT_SUCCESS;
338 }
339
340 int main(int argc, char *argv[])
341 {
342   bool          quiet = false;
343   char const *  vserver;
344   VserverTag    tag;
345   
346   while (1) {
347     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
348     if (c==-1) break;
349
350     switch (c) {
351       case 'h'          :  showHelp(1, argv[0], 0);
352       case 'v'          :  showVersion();
353       case 'l'          :  showTags();
354       case 'q'          :  quiet = true; break;
355       default           :
356         WRITE_MSG(2, "Try '");
357         WRITE_STR(2, argv[0]);
358         WRITE_MSG(2, " --help\" for more information.\n");
359         exit(1);
360         break;
361     }
362   }
363
364   if (optind+2>argc) {
365     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
366     exit(1);
367   }
368
369   vserver = argv[optind];
370   tag     = stringToTag(argv[optind+1]);
371
372   if (tag==tgNONE) {
373     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
374     exit(1);
375   }
376
377   if (quiet) {
378     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
379     Edup2(fd, 1);
380     Eclose(fd);
381   }
382
383   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
384 }