updated VDIR tag to new interface
[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 }     VserverTag;
36
37 static struct {
38     char const * const  tag;
39     VserverTag const    val;
40     char const * const  descr;
41 }  const TAGS[] = {
42   { "CONTEXT", tgCONTEXT, "the current and/or assigned context" },
43   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
44   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
45   { "NAME",    tgNAME,    "gives out the name of the vserver" },
46   { "CFGDIR",  tgCFGDIR,  "gives out the configuration directory of the vserver" },
47   { "APPDIR",  tgAPPDIR,  "gives out the name of the toplevel application cfgdir" },
48 };
49
50 #define TAGS_COUNT      (sizeof(TAGS)/sizeof(TAGS[0]))
51
52 int wrapper_exit_code = 1;
53
54 static struct option const
55 CMDLINE_OPTIONS[] = {
56   { "help",     no_argument,  0, 'h' },
57   { "version",  no_argument,  0, 'v' },
58   { 0,0,0,0 }
59 };
60
61
62 static void
63 showHelp(int fd, char const *cmd, int res)
64 {
65   WRITE_MSG(fd, "Usage:  ");
66   WRITE_STR(fd, cmd);
67   WRITE_MSG(fd,
68             " [-q] <vserver> <tag>\n"
69             "Please report bugs to " PACKAGE_BUGREPORT "\n");
70   exit(res);
71 }
72
73 static void
74 showVersion()
75 {
76   WRITE_MSG(1,
77             "vserver-info " VERSION " -- returns information about vservers\n"
78             "This program is part of " PACKAGE_STRING "\n\n"
79             "Copyright (C) 2003 Enrico Scholz\n"
80             VERSION_COPYRIGHT_DISCLAIMER);
81   exit(0);
82 }
83
84 static void
85 showTags()
86 {
87   char const *          delim = "";
88   size_t        i;
89
90   WRITE_MSG(1, "Valid tags are: ");
91   for (i=0; i<TAGS_COUNT; ++i) {
92     WRITE_STR(1, delim);
93     WRITE_STR(1, TAGS[i].tag);
94
95     delim = ", ";
96   }
97   WRITE_MSG(1, "\n");
98   exit(0);
99 }
100
101 static VserverTag
102 stringToTag(char const *str)
103 {
104   size_t        i;
105   for (i=0; i<TAGS_COUNT; ++i)
106     if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
107
108   return tgNONE;
109 }
110
111 static int
112 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
113 {
114   char const *          res = 0;
115   char                  buf[sizeof(xid_t)*4 + 16];
116   xid_t                 ctx;
117   
118   switch (tag) {
119     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
120     case tgVDIR         :
121       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
122       break;
123     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
124     case tgAPPDIR       :
125       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
126       break;
127     case tgCONTEXT      :
128       ctx = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
129       if (ctx!=VC_NOCTX) {
130         utilvserver_fmt_long(buf, ctx);
131         res = buf;
132       }
133       break;
134       
135     case tgRUNNING      :
136       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
137       break;
138
139     default             :  assert(false); abort();  // TODO
140   }
141
142   if (res==0) return EXIT_FAILURE;
143   WRITE_STR(1, res);
144   WRITE_MSG(1, "\n");
145   return EXIT_SUCCESS;
146 }
147
148 int main(int argc, char *argv[])
149 {
150   bool          quiet = false;
151   char const *  vserver;
152   VserverTag    tag;
153   
154   while (1) {
155     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
156     if (c==-1) break;
157
158     switch (c) {
159       case 'h'          :  showHelp(1, argv[0], 0);
160       case 'v'          :  showVersion();
161       case 'l'          :  showTags();
162       case 'q'          :  quiet = true; break;
163       default           :
164         WRITE_MSG(2, "Try '");
165         WRITE_STR(2, argv[0]);
166         WRITE_MSG(2, " --help\" for more information.\n");
167         exit(1);
168         break;
169     }
170   }
171
172   if (optind+2>argc) {
173     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
174     exit(1);
175   }
176
177   vserver = argv[optind];
178   tag     = stringToTag(argv[optind+1]);
179
180   if (tag==tgNONE) {
181     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
182     exit(1);
183   }
184
185   if (quiet) {
186     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
187     Edup2(fd, 1);
188     Eclose(fd);
189   }
190
191   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
192 }