added more documentation
[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   { "xid",         required_argument, 0, 'x' },
77   { "set",         no_argument,       0, 's' },
78   { "get",         no_argument,       0, 'g' },
79   { "context",     no_argument,       0, CMD_UTSCONTEXT },
80   { "sysname",     no_argument,       0, CMD_UTSSYSNAME },
81   { "nodename",    no_argument,       0, CMD_UTSNODENAME },
82   { "release",     no_argument,       0, CMD_UTSRELEASE },
83   { "ver",         no_argument,       0, CMD_UTSVERSION },
84   { "machine",     no_argument,       0, CMD_UTSMACHINE },
85   { "domainname" , no_argument,       0, CMD_UTSDOMAINNAME },
86   { "dir",         required_argument, 0, CMD_DIR },
87   { "missingok",   no_argument,       0, CMD_MISSINGOK },
88   { "help",        no_argument,       0, CMD_HELP },
89   { "version",     no_argument,       0, CMD_VERSION },
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>] (--dir <dir> [--missingok] [--])|(--<TAG> [--] <value>) [<command> <args>*]\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             "   --dir <dir>  ...  read values from files in <dir>. These files must\n"
111             "                     have a valid TAG as their name. An exception is that\n"
112             "                     the 'ver' file must be named 'version'\n"
113             "   --missingok  ...  do not fail when the <DIR> from '--dir' does not exist.\n"
114             "\n"
115             " Valid TAGs are:\n"
116             "   context, sysname, nodename, release, ver, machine, domainname\n"
117             "\n"
118             "Please report bugs to " PACKAGE_BUGREPORT "\n");
119   exit(res);
120 }
121
122 static void
123 showVersion()
124 {
125   WRITE_MSG(1,
126             "vuname " VERSION " -- modifies and shows uname entries of vserver contexts\n"
127             "This program is part of " PACKAGE_STRING "\n\n"
128             "Copyright (C) 2004 Enrico Scholz\n"
129             VERSION_COPYRIGHT_DISCLAIMER);
130   exit(0);
131 }
132
133 static unsigned int
134 getTrueCount(bool const *field, size_t cnt)
135 {
136   unsigned int  res = 0;
137   while (cnt>0) {
138     --cnt;
139     if (field[cnt]) ++res;
140   }
141
142   return res;
143 }
144
145 static int
146 setFromDir(char const *pathname, bool is_missingok, xid_t xid)
147 {
148   struct stat           st;
149   size_t                i;
150   size_t                l_pathname = strlen(pathname);
151   char                  buf[l_pathname + sizeof("/domainname") + 32];
152   
153   if (stat(pathname, &st)==-1) {
154     if (errno==ENOENT && is_missingok) return EXIT_SUCCESS;
155     PERROR_Q(ENSC_WRAPPERS_PREFIX "fstat", pathname);
156     exit(wrapper_exit_code);
157   }
158
159   memcpy(buf, pathname, l_pathname);
160   if (l_pathname>0 && pathname[l_pathname-1]!='/')
161     buf[l_pathname++] = '/';
162   
163   for (i=0; i<DIM_OF(UTS_STRINGS); ++i) {
164     char *      ptr   = buf+l_pathname;
165     int         fd;
166
167     // ignore unimplemented uts-names
168     if (UTS_STRINGS[i]==0) continue;
169     strcpy(ptr, UTS_STRINGS[i]);
170     for (;*ptr;++ptr) *ptr = tolower(*ptr);
171     fd = open(buf, O_RDONLY);
172     if (fd!=-1) {
173       size_t    l = Elseek(fd, 0, SEEK_END);
174       char      name[l+1];
175       Elseek(fd,0,SEEK_SET);
176       EreadAll(fd, name, l); name[l] = '\0';
177       Eclose(fd);
178
179       if (vc_set_vhi_name(xid, (vc_uts_type)(i), name, l)==-1) {
180         PERROR_U(ENSC_WRAPPERS_PREFIX "vc_set_vhi_name", UTS_STRINGS[i]);
181         exit(wrapper_exit_code);
182       }
183     }
184   }
185
186   return EXIT_SUCCESS;
187 }
188
189 int main(int argc, char *argv[])
190 {
191   struct Arguments      args = {
192     .handle_opts = { 0,0,0,0,0,0,0 },
193     .do_set      = false,
194     .xid         = VC_SAMECTX,
195   };
196   unsigned int          opt_cnt;
197   size_t                i;
198   bool                  failed = false;
199   bool                  passed = false;
200   char const *          delim  = "";
201   char                  result_buf[1024] = { [0] = '\0' };
202
203   assert(DIM_OF(UTS_MAPPING) == DIM_OF(args.handle_opts));
204   
205   while (1) {
206     int         c = getopt_long(argc, argv, "+gsx:", CMDLINE_OPTIONS, 0);
207     if (c==-1) break;
208
209     if (c>=CMD_UTSCONTEXT && c<=CMD_UTSDOMAINNAME)
210       args.handle_opts[c-CMD_UTSCONTEXT] = true;
211     else switch (c) {
212       case CMD_HELP     :  showHelp(1, argv[0], 0);
213       case CMD_VERSION  :  showVersion();
214       case CMD_DIR      :  args.dir          = optarg; break;
215       case CMD_MISSINGOK:  args.is_missingok = true; break;
216       case 'g'          :  args.do_set       = true; break;
217       case 's'          :  args.do_set       = true; break;
218       case 'x'          :  args.xid          = atoi(optarg); break;
219       default           :
220         WRITE_MSG(2, "Try '");
221         WRITE_STR(2, argv[0]);
222         WRITE_MSG(2, " --help\" for more information.\n");
223         return EXIT_FAILURE;
224         break;
225     }
226   }
227
228   if (args.dir) {
229     int         rc = setFromDir(args.dir, args.is_missingok, args.xid);
230     if (rc==0 && optind<argc)
231       EexecvpD(argv[optind], argv+optind);
232
233     return rc;
234   }
235
236   opt_cnt = getTrueCount(args.handle_opts, DIM_OF(args.handle_opts));
237   
238   if (args.do_set && optind==argc)
239     WRITE_MSG(2, "No value given; use '--help' for more information\n");
240   else if (args.do_set && opt_cnt<=0)
241     WRITE_MSG(2, "No field given which shall be set; use '--help' for more information\n");
242   else if (args.do_set && opt_cnt>1)
243     WRITE_MSG(2, "Can not set multiple fields; use '--help' for more information\n");
244   else if (!args.do_set && optind!=argc)
245     WRITE_MSG(2, "Can not specifiy a value with '-g'; use '--help' for more information\n");
246   else {
247     if (args.do_set) args.value = argv[optind];
248
249     if (!args.do_set && opt_cnt==0)
250       for (i=0; i<DIM_OF(args.handle_opts); ++i) args.handle_opts[i]=true;
251     
252     for (i=0; i<DIM_OF(args.handle_opts); ++i) {
253       if (!args.handle_opts[i]) continue;
254
255       if (args.do_set) {
256         if (vc_set_vhi_name(args.xid, UTS_MAPPING[i], args.value, strlen(args.value))==-1) {
257           perror("vc_set_vhi_name()");
258           return EXIT_FAILURE;
259         }
260       }
261       else {
262         char            buf[128];
263         if (vc_get_vhi_name(args.xid, UTS_MAPPING[i], buf, sizeof(buf)-1)==-1) {
264           perror("vc_get_vhi_name()");
265           failed = true;
266           strcpy(buf, "???");
267         }
268         else
269           passed = true;
270         strcat(result_buf, delim);
271         strcat(result_buf, buf);
272         delim = " ";
273       }
274     }
275
276     if (!args.do_set && passed) {
277       strcat(result_buf, "\n");
278       WRITE_STR(1, result_buf);
279     }
280
281     if ((!failed || passed) && optind+1<argc)
282       EexecvpD(argv[optind+1], argv+optind+1);
283
284     return failed ? passed ? wrapper_exit_code-1 : wrapper_exit_code : EXIT_SUCCESS;
285   }
286
287   return wrapper_exit_code;
288 }