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