richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
4cad4c
From 2808e53f785e9ca7fdab286678e784b661b4c185 Mon Sep 17 00:00:00 2001
4cad4c
From: Zsolt Dollenstein <zsol.zsol@gmail.com>
4cad4c
Date: Tue, 3 Jul 2018 12:22:29 -0700
4cad4c
Subject: [PATCH] Add support for opening files for appending
4cad4c
4cad4c
Addresses part of #8983
4cad4c
4cad4c
(cherry picked from commit 566b7d23eb747e9c5a74e5647693077b52395fc5)
4cad4c
4cad4c
Resolves: #1809175
4cad4c
---
4cad4c
 man/systemd.exec.xml                          | 16 ++++++----
4cad4c
 src/core/dbus-execute.c                       | 30 ++++++++++++++-----
4cad4c
 src/core/execute.c                            | 20 ++++++++++---
4cad4c
 src/core/execute.h                            |  1 +
4cad4c
 src/core/load-fragment.c                      | 11 +++++++
4cad4c
 src/core/main.c                               |  4 +--
4cad4c
 src/test/test-execute.c                       | 10 +++++++
4cad4c
 test/meson.build                              |  2 ++
4cad4c
 .../exec-standardoutput-append.service        | 13 ++++++++
4cad4c
 .../exec-standardoutput-file.service          | 13 ++++++++
4cad4c
 10 files changed, 101 insertions(+), 19 deletions(-)
4cad4c
 create mode 100644 test/test-execute/exec-standardoutput-append.service
4cad4c
 create mode 100644 test/test-execute/exec-standardoutput-file.service
4cad4c
4cad4c
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
4cad4c
index bdaed68162..e2a5ede968 100644
4cad4c
--- a/man/systemd.exec.xml
4cad4c
+++ b/man/systemd.exec.xml
4cad4c
@@ -1792,8 +1792,8 @@ SystemCallErrorNumber=EPERM</programlisting>
4cad4c
         of <option>inherit</option>, <option>null</option>, <option>tty</option>, <option>journal</option>,
4cad4c
         <option>syslog</option>, <option>kmsg</option>, <option>journal+console</option>,
4cad4c
         <option>syslog+console</option>, <option>kmsg+console</option>,
4cad4c
-        <option>file:<replaceable>path</replaceable></option>, <option>socket</option> or
4cad4c
-        <option>fd:<replaceable>name</replaceable></option>.</para>
4cad4c
+        <option>file:<replaceable>path</replaceable></option>, <option>append:<replaceable>path</replaceable></option>,
4cad4c
+        <option>socket</option> or<option>fd:<replaceable>name</replaceable></option>.</para>
4cad4c
 
4cad4c
         <para><option>inherit</option> duplicates the file descriptor of standard input for standard output.</para>
4cad4c
 
4cad4c
@@ -1824,11 +1824,17 @@ SystemCallErrorNumber=EPERM</programlisting>
4cad4c
 
4cad4c
         <para>The <option>file:<replaceable>path</replaceable></option> option may be used to connect a specific file
4cad4c
         system object to standard output. The semantics are similar to the same option of
4cad4c
-        <varname>StandardInput=</varname>, see above. If standard input and output are directed to the same file path,
4cad4c
-        it is opened only once, for reading as well as writing and duplicated. This is particular useful when the
4cad4c
-        specified path refers to an <constant>AF_UNIX</constant> socket in the file system, as in that case only a
4cad4c
+        <varname>StandardInput=</varname>, see above. If <replaceable>path</replaceable> refers to a regular file
4cad4c
+        on the filesystem, it is opened (created if it doesn't exist yet) for writing at the beginning of the file,
4cad4c
+        but without truncating it.
4cad4c
+        If standard input and output are directed to the same file path, it is opened only once, for reading as well
4cad4c
+        as writing and duplicated. This is particularly useful when the specified path refers to an
4cad4c
+        <constant>AF_UNIX</constant> socket in the file system, as in that case only a
4cad4c
         single stream connection is created for both input and output.</para>
4cad4c
 
4cad4c
+        <para><option>append:<replaceable>path</replaceable></option> is similar to <option>file:<replaceable>path
4cad4c
+        </replaceable></option> above, but it opens the file in append mode.</para>
4cad4c
+
4cad4c
         <para><option>socket</option> connects standard output to a socket acquired via socket activation. The
4cad4c
         semantics are similar to the same option of <varname>StandardInput=</varname>, see above.</para>
4cad4c
 
4cad4c
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
4cad4c
index e7c0b893d1..f9527e56b2 100644
4cad4c
--- a/src/core/dbus-execute.c
4cad4c
+++ b/src/core/dbus-execute.c
4cad4c
@@ -1772,7 +1772,10 @@ int bus_exec_context_set_transient_property(
4cad4c
 
4cad4c
                 return 1;
4cad4c
 
4cad4c
-        } else if (STR_IN_SET(name, "StandardInputFile", "StandardOutputFile", "StandardErrorFile")) {
4cad4c
+        } else if (STR_IN_SET(name,
4cad4c
+                              "StandardInputFile",
4cad4c
+                              "StandardOutputFile", "StandardOutputFileToCreate", "StandardOutputFileToAppend",
4cad4c
+                              "StandardErrorFile", "StandardErrorFileToCreate", "StandardErrorFileToAppend")) {
4cad4c
                 const char *s;
4cad4c
 
4cad4c
                 r = sd_bus_message_read(message, "s", &s);
4cad4c
@@ -1796,23 +1799,34 @@ int bus_exec_context_set_transient_property(
4cad4c
                                 c->std_input = EXEC_INPUT_FILE;
4cad4c
                                 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=file:%s", s);
4cad4c
 
4cad4c
-                        } else if (streq(name, "StandardOutputFile")) {
4cad4c
+                        } else if (STR_IN_SET(name, "StandardOutputFile", "StandardOutputFileToAppend")) {
4cad4c
                                 r = free_and_strdup(&c->stdio_file[STDOUT_FILENO], empty_to_null(s));
4cad4c
                                 if (r < 0)
4cad4c
                                         return r;
4cad4c
 
4cad4c
-                                c->std_output = EXEC_OUTPUT_FILE;
4cad4c
-                                unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
4cad4c
-
4cad4c
+                                if (streq(name, "StandardOutputFile")) {
4cad4c
+                                        c->std_output = EXEC_OUTPUT_FILE;
4cad4c
+                                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
4cad4c
+                                } else {
4cad4c
+                                        assert(streq(name, "StandardOutputFileToAppend"));
4cad4c
+                                        c->std_output = EXEC_OUTPUT_FILE_APPEND;
4cad4c
+                                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=append:%s", s);
4cad4c
+                                }
4cad4c
                         } else {
4cad4c
-                                assert(streq(name, "StandardErrorFile"));
4cad4c
+                                assert(STR_IN_SET(name, "StandardErrorFile", "StandardErrorFileToAppend"));
4cad4c
 
4cad4c
                                 r = free_and_strdup(&c->stdio_file[STDERR_FILENO], empty_to_null(s));
4cad4c
                                 if (r < 0)
4cad4c
                                         return r;
4cad4c
 
4cad4c
-                                c->std_error = EXEC_OUTPUT_FILE;
4cad4c
-                                unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=file:%s", s);
4cad4c
+                                if (streq(name, "StandardErrorFile")) {
4cad4c
+                                        c->std_error = EXEC_OUTPUT_FILE;
4cad4c
+                                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
4cad4c
+                                } else {
4cad4c
+                                      assert(streq(name, "StandardErrorFileToAppend"));
4cad4c
+                                      c->std_error = EXEC_OUTPUT_FILE_APPEND;
4cad4c
+                                      unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=append:%s", s);
4cad4c
+                                }
4cad4c
                         }
4cad4c
                 }
4cad4c
 
4cad4c
diff --git a/src/core/execute.c b/src/core/execute.c
4cad4c
index f012023224..3c54ac1110 100644
4cad4c
--- a/src/core/execute.c
4cad4c
+++ b/src/core/execute.c
4cad4c
@@ -89,6 +89,7 @@
4cad4c
 #include "strv.h"
4cad4c
 #include "syslog-util.h"
4cad4c
 #include "terminal-util.h"
4cad4c
+#include "umask-util.h"
4cad4c
 #include "unit.h"
4cad4c
 #include "user-util.h"
4cad4c
 #include "util.h"
4cad4c
@@ -675,9 +676,10 @@ static int setup_output(
4cad4c
                 (void) fd_nonblock(named_iofds[fileno], false);
4cad4c
                 return dup2(named_iofds[fileno], fileno) < 0 ? -errno : fileno;
4cad4c
 
4cad4c
-        case EXEC_OUTPUT_FILE: {
4cad4c
+        case EXEC_OUTPUT_FILE:
4cad4c
+        case EXEC_OUTPUT_FILE_APPEND: {
4cad4c
                 bool rw;
4cad4c
-                int fd;
4cad4c
+                int fd, flags;
4cad4c
 
4cad4c
                 assert(context->stdio_file[fileno]);
4cad4c
 
4cad4c
@@ -687,11 +689,16 @@ static int setup_output(
4cad4c
                 if (rw)
4cad4c
                         return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno;
4cad4c
 
4cad4c
-                fd = acquire_path(context->stdio_file[fileno], O_WRONLY, 0666 & ~context->umask);
4cad4c
+                flags = O_WRONLY;
4cad4c
+                if (o == EXEC_OUTPUT_FILE_APPEND)
4cad4c
+                        flags |= O_APPEND;
4cad4c
+
4cad4c
+                fd = acquire_path(context->stdio_file[fileno], flags, 0666 & ~context->umask);
4cad4c
+
4cad4c
                 if (fd < 0)
4cad4c
                         return fd;
4cad4c
 
4cad4c
-                return move_fd(fd, fileno, false);
4cad4c
+                return move_fd(fd, fileno, 0);
4cad4c
         }
4cad4c
 
4cad4c
         default:
4cad4c
@@ -4168,8 +4175,12 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
4cad4c
                 fprintf(f, "%sStandardInputFile: %s\n", prefix, c->stdio_file[STDIN_FILENO]);
4cad4c
         if (c->std_output == EXEC_OUTPUT_FILE)
4cad4c
                 fprintf(f, "%sStandardOutputFile: %s\n", prefix, c->stdio_file[STDOUT_FILENO]);
4cad4c
+        if (c->std_output == EXEC_OUTPUT_FILE_APPEND)
4cad4c
+                fprintf(f, "%sStandardOutputFileToAppend: %s\n", prefix, c->stdio_file[STDOUT_FILENO]);
4cad4c
         if (c->std_error == EXEC_OUTPUT_FILE)
4cad4c
                 fprintf(f, "%sStandardErrorFile: %s\n", prefix, c->stdio_file[STDERR_FILENO]);
4cad4c
+        if (c->std_error == EXEC_OUTPUT_FILE_APPEND)
4cad4c
+                fprintf(f, "%sStandardErrorFileToAppend: %s\n", prefix, c->stdio_file[STDERR_FILENO]);
4cad4c
 
4cad4c
         if (c->tty_path)
4cad4c
                 fprintf(f,
4cad4c
@@ -5111,6 +5122,7 @@ static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
4cad4c
         [EXEC_OUTPUT_SOCKET] = "socket",
4cad4c
         [EXEC_OUTPUT_NAMED_FD] = "fd",
4cad4c
         [EXEC_OUTPUT_FILE] = "file",
4cad4c
+        [EXEC_OUTPUT_FILE_APPEND] = "append",
4cad4c
 };
4cad4c
 
4cad4c
 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
4cad4c
diff --git a/src/core/execute.h b/src/core/execute.h
4cad4c
index 2266355962..86c1cee84c 100644
4cad4c
--- a/src/core/execute.h
4cad4c
+++ b/src/core/execute.h
4cad4c
@@ -57,6 +57,7 @@ typedef enum ExecOutput {
4cad4c
         EXEC_OUTPUT_SOCKET,
4cad4c
         EXEC_OUTPUT_NAMED_FD,
4cad4c
         EXEC_OUTPUT_FILE,
4cad4c
+        EXEC_OUTPUT_FILE_APPEND,
4cad4c
         _EXEC_OUTPUT_MAX,
4cad4c
         _EXEC_OUTPUT_INVALID = -1
4cad4c
 } ExecOutput;
4cad4c
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
4cad4c
index 2082166afb..9b2724307d 100644
4cad4c
--- a/src/core/load-fragment.c
4cad4c
+++ b/src/core/load-fragment.c
4cad4c
@@ -1016,6 +1016,17 @@ int config_parse_exec_output(
4cad4c
 
4cad4c
                 eo = EXEC_OUTPUT_FILE;
4cad4c
 
4cad4c
+        } else if ((n = startswith(rvalue, "append:"))) {
4cad4c
+
4cad4c
+                r = unit_full_printf(u, n, &resolved);
4cad4c
+                if (r < 0)
4cad4c
+                        return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
4cad4c
+
4cad4c
+                r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
4cad4c
+                if (r < 0)
4cad4c
+                        return -ENOEXEC;
4cad4c
+
4cad4c
+                eo = EXEC_OUTPUT_FILE_APPEND;
4cad4c
         } else {
4cad4c
                 eo = exec_output_from_string(rvalue);
4cad4c
                 if (eo < 0) {
4cad4c
diff --git a/src/core/main.c b/src/core/main.c
4cad4c
index 9f238a8430..25536054b3 100644
4cad4c
--- a/src/core/main.c
4cad4c
+++ b/src/core/main.c
4cad4c
@@ -620,8 +620,8 @@ static int config_parse_output_restricted(
4cad4c
                 return 0;
4cad4c
         }
4cad4c
 
4cad4c
-        if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE)) {
4cad4c
-                log_syntax(unit, LOG_ERR, filename, line, 0, "Standard output types socket, fd:, file: are not supported as defaults, ignoring: %s", rvalue);
4cad4c
+        if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND)) {
4cad4c
+                log_syntax(unit, LOG_ERR, filename, line, 0, "Standard output types socket, fd:, file:, append: are not supported as defaults, ignoring: %s", rvalue);
4cad4c
                 return 0;
4cad4c
         }
4cad4c
 
4cad4c
diff --git a/src/test/test-execute.c b/src/test/test-execute.c
4cad4c
index 637ffe96bb..0f8dc883b1 100644
4cad4c
--- a/src/test/test-execute.c
4cad4c
+++ b/src/test/test-execute.c
4cad4c
@@ -651,6 +651,14 @@ static void test_exec_standardinput(Manager *m) {
4cad4c
         test(m, "exec-standardinput-file.service", 0, CLD_EXITED);
4cad4c
 }
4cad4c
 
4cad4c
+static void test_exec_standardoutput(Manager *m) {
4cad4c
+        test(m, "exec-standardoutput-file.service", 0, CLD_EXITED);
4cad4c
+}
4cad4c
+
4cad4c
+static void test_exec_standardoutput_append(Manager *m) {
4cad4c
+        test(m, "exec-standardoutput-append.service", 0, CLD_EXITED);
4cad4c
+}
4cad4c
+
4cad4c
 static int run_tests(UnitFileScope scope, const test_function_t *tests) {
4cad4c
         const test_function_t *test = NULL;
4cad4c
         _cleanup_(manager_freep) Manager *m = NULL;
4cad4c
@@ -698,6 +706,8 @@ int main(int argc, char *argv[]) {
4cad4c
                 test_exec_restrictnamespaces,
4cad4c
                 test_exec_runtimedirectory,
4cad4c
                 test_exec_standardinput,
4cad4c
+                test_exec_standardoutput,
4cad4c
+                test_exec_standardoutput_append,
4cad4c
                 test_exec_supplementarygroups,
4cad4c
                 test_exec_systemcallerrornumber,
4cad4c
                 test_exec_systemcallfilter,
4cad4c
diff --git a/test/meson.build b/test/meson.build
4cad4c
index fb9f2cdb9b..4d1c51048c 100644
4cad4c
--- a/test/meson.build
4cad4c
+++ b/test/meson.build
4cad4c
@@ -115,6 +115,8 @@ test_data_files = '''
4cad4c
         test-execute/exec-specifier@.service
4cad4c
         test-execute/exec-standardinput-data.service
4cad4c
         test-execute/exec-standardinput-file.service
4cad4c
+        test-execute/exec-standardoutput-file.service
4cad4c
+        test-execute/exec-standardoutput-append.service
4cad4c
         test-execute/exec-supplementarygroups-multiple-groups-default-group-user.service
4cad4c
         test-execute/exec-supplementarygroups-multiple-groups-withgid.service
4cad4c
         test-execute/exec-supplementarygroups-multiple-groups-withuid.service
4cad4c
diff --git a/test/test-execute/exec-standardoutput-append.service b/test/test-execute/exec-standardoutput-append.service
4cad4c
new file mode 100644
4cad4c
index 0000000000..8983bb056b
4cad4c
--- /dev/null
4cad4c
+++ b/test/test-execute/exec-standardoutput-append.service
4cad4c
@@ -0,0 +1,13 @@
4cad4c
+[Unit]
4cad4c
+Description=Test for StandardOutput=append:
4cad4c
+
4cad4c
+[Service]
4cad4c
+ExecStartPre=sh -c 'printf "hello\n" > /tmp/test-exec-standardoutput-output'
4cad4c
+ExecStartPre=sh -c 'printf "hello\nhello\n" > /tmp/test-exec-standardoutput-expected'
4cad4c
+StandardInput=data
4cad4c
+StandardInputText=hello
4cad4c
+StandardOutput=append:/tmp/test-exec-standardoutput-output
4cad4c
+StandardError=null
4cad4c
+ExecStart=cat
4cad4c
+ExecStart=cmp /tmp/test-exec-standardoutput-output /tmp/test-exec-standardoutput-expected
4cad4c
+Type=oneshot
4cad4c
diff --git a/test/test-execute/exec-standardoutput-file.service b/test/test-execute/exec-standardoutput-file.service
4cad4c
new file mode 100644
4cad4c
index 0000000000..71e2604b94
4cad4c
--- /dev/null
4cad4c
+++ b/test/test-execute/exec-standardoutput-file.service
4cad4c
@@ -0,0 +1,13 @@
4cad4c
+[Unit]
4cad4c
+Description=Test for StandardOutput=file:
4cad4c
+
4cad4c
+[Service]
4cad4c
+ExecStartPre=sh -c 'printf "nooo\nhello\n" > /tmp/test-exec-standardoutput-output'
4cad4c
+ExecStartPre=sh -c 'printf "hello\nello\n" > /tmp/test-exec-standardoutput-expected'
4cad4c
+StandardInput=data
4cad4c
+StandardInputText=hello
4cad4c
+StandardOutput=file:/tmp/test-exec-standardoutput-output
4cad4c
+StandardError=null
4cad4c
+ExecStart=cat
4cad4c
+ExecStart=cmp /tmp/test-exec-standardoutput-expected /tmp/test-exec-standardoutput-output
4cad4c
+Type=oneshot