ready for tagging
[util-vserver.git] / src / vserver-info.c
1 // $Id: vserver-info.c 2589 2007-08-16 03:06:50Z dhozac $    --*- 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 #undef _POSIX_SOURCE
47 #include "capability-compat.h"
48
49 typedef enum { tgNONE,tgCONTEXT, tgID, tgRUNNING,
50                tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
51                tgAPIVER, tgPXID,
52                tgINITPID, tgINITPID_PID,
53                tgXID, tgUTS, tgSYSINFO,
54                tgFEATURE, tgCANONIFY,
55                tgVERIFYCAP, tgXIDTYPE, tgVERIFYPROC,
56                tgNID, tgTAG,
57 }       VserverTag;
58
59 static struct {
60     char const * const  tag;
61     VserverTag const    val;
62     char const * const  descr;
63 }  const TAGS[] = {
64   { "CONTEXT", tgCONTEXT, ("the current and/or assigned context; when an optinal argument "
65                            "evaluates to false,only the current context will be printed") },
66   { "ID",      tgID,      "gives out the vserver-id for the context-xid" },
67   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
68   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
69   { "NAME",    tgNAME,    "gives out the name of the vserver" },
70   { "CFGDIR",  tgCFGDIR,  "gives out the configuration directory of the vserver" },
71   { "APPDIR",  tgAPPDIR,  "gives out the name of the toplevel application cfgdir" },
72   { "INITPID",     tgINITPID,     "gives out the initpid of the given context" },
73   { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
74   { "XID",         tgXID,         "gives out the context-id of the given pid" },
75   { "APIVER",      tgAPIVER,      "gives out the version of the kernel API" },
76   { "UTS",         tgUTS,         ("gives out an uts-entry; possible entries are "
77                                    "context, sysname, nodename, release, version, "
78                                    "machine and domainname") },
79   { "SYSINFO",     tgSYSINFO,     "gives out information about the systen" },
80   { "FEATURE",     tgFEATURE,     "returns 0 iff the queried feature is supported" },
81   { "PXID",        tgPXID,        "returns the xid of the parent context" },
82   { "CANONIFY",    tgCANONIFY,    "canonifies the vserver-name and removes dangerous characters" },
83   { "VERIFYCAP",   tgVERIFYCAP,   "test if the kernel supports linux capabilities" },
84   { "VERIFYPROC",  tgVERIFYPROC,  "test if /proc can be read by contexts!=0" },
85   { "XIDTYPE",     tgXIDTYPE,     "returns the type of the given XID" },
86   { "NID",         tgNID,         "outputs the network context-id of the given pid" },
87   { "TAG",         tgTAG,         "outputs the filesystem tag of the given pid" },
88 };
89
90 int wrapper_exit_code = 1;
91
92 static struct option const
93 CMDLINE_OPTIONS[] = {
94   { "help",     no_argument,  0, 'h' },
95   { "version",  no_argument,  0, 'v' },
96   { 0,0,0,0 }
97 };
98
99
100 static void
101 showHelp(int fd, char const *cmd, int res)
102 {
103   WRITE_MSG(fd, "Usage:  ");
104   WRITE_STR(fd, cmd);
105   WRITE_MSG(fd,
106             " [-ql] <vserver>|<pid>|<context> <tag>\n"
107             "Please report bugs to " PACKAGE_BUGREPORT "\n");
108   exit(res);
109 }
110
111 static void
112 showVersion()
113 {
114   WRITE_MSG(1,
115             "vserver-info " VERSION " -- returns information about vservers\n"
116             "This program is part of " PACKAGE_STRING "\n\n"
117             "Copyright (C) 2003 Enrico Scholz\n"
118             VERSION_COPYRIGHT_DISCLAIMER);
119   exit(0);
120 }
121
122 static void
123 showTags()
124 {
125   char const *          delim = "";
126   size_t        i;
127
128   WRITE_MSG(1, "Valid tags are: ");
129   for (i=0; i<DIM_OF(TAGS); ++i) {
130     WRITE_STR(1, delim);
131     WRITE_STR(1, TAGS[i].tag);
132
133     delim = ", ";
134   }
135   WRITE_MSG(1, "\n");
136   exit(0);
137 }
138
139 static VserverTag
140 stringToTag(char const *str)
141 {
142   size_t        i;
143   for (i=0; i<DIM_OF(TAGS); ++i)
144     if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
145
146   return tgNONE;
147 }
148
149 static vc_uts_type
150 utsText2Tag(char const *str)
151 {
152   if      (strcmp(str, "context")   ==0) return vcVHI_CONTEXT;
153   else if (strcmp(str, "sysname")   ==0) return vcVHI_SYSNAME;
154   else if (strcmp(str, "nodename")  ==0) return vcVHI_NODENAME;
155   else if (strcmp(str, "release")   ==0) return vcVHI_RELEASE;
156   else if (strcmp(str, "version")   ==0) return vcVHI_VERSION;
157   else if (strcmp(str, "machine")   ==0) return vcVHI_MACHINE;
158   else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
159   else {
160     WRITE_MSG(2, "Unknown UTS tag\n");
161     exit(1);
162   }
163 }
164
165 static bool
166 verifyProc()
167 {
168   char const            *errptr;
169   
170   if (!switchToWatchXid(&errptr)) {
171     perror(errptr);
172     return false;
173   }
174
175   if (access("/proc/uptime", R_OK)==-1) {
176     if (errno!=ENOENT)
177       perror("access(\"/proc/uptime\")");
178     
179     return false;
180   }
181
182   return true;
183 }
184
185 static bool
186 verifyCap()
187 {
188   struct __user_cap_header_struct header;
189   struct __user_cap_data_struct user;
190   header.version = _LINUX_CAPABILITY_VERSION;
191   header.pid     = 0;
192
193   if (getuid()!=0) {
194     WRITE_MSG(2, "'VERIFYCAP' can be executed as root only\n");
195     return false;
196   }
197
198 //  if( prctl( PR_SET_KEEPCAPS, 1,0,0,0 ) < 0 ) {
199 //    perror( "prctl:" );
200 //    return false;
201 //  }
202   
203   if (capget(&header, &user)==-1) {
204     perror("capget()");
205     return false;
206   }
207
208   user.effective   = 0;
209   user.permitted   = 0;
210   user.inheritable = 0;
211
212   if (capset(&header, &user)==-1) {
213     perror("capset()");
214     return false;
215   }
216
217   return chroot("/")==-1;
218 }
219
220 static char *
221 getAPIVer(char *buf)
222 {
223   int           v = vc_get_version();
224   size_t        l;
225   
226   if (v==-1) return 0;
227
228   
229   l = utilvserver_fmt_xulong(0, (unsigned int)v);
230   memcpy(buf, "0x00000000", 10);
231   utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
232
233   return buf;
234 }
235
236 static inline char *
237 getCtxId(char *buf, const char *pid_str, xid_t (*get_id)(pid_t pid), const char *err_str)
238 {
239   pid_t         pid = atoi(pid_str);
240   xid_t         xid = get_id(pid);
241
242   if (xid==VC_NOCTX) perror(err_str);
243   else {
244     utilvserver_fmt_long(buf, xid);
245     return buf;
246   }
247
248   return 0;
249 }
250
251 static char *
252 getXid(char *buf, char const *pid_str)
253 {
254   return getCtxId(buf, pid_str, vc_get_task_xid, "vc_get_task_xid()");
255 }
256
257 static char *
258 getNid(char *buf, const char *pid_str)
259 {
260   return getCtxId(buf, pid_str, vc_get_task_nid, "vc_get_task_nid()");
261 }
262
263 static char *
264 getTag(char *buf, const char *pid_str)
265 {
266   return getCtxId(buf, pid_str, vc_get_task_tag, "vc_get_task_tag()");
267 }
268
269 static char *
270 getPXid(char UNUSED *buf, char const UNUSED *vserver)
271 {
272   // TODO: implement me when available
273   return 0;
274 }
275
276 static char *
277 getInitPid_native(char *buf, xid_t xid)
278 {
279   struct vc_vx_info             info;
280   
281   if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
282   else {
283     utilvserver_fmt_long(buf, info.initpid);
284     return buf;
285   }
286
287   return 0;
288 }
289
290 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_V11)
291 static int
292 selectPid(struct dirent const *ent)
293 {
294   return atoi(ent->d_name)!=0;
295 }
296
297 static bool
298 getInitPid_internal(pid_t pid, xid_t xid, pid_t *res)
299 {
300   *res = -1;
301   
302   for (;*res==-1;) {
303     size_t                      bufsize = utilvserver_getProcEntryBufsize();
304     char                        buf[bufsize+1];
305     char                        *pos = 0;
306
307     pos = utilvserver_getProcEntry(pid, "\ns_context: ", buf, bufsize);
308     if (pos==0 && errno==EAGAIN) continue;
309
310     if (pos==0 || (xid_t)atoi(pos)!=xid) return false;
311
312     buf[bufsize] = '\0';
313     pos          = strstr(buf, "\ninitpid: ");
314     
315     if (pos!=0) {
316       pos       += sizeof("\ninitpid: ")-1;
317       if (strncmp(pos, "none", 4)==0) *res = -1;
318       else                            *res = atoi(pos);
319     }
320   }
321
322   return true;
323 }
324
325 static char *
326 getInitPid_emulated(char *buf, xid_t xid)
327 {
328   struct dirent **namelist;
329   int           n;
330
331   switchToWatchXid(0);  // ignore errors silently...
332   n = scandir("/proc", &namelist, selectPid, alphasort);
333   if (n<0) perror("scandir()");
334   else while (n--) {
335     pid_t       pid;
336     if (!getInitPid_internal(atoi(namelist[n]->d_name), xid, &pid)) continue;
337
338     utilvserver_fmt_long(buf, pid);
339     return buf;
340   }
341
342   return 0;
343 }
344 #else // VC_ENABLE_API_COMPAT
345 static char *
346 getInitPid_emulated(char UNUSED *buf, xid_t UNUSED xid)
347 {
348   WRITE_MSG(2, "tools were built without compat API, getInitPid() not available\n");
349   return 0;
350 }
351 #endif // VC_ENABLE_API_COMPAT
352
353 static char *
354 getInitPid(char *buf, xid_t xid)
355 {
356   if (vc_isSupported(vcFEATURE_VINFO))
357     return getInitPid_native(buf, xid);
358   else
359     return getInitPid_emulated(buf, xid);
360 }
361
362 static char *
363 getInitPidPid(char *buf, char const *vserver)
364 {
365   struct vc_vx_info     info;
366   pid_t                 pid = atoi(vserver);
367   xid_t                 xid = vc_get_task_xid(pid);
368
369   if (xid==VC_NOCTX) perror("vc_get_task_xid()");
370   else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
371   else {
372     utilvserver_fmt_long(buf, info.initpid);
373     return buf;
374   }
375
376   return 0;
377 }
378
379 static char *
380 getUTS(char *buf, xid_t xid, size_t argc, char * argv[])
381 {
382   if (argc>0) {
383     vc_uts_type type = utsText2Tag(argv[0]);
384     if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
385       perror("vc_get_vhi_name()");
386     else
387       return buf;
388   }
389   else {
390     bool                is_passed = false;
391     char                tmp[128];
392 #define APPEND_UTS(TYPE)                                                \
393     (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
394      (strcat(buf, "??? ")))
395
396     if (APPEND_UTS(vcVHI_CONTEXT) &&
397         APPEND_UTS(vcVHI_SYSNAME) &&
398         APPEND_UTS(vcVHI_NODENAME) &&
399         APPEND_UTS(vcVHI_RELEASE) &&
400         APPEND_UTS(vcVHI_VERSION) &&
401         APPEND_UTS(vcVHI_MACHINE) &&
402         APPEND_UTS(vcVHI_DOMAINNAME) &&
403         is_passed)
404       return buf;
405
406     perror("vc_get_vhi_name()");
407 #undef APPEND_UTS
408   }
409
410   return 0;
411 }
412
413 static int
414 printSysInfo(char *buf)
415 {
416   int                   fd = open(PKGLIBDIR "/FEATURES.txt", O_RDONLY);
417   struct utsname        uts;
418
419   if (uname(&uts)==-1)
420     perror("uname()");
421   else {
422     WRITE_MSG(1,
423               "Versions:\n"
424               "                   Kernel: ");
425     WRITE_STR(1, uts.release);
426     WRITE_MSG(1, "\n"
427               "                   VS-API: ");
428
429     memset(buf, 0, 128);
430     if (getAPIVer(buf)) WRITE_STR(1, buf);
431     else                WRITE_MSG(1, "???");
432     
433     WRITE_MSG(1, "\n"
434               "             util-vserver: " PACKAGE_VERSION "; " __DATE__ ", " __TIME__"\n"
435               "\n");
436   }
437
438   if (fd==-1)
439     WRITE_MSG(1, "FEATURES.txt not found\n");
440   else {
441     off_t               l  = Elseek(fd, 0, SEEK_END);
442     Elseek(fd, 0, SEEK_SET);
443     {
444       char              buf[l];
445       EreadAll(fd, buf, l);
446       EwriteAll(1, buf, l);
447     }
448     Eclose(fd);
449   }
450
451   return EXIT_SUCCESS;
452 }
453
454 static char *
455 getContext(char *buf, char const *vserver, bool allow_only_static)
456 {
457   xid_t         xid = vc_getVserverCtx(vserver, vcCFG_AUTO,
458                                        allow_only_static, 0, vcCTX_XID);
459   if (xid==VC_NOCTX) return 0;
460   
461   utilvserver_fmt_long(buf, xid);
462   return buf;
463 }
464
465 static char const *
466 getXIDType(xid_t xid, int argc, char *argv[])
467 {
468   char const *          tp;
469   
470   switch (vc_getXIDType(xid)) {
471     case vcTYPE_INVALID         :  tp = "invalid"; break;
472     case vcTYPE_MAIN            :  tp = "main";    break;
473     case vcTYPE_WATCH           :  tp = "watch";   break;
474     case vcTYPE_STATIC          :  tp = "static";  break;
475     case vcTYPE_DYNAMIC         :  tp = "dynamic"; break;
476     default                     :  tp = 0;         break;
477   }
478
479   if (argc==0 || tp==0)
480     return tp;
481
482   while (argc>0) {
483     --argc;
484     if (strcasecmp(argv[argc], tp)==0) return tp;
485   }
486
487   return 0;
488 }
489
490 static int
491 testFeature(int argc, char *argv[])
492 {
493   return (argc>0 && vc_isSupportedString(argv[0])) ? EXIT_SUCCESS : EXIT_FAILURE;
494 }
495
496 static bool
497 str2bool(char const *str)
498 {
499   return atoi(str)!=0 || strchr("yYtT", str[0])!=0 || strcasecmp("true", str)==0;
500 }
501
502 static int
503 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
504 {
505   char const *          res = 0;
506   char                  buf[sizeof(xid_t)*4 + 1024 + strlen(vserver)];
507
508   memset(buf, 0, sizeof buf);
509   switch (tag) {
510     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
511     case tgVDIR         :
512       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
513       break;
514     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
515     case tgAPPDIR       :
516       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
517       break;
518       
519     case tgRUNNING      : {
520       signed long               xid;    // type is a small hack, but should be ok...
521       struct vc_vx_info         info;
522         
523       if (isNumber(vserver, &xid, true) && xid>=0)
524         res = (vc_get_vx_info(xid, &info)==-1) ? 0 : "1";
525       else
526         res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0, vcCTX_XID)==VC_NOCTX) ? 0 : "1";
527       
528       break;
529     }
530
531     case tgCANONIFY     :
532       strcpy(buf, vserver);
533       if (canonifyVserverName(buf)>0) res = buf;
534       break;
535       
536     case tgCONTEXT      :  res = getContext(buf, vserver,
537                                             argc==0 || str2bool(argv[0])); break;
538     case tgINITPID_PID  :  res = getInitPidPid(buf, vserver);  break;
539     case tgAPIVER       :  res = getAPIVer(buf);               break;
540     case tgXID          :  res = getXid(buf, vserver);         break;
541     case tgPXID         :  res = getPXid(buf, vserver);        break;
542     case tgSYSINFO      :  return printSysInfo(buf);           break;
543     case tgFEATURE      :  return testFeature(argc,argv);      break;
544     case tgVERIFYCAP    :  return verifyCap() ? 0 : 1;         break;
545     case tgVERIFYPROC   :  return verifyProc() ? 0 : 1;        break;
546     case tgNID          :  res = getNid(buf, vserver);         break;
547     case tgTAG          :  res = getTag(buf, vserver);         break;
548
549
550     default             : {
551       xid_t             xid = *vserver!='\0' ? vc_xidopt2xid(vserver,true,0) : VC_SAMECTX;
552
553       switch (tag) {
554         case tgID       :  res = vc_getVserverByCtx(xid,0,0);  break;
555         case tgINITPID  :  res = getInitPid(buf, xid);         break;
556         case tgUTS      :  res = getUTS(buf, xid, argc, argv); break;
557         case tgXIDTYPE  :  res = getXIDType(xid, argc, argv);  break;
558     
559         default         :  assert(false); abort();  // TODO
560       }
561     }
562   }
563
564   if (res==0) return EXIT_FAILURE;
565   WRITE_STR(1, res);
566   WRITE_MSG(1, "\n");
567   return EXIT_SUCCESS;
568 }
569
570 int main(int argc, char *argv[])
571 {
572   bool          quiet = false;
573   char const *  vserver;
574   VserverTag    tag;
575   
576   while (1) {
577     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
578     if (c==-1) break;
579
580     switch (c) {
581       case 'h'          :  showHelp(1, argv[0], 0);
582       case 'v'          :  showVersion();
583       case 'l'          :  showTags();
584       case 'q'          :  quiet = true; break;
585       default           :
586         WRITE_MSG(2, "Try '");
587         WRITE_STR(2, argv[0]);
588         WRITE_MSG(2, " --help' for more information.\n");
589         exit(1);
590         break;
591     }
592   }
593
594   if (optind+2>argc) {
595     execQuery("-", tgSYSINFO, 0, 0);
596     WRITE_MSG(2, "\nAssumed 'SYSINFO' as no other option given; try '--help' for more information.\n");
597     exit(0);
598   }
599
600   vserver = argv[optind];
601   tag     = stringToTag(argv[optind+1]);
602
603   if (tag==tgNONE) {
604     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
605     exit(1);
606   }
607
608   if (quiet) {
609     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
610     Edup2(fd, 1);
611     Eclose(fd);
612   }
613
614   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
615 }