10ea63b32a0c5a6a6353c20e4588c5c22772f8df
[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 "util.h"
24 #include "wrappers.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
34 typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
35                tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
36                tgAPIVER,
37                tgINITPID, tgINITPID_PID,
38                tgXID,
39 }       VserverTag;
40
41 static struct {
42     char const * const  tag;
43     VserverTag const    val;
44     char const * const  descr;
45 }  const TAGS[] = {
46   { "CONTEXT", tgCONTEXT, "the current and/or assigned context" },
47   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
48   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
49   { "NAME",    tgNAME,    "gives out the name of the vserver" },
50   { "CFGDIR",  tgCFGDIR,  "gives out the configuration directory of the vserver" },
51   { "APPDIR",  tgAPPDIR,  "gives out the name of the toplevel application cfgdir" },
52   { "INITPID",     tgINITPID,     "gives out the initpid of the given context" },
53   { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
54   { "XID",         tgXID,         "gives out the context-id of the given pid" },
55   { "APIVER",      tgAPIVER,      "gives out the version of the kernel API" }
56 };
57
58 int wrapper_exit_code = 1;
59
60 static struct option const
61 CMDLINE_OPTIONS[] = {
62   { "help",     no_argument,  0, 'h' },
63   { "version",  no_argument,  0, 'v' },
64   { 0,0,0,0 }
65 };
66
67
68 static void
69 showHelp(int fd, char const *cmd, int res)
70 {
71   WRITE_MSG(fd, "Usage:  ");
72   WRITE_STR(fd, cmd);
73   WRITE_MSG(fd,
74             " [-ql] <vserver>|<pid>|<context> <tag>\n"
75             "Please report bugs to " PACKAGE_BUGREPORT "\n");
76   exit(res);
77 }
78
79 static void
80 showVersion()
81 {
82   WRITE_MSG(1,
83             "vserver-info " VERSION " -- returns information about vservers\n"
84             "This program is part of " PACKAGE_STRING "\n\n"
85             "Copyright (C) 2003 Enrico Scholz\n"
86             VERSION_COPYRIGHT_DISCLAIMER);
87   exit(0);
88 }
89
90 static void
91 showTags()
92 {
93   char const *          delim = "";
94   size_t        i;
95
96   WRITE_MSG(1, "Valid tags are: ");
97   for (i=0; i<DIM_OF(TAGS); ++i) {
98     WRITE_STR(1, delim);
99     WRITE_STR(1, TAGS[i].tag);
100
101     delim = ", ";
102   }
103   WRITE_MSG(1, "\n");
104   exit(0);
105 }
106
107 static VserverTag
108 stringToTag(char const *str)
109 {
110   size_t        i;
111   for (i=0; i<DIM_OF(TAGS); ++i)
112     if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
113
114   return tgNONE;
115 }
116
117 static int
118 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
119 {
120   char const *          res = 0;
121   char                  buf[sizeof(xid_t)*4 + 16];
122   xid_t                 ctx;
123
124   memset(buf, 0, sizeof buf);
125   switch (tag) {
126     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
127     case tgVDIR         :
128       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
129       break;
130     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
131     case tgAPPDIR       :
132       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
133       break;
134       
135     case tgCONTEXT      :
136       ctx = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
137       if (ctx!=VC_NOCTX) {
138         utilvserver_fmt_long(buf, ctx);
139         res = buf;
140       }
141       break;
142       
143     case tgRUNNING      :
144       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
145       break;
146
147     case tgXID          :
148     {
149       pid_t     pid = atoi(vserver);
150       xid_t     xid = vc_get_task_xid(pid);
151       if (xid==VC_NOCTX) perror("vc_get_task_xid()");
152       else {
153         utilvserver_fmt_long(buf, xid);
154         res = buf;
155       }
156       break;
157     }
158
159     case tgINITPID      :
160     {
161       xid_t                     xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
162       struct vc_vx_info         info;
163       if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
164       else {
165         utilvserver_fmt_long(buf, info.xid);
166         res = buf;
167       }
168       break;
169     }
170
171     case tgINITPID_PID  :
172     {
173       pid_t                     pid = atoi(vserver);
174       xid_t                     xid = vc_get_task_xid(pid);
175       struct vc_vx_info         info;
176
177       if (xid==VC_NOCTX) perror("vc_get_task_xid()");
178       else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
179       else {
180         utilvserver_fmt_long(buf, info.xid);
181         res = buf;
182       }
183       break;
184     }
185
186     case tgAPIVER       :
187     {
188       int       v = vc_get_version();
189       if (v!=-1) {
190         size_t  l = utilvserver_fmt_xulong(0, (unsigned int)v);
191         memcpy(buf, "0x00000000", 10);
192         utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
193         res = buf;
194       }
195       break;
196     }
197     default             :  assert(false); abort();  // TODO
198   }
199
200   if (res==0) return EXIT_FAILURE;
201   WRITE_STR(1, res);
202   WRITE_MSG(1, "\n");
203   return EXIT_SUCCESS;
204 }
205
206 int main(int argc, char *argv[])
207 {
208   bool          quiet = false;
209   char const *  vserver;
210   VserverTag    tag;
211   
212   while (1) {
213     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
214     if (c==-1) break;
215
216     switch (c) {
217       case 'h'          :  showHelp(1, argv[0], 0);
218       case 'v'          :  showVersion();
219       case 'l'          :  showTags();
220       case 'q'          :  quiet = true; break;
221       default           :
222         WRITE_MSG(2, "Try '");
223         WRITE_STR(2, argv[0]);
224         WRITE_MSG(2, " --help\" for more information.\n");
225         exit(1);
226         break;
227     }
228   }
229
230   if (optind+2>argc) {
231     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
232     exit(1);
233   }
234
235   vserver = argv[optind];
236   tag     = stringToTag(argv[optind+1]);
237
238   if (tag==tgNONE) {
239     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
240     exit(1);
241   }
242
243   if (quiet) {
244     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
245     Edup2(fd, 1);
246     Eclose(fd);
247   }
248
249   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
250 }