]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
data-fd-util: accept size == SIZE_MAX and translate that to strlen
authorMike Yuan <me@yhndnzj.com>
Tue, 12 Mar 2024 07:42:23 +0000 (15:42 +0800)
committerMike Yuan <me@yhndnzj.com>
Tue, 12 Mar 2024 08:42:48 +0000 (16:42 +0800)
Like what we already do at other places

src/core/dbus-manager.c
src/core/exec-invoke.c
src/home/homed-home.c
src/home/homework-cifs.c
src/oom/oomd-manager-bus.c
src/shared/bus-util.c
src/shared/data-fd-util.c
src/shared/data-fd-util.h
src/test/test-data-fd-util.c
src/test/test-fd-util.c
src/test/test-varlink.c

index fbc5fce1282afe42e16a2d8e833b768b8820d3ac..11f93b0c8069958dd8aaec505c132dd6e325f99b 100644 (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;
 
index 91c7d412668c1ba128f47c4db22ae0164ad4a116..0db13bfa8807d3c7c960ff393762aa3e4dcfccd2 100644 (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;
 
index f3c72926d796715280594cf6a7873d4aa3a477f3..edde6fafd3cbb092378068b9e9933e50e9d0c450 100644 (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;
 
index 5d8713111e497792967f4c0f0173d52c36ebf6ca..bb95741eb67d81a9e3eae7132f8d816402237167 100644 (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");
 
index 0581d58016a4c5eab50a58b90873ecd86e1b7cec..7d2edb5561015941511d56860bc52b6e3b5659a4 100644 (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;
 
index 88e1249b8edb171af2d3626056e98f37cbd86137..3f54914591161175301b3b94c15ce14144ab0e90 100644 (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;
 
index 5dd27331c39ba6d58fe48d47f6cffe47a2d8509c..adc7d7417e0f3459931353e09507a398122cc59a 100644 (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));
index 2e073ee45ecc2467d3ca9d44ebc528c5ccce86c6..d77e09fb06a227a93f5279c31ef0f43245085b3b 100644 (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);
index aa68132e45f6ccfd4418c3b7c08e9975f1c495a6..6d1cfe62899b2e35718ecaf0ad29ee715336c5a9 100644 (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);
index 5dd9e48c803849646e4765b38d914442c6eb5be0..cd4ce8359739dc2ac14664dc1b62030935e7eaad 100644 (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);
 
index 65e7dfafe9611f8a4c1042960daf0cedb0519e74..cd9c5936130ff76a011427c6e5add8be2e8ce9f2 100644 (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);