[systemd-commits] 10 commits - Makefile.am man/sd_journal_get_cutoff_realtime_usec.xml src/core src/journal src/locale src/login src/machine src/nspawn src/resolve src/shared src/test
Zbigniew JÄdrzejewski-Szmek
zbyszek at kemper.freedesktop.org
Fri Jul 18 18:53:10 PDT 2014
Makefile.am | 12 +-
man/sd_journal_get_cutoff_realtime_usec.xml | 36 +++++---
src/core/manager.c | 2
src/core/service.c | 6 +
src/journal/compress.c | 4
src/journal/coredumpctl.c | 3
src/journal/journald-native.c | 11 +-
src/journal/journald-stream.c | 4
src/journal/journald-syslog.c | 2
src/journal/sd-journal.c | 16 +--
src/locale/generate-kbd-model-map | 31 -------
src/login/logind-acl.c | 7 +
src/login/logind-session.h | 2
src/machine/machinectl.c | 2
src/nspawn/nspawn.c | 4
src/resolve/resolved-dns-scope.c | 7 -
src/shared/ask-password-api.c | 2
src/shared/barrier.c | 114 ++++++++++------------------
src/shared/barrier.h | 4
src/shared/pty.c | 3
src/test/test-barrier.c | 4
21 files changed, 114 insertions(+), 162 deletions(-)
New commits:
commit a7850c7d1339b490ac021ff82c2081285ea28503
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Dec 27 17:14:24 2013 -0500
core: show timeouts when watchdog howls
diff --git a/src/core/service.c b/src/core/service.c
index 0f542ed..6a4665a 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -2551,11 +2551,15 @@ static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *us
static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
Service *s = SERVICE(userdata);
+ char t[FORMAT_TIMESPAN_MAX];
assert(s);
assert(source == s->watchdog_event_source);
- log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
+ log_error_unit(UNIT(s)->id,
+ "%s watchdog timeout (limit %s)!",
+ UNIT(s)->id,
+ format_timespan(t, sizeof(t), s->watchdog_usec, 1));
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
return 0;
commit f7a5bb2842037fa27dbc99d92c3fee7fe1bbbc2a
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 14:01:13 2014 -0400
Small modernizations
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index d1450c0..e5f4d45 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -131,11 +131,10 @@ static int add_match(Set *set, const char *match) {
goto fail;
log_debug("Adding pattern: %s", pattern);
- r = set_put(set, pattern);
+ r = set_consume(set, pattern);
if (r < 0) {
log_error("Failed to add pattern '%s': %s",
pattern, strerror(-r));
- free(pattern);
goto fail;
}
diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c
index 0f3936a..d73280c 100644
--- a/src/journal/journald-native.c
+++ b/src/journal/journald-native.c
@@ -383,12 +383,15 @@ void server_process_native_file(
}
int server_open_native_socket(Server*s) {
- union sockaddr_union sa;
int one, r;
assert(s);
if (s->native_fd < 0) {
+ union sockaddr_union sa = {
+ .un.sun_family = AF_UNIX,
+ .un.sun_path = "/run/systemd/journal/socket",
+ };
s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (s->native_fd < 0) {
@@ -396,10 +399,6 @@ int server_open_native_socket(Server*s) {
return -errno;
}
- zero(sa);
- sa.un.sun_family = AF_UNIX;
- strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
-
unlink(sa.un.sun_path);
r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
diff --git a/src/login/logind-acl.c b/src/login/logind-acl.c
index af7c352..b76e16d 100644
--- a/src/login/logind-acl.c
+++ b/src/login/logind-acl.c
@@ -284,7 +284,7 @@ int devnode_acl_all(struct udev *udev,
k = devnode_acl(n, flush, del, old_uid, add, new_uid);
if (k == -ENOENT)
log_debug("Device %s disappeared while setting ACLs", n);
- else if (k < 0)
+ else if (k < 0 && r == 0)
r = k;
}
diff --git a/src/login/logind-session.h b/src/login/logind-session.h
index 81957df..e62b76d 100644
--- a/src/login/logind-session.h
+++ b/src/login/logind-session.h
@@ -72,7 +72,7 @@ enum KillWho {
struct Session {
Manager *manager;
- char *id;
+ const char *id;
unsigned int pos;
SessionType type;
SessionClass class;
commit 6b9732b2bf0499c5e4ea8a9d4f6051d98033f680
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Mon Mar 3 19:49:40 2014 -0500
Be more verbose when bind or listen fails
Also be more verbose in devnode_acl_all().
diff --git a/src/core/manager.c b/src/core/manager.c
index e7a3a2a..9d9a713 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -554,7 +554,7 @@ static int manager_setup_notify(Manager *m) {
strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)-1);
r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
- log_error("bind() failed: %m");
+ log_error("bind(@%s) failed: %m", sa.un.sun_path+1);
return -errno;
}
diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c
index 6674f3b..0f3936a 100644
--- a/src/journal/journald-native.c
+++ b/src/journal/journald-native.c
@@ -404,7 +404,7 @@ int server_open_native_socket(Server*s) {
r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
- log_error("bind() failed: %m");
+ log_error("bind(%s) failed: %m", sa.un.sun_path);
return -errno;
}
diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
index 89da150..8a983d8 100644
--- a/src/journal/journald-stream.c
+++ b/src/journal/journald-stream.c
@@ -450,14 +450,14 @@ int server_open_stdout_socket(Server *s) {
r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
- log_error("bind() failed: %m");
+ log_error("bind(%s) failed: %m", sa.un.sun_path);
return -errno;
}
chmod(sa.un.sun_path, 0666);
if (listen(s->stdout_fd, SOMAXCONN) < 0) {
- log_error("listen() failed: %m");
+ log_error("listen(%s) failed: %m", sa.un.sun_path);
return -errno;
}
} else
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
index afeb8bd..656dc72 100644
--- a/src/journal/journald-syslog.c
+++ b/src/journal/journald-syslog.c
@@ -441,7 +441,7 @@ int server_open_syslog_socket(Server *s) {
r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
- log_error("bind() failed: %m");
+ log_error("bind(%s) failed: %m", sa.un.sun_path);
return -errno;
}
diff --git a/src/login/logind-acl.c b/src/login/logind-acl.c
index 4bbeb64..af7c352 100644
--- a/src/login/logind-acl.c
+++ b/src/login/logind-acl.c
@@ -277,7 +277,10 @@ int devnode_acl_all(struct udev *udev,
SET_FOREACH(n, nodes, i) {
int k;
- log_debug("Fixing up ACLs at %s for seat %s", n, seat);
+ log_debug("Changing ACLs at %s for seat %s (uid "UID_FMT"â"UID_FMT"%s%s)",
+ n, seat, old_uid, new_uid,
+ del ? " del" : "", add ? " add" : "");
+
k = devnode_acl(n, flush, del, old_uid, add, new_uid);
if (k == -ENOENT)
log_debug("Device %s disappeared while setting ACLs", n);
diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c
index 5997a03..8d03f4a 100644
--- a/src/shared/ask-password-api.c
+++ b/src/shared/ask-password-api.c
@@ -270,7 +270,7 @@ static int create_socket(char **name) {
if (r < 0) {
r = -errno;
- log_error("bind() failed: %m");
+ log_error("bind(%s) failed: %m", sa.un.sun_path);
goto fail;
}
commit 3fb97a58fa3f233cc980cdc4ae33230a361b3c34
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 21:44:59 2014 -0400
Nuke update-kbd-map
Our version has evolved independently of the original table
in systemd-config-keyboard, so it cannot be ever regenerated from
original upstream. Remove script to avoid confusion.
diff --git a/Makefile.am b/Makefile.am
index 15a38bd..8d6c317 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -4438,9 +4438,6 @@ dist_pkgdata_DATA += \
dist_noinst_SCRIPT = \
src/locale/generate-kbd-model-map
-update-kbd-model-map: src/locale/generate-kbd-model-map
- $PYTHON $< >src/locale/kbd-model-map
-
localectl_SOURCES = \
src/locale/localectl.c
diff --git a/src/locale/generate-kbd-model-map b/src/locale/generate-kbd-model-map
deleted file mode 100755
index b8ffa0f..0000000
--- a/src/locale/generate-kbd-model-map
+++ /dev/null
@@ -1,31 +0,0 @@
-import sys
-import system_config_keyboard.keyboard_models
-
-def strdash(s):
- return s.strip() or '-'
-
-def tab_extend(s, n=1):
- s = strdash(s)
- k = len(s) // 8
-
- if k >= n:
- f = 1
- else:
- f = n - k
-
- return s + '\t'*f
-
-
-models = system_config_keyboard.keyboard_models.KeyboardModels().get_models()
-
-print "# Generated from system-config-keyboard's model list"
-print "# consolelayout\t\txlayout\txmodel\t\txvariant\txoptions"
-
-for key, value in reversed(models.items()):
- options = "terminate:ctrl_alt_bksp"
- if value[4]:
- options += ',' + value[4]
-
- print ''.join((tab_extend(key, 3), tab_extend(value[1]),
- tab_extend(value[2], 2), tab_extend(value[3], 2),
- options))
commit e091457e822ff35ff7b1d3b3a1f91c2ee6249e5a
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 21:44:58 2014 -0400
Makefile.am: tweaks to python commands
diff --git a/Makefile.am b/Makefile.am
index f0482cf..15a38bd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2898,11 +2898,12 @@ test_unifont_LDADD = \
libsystemd-internal.la \
libsystemd-shared.la
-update-unifont:
- $(AM_V_GEN)cat $(top_srcdir)/src/libsystemd-terminal/unifont.hex | $(PYTHON) $(top_srcdir)/tools/compile-unifont.py >$(top_srcdir)/src/libsystemd-terminal/unifont-glyph-array.bin
- @echo "unifont-glyph-array.bin has been regenerated"
-
.PHONY: update-unifont
+update-unifont: tools/compile-unifont.py
+ $(AM_V_GEN)$(PYTHON) $< \
+ <$(top_srcdir)/src/libsystemd-terminal/unifont.hex \
+ >$(top_srcdir)/src/libsystemd-terminal/unifont-glyph-array.bin
+ @echo "unifont-glyph-array.bin has been regenerated"
# ------------------------------------------------------------------------------
if ENABLE_GTK_DOC
commit e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26ef
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 21:44:58 2014 -0400
machinectl: make sure we are not reading an unitialized variable
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index cc3be98..574c988 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -376,6 +376,8 @@ static int map_netif(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_
r = sd_bus_message_read_array(m, SD_BUS_TYPE_INT32, &v, &l);
if (r < 0)
return r;
+ if (r == 0)
+ return -EBADMSG;
i->n_netif = l / sizeof(int32_t);
i->netif = memdup(v, l);
commit 1651e2c61e544de9ca947c8b3202552b1272ef57
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 21:44:36 2014 -0400
man,journal: add note about sd_journal_get_cutoff_monotonic_usec return value
Also modify the function itself to be a bit simpler to read.
diff --git a/man/sd_journal_get_cutoff_realtime_usec.xml b/man/sd_journal_get_cutoff_realtime_usec.xml
index 0b24399..673cff4 100644
--- a/man/sd_journal_get_cutoff_realtime_usec.xml
+++ b/man/sd_journal_get_cutoff_realtime_usec.xml
@@ -74,25 +74,29 @@
<title>Description</title>
<para><function>sd_journal_get_cutoff_realtime_usec()</function>
- gets the realtime (wallclock) timestamps of the first
- and last entries accessible in the journal. It takes
- three arguments: the journal context object and two
- pointers to 64-bit unsigned integers to store the
- timestamps in. The timestamps are in microseconds
- since the epoch,
+ retrieves the realtime (wallclock) timestamps of the
+ first and last entries accessible in the journal. It
+ takes three arguments: the journal context object
+ <parameter>j</parameter> and two pointers
+ <parameter>from</parameter> and
+ <parameter>to</parameter> pointing at 64-bit unsigned
+ integers to store the timestamps in. The timestamps
+ are in microseconds since the epoch,
i.e. <constant>CLOCK_REALTIME</constant>. Either one
of the two timestamp arguments may be passed as
<constant>NULL</constant> in case the timestamp is not
needed, but not both.</para>
<para><function>sd_journal_get_cutoff_monotonic_usec()</function>
- gets the monotonic timestamps of the first and last
- entries accessible in the journal. It takes three
- arguments: the journal context object, a 128-bit
- identifier for the boot, and two pointers to 64-bit
- unsigned integers to store the timestamps. The
- timestamps are in microseconds since boot-up of the
- specific boot,
+ retrieves the monotonic timestamps of the first and
+ last entries accessible in the journal. It takes three
+ arguments: the journal context object
+ <parameter>j</parameter>, a 128-bit identifier for the
+ boot <parameter>boot_id</parameter>, and two pointers
+ to 64-bit unsigned integers to store the timestamps,
+ <parameter>from</parameter> and
+ <parameter>to</parameter>. The timestamps are in
+ microseconds since boot-up of the specific boot,
i.e. <constant>CLOCK_MONOTONIC</constant>. Since the
monotonic clock begins new with every reboot it only
defines a well-defined point in time when used
@@ -113,6 +117,12 @@
<function>sd_journal_get_cutoff_monotonic_usec()</function>
return 1 on success, 0 if not suitable entries are in
the journal or a negative errno-style error code.</para>
+
+ <para>Locations pointed to by parameters
+ <parameter>from</parameter> and
+ <parameter>to</parameter> will be set only if the
+ return value is positive, and obviously, the
+ parameters are non-null.</para>
</refsect1>
<refsect1>
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index f674abf..6349aeb 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -2399,7 +2399,7 @@ _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from,
_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
Iterator i;
JournalFile *f;
- bool first = true;
+ bool found = false;
int r;
assert_return(j, -EINVAL);
@@ -2418,21 +2418,21 @@ _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot
if (r == 0)
continue;
- if (first) {
+ if (found) {
if (from)
- *from = fr;
+ *from = MIN(fr, *from);
if (to)
- *to = t;
- first = false;
+ *to = MAX(t, *to);
} else {
if (from)
- *from = MIN(fr, *from);
+ *from = fr;
if (to)
- *to = MAX(t, *to);
+ *to = t;
+ found = true;
}
}
- return first ? 0 : 1;
+ return found;
}
void journal_print_header(sd_journal *j) {
commit 01c3322e017989d25f7b4b51268245d5315ae678
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 21:44:36 2014 -0400
compress: fix return value
diff --git a/src/journal/compress.c b/src/journal/compress.c
index 316c1a6..ee18bc8 100644
--- a/src/journal/compress.c
+++ b/src/journal/compress.c
@@ -132,7 +132,7 @@ int decompress_blob_xz(const void *src, uint64_t src_size,
space = MIN(src_size * 2, dst_max ?: (uint64_t) -1);
if (!greedy_realloc(dst, dst_alloc_size, space, 1))
- return false;
+ return -ENOMEM;
s.next_in = src;
s.avail_in = src_size;
@@ -158,7 +158,7 @@ int decompress_blob_xz(const void *src, uint64_t src_size,
used = space - s.avail_out;
space = MIN(2 * space, dst_max ?: (uint64_t) -1);
if (!greedy_realloc(dst, dst_alloc_size, space, 1))
- return false;
+ return -ENOMEM;
s.avail_out = space - used;
s.next_out = *dst + used;
commit 901fd8164797f3eeb9921c85915dc409d49ab5d8
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 21:44:34 2014 -0400
resolved: do not use unitialized variable
diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c
index e74fcd4..190c5f4 100644
--- a/src/resolve/resolved-dns-scope.c
+++ b/src/resolve/resolved-dns-scope.c
@@ -182,7 +182,7 @@ int dns_scope_tcp_socket(DnsScope *s) {
_cleanup_close_ int fd = -1;
union sockaddr_union sa = {};
socklen_t salen;
- int one, ifindex, ret;
+ int one, ret;
DnsServer *srv;
int r;
@@ -192,9 +192,6 @@ int dns_scope_tcp_socket(DnsScope *s) {
if (!srv)
return -ESRCH;
- if (s->link)
- ifindex = s->link->ifindex;
-
sa.sa.sa_family = srv->family;
if (srv->family == AF_INET) {
sa.in.sin_port = htobe16(53);
@@ -203,7 +200,7 @@ int dns_scope_tcp_socket(DnsScope *s) {
} else if (srv->family == AF_INET6) {
sa.in6.sin6_port = htobe16(53);
sa.in6.sin6_addr = srv->address.in6;
- sa.in6.sin6_scope_id = ifindex;
+ sa.in6.sin6_scope_id = s->link ? s->link->ifindex : 0;
salen = sizeof(sa.in6);
} else
return -EAFNOSUPPORT;
commit 7566e26721ee95d6fc864e9e6654fb61bd3cd603
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Jul 18 20:12:13 2014 -0400
barrier: initalize file descriptors with -1
Explicitly initalize descriptors using explicit assignment like
bus_error. This makes barriers follow the same conventions as
everything else and makes things a bit simpler too.
Rename barier_init to barier_create so it is obvious that it is
not about initialization.
Remove some parens, etc.
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 657512d..7c47f6e 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -3073,13 +3073,13 @@ int main(int argc, char *argv[]) {
for (;;) {
ContainerStatus container_status;
- _cleanup_(barrier_destroy) Barrier barrier = { };
+ _cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL;
struct sigaction sa = {
.sa_handler = nop_handler,
.sa_flags = SA_NOCLDSTOP,
};
- r = barrier_init(&barrier);
+ r = barrier_create(&barrier);
if (r < 0) {
log_error("Cannot initialize IPC barrier: %s", strerror(-r));
goto finish;
diff --git a/src/shared/barrier.c b/src/shared/barrier.c
index c198329..4ac42d0 100644
--- a/src/shared/barrier.c
+++ b/src/shared/barrier.c
@@ -93,7 +93,7 @@
*/
/**
- * barrier_init() - Initialize a barrier object
+ * barrier_create() - Initialize a barrier object
* @obj: barrier to initialize
*
* This initializes a barrier object. The caller is responsible of allocating
@@ -111,26 +111,16 @@
*
* Returns: 0 on success, negative error code on failure.
*/
-int barrier_init(Barrier *obj) {
- _cleanup_(barrier_destroy) Barrier b = { };
- int r;
-
- assert_return(obj, -EINVAL);
-
- b.me = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
- if (b.me < 0)
- return -errno;
-
- b.them = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
- if (b.them < 0)
- return -errno;
+int barrier_create(Barrier *b) {
+ assert(b);
- r = pipe2(b.pipe, O_CLOEXEC | O_NONBLOCK);
- if (r < 0)
+ if ((b->me = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) < 0 ||
+ (b->them = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) < 0 ||
+ pipe2(b->pipe, O_CLOEXEC | O_NONBLOCK) < 0) {
+ barrier_destroy(b);
return -errno;
+ }
- memcpy(obj, &b, sizeof(b));
- zero(b);
return 0;
}
@@ -138,15 +128,11 @@ int barrier_init(Barrier *obj) {
* barrier_destroy() - Destroy a barrier object
* @b: barrier to destroy or NULL
*
- * This destroys a barrier object that has previously been initialized via
- * barrier_init(). The object is released and reset to invalid state.
- * Therefore, it is safe to call barrier_destroy() multiple times or even if
- * barrier_init() failed. However, you must not call barrier_destroy() if you
- * never called barrier_init() on the object before.
- *
- * It is safe to initialize a barrier via zero() / memset(.., 0, ...). Even
- * though it has embedded FDs, barrier_destroy() can deal with zeroed objects
- * just fine.
+ * This destroys a barrier object that has previously been passed to
+ * barrier_create(). The object is released and reset to invalid
+ * state. Therefore, it is safe to call barrier_destroy() multiple
+ * times or even if barrier_create() failed. However, barrier must be
+ * always initalized with BARRIER_NULL.
*
* If @b is NULL, this is a no-op.
*/
@@ -154,21 +140,9 @@ void barrier_destroy(Barrier *b) {
if (!b)
return;
- /* @me and @them cannot be both FD 0. Lets be pedantic and check the
- * pipes and barriers, too. If all are 0, the object was zero()ed and
- * is invalid. This allows users to use zero(barrier) to reset the
- * backing memory. */
- if (b->me == 0 &&
- b->them == 0 &&
- b->pipe[0] == 0 &&
- b->pipe[1] == 0 &&
- b->barriers == 0)
- return;
-
b->me = safe_close(b->me);
b->them = safe_close(b->them);
- b->pipe[0] = safe_close(b->pipe[0]);
- b->pipe[1] = safe_close(b->pipe[1]);
+ safe_close_pair(b->pipe);
b->barriers = 0;
}
@@ -177,13 +151,14 @@ void barrier_destroy(Barrier *b) {
* @b: barrier to operate on
* @role: role to set on the barrier
*
- * This sets the roles on a barrier object. This is needed to know which
- * side of the barrier you're on. Usually, the parent creates the barrier via
- * barrier_init() and then calls fork() or clone(). Therefore, the FDs are
- * duplicated and the child retains the same barrier object.
+ * This sets the roles on a barrier object. This is needed to know
+ * which side of the barrier you're on. Usually, the parent creates
+ * the barrier via barrier_create() and then calls fork() or clone().
+ * Therefore, the FDs are duplicated and the child retains the same
+ * barrier object.
*
- * Both sides need to call barrier_set_role() after fork() or clone() are done.
- * If this is not done, barriers will not work correctly.
+ * Both sides need to call barrier_set_role() after fork() or clone()
+ * are done. If this is not done, barriers will not work correctly.
*
* Note that barriers could be supported without fork() or clone(). However,
* this is currently not needed so it hasn't been implemented.
@@ -196,9 +171,9 @@ void barrier_set_role(Barrier *b, unsigned int role) {
/* make sure this is only called once */
assert(b->pipe[1] >= 0 && b->pipe[1] >= 0);
- if (role == BARRIER_PARENT) {
+ if (role == BARRIER_PARENT)
b->pipe[1] = safe_close(b->pipe[1]);
- } else {
+ else {
b->pipe[0] = safe_close(b->pipe[0]);
/* swap me/them for children */
@@ -218,7 +193,7 @@ static bool barrier_write(Barrier *b, uint64_t buf) {
do {
len = write(b->me, &buf, sizeof(buf));
- } while (len < 0 && (errno == EAGAIN || errno == EINTR));
+ } while (len < 0 && IN_SET(errno, EAGAIN, EINTR));
if (len != sizeof(buf))
goto error;
@@ -229,9 +204,8 @@ static bool barrier_write(Barrier *b, uint64_t buf) {
b->barriers = BARRIER_WE_ABORTED;
else
b->barriers = BARRIER_I_ABORTED;
- } else if (!barrier_is_aborted(b)) {
+ } else if (!barrier_is_aborted(b))
b->barriers += buf;
- }
return !barrier_i_aborted(b);
@@ -241,52 +215,48 @@ error:
* pipe-ends and treat this as abortion. The other end will notice the
* pipe-close and treat it as abortion, too. */
- b->pipe[0] = safe_close(b->pipe[0]);
- b->pipe[1] = safe_close(b->pipe[1]);
+ safe_close_pair(b->pipe);
b->barriers = BARRIER_WE_ABORTED;
return false;
}
/* waits for barriers; returns false if they aborted, otherwise true */
static bool barrier_read(Barrier *b, int64_t comp) {
- uint64_t buf;
- ssize_t len;
- struct pollfd pfd[2] = { };
- int r;
-
if (barrier_they_aborted(b))
return false;
while (b->barriers > comp) {
- pfd[0].fd = (b->pipe[0] >= 0) ? b->pipe[0] : b->pipe[1];
- pfd[0].events = POLLHUP;
- pfd[0].revents = 0;
- pfd[1].fd = b->them;
- pfd[1].events = POLLIN;
- pfd[1].revents = 0;
+ struct pollfd pfd[2] = {
+ { .fd = b->pipe[0] >= 0 ? b->pipe[0] : b->pipe[1],
+ .events = POLLHUP },
+ { .fd = b->them,
+ .events = POLLIN }};
+ uint64_t buf;
+ int r;
r = poll(pfd, 2, -1);
- if (r < 0 && (errno == EAGAIN || errno == EINTR))
+ if (r < 0 && IN_SET(errno, EAGAIN, EINTR))
continue;
else if (r < 0)
goto error;
if (pfd[1].revents) {
- /* events on @them signal us new data */
+ ssize_t len;
+
+ /* events on @them signal new data for us */
len = read(b->them, &buf, sizeof(buf));
- if (len < 0 && (errno == EAGAIN || errno == EINTR))
+ if (len < 0 && IN_SET(errno, EAGAIN, EINTR))
continue;
if (len != sizeof(buf))
goto error;
- } else if (pfd[0].revents & (POLLHUP | POLLERR | POLLNVAL)) {
+ } else if (pfd[0].revents & (POLLHUP | POLLERR | POLLNVAL))
/* POLLHUP on the pipe tells us the other side exited.
* We treat this as implicit abortion. But we only
* handle it if there's no event on the eventfd. This
* guarantees that exit-abortions do not overwrite real
* barriers. */
buf = BARRIER_ABORTION;
- }
/* lock if they aborted */
if (buf >= (uint64_t)BARRIER_ABORTION) {
@@ -294,9 +264,8 @@ static bool barrier_read(Barrier *b, int64_t comp) {
b->barriers = BARRIER_WE_ABORTED;
else
b->barriers = BARRIER_THEY_ABORTED;
- } else if (!barrier_is_aborted(b)) {
+ } else if (!barrier_is_aborted(b))
b->barriers -= buf;
- }
}
return !barrier_they_aborted(b);
@@ -307,8 +276,7 @@ error:
* pipe-ends and treat this as abortion. The other end will notice the
* pipe-close and treat it as abortion, too. */
- b->pipe[0] = safe_close(b->pipe[0]);
- b->pipe[1] = safe_close(b->pipe[1]);
+ safe_close_pair(b->pipe);
b->barriers = BARRIER_WE_ABORTED;
return false;
}
diff --git a/src/shared/barrier.h b/src/shared/barrier.h
index 7f76ec7..53b4439 100644
--- a/src/shared/barrier.h
+++ b/src/shared/barrier.h
@@ -57,7 +57,9 @@ struct Barrier {
int64_t barriers;
};
-int barrier_init(Barrier *obj);
+#define BARRIER_NULL {-1, -1, {-1, -1}, 0}
+
+int barrier_create(Barrier *obj);
void barrier_destroy(Barrier *b);
void barrier_set_role(Barrier *b, unsigned int role);
diff --git a/src/shared/pty.c b/src/shared/pty.c
index 11d76f8..2863da4 100644
--- a/src/shared/pty.c
+++ b/src/shared/pty.c
@@ -105,6 +105,7 @@ int pty_new(Pty **out) {
pty->ref = 1;
pty->fd = -1;
+ pty->barrier = (Barrier) BARRIER_NULL;
pty->fd = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC | O_NONBLOCK);
if (pty->fd < 0)
@@ -127,7 +128,7 @@ int pty_new(Pty **out) {
if (r < 0)
return -errno;
- r = barrier_init(&pty->barrier);
+ r = barrier_create(&pty->barrier);
if (r < 0)
return r;
diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c
index 640e508..672a51e 100644
--- a/src/test/test-barrier.c
+++ b/src/test/test-barrier.c
@@ -59,10 +59,10 @@ static void msleep(unsigned long msecs) {
#define TEST_BARRIER(_FUNCTION, _CHILD_CODE, _WAIT_CHILD, _PARENT_CODE, _WAIT_PARENT) \
static void _FUNCTION(void) { \
- Barrier b; \
+ Barrier b = BARRIER_NULL; \
pid_t pid1, pid2; \
\
- assert_se(barrier_init(&b) >= 0); \
+ assert_se(barrier_create(&b) >= 0); \
\
pid1 = fork(); \
assert_se(pid1 >= 0); \
More information about the systemd-commits
mailing list