// $Id: vuname.c 2403 2006-11-24 23:06:08Z dhozac $ --*- c -*-- // Copyright (C) 2004 Enrico Scholz // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifdef HAVE_CONFIG_H # include #endif #include "vserver.h" #include "util.h" #include #include #include #include #include #include #include #include #include #define ENSC_WRAPPERS_PREFIX "vuname: " #define ENSC_WRAPPERS_UNISTD 1 #define ENSC_WRAPPERS_IO 1 #define ENSC_WRAPPERS_VSERVER 1 #include #define CMD_HELP 0x1000 #define CMD_VERSION 0x1001 #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 Tag { bool is_set; char const * value; }; struct Arguments { struct Tag tags[DIM_OF(UTS_MAPPING)]; xid_t xid; bool do_set; char const * dir; bool is_missingok; }; static struct option const CMDLINE_OPTIONS[] = { { "help", no_argument, 0, CMD_HELP }, { "version", no_argument, 0, CMD_VERSION }, { "xid", required_argument, 0, 'x' }, { "set", no_argument, 0, 's' }, { "get", no_argument, 0, 'g' }, { "dir", required_argument, 0, CMD_DIR }, { "missingok", no_argument, 0, CMD_MISSINGOK }, { 0,0,0,0 } }; static void showHelp(int fd, char const *cmd, int res) { VSERVER_DECLARE_CMD(cmd); WRITE_MSG(fd, "Usage: "); WRITE_STR(fd, cmd); WRITE_MSG(fd, " [-g] --xid *\n" " or "); WRITE_STR(fd, cmd); WRITE_MSG(fd, " -s --xid -t = [--] [ *]\n" " or "); WRITE_STR(fd, cmd); WRITE_MSG(fd, " --dir --xid [--missingok] [--] [ *]\n\n" " Options:\n" " -g ... get and print the value\n" " -s ... set the value\n\n" " --xid ... operate on this context; 'self' means the current one\n" " -t =\n" " ... set to ; this option can be repeated multiple time\n" " --dir ... read values from files in . These files must\n" " have a valid TAG as their name\n" " --missingok ... do not fail when the from '--dir' does not exist.\n" "\n" " Possible values for TAG are:\n" " context, sysname, nodename, release, version, machine, domainname\n" "\n" "Please report bugs to " PACKAGE_BUGREPORT "\n"); exit(res); } static void showVersion() { WRITE_MSG(1, "vuname " VERSION " -- modifies and shows uname entries of vserver contexts\n" "This program is part of " PACKAGE_STRING "\n\n" "Copyright (C) 2004 Enrico Scholz\n" VERSION_COPYRIGHT_DISCLAIMER); exit(0); } static void 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; 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; i0 && name[l-1]=='\n') --l; name[l] = '\0'; Eclose(fd); if (vc_set_vhi_name(xid, (vc_uts_type)(i), name, l)==-1) { PERROR_U(ENSC_WRAPPERS_PREFIX "vc_set_vhi_name", UTS_STRINGS[i]); exit(wrapper_exit_code); } } } } static size_t findUtsIdx(char const *str, size_t len) { size_t i; for (i=0; i