diff options
author | David Tardon <dtardon@redhat.com> | 2023-05-05 09:10:49 +0200 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2023-05-05 09:10:56 +0200 |
commit | e652663a043cb80936bb12ad5c87766fc5150c24 (patch) | |
tree | 0be9d85c11057f6b66b3f4604c27fd85051a7599 /src | |
parent | test: add a test for parse_fd() (diff) | |
download | systemd-e652663a043cb80936bb12ad5c87766fc5150c24.tar.xz systemd-e652663a043cb80936bb12ad5c87766fc5150c24.zip |
tree-wide: use parse_fd()
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/fd-util.c | 3 | ||||
-rw-r--r-- | src/core/automount.c | 2 | ||||
-rw-r--r-- | src/core/dynamic-user.c | 4 | ||||
-rw-r--r-- | src/core/execute.c | 28 | ||||
-rw-r--r-- | src/core/main.c | 10 | ||||
-rw-r--r-- | src/core/manager-serialize.c | 4 | ||||
-rw-r--r-- | src/core/service.c | 12 | ||||
-rw-r--r-- | src/core/unit-serialize.c | 4 | ||||
-rw-r--r-- | src/notify/notify.c | 8 | ||||
-rw-r--r-- | src/shared/bpf-program.c | 8 | ||||
-rw-r--r-- | src/shared/varlink.c | 10 |
11 files changed, 46 insertions, 47 deletions
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 974a7aac65..907bfeb600 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -416,7 +416,8 @@ int close_all_fds(const int except[], size_t n_except) { if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN)) continue; - if (safe_atoi(de->d_name, &fd) < 0) + fd = parse_fd(de->d_name); + if (fd < 0) /* Let's better ignore this, just in case */ continue; diff --git a/src/core/automount.c b/src/core/automount.c index 4cffca419c..89755e3a9b 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -938,7 +938,7 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu } else if (streq(key, "pipe-fd")) { int fd; - if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) log_unit_debug(u, "Failed to parse pipe-fd value: %s", value); else { safe_close(a->pipe_fd); diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c index 19cf3faca6..e99a6694f6 100644 --- a/src/core/dynamic-user.c +++ b/src/core/dynamic-user.c @@ -645,12 +645,12 @@ void dynamic_user_deserialize_one(Manager *m, const char *value, FDSet *fds) { return; } - if (safe_atoi(s0, &fd0) < 0 || !fdset_contains(fds, fd0)) { + if ((fd0 = parse_fd(s0)) < 0 || !fdset_contains(fds, fd0)) { log_debug("Unable to process dynamic user fd specification."); return; } - if (safe_atoi(s1, &fd1) < 0 || !fdset_contains(fds, fd1)) { + if ((fd1 = parse_fd(s1)) < 0 || !fdset_contains(fds, fd1)) { log_debug("Unable to process dynamic user fd specification."); return; } diff --git a/src/core/execute.c b/src/core/execute.c index 04dcf4b427..8ddd7362a3 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -7338,7 +7338,7 @@ int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char } else if (streq(key, "netns-socket-0")) { int fd; - if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd)) { + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) { log_unit_debug(u, "Failed to parse netns socket value: %s", value); return 0; } @@ -7349,7 +7349,7 @@ int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char } else if (streq(key, "netns-socket-1")) { int fd; - if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd)) { + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) { log_unit_debug(u, "Failed to parse netns socket value: %s", value); return 0; } @@ -7422,9 +7422,9 @@ int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fd n = strcspn(v, " "); buf = strndupa_safe(v, n); - r = safe_atoi(buf, &netns_fdpair[0]); - if (r < 0) - return log_debug_errno(r, "Unable to parse exec-runtime specification netns-socket-0=%s: %m", buf); + netns_fdpair[0] = parse_fd(buf); + if (netns_fdpair[0] < 0) + return log_debug_errno(netns_fdpair[0], "Unable to parse exec-runtime specification netns-socket-0=%s: %m", buf); if (!fdset_contains(fds, netns_fdpair[0])) return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "exec-runtime specification netns-socket-0= refers to unknown fd %d: %m", netns_fdpair[0]); @@ -7441,9 +7441,9 @@ int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fd n = strcspn(v, " "); buf = strndupa_safe(v, n); - r = safe_atoi(buf, &netns_fdpair[1]); - if (r < 0) - return log_debug_errno(r, "Unable to parse exec-runtime specification netns-socket-1=%s: %m", buf); + netns_fdpair[1] = parse_fd(buf); + if (netns_fdpair[1] < 0) + return log_debug_errno(netns_fdpair[1], "Unable to parse exec-runtime specification netns-socket-1=%s: %m", buf); if (!fdset_contains(fds, netns_fdpair[1])) return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "exec-runtime specification netns-socket-1= refers to unknown fd %d: %m", netns_fdpair[1]); @@ -7460,9 +7460,9 @@ int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fd n = strcspn(v, " "); buf = strndupa_safe(v, n); - r = safe_atoi(buf, &ipcns_fdpair[0]); - if (r < 0) - return log_debug_errno(r, "Unable to parse exec-runtime specification ipcns-socket-0=%s: %m", buf); + ipcns_fdpair[0] = parse_fd(buf); + if (ipcns_fdpair[0] < 0) + return log_debug_errno(ipcns_fdpair[0], "Unable to parse exec-runtime specification ipcns-socket-0=%s: %m", buf); if (!fdset_contains(fds, ipcns_fdpair[0])) return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "exec-runtime specification ipcns-socket-0= refers to unknown fd %d: %m", ipcns_fdpair[0]); @@ -7479,9 +7479,9 @@ int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fd n = strcspn(v, " "); buf = strndupa_safe(v, n); - r = safe_atoi(buf, &ipcns_fdpair[1]); - if (r < 0) - return log_debug_errno(r, "Unable to parse exec-runtime specification ipcns-socket-1=%s: %m", buf); + ipcns_fdpair[1] = parse_fd(buf); + if (ipcns_fdpair[1] < 0) + return log_debug_errno(ipcns_fdpair[1], "Unable to parse exec-runtime specification ipcns-socket-1=%s: %m", buf); if (!fdset_contains(fds, ipcns_fdpair[1])) return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "exec-runtime specification ipcns-socket-1= refers to unknown fd %d: %m", ipcns_fdpair[1]); diff --git a/src/core/main.c b/src/core/main.c index 0b2eb13d5e..ab545a6421 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1002,13 +1002,11 @@ static int parse_argv(int argc, char *argv[]) { int fd; FILE *f; - r = safe_atoi(optarg, &fd); - if (r < 0) - return log_error_errno(r, "Failed to parse deserialize option \"%s\": %m", optarg); + fd = parse_fd(optarg); + if (fd == -ERANGE) + return log_error_errno(fd, "Invalid deserialize fd: %s", optarg); if (fd < 0) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Invalid deserialize fd: %d", - fd); + return log_error_errno(fd, "Failed to parse deserialize option \"%s\": %m", optarg); (void) fd_cloexec(fd, true); diff --git a/src/core/manager-serialize.c b/src/core/manager-serialize.c index 270d95dd70..4570f06b73 100644 --- a/src/core/manager-serialize.c +++ b/src/core/manager-serialize.c @@ -476,7 +476,7 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { } else if ((val = startswith(l, "notify-fd="))) { int fd; - if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(val)) < 0 || !fdset_contains(fds, fd)) log_notice("Failed to parse notify fd, ignoring: \"%s\"", val); else { m->notify_event_source = sd_event_source_disable_unref(m->notify_event_source); @@ -492,7 +492,7 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { } else if ((val = startswith(l, "cgroups-agent-fd="))) { int fd; - if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(val)) < 0 || !fdset_contains(fds, fd)) log_notice("Failed to parse cgroups agent fd, ignoring.: %s", val); else { m->cgroups_agent_event_source = sd_event_source_disable_unref(m->cgroups_agent_event_source); diff --git a/src/core/service.c b/src/core/service.c index 2ba7511ad2..a7fd518fee 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -3235,7 +3235,7 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value, } else if (streq(key, "socket-fd")) { int fd; - if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) log_unit_debug(u, "Failed to parse socket-fd value: %s", value); else { asynchronous_close(s->socket_fd); @@ -3246,7 +3246,7 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value, int fd, do_poll; r = extract_first_word(&value, &fdv, NULL, 0); - if (r <= 0 || safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) { + if (r <= 0 || (fd = parse_fd(fdv)) < 0 || !fdset_contains(fds, fd)) { log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); return 0; } @@ -3324,7 +3324,7 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value, } else if (streq(key, "stdin-fd")) { int fd; - if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) log_unit_debug(u, "Failed to parse stdin-fd value: %s", value); else { asynchronous_close(s->stdin_fd); @@ -3334,7 +3334,7 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value, } else if (streq(key, "stdout-fd")) { int fd; - if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) log_unit_debug(u, "Failed to parse stdout-fd value: %s", value); else { asynchronous_close(s->stdout_fd); @@ -3344,7 +3344,7 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value, } else if (streq(key, "stderr-fd")) { int fd; - if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) log_unit_debug(u, "Failed to parse stderr-fd value: %s", value); else { asynchronous_close(s->stderr_fd); @@ -3354,7 +3354,7 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value, } else if (streq(key, "exec-fd")) { int fd; - if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(value)) < 0 || !fdset_contains(fds, fd)) log_unit_debug(u, "Failed to parse exec-fd value: %s", value); else { s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); diff --git a/src/core/unit-serialize.c b/src/core/unit-serialize.c index 8055d9e533..cb209c5bde 100644 --- a/src/core/unit-serialize.c +++ b/src/core/unit-serialize.c @@ -391,7 +391,7 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { else if (STR_IN_SET(l, "ipv4-socket-bind-bpf-link-fd", "ipv6-socket-bind-bpf-link-fd")) { int fd; - if (safe_atoi(v, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + if ((fd = parse_fd(v)) < 0 || !fdset_contains(fds, fd)) log_unit_debug(u, "Failed to parse %s value: %s, ignoring.", l, v); else { if (fdset_remove(fds, fd) < 0) { @@ -423,7 +423,7 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { } else if (streq(l, "restrict-ifaces-bpf-fd")) { int fd; - if (safe_atoi(v, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) { + if ((fd = parse_fd(v)) < 0 || !fdset_contains(fds, fd)) { log_unit_debug(u, "Failed to parse restrict-ifaces-bpf-fd value: %s", v); continue; } diff --git a/src/notify/notify.c b/src/notify/notify.c index 8a551a9399..8d8e6db072 100644 --- a/src/notify/notify.c +++ b/src/notify/notify.c @@ -215,11 +215,11 @@ static int parse_argv(int argc, char *argv[]) { _cleanup_close_ int owned_fd = -EBADF; int fdnr; - r = safe_atoi(optarg, &fdnr); - if (r < 0) - return log_error_errno(r, "Failed to parse file descriptor: %s", optarg); + fdnr = parse_fd(optarg); + if (fdnr == -ERANGE) + return log_error_errno(fdnr, "File descriptor can't be negative: %s", optarg); if (fdnr < 0) - return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "File descriptor can't be negative: %i", fdnr); + return log_error_errno(fdnr, "Failed to parse file descriptor: %s", optarg); if (!passed) { /* Take possession of all passed fds */ diff --git a/src/shared/bpf-program.c b/src/shared/bpf-program.c index f4bb7f390c..d924a973ec 100644 --- a/src/shared/bpf-program.c +++ b/src/shared/bpf-program.c @@ -449,11 +449,11 @@ int bpf_program_deserialize_attachment(const char *v, FDSet *fds, BPFProgram **b if (r == 0) return -EINVAL; - r = safe_atoi(sfd, &ifd); - if (r < 0) - return r; - if (ifd < 0) + ifd = parse_fd(sfd); + if (ifd == -ERANGE) return -EBADF; + if (ifd < 0) + return r; /* Extract second word: the attach type */ r = extract_first_word(&v, &sat, NULL, 0); diff --git a/src/shared/varlink.c b/src/shared/varlink.c index 808e2b2dba..a4936bff26 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -3060,12 +3060,12 @@ int varlink_server_deserialize_one(VarlinkServer *s, const char *value, FDSet *f n = strcspn(v, " "); buf = strndupa_safe(v, n); - r = safe_atoi(buf, &fd); - if (r < 0) - return log_debug_errno(r, "Unable to parse VarlinkServerSocket varlink-server-socket-fd=%s: %m", buf); - if (fd < 0) + fd = parse_fd(buf); + if (fd == -ERANGE) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), - "VarlinkServerSocket varlink-server-socket-fd= has an invalid value: %d", fd); + "VarlinkServerSocket varlink-server-socket-fd= has an invalid value: %s", buf); + if (fd < 0) + return log_debug_errno(fd, "Unable to parse VarlinkServerSocket varlink-server-socket-fd=%s: %m", buf); if (!fdset_contains(fds, fd)) return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "VarlinkServerSocket varlink-server-socket-fd= has unknown fd %d: %m", fd); |