summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/basic/process-util.h11
-rw-r--r--src/libsystemd/libsystemd.sym1
-rw-r--r--src/libsystemd/sd-json/sd-json.c39
-rw-r--r--src/systemd/sd-json.h1
4 files changed, 52 insertions, 0 deletions
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index 59732a8676..2efe89e135 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -148,10 +148,21 @@ static inline bool sched_priority_is_valid(int i) {
return i >= 0 && i <= sched_get_priority_max(SCHED_RR);
}
+#define PID_AUTOMATIC ((pid_t) INT_MIN) /* special value indicating "acquire pid from connection peer */
+#define PID_INVALID ((pid_t) 0) /* default value for "invalid pid" */
+
static inline bool pid_is_valid(pid_t p) {
return p > 0;
}
+static inline bool pid_is_automatic(pid_t p) {
+ return p == PID_AUTOMATIC;
+}
+
+static inline bool pid_is_valid_or_automatic(pid_t p) {
+ return pid_is_valid(p) || pid_is_automatic(p);
+}
+
pid_t getpid_cached(void);
void reset_cached_pid(void);
diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym
index b3f46f90f1..d642ef2581 100644
--- a/src/libsystemd/libsystemd.sym
+++ b/src/libsystemd/libsystemd.sym
@@ -864,6 +864,7 @@ global:
sd_json_dispatch_strv;
sd_json_dispatch_tristate;
sd_json_dispatch_uid_gid;
+ sd_json_dispatch_pid;
sd_json_dispatch_uint16;
sd_json_dispatch_uint32;
sd_json_dispatch_uint64;
diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c
index 3c6f6b140b..58ae134e16 100644
--- a/src/libsystemd/sd-json/sd-json.c
+++ b/src/libsystemd/sd-json/sd-json.c
@@ -29,6 +29,7 @@
#include "memory-util.h"
#include "memstream-util.h"
#include "path-util.h"
+#include "process-util.h"
#include "set.h"
#include "signal-util.h"
#include "string-table.h"
@@ -5507,6 +5508,44 @@ _public_ int sd_json_dispatch_uid_gid(const char *name, sd_json_variant *variant
return 0;
}
+_public_ int sd_json_dispatch_pid(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
+ /* pid_t is a signed type, but we don't consider negative values as valid.
+ * There is a special treatment for 0 if SD_JSON_RELAX flag present. */
+
+ pid_t *pid = userdata;
+ uint32_t k;
+ int r;
+
+ assert_return(variant, -EINVAL);
+ assert_cc(sizeof(pid_t) == sizeof(uint32_t));
+
+ if (sd_json_variant_is_null(variant)) {
+ *pid = PID_INVALID;
+ return 0;
+ }
+
+ r = sd_json_dispatch_uint32(name, variant, flags, &k);
+ if (r < 0)
+ return r;
+
+ if (k == 0) {
+ if (!FLAGS_SET(flags, SD_JSON_RELAX))
+ goto invalid_value;
+
+ *pid = PID_AUTOMATIC;
+ return 0;
+ }
+
+ if (!pid_is_valid(k))
+ goto invalid_value;
+
+ *pid = k;
+ return 0;
+
+invalid_value:
+ return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid PID.", strna(name));
+}
+
_public_ int sd_json_dispatch_id128(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
sd_id128_t *uuid = userdata;
int r;
diff --git a/src/systemd/sd-json.h b/src/systemd/sd-json.h
index f3c9a27257..a73eeb8dd6 100644
--- a/src/systemd/sd-json.h
+++ b/src/systemd/sd-json.h
@@ -328,6 +328,7 @@ int sd_json_dispatch_int8(const char *name, sd_json_variant *variant, sd_json_di
int sd_json_dispatch_uint8(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata);
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_pid(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);