2 * Copyright (c) 2010 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
37 #include <sys/sysctl.h>
52 #define RCTL_DEFAULT_BUFSIZE 128 * 1024
55 parse_user(const char *s, id_t *uidp, const char *unexpanded_rule)
66 if (!isnumber(s[0])) {
67 warnx("malformed rule '%s': unknown user '%s'",
72 *uidp = strtod(s, &end);
73 if ((size_t)(end - s) != strlen(s)) {
74 warnx("malformed rule '%s': trailing characters "
75 "after numerical id", unexpanded_rule);
83 parse_group(const char *s, id_t *gidp, const char *unexpanded_rule)
94 if (!isnumber(s[0])) {
95 warnx("malformed rule '%s': unknown group '%s'",
100 *gidp = strtod(s, &end);
101 if ((size_t)(end - s) != strlen(s)) {
102 warnx("malformed rule '%s': trailing characters "
103 "after numerical id", unexpanded_rule);
111 * Replace human-readable number with its expanded form.
114 expand_amount(const char *rule, const char *unexpanded_rule)
117 const char *subject, *subject_id, *resource, *action, *amount, *per;
118 char *copy, *expanded, *tofree;
121 tofree = copy = strdup(rule);
127 subject = strsep(©, ":");
128 subject_id = strsep(©, ":");
129 resource = strsep(©, ":");
130 action = strsep(©, "=/");
131 amount = strsep(©, "/");
134 if (amount == NULL || strlen(amount) == 0) {
136 * The "copy" has already been tinkered with by strsep().
147 assert(subject != NULL);
148 assert(subject_id != NULL);
149 assert(resource != NULL);
150 assert(action != NULL);
152 if (expand_number(amount, &num)) {
153 warnx("malformed rule '%s': invalid numeric value '%s'",
154 unexpanded_rule, amount);
160 ret = asprintf(&expanded, "%s:%s:%s:%s=%ju",
161 subject, subject_id, resource, action, (uintmax_t)num);
163 ret = asprintf(&expanded, "%s:%s:%s:%s=%ju/%s",
164 subject, subject_id, resource, action, (uintmax_t)num, per);
179 expand_rule(const char *rule, bool resolve_ids)
182 const char *subject, *textid, *rest;
183 char *copy, *expanded, *resolved, *tofree;
186 tofree = copy = strdup(rule);
192 subject = strsep(©, ":");
193 textid = strsep(©, ":");
194 if (textid == NULL) {
195 warnx("malformed rule '%s': missing subject", rule);
203 if (strcasecmp(subject, "u") == 0)
205 else if (strcasecmp(subject, "g") == 0)
207 else if (strcasecmp(subject, "p") == 0)
209 else if (strcasecmp(subject, "l") == 0 ||
210 strcasecmp(subject, "c") == 0 ||
211 strcasecmp(subject, "class") == 0)
212 subject = "loginclass";
213 else if (strcasecmp(subject, "j") == 0)
217 strcasecmp(subject, "user") == 0 && strlen(textid) > 0) {
218 error = parse_user(textid, &id, rule);
223 ret = asprintf(&resolved, "%s:%d:%s", subject, (int)id, rest);
224 } else if (resolve_ids &&
225 strcasecmp(subject, "group") == 0 && strlen(textid) > 0) {
226 error = parse_group(textid, &id, rule);
231 ret = asprintf(&resolved, "%s:%d:%s", subject, (int)id, rest);
233 ret = asprintf(&resolved, "%s:%s:%s", subject, textid, rest);
244 expanded = expand_amount(resolved, rule);
251 humanize_ids(char *rule)
256 const char *subject, *textid, *rest;
257 char *end, *humanized;
260 subject = strsep(&rule, ":");
261 textid = strsep(&rule, ":");
263 errx(1, "rule passed from the kernel didn't contain subject");
269 /* Replace numerical user and group ids with names. */
270 if (strcasecmp(subject, "user") == 0) {
271 id = strtod(textid, &end);
272 if ((size_t)(end - textid) != strlen(textid))
273 errx(1, "malformed uid '%s'", textid);
276 textid = pwd->pw_name;
277 } else if (strcasecmp(subject, "group") == 0) {
278 id = strtod(textid, &end);
279 if ((size_t)(end - textid) != strlen(textid))
280 errx(1, "malformed gid '%s'", textid);
283 textid = grp->gr_name;
286 ret = asprintf(&humanized, "%s:%s:%s", subject, textid, rest);
294 str2int64(const char *str, int64_t *value)
301 *value = strtoul(str, &end, 10);
302 if ((size_t)(end - str) != strlen(str))
309 humanize_amount(char *rule)
312 const char *subject, *subject_id, *resource, *action, *amount, *per;
313 char *copy, *humanized, buf[6], *tofree;
316 tofree = copy = strdup(rule);
320 subject = strsep(©, ":");
321 subject_id = strsep(©, ":");
322 resource = strsep(©, ":");
323 action = strsep(©, "=/");
324 amount = strsep(©, "/");
327 if (amount == NULL || strlen(amount) == 0 ||
328 str2int64(amount, &num) != 0) {
333 assert(subject != NULL);
334 assert(subject_id != NULL);
335 assert(resource != NULL);
336 assert(action != NULL);
338 if (humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE,
339 HN_DECIMAL | HN_NOSPACE) == -1)
340 err(1, "humanize_number");
343 ret = asprintf(&humanized, "%s:%s:%s:%s=%s",
344 subject, subject_id, resource, action, buf);
346 ret = asprintf(&humanized, "%s:%s:%s:%s=%s/%s",
347 subject, subject_id, resource, action, buf, per);
358 * Print rules, one per line.
361 print_rules(char *rules, int hflag, int nflag)
365 while ((rule = strsep(&rules, ",")) != NULL) {
369 rule = humanize_ids(rule);
371 rule = humanize_amount(rule);
372 printf("%s\n", rule);
379 int error, racct_enable;
380 size_t racct_enable_len;
382 racct_enable_len = sizeof(racct_enable);
383 error = sysctlbyname("kern.racct.enable",
384 &racct_enable, &racct_enable_len, NULL, 0);
388 errx(1, "RACCT/RCTL support not present in kernel; see rctl(8) for details");
390 err(1, "sysctlbyname");
393 if (racct_enable == 0)
394 errx(1, "RACCT/RCTL present, but disabled; enable using kern.racct.enable=1 tunable");
398 add_rule(const char *rule, const char *unexpanded_rule)
402 error = rctl_add_rule(rule, strlen(rule) + 1, NULL, 0);
406 warn("failed to add rule '%s'", unexpanded_rule);
413 show_limits(const char *filter, const char *unexpanded_rule,
414 int hflag, int nflag)
418 size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
422 outbuf = realloc(outbuf, outbuflen);
425 error = rctl_get_limits(filter, strlen(filter) + 1,
433 warn("failed to get limits for '%s'", unexpanded_rule);
439 print_rules(outbuf, hflag, nflag);
446 remove_rule(const char *filter, const char *unexpanded_rule)
450 error = rctl_remove_rule(filter, strlen(filter) + 1, NULL, 0);
454 warn("failed to remove rule '%s'", unexpanded_rule);
461 humanize_usage_amount(char *usage)
464 const char *resource, *amount;
465 char *copy, *humanized, buf[6], *tofree;
468 tofree = copy = strdup(usage);
472 resource = strsep(©, "=");
475 assert(resource != NULL);
476 assert(amount != NULL);
478 if (str2int64(amount, &num) != 0 ||
479 humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE,
480 HN_DECIMAL | HN_NOSPACE) == -1) {
485 ret = asprintf(&humanized, "%s=%s", resource, buf);
494 * Query the kernel about a resource usage and print it out.
497 show_usage(const char *filter, const char *unexpanded_rule, int hflag)
500 char *copy, *outbuf = NULL, *tmp;
501 size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
505 outbuf = realloc(outbuf, outbuflen);
508 error = rctl_get_racct(filter, strlen(filter) + 1,
516 warn("failed to show resource consumption for '%s'",
524 while ((tmp = strsep(©, ",")) != NULL) {
529 tmp = humanize_usage_amount(tmp);
540 * Query the kernel about resource limit rules and print them out.
543 show_rules(const char *filter, const char *unexpanded_rule,
544 int hflag, int nflag)
548 size_t filterlen, outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
551 filterlen = strlen(filter) + 1;
557 outbuf = realloc(outbuf, outbuflen);
560 error = rctl_get_rules(filter, filterlen, outbuf, outbuflen);
567 warn("failed to show rules for '%s'", unexpanded_rule);
573 print_rules(outbuf, hflag, nflag);
583 fprintf(stderr, "usage: rctl [ -h ] [-a rule | -l filter | -r filter "
584 "| -u filter | filter]\n");
589 main(int argc __unused, char **argv __unused)
591 int ch, aflag = 0, hflag = 0, nflag = 0, lflag = 0, rflag = 0,
593 char *rule = NULL, *unexpanded_rule;
594 int i, cumulated_error, error;
596 while ((ch = getopt(argc, argv, "ahlnru")) != -1) {
626 if (aflag + lflag + rflag + uflag > 1)
627 errx(1, "at most one of -a, -l, -r, or -u may be specified");
630 if (aflag + lflag + rflag + uflag == 0) {
632 show_rules(rule, rule, hflag, nflag);
642 for (i = 0; i < argc; i++) {
643 unexpanded_rule = argv[i];
646 * Skip resolving if passed -n _and_ -a. Ignore -n otherwise,
647 * so we can still do "rctl -n u:root" and see the rules without
650 if (aflag != 0 && nflag != 0)
651 rule = expand_rule(unexpanded_rule, false);
653 rule = expand_rule(unexpanded_rule, true);
661 * The reason for passing the unexpanded_rule is to make
662 * it easier for the user to search for the problematic
663 * rule in the passed input.
666 error = add_rule(rule, unexpanded_rule);
668 error = show_limits(rule, unexpanded_rule,
671 error = remove_rule(rule, unexpanded_rule);
673 error = show_usage(rule, unexpanded_rule, hflag);
675 error = show_rules(rule, unexpanded_rule,
685 return (cumulated_error);