[systemd-commits] 2 commits - TODO src/libsystemd-bus src/login src/machine src/shared src/systemd src/test
Lennart Poettering
lennart at kemper.freedesktop.org
Wed Nov 20 16:03:50 PST 2013
TODO | 4 -
src/libsystemd-bus/libsystemd-bus.sym | 4 +
src/libsystemd-bus/sd-bus.c | 72 ++++++++++++++++++++++++++++++++++
src/libsystemd-bus/sd-event.c | 32 ++++++++++-----
src/libsystemd-bus/test-bus-marshal.c | 25 +++++++++++
src/login/logind-seat-dbus.c | 4 -
src/login/logind-session-dbus.c | 4 -
src/machine/machine-dbus.c | 4 -
src/shared/unit-name.c | 5 +-
src/shared/util.c | 72 ----------------------------------
src/shared/util.h | 3 -
src/systemd/sd-bus.h | 3 +
src/test/test-util.c | 24 -----------
13 files changed, 135 insertions(+), 121 deletions(-)
New commits:
commit 28383ba18963cdedd98ced271b3425f7321119b7
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Nov 21 01:03:26 2013 +0100
bus: add API calls to escape string components of objects paths
diff --git a/TODO b/TODO
index 454d283..1617f45 100644
--- a/TODO
+++ b/TODO
@@ -72,8 +72,6 @@ Features:
* sd-bus: support "const" properties as flag
-* sd-bus: add api call to escape bus path components
-
* sd-event: when a handler returns an error, just turn off its event
source, but do not return anything up to the event loop
caller. Instead add parameter to sd_event_request_quit() to take
diff --git a/src/libsystemd-bus/libsystemd-bus.sym b/src/libsystemd-bus/libsystemd-bus.sym
index 8fa2cde..a8df513 100644
--- a/src/libsystemd-bus/libsystemd-bus.sym
+++ b/src/libsystemd-bus/libsystemd-bus.sym
@@ -187,6 +187,10 @@ global:
sd_bus_error_is_set;
sd_bus_error_has_name;
+ /* Escaping */
+ sd_bus_label_escape;
+ sd_bus_label_unescape;
+
/* sd-memfd functions */
sd_memfd_new;
sd_memfd_make;
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 72d04d2..0ae5256 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -2712,3 +2712,75 @@ _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
return -ENXIO;
}
+
+_public_ char *sd_bus_label_escape(const char *s) {
+ char *r, *t;
+ const char *f;
+
+ assert_return(s, NULL);
+
+ /* Escapes all chars that D-Bus' object path cannot deal
+ * with. Can be reversed with bus_path_unescape(). We special
+ * case the empty string. */
+
+ if (*s == 0)
+ return strdup("_");
+
+ r = new(char, strlen(s)*3 + 1);
+ if (!r)
+ return NULL;
+
+ for (f = s, t = r; *f; f++) {
+
+ /* Escape everything that is not a-zA-Z0-9. We also
+ * escape 0-9 if it's the first character */
+
+ if (!(*f >= 'A' && *f <= 'Z') &&
+ !(*f >= 'a' && *f <= 'z') &&
+ !(f > s && *f >= '0' && *f <= '9')) {
+ *(t++) = '_';
+ *(t++) = hexchar(*f >> 4);
+ *(t++) = hexchar(*f);
+ } else
+ *(t++) = *f;
+ }
+
+ *t = 0;
+
+ return r;
+}
+
+_public_ char *sd_bus_label_unescape(const char *f) {
+ char *r, *t;
+
+ assert_return(f, NULL);
+
+ /* Special case for the empty string */
+ if (streq(f, "_"))
+ return strdup("");
+
+ r = new(char, strlen(f) + 1);
+ if (!r)
+ return NULL;
+
+ for (t = r; *f; f++) {
+
+ if (*f == '_') {
+ int a, b;
+
+ if ((a = unhexchar(f[1])) < 0 ||
+ (b = unhexchar(f[2])) < 0) {
+ /* Invalid escape code, let's take it literal then */
+ *(t++) = '_';
+ } else {
+ *(t++) = (char) ((a << 4) | b);
+ f += 2;
+ }
+ } else
+ *(t++) = *f;
+ }
+
+ *t = 0;
+
+ return r;
+}
diff --git a/src/libsystemd-bus/test-bus-marshal.c b/src/libsystemd-bus/test-bus-marshal.c
index b7606d7..98737b6 100644
--- a/src/libsystemd-bus/test-bus-marshal.c
+++ b/src/libsystemd-bus/test-bus-marshal.c
@@ -39,6 +39,29 @@
#include "bus-util.h"
#include "bus-dump.h"
+static void test_bus_label_escape_one(const char *a, const char *b) {
+ _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL;
+
+ assert_se(t = sd_bus_label_escape(a));
+ assert_se(streq(t, b));
+
+ assert_se(x = sd_bus_label_unescape(t));
+ assert_se(streq(a, x));
+
+ assert_se(y = sd_bus_label_unescape(b));
+ assert_se(streq(a, y));
+}
+
+static void test_bus_label_escape(void) {
+ test_bus_label_escape_one("foo123bar", "foo123bar");
+ test_bus_label_escape_one("foo.bar", "foo_2ebar");
+ test_bus_label_escape_one("foo_2ebar", "foo_5f2ebar");
+ test_bus_label_escape_one("", "_");
+ test_bus_label_escape_one("_", "_5f");
+ test_bus_label_escape_one("1", "_31");
+ test_bus_label_escape_one(":1", "_3a1");
+}
+
int main(int argc, char *argv[]) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *copy = NULL;
int r, boolean;
@@ -294,5 +317,7 @@ int main(int argc, char *argv[]) {
assert_se(streq(c, "ccc"));
assert_se(streq(d, "3"));
+ test_bus_label_escape();
+
return 0;
}
diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c
index 53141a8..268cd99 100644
--- a/src/login/logind-seat-dbus.c
+++ b/src/login/logind-seat-dbus.c
@@ -294,7 +294,7 @@ int seat_object_find(sd_bus *bus, const char *path, const char *interface, void
if (!p)
return 0;
- e = bus_path_unescape(p);
+ e = sd_bus_label_unescape(p);
if (!e)
return -ENOMEM;
@@ -312,7 +312,7 @@ char *seat_bus_path(Seat *s) {
assert(s);
- t = bus_path_escape(s->id);
+ t = sd_bus_label_escape(s->id);
if (!t)
return NULL;
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
index 4995391..cebdfe3 100644
--- a/src/login/logind-session-dbus.c
+++ b/src/login/logind-session-dbus.c
@@ -499,7 +499,7 @@ int session_object_find(sd_bus *bus, const char *path, const char *interface, vo
if (!p)
return 0;
- e = bus_path_unescape(p);
+ e = sd_bus_label_unescape(p);
if (!e)
return -ENOMEM;
@@ -517,7 +517,7 @@ char *session_bus_path(Session *s) {
assert(s);
- t = bus_path_escape(s->id);
+ t = sd_bus_label_escape(s->id);
if (!t)
return NULL;
diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c
index db379d2..7f3b486 100644
--- a/src/machine/machine-dbus.c
+++ b/src/machine/machine-dbus.c
@@ -176,7 +176,7 @@ int machine_object_find(sd_bus *bus, const char *path, const char *interface, vo
if (!p)
return 0;
- e = bus_path_unescape(p);
+ e = sd_bus_label_unescape(p);
if (!e)
return -ENOMEM;
@@ -194,7 +194,7 @@ char *machine_bus_path(Machine *m) {
assert(m);
- e = bus_path_escape(m->name);
+ e = sd_bus_label_escape(m->name);
if (!e)
return NULL;
diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c
index bc8094d..2335463 100644
--- a/src/shared/unit-name.c
+++ b/src/shared/unit-name.c
@@ -23,6 +23,7 @@
#include <string.h>
#include <assert.h>
+#include "sd-bus.h"
#include "path-util.h"
#include "util.h"
#include "unit-name.h"
@@ -459,7 +460,7 @@ char *unit_dbus_path_from_name(const char *name) {
assert(name);
- e = bus_path_escape(name);
+ e = sd_bus_label_escape(name);
if (!e)
return NULL;
@@ -474,7 +475,7 @@ int unit_name_from_dbus_path(const char *path, char **name) {
if (!e)
return -EINVAL;
- n = bus_path_unescape(e);
+ n = sd_bus_label_unescape(e);
if (!n)
return -ENOMEM;
diff --git a/src/shared/util.c b/src/shared/util.c
index deb74c4..97c9497 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -1356,78 +1356,6 @@ char *xescape(const char *s, const char *bad) {
return r;
}
-char *bus_path_escape(const char *s) {
- char *r, *t;
- const char *f;
-
- assert(s);
-
- /* Escapes all chars that D-Bus' object path cannot deal
- * with. Can be reversed with bus_path_unescape(). We special
- * case the empty string. */
-
- if (*s == 0)
- return strdup("_");
-
- r = new(char, strlen(s)*3 + 1);
- if (!r)
- return NULL;
-
- for (f = s, t = r; *f; f++) {
-
- /* Escape everything that is not a-zA-Z0-9. We also
- * escape 0-9 if it's the first character */
-
- if (!(*f >= 'A' && *f <= 'Z') &&
- !(*f >= 'a' && *f <= 'z') &&
- !(f > s && *f >= '0' && *f <= '9')) {
- *(t++) = '_';
- *(t++) = hexchar(*f >> 4);
- *(t++) = hexchar(*f);
- } else
- *(t++) = *f;
- }
-
- *t = 0;
-
- return r;
-}
-
-char *bus_path_unescape(const char *f) {
- char *r, *t;
-
- assert(f);
-
- /* Special case for the empty string */
- if (streq(f, "_"))
- return strdup("");
-
- r = new(char, strlen(f) + 1);
- if (!r)
- return NULL;
-
- for (t = r; *f; f++) {
-
- if (*f == '_') {
- int a, b;
-
- if ((a = unhexchar(f[1])) < 0 ||
- (b = unhexchar(f[2])) < 0) {
- /* Invalid escape code, let's take it literal then */
- *(t++) = '_';
- } else {
- *(t++) = (char) ((a << 4) | b);
- f += 2;
- }
- } else
- *(t++) = *f;
- }
-
- *t = 0;
-
- return r;
-}
-
char *ascii_strlower(char *t) {
char *p;
diff --git a/src/shared/util.h b/src/shared/util.h
index d46ff27..00d2364 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -237,9 +237,6 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre
char *xescape(const char *s, const char *bad);
-char *bus_path_escape(const char *s);
-char *bus_path_unescape(const char *s);
-
char *ascii_strlower(char *path);
bool dirent_is_file(const struct dirent *de) _pure_;
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index a09b39f..fb7925c 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -282,6 +282,9 @@ int sd_bus_error_has_name(const sd_bus_error *e, const char *name);
&(x).bytes[8], &(x).bytes[9], &(x).bytes[10], &(x).bytes[11], \
&(x).bytes[12], &(x).bytes[13], &(x).bytes[14], &(x).bytes[15]
+char *sd_bus_label_escape(const char *s);
+char *sd_bus_label_unescape(const char *f);
+
_SD_END_DECLARATIONS;
#endif
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 7fd0572..afc3247 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -351,29 +351,6 @@ static void test_memdup_multiply(void) {
free(dup);
}
-static void test_bus_path_escape_one(const char *a, const char *b) {
- _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL;
-
- assert_se(t = bus_path_escape(a));
- assert_se(streq(t, b));
-
- assert_se(x = bus_path_unescape(t));
- assert_se(streq(a, x));
-
- assert_se(y = bus_path_unescape(b));
- assert_se(streq(a, y));
-}
-
-static void test_bus_path_escape(void) {
- test_bus_path_escape_one("foo123bar", "foo123bar");
- test_bus_path_escape_one("foo.bar", "foo_2ebar");
- test_bus_path_escape_one("foo_2ebar", "foo_5f2ebar");
- test_bus_path_escape_one("", "_");
- test_bus_path_escape_one("_", "_5f");
- test_bus_path_escape_one("1", "_31");
- test_bus_path_escape_one(":1", "_3a1");
-}
-
static void test_hostname_is_valid(void) {
assert(hostname_is_valid("foobar"));
assert(hostname_is_valid("foobar.com"));
@@ -616,7 +593,6 @@ int main(int argc, char *argv[]) {
test_foreach_word_quoted();
test_default_term_for_tty();
test_memdup_multiply();
- test_bus_path_escape();
test_hostname_is_valid();
test_u64log2();
test_get_process_comm();
commit 850516e012f0e2c96cecb63c1e8997e13912fcd6
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Nov 21 00:46:13 2013 +0100
sd-event: try to move timer wakeups to the same spot within each minute
diff --git a/TODO b/TODO
index 21996ea..454d283 100644
--- a/TODO
+++ b/TODO
@@ -49,8 +49,6 @@ Features:
* timer: expose accuracy as unit setting
-* sd-event: do per-minute coalescing of timer events too
-
* when we detect low battery and no AC on boot, show pretty splash and refuse boot
* move libasyncns into systemd as libsystemd-asyncns
diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c
index 2dc3672..2a8eac6 100644
--- a/src/libsystemd-bus/sd-event.c
+++ b/src/libsystemd-bus/sd-event.c
@@ -640,15 +640,16 @@ static int event_setup_timer_fd(
}
/* When we sleep for longer, we try to realign the wakeup to
- the same time wihtin each second, so that events all across
- the system can be coalesced into a single CPU
- wakeup. However, let's take some system-specific randomness
- for this value, so that in a network of systems with synced
- clocks timer events are distributed a bit. Here, we
- calculate a perturbation usec offset from the boot ID. */
+ the same time wihtin each minute/second/250ms, so that
+ events all across the system can be coalesced into a single
+ CPU wakeup. However, let's take some system-specific
+ randomness for this value, so that in a network of systems
+ with synced clocks timer events are distributed a
+ bit. Here, we calculate a perturbation usec offset from the
+ boot ID. */
if (sd_id128_get_boot(&bootid) >= 0)
- e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_SEC;
+ e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
*timer_fd = fd;
return 0;
@@ -1363,13 +1364,24 @@ static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
dispatch as much as possible on the entire system.
We implement this by waking up everywhere at the same time
- within any given second if we can, synchronised via the
+ within any given minute if we can, synchronised via the
perturbation value determined from the boot ID. If we can't,
- then we try to find the same spot in every a 250ms
+ then we try to find the same spot in every 1s and then 250ms
step. Otherwise, we pick the last possible time to wake up.
*/
- c = (b / USEC_PER_SEC) * USEC_PER_SEC + e->perturb;
+ c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
+ if (c >= b) {
+ if (_unlikely_(c < USEC_PER_MINUTE))
+ return b;
+
+ c -= USEC_PER_MINUTE;
+ }
+
+ if (c >= a)
+ return c;
+
+ c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
if (c >= b) {
if (_unlikely_(c < USEC_PER_SEC))
return b;
More information about the systemd-commits
mailing list