added support for new migrate feature
[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_PERIOD              0x4002
41 #define CMD_FLEVEL              0x4003
42 #define CMD_BSIZE               0x4004
43
44 int                     wrapper_exit_code = 255;
45
46 struct option const
47 CMDLINE_OPTIONS[] = {
48   { "help",     no_argument,  0, CMD_HELP },
49   { "version",  no_argument,  0, CMD_VERSION },
50   { "ctx",         required_argument, 0, CMD_XID },
51   { "fill-rate",   required_argument, 0, CMD_FRATE },
52   { "period",      required_argument, 0, CMD_PERIOD },
53   { "fill-level",  required_argument, 0, CMD_FLEVEL },
54   { "bucket-size", required_argument, 0, CMD_BSIZE },
55   {0,0,0,0}
56 };
57
58 static void
59 showHelp(int fd, char const *cmd, int res)
60 {
61   VSERVER_DECLARE_CMD(cmd);
62
63   WRITE_MSG(fd, "Usage:\n  ");
64   WRITE_STR(fd, cmd);
65   WRITE_MSG(fd,
66             " [--ctx <xid>] [--fill-rate <rate>] [--period <period>] [--fill-level <level>] [--bucket-size <size>] [--] [<command> <args>*]\n"
67             "\n"
68             "Please report bugs to " PACKAGE_BUGREPORT "\n");
69
70   exit(res);
71 }
72
73 static void
74 showVersion()
75 {
76   WRITE_MSG(1,
77             "vsched " VERSION " -- modifies scheduling parameters\n"
78             "This program is part of " PACKAGE_STRING "\n\n"
79             "Copyright (C) 2003,2004 Enrico Scholz\n"
80             VERSION_COPYRIGHT_DISCLAIMER);
81   exit(0);
82 }
83
84 int main(int argc, char *argv[])
85 {
86   xid_t                 xid   = VC_NOCTX;
87   struct vc_set_sched   sched = { 0,0,0,0 };
88   
89   while (1) {
90     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
91     if (c==-1) break;
92
93     switch (c) {
94       case CMD_HELP     :  showHelp(1, argv[0], 0);
95       case CMD_VERSION  :  showVersion();
96       case CMD_XID      :  xid = atoi(optarg); break;
97       case CMD_FRATE    :  sched.fill_rate   = atoi(optarg); break;
98       case CMD_PERIOD   :  sched.period      = atoi(optarg); break;
99       case CMD_FLEVEL   :  sched.fill_level  = atoi(optarg); break;
100       case CMD_BSIZE    :  sched.bucket_size = atoi(optarg); break;
101       default           :
102         WRITE_MSG(2, "Try '");
103         WRITE_STR(2, argv[0]);
104         WRITE_MSG(2, " --help\" for more information.\n");
105         return EXIT_FAILURE;
106         break;
107     }
108   }
109
110   if (xid==VC_NOCTX && optind==argc) {
111     WRITE_MSG(2, "Neither '--xid' nor a program was specified; try '--help' for more information\n");
112     exit(255);
113   }
114
115   if (xid==VC_NOCTX)
116     xid = Evc_get_task_xid(0);
117
118   if (vc_set_sched(xid, &sched)==-1) {
119     perror("vc_set_sched()");
120     exit(255);
121   }
122
123   EexecvpD(argv[optind],argv+optind);
124 }