modified showVersion() to show current version instead of an hardcoded
[util-vserver.git] / util-vserver / src / setctxlimit.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on setctxlimit.cc by Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 /*
21         Set the global per context limit of a resource (memory, file handle).
22         This utility can do it either for the current context or a selected
23         one.
24
25         It uses the same options as ulimit, when possible
26 */
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <sys/resource.h>
36
37 #include "vserver.h"
38
39 static void set_ctxlim (int res, long lim, const char *msg)
40 {
41         if (call_set_ctxlimit(res,lim)==-1){
42                 fprintf (stderr,"Error setting limit \"%s\": %s\n"
43                         ,msg,strerror(errno));
44                 exit (-1);
45         }
46 }
47
48 static void usage()
49 {
50                 fprintf (stderr,"setctxlimit version %s\n",VERSION);
51                 fprintf (stderr
52                         ,"setctxlimit [ --ctx context ] limits\n"
53                          "\n"
54                          "-n nax opened files\n");
55 }
56
57 int main (int argc, char *argv[])
58 {
59         int ret = -1;
60         if (argc < 2){
61                 usage();
62         }else{
63                 int i;
64                 ret = 0;
65                 for (i=1; i<argc; i++){
66                         const char *arg = argv[i];
67                         const char *narg = argv[i+1];
68                         int val;
69                         
70                         if (narg == NULL) narg = "";
71                         val = atoi(narg);
72                         if (strcmp(arg,"--help")==0){
73                                 usage();
74                         }else if (strcmp(arg,"--ctx")==0){
75                                 int tb[1];
76
77                                 tb[0] = val;
78                                 if (call_new_s_context (1,tb,0,0)==-1){
79                                         fprintf (stderr,"Can't select context %d (%s)\n"
80                                                 ,val,strerror(errno));
81                                         exit (-1);
82                                 }
83                         }else if (strcmp (arg,"-n")==0){
84                                 set_ctxlim (RLIMIT_NOFILE,val,"Number of opened files");
85                         }
86                 }
87         }
88         return ret;     
89 }
90