From: Ivan Kruglov Date: Wed, 9 Oct 2024 11:35:14 +0000 (+0200) Subject: sd-json: add sd_json_dispatch_signal() X-Git-Tag: v257-rc1~259^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=729fbe78dfcff5303739da8f9594c7faf3483f1f;p=thirdparty%2Fsystemd.git sd-json: add sd_json_dispatch_signal() --- diff --git a/src/basic/signal-util.h b/src/basic/signal-util.h index 633993ca00e..5739fe05593 100644 --- a/src/basic/signal-util.h +++ b/src/basic/signal-util.h @@ -46,6 +46,7 @@ static inline void block_signals_reset(sigset_t *ss) { assert_se(sigprocmask_many(SIG_BLOCK, &_t, __VA_ARGS__) >= 0); \ _t; \ }) +#define SIGNO_INVALID (-EINVAL) static inline bool SIGNAL_VALID(int signo) { return signo > 0 && signo < _NSIG; diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index acee085f2f1..b3f46f90f19 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -868,6 +868,7 @@ global: sd_json_dispatch_uint32; sd_json_dispatch_uint64; sd_json_dispatch_uint8; + sd_json_dispatch_signal; sd_json_dispatch_unsupported; sd_json_dispatch_variant; sd_json_dispatch_variant_noref; diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index 33ccdcac5aa..3c6f6b140b1 100644 --- a/src/libsystemd/sd-json/sd-json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -30,6 +30,7 @@ #include "memstream-util.h" #include "path-util.h" #include "set.h" +#include "signal-util.h" #include "string-table.h" #include "string-util.h" #include "strv.h" @@ -5527,6 +5528,29 @@ _public_ int sd_json_dispatch_id128(const char *name, sd_json_variant *variant, return 0; } +_public_ int sd_json_dispatch_signal(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + int *signo = userdata; + uint32_t k; + int r; + + assert_return(variant, -EINVAL); + + if (sd_json_variant_is_null(variant)) { + *signo = SIGNO_INVALID; + return 0; + } + + r = sd_json_dispatch_uint32(name, variant, flags, &k); + if (r < 0) + return r; + + if (!SIGNAL_VALID(k)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid signal.", strna(name)); + + *signo = k; + return 0; +} + _public_ int sd_json_dispatch_unsupported(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not allowed in this object.", strna(name)); } diff --git a/src/systemd/sd-json.h b/src/systemd/sd-json.h index b0b171dcb62..f3c9a272572 100644 --- a/src/systemd/sd-json.h +++ b/src/systemd/sd-json.h @@ -329,6 +329,7 @@ int sd_json_dispatch_uint8(const char *name, sd_json_variant *variant, sd_json_d int sd_json_dispatch_double(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int sd_json_dispatch_uid_gid(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int sd_json_dispatch_id128(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_signal(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int sd_json_dispatch_unsupported(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); #define sd_json_dispatch_uint sd_json_dispatch_uint32