From ccdc248ee4d7a8d747c2c87848133855dc982b96 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Sat, 6 Mar 2004 01:54:13 +0000 Subject: [PATCH] made it chainable added '--dir' option git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@1126 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/src/vuname.c | 165 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 115 insertions(+), 50 deletions(-) diff --git a/util-vserver/src/vuname.c b/util-vserver/src/vuname.c index 2f0162b..0f5d422 100644 --- a/util-vserver/src/vuname.c +++ b/util-vserver/src/vuname.c @@ -28,6 +28,14 @@ #include #include #include +#include +#include +#include + +#define ENSC_WRAPPERS_PREFIX "vuname: " +#define ENSC_WRAPPERS_UNISTD 1 +#define ENSC_WRAPPERS_IO 1 +#include #define CMD_HELP 0x1000 #define CMD_VERSION 0x1001 @@ -38,18 +46,29 @@ #define CMD_UTSVERSION 0x4004 #define CMD_UTSMACHINE 0x4005 #define CMD_UTSDOMAINNAME 0x4006 +#define CMD_DIR 0x4007 +#define CMD_MISSINGOK 0x4008 +int wrapper_exit_code = 255; static vc_uts_type const UTS_MAPPING[7] = { vcVHI_CONTEXT, vcVHI_SYSNAME, vcVHI_NODENAME, vcVHI_RELEASE, vcVHI_VERSION, vcVHI_MACHINE, vcVHI_DOMAINNAME }; +#define DECL(UTS) [vcVHI_ ## UTS] = #UTS +static char const * const UTS_STRINGS[] = { + DECL(CONTEXT), DECL(SYSNAME), DECL(NODENAME), DECL(RELEASE), + DECL(VERSION), DECL(MACHINE), DECL(DOMAINNAME) +}; + struct Arguments { bool handle_opts[DIM_OF(UTS_MAPPING)]; xid_t xid; bool do_set; char const * value; + char const * dir; + bool is_missingok; }; static struct option const @@ -66,6 +85,8 @@ CMDLINE_OPTIONS[] = { { "version", no_argument, 0, CMD_UTSVERSION }, { "machine", no_argument, 0, CMD_UTSMACHINE }, { "domainname" , no_argument, 0, CMD_UTSDOMAINNAME }, + { "dir", required_argument, 0, CMD_DIR }, + { "missingok", no_argument, 0, CMD_MISSINGOK }, { 0,0,0,0 } }; @@ -115,6 +136,50 @@ getTrueCount(bool const *field, size_t cnt) return res; } +static int +setFromDir(char const *pathname, bool is_missingok, xid_t xid) +{ + struct stat st; + size_t i; + size_t l_pathname = strlen(pathname); + char buf[l_pathname + sizeof("/domainname") + 32]; + + if (stat(pathname, &st)==-1) { + if (errno==ENOENT && is_missingok) return EXIT_SUCCESS; + PERROR_Q(ENSC_WRAPPERS_PREFIX "fstat", pathname); + exit(wrapper_exit_code); + } + + memcpy(buf, pathname, l_pathname); + if (l_pathname>0 && pathname[l_pathname-1]!='/') + buf[l_pathname++] = '/'; + + for (i=0; iargc) { - WRITE_MSG(2, "Too much values given; use '--help' for more information\n"); - return EXIT_FAILURE; + return rc; } - if (args.do_set && opt_cnt<=0) { + opt_cnt = getTrueCount(args.handle_opts, DIM_OF(args.handle_opts)); + + if (args.do_set && optind==argc) + WRITE_MSG(2, "No value given; use '--help' for more information\n"); + else if (args.do_set && opt_cnt<=0) WRITE_MSG(2, "No field given which shall be set; use '--help' for more information\n"); - return EXIT_FAILURE; - } - - if (args.do_set && opt_cnt>1) { + else if (args.do_set && opt_cnt>1) WRITE_MSG(2, "Can not set multiple fields; use '--help' for more information\n"); - return EXIT_FAILURE; - } - - if (!args.do_set && optind!=argc) { + else if (!args.do_set && optind!=argc) WRITE_MSG(2, "Can not specifiy a value with '-g'; use '--help' for more information\n"); - return EXIT_FAILURE; - } - - if (args.do_set) args.value = argv[optind]; + else { + if (args.do_set) args.value = argv[optind]; - if (!args.do_set && opt_cnt==0) - for (i=0; i