2011-03-30 18:27:52 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2010 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Edward Tomasz Napierala under sponsorship
|
|
|
|
* from the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/rctl.h>
|
2015-04-29 16:41:49 +00:00
|
|
|
#include <sys/sysctl.h>
|
2011-03-30 18:27:52 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <libutil.h>
|
|
|
|
#include <pwd.h>
|
2015-11-29 12:01:36 +00:00
|
|
|
#include <stdbool.h>
|
2011-03-30 18:27:52 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-11-05 19:15:39 +00:00
|
|
|
#define RCTL_DEFAULT_BUFSIZE 128 * 1024
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
static int
|
2015-11-29 13:14:45 +00:00
|
|
|
parse_user(const char *s, id_t *uidp, const char *unexpanded_rule)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
|
|
|
pwd = getpwnam(s);
|
2015-11-29 12:01:36 +00:00
|
|
|
if (pwd != NULL) {
|
|
|
|
*uidp = pwd->pw_uid;
|
|
|
|
return (0);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
if (!isnumber(s[0])) {
|
2015-11-29 13:14:45 +00:00
|
|
|
warnx("malformed rule '%s': uknown user '%s'",
|
|
|
|
unexpanded_rule, s);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
*uidp = strtod(s, &end);
|
|
|
|
if ((size_t)(end - s) != strlen(s)) {
|
2015-11-29 13:14:45 +00:00
|
|
|
warnx("malformed rule '%s': trailing characters "
|
|
|
|
"after numerical id", unexpanded_rule);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
return (0);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
static int
|
2015-11-29 13:14:45 +00:00
|
|
|
parse_group(const char *s, id_t *gidp, const char *unexpanded_rule)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
struct group *grp;
|
|
|
|
|
|
|
|
grp = getgrnam(s);
|
2015-11-29 12:01:36 +00:00
|
|
|
if (grp != NULL) {
|
|
|
|
*gidp = grp->gr_gid;
|
|
|
|
return (0);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
if (!isnumber(s[0])) {
|
2015-11-29 13:14:45 +00:00
|
|
|
warnx("malformed rule '%s': uknown group '%s'",
|
|
|
|
unexpanded_rule, s);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
*gidp = strtod(s, &end);
|
|
|
|
if ((size_t)(end - s) != strlen(s)) {
|
2015-11-29 13:14:45 +00:00
|
|
|
warnx("malformed rule '%s': trailing characters "
|
|
|
|
"after numerical id", unexpanded_rule);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
return (0);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-11-29 12:01:36 +00:00
|
|
|
* Replace human-readable number with its expanded form.
|
2011-03-30 18:27:52 +00:00
|
|
|
*/
|
|
|
|
static char *
|
2015-11-29 13:14:45 +00:00
|
|
|
expand_amount(char *rule, const char *unexpanded_rule)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
uint64_t num;
|
|
|
|
const char *subject, *subject_id, *resource, *action, *amount, *per;
|
2015-11-29 13:14:45 +00:00
|
|
|
char *expanded;
|
2015-11-29 12:09:12 +00:00
|
|
|
int ret;
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 13:14:45 +00:00
|
|
|
subject = strsep(&rule, ":");
|
|
|
|
subject_id = strsep(&rule, ":");
|
|
|
|
resource = strsep(&rule, ":");
|
|
|
|
action = strsep(&rule, "=/");
|
|
|
|
amount = strsep(&rule, "/");
|
|
|
|
per = rule;
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 13:14:45 +00:00
|
|
|
if (amount == NULL || strlen(amount) == 0)
|
2011-03-30 18:27:52 +00:00
|
|
|
return (rule);
|
|
|
|
|
|
|
|
assert(subject != NULL);
|
|
|
|
assert(subject_id != NULL);
|
|
|
|
assert(resource != NULL);
|
|
|
|
assert(action != NULL);
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
if (expand_number(amount, &num)) {
|
2015-11-29 13:14:45 +00:00
|
|
|
warnx("malformed rule '%s': invalid numeric value '%s'",
|
|
|
|
unexpanded_rule, amount);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:09:12 +00:00
|
|
|
if (per == NULL) {
|
|
|
|
ret = asprintf(&expanded, "%s:%s:%s:%s=%ju",
|
|
|
|
subject, subject_id, resource, action, (uintmax_t)num);
|
|
|
|
} else {
|
|
|
|
ret = asprintf(&expanded, "%s:%s:%s:%s=%ju/%s",
|
|
|
|
subject, subject_id, resource, action, (uintmax_t)num, per);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:09:12 +00:00
|
|
|
if (ret <= 0) {
|
2015-11-29 12:01:36 +00:00
|
|
|
warn("asprintf");
|
|
|
|
return (NULL);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
|
|
|
return (expanded);
|
|
|
|
}
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
static char *
|
2015-11-29 13:14:45 +00:00
|
|
|
expand_rule(const char *rule, bool resolve_ids)
|
2015-11-29 12:01:36 +00:00
|
|
|
{
|
|
|
|
id_t id;
|
|
|
|
const char *subject, *textid, *rest;
|
2015-11-29 13:14:45 +00:00
|
|
|
char *copy, *expanded, *resolved, *tofree;
|
2015-11-29 12:09:12 +00:00
|
|
|
int error, ret;
|
2015-11-29 12:01:36 +00:00
|
|
|
|
2015-11-29 13:14:45 +00:00
|
|
|
tofree = copy = strdup(rule);
|
|
|
|
if (copy == NULL) {
|
|
|
|
warn("strdup");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
subject = strsep(©, ":");
|
|
|
|
textid = strsep(©, ":");
|
2015-11-29 12:01:36 +00:00
|
|
|
if (textid == NULL) {
|
2015-11-29 13:14:45 +00:00
|
|
|
warnx("malformed rule '%s': missing subject", rule);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2015-11-29 13:14:45 +00:00
|
|
|
if (copy != NULL)
|
|
|
|
rest = copy;
|
2015-11-29 12:01:36 +00:00
|
|
|
else
|
|
|
|
rest = "";
|
|
|
|
|
|
|
|
if (strcasecmp(subject, "u") == 0)
|
|
|
|
subject = "user";
|
|
|
|
else if (strcasecmp(subject, "g") == 0)
|
|
|
|
subject = "group";
|
|
|
|
else if (strcasecmp(subject, "p") == 0)
|
|
|
|
subject = "process";
|
|
|
|
else if (strcasecmp(subject, "l") == 0 ||
|
|
|
|
strcasecmp(subject, "c") == 0 ||
|
|
|
|
strcasecmp(subject, "class") == 0)
|
|
|
|
subject = "loginclass";
|
|
|
|
else if (strcasecmp(subject, "j") == 0)
|
|
|
|
subject = "jail";
|
|
|
|
|
|
|
|
if (resolve_ids &&
|
|
|
|
strcasecmp(subject, "user") == 0 && strlen(textid) > 0) {
|
2015-11-29 13:14:45 +00:00
|
|
|
error = parse_user(textid, &id, rule);
|
|
|
|
if (error != 0) {
|
|
|
|
free(tofree);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (NULL);
|
2015-11-29 13:14:45 +00:00
|
|
|
}
|
2015-11-29 12:09:12 +00:00
|
|
|
ret = asprintf(&resolved, "%s:%d:%s", subject, (int)id, rest);
|
2015-11-29 12:01:36 +00:00
|
|
|
} else if (resolve_ids &&
|
|
|
|
strcasecmp(subject, "group") == 0 && strlen(textid) > 0) {
|
2015-11-29 13:14:45 +00:00
|
|
|
error = parse_group(textid, &id, rule);
|
|
|
|
if (error != 0) {
|
|
|
|
free(tofree);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (NULL);
|
2015-11-29 13:14:45 +00:00
|
|
|
}
|
2015-11-29 12:09:12 +00:00
|
|
|
ret = asprintf(&resolved, "%s:%d:%s", subject, (int)id, rest);
|
2015-11-29 12:01:36 +00:00
|
|
|
} else {
|
2015-11-29 12:09:12 +00:00
|
|
|
ret = asprintf(&resolved, "%s:%s:%s", subject, textid, rest);
|
2015-11-29 12:01:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 12:09:12 +00:00
|
|
|
if (ret <= 0) {
|
2015-11-29 12:01:36 +00:00
|
|
|
warn("asprintf");
|
2015-11-29 13:14:45 +00:00
|
|
|
free(tofree);
|
2015-11-29 12:01:36 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2015-11-29 13:14:45 +00:00
|
|
|
free(tofree);
|
|
|
|
|
|
|
|
expanded = expand_amount(resolved, rule);
|
|
|
|
free(resolved);
|
|
|
|
|
|
|
|
return (expanded);
|
2015-11-29 12:01:36 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 18:27:52 +00:00
|
|
|
static char *
|
|
|
|
humanize_ids(char *rule)
|
|
|
|
{
|
|
|
|
id_t id;
|
|
|
|
struct passwd *pwd;
|
|
|
|
struct group *grp;
|
|
|
|
const char *subject, *textid, *rest;
|
2015-11-29 11:30:17 +00:00
|
|
|
char *end, *humanized;
|
2015-11-29 12:09:12 +00:00
|
|
|
int ret;
|
2011-03-30 18:27:52 +00:00
|
|
|
|
|
|
|
subject = strsep(&rule, ":");
|
|
|
|
textid = strsep(&rule, ":");
|
|
|
|
if (textid == NULL)
|
|
|
|
errx(1, "rule passed from the kernel didn't contain subject");
|
|
|
|
if (rule != NULL)
|
|
|
|
rest = rule;
|
|
|
|
else
|
|
|
|
rest = "";
|
|
|
|
|
|
|
|
/* Replace numerical user and group ids with names. */
|
|
|
|
if (strcasecmp(subject, "user") == 0) {
|
2015-11-29 11:30:17 +00:00
|
|
|
id = strtod(textid, &end);
|
|
|
|
if ((size_t)(end - textid) != strlen(textid))
|
|
|
|
errx(1, "malformed uid '%s'", textid);
|
2011-03-30 18:27:52 +00:00
|
|
|
pwd = getpwuid(id);
|
|
|
|
if (pwd != NULL)
|
|
|
|
textid = pwd->pw_name;
|
|
|
|
} else if (strcasecmp(subject, "group") == 0) {
|
2015-11-29 11:30:17 +00:00
|
|
|
id = strtod(textid, &end);
|
|
|
|
if ((size_t)(end - textid) != strlen(textid))
|
|
|
|
errx(1, "malformed gid '%s'", textid);
|
2011-03-30 18:27:52 +00:00
|
|
|
grp = getgrgid(id);
|
|
|
|
if (grp != NULL)
|
|
|
|
textid = grp->gr_name;
|
|
|
|
}
|
|
|
|
|
2015-11-29 12:09:12 +00:00
|
|
|
ret = asprintf(&humanized, "%s:%s:%s", subject, textid, rest);
|
|
|
|
if (ret <= 0)
|
2011-03-30 18:27:52 +00:00
|
|
|
err(1, "asprintf");
|
|
|
|
|
|
|
|
return (humanized);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
str2int64(const char *str, int64_t *value)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
*value = strtoul(str, &end, 10);
|
|
|
|
if ((size_t)(end - str) != strlen(str))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
humanize_amount(char *rule)
|
|
|
|
{
|
|
|
|
int64_t num;
|
|
|
|
const char *subject, *subject_id, *resource, *action, *amount, *per;
|
2015-11-29 12:21:02 +00:00
|
|
|
char *copy, *humanized, buf[6], *tofree;
|
2015-11-29 12:09:12 +00:00
|
|
|
int ret;
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:21:02 +00:00
|
|
|
tofree = copy = strdup(rule);
|
2011-03-30 18:27:52 +00:00
|
|
|
if (copy == NULL)
|
|
|
|
err(1, "strdup");
|
|
|
|
|
|
|
|
subject = strsep(©, ":");
|
|
|
|
subject_id = strsep(©, ":");
|
|
|
|
resource = strsep(©, ":");
|
|
|
|
action = strsep(©, "=/");
|
|
|
|
amount = strsep(©, "/");
|
|
|
|
per = copy;
|
|
|
|
|
|
|
|
if (amount == NULL || strlen(amount) == 0 ||
|
|
|
|
str2int64(amount, &num) != 0) {
|
2015-11-29 12:21:02 +00:00
|
|
|
free(tofree);
|
2011-03-30 18:27:52 +00:00
|
|
|
return (rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(subject != NULL);
|
|
|
|
assert(subject_id != NULL);
|
|
|
|
assert(resource != NULL);
|
|
|
|
assert(action != NULL);
|
|
|
|
|
|
|
|
if (humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE,
|
|
|
|
HN_DECIMAL | HN_NOSPACE) == -1)
|
|
|
|
err(1, "humanize_number");
|
|
|
|
|
2015-11-29 12:09:12 +00:00
|
|
|
if (per == NULL) {
|
|
|
|
ret = asprintf(&humanized, "%s:%s:%s:%s=%s",
|
|
|
|
subject, subject_id, resource, action, buf);
|
|
|
|
} else {
|
|
|
|
ret = asprintf(&humanized, "%s:%s:%s:%s=%s/%s",
|
|
|
|
subject, subject_id, resource, action, buf, per);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:09:12 +00:00
|
|
|
if (ret <= 0)
|
2011-03-30 18:27:52 +00:00
|
|
|
err(1, "asprintf");
|
|
|
|
|
2015-11-29 12:21:02 +00:00
|
|
|
free(tofree);
|
2011-03-30 18:27:52 +00:00
|
|
|
return (humanized);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print rules, one per line.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
print_rules(char *rules, int hflag, int nflag)
|
|
|
|
{
|
|
|
|
char *rule;
|
|
|
|
|
|
|
|
while ((rule = strsep(&rules, ",")) != NULL) {
|
|
|
|
if (rule[0] == '\0')
|
|
|
|
break; /* XXX */
|
|
|
|
if (nflag == 0)
|
|
|
|
rule = humanize_ids(rule);
|
|
|
|
if (hflag)
|
|
|
|
rule = humanize_amount(rule);
|
|
|
|
printf("%s\n", rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 16:41:49 +00:00
|
|
|
static void
|
|
|
|
enosys(void)
|
|
|
|
{
|
|
|
|
int error, racct_enable;
|
|
|
|
size_t racct_enable_len;
|
|
|
|
|
|
|
|
racct_enable_len = sizeof(racct_enable);
|
|
|
|
error = sysctlbyname("kern.racct.enable",
|
|
|
|
&racct_enable, &racct_enable_len, NULL, 0);
|
|
|
|
|
|
|
|
if (error != 0) {
|
|
|
|
if (errno == ENOENT)
|
2015-04-29 17:01:28 +00:00
|
|
|
errx(1, "RACCT/RCTL support not present in kernel; see rctl(8) for details");
|
2015-04-29 16:41:49 +00:00
|
|
|
|
|
|
|
err(1, "sysctlbyname");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (racct_enable == 0)
|
|
|
|
errx(1, "RACCT/RCTL present, but disabled; enable using kern.racct.enable=1 tunable");
|
|
|
|
}
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
static int
|
2015-11-29 13:14:45 +00:00
|
|
|
add_rule(const char *rule, const char *unexpanded_rule)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = rctl_add_rule(rule, strlen(rule) + 1, NULL, 0);
|
2015-04-29 16:41:49 +00:00
|
|
|
if (error != 0) {
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
enosys();
|
2015-11-29 13:14:45 +00:00
|
|
|
warn("failed to add rule '%s'", unexpanded_rule);
|
2015-04-29 16:41:49 +00:00
|
|
|
}
|
2015-11-29 12:01:36 +00:00
|
|
|
|
|
|
|
return (error);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
static int
|
2015-11-29 13:14:45 +00:00
|
|
|
show_limits(const char *filter, const char *unexpanded_rule,
|
|
|
|
int hflag, int nflag)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
char *outbuf = NULL;
|
|
|
|
size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
|
|
|
|
|
2015-11-29 12:33:56 +00:00
|
|
|
for (;;) {
|
2011-03-30 18:27:52 +00:00
|
|
|
outbuflen *= 4;
|
|
|
|
outbuf = realloc(outbuf, outbuflen);
|
|
|
|
if (outbuf == NULL)
|
|
|
|
err(1, "realloc");
|
2015-11-29 12:33:56 +00:00
|
|
|
error = rctl_get_limits(filter, strlen(filter) + 1,
|
|
|
|
outbuf, outbuflen);
|
|
|
|
if (error == 0)
|
|
|
|
break;
|
|
|
|
if (errno == ERANGE)
|
|
|
|
continue;
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
enosys();
|
2015-11-29 13:14:45 +00:00
|
|
|
warn("failed to get limits for '%s'", unexpanded_rule);
|
2015-11-29 12:33:56 +00:00
|
|
|
free(outbuf);
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:33:56 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
|
|
|
print_rules(outbuf, hflag, nflag);
|
|
|
|
free(outbuf);
|
2015-11-29 12:01:36 +00:00
|
|
|
|
|
|
|
return (error);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
static int
|
2015-11-29 13:14:45 +00:00
|
|
|
remove_rule(const char *filter, const char *unexpanded_rule)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = rctl_remove_rule(filter, strlen(filter) + 1, NULL, 0);
|
2015-04-29 16:41:49 +00:00
|
|
|
if (error != 0) {
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
enosys();
|
2015-11-29 13:14:45 +00:00
|
|
|
warn("failed to remove rule '%s'", unexpanded_rule);
|
2015-04-29 16:41:49 +00:00
|
|
|
}
|
2015-11-29 12:01:36 +00:00
|
|
|
|
|
|
|
return (error);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
humanize_usage_amount(char *usage)
|
|
|
|
{
|
|
|
|
int64_t num;
|
|
|
|
const char *resource, *amount;
|
2015-11-29 12:21:02 +00:00
|
|
|
char *copy, *humanized, buf[6], *tofree;
|
2015-11-29 12:09:12 +00:00
|
|
|
int ret;
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:21:02 +00:00
|
|
|
tofree = copy = strdup(usage);
|
2011-03-30 18:27:52 +00:00
|
|
|
if (copy == NULL)
|
|
|
|
err(1, "strdup");
|
|
|
|
|
|
|
|
resource = strsep(©, "=");
|
|
|
|
amount = copy;
|
|
|
|
|
|
|
|
assert(resource != NULL);
|
|
|
|
assert(amount != NULL);
|
|
|
|
|
|
|
|
if (str2int64(amount, &num) != 0 ||
|
|
|
|
humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE,
|
|
|
|
HN_DECIMAL | HN_NOSPACE) == -1) {
|
2015-11-29 12:21:02 +00:00
|
|
|
free(tofree);
|
2011-03-30 18:27:52 +00:00
|
|
|
return (usage);
|
|
|
|
}
|
|
|
|
|
2015-11-29 12:09:12 +00:00
|
|
|
ret = asprintf(&humanized, "%s=%s", resource, buf);
|
|
|
|
if (ret <= 0)
|
2011-03-30 18:27:52 +00:00
|
|
|
err(1, "asprintf");
|
|
|
|
|
2015-11-29 12:21:02 +00:00
|
|
|
free(tofree);
|
2011-03-30 18:27:52 +00:00
|
|
|
return (humanized);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Query the kernel about a resource usage and print it out.
|
|
|
|
*/
|
2015-11-29 12:01:36 +00:00
|
|
|
static int
|
2015-11-29 13:14:45 +00:00
|
|
|
show_usage(const char *filter, const char *unexpanded_rule, int hflag)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
int error;
|
2015-11-29 12:21:02 +00:00
|
|
|
char *copy, *outbuf = NULL, *tmp;
|
2011-03-30 18:27:52 +00:00
|
|
|
size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
|
|
|
|
|
2015-11-29 12:33:56 +00:00
|
|
|
for (;;) {
|
2011-03-30 18:27:52 +00:00
|
|
|
outbuflen *= 4;
|
|
|
|
outbuf = realloc(outbuf, outbuflen);
|
|
|
|
if (outbuf == NULL)
|
|
|
|
err(1, "realloc");
|
2015-11-29 12:33:56 +00:00
|
|
|
error = rctl_get_racct(filter, strlen(filter) + 1,
|
|
|
|
outbuf, outbuflen);
|
|
|
|
if (error == 0)
|
|
|
|
break;
|
|
|
|
if (errno == ERANGE)
|
|
|
|
continue;
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
enosys();
|
2015-11-29 13:14:45 +00:00
|
|
|
warn("failed to show resource consumption for '%s'",
|
|
|
|
unexpanded_rule);
|
2015-11-29 12:33:56 +00:00
|
|
|
free(outbuf);
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:33:56 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:21:02 +00:00
|
|
|
copy = outbuf;
|
|
|
|
while ((tmp = strsep(©, ",")) != NULL) {
|
2011-03-30 18:27:52 +00:00
|
|
|
if (tmp[0] == '\0')
|
|
|
|
break; /* XXX */
|
|
|
|
|
|
|
|
if (hflag)
|
|
|
|
tmp = humanize_usage_amount(tmp);
|
|
|
|
|
|
|
|
printf("%s\n", tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(outbuf);
|
2015-11-29 12:01:36 +00:00
|
|
|
|
|
|
|
return (error);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Query the kernel about resource limit rules and print them out.
|
|
|
|
*/
|
2015-11-29 12:01:36 +00:00
|
|
|
static int
|
2015-11-29 13:14:45 +00:00
|
|
|
show_rules(const char *filter, const char *unexpanded_rule,
|
|
|
|
int hflag, int nflag)
|
2011-03-30 18:27:52 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
char *outbuf = NULL;
|
|
|
|
size_t filterlen, outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
|
|
|
|
|
|
|
|
if (filter != NULL)
|
|
|
|
filterlen = strlen(filter) + 1;
|
|
|
|
else
|
|
|
|
filterlen = 0;
|
|
|
|
|
2015-11-29 12:33:56 +00:00
|
|
|
for (;;) {
|
2011-03-30 18:27:52 +00:00
|
|
|
outbuflen *= 4;
|
|
|
|
outbuf = realloc(outbuf, outbuflen);
|
|
|
|
if (outbuf == NULL)
|
|
|
|
err(1, "realloc");
|
|
|
|
error = rctl_get_rules(filter, filterlen, outbuf, outbuflen);
|
2015-11-29 12:33:56 +00:00
|
|
|
if (error == 0)
|
|
|
|
break;
|
|
|
|
if (errno == ERANGE)
|
|
|
|
continue;
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
enosys();
|
2015-11-29 13:14:45 +00:00
|
|
|
warn("failed to show rules for '%s'", unexpanded_rule);
|
2015-11-29 12:33:56 +00:00
|
|
|
free(outbuf);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
|
|
|
print_rules(outbuf, hflag, nflag);
|
|
|
|
free(outbuf);
|
2015-11-29 12:01:36 +00:00
|
|
|
|
|
|
|
return (error);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr, "usage: rctl [ -h ] [-a rule | -l filter | -r filter "
|
|
|
|
"| -u filter | filter]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc __unused, char **argv __unused)
|
|
|
|
{
|
|
|
|
int ch, aflag = 0, hflag = 0, nflag = 0, lflag = 0, rflag = 0,
|
|
|
|
uflag = 0;
|
2015-11-29 13:14:45 +00:00
|
|
|
char *rule = NULL, *unexpanded_rule;
|
|
|
|
int i, cumulated_error, error;
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ahlnru")) != -1) {
|
2011-03-30 18:27:52 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2015-11-29 12:01:36 +00:00
|
|
|
|
|
|
|
if (aflag + lflag + rflag + uflag > 1)
|
|
|
|
errx(1, "at most one of -a, -l, -r, or -u may be specified");
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
if (aflag + lflag + rflag + uflag == 0) {
|
2011-03-30 18:27:52 +00:00
|
|
|
rule = strdup("::");
|
2015-11-29 13:14:45 +00:00
|
|
|
show_rules(rule, rule, hflag, nflag);
|
2015-11-29 12:01:36 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
usage();
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
cumulated_error = 0;
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
2015-11-29 13:14:45 +00:00
|
|
|
unexpanded_rule = argv[i];
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
/*
|
|
|
|
* Skip resolving if passed -n _and_ -a. Ignore -n otherwise,
|
|
|
|
* so we can still do "rctl -n u:root" and see the rules without
|
|
|
|
* resolving the UID.
|
|
|
|
*/
|
|
|
|
if (aflag != 0 && nflag != 0)
|
2015-11-29 13:14:45 +00:00
|
|
|
rule = expand_rule(unexpanded_rule, false);
|
2015-11-29 12:01:36 +00:00
|
|
|
else
|
2015-11-29 13:14:45 +00:00
|
|
|
rule = expand_rule(unexpanded_rule, true);
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
if (rule == NULL) {
|
|
|
|
cumulated_error++;
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 13:14:45 +00:00
|
|
|
/*
|
|
|
|
* The reason for passing the unexpanded_rule is to make
|
|
|
|
* it easier for the user to search for the problematic
|
|
|
|
* rule in the passed input.
|
|
|
|
*/
|
2015-11-29 12:01:36 +00:00
|
|
|
if (aflag) {
|
2015-11-29 13:14:45 +00:00
|
|
|
error = add_rule(rule, unexpanded_rule);
|
2015-11-29 12:01:36 +00:00
|
|
|
} else if (lflag) {
|
2015-11-29 13:14:45 +00:00
|
|
|
error = show_limits(rule, unexpanded_rule,
|
|
|
|
hflag, nflag);
|
2015-11-29 12:01:36 +00:00
|
|
|
} else if (rflag) {
|
2015-11-29 13:14:45 +00:00
|
|
|
error = remove_rule(rule, unexpanded_rule);
|
2015-11-29 12:01:36 +00:00
|
|
|
} else if (uflag) {
|
2015-11-29 13:14:45 +00:00
|
|
|
error = show_usage(rule, unexpanded_rule, hflag);
|
2015-11-29 12:01:36 +00:00
|
|
|
} else {
|
2015-11-29 13:14:45 +00:00
|
|
|
error = show_rules(rule, unexpanded_rule,
|
|
|
|
hflag, nflag);
|
2015-11-29 12:01:36 +00:00
|
|
|
}
|
2011-03-30 18:27:52 +00:00
|
|
|
|
2015-11-29 13:14:45 +00:00
|
|
|
if (error != 0)
|
|
|
|
cumulated_error++;
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
free(rule);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 12:01:36 +00:00
|
|
|
return (cumulated_error);
|
2011-03-30 18:27:52 +00:00
|
|
|
}
|