fixed format-string error
[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 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <sys/resource.h>
32
33 #include "vserver.h"
34
35 static void set_ctxlim (int res, long lim, const char *msg)
36 {
37         if (call_set_ctxlimit(res,lim)==-1){
38                 fprintf (stderr,"Error setting limit \"%s\": %s\n"
39                         ,msg,strerror(errno));
40                 exit (-1);
41         }
42 }
43
44 static void usage()
45 {
46                 fprintf (stderr,"setctxlimit version %s\n",VERSION);
47                 fprintf (stderr
48                         ,"setctxlimit [ --ctx context ] limits\n"
49                          "\n"
50                          "-n nax opened files\n");
51 }
52
53 int main (int argc, char *argv[])
54 {
55         int ret = -1;
56         if (argc < 2){
57                 usage();
58         }else{
59                 int i;
60                 ret = 0;
61                 for (i=1; i<argc; i++){
62                         const char *arg = argv[i];
63                         const char *narg = argv[i+1];
64                         int val;
65                         
66                         if (narg == NULL) narg = "";
67                         val = atoi(narg);
68                         if (strcmp(arg,"--help")==0){
69                                 usage();
70                         }else if (strcmp(arg,"--ctx")==0){
71                                 int tb[1];
72
73                                 tb[0] = val;
74                                 if (call_new_s_context (1,tb,0,0)==-1){
75                                         fprintf (stderr,"Can't select context %d (%s)\n"
76                                                 ,val,strerror(errno));
77                                         exit (-1);
78                                 }
79                         }else if (strcmp (arg,"-n")==0){
80                                 set_ctxlim (RLIMIT_NOFILE,val,"Number of opened files");
81                         }
82                 }
83         }
84         return ret;     
85 }
86