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