Merge pull request #8981 from keszybz/ratelimit-and-dbus

Ratelimit renaming and dbus error message fix
This commit is contained in:
Lennart Poettering 2018-05-18 21:38:30 +02:00 committed by GitHub
commit 6f8fa29465
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 49 additions and 37 deletions

View file

@ -28,20 +28,23 @@ __contains_word () {
__get_machines() {
local a b
machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
machinectl list --no-legend --no-pager 2>/dev/null |
{ while read a b; do echo " $a"; done; };
}
__get_busnames() {
local mode=$1
local a b
busctl $mode list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
busctl $mode list --no-legend --no-pager 2>/dev/null |
{ while read a b; do echo " $a"; done; };
}
__get_objects() {
local mode=$1
local busname=$2
local a b
busctl $mode tree --list --no-legend --no-pager $busname | { while read a b; do echo " $a"; done; };
busctl $mode tree --list --no-legend --no-pager $busname 2>/dev/null |
{ while read a b; do echo " $a"; done; };
}
__get_interfaces() {
@ -49,7 +52,8 @@ __get_interfaces() {
local busname=$2
local path=$3
local a b c
busctl $mode introspect --list --no-legend --no-pager $busname $path | { while read a b c; do [[ "$b" == "interface" ]] && echo " $a"; done; };
busctl $mode introspect --list --no-legend --no-pager $busname $path 2>/dev/null |
{ while read a b c; do [[ "$b" == "interface" ]] && echo " $a"; done; };
}
__get_members() {
@ -60,7 +64,9 @@ __get_members() {
local type=$5
local flags=$6
local a b c d e
busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c d e; do [[ "$b" == "$type" && ( -z $flags || "$e" == "$flags" ) ]] && echo " $a"; done; };
busctl $mode introspect --list --no-legend --no-pager $busname $path $interface 2>/dev/null |
sed -e 's/^\.//' |
{ while read a b c d e; do [[ "$b" == "$type" && ( -z $flags || "$e" == "$flags" ) ]] && echo " $a"; done; };
}
__get_signature() {
@ -70,7 +76,8 @@ __get_signature() {
local interface=$4
local member=$5
local a b c d
busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c d; do [[ "$a" == "$member" && "$c" != '-' ]] && echo " \"$c\""; done; };
busctl $mode introspect --list --no-legend --no-pager $busname $path $interface 2>/dev/null |
sed -e 's/^\.//' | { while read a b c d; do [[ "$a" == "$member" && "$c" != '-' ]] && echo " \"$c\""; done; };
}
_busctl() {

View file

@ -242,7 +242,7 @@ int fchmod_umask(int fd, mode_t m) {
}
int fchmod_opath(int fd, mode_t m) {
char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
/* This function operates also on fd that might have been opened with
* O_PATH. Indeed fchmodat() doesn't have the AT_EMPTY_PATH flag like

View file

@ -13,7 +13,7 @@
/* Modelled after Linux' lib/ratelimit.c by Dave Young
* <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
bool ratelimit_test(RateLimit *r) {
bool ratelimit_below(RateLimit *r) {
usec_t ts;
assert(r);

View file

@ -43,4 +43,4 @@ typedef struct RateLimit {
_r->begin = 0; \
} while (false)
bool ratelimit_test(RateLimit *r);
bool ratelimit_below(RateLimit *r);

View file

@ -230,7 +230,6 @@ static int mac_selinux_filter(sd_bus_message *message, void *userdata, sd_bus_er
path = sd_bus_message_get_path(message);
if (object_path_startswith("/org/freedesktop/systemd1", path)) {
r = mac_selinux_access_check(message, verb, error);
if (r < 0)
return r;
@ -258,7 +257,6 @@ static int mac_selinux_filter(sd_bus_message *message, void *userdata, sd_bus_er
else
manager_load_unit_from_dbus_path(m, path, NULL, &u);
}
if (!u)
return 0;

View file

@ -845,7 +845,7 @@ static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
if (revents != EPOLLIN) {
static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
if (ratelimit_test(&limit))
if (ratelimit_below(&limit))
log_warning("Failed to get udev event");
if (!(revents & EPOLLIN))
return 0;

View file

@ -2338,7 +2338,7 @@ static void manager_handle_ctrl_alt_del(Manager *m) {
* 7 times within 2s, we reboot/shutdown immediately,
* unless it was disabled in system.conf */
if (ratelimit_test(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE)
if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE)
manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
else
emergency_action(m, m->cad_burst_action, NULL,
@ -2633,7 +2633,7 @@ int manager_loop(Manager *m) {
if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY && MANAGER_IS_SYSTEM(m))
watchdog_ping();
if (!ratelimit_test(&rl)) {
if (!ratelimit_below(&rl)) {
/* Yay, something is going seriously wrong, pause a little */
log_warning("Looping too fast. Throttling execution a little.");
sleep(1);
@ -2697,12 +2697,19 @@ int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e,
return 0;
}
return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID, "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.", SD_ID128_FORMAT_VAL(invocation_id));
return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID,
"No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.",
SD_ID128_FORMAT_VAL(invocation_id));
}
/* If this didn't work, we check if this is a unit name */
if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is neither a valid invocation ID nor unit name.", n);
if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
_cleanup_free_ char *nn = NULL;
nn = cescape(n);
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS,
"Unit name %s is neither a valid invocation ID nor unit name.", strnull(nn));
}
r = manager_load_unit(m, n, NULL, e, &u);
if (r < 0)

View file

@ -2245,7 +2245,7 @@ static void socket_enter_running(Socket *s, int cfd) {
return;
}
if (!ratelimit_test(&s->trigger_limit)) {
if (!ratelimit_below(&s->trigger_limit)) {
safe_close(cfd);
log_unit_warning(UNIT(s), "Trigger limit hit, refusing further activation.");
socket_enter_stop_pre(s, SOCKET_FAILURE_TRIGGER_LIMIT_HIT);

View file

@ -1696,7 +1696,7 @@ void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t) {
int unit_start_limit_test(Unit *u) {
assert(u);
if (ratelimit_test(&u->start_limit)) {
if (ratelimit_below(&u->start_limit)) {
u->start_limit_hit = false;
return 0;
}
@ -1989,7 +1989,7 @@ static void unit_check_unneeded(Unit *u) {
/* If stopping a unit fails continuously we might enter a stop
* loop here, hence stop acting on the service being
* unnecessary after a while. */
if (!ratelimit_test(&u->auto_stop_ratelimit)) {
if (!ratelimit_below(&u->auto_stop_ratelimit)) {
log_unit_warning(u, "Unit not needed anymore, but not stopping since we tried this too often recently.");
return;
}
@ -2039,7 +2039,7 @@ static void unit_check_binds_to(Unit *u) {
/* If stopping a unit fails continuously we might enter a stop
* loop here, hence stop acting on the service being
* unnecessary after a while. */
if (!ratelimit_test(&u->auto_stop_ratelimit)) {
if (!ratelimit_below(&u->auto_stop_ratelimit)) {
log_unit_warning(u, "Unit is bound to inactive unit %s, but not stopping since we tried this too often recently.", other->id);
return;
}

View file

@ -126,7 +126,7 @@ static void raw_export_report_progress(RawExport *e) {
if (percent == e->last_percent)
return;
if (!ratelimit_test(&e->progress_rate_limit))
if (!ratelimit_below(&e->progress_rate_limit))
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);

View file

@ -134,7 +134,7 @@ static void tar_export_report_progress(TarExport *e) {
if (percent == e->last_percent)
return;
if (!ratelimit_test(&e->progress_rate_limit))
if (!ratelimit_below(&e->progress_rate_limit))
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);

View file

@ -149,7 +149,7 @@ static void raw_import_report_progress(RawImport *i) {
if (percent == i->last_percent)
return;
if (!ratelimit_test(&i->progress_rate_limit))
if (!ratelimit_below(&i->progress_rate_limit))
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);

View file

@ -156,7 +156,7 @@ static void tar_import_report_progress(TarImport *i) {
if (percent == i->last_percent)
return;
if (!ratelimit_test(&i->progress_rate_limit))
if (!ratelimit_below(&i->progress_rate_limit))
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);

View file

@ -221,7 +221,7 @@ static int dns_scope_emit_one(DnsScope *s, int fd, DnsPacket *p) {
if (DNS_PACKET_QDCOUNT(p) > 1)
return -EOPNOTSUPP;
if (!ratelimit_test(&s->ratelimit))
if (!ratelimit_below(&s->ratelimit))
return -EBUSY;
family = s->family;
@ -246,7 +246,7 @@ static int dns_scope_emit_one(DnsScope *s, int fd, DnsPacket *p) {
case DNS_PROTOCOL_MDNS:
assert(fd < 0);
if (!ratelimit_test(&s->ratelimit))
if (!ratelimit_below(&s->ratelimit))
return -EBUSY;
family = s->family;
@ -759,7 +759,7 @@ void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
} else {
int fd;
if (!ratelimit_test(&s->ratelimit))
if (!ratelimit_below(&s->ratelimit))
return;
if (p->family == AF_INET)

View file

@ -251,7 +251,7 @@ static int mdns_scope_process_query(DnsScope *s, DnsPacket *p) {
if (r < 0)
return log_debug_errno(r, "Failed to build reply packet: %m");
if (!ratelimit_test(&s->ratelimit))
if (!ratelimit_below(&s->ratelimit))
return 0;
r = dns_scope_emit_udp(s, -1, reply);

View file

@ -11,24 +11,24 @@
#include "ratelimit.h"
#include "time-util.h"
static void test_ratelimit_test(void) {
static void test_ratelimit_below(void) {
int i;
RATELIMIT_DEFINE(ratelimit, 1 * USEC_PER_SEC, 10);
for (i = 0; i < 10; i++)
assert_se(ratelimit_test(&ratelimit));
assert_se(!ratelimit_test(&ratelimit));
assert_se(ratelimit_below(&ratelimit));
assert_se(!ratelimit_below(&ratelimit));
sleep(1);
for (i = 0; i < 10; i++)
assert_se(ratelimit_test(&ratelimit));
assert_se(ratelimit_below(&ratelimit));
RATELIMIT_INIT(ratelimit, 0, 10);
for (i = 0; i < 10000; i++)
assert_se(ratelimit_test(&ratelimit));
assert_se(ratelimit_below(&ratelimit));
}
int main(int argc, char *argv[]) {
test_ratelimit_test();
test_ratelimit_below();
return 0;
}

View file

@ -804,7 +804,7 @@ int manager_connect(Manager *m) {
manager_disconnect(m);
m->event_retry = sd_event_source_unref(m->event_retry);
if (!ratelimit_test(&m->ratelimit)) {
if (!ratelimit_below(&m->ratelimit)) {
log_debug("Slowing down attempts to contact servers.");
r = sd_event_add_time(m->event, &m->event_retry, clock_boottime_or_monotonic(), now(clock_boottime_or_monotonic()) + RETRY_USEC, 0, manager_retry_connect, m);

View file

@ -1307,7 +1307,7 @@ static int item_do(Item *i, int fd, const struct stat *st, fdaction_t action) {
r = action(i, fd, st);
if (S_ISDIR(st->st_mode)) {
char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;