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 { "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 },
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>] --<TAG> [--] <value>\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"
111 " context, sysname, nodename, release, version, machine, domainname\n\n"
112 "Please report bugs to " PACKAGE_BUGREPORT "\n");
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);
128 getTrueCount(bool const *field, size_t cnt)
130 unsigned int res = 0;
133 if (field[cnt]) ++res;
140 setFromDir(char const *pathname, bool is_missingok, xid_t xid)
144 size_t l_pathname = strlen(pathname);
145 char buf[l_pathname + sizeof("/domainname") + 32];
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);
153 memcpy(buf, pathname, l_pathname);
154 if (l_pathname>0 && pathname[l_pathname-1]!='/')
155 buf[l_pathname++] = '/';
157 for (i=0; i<DIM_OF(UTS_STRINGS); ++i) {
158 char * ptr = buf+l_pathname;
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);
167 size_t l = Elseek(fd, 0, SEEK_END);
169 Elseek(fd,0,SEEK_SET);
170 EreadAll(fd, name, l); name[l] = '\0';
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);
183 int main(int argc, char *argv[])
185 struct Arguments args = {
186 .handle_opts = { 0,0,0,0,0,0,0 },
190 unsigned int opt_cnt;
194 char const * delim = "";
195 char result_buf[1024] = { [0] = '\0' };
197 assert(DIM_OF(UTS_MAPPING) == DIM_OF(args.handle_opts));
200 int c = getopt_long(argc, argv, "gsx:", CMDLINE_OPTIONS, 0);
203 if (c>=CMD_UTSCONTEXT && c<=CMD_UTSDOMAINNAME)
204 args.handle_opts[c-CMD_UTSCONTEXT] = true;
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;
214 WRITE_MSG(2, "Try '");
215 WRITE_STR(2, argv[0]);
216 WRITE_MSG(2, " --help\" for more information.\n");
223 int rc = setFromDir(args.dir, args.is_missingok, args.xid);
224 if (rc==0 && optind<argc)
225 EexecvpD(argv[optind], argv+optind);
230 opt_cnt = getTrueCount(args.handle_opts, DIM_OF(args.handle_opts));
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");
241 if (args.do_set) args.value = argv[optind];
243 if (!args.do_set && opt_cnt==0)
244 for (i=0; i<DIM_OF(args.handle_opts); ++i) args.handle_opts[i]=true;
246 for (i=0; i<DIM_OF(args.handle_opts); ++i) {
247 if (!args.handle_opts[i]) continue;
250 if (vc_set_vhi_name(args.xid, UTS_MAPPING[i], args.value, strlen(args.value))==-1) {
251 perror("vc_set_vhi_name()");
257 if (vc_get_vhi_name(args.xid, UTS_MAPPING[i], buf, sizeof(buf)-1)==-1) {
258 perror("vc_get_vhi_name()");
264 strcat(result_buf, delim);
265 strcat(result_buf, buf);
270 if (!args.do_set && passed) {
271 strcat(result_buf, "\n");
272 WRITE_STR(1, result_buf);
275 if ((!failed || passed) && optind+1<argc)
276 EexecvpD(argv[optind+1], argv+optind+1);
278 return failed ? passed ? wrapper_exit_code-1 : wrapper_exit_code : EXIT_SUCCESS;
281 return wrapper_exit_code;