minor optimizations
[util-vserver.git] / util-vserver / src / vattribute.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
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.
8 //  
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.
13 //  
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.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24 #include <lib/vserver.h>
25
26 #include <getopt.h>
27 #include <stdint.h>
28 #include <errno.h>
29
30 #define ENSC_WRAPPERS_PREFIX    "vattribute: "
31 #define ENSC_WRAPPERS_VSERVER   1
32 #define ENSC_WRAPPERS_UNISTD    1
33 #include <wrappers.h>
34
35 #define CMD_HELP                0x1000
36 #define CMD_VERSION             0x1001
37 #define CMD_XID                 0x2000
38 #define CMD_SET                 0x2001
39 #define CMD_CCAP                0x2002
40 #define CMD_FLAG                0x2003
41 #define CMD_SECURE              0x2004
42 #define CMD_BCAP                0x2005
43
44 int                     wrapper_exit_code = 1;
45
46 struct option const
47 CMDLINE_OPTIONS[] = {
48   { "help",       no_argument,       0, CMD_HELP },
49   { "version",    no_argument,       0, CMD_VERSION },
50   { "xid",        required_argument, 0, CMD_XID },
51   { "set",        no_argument,       0, CMD_SET },
52   { "ccap",       required_argument, 0, CMD_CCAP },
53   { "bcap",       required_argument, 0, CMD_BCAP },
54   { "flag",       required_argument, 0, CMD_FLAG },
55   { "secure",     no_argument,       0, CMD_SECURE },
56   {0,0,0,0}
57 };
58
59 struct Arguments {
60     xid_t               xid;
61     struct vc_ctx_flags flags;
62     struct vc_ctx_caps  caps;
63 };
64
65 static void
66 showHelp(int fd, char const *cmd, int res)
67 {
68   WRITE_MSG(fd, "Usage:\n    ");
69   WRITE_STR(fd, cmd);
70   WRITE_MSG(fd,
71             " --set [--xid <xid>] [--bcap [~!]<cap>] [--ccap [~!]<cap>] [--flag [~!]<flag>] [--secure] -- [<program> <args>*]\n"
72             "\n"
73             " --bcap <cap>   ...  system  capability to be added\n"
74             " --cap  <cap>   ...  context capability to be added\n"
75             "\n"
76             "Please report bugs to " PACKAGE_BUGREPORT "\n");
77
78   exit(res);
79 }
80
81 static void
82 showVersion()
83 {
84   WRITE_MSG(1,
85             "vattribute " VERSION " -- sets attributes of vservers\n"
86             "This program is part of " PACKAGE_STRING "\n\n"
87             "Copyright (C) 2004 Enrico Scholz\n"
88             VERSION_COPYRIGHT_DISCLAIMER);
89   exit(0);
90 }
91
92 static void
93 parseFlags(char const *str, struct vc_ctx_flags *flags)
94 {
95   struct vc_err_listparser      err;
96   int                           rc;
97
98   rc = vc_list2cflag(str,0, &err, flags);
99   
100   if (rc==-1) {
101     WRITE_MSG(2, "Unknown flag '");
102     write(2, err.ptr, err.len);
103     WRITE_MSG(2, "'\n");
104     exit(wrapper_exit_code);
105   }
106 }
107
108 static void
109 parseBCaps(char const *str, struct vc_ctx_caps *caps)
110 {
111   struct vc_err_listparser      err;
112   int                           rc;
113
114   rc = vc_list2bcap(str,0, &err, caps);
115   
116   if (rc==-1) {
117     WRITE_MSG(2, "Unknown bcap '");
118     write(2, err.ptr, err.len);
119     WRITE_MSG(2, "'\n");
120     exit(wrapper_exit_code);
121   }
122 }
123
124 static void
125 parseCCaps(char const *str, struct vc_ctx_caps *caps)
126 {
127   struct vc_err_listparser      err;
128   int                           rc;
129
130   rc = vc_list2ccap(str,0, &err, caps);
131   
132   if (rc==-1) {
133     WRITE_MSG(2, "Unknown ccap '");
134     write(2, err.ptr, err.len);
135     WRITE_MSG(2, "'\n");
136     exit(wrapper_exit_code);
137   }
138 }
139
140 static void
141 parseSecure(struct vc_ctx_flags UNUSED * flags,
142             struct vc_ctx_caps  UNUSED * caps)
143 {
144   caps->ccaps = ~vc_get_insecureccaps();
145   caps->cmask = ~0ull;
146   caps->bcaps = ~vc_get_insecurebcaps();
147   caps->bmask = ~0ull;
148 }
149
150 int main(int argc, char *argv[])
151 {
152   struct Arguments              args = {
153     .xid   = VC_NOCTX,
154     .flags = { .flagword = 0, .mask = 0 },
155     .caps  = { .bcaps = 0, .bmask = 0,.ccaps = 0, .cmask = 0 },
156   };
157   
158   while (1) {
159     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
160     if (c==-1) break;
161
162     switch (c) {
163       case CMD_HELP     :  showHelp(1, argv[0], 0);
164       case CMD_VERSION  :  showVersion();
165       case CMD_SET      :  break; // default op currently
166       case CMD_XID      :  args.xid = Evc_xidopt2xid(optarg,true); break;
167       case CMD_FLAG     :  parseFlags(optarg, &args.flags);        break;
168       case CMD_CCAP     :  parseCCaps(optarg, &args.caps);         break;
169       case CMD_BCAP     :  parseBCaps(optarg, &args.caps);         break;
170       case CMD_SECURE   :  parseSecure(&args.flags, &args.caps);   break;
171       default           :
172         WRITE_MSG(2, "Try '");
173         WRITE_STR(2, argv[0]);
174         WRITE_MSG(2, " --help\" for more information.\n");
175         return 255;
176         break;
177     }
178   }
179
180   if (args.xid==VC_NOCTX) args.xid = Evc_get_task_xid(0);
181
182   if ((args.caps.cmask || args.caps.bmask) &&
183       vc_set_ccaps(args.xid, &args.caps)==-1)
184     perror(ENSC_WRAPPERS_PREFIX "vc_set_ccaps()");
185   else if (args.flags.mask &&
186            vc_set_cflags(args.xid, &args.flags)==-1)
187     perror(ENSC_WRAPPERS_PREFIX "vc_set_flags()");
188   else if (optind<argc)
189     EexecvpD(argv[optind], argv+optind);
190   else
191     return EXIT_SUCCESS;
192
193   return EXIT_FAILURE;
194 }