a5ac32e94432b990ac22f5d6c6c71a38778075bd
[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 "lib/utils-legacy.h"
24 #include "pathconfig.h"
25 #include "util.h"
26
27 #include "internal.h"
28 #include "vserver.h"
29
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <assert.h>
33 #include <stdbool.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <sys/utsname.h>
37 #include <dirent.h>
38
39 #define ENSC_WRAPPERS_FCNTL     1
40 #define ENSC_WRAPPERS_IO        1
41 #define ENSC_WRAPPERS_UNISTD    1
42 #include <wrappers.h>
43
44 typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
45                tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
46                tgAPIVER,
47                tgINITPID, tgINITPID_PID,
48                tgXID, tgUTS, tgSYSINFO,
49                tgFEATURE,
50 }       VserverTag;
51
52 static struct {
53     char const * const  tag;
54     VserverTag const    val;
55     char const * const  descr;
56 }  const TAGS[] = {
57   { "CONTEXT", tgCONTEXT, ("the current and/or assigned context; when an optinal argument "
58                            "evaluates to false,only the current context will be printed") },
59   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
60   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
61   { "NAME",    tgNAME,    "gives out the name of the vserver" },
62   { "CFGDIR",  tgCFGDIR,  "gives out the configuration directory of the vserver" },
63   { "APPDIR",  tgAPPDIR,  "gives out the name of the toplevel application cfgdir" },
64   { "INITPID",     tgINITPID,     "gives out the initpid of the given context" },
65   { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
66   { "XID",         tgXID,         "gives out the context-id of the given pid" },
67   { "APIVER",      tgAPIVER,      "gives out the version of the kernel API" },
68   { "UTS",         tgUTS,         ("gives out an uts-entry; possible entries are "
69                                    "context, sysname, nodename, release, version, "
70                                    "machine and domainname") },
71   { "SYSINFO",     tgSYSINFO,     "gives out information about the systen" },
72   { "FEATURE",     tgFEATURE,     "returns 0 iff the queried feature is supported" },
73 };
74
75 int wrapper_exit_code = 1;
76
77 static struct option const
78 CMDLINE_OPTIONS[] = {
79   { "help",     no_argument,  0, 'h' },
80   { "version",  no_argument,  0, 'v' },
81   { 0,0,0,0 }
82 };
83
84
85 static void
86 showHelp(int fd, char const *cmd, int res)
87 {
88   WRITE_MSG(fd, "Usage:  ");
89   WRITE_STR(fd, cmd);
90   WRITE_MSG(fd,
91             " [-ql] <vserver>|<pid>|<context> <tag>\n"
92             "Please report bugs to " PACKAGE_BUGREPORT "\n");
93   exit(res);
94 }
95
96 static void
97 showVersion()
98 {
99   WRITE_MSG(1,
100             "vserver-info " VERSION " -- returns information about vservers\n"
101             "This program is part of " PACKAGE_STRING "\n\n"
102             "Copyright (C) 2003 Enrico Scholz\n"
103             VERSION_COPYRIGHT_DISCLAIMER);
104   exit(0);
105 }
106
107 static void
108 showTags()
109 {
110   char const *          delim = "";
111   size_t        i;
112
113   WRITE_MSG(1, "Valid tags are: ");
114   for (i=0; i<DIM_OF(TAGS); ++i) {
115     WRITE_STR(1, delim);
116     WRITE_STR(1, TAGS[i].tag);
117
118     delim = ", ";
119   }
120   WRITE_MSG(1, "\n");
121   exit(0);
122 }
123
124 static VserverTag
125 stringToTag(char const *str)
126 {
127   size_t        i;
128   for (i=0; i<DIM_OF(TAGS); ++i)
129     if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
130
131   return tgNONE;
132 }
133
134 static vc_uts_type
135 utsText2Tag(char const *str)
136 {
137   if      (strcmp(str, "context")   ==0) return vcVHI_CONTEXT;
138   else if (strcmp(str, "sysname")   ==0) return vcVHI_SYSNAME;
139   else if (strcmp(str, "nodename")  ==0) return vcVHI_NODENAME;
140   else if (strcmp(str, "release")   ==0) return vcVHI_RELEASE;
141   else if (strcmp(str, "version")   ==0) return vcVHI_VERSION;
142   else if (strcmp(str, "machine")   ==0) return vcVHI_MACHINE;
143   else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
144   else {
145     WRITE_MSG(2, "Unknown UTS tag\n");
146     exit(1);
147   }
148 }
149
150 static char *
151 getAPIVer(char *buf)
152 {
153   int           v = vc_get_version();
154   size_t        l;
155   
156   if (v==-1) return 0;
157
158   
159   l = utilvserver_fmt_xulong(0, (unsigned int)v);
160   memcpy(buf, "0x00000000", 10);
161   utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
162
163   return buf;
164 }
165
166 static char *
167 getXid(char *buf, char const *vserver)
168 {
169   pid_t         pid = atoi(vserver);
170   xid_t         xid = vc_get_task_xid(pid);
171
172   if (xid==VC_NOCTX) perror("vc_get_task_xid()");
173   else {
174     utilvserver_fmt_long(buf, xid);
175     return buf;
176   }
177
178   return 0;
179 }
180
181 static char *
182 getInitPid_native(char *buf, xid_t xid)
183 {
184   struct vc_vx_info             info;
185   
186   if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
187   else {
188     utilvserver_fmt_long(buf, info.initpid);
189     return buf;
190   }
191
192   return 0;
193 }
194
195 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_V11)
196 static int
197 selectPid(struct dirent const *ent)
198 {
199   return atoi(ent->d_name)!=0;
200 }
201
202 static bool
203 getInitPid_internal(pid_t pid, xid_t xid, pid_t *res)
204 {
205   *res = -1;
206   
207   for (;*res==-1;) {
208     size_t                      bufsize = utilvserver_getProcEntryBufsize();
209     char                        buf[bufsize+1];
210     char                        *pos = 0;
211
212     pos = utilvserver_getProcEntry(pid, "\ns_context: ", buf, bufsize);
213     if (pos==0 && errno==EAGAIN) continue;
214
215     if (pos==0 || (xid_t)atoi(pos)!=xid) return false;
216
217     buf[bufsize] = '\0';
218     pos          = strstr(buf, "\ninitpid: ");
219     
220     if (pos!=0) {
221       pos         += sizeof("\ninitpid: ")-1;
222       if (strncmp(pos, "none", 4)==0) *res = -1;
223       else                            *res = atoi(pos);
224     }
225   }
226
227   return true;
228 }
229
230 static char *
231 getInitPid_emulated(char *buf, xid_t xid)
232 {
233   struct dirent **namelist;
234   int           n;
235   
236   vc_new_s_context(1,0,0);      // ignore errors silently...
237   n = scandir("/proc", &namelist, selectPid, alphasort);
238   if (n<0) perror("scandir()");
239   else while (n--) {
240     pid_t       pid;
241     if (!getInitPid_internal(atoi(namelist[n]->d_name), xid, &pid)) continue;
242
243     utilvserver_fmt_long(buf, pid);
244     return buf;
245   }
246
247   return 0;
248 }
249 #endif // VC_ENABLE_API_COMPAT
250
251 static char *
252 getInitPid(char *buf, xid_t xid)
253 {
254   if (vc_isSupported(vcFEATURE_VINFO))
255     return getInitPid_native(buf, xid);
256   else
257     return getInitPid_emulated(buf, xid);
258 }
259
260 static char *
261 getInitPidPid(char *buf, char const *vserver)
262 {
263   struct vc_vx_info     info;
264   pid_t                 pid = atoi(vserver);
265   xid_t                 xid = vc_get_task_xid(pid);
266
267   if (xid==VC_NOCTX) perror("vc_get_task_xid()");
268   else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
269   else {
270     utilvserver_fmt_long(buf, info.initpid);
271     return buf;
272   }
273
274   return 0;
275 }
276
277 static char *
278 getUTS(char *buf, xid_t xid, size_t argc, char * argv[])
279 {
280   if (argc>0) {
281     vc_uts_type type = utsText2Tag(argv[0]);
282     if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
283       perror("vc_get_vhi_name()");
284     else
285       return buf;
286   }
287   else {
288     bool                is_passed = false;
289     char                tmp[128];
290 #define APPEND_UTS(TYPE)                                                \
291     (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
292      (strcat(buf, "??? ")))
293
294     if (APPEND_UTS(vcVHI_CONTEXT) &&
295         APPEND_UTS(vcVHI_SYSNAME) &&
296         APPEND_UTS(vcVHI_NODENAME) &&
297         APPEND_UTS(vcVHI_RELEASE) &&
298         APPEND_UTS(vcVHI_VERSION) &&
299         APPEND_UTS(vcVHI_MACHINE) &&
300         APPEND_UTS(vcVHI_DOMAINNAME) &&
301         is_passed)
302       return buf;
303
304     perror("vc_get_vhi_name()");
305 #undef APPEND_UTS
306   }
307
308   return 0;
309 }
310
311 static int
312 printSysInfo(char *buf)
313 {
314   int                   fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY);
315   struct utsname        uts;
316
317   if (uname(&uts)==-1)
318     perror("uname()");
319   else {
320     WRITE_MSG(1,
321               "Versions:\n"
322               "                   Kernel: ");
323     WRITE_STR(1, uts.release);
324     WRITE_MSG(1, "\n"
325               "                   VS-API: ");
326
327     memset(buf, 0, 128);
328     if (getAPIVer(buf)) WRITE_STR(1, buf);
329     else                WRITE_MSG(1, "???");
330     
331     WRITE_MSG(1, "\n"
332               "             util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n"
333               "\n");
334   }
335
336   if (fd==-1)
337     WRITE_MSG(1, "FEATURES.txt not found\n");
338   else {
339     off_t               l  = Elseek(fd, 0, SEEK_END);
340     Elseek(fd, 0, SEEK_SET);
341     {
342       char              buf[l];
343       EreadAll(fd, buf, l);
344       EwriteAll(1, buf, l);
345     }
346     Eclose(fd);
347   }
348
349   return EXIT_SUCCESS;
350 }
351
352 static char *
353 getContext(char *buf, char const *vserver, bool allow_only_static)
354 {
355   xid_t         xid = vc_getVserverCtx(vserver, vcCFG_AUTO,
356                                        allow_only_static, 0);
357   if (xid==VC_NOCTX) return 0;
358   
359   utilvserver_fmt_long(buf, xid);
360   return buf;
361 }
362
363 static int
364 testFeature(int argc, char *argv[])
365 {
366   return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE;
367 }
368
369 static bool
370 str2bool(char const *str)
371 {
372   return atoi(str)!=0 || strchr("yYtY", str[0])!=0;
373 }
374
375 static int
376 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
377 {
378   char const *          res = 0;
379   char                  buf[sizeof(xid_t)*4 + 1024];
380   xid_t                 xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
381
382   memset(buf, 0, sizeof buf);
383   switch (tag) {
384     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
385     case tgVDIR         :
386       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
387       break;
388     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
389     case tgAPPDIR       :
390       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
391       break;
392       
393     case tgRUNNING      :
394       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
395       break;
396
397     case tgCONTEXT      :  res = getContext(buf, vserver,
398                                             argc==0 || str2bool(argv[0])); break;
399     case tgXID          :  res = getXid(buf, vserver);         break;
400     case tgINITPID      :  res = getInitPid(buf, xid);         break;
401     case tgINITPID_PID  :  res = getInitPidPid(buf, vserver);  break;
402     case tgAPIVER       :  res = getAPIVer(buf);               break;
403     case tgUTS          :  res = getUTS(buf, xid, argc, argv); break;
404     case tgSYSINFO      :  return printSysInfo(buf);           break;
405     case tgFEATURE      :  return testFeature(argc,argv);      break;
406     
407     default             :  assert(false); abort();  // TODO
408   }
409
410   if (res==0) return EXIT_FAILURE;
411   WRITE_STR(1, res);
412   WRITE_MSG(1, "\n");
413   return EXIT_SUCCESS;
414 }
415
416 int main(int argc, char *argv[])
417 {
418   bool          quiet = false;
419   char const *  vserver;
420   VserverTag    tag;
421   
422   while (1) {
423     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
424     if (c==-1) break;
425
426     switch (c) {
427       case 'h'          :  showHelp(1, argv[0], 0);
428       case 'v'          :  showVersion();
429       case 'l'          :  showTags();
430       case 'q'          :  quiet = true; break;
431       default           :
432         WRITE_MSG(2, "Try '");
433         WRITE_STR(2, argv[0]);
434         WRITE_MSG(2, " --help\" for more information.\n");
435         exit(1);
436         break;
437     }
438   }
439
440   if (optind+2>argc) {
441     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
442     exit(1);
443   }
444
445   vserver = argv[optind];
446   tag     = stringToTag(argv[optind+1]);
447
448   if (tag==tgNONE) {
449     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
450     exit(1);
451   }
452
453   if (quiet) {
454     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
455     Edup2(fd, 1);
456     Eclose(fd);
457   }
458
459   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
460 }