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.
32 #define CMD_HELP 0x1000
33 #define CMD_VERSION 0x1001
34 #define CMD_UTSCONTEXT 0x4000
35 #define CMD_UTSSYSNAME 0x4001
36 #define CMD_UTSNODENAME 0x4002
37 #define CMD_UTSRELEASE 0x4003
38 #define CMD_UTSVERSION 0x4004
39 #define CMD_UTSMACHINE 0x4005
40 #define CMD_UTSDOMAINNAME 0x4006
43 static vc_uts_type const UTS_MAPPING[7] = {
44 vcVHI_CONTEXT, vcVHI_SYSNAME, vcVHI_NODENAME,
45 vcVHI_RELEASE, vcVHI_VERSION, vcVHI_MACHINE,
49 bool handle_opts[DIM_OF(UTS_MAPPING)];
55 static struct option const
57 { "help", no_argument, 0, CMD_HELP },
58 { "version", no_argument, 0, CMD_VERSION },
59 { "xid", required_argument, 0, 'x' },
60 { "set", no_argument, 0, 's' },
61 { "get", no_argument, 0, 'g' },
62 { "context", no_argument, 0, CMD_UTSCONTEXT },
63 { "sysname", no_argument, 0, CMD_UTSSYSNAME },
64 { "nodename", no_argument, 0, CMD_UTSNODENAME },
65 { "release", no_argument, 0, CMD_UTSRELEASE },
66 { "version", no_argument, 0, CMD_UTSVERSION },
67 { "machine", no_argument, 0, CMD_UTSMACHINE },
68 { "domainname" , no_argument, 0, CMD_UTSDOMAINNAME },
73 showHelp(int fd, char const *cmd, int res)
75 VSERVER_DECLARE_CMD(cmd);
77 WRITE_MSG(fd, "Usage: ");
80 " [-g] [--xid|x <xid>] [--<TAG>]*\n"
84 " -s [--xid|x <xid>] --<TAG> [--] <value>\n\n"
86 " -x <xid> ... operate on this context (default: current one)\n"
87 " -g ... get and print the value\n"
88 " -s ... set the value\n\n"
90 " context, sysname, nodename, release, version, machine, domainname\n\n"
91 "Please report bugs to " PACKAGE_BUGREPORT "\n");
99 "vuname " VERSION " -- modifies and shows uname entries of vserver contexts\n"
100 "This program is part of " PACKAGE_STRING "\n\n"
101 "Copyright (C) 2004 Enrico Scholz\n"
102 VERSION_COPYRIGHT_DISCLAIMER);
107 getTrueCount(bool const *field, size_t cnt)
109 unsigned int res = 0;
112 if (field[cnt]) ++res;
118 int main(int argc, char *argv[])
120 struct Arguments args = {
121 .handle_opts = { 0,0,0,0,0,0,0 },
125 unsigned int opt_cnt;
129 char const * delim = "";
130 char result_buf[1024] = { [0] = '\0' };
132 assert(DIM_OF(UTS_MAPPING) == DIM_OF(args.handle_opts));
135 int c = getopt_long(argc, argv, "gsx:", CMDLINE_OPTIONS, 0);
138 if (c>=CMD_UTSCONTEXT && c<=CMD_UTSDOMAINNAME)
139 args.handle_opts[c-CMD_UTSCONTEXT] = true;
141 case CMD_HELP : showHelp(1, argv[0], 0);
142 case CMD_VERSION : showVersion();
143 case 'g' : args.do_set = true; break;
144 case 's' : args.do_set = true; break;
145 case 'x' : args.xid = atoi(optarg); break;
147 WRITE_MSG(2, "Try '");
148 WRITE_STR(2, argv[0]);
149 WRITE_MSG(2, " --help\" for more information.\n");
155 opt_cnt = getTrueCount(args.handle_opts, DIM_OF(args.handle_opts));
157 if (args.do_set && optind==argc) {
158 WRITE_MSG(2, "No value given; use '--help' for more information\n");
162 if (args.do_set && optind+1>argc) {
163 WRITE_MSG(2, "Too much values given; use '--help' for more information\n");
167 if (args.do_set && opt_cnt<=0) {
168 WRITE_MSG(2, "No field given which shall be set; use '--help' for more information\n");
172 if (args.do_set && opt_cnt>1) {
173 WRITE_MSG(2, "Can not set multiple fields; use '--help' for more information\n");
177 if (!args.do_set && optind!=argc) {
178 WRITE_MSG(2, "Can not specifiy a value with '-g'; use '--help' for more information\n");
182 if (args.do_set) args.value = argv[optind];
184 if (!args.do_set && opt_cnt==0)
185 for (i=0; i<DIM_OF(args.handle_opts); ++i) args.handle_opts[i]=true;
187 for (i=0; i<DIM_OF(args.handle_opts); ++i) {
188 if (!args.handle_opts[i]) continue;
191 if (vc_set_vhi_name(args.xid, UTS_MAPPING[i], args.value, strlen(args.value))==-1) {
192 perror("vc_set_vhi_name()");
198 if (vc_get_vhi_name(args.xid, UTS_MAPPING[i], buf, sizeof(buf)-1)==-1) {
199 perror("vc_get_vhi_name()");
205 strcat(result_buf, delim);
206 strcat(result_buf, buf);
211 if (!args.do_set && passed) {
212 strcat(result_buf, "\n");
213 WRITE_STR(1, result_buf);
216 return failed ? passed ? 2 : EXIT_FAILURE : EXIT_SUCCESS;