summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-01-25 23:35:28 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-01-27 23:17:02 -0500
commit65b3903ff576488eaabb51d3c4fbf9c73d867d7c (patch)
tree0343e9d79057f6ca67acb87d6cc06f45ab7a1384 /src/test
parent8e33886ec582336564ae11b80023abe93d7599c0 (diff)
journal: guarantee async-signal-safety in sd_journald_sendv
signal(7) provides a list of functions which may be called from a signal handler. Other functions, which only call those functions and don't access global memory and are reentrant are also safe. sd_j_sendv was mostly OK, but would call mkostemp and writev in a fallback path, which are unsafe. Being able to call sd_j_sendv in a async-signal-safe way is important because it allows it be used in signal handlers. Safety is achieved by replacing mkostemp with open(O_TMPFILE) and an open-coded writev replacement which uses write. Unfortunately, O_TMPFILE is only available on kernels >= 3.11. When O_TMPFILE is unavailable, an open-coded mkostemp is used. https://bugzilla.gnome.org/show_bug.cgi?id=722889
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-tmpfiles.c51
-rw-r--r--src/test/test-util.c26
2 files changed, 77 insertions, 0 deletions
diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c
new file mode 100644
index 0000000000..f25a0dca52
--- /dev/null
+++ b/src/test/test-tmpfiles.c
@@ -0,0 +1,51 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Zbigniew Jędrzejewski-Szmek
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "util.h"
+
+int main(int argc, char** argv) {
+ const char *p = argv[1] ?: "/tmp";
+ char *pattern = strappenda(p, "/systemd-test-XXXXXX");
+ _cleanup_close_ int fd, fd2;
+ _cleanup_free_ char *cmd, *cmd2;
+
+ fd = open_tmpfile(p, O_RDWR);
+ assert(fd >= 0);
+
+ assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid(), fd) > 0);
+ system(cmd);
+
+ fd2 = mkostemp_safe(pattern, O_RDWR);
+ assert(fd >= 0);
+ assert_se(unlink(pattern) == 0);
+
+ assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid(), fd2) > 0);
+ system(cmd2);
+
+ return 0;
+}
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 05b2294e24..f819589b52 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -28,6 +28,8 @@
#include "util.h"
#include "strv.h"
+#include "def.h"
+#include "fileio.h"
static void test_streq_ptr(void) {
assert_se(streq_ptr(NULL, NULL));
@@ -578,6 +580,29 @@ static void test_in_set(void) {
assert_se(!IN_SET(0, 1, 2, 3, 4));
}
+static void test_writev_safe(void) {
+ char name[] = "/tmp/test-writev_safe.XXXXXX";
+ _cleanup_free_ char *contents;
+ size_t size;
+ int fd, r;
+
+ struct iovec iov[3];
+ IOVEC_SET_STRING(iov[0], "abc\n");
+ IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n");
+ IOVEC_SET_STRING(iov[2], "");
+
+ fd = mkstemp(name);
+ printf("test_writev_safe: %s", name);
+
+ r = writev_safe(fd, iov, 3);
+ assert(r == 0);
+
+ r = read_full_file(name, &contents, &size);
+ assert(r == 0);
+ printf("contents: %s", contents);
+ assert(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
+}
+
int main(int argc, char *argv[]) {
test_streq_ptr();
test_first_word();
@@ -615,6 +640,7 @@ int main(int argc, char *argv[]) {
test_fstab_node_to_udev_node();
test_get_files_in_directory();
test_in_set();
+ test_writev_safe();
return 0;
}