3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
35 #define ENSC_WRAPPERS_PREFIX "vuname: "
36 #define ENSC_WRAPPERS_UNISTD 1
37 #define ENSC_WRAPPERS_IO 1
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
52 int wrapper_exit_code = 255;
54 static vc_uts_type const UTS_MAPPING[7] = {
55 vcVHI_CONTEXT, vcVHI_SYSNAME, vcVHI_NODENAME,
56 vcVHI_RELEASE, vcVHI_VERSION, vcVHI_MACHINE,
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)
66 bool handle_opts[DIM_OF(UTS_MAPPING)];
74 static struct option const
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 },
94 showHelp(int fd, char const *cmd, int res)
96 VSERVER_DECLARE_CMD(cmd);
98 WRITE_MSG(fd, "Usage: ");
101 " [-g] [--xid|x <xid>] [--<TAG>]*\n"
105 " -s [--xid|x <xid>] (--dir <dir> [--missingok] [--])|(--<TAG> [--] <value>) [<command> <args>*]\n\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"
116 " context, sysname, nodename, release, ver, machine, domainname\n"
118 "Please report bugs to " PACKAGE_BUGREPORT "\n");
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);
134 getTrueCount(bool const *field, size_t cnt)
136 unsigned int res = 0;
139 if (field[cnt]) ++res;
146 setFromDir(char const *pathname, bool is_missingok, xid_t xid)
150 size_t l_pathname = strlen(pathname);
151 char buf[l_pathname + sizeof("/domainname") + 32];
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);
159 memcpy(buf, pathname, l_pathname);
160 if (l_pathname>0 && pathname[l_pathname-1]!='/')
161 buf[l_pathname++] = '/';
163 for (i=0; i<DIM_OF(UTS_STRINGS); ++i) {
164 char * ptr = buf+l_pathname;
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);
173 size_t l = Elseek(fd, 0, SEEK_END);
175 Elseek(fd,0,SEEK_SET);
176 EreadAll(fd, name, l); name[l] = '\0';
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);
189 int main(int argc, char *argv[])
191 struct Arguments args = {
192 .handle_opts = { 0,0,0,0,0,0,0 },
196 unsigned int opt_cnt;
200 char const * delim = "";
201 char result_buf[1024] = { [0] = '\0' };
203 assert(DIM_OF(UTS_MAPPING) == DIM_OF(args.handle_opts));
206 int c = getopt_long(argc, argv, "+gsx:", CMDLINE_OPTIONS, 0);
209 if (c>=CMD_UTSCONTEXT && c<=CMD_UTSDOMAINNAME)
210 args.handle_opts[c-CMD_UTSCONTEXT] = true;
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;
220 WRITE_MSG(2, "Try '");
221 WRITE_STR(2, argv[0]);
222 WRITE_MSG(2, " --help\" for more information.\n");
229 int rc = setFromDir(args.dir, args.is_missingok, args.xid);
230 if (rc==0 && optind<argc)
231 EexecvpD(argv[optind], argv+optind);
236 opt_cnt = getTrueCount(args.handle_opts, DIM_OF(args.handle_opts));
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");
247 if (args.do_set) args.value = argv[optind];
249 if (!args.do_set && opt_cnt==0)
250 for (i=0; i<DIM_OF(args.handle_opts); ++i) args.handle_opts[i]=true;
252 for (i=0; i<DIM_OF(args.handle_opts); ++i) {
253 if (!args.handle_opts[i]) continue;
256 if (vc_set_vhi_name(args.xid, UTS_MAPPING[i], args.value, strlen(args.value))==-1) {
257 perror("vc_set_vhi_name()");
263 if (vc_get_vhi_name(args.xid, UTS_MAPPING[i], buf, sizeof(buf)-1)==-1) {
264 perror("vc_get_vhi_name()");
270 strcat(result_buf, delim);
271 strcat(result_buf, buf);
276 if (!args.do_set && passed) {
277 strcat(result_buf, "\n");
278 WRITE_STR(1, result_buf);
281 if ((!failed || passed) && optind+1<argc)
282 EexecvpD(argv[optind+1], argv+optind+1);
284 return failed ? passed ? wrapper_exit_code-1 : wrapper_exit_code : EXIT_SUCCESS;
287 return wrapper_exit_code;