2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
38 #include <sys/sysctl.h>
53 #define RCTL_DEFAULT_BUFSIZE 128 * 1024
56 parse_user(const char *s, id_t *uidp, const char *unexpanded_rule)
67 if (!isnumber(s[0])) {
68 warnx("malformed rule '%s': unknown user '%s'",
73 *uidp = strtod(s, &end);
74 if ((size_t)(end - s) != strlen(s)) {
75 warnx("malformed rule '%s': trailing characters "
76 "after numerical id", unexpanded_rule);
84 parse_group(const char *s, id_t *gidp, const char *unexpanded_rule)
95 if (!isnumber(s[0])) {
96 warnx("malformed rule '%s': unknown group '%s'",
101 *gidp = strtod(s, &end);
102 if ((size_t)(end - s) != strlen(s)) {
103 warnx("malformed rule '%s': trailing characters "
104 "after numerical id", unexpanded_rule);
112 * Replace human-readable number with its expanded form.
115 expand_amount(const char *rule, const char *unexpanded_rule)
118 const char *subject, *subject_id, *resource, *action, *amount, *per;
119 char *copy, *expanded, *tofree;
122 tofree = copy = strdup(rule);
128 subject = strsep(©, ":");
129 subject_id = strsep(©, ":");
130 resource = strsep(©, ":");
131 action = strsep(©, "=/");
132 amount = strsep(©, "/");
135 if (amount == NULL || strlen(amount) == 0) {
137 * The "copy" has already been tinkered with by strsep().
148 assert(subject != NULL);
149 assert(subject_id != NULL);
150 assert(resource != NULL);
151 assert(action != NULL);
153 if (expand_number(amount, &num)) {
154 warnx("malformed rule '%s': invalid numeric value '%s'",
155 unexpanded_rule, amount);
161 ret = asprintf(&expanded, "%s:%s:%s:%s=%ju",
162 subject, subject_id, resource, action, (uintmax_t)num);
164 ret = asprintf(&expanded, "%s:%s:%s:%s=%ju/%s",
165 subject, subject_id, resource, action, (uintmax_t)num, per);
180 expand_rule(const char *rule, bool resolve_ids)
183 const char *subject, *textid, *rest;
184 char *copy, *expanded, *resolved, *tofree;
187 tofree = copy = strdup(rule);
193 subject = strsep(©, ":");
194 textid = strsep(©, ":");
195 if (textid == NULL) {
196 warnx("malformed rule '%s': missing subject", rule);
204 if (strcasecmp(subject, "u") == 0)
206 else if (strcasecmp(subject, "g") == 0)
208 else if (strcasecmp(subject, "p") == 0)
210 else if (strcasecmp(subject, "l") == 0 ||
211 strcasecmp(subject, "c") == 0 ||
212 strcasecmp(subject, "class") == 0)
213 subject = "loginclass";
214 else if (strcasecmp(subject, "j") == 0)
218 strcasecmp(subject, "user") == 0 && strlen(textid) > 0) {
219 error = parse_user(textid, &id, rule);
224 ret = asprintf(&resolved, "%s:%d:%s", subject, (int)id, rest);
225 } else if (resolve_ids &&
226 strcasecmp(subject, "group") == 0 && strlen(textid) > 0) {
227 error = parse_group(textid, &id, rule);
232 ret = asprintf(&resolved, "%s:%d:%s", subject, (int)id, rest);
234 ret = asprintf(&resolved, "%s:%s:%s", subject, textid, rest);
245 expanded = expand_amount(resolved, rule);
252 humanize_ids(char *rule)
257 const char *subject, *textid, *rest;
258 char *end, *humanized;
261 subject = strsep(&rule, ":");
262 textid = strsep(&rule, ":");
264 errx(1, "rule passed from the kernel didn't contain subject");
270 /* Replace numerical user and group ids with names. */
271 if (strcasecmp(subject, "user") == 0) {
272 id = strtod(textid, &end);
273 if ((size_t)(end - textid) != strlen(textid))
274 errx(1, "malformed uid '%s'", textid);
277 textid = pwd->pw_name;
278 } else if (strcasecmp(subject, "group") == 0) {
279 id = strtod(textid, &end);
280 if ((size_t)(end - textid) != strlen(textid))
281 errx(1, "malformed gid '%s'", textid);
284 textid = grp->gr_name;
287 ret = asprintf(&humanized, "%s:%s:%s", subject, textid, rest);
295 str2int64(const char *str, int64_t *value)
302 *value = strtoul(str, &end, 10);
303 if ((size_t)(end - str) != strlen(str))
310 humanize_amount(char *rule)
313 const char *subject, *subject_id, *resource, *action, *amount, *per;
314 char *copy, *humanized, buf[6], *tofree;
317 tofree = copy = strdup(rule);
321 subject = strsep(©, ":");
322 subject_id = strsep(©, ":");
323 resource = strsep(©, ":");
324 action = strsep(©, "=/");
325 amount = strsep(©, "/");
328 if (amount == NULL || strlen(amount) == 0 ||
329 str2int64(amount, &num) != 0) {
334 assert(subject != NULL);
335 assert(subject_id != NULL);
336 assert(resource != NULL);
337 assert(action != NULL);
339 if (humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE,
340 HN_DECIMAL | HN_NOSPACE) == -1)
341 err(1, "humanize_number");
344 ret = asprintf(&humanized, "%s:%s:%s:%s=%s",
345 subject, subject_id, resource, action, buf);
347 ret = asprintf(&humanized, "%s:%s:%s:%s=%s/%s",
348 subject, subject_id, resource, action, buf, per);
359 * Print rules, one per line.
362 print_rules(char *rules, int hflag, int nflag)
366 while ((rule = strsep(&rules, ",")) != NULL) {
370 rule = humanize_ids(rule);
372 rule = humanize_amount(rule);
373 printf("%s\n", rule);
380 size_t racct_enable_len;
384 racct_enable_len = sizeof(racct_enable);
385 error = sysctlbyname("kern.racct.enable",
386 &racct_enable, &racct_enable_len, NULL, 0);
390 errx(1, "RACCT/RCTL support not present in kernel; see rctl(8) for details");
392 err(1, "sysctlbyname");
396 errx(1, "RACCT/RCTL present, but disabled; enable using kern.racct.enable=1 tunable");
400 add_rule(const char *rule, const char *unexpanded_rule)
404 error = rctl_add_rule(rule, strlen(rule) + 1, NULL, 0);
408 warn("failed to add rule '%s'", unexpanded_rule);
415 show_limits(const char *filter, const char *unexpanded_rule,
416 int hflag, int nflag)
420 size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
424 outbuf = realloc(outbuf, outbuflen);
427 error = rctl_get_limits(filter, strlen(filter) + 1,
435 warn("failed to get limits for '%s'", unexpanded_rule);
441 print_rules(outbuf, hflag, nflag);
448 remove_rule(const char *filter, const char *unexpanded_rule)
452 error = rctl_remove_rule(filter, strlen(filter) + 1, NULL, 0);
456 warn("failed to remove rule '%s'", unexpanded_rule);
463 humanize_usage_amount(char *usage)
466 const char *resource, *amount;
467 char *copy, *humanized, buf[6], *tofree;
470 tofree = copy = strdup(usage);
474 resource = strsep(©, "=");
477 assert(resource != NULL);
478 assert(amount != NULL);
480 if (str2int64(amount, &num) != 0 ||
481 humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE,
482 HN_DECIMAL | HN_NOSPACE) == -1) {
487 ret = asprintf(&humanized, "%s=%s", resource, buf);
496 * Query the kernel about a resource usage and print it out.
499 show_usage(const char *filter, const char *unexpanded_rule, int hflag)
502 char *copy, *outbuf = NULL, *tmp;
503 size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
507 outbuf = realloc(outbuf, outbuflen);
510 error = rctl_get_racct(filter, strlen(filter) + 1,
518 warn("failed to show resource consumption for '%s'",
526 while ((tmp = strsep(©, ",")) != NULL) {
531 tmp = humanize_usage_amount(tmp);
542 * Query the kernel about resource limit rules and print them out.
545 show_rules(const char *filter, const char *unexpanded_rule,
546 int hflag, int nflag)
550 size_t filterlen, outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
553 filterlen = strlen(filter) + 1;
559 outbuf = realloc(outbuf, outbuflen);
562 error = rctl_get_rules(filter, filterlen, outbuf, outbuflen);
569 warn("failed to show rules for '%s'", unexpanded_rule);
575 print_rules(outbuf, hflag, nflag);
585 fprintf(stderr, "usage: rctl [ -h ] [-a rule | -l filter | -r filter "
586 "| -u filter | filter]\n");
591 main(int argc, char **argv)
593 int ch, aflag = 0, hflag = 0, nflag = 0, lflag = 0, rflag = 0,
595 char *rule = NULL, *unexpanded_rule;
596 int i, cumulated_error, error;
598 while ((ch = getopt(argc, argv, "ahlnru")) != -1) {
628 if (aflag + lflag + rflag + uflag > 1)
629 errx(1, "at most one of -a, -l, -r, or -u may be specified");
632 if (aflag + lflag + rflag + uflag == 0) {
634 show_rules(rule, rule, hflag, nflag);
644 for (i = 0; i < argc; i++) {
645 unexpanded_rule = argv[i];
648 * Skip resolving if passed -n _and_ -a. Ignore -n otherwise,
649 * so we can still do "rctl -n u:root" and see the rules without
652 if (aflag != 0 && nflag != 0)
653 rule = expand_rule(unexpanded_rule, false);
655 rule = expand_rule(unexpanded_rule, true);
663 * The reason for passing the unexpanded_rule is to make
664 * it easier for the user to search for the problematic
665 * rule in the passed input.
668 error = add_rule(rule, unexpanded_rule);
670 error = show_limits(rule, unexpanded_rule,
673 error = remove_rule(rule, unexpanded_rule);
675 error = show_usage(rule, unexpanded_rule, hflag);
677 error = show_rules(rule, unexpanded_rule,
687 return (cumulated_error);