setFlags(): implemented it
[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_CAP                 0x2002
40 #define CMD_FLAG                0x2003
41 #define CMD_SECURE              0x2004
42
43 int                     wrapper_exit_code = 1;
44
45 struct option const
46 CMDLINE_OPTIONS[] = {
47   { "help",       no_argument,       0, CMD_HELP },
48   { "version",    no_argument,       0, CMD_VERSION },
49   { "xid",        required_argument, 0, CMD_XID },
50   { "set",        no_argument,       0, CMD_SET },
51   { "cap",        required_argument, 0, CMD_CAP },
52   { "flag",       required_argument, 0, CMD_FLAG },
53   { "secure",     no_argument,       0, CMD_SECURE },
54   {0,0,0,0}
55 };
56
57 struct Arguments {
58     xid_t               xid;
59     struct vc_ctx_flags flags;
60     struct vc_ctx_flags caps;
61 };
62
63 static void
64 showHelp(int fd, char const *cmd, int res)
65 {
66   WRITE_MSG(fd, "Usage:\n    ");
67   WRITE_STR(fd, cmd);
68   WRITE_MSG(fd,
69             " --set [--xid <xid>] [--cap [~!]<cap>] [--flag [~!]<flag>] [--secure] -- [<program> <args>*]\n"
70             "\n"
71             "Please report bugs to " PACKAGE_BUGREPORT "\n");
72
73   exit(res);
74 }
75
76 static void
77 showVersion()
78 {
79   WRITE_MSG(1,
80             "vattribute " VERSION " -- sets attributes of vservers\n"
81             "This program is part of " PACKAGE_STRING "\n\n"
82             "Copyright (C) 2004 Enrico Scholz\n"
83             VERSION_COPYRIGHT_DISCLAIMER);
84   exit(0);
85 }
86
87 static void
88 setFlags(char const UNUSED *str, struct vc_ctx_flags UNUSED * flags)
89 {
90   char const            *err_ptr;
91   size_t                err_len;
92   int                   rc;
93
94   rc = vc_list2flag(str,0, &err_ptr,&err_len, &flags->flagword, &flags->mask);
95   
96   if (rc==-1) {
97     WRITE_MSG(2, "Unknown flag '");
98     write(2, err_ptr, err_len);
99     WRITE_MSG(2, "'\n");
100     exit(wrapper_exit_code);
101   }
102 }
103
104 static void
105 setCaps(char const UNUSED *str, struct vc_ctx_flags UNUSED * caps)
106 {
107 #warning Implement me...
108   abort();
109 }
110
111 static void
112 setSecure(struct vc_ctx_flags UNUSED * flags,
113           struct vc_ctx_flags UNUSED * caps)
114 {
115 #warning Implement me...
116   abort();
117 }
118
119
120 int main(int argc, char *argv[])
121 {
122   struct Arguments              args = {
123     .xid   = VC_NOCTX,
124     .flags = { .flagword = 0, .mask = 0 },
125     .caps  = { .flagword = 0, .mask = 0 },
126   };
127   
128   while (1) {
129     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
130     if (c==-1) break;
131
132     switch (c) {
133       case CMD_HELP     :  showHelp(1, argv[0], 0);
134       case CMD_VERSION  :  showVersion();
135       case CMD_SET      :  break; // default op currently
136       case CMD_XID      :  args.xid = atoi(optarg); break;
137       case CMD_FLAG     :  setFlags(optarg, &args.flags);      break;
138       case CMD_CAP      :  setCaps(optarg,  &args.caps);       break;
139       case CMD_SECURE   :  setSecure(&args.flags, &args.caps); break;
140       default           :
141         WRITE_MSG(2, "Try '");
142         WRITE_STR(2, argv[0]);
143         WRITE_MSG(2, " --help\" for more information.\n");
144         return 255;
145         break;
146     }
147   }
148
149   if (args.xid==VC_NOCTX) args.xid = Evc_get_task_xid(0);
150
151   if (vc_set_flags(args.xid,&args.flags)==-1)
152     perror("vc_set_flags()");
153 //  else if (vc_set_caps(xid, &args.caps)==-1)
154 //    perror("vc_set_caps()");
155   else if (optind<argc)
156     EexecvpD(argv[optind], argv+optind);
157   else
158     return EXIT_SUCCESS;
159
160   return EXIT_FAILURE;
161 }