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