added INITPID* and XID tags
[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                tgINITPID, tgINITPID_PID,
37                tgXID,
38 }       VserverTag;
39
40 static struct {
41     char const * const  tag;
42     VserverTag const    val;
43     char const * const  descr;
44 }  const TAGS[] = {
45   { "CONTEXT", tgCONTEXT, "the current and/or assigned context" },
46   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
47   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
48   { "NAME",    tgNAME,    "gives out the name of the vserver" },
49   { "CFGDIR",  tgCFGDIR,  "gives out the configuration directory of the vserver" },
50   { "APPDIR",  tgAPPDIR,  "gives out the name of the toplevel application cfgdir" },
51   { "INITPID",     tgINITPID,     "gives out the initpid of the given context" },
52   { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
53   { "XID",         tgXID,         "gives out the context-id of the given pid" },
54 };
55
56 #define TAGS_COUNT      (sizeof(TAGS)/sizeof(TAGS[0]))
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             " [-q] <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<TAGS_COUNT; ++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<TAGS_COUNT; ++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   switch (tag) {
125     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
126     case tgVDIR         :
127       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
128       break;
129     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
130     case tgAPPDIR       :
131       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
132       break;
133       
134     case tgCONTEXT      :
135       ctx = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
136       if (ctx!=VC_NOCTX) {
137         utilvserver_fmt_long(buf, ctx);
138         res = buf;
139       }
140       break;
141       
142     case tgRUNNING      :
143       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
144       break;
145
146     case tgXID          :
147     {
148       pid_t     pid = atoi(vserver);
149       xid_t     xid = vc_get_task_xid(pid);
150       if (xid==VC_NOCTX) perror("vc_get_task_xid()");
151       else {
152         utilvserver_fmt_long(buf, xid);
153         res = buf;
154       }
155       break;
156     }
157
158     case tgINITPID      :
159     {
160       xid_t                     xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
161       struct vc_vx_info         info;
162       if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
163       else {
164         utilvserver_fmt_long(buf, info.xid);
165         res = buf;
166       }
167       break;
168     }
169
170     case tgINITPID_PID  :
171     {
172       pid_t                     pid = atoi(vserver);
173       xid_t                     xid = vc_get_task_xid(pid);
174       struct vc_vx_info         info;
175
176       if (xid==VC_NOCTX) perror("vc_get_task_xid()");
177       else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
178       else {
179         utilvserver_fmt_long(buf, info.xid);
180         res = buf;
181       }
182       break;
183     }
184
185     default             :  assert(false); abort();  // TODO
186   }
187
188   if (res==0) return EXIT_FAILURE;
189   WRITE_STR(1, res);
190   WRITE_MSG(1, "\n");
191   return EXIT_SUCCESS;
192 }
193
194 int main(int argc, char *argv[])
195 {
196   bool          quiet = false;
197   char const *  vserver;
198   VserverTag    tag;
199   
200   while (1) {
201     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
202     if (c==-1) break;
203
204     switch (c) {
205       case 'h'          :  showHelp(1, argv[0], 0);
206       case 'v'          :  showVersion();
207       case 'l'          :  showTags();
208       case 'q'          :  quiet = true; break;
209       default           :
210         WRITE_MSG(2, "Try '");
211         WRITE_STR(2, argv[0]);
212         WRITE_MSG(2, " --help\" for more information.\n");
213         exit(1);
214         break;
215     }
216   }
217
218   if (optind+2>argc) {
219     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
220     exit(1);
221   }
222
223   vserver = argv[optind];
224   tag     = stringToTag(argv[optind+1]);
225
226   if (tag==tgNONE) {
227     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
228     exit(1);
229   }
230
231   if (quiet) {
232     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
233     Edup2(fd, 1);
234     Eclose(fd);
235   }
236
237   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
238 }