added APPDIR tag
[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         :  res = vc_getVserverVdir(vserver, vcCFG_AUTO); break;
121     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
122     case tgAPPDIR       :
123       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
124       break;
125     case tgCONTEXT      :
126       ctx = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
127       if (ctx!=VC_NOCTX) {
128         utilvserver_fmt_long(buf, ctx);
129         res = buf;
130       }
131       break;
132       
133     case tgRUNNING      :
134       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
135       break;
136
137     default             :  assert(false); abort();  // TODO
138   }
139
140   if (res==0) return EXIT_FAILURE;
141   WRITE_STR(1, res);
142   WRITE_MSG(1, "\n");
143   return EXIT_SUCCESS;
144 }
145
146 int main(int argc, char *argv[])
147 {
148   bool          quiet = false;
149   char const *  vserver;
150   VserverTag    tag;
151   
152   while (1) {
153     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
154     if (c==-1) break;
155
156     switch (c) {
157       case 'h'          :  showHelp(1, argv[0], 0);
158       case 'v'          :  showVersion();
159       case 'l'          :  showTags();
160       case 'q'          :  quiet = true; break;
161       default           :
162         WRITE_MSG(2, "Try '");
163         WRITE_STR(2, argv[0]);
164         WRITE_MSG(2, " --help\" for more information.\n");
165         exit(1);
166         break;
167     }
168   }
169
170   if (optind+2>argc) {
171     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
172     exit(1);
173   }
174
175   vserver = argv[optind];
176   tag     = stringToTag(argv[optind+1]);
177
178   if (tag==tgNONE) {
179     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
180     exit(1);
181   }
182
183   if (quiet) {
184     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
185     Edup2(fd, 1);
186     Eclose(fd);
187   }
188
189   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
190 }