use new ensc_wrappers/ headers
[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
25 #include "internal.h"
26 #include "vserver.h"
27
28 #include <stdlib.h>
29 #include <getopt.h>
30 #include <assert.h>
31 #include <stdbool.h>
32 #include <fcntl.h>
33
34 #define ENSC_WRAPPERS_FCNTL     1
35 #define ENSC_WRAPPERS_UNISTD    1
36 #include <wrappers.h>
37
38 typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
39                tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
40                tgAPIVER,
41                tgINITPID, tgINITPID_PID,
42                tgXID, tgUTS,
43 }       VserverTag;
44
45 static struct {
46     char const * const  tag;
47     VserverTag const    val;
48     char const * const  descr;
49 }  const TAGS[] = {
50   { "CONTEXT", tgCONTEXT, "the current and/or assigned context" },
51   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
52   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
53   { "NAME",    tgNAME,    "gives out the name of the vserver" },
54   { "CFGDIR",  tgCFGDIR,  "gives out the configuration directory of the vserver" },
55   { "APPDIR",  tgAPPDIR,  "gives out the name of the toplevel application cfgdir" },
56   { "INITPID",     tgINITPID,     "gives out the initpid of the given context" },
57   { "INITPID_PID", tgINITPID_PID, "gives out the initpid of the given pid" },
58   { "XID",         tgXID,         "gives out the context-id of the given pid" },
59   { "APIVER",      tgAPIVER,      "gives out the version of the kernel API" },
60   { "UTS",         tgUTS,         ("gives out an uts-entry; possible entries are "
61                                    "context, sysname, nodename, release, version, "
62                                    "machine and domainname") },
63 };
64
65 int wrapper_exit_code = 1;
66
67 static struct option const
68 CMDLINE_OPTIONS[] = {
69   { "help",     no_argument,  0, 'h' },
70   { "version",  no_argument,  0, 'v' },
71   { 0,0,0,0 }
72 };
73
74
75 static void
76 showHelp(int fd, char const *cmd, int res)
77 {
78   WRITE_MSG(fd, "Usage:  ");
79   WRITE_STR(fd, cmd);
80   WRITE_MSG(fd,
81             " [-ql] <vserver>|<pid>|<context> <tag>\n"
82             "Please report bugs to " PACKAGE_BUGREPORT "\n");
83   exit(res);
84 }
85
86 static void
87 showVersion()
88 {
89   WRITE_MSG(1,
90             "vserver-info " VERSION " -- returns information about vservers\n"
91             "This program is part of " PACKAGE_STRING "\n\n"
92             "Copyright (C) 2003 Enrico Scholz\n"
93             VERSION_COPYRIGHT_DISCLAIMER);
94   exit(0);
95 }
96
97 static void
98 showTags()
99 {
100   char const *          delim = "";
101   size_t        i;
102
103   WRITE_MSG(1, "Valid tags are: ");
104   for (i=0; i<DIM_OF(TAGS); ++i) {
105     WRITE_STR(1, delim);
106     WRITE_STR(1, TAGS[i].tag);
107
108     delim = ", ";
109   }
110   WRITE_MSG(1, "\n");
111   exit(0);
112 }
113
114 static VserverTag
115 stringToTag(char const *str)
116 {
117   size_t        i;
118   for (i=0; i<DIM_OF(TAGS); ++i)
119     if (strcmp(TAGS[i].tag, str)==0) return TAGS[i].val;
120
121   return tgNONE;
122 }
123
124 static vc_uts_type
125 utsText2Tag(char const *str)
126 {
127   if      (strcmp(str, "context")   ==0) return vcVHI_CONTEXT;
128   else if (strcmp(str, "sysname")   ==0) return vcVHI_SYSNAME;
129   else if (strcmp(str, "nodename")  ==0) return vcVHI_NODENAME;
130   else if (strcmp(str, "release")   ==0) return vcVHI_RELEASE;
131   else if (strcmp(str, "version")   ==0) return vcVHI_VERSION;
132   else if (strcmp(str, "machine")   ==0) return vcVHI_MACHINE;
133   else if (strcmp(str, "domainname")==0) return vcVHI_DOMAINNAME;
134   else {
135     WRITE_MSG(2, "Unknown UTS tag\n");
136     exit(1);
137   }
138 }
139
140 static int
141 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
142 {
143   char const *          res = 0;
144   char                  buf[sizeof(xid_t)*4 + 1024];
145   xid_t                 xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
146
147   memset(buf, 0, sizeof buf);
148   switch (tag) {
149     case tgNAME         :  res = vc_getVserverName(vserver, vcCFG_AUTO); break;
150     case tgVDIR         :
151       res = vc_getVserverVdir(vserver, vcCFG_AUTO, argc>0 && atoi(argv[0]));
152       break;
153     case tgCFGDIR       :  res = vc_getVserverCfgDir(vserver, vcCFG_AUTO);     break;
154     case tgAPPDIR       :
155       res = vc_getVserverAppDir(vserver, vcCFG_AUTO, argc==0 ? "" : argv[0]);
156       break;
157       
158     case tgCONTEXT      :
159       xid = vc_getVserverCtx(vserver, vcCFG_AUTO, true, 0);
160       if (xid!=VC_NOCTX) {
161         utilvserver_fmt_long(buf, xid);
162         res = buf;
163       }
164       break;
165       
166     case tgRUNNING      :
167       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
168       break;
169
170     case tgXID          :
171     {
172       pid_t     pid = atoi(vserver);
173
174       xid = vc_get_task_xid(pid);
175       if (xid==VC_NOCTX) perror("vc_get_task_xid()");
176       else {
177         utilvserver_fmt_long(buf, xid);
178         res = buf;
179       }
180       break;
181     }
182
183     case tgINITPID      :
184     {
185       struct vc_vx_info         info;
186       if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
187       else {
188         utilvserver_fmt_long(buf, info.xid);
189         res = buf;
190       }
191       break;
192     }
193
194     case tgINITPID_PID  :
195     {
196       pid_t                     pid = atoi(vserver);
197       struct vc_vx_info         info;
198
199       xid = vc_get_task_xid(pid);
200       if (xid==VC_NOCTX) perror("vc_get_task_xid()");
201       else if (vc_get_vx_info(xid, &info)==-1) perror("vc_get_vx_info()");
202       else {
203         utilvserver_fmt_long(buf, info.xid);
204         res = buf;
205       }
206       break;
207     }
208
209     case tgAPIVER       :
210     {
211       int       v = vc_get_version();
212       if (v!=-1) {
213         size_t  l = utilvserver_fmt_xulong(0, (unsigned int)v);
214         memcpy(buf, "0x00000000", 10);
215         utilvserver_fmt_xulong(buf+2+8-l, (unsigned int)v);
216         res = buf;
217       }
218       break;
219     }
220
221     case tgUTS          :
222     {
223       if (argc>0) {
224         vc_uts_type     type = utsText2Tag(argv[0]);
225         if (vc_get_vhi_name(xid, type, buf, sizeof(buf)-1)==-1)
226           perror("vc_get_vhi_name()");
227         else
228           res=buf;
229       }
230       else {
231         bool            is_passed = false;
232         char            tmp[128];
233 #define APPEND_UTS(TYPE) \
234         (((vc_get_vhi_name(xid, TYPE, tmp, sizeof(tmp)-1)!=-1) && (strcat(buf, tmp), strcat(buf, " "), is_passed=true)) || \
235          (strcat(buf, "??? ")))
236
237         APPEND_UTS(vcVHI_CONTEXT) &&
238           APPEND_UTS(vcVHI_SYSNAME) &&
239           APPEND_UTS(vcVHI_NODENAME) &&
240           APPEND_UTS(vcVHI_RELEASE) &&
241           APPEND_UTS(vcVHI_VERSION) &&
242           APPEND_UTS(vcVHI_MACHINE) &&
243           APPEND_UTS(vcVHI_DOMAINNAME) &&
244           is_passed &&
245           (res = buf);
246
247         if (!is_passed)
248           perror("vc_get_vhi_name()");
249
250 #undef APPEND_UTS
251       }
252       break;
253     }
254     
255     default             :  assert(false); abort();  // TODO
256   }
257
258   if (res==0) return EXIT_FAILURE;
259   WRITE_STR(1, res);
260   WRITE_MSG(1, "\n");
261   return EXIT_SUCCESS;
262 }
263
264 int main(int argc, char *argv[])
265 {
266   bool          quiet = false;
267   char const *  vserver;
268   VserverTag    tag;
269   
270   while (1) {
271     int         c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0);
272     if (c==-1) break;
273
274     switch (c) {
275       case 'h'          :  showHelp(1, argv[0], 0);
276       case 'v'          :  showVersion();
277       case 'l'          :  showTags();
278       case 'q'          :  quiet = true; break;
279       default           :
280         WRITE_MSG(2, "Try '");
281         WRITE_STR(2, argv[0]);
282         WRITE_MSG(2, " --help\" for more information.\n");
283         exit(1);
284         break;
285     }
286   }
287
288   if (optind+2>argc) {
289     WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
290     exit(1);
291   }
292
293   vserver = argv[optind];
294   tag     = stringToTag(argv[optind+1]);
295
296   if (tag==tgNONE) {
297     WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n");
298     exit(1);
299   }
300
301   if (quiet) {
302     int         fd = Eopen("/dev/null", O_WRONLY, 0644);
303     Edup2(fd, 1);
304     Eclose(fd);
305   }
306
307   return execQuery(vserver, tag, argc-(optind+2), argv+optind+2);
308 }