]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: add serialization/deserialization for ExecCommand
authorLuca Boccassi <bluca@debian.org>
Thu, 31 Aug 2023 23:38:01 +0000 (00:38 +0100)
committerLuca Boccassi <bluca@debian.org>
Thu, 12 Oct 2023 13:56:25 +0000 (14:56 +0100)
src/core/execute-serialize.c
src/core/execute-serialize.h
src/core/execute.h

index c13ea82875afbd27e9863974307b6ef9299b0d77..2d3445e01a98a96711be49a67e84e2283cc8c89c 100644 (file)
@@ -2106,10 +2106,70 @@ static int exec_context_deserialize(ExecContext *c, FILE *f) {
         return 0;
 }
 
+static int exec_command_serialize(const ExecCommand *c, FILE *f) {
+        int r;
+
+        assert(c);
+        assert(f);
+
+        r = serialize_item(f, "exec-command-path", c->path);
+        if (r < 0)
+                return r;
+
+        r = serialize_strv(f, "exec-command-argv", c->argv);
+        if (r < 0)
+                return r;
+
+        r = serialize_item_format(f, "exec-command-flags", "%d", (int) c->flags);
+        if (r < 0)
+                return r;
+
+        fputc('\n', f); /* End marker */
+
+        return 0;
+}
+
+static int exec_command_deserialize(ExecCommand *c, FILE *f) {
+        int r;
+
+        assert(c);
+        assert(f);
+
+        for (;;) {
+                _cleanup_free_ char *l = NULL;
+                const char *val;
+
+                r = deserialize_read_line(f, &l);
+                if (r < 0)
+                        return r;
+                if (r == 0) /* eof or end marker */
+                        break;
+
+                if ((val = startswith(l, "exec-command-path="))) {
+                        r = free_and_strdup(&c->path, val);
+                        if (r < 0)
+                                return r;
+                } else if ((val = startswith(l, "exec-command-argv="))) {
+                        r = deserialize_strv(&c->argv, val);
+                        if (r < 0)
+                                return r;
+                } else if ((val = startswith(l, "exec-command-flags="))) {
+                        r = safe_atoi(val, &c->flags);
+                        if (r < 0)
+                                return r;
+                } else
+                        log_warning("Failed to parse serialized line, ignorning: %s", l);
+
+        }
+
+        return 0;
+}
+
 int exec_serialize_invocation(
                 FILE *f,
                 FDSet *fds,
-                const ExecContext *ctx) {
+                const ExecContext *ctx,
+                const ExecCommand *cmd) {
 
         int r;
 
@@ -2120,13 +2180,18 @@ int exec_serialize_invocation(
         if (r < 0)
                 return log_debug_errno(r, "Failed to serialize context: %m");
 
+        r = exec_command_serialize(cmd, f);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to serialize command: %m");
+
         return 0;
 }
 
 int exec_deserialize_invocation(
                 FILE *f,
                 FDSet *fds,
-                ExecContext *ctx) {
+                ExecContext *ctx,
+                ExecCommand *cmd) {
 
         int r;
 
@@ -2137,5 +2202,9 @@ int exec_deserialize_invocation(
         if (r < 0)
                 return log_debug_errno(r, "Failed to deserialize context: %m");
 
+        r = exec_command_deserialize(cmd, f);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to deserialize command: %m");
+
         return 0;
 }
index 72635f25494331dccf23acc286ea2b860bc8ba5a..339ba8cafa6d92cb96b3b13d4dcf316c6596cc9d 100644 (file)
@@ -8,8 +8,10 @@
 
 int exec_serialize_invocation(FILE *f,
         FDSet *fds,
-        const ExecContext *ctx);
+        const ExecContext *ctx,
+        const ExecCommand *cmd);
 
 int exec_deserialize_invocation(FILE *f,
         FDSet *fds,
-        ExecContext *ctx);
+        ExecContext *ctx,
+        ExecCommand *cmd);
index 6f7d0686e83107ef922df904b757fd58d8796f65..bcd4a39274dff7c997f7f76d40a6038f13593ec6 100644 (file)
@@ -100,7 +100,7 @@ struct ExecStatus {
 struct ExecCommand {
         char *path;
         char **argv;
-        ExecStatus exec_status;
+        ExecStatus exec_status; /* Note that this is not serialized to sd-executor */
         ExecCommandFlags flags;
         LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
 };