64f6832e0f1a4ff3b1779ecc644caa8e9f84276a
[util-vserver.git] / util-vserver / src / vuname.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 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 "vserver.h"
24 #include "util.h"
25
26 #include <getopt.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <stdio.h>
30 #include <libgen.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34
35 #define ENSC_WRAPPERS_PREFIX    "vuname: "
36 #define ENSC_WRAPPERS_UNISTD    1
37 #define ENSC_WRAPPERS_IO        1
38 #include <wrappers.h>
39
40 #define CMD_HELP                0x1000
41 #define CMD_VERSION             0x1001
42 #define CMD_UTSCONTEXT          0x4000
43 #define CMD_UTSSYSNAME          0x4001
44 #define CMD_UTSNODENAME         0x4002
45 #define CMD_UTSRELEASE          0x4003
46 #define CMD_UTSVERSION          0x4004
47 #define CMD_UTSMACHINE          0x4005
48 #define CMD_UTSDOMAINNAME       0x4006
49 #define CMD_DIR                 0x4007
50 #define CMD_MISSINGOK           0x4008
51
52 int                     wrapper_exit_code = 255;
53
54 static vc_uts_type const        UTS_MAPPING[7] = {
55   vcVHI_CONTEXT, vcVHI_SYSNAME, vcVHI_NODENAME,
56   vcVHI_RELEASE, vcVHI_VERSION, vcVHI_MACHINE,
57   vcVHI_DOMAINNAME };
58
59 #define DECL(UTS) [vcVHI_ ## UTS] = #UTS
60 static char const * const       UTS_STRINGS[] = {
61   DECL(CONTEXT), DECL(SYSNAME), DECL(NODENAME), DECL(RELEASE),
62   DECL(VERSION), DECL(MACHINE), DECL(DOMAINNAME)
63 };
64
65 struct Arguments {
66     bool                handle_opts[DIM_OF(UTS_MAPPING)];
67     xid_t               xid;
68     bool                do_set;
69     char const *        value;
70     char const *        dir;
71     bool                is_missingok;
72 };
73
74 static struct option const
75 CMDLINE_OPTIONS[] = {
76   { "help",     no_argument,  0, CMD_HELP },
77   { "version",  no_argument,  0, CMD_VERSION },
78   { "xid",      required_argument, 0, 'x' },
79   { "set",      no_argument,       0, 's' },
80   { "get",      no_argument,       0, 'g' },
81   { "context",     no_argument, 0, CMD_UTSCONTEXT },
82   { "sysname",     no_argument, 0, CMD_UTSSYSNAME },
83   { "nodename",    no_argument, 0, CMD_UTSNODENAME },
84   { "release",     no_argument, 0, CMD_UTSRELEASE },
85   { "version",     no_argument, 0, CMD_UTSVERSION },
86   { "machine",     no_argument, 0, CMD_UTSMACHINE },
87   { "domainname" , no_argument, 0, CMD_UTSDOMAINNAME },
88   { "dir",         required_argument, 0, CMD_DIR },
89   { "missingok",   no_argument,       0, CMD_MISSINGOK },
90   { 0,0,0,0 }
91 };
92
93 static void
94 showHelp(int fd, char const *cmd, int res)
95 {
96   VSERVER_DECLARE_CMD(cmd);
97   
98   WRITE_MSG(fd, "Usage:  ");
99   WRITE_STR(fd, cmd);
100   WRITE_MSG(fd,
101             " [-g] [--xid|x <xid>] [--<TAG>]*\n"
102             "    or  ");
103   WRITE_STR(fd, cmd);
104   WRITE_MSG(fd,     
105             "  -s  [--xid|x <xid>]  --<TAG> [--] <value>\n\n"
106             " Options:\n"
107             "   -x <xid>  ...  operate on this context (default: current one)\n"
108             "   -g        ...  get and print the value\n"
109             "   -s        ...  set the value\n\n"
110             " Valid TAGs are:\n"
111             "   context, sysname, nodename, release, version, machine, domainname\n\n"
112             "Please report bugs to " PACKAGE_BUGREPORT "\n");
113   exit(res);
114 }
115
116 static void
117 showVersion()
118 {
119   WRITE_MSG(1,
120             "vuname " VERSION " -- modifies and shows uname entries of vserver contexts\n"
121             "This program is part of " PACKAGE_STRING "\n\n"
122             "Copyright (C) 2004 Enrico Scholz\n"
123             VERSION_COPYRIGHT_DISCLAIMER);
124   exit(0);
125 }
126
127 static unsigned int
128 getTrueCount(bool const *field, size_t cnt)
129 {
130   unsigned int  res = 0;
131   while (cnt>0) {
132     --cnt;
133     if (field[cnt]) ++res;
134   }
135
136   return res;
137 }
138
139 static int
140 setFromDir(char const *pathname, bool is_missingok, xid_t xid)
141 {
142   struct stat           st;
143   size_t                i;
144   size_t                l_pathname = strlen(pathname);
145   char                  buf[l_pathname + sizeof("/domainname") + 32];
146   
147   if (stat(pathname, &st)==-1) {
148     if (errno==ENOENT && is_missingok) return EXIT_SUCCESS;
149     PERROR_Q(ENSC_WRAPPERS_PREFIX "fstat", pathname);
150     exit(wrapper_exit_code);
151   }
152
153   memcpy(buf, pathname, l_pathname);
154   if (l_pathname>0 && pathname[l_pathname-1]!='/')
155     buf[l_pathname++] = '/';
156   
157   for (i=0; i<DIM_OF(UTS_STRINGS); ++i) {
158     char *      ptr   = buf+l_pathname;
159     int         fd;
160
161     // ignore unimplemented uts-names
162     if (UTS_STRINGS[i]==0) continue;
163     strcpy(ptr, UTS_STRINGS[i]);
164     for (;*ptr;++ptr) *ptr = tolower(*ptr);
165     fd = open(buf, O_RDONLY);
166     if (fd!=-1) {
167       size_t    l = Elseek(fd, 0, SEEK_END);
168       char      name[l+1];
169       Elseek(fd,0,SEEK_SET);
170       EreadAll(fd, name, l); name[l] = '\0';
171       Eclose(fd);
172
173       if (vc_set_vhi_name(xid, (vc_uts_type)(i), name, l)==-1) {
174         PERROR_U(ENSC_WRAPPERS_PREFIX "vc_set_vhi_name", UTS_STRINGS[i]);
175         exit(wrapper_exit_code);
176       }
177     }
178   }
179
180   return EXIT_SUCCESS;
181 }
182
183 int main(int argc, char *argv[])
184 {
185   struct Arguments      args = {
186     .handle_opts = { 0,0,0,0,0,0,0 },
187     .do_set      = false,
188     .xid         = VC_SAMECTX,
189   };
190   unsigned int          opt_cnt;
191   size_t                i;
192   bool                  failed = false;
193   bool                  passed = false;
194   char const *          delim  = "";
195   char                  result_buf[1024] = { [0] = '\0' };
196
197   assert(DIM_OF(UTS_MAPPING) == DIM_OF(args.handle_opts));
198   
199   while (1) {
200     int         c = getopt_long(argc, argv, "+gsx:", CMDLINE_OPTIONS, 0);
201     if (c==-1) break;
202
203     if (c>=CMD_UTSCONTEXT && c<=CMD_UTSDOMAINNAME)
204       args.handle_opts[c-CMD_UTSCONTEXT] = true;
205     else switch (c) {
206       case CMD_HELP     :  showHelp(1, argv[0], 0);
207       case CMD_VERSION  :  showVersion();
208       case CMD_DIR      :  args.dir          = optarg; break;
209       case CMD_MISSINGOK:  args.is_missingok = true; break;
210       case 'g'          :  args.do_set       = true; break;
211       case 's'          :  args.do_set       = true; break;
212       case 'x'          :  args.xid          = atoi(optarg); break;
213       default           :
214         WRITE_MSG(2, "Try '");
215         WRITE_STR(2, argv[0]);
216         WRITE_MSG(2, " --help\" for more information.\n");
217         return EXIT_FAILURE;
218         break;
219     }
220   }
221
222   if (args.dir) {
223     int         rc = setFromDir(args.dir, args.is_missingok, args.xid);
224     if (rc==0 && optind<argc)
225       EexecvpD(argv[optind], argv+optind);
226
227     return rc;
228   }
229
230   opt_cnt = getTrueCount(args.handle_opts, DIM_OF(args.handle_opts));
231   
232   if (args.do_set && optind==argc)
233     WRITE_MSG(2, "No value given; use '--help' for more information\n");
234   else if (args.do_set && opt_cnt<=0)
235     WRITE_MSG(2, "No field given which shall be set; use '--help' for more information\n");
236   else if (args.do_set && opt_cnt>1)
237     WRITE_MSG(2, "Can not set multiple fields; use '--help' for more information\n");
238   else if (!args.do_set && optind!=argc)
239     WRITE_MSG(2, "Can not specifiy a value with '-g'; use '--help' for more information\n");
240   else {
241     if (args.do_set) args.value = argv[optind];
242
243     if (!args.do_set && opt_cnt==0)
244       for (i=0; i<DIM_OF(args.handle_opts); ++i) args.handle_opts[i]=true;
245     
246     for (i=0; i<DIM_OF(args.handle_opts); ++i) {
247       if (!args.handle_opts[i]) continue;
248
249       if (args.do_set) {
250         if (vc_set_vhi_name(args.xid, UTS_MAPPING[i], args.value, strlen(args.value))==-1) {
251           perror("vc_set_vhi_name()");
252           return EXIT_FAILURE;
253         }
254       }
255       else {
256         char            buf[128];
257         if (vc_get_vhi_name(args.xid, UTS_MAPPING[i], buf, sizeof(buf)-1)==-1) {
258           perror("vc_get_vhi_name()");
259           failed = true;
260           strcpy(buf, "???");
261         }
262         else
263           passed = true;
264         strcat(result_buf, delim);
265         strcat(result_buf, buf);
266         delim = " ";
267       }
268     }
269
270     if (!args.do_set && passed) {
271       strcat(result_buf, "\n");
272       WRITE_STR(1, result_buf);
273     }
274
275     if ((!failed || passed) && optind+1<argc)
276       EexecvpD(argv[optind+1], argv+optind+1);
277
278     return failed ? passed ? wrapper_exit_code-1 : wrapper_exit_code : EXIT_SUCCESS;
279   }
280
281   return wrapper_exit_code;
282 }