3 // Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <lib/internal.h>
34 #define ENSC_WRAPPERS_PREFIX "vdlimit: "
35 #define ENSC_WRAPPERS_UNISTD 1
36 #define ENSC_WRAPPERS_VSERVER 1
39 #define CMD_HELP 0x1000
40 #define CMD_VERSION 0x1001
42 int wrapper_exit_code = 1;
46 { "help", no_argument, 0, CMD_HELP },
47 { "version", no_argument, 0, CMD_VERSION },
48 { "xid", required_argument, 0, 'x' },
49 { "set", required_argument, 0, 's' },
50 { "remove", no_argument, 0, 'd' },
51 { "flags", required_argument, 0, 'f' },
56 showHelp(int fd, char const *cmd)
58 WRITE_MSG(fd, "Usage: ");
61 " --xid <xid> [--flags <flags>] (--set <limit>=<value>|--remove) <mount point>\n"
63 " --set|-s <limit>=<value> ... set <limit> to <value>, where limit is \n"
64 " one of: space_used, space_total, inodes_used,\n"
65 " inodes_total, reserved\n"
66 " --remove|-d ... removes the disk limit for <xid> from <mount point>\n"
68 "Please report bugs to " PACKAGE_BUGREPORT "\n");
77 "vdlimit " VERSION " -- manages disk limits\n"
78 "This program is part of " PACKAGE_STRING "\n\n"
79 "Copyright (C) 2005 Enrico Scholz\n"
80 VERSION_COPYRIGHT_DISCLAIMER);
85 setDlimit(char const *filename, xid_t xid, uint32_t flags, struct vc_ctx_dlimit const *limit)
87 bool was_added = false;
89 if (vc_get_dlimit(filename, xid, flags, 0) == -1) {
90 if (vc_add_dlimit(filename, xid, flags) == -1) {
91 perror(ENSC_WRAPPERS_PREFIX "vc_add_dlimit()");
92 exit(wrapper_exit_code);
98 if (vc_set_dlimit(filename, xid, flags, limit) == -1) {
99 perror(ENSC_WRAPPERS_PREFIX "vc_set_dlimit()");
102 vc_rem_dlimit(filename, xid, flags)==-1)
103 perror(ENSC_WRAPPERS_PREFIX "vc_rem_dlimit()");
105 exit(wrapper_exit_code);
110 remDlimit(char const *filename, xid_t xid, uint32_t flags)
112 if (vc_rem_dlimit(filename, xid, flags) == -1) {
113 perror(ENSC_WRAPPERS_PREFIX "vc_rem_dlimit()");
114 exit(wrapper_exit_code);
119 writeInt(int fd, char const *prefix, unsigned int val)
121 char buf[sizeof(val)*3 + 2];
122 size_t len = utilvserver_fmt_uint(buf, val);
125 WRITE_STR(fd, prefix);
126 if (val == VC_CDLIM_INFINITY)
129 Vwrite(fd, buf, len);
133 printDlimit(char const *filename, xid_t xid, uint32_t flags, bool formatted)
135 struct vc_ctx_dlimit limit;
137 if (vc_get_dlimit(filename, xid, flags, &limit) == -1) {
138 perror(ENSC_WRAPPERS_PREFIX "vc_get_dlimit()");
139 exit(wrapper_exit_code);
143 writeInt (1, 0, xid);
145 WRITE_STR(1, filename);
146 writeInt (1, "\nspace_used=", limit.space_used);
147 writeInt (1, "\nspace_total=", limit.space_total);
148 writeInt (1, "\ninodes_used=", limit.inodes_used);
149 writeInt (1, "\ninodes_total=", limit.inodes_total);
150 writeInt (1, "\nreserved=", limit.reserved);
154 writeInt (1, 0, xid);
155 writeInt (1, " ", limit.space_used);
156 writeInt (1, " ", limit.space_total);
157 writeInt (1, " ", limit.inodes_used);
158 writeInt (1, " ", limit.inodes_total);
159 writeInt (1, " ", limit.reserved);
161 WRITE_STR(1, filename);
168 setDLimitField(struct vc_ctx_dlimit *dst, char const *opt)
171 char const * const orig_opt = opt;
173 #define GET_VAL_PTR(CMP, VAL) \
174 (strncmp(opt, CMP "=", sizeof(CMP))==0) ? \
175 (opt+=sizeof(CMP), &VAL) : 0
177 if ((ptr=GET_VAL_PTR("space_used", dst->space_used))!=0) {}
178 else if ((ptr=GET_VAL_PTR("space_total", dst->space_total))!=0) {}
179 else if ((ptr=GET_VAL_PTR("inodes_used", dst->inodes_used))!=0) {}
180 else if ((ptr=GET_VAL_PTR("inodes_total", dst->inodes_total))!=0) {}
181 else if ((ptr=GET_VAL_PTR("reserved", dst->reserved))!=0) {}
186 if (ptr!=0 && *ptr==VC_CDLIM_KEEP) {
188 long val = strtol(opt, &endptr, 0);
190 if (*opt==0 || *endptr!='\0') {
191 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not parse number in '");
192 WRITE_STR(2, orig_opt);
200 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "value already set in '");
201 WRITE_STR(2, orig_opt);
206 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "unknown limit in '");
207 WRITE_STR(2, orig_opt);
216 isHigherLimit(uint_least32_t lhs, uint_least32_t rhs)
218 if (lhs==VC_CDLIM_KEEP || rhs==VC_CDLIM_KEEP) return false;
223 int main(int argc, char *argv[])
226 bool do_remove = false;
227 xid_t xid = VC_NOCTX;
232 struct vc_ctx_dlimit limit = {
233 .space_used = VC_CDLIM_KEEP,
234 .space_total = VC_CDLIM_KEEP,
235 .inodes_used = VC_CDLIM_KEEP,
236 .inodes_total = VC_CDLIM_KEEP,
237 .reserved = VC_CDLIM_KEEP
241 int c = getopt_long(argc, argv, "+x:s:df:", CMDLINE_OPTIONS, 0);
245 case CMD_HELP : showHelp(1, argv[0]);
246 case CMD_VERSION : showVersion();
247 case 'x' : xid = Evc_xidopt2xid(optarg, true); break;
249 if (!setDLimitField(&limit, optarg))
254 case 'd' : do_remove = true; break;
257 flags = strtol(optarg, &endptr, 0);
258 if ((flags == 0 && errno != 0) || *endptr != '\0') {
259 WRITE_MSG(2, "Invalid flags argument: '");
260 WRITE_STR(2, optarg);
261 WRITE_MSG(2, "'; try '--help' for more information\n");
268 WRITE_MSG(2, "Try '");
269 WRITE_STR(2, argv[0]);
270 WRITE_MSG(2, " --help' for more information.\n");
276 sum = ((do_set ? 1 : 0) + (do_remove ? 1 : 0));
279 WRITE_MSG(2, "Can not specify multiple operations; try '--help' for more information\n");
280 else if (optind==argc)
281 WRITE_MSG(2, "No mount point specified; try '--help' for more information\n");
282 else if (xid==VC_NOCTX)
283 WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
284 else if (isHigherLimit(limit.space_used, limit.space_total))
285 WRITE_MSG(2, "invalid parameters: 'space_used' is larger than 'space_total'\n");
286 else if (isHigherLimit(limit.inodes_used, limit.inodes_total))
287 WRITE_MSG(2, "invalid parameters: 'inodes_used' is larger than 'inodes_total'\n");
289 for (; optind < argc; ++optind) {
290 if (do_set) setDlimit(argv[optind], xid, flags, &limit);
291 else if (do_remove) remDlimit(argv[optind], xid, flags);
292 else printDlimit(argv[optind], xid, flags, true);