vdlimit is a replacement for the prior tools used to set disklimits.
[util-vserver.git] / src / vdlimit.c
1
2 /*
3 ** (c) 2004 Herbert Poetzl
4 **
5 ** V0.01        ioctls so far
6 **
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #  include <config.h>
11 #endif
12 #include "compat.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdint.h>
24
25 #include "vserver.h"
26 #include "vserver-internal.h"
27 #include "dlimit.h"
28  
29 #ifdef O_LARGEFILE
30 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
31 #else
32 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
33 #endif
34
35 static  char    err_flag = 0;
36
37 static  char    opt_xid = 0;
38 static  char    opt_flag = 0;
39 static  char    opt_vers = 0;
40 static  char    opt_add = 0;
41 static  char    opt_rem = 0;
42 static  char    opt_set = 0;
43
44 static  char *  cmd_name = NULL;
45 static  char *  set_arg = NULL;
46
47 static  int     num_xid = 0;
48 static  int     num_flag = 0;
49
50
51
52
53 #define OPTIONS "+hadf:x:S:V"
54
55 int     main(int argc, char *argv[])
56 {
57         extern int optind;
58         extern char *optarg;
59         char c, errflg = 0;
60         int r;
61
62
63         cmd_name = argv[0];
64         while ((c = getopt(argc, argv, OPTIONS)) != EOF) {
65             switch (c) {
66             case 'h':
67                 fprintf(stderr,
68                     "This is %s " VERSION "\n"
69                     "options are:\n"
70                     "-h        print this help message\n"
71                     "-a        add dlimit entry\n"
72                     "-d        delete dlimit entry\n"
73                     "-f <num>  flag value in decimal\n"
74                     "-x <num>  context id\n"
75                     "-S <vals> current/limit values\n"
76                     "-V        verify interface version\n"
77                     "--        end of options\n"
78                     ,cmd_name);
79                 exit(0);
80                 break;
81             case 'a':
82                 opt_add = 1;
83                 break;
84             case 'd':
85                 opt_rem = 1;
86                 break;
87             case 'f':
88                 num_flag = atol(optarg);
89                 opt_flag = 1;
90                 break;
91             case 'x':
92                 num_xid = atol(optarg);
93                 opt_xid = 1;
94                 break;
95             case 'S':
96                 set_arg = optarg;
97                 opt_set = 1;
98                 break;
99             case 'V':
100                 opt_vers = 1;
101                 break;
102             case '?':
103             default:
104                 errflg++;
105                 break;
106             }
107         }
108         if (errflg) {
109             fprintf(stderr, 
110                 "Usage: %s -[" OPTIONS "] <path> ...\n"
111                 "%s -h for help.\n",
112                 cmd_name, cmd_name);
113             exit(2);
114         }
115
116         if (opt_vers) {
117             r = vc_get_version();
118             if (r<0)
119                 perror("vc_get_version");
120             else 
121                 printf("version: %04x:%04x\n", 
122                     (r>>16)&0xFFFF, r&0xFFFF);
123         }
124
125         for (; optind < argc; optind++) {
126             struct vcmd_ctx_dlimit_base_v0 init;
127             struct vcmd_ctx_dlimit_v0 data;
128
129             init.name = argv[optind];
130             init.flags = num_flag;
131
132             if (opt_rem) {
133                 r = vserver(VCMD_rem_dlimit, num_xid, &init);
134                 if (r<0)
135                     perror("vc_rem_dlimit");
136             } 
137             if (opt_add) {
138                 r = vserver(VCMD_add_dlimit, num_xid, &init);
139                 if (r<0)
140                     perror("vc_add_dlimit");
141             }
142                     
143             memset(&data, 0, sizeof(data));
144             data.name = argv[optind];
145             data.flags = num_flag;
146
147             if (opt_set) {
148                 sscanf(set_arg, "%u,%u,%u,%u,%u",
149                     &data.space_used, &data.space_total,
150                     &data.inodes_used, &data.inodes_total,
151                     &data.reserved);
152
153                 r = vserver(VCMD_set_dlimit, num_xid, &data);
154                 if (r<0)
155                     perror("vc_set_dlimit");
156             }
157
158             memset(&data, 0, sizeof(data));
159             data.name = argv[optind];
160             data.flags = num_flag;
161
162             r = vserver(VCMD_get_dlimit, num_xid, &data);
163             if (r<0)
164                 perror("vc_get_dlimit");
165
166             printf("%s: %u,%u,%u,%u,%u\n", argv[optind],
167                 data.space_used, data.space_total,
168                 data.inodes_used, data.inodes_total,
169                 data.reserved);
170         }
171         
172         exit((err_flag)?1:0);
173 }
174