use the specialized utilvserver_fmt_xuint64() function
[util-vserver.git] / util-vserver / src / vlimit.c
1 // $Id$
2
3 // Copyright (C) 2003 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; either version 2, or (at your option)
8 // any later version.
9 //  
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //  
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 /*
20         Set the global per context limit of a resource (memory, file handle).
21         This utility can do it either for the current context or a selected
22         one.
23
24         It uses the same options as ulimit, when possible
25 */
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29 #include "compat.h"
30
31 #include "vserver.h"
32 #include "internal.h"
33
34 #include <getopt.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #define VERSION_COPYRIGHT_DISCLAIMER
43
44 inline static void UNUSED
45 writeStr(int fd, char const *cmd)
46 {
47   (void)write(fd, cmd, strlen(cmd));
48 }
49
50 #define WRITE_MSG(FD,X)         (void)(write(FD,X,sizeof(X)-1))
51 #define WRITE_STR(FD,X)         writeStr(FD,X)
52
53 #define NUMLIM(X) \
54 { #X, required_argument, 0, 2048|X }
55
56 static struct option const
57 CMDLINE_OPTIONS[] = {
58   { "help",     no_argument,  0, 'h' },
59   { "version",  no_argument,  0, 'v' },
60   { "all",      no_argument,  0, 'a' },
61   NUMLIM( 0), NUMLIM( 1), NUMLIM( 2), NUMLIM( 3),
62   NUMLIM( 4), NUMLIM( 5), NUMLIM( 6), NUMLIM( 7),
63   NUMLIM( 8), NUMLIM( 9), NUMLIM(10), NUMLIM(11),
64   NUMLIM(12), NUMLIM(13), NUMLIM(14), NUMLIM(15),
65   NUMLIM(16), NUMLIM(17), NUMLIM(18), NUMLIM(19),
66   NUMLIM(20), NUMLIM(21), NUMLIM(22), NUMLIM(23),
67   NUMLIM(24), NUMLIM(25), NUMLIM(26), NUMLIM(27),
68   NUMLIM(28), NUMLIM(29), NUMLIM(30), NUMLIM(31),
69   { 0,0,0,0 }
70 };
71
72 static void
73 showHelp(int fd, char const *cmd, int res)
74 {
75   WRITE_MSG(fd, "Usage:  ");
76   WRITE_STR(fd, cmd);
77   WRITE_MSG(fd,
78             " [-c|--ctx <ctx>] [-a|--all] [-MSH  --<nr> <value>]*\n"
79             "Please report bugs to " PACKAGE_BUGREPORT "\n");
80   exit(res);
81 }
82
83 static void
84 showVersion()
85 {
86   WRITE_MSG(1,
87             "vlimit " VERSION " -- limits context-resources\n"
88             "This program is part of " PACKAGE_STRING "\n\n"
89             "Copyright (C) 2003 Enrico Scholz\n"
90             VERSION_COPYRIGHT_DISCLAIMER);
91   exit(0);
92 }
93
94 static void *
95 appendLimit(char *ptr, bool do_it, vc_limit_t lim)
96 {
97   memcpy(ptr, "  ", 2);
98   ptr += 2;
99   if (do_it) {
100     if (lim==VC_LIM_INFINITY) {
101       strcpy(ptr, "INF");
102       ptr += 3;
103     }
104     else {
105       memcpy(ptr, "0x", 2); ptr += 2;
106       
107       ptr += utilvserver_fmt_xuint64(ptr, lim);
108       *ptr = ' ';
109     }
110   }
111   else {
112     memcpy(ptr, "N/A", 3);
113     ptr += 3;
114   }
115
116   return ptr;
117 }
118
119 static void
120 showAll(int ctx)
121 {
122   struct vc_rlimit_mask mask;
123   size_t                i;
124
125   if (vc_get_rlimit_mask(-2, &mask)==-1) {
126     perror("vc_get_rlimit_mask()");
127     //exit(1);
128   }
129
130   for (i=0; i<32; ++i) {
131     uint32_t            bitmask = (1<<i);
132     struct vc_rlimit    limit;
133     char                buf[100], *ptr=buf;
134     
135     if (vc_get_rlimit(ctx, i, &limit)==-1) {
136       perror("vc_get_rlimit()");
137       //continue;
138     }
139
140     memset(buf, ' ', sizeof buf);
141     ptr += utilvserver_fmt_uint(ptr, i);
142     *ptr = ' ';
143
144     ptr  = appendLimit(buf+10, mask.min &bitmask, limit.min);
145     ptr  = appendLimit(buf+30, mask.soft&bitmask, limit.soft);
146     ptr  = appendLimit(buf+50, mask.hard&bitmask, limit.hard);
147
148     *ptr++ = '\n';
149     write(1, buf, ptr-buf);
150  }
151 }
152
153 static void
154 setLimits(int ctx, struct vc_rlimit const limits[], uint32_t mask)
155 {
156   size_t                i;
157   for (i=0; i<32; ++i) {
158     if ((mask & (1<<i))==0) continue;
159     if (vc_set_rlimit(ctx, i, limits+i)) {
160       perror("vc_set_rlimit()");
161     }
162   }
163 }
164
165 int main (int argc, char *argv[])
166 {
167   // overall used limits
168   uint32_t              lim_mask = 0;
169   int                   set_mask = 0;
170   struct vc_rlimit      limits[32];
171   bool                  show_all = false;
172   ctx_t                 ctx      = VC_SAMECTX;
173
174   {
175     size_t              i;
176     for (i=0; i<32; ++i) {
177       limits[i].min  = VC_LIM_KEEP;
178       limits[i].soft = VC_LIM_KEEP;
179       limits[i].hard = VC_LIM_KEEP;
180     }
181   }
182   
183   while (1) {
184     int         c = getopt_long(argc, argv, "MSHhvac:", CMDLINE_OPTIONS, 0);
185     if (c==-1) break;
186
187     if (2048<=c && c<2048+32) {
188       int               id  = c-2048;
189       vc_limit_t        val;
190       
191       if (strcmp(optarg, "inf")==0) val = VC_LIM_INFINITY;
192       else                          val = atoll(optarg);
193
194       if (set_mask & 1) limits[id].min  = val;
195       if (set_mask & 2) limits[id].soft = val;
196       if (set_mask & 4) limits[id].soft = val;
197
198       lim_mask |= (1<<id);
199       set_mask  = 0;
200     }
201     else switch (c) {
202       case 'h'          :  showHelp(1, argv[0], 0);
203       case 'v'          :  showVersion();
204       case 'c'          :  ctx      = atoi(optarg); break;
205       case 'a'          :  show_all = true;         break;
206       case 'M'          :
207       case 'S'          :
208       case 'H'          :
209         switch (c) {
210           case 'M'      :  set_mask |= 1; break;
211           case 'S'      :  set_mask |= 2; break;
212           case 'H'      :  set_mask |= 4; break;
213           default       :  assert(false);
214         }
215         break;
216         
217       default           :
218         WRITE_MSG(2, "Try '");
219         WRITE_STR(2, argv[0]);
220         WRITE_MSG(2, " --help\" for more information.\n");
221         return EXIT_FAILURE;
222         break;
223     }
224   }
225
226   setLimits(ctx, limits, lim_mask);
227   if (show_all) showAll(ctx);
228
229   return EXIT_SUCCESS;
230 }