added an option the 'CONTEXT' query so that it can be choosen between
[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; when an optinal argument "
56                            "evaluates to false,only the current context will be printed") },
57   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
58   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
59   { "NAME",    tgNAME,    "gives out the name of the vserver" },
60   { "CFGDIR",  tgCFGDIR,  "gives out the configuration directory of the vserver" },
61   { "APPDIR",  tgAPPDIR,  "gives out the name of the toplevel application cfgdir" },
62   { "INITPID",     tgINITPID,     "gives out the initpid of the given context" },
63   { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
64   { "XID",         tgXID,         "gives out the context-id of the given pid" },
65   { "APIVER",      tgAPIVER,      "gives out the version of the kernel API" },
66   { "UTS",         tgUTS,         ("gives out an uts-entry; possible entries are "
67                                    "context, sysname, nodename, release, version, "
68                                    "machine and domainname") },
69   { "SYSINFO",     tgSYSINFO,     "gives out information about the systen" },
70   { "FEATURE",     tgFEATURE,     "returns 0 iff the queried feature is supported" },
71 };
72
73 int wrapper_exit_code = 1;
74
75 static struct option const
76 CMDLINE_OPTIONS[] = {
77   { "help",     no_argument,  0, 'h' },
78   { "version",  no_argument,  0, 'v' },
79   { 0,0,0,0 }
80 };
81
82
83 static void
84 showHelp(int fd, char const *cmd, int res)
85 {
86   WRITE_MSG(fd, "Usage:  ");
87   WRITE_STR(fd, cmd);
88   WRITE_MSG(fd,
89             " [-ql] <vserver>|<pid>|<context> <tag>\n"
90             "Please report bugs to " PACKAGE_BUGREPORT "\n");
91   exit(res);
92 }
93
94 static void
95 showVersion()
96 {
97   WRITE_MSG(1,
98             "vserver-info " VERSION " -- returns information about vservers\n"
99             "This program is part of " PACKAGE_STRING "\n\n"
100             "Copyright (C) 2003 Enrico Scholz\n"
101             VERSION_COPYRIGHT_DISCLAIMER);
102   exit(0);
103 }
104
105 static void
106 showTags()
107 {
108   char const *          delim = "";
109   size_t        i;
110
111   WRITE_MSG(1, "Valid tags are: ");
112   for (i=0; i<DIM_OF(TAGS); ++i) {
113     WRITE_STR(1, delim);
114     WRITE_STR(1, TAGS[i].tag);
115
116     delim = ", ";
117   }
118   WRITE_MSG(1, "\n");
119   exit(0);
120 }
121
122 static VserverTag
123 stringToTag(char const *str)
124 {
125   size_t        i;
126   for (i=0; i<DIM_OF(TAGS); ++i)
127     if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
128
129   return tgNONE;
130 }
131
132 static vc_uts_type
133 utsText2Tag(char const *str)
134 {
135   if      (strcmp(str, "context")   ==0) return vcVHI_CONTEXT;
136   else if (strcmp(str, "sysname")   ==0) return vcVHI_SYSNAME;
137   else if (strcmp(str, "nodename")  ==0) return vcVHI_NODENAME;
138   else if (strcmp(str, "release")   ==0) return vcVHI_RELEASE;
139   else if (strcmp(str, "version")   ==0) return vcVHI_VERSION;
140   else if (strcmp(str, "machine")   ==0) return vcVHI_MACHINE;
141   else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
142   else {
143     WRITE_MSG(2, "Unknown UTS tag\n");
144     exit(1);
145   }
146 }
147
148 static char *
149 getAPIVer(char *buf)
150 {
151   int           v = vc_get_version();
152   size_t        l;
153   
154   if (v==-1) return 0;
155
156   
157   l = utilvserver_fmt_xulong(0, (unsigned int)v);
158   memcpy(buf, "0x00000000", 10);
159   utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
160
161   return buf;
162 }
163
164 static char *
165 getXid(char *buf, char const *vserver)
166 {
167   pid_t         pid = atoi(vserver);
168   xid_t         xid = vc_get_task_xid(pid);
169
170   if (xid==VC_NOCTX) perror("vc_get_task_xid()");
171   else {
172     utilvserver_fmt_long(buf, xid);
173     return buf;
174   }
175
176   return 0;
177 }
178
179 static char *
180 getInitPid(char *buf, xid_t xid)
181 {
182   struct vc_vx_info             info;
183   
184   if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
185   else {
186     utilvserver_fmt_long(buf, info.initpid);
187     return buf;
188   }
189
190   return 0;
191 }
192
193 static char *
194 getInitPidPid(char *buf, char const *vserver)
195 {
196   struct vc_vx_info     info;
197   pid_t                 pid = atoi(vserver);
198   xid_t                 xid = vc_get_task_xid(pid);
199
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()");
202   else {
203     utilvserver_fmt_long(buf, info.initpid);
204     return buf;
205   }
206
207   return 0;
208 }
209
210 static char *
211 getUTS(char *buf, xid_t xid, size_t argc, char * argv[])
212 {
213   if (argc>0) {
214     vc_uts_type type = utsText2Tag(argv[0]);
215     if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
216       perror("vc_get_vhi_name()");
217     else
218       return buf;
219   }
220   else {
221     bool                is_passed = false;
222     char                tmp[128];
223 #define APPEND_UTS(TYPE)                                                \
224     (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
225      (strcat(buf, "??? ")))
226
227     if (APPEND_UTS(vcVHI_CONTEXT) &&
228         APPEND_UTS(vcVHI_SYSNAME) &&
229         APPEND_UTS(vcVHI_NODENAME) &&
230         APPEND_UTS(vcVHI_RELEASE) &&
231         APPEND_UTS(vcVHI_VERSION) &&
232         APPEND_UTS(vcVHI_MACHINE) &&
233         APPEND_UTS(vcVHI_DOMAINNAME) &&
234         is_passed)
235       return buf;
236
237     perror("vc_get_vhi_name()");
238 #undef APPEND_UTS
239   }
240
241   return 0;
242 }
243
244 static int
245 printSysInfo(char *buf)
246 {
247   int                   fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY);
248   struct utsname        uts;
249
250   if (uname(&uts)==-1)
251     perror("uname()");
252   else {
253     WRITE_MSG(1,
254               "Versions:\n"
255               "                   Kernel: ");
256     WRITE_STR(1, uts.release);
257     WRITE_MSG(1, "\n"
258               "                   VS-API: ");
259
260     memset(buf, 0, 128);
261     if (getAPIVer(buf)) WRITE_STR(1, buf);
262     else                WRITE_MSG(1, "???");
263     
264     WRITE_MSG(1, "\n"
265               "             util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n"
266               "\n");
267   }
268
269   if (fd==-1)
270     WRITE_MSG(1, "FEATURES.txt not found\n");
271   else {
272     off_t               l  = Elseek(fd, 0, SEEK_END);
273     Elseek(fd, 0, SEEK_SET);
274     {
275       char              buf[l];
276       EreadAll(fd, buf, l);
277       EwriteAll(1, buf, l);
278     }
279     Eclose(fd);
280   }
281
282   return EXIT_SUCCESS;
283 }
284
285 static char *
286 getContext(char *buf, char const *vserver, bool allow_only_static)
287 {
288   xid_t         xid = vc_getVserverCtx(vserver, vcCFG_AUTO,
289                                        allow_only_static, 0);
290   if (xid==VC_NOCTX) return 0;
291   
292   utilvserver_fmt_long(buf, xid);
293   return buf;
294 }
295
296 static int
297 testFeature(int argc, char *argv[])
298 {
299   return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE;
300 }
301
302 static bool
303 str2bool(char const *str)
304 {
305   return atoi(str)!=0 || strchr("yYtY", str[0])!=0;
306 }
307
308 static int
309 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
310 {
311   char const *          res = 0;
312   char                  buf[sizeof(xid_t)*4 + 1024];
313   xid_t                 xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
314
315   memset(buf, 0, sizeof buf);
316   switch (tag) {
317     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
318     case tgVDIR         :
319       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
320       break;
321     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
322     case tgAPPDIR       :
323       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
324       break;
325       
326     case tgRUNNING      :
327       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
328       break;
329
330     case tgCONTEXT      :  res = getContext(buf, vserver,
331                                             argc==0 || str2bool(argv[0])); break;
332     case tgXID          :  res = getXid(buf, vserver);         break;
333     case tgINITPID      :  res = getInitPid(buf, xid);         break;
334     case tgINITPID_PID  :  res = getInitPidPid(buf, vserver);  break;
335     case tgAPIVER       :  res = getAPIVer(buf);               break;
336     case tgUTS          :  res = getUTS(buf, xid, argc, argv); break;
337     case tgSYSINFO      :  return printSysInfo(buf);           break;
338     case tgFEATURE      :  return testFeature(argc,argv);      break;
339     
340     default             :  assert(false); abort();  // TODO
341   }
342
343   if (res==0) return EXIT_FAILURE;
344   WRITE_STR(1, res);
345   WRITE_MSG(1, "\n");
346   return EXIT_SUCCESS;
347 }
348
349 int main(int argc, char *argv[])
350 {
351   bool          quiet = false;
352   char const *  vserver;
353   VserverTag    tag;
354   
355   while (1) {
356     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
357     if (c==-1) break;
358
359     switch (c) {
360       case 'h'          :  showHelp(1, argv[0], 0);
361       case 'v'          :  showVersion();
362       case 'l'          :  showTags();
363       case 'q'          :  quiet = true; break;
364       default           :
365         WRITE_MSG(2, "Try '");
366         WRITE_STR(2, argv[0]);
367         WRITE_MSG(2, " --help\" for more information.\n");
368         exit(1);
369         break;
370     }
371   }
372
373   if (optind+2>argc) {
374     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
375     exit(1);
376   }
377
378   vserver = argv[optind];
379   tag     = stringToTag(argv[optind+1]);
380
381   if (tag==tgNONE) {
382     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
383     exit(1);
384   }
385
386   if (quiet) {
387     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
388     Edup2(fd, 1);
389     Eclose(fd);
390   }
391
392   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
393 }