From: Luca Boccassi Date: Thu, 31 Aug 2023 23:38:01 +0000 (+0100) Subject: core: add serialization/deserialization for ExecCommand X-Git-Tag: v255-rc1~250^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b806a5d35e5c294adfc45baaab1c40ba80e84d37;p=thirdparty%2Fsystemd.git core: add serialization/deserialization for ExecCommand --- diff --git a/src/core/execute-serialize.c b/src/core/execute-serialize.c index c13ea82875a..2d3445e01a9 100644 --- a/src/core/execute-serialize.c +++ b/src/core/execute-serialize.c @@ -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; } diff --git a/src/core/execute-serialize.h b/src/core/execute-serialize.h index 72635f25494..339ba8cafa6 100644 --- a/src/core/execute-serialize.h +++ b/src/core/execute-serialize.h @@ -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); diff --git a/src/core/execute.h b/src/core/execute.h index 6f7d0686e83..bcd4a39274d 100644 --- a/src/core/execute.h +++ b/src/core/execute.h @@ -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 */ };