updated to new API
[util-vserver.git] / util-vserver / src / vsched.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 "vserver.h"
25
26 #include <errno.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <libgen.h>
30
31 #define ENSC_WRAPPERS_PREFIX    "vsched: "
32 #define ENSC_WRAPPERS_VSERVER   1
33 #define ENSC_WRAPPERS_UNISTD    1
34 #include <wrappers.h>
35
36 #define CMD_HELP                0x1000
37 #define CMD_VERSION             0x1001
38 #define CMD_XID                 0x4000
39 #define CMD_FRATE               0x4001
40 #define CMD_INTERVAL            0x4002
41 #define CMD_TOKENS              0x4003
42 #define CMD_TOK_MIN             0x4004
43 #define CMD_TOK_MAX             0x4005
44 #define CMD_CPU_MASK            0x4006
45
46 int                     wrapper_exit_code = 255;
47
48 struct option const
49 CMDLINE_OPTIONS[] = {
50   { "help",     no_argument,  0, CMD_HELP },
51   { "version",  no_argument,  0, CMD_VERSION },
52   { "ctx",         required_argument, 0, CMD_XID },
53   { "fill-rate",   required_argument, 0, CMD_FRATE },
54   { "interval",    required_argument, 0, CMD_INTERVAL },
55   { "tokens",      required_argument, 0, CMD_TOKENS },
56   { "tokens_min",  required_argument, 0, CMD_TOK_MIN },
57   { "tokens_max",  required_argument, 0, CMD_TOK_MAX },
58   { "cpu_mask",    required_argument, 0, CMD_CPU_MASK },
59   {0,0,0,0}
60 };
61
62 static void
63 showHelp(int fd, char const *cmd, int res)
64 {
65   VSERVER_DECLARE_CMD(cmd);
66
67   WRITE_MSG(fd, "Usage:\n  ");
68   WRITE_STR(fd, cmd);
69   WRITE_MSG(fd,
70             " [--ctx <xid>] [--fill-rate <rate>] [--interval <interval>] [--tokens <tokens>] [--tokens_min <tokens>] [--tokens_max <tokens>] [--cpu_mask <mask>] [--] [<command> <args>*]\n"
71             "\n"
72             "Please report bugs to " PACKAGE_BUGREPORT "\n");
73
74   exit(res);
75 }
76
77 static void
78 showVersion()
79 {
80   WRITE_MSG(1,
81             "vsched " VERSION " -- modifies scheduling parameters\n"
82             "This program is part of " PACKAGE_STRING "\n\n"
83             "Copyright (C) 2003,2004 Enrico Scholz\n"
84             VERSION_COPYRIGHT_DISCLAIMER);
85   exit(0);
86 }
87
88 int main(int argc, char *argv[])
89 {
90   xid_t                 xid   = VC_NOCTX;
91   struct vc_set_sched   sched = { 0,0,0,0,0,0 };
92   
93   while (1) {
94     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
95     if (c==-1) break;
96
97     switch (c) {
98       case CMD_HELP     :  showHelp(1, argv[0], 0);
99       case CMD_VERSION  :  showVersion();
100       case CMD_XID      :  xid = atoi(optarg); break;
101       case CMD_FRATE    :  sched.fill_rate   = atoi(optarg); break;
102       case CMD_INTERVAL :  sched.interval    = atoi(optarg); break;
103       case CMD_TOKENS   :  sched.tokens      = atoi(optarg); break;
104       case CMD_TOK_MIN  :  sched.tokens_min  = atoi(optarg); break;
105       case CMD_TOK_MAX  :  sched.tokens_max  = atoi(optarg); break;
106       case CMD_CPU_MASK :  sched.cpu_mask    = atoi(optarg); break;
107       default           :
108         WRITE_MSG(2, "Try '");
109         WRITE_STR(2, argv[0]);
110         WRITE_MSG(2, " --help\" for more information.\n");
111         return EXIT_FAILURE;
112         break;
113     }
114   }
115
116   if (xid==VC_NOCTX && optind==argc) {
117     WRITE_MSG(2, "Neither '--xid' nor a program was specified; try '--help' for more information\n");
118     exit(255);
119   }
120
121   if (xid==VC_NOCTX)
122     xid = Evc_get_task_xid(0);
123
124   if (vc_set_sched(xid, &sched)==-1) {
125     perror("vc_set_sched()");
126     exit(255);
127   }
128
129   EexecvpD(argv[optind],argv+optind);
130 }