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