From d2132d3d8d68e4f5dbe935c8af7a50eb124bcb8e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 6 May 2023 14:11:08 +0900 Subject: [PATCH] parse-util: make parse_fd() return -EBADF The previous error code -ERANGE is slightly ambiguous, and use more specific one. This also drops unnecessary error handlings. Follow-up for 754d8b9c330150fdb3767491e24975f7dfe2a203 and e652663a043cb80936bb12ad5c87766fc5150c24. --- src/basic/parse-util.c | 2 +- src/core/main.c | 4 +--- src/notify/notify.c | 2 -- src/shared/bpf-program.c | 2 -- src/shared/varlink.c | 3 --- src/test/test-parse-util.c | 4 ++-- 6 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index d6138f0295..cbe5ad6a32 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -343,7 +343,7 @@ int parse_fd(const char *t) { return r; if (fd < 0) - return -ERANGE; + return -EBADF; return fd; } diff --git a/src/core/main.c b/src/core/main.c index f36f6c4c9e..5274bcc106 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1003,10 +1003,8 @@ static int parse_argv(int argc, char *argv[]) { FILE *f; fd = parse_fd(optarg); - if (fd == -ERANGE) - return log_error_errno(fd, "Invalid serialization fd: %s", optarg); if (fd < 0) - return log_error_errno(fd, "Failed to parse deserialize option \"%s\": %m", optarg); + return log_error_errno(fd, "Failed to parse serialization fd \"%s\": %m", optarg); (void) fd_cloexec(fd, true); diff --git a/src/notify/notify.c b/src/notify/notify.c index 8d8e6db072..1aecd0e781 100644 --- a/src/notify/notify.c +++ b/src/notify/notify.c @@ -216,8 +216,6 @@ static int parse_argv(int argc, char *argv[]) { int fdnr; 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(fdnr, "Failed to parse file descriptor: %s", optarg); diff --git a/src/shared/bpf-program.c b/src/shared/bpf-program.c index d924a973ec..bbdd4f64ac 100644 --- a/src/shared/bpf-program.c +++ b/src/shared/bpf-program.c @@ -450,8 +450,6 @@ int bpf_program_deserialize_attachment(const char *v, FDSet *fds, BPFProgram **b return -EINVAL; ifd = parse_fd(sfd); - if (ifd == -ERANGE) - return -EBADF; if (ifd < 0) return r; diff --git a/src/shared/varlink.c b/src/shared/varlink.c index a4936bff26..ab97af57e2 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -3061,9 +3061,6 @@ int varlink_server_deserialize_one(VarlinkServer *s, const char *value, FDSet *f buf = strndupa_safe(v, n); fd = parse_fd(buf); - if (fd == -ERANGE) - return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), - "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)) diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c index 75fc3d9455..1ba8c8987f 100644 --- a/src/test/test-parse-util.c +++ b/src/test/test-parse-util.c @@ -869,8 +869,8 @@ TEST(parse_fd) { assert_se(parse_fd("0") == 0); assert_se(parse_fd("1") == 1); - assert_se(parse_fd("-1") == -ERANGE); - assert_se(parse_fd("-3") == -ERANGE); + assert_se(parse_fd("-1") == -EBADF); + assert_se(parse_fd("-3") == -EBADF); assert_se(parse_fd("") == -EINVAL); assert_se(parse_fd("12.3") == -EINVAL);