data-fd-util: accept size == SIZE_MAX and translate that to strlen

Like what we already do at other places
This commit is contained in:
Mike Yuan
2024-03-12 16:42:48 +08:00
parent 7799e7d9f0
commit 0870fc2404
11 changed files with 27 additions and 19 deletions
+1 -1
View File
@@ -1444,7 +1444,7 @@ static int method_dump(sd_bus_message *message, void *userdata, sd_bus_error *er
static int reply_dump_by_fd(sd_bus_message *message, char *dump) {
_cleanup_close_ int fd = -EBADF;
fd = acquire_data_fd(dump, strlen(dump), 0);
fd = acquire_data_fd(dump);
if (fd < 0)
return fd;
+1 -1
View File
@@ -410,7 +410,7 @@ static int setup_input(
case EXEC_INPUT_DATA: {
int fd;
fd = acquire_data_fd(context->stdin_data, context->stdin_data_size, 0);
fd = acquire_data_fd_full(context->stdin_data, context->stdin_data_size, /* flags = */ 0);
if (fd < 0)
return fd;
+1 -1
View File
@@ -1258,7 +1258,7 @@ static int home_start_work(
if (r < 0)
return r;
stdin_fd = acquire_data_fd(formatted, strlen(formatted), 0);
stdin_fd = acquire_data_fd(formatted);
if (stdin_fd < 0)
return stdin_fd;
+1 -1
View File
@@ -76,7 +76,7 @@ int home_setup_cifs(
pid_t mount_pid;
int exit_status;
passwd_fd = acquire_data_fd(*pw, strlen(*pw), /* flags= */ 0);
passwd_fd = acquire_data_fd(*pw);
if (passwd_fd < 0)
return log_error_errno(passwd_fd, "Failed to create data FD for password: %m");
+1 -1
View File
@@ -22,7 +22,7 @@ static int bus_method_dump_by_fd(sd_bus_message *message, void *userdata, sd_bus
if (r < 0)
return r;
fd = acquire_data_fd(dump, strlen(dump), 0);
fd = acquire_data_fd(dump);
if (fd < 0)
return fd;
+1 -1
View File
@@ -627,7 +627,7 @@ static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdat
if (r < 0)
return r;
fd = acquire_data_fd(dump, dump_size, 0);
fd = acquire_data_fd_full(dump, dump_size, /* flags = */ 0);
if (fd < 0)
return fd;
+4 -1
View File
@@ -25,7 +25,7 @@
/* If memfd/pipe didn't work out, then let's use a file in /tmp up to a size of 1M. If it's large than that use /var/tmp instead. */
#define DATA_FD_TMP_LIMIT (1U * U64_MB)
int acquire_data_fd(const void *data, size_t size, DataFDFlags flags) {
int acquire_data_fd_full(const void *data, size_t size, DataFDFlags flags) {
_cleanup_close_ int fd = -EBADF;
ssize_t n;
int r;
@@ -51,6 +51,9 @@ int acquire_data_fd(const void *data, size_t size, DataFDFlags flags) {
* It sucks a bit that depending on the situation we return very different objects here, but that's Linux I
* figure. */
if (size == SIZE_MAX)
size = strlen(data);
if (size == 0 && !FLAGS_SET(flags, ACQUIRE_NO_DEV_NULL))
/* As a special case, return /dev/null if we have been called for an empty data block */
return RET_NERRNO(open("/dev/null", O_RDONLY|O_CLOEXEC|O_NOCTTY));
+6 -1
View File
@@ -2,6 +2,7 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
typedef enum DataFDFlags {
ACQUIRE_NO_DEV_NULL = 1 << 0,
@@ -11,6 +12,10 @@ typedef enum DataFDFlags {
ACQUIRE_NO_REGULAR = 1 << 4,
} DataFDFlags;
int acquire_data_fd(const void *data, size_t size, DataFDFlags flags);
int acquire_data_fd_full(const void *data, size_t size, DataFDFlags flags);
static inline int acquire_data_fd(const void *data) {
return acquire_data_fd_full(data, SIZE_MAX, 0);
}
int copy_data_fd(int fd);
int memfd_clone_fd(int fd, const char *name, int mode);
+5 -5
View File
@@ -17,7 +17,7 @@ static void test_acquire_data_fd_one(unsigned flags) {
char rbuffer[sizeof(wbuffer)];
int fd;
fd = acquire_data_fd("foo", 3, flags);
fd = acquire_data_fd_full("foo", 3, flags);
assert_se(fd >= 0);
zero(rbuffer);
@@ -26,7 +26,7 @@ static void test_acquire_data_fd_one(unsigned flags) {
fd = safe_close(fd);
fd = acquire_data_fd("", 0, flags);
fd = acquire_data_fd_full("", SIZE_MAX, flags);
assert_se(fd >= 0);
zero(rbuffer);
@@ -37,7 +37,7 @@ static void test_acquire_data_fd_one(unsigned flags) {
random_bytes(wbuffer, sizeof(wbuffer));
fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags);
fd = acquire_data_fd_full(wbuffer, sizeof(wbuffer), flags);
assert_se(fd >= 0);
zero(rbuffer);
@@ -98,14 +98,14 @@ TEST(copy_data_fd) {
fd1 = safe_close(fd1);
fd2 = safe_close(fd2);
fd1 = acquire_data_fd("hallo", 6, 0);
fd1 = acquire_data_fd("hallo");
assert_se(fd1 >= 0);
fd2 = copy_data_fd(fd1);
assert_se(fd2 >= 0);
safe_close(fd1);
fd1 = acquire_data_fd("hallo", 6, 0);
fd1 = acquire_data_fd("hallo");
assert_se(fd1 >= 0);
assert_equal_fd(fd1, fd2);
+1 -1
View File
@@ -170,7 +170,7 @@ TEST(rearrange_stdio) {
assert_se(fd_move_above_stdio(0) == 3);
}
assert_se(open("/dev/full", O_WRONLY|O_CLOEXEC) == 0);
assert_se(acquire_data_fd("foobar", 6, 0) == 2);
assert_se(acquire_data_fd("foobar") == 2);
assert_se(rearrange_stdio(2, 0, 1) >= 0);
+5 -5
View File
@@ -123,8 +123,8 @@ static int method_passfd(Varlink *link, JsonVariant *parameters, VarlinkMethodFl
test_fd(yy, "bar", 3);
test_fd(zz, "quux", 4);
_cleanup_close_ int vv = acquire_data_fd("miau", 4, 0);
_cleanup_close_ int ww = acquire_data_fd("wuff", 4, 0);
_cleanup_close_ int vv = acquire_data_fd("miau");
_cleanup_close_ int ww = acquire_data_fd("wuff");
assert_se(vv >= 0);
assert_se(ww >= 0);
@@ -275,9 +275,9 @@ static void *thread(void *arg) {
assert_se(json_variant_integer(json_variant_by_key(o, "sum")) == 88 + 99);
assert_se(!e);
int fd1 = acquire_data_fd("foo", 3, 0);
int fd2 = acquire_data_fd("bar", 3, 0);
int fd3 = acquire_data_fd("quux", 4, 0);
int fd1 = acquire_data_fd("foo");
int fd2 = acquire_data_fd("bar");
int fd3 = acquire_data_fd("quux");
assert_se(fd1 >= 0);
assert_se(fd2 >= 0);