From ee408ef2c5e17764acbc2ed2351c5c95db39abae Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Sat, 1 Mar 2008 01:07:49 +0000 Subject: [PATCH] Factor out bitfield-to-string code to src/attribute-util.h. Add --get for nattribute too. git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2694 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- src/attribute-util.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/nattribute.c | 61 ++++++++++++++++++++++++++++++++++++------------ src/vattribute.c | 48 ++++---------------------------------- 3 files changed, 115 insertions(+), 59 deletions(-) create mode 100644 src/attribute-util.h diff --git a/src/attribute-util.h b/src/attribute-util.h new file mode 100644 index 0000000..5f11938 --- /dev/null +++ b/src/attribute-util.h @@ -0,0 +1,65 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2007-2008 Daniel Hokka Zakrisson +// +// 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. + +#ifndef _ATTRIBUTE_UTIL_H +#define _ATTRIBUTE_UTIL_H + +static inline int +ffsull(unsigned long long word) +{ + int bit; + for (bit = 0; bit < 64; bit++) { + if (word & (1ULL << bit)) + break; + } + if (bit == 64) + bit = 0; + return bit; +} + +#define print_bitfield(fd, type, name, var) \ + do { \ + int first; \ + WRITE_MSG(fd, name ":\n"); \ + first = 1; \ + while (1) { \ + char const *i; \ + i = vc_lo ## type ## 2text(var); \ + if (!i) \ + break; \ + if (!first) \ + WRITE_MSG(fd, ","); \ + else \ + first = 0; \ + WRITE_STR(fd, i); \ + } \ + while (*(var)) { \ + int bit = ffsull(*(var)); \ + if (!bit) \ + break; \ + if (!first) \ + WRITE_MSG(fd, ","); \ + else \ + first = 0; \ + WRITE_MSG(fd, "^"); \ + WRITE_INT(fd, bit); \ + *(var) &= ~(1ULL << bit); \ + } \ + WRITE_MSG(fd, "\n"); \ + } while (0) + +#endif diff --git a/src/nattribute.c b/src/nattribute.c index f4ea7a1..ed10bad 100644 --- a/src/nattribute.c +++ b/src/nattribute.c @@ -22,6 +22,7 @@ #endif #include "util.h" +#include "attribute-util.h" #include #include @@ -40,6 +41,7 @@ #define CMD_CAP 0x2002 #define CMD_FLAG 0x2003 #define CMD_SECURE 0x2004 +#define CMD_GET 0x2005 int wrapper_exit_code = 1; @@ -52,13 +54,15 @@ CMDLINE_OPTIONS[] = { { "ncap", required_argument, 0, CMD_CAP }, { "flag", required_argument, 0, CMD_FLAG }, { "secure", no_argument, 0, CMD_SECURE }, + { "get", no_argument, 0, CMD_GET }, {0,0,0,0} }; struct Arguments { + int mode; nid_t nid; - struct vc_net_flags flags; - struct vc_net_caps caps; + struct vc_net_flags flags; + struct vc_net_caps caps; }; static void @@ -67,7 +71,8 @@ showHelp(int fd, char const *cmd, int res) WRITE_MSG(fd, "Usage:\n "); WRITE_STR(fd, cmd); WRITE_MSG(fd, - " --set [--nid ] [--ncap [~!]] [--flag [~!]] [--secure] -- [ *]\n" + " {--set|--get} [--nid ] [--ncap [~!]] [--flag [~!]] [--secure] --\n" + " [ *]\n" "\n" " --ncap ... network capability to be added\n" " --flag ... network flag to be added\n" @@ -81,7 +86,7 @@ static void showVersion() { WRITE_MSG(1, - "nattribute " VERSION " -- sets attributes of network contexts\n" + "nattribute " VERSION " -- sets/gets attributes of network contexts\n" "This program is part of " PACKAGE_STRING "\n\n" "Copyright (C) 2004 Enrico Scholz\n" "Copyright (C) 2006 Daniel Hokka Zakrisson\n" @@ -133,9 +138,25 @@ parseSecure(struct vc_net_flags * flags, flags->mask = VC_NXF_HIDE_NETIF; } +static int +printAttrs(struct Arguments *args) +{ + struct vc_net_flags flags; + struct vc_net_caps caps; + + Evc_get_nflags(args->nid, &flags); + Evc_get_ncaps(args->nid, &caps); + + print_bitfield(1, ncap, "ncapabilities", &caps.ncaps); + print_bitfield(1, nflag, "nflags", &flags.flagword); + + return 0; +} + int main(int argc, char *argv[]) { struct Arguments args = { + .mode = CMD_SET, .nid = VC_NOCTX, .flags = { .flagword = 0, .mask = 0 }, .caps = { .ncaps = 0, .cmask = 0 }, @@ -148,7 +169,8 @@ int main(int argc, char *argv[]) switch (c) { case CMD_HELP : showHelp(1, argv[0], 0); case CMD_VERSION : showVersion(); - case CMD_SET : break; // default op currently + case CMD_SET : args.mode = CMD_SET; break; + case CMD_GET : args.mode = CMD_GET; break; case CMD_NID : args.nid = Evc_nidopt2nid(optarg,true); break; case CMD_FLAG : parseFlags(optarg, &args.flags); break; case CMD_CAP : parseNCaps(optarg, &args.caps); break; @@ -164,16 +186,25 @@ int main(int argc, char *argv[]) if (args.nid==VC_NOCTX) args.nid = Evc_get_task_nid(0); - if (args.caps.cmask && - vc_set_ncaps(args.nid, &args.caps)==-1) - perror(ENSC_WRAPPERS_PREFIX "vc_set_ncaps()"); - else if (args.flags.mask && - vc_set_nflags(args.nid, &args.flags)==-1) - perror(ENSC_WRAPPERS_PREFIX "vc_set_nflags()"); - else if (optind #include @@ -156,19 +157,6 @@ parseSecure(struct vc_ctx_flags UNUSED * flags, flags->mask = VC_VXF_HIDE_NETIF; } -static inline int -ffsull(unsigned long long word) -{ - int bit; - for (bit = 0; bit < 64; bit++) { - if (word & (1ULL << bit)) - break; - } - if (bit == 64) - bit = 0; - return bit; -} - static int printAttrs(struct Arguments *args) { @@ -179,37 +167,9 @@ printAttrs(struct Arguments *args) Evc_get_cflags(args->xid, &flags); Evc_get_ccaps(args->xid, &caps); -#define PRINT_VALUES(type, name, var) \ - WRITE_MSG(1, name ":\n"); \ - first = 1; \ - while (1) { \ - char const *i; \ - i = vc_lo ## type ## 2text(var); \ - if (!i) \ - break; \ - if (!first) \ - WRITE_MSG(1, ","); \ - else \ - first = 0; \ - WRITE_STR(1, i); \ - } \ - while (*(var)) { \ - int bit = ffsull(*(var)); \ - if (!bit) \ - break; \ - if (!first) \ - WRITE_MSG(1, ","); \ - else \ - first = 0; \ - WRITE_MSG(1, "^"); \ - WRITE_INT(1, bit); \ - *(var) &= ~(1ULL << bit); \ - } \ - WRITE_MSG(1, "\n"); - - PRINT_VALUES(bcap, "bcapabilities", &caps.bcaps); - PRINT_VALUES(ccap, "ccapabilities", &caps.ccaps); - PRINT_VALUES(cflag, "flags", &flags.flagword); + print_bitfield(1, bcap, "bcapabilities", &caps.bcaps); + print_bitfield(1, ccap, "ccapabilities", &caps.ccaps); + print_bitfield(1, cflag, "flags", &flags.flagword); return 0; } -- 1.8.1.5