chiark / gitweb /
memfd: internalize functions, drop sd_memfd type
authorDaniel Mack <zonque@gmail.com>
Mon, 18 Aug 2014 11:28:43 +0000 (13:28 +0200)
committerDaniel Mack <zonque@gmail.com>
Mon, 18 Aug 2014 11:32:08 +0000 (13:32 +0200)
Remove the sd_ prefix from internal functions and get rid of the sd_memfd
type. As a memfd is now just a native file descriptor, we can get rid of our
own wrapper type, and also use close() and dup() on them directly.

man/sd_bus_message_append_array.xml
man/sd_bus_message_append_string_memfd.xml
src/libsystemd/sd-bus/bus-message.c
src/libsystemd/sd-bus/test-bus-zero-copy.c
src/shared/memfd.c
src/shared/memfd.h
src/systemd/sd-bus.h

index e0f6767ec2132b87410ed58525a228b5359fa24c..ab1fcd26cbab502d41a3442bd13a26f67f94e656 100644 (file)
@@ -68,7 +68,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
         <funcdef>int sd_bus_message_append_array_memfd</funcdef>
         <paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
         <paramdef>char <parameter>type</parameter></paramdef>
-        <paramdef>sd_memfd *<parameter>memfd</parameter></paramdef>
+        <paramdef>int <parameter>memfd</parameter></paramdef>
       </funcprototype>
 
       <funcprototype>
index fd857ccb19b3dfbbee49176a7fb48a452dada36b..d18ca1a85824492e97458637f7519ff4fcfb79bc 100644 (file)
@@ -58,7 +58,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
       <funcprototype>
         <funcdef>int sd_bus_message_append_string_memfd</funcdef>
         <paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
-        <paramdef>sd_memfd *<parameter>memfd</parameter></paramdef>
+        <paramdef>int <parameter>memfd</parameter></paramdef>
       </funcprototype>
 
       <funcprototype>
index 79dc471d41e3b027529ca02d2b488bae9f6d763c..3e60842172d2d7356e8eb952011d0d8bbcf9bcf4 100644 (file)
@@ -2527,7 +2527,7 @@ _public_ int sd_bus_message_append_array_iovec(
 
 _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
                                                char type,
-                                               sd_memfd *memfd) {
+                                               int memfd) {
         _cleanup_close_ int copy_fd = -1;
         struct bus_body_part *part;
         ssize_t align, sz;
@@ -2537,7 +2537,7 @@ _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
 
         if (!m)
                 return -EINVAL;
-        if (!memfd)
+        if (memfd < 0)
                 return -EINVAL;
         if (m->sealed)
                 return -EPERM;
@@ -2546,15 +2546,15 @@ _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
         if (m->poisoned)
                 return -ESTALE;
 
-        r = sd_memfd_set_sealed(memfd);
+        r = memfd_set_sealed(memfd);
         if (r < 0)
                 return r;
 
-        copy_fd = sd_memfd_dup_fd(memfd);
+        copy_fd = dup(memfd);
         if (copy_fd < 0)
                 return copy_fd;
 
-        r = sd_memfd_get_size(memfd, &size);
+        r = memfd_get_size(memfd, &size);
         if (r < 0)
                 return r;
 
@@ -2593,7 +2593,7 @@ _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
         return sd_bus_message_close_container(m);
 }
 
-_public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) {
+_public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, int memfd) {
         _cleanup_close_ int copy_fd = -1;
         struct bus_body_part *part;
         struct bus_container *c;
@@ -2602,19 +2602,19 @@ _public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *mem
         int r;
 
         assert_return(m, -EINVAL);
-        assert_return(memfd, -EINVAL);
+        assert_return(memfd >= 0, -EINVAL);
         assert_return(!m->sealed, -EPERM);
         assert_return(!m->poisoned, -ESTALE);
 
-        r = sd_memfd_set_sealed(memfd);
+        r = memfd_set_sealed(memfd);
         if (r < 0)
                 return r;
 
-        copy_fd = sd_memfd_dup_fd(memfd);
+        copy_fd = dup(memfd);
         if (copy_fd < 0)
                 return copy_fd;
 
-        r = sd_memfd_get_size(memfd, &size);
+        r = memfd_get_size(memfd, &size);
         if (r < 0)
                 return r;
 
index e4a87ab3c530157cdf96ad62c4164b0441270639..1d279e6032e3a543df7e65f6301c001e282a12a1 100644 (file)
@@ -43,7 +43,7 @@ int main(int argc, char *argv[]) {
         sd_bus *a, *b;
         int r, bus_ref;
         sd_bus_message *m;
-        sd_memfd *f;
+        int f;
         uint64_t sz;
         uint32_t u32;
         size_t i, l;
@@ -93,7 +93,7 @@ int main(int argc, char *argv[]) {
         memset(p+1, 'L', FIRST_ARRAY-2);
         p[FIRST_ARRAY-1] = '>';
 
-        r = sd_memfd_new_and_map(&f, NULL, STRING_SIZE, (void**) &s);
+        r = memfd_new_and_map(&f, NULL, STRING_SIZE, (void**) &s);
         assert_se(r >= 0);
 
         s[0] = '<';
@@ -103,16 +103,16 @@ int main(int argc, char *argv[]) {
         s[STRING_SIZE-1] = 0;
         munmap(s, STRING_SIZE);
 
-        r = sd_memfd_get_size(f, &sz);
+        r = memfd_get_size(f, &sz);
         assert_se(r >= 0);
         assert_se(sz == STRING_SIZE);
 
         r = sd_bus_message_append_string_memfd(m, f);
         assert_se(r >= 0);
 
-        sd_memfd_free(f);
+        close(f);
 
-        r = sd_memfd_new_and_map(&f, NULL, SECOND_ARRAY, (void**) &p);
+        r = memfd_new_and_map(&f, NULL, SECOND_ARRAY, (void**) &p);
         assert_se(r >= 0);
 
         p[0] = '<';
@@ -120,14 +120,14 @@ int main(int argc, char *argv[]) {
         p[SECOND_ARRAY-1] = '>';
         munmap(p, SECOND_ARRAY);
 
-        r = sd_memfd_get_size(f, &sz);
+        r = memfd_get_size(f, &sz);
         assert_se(r >= 0);
         assert_se(sz == SECOND_ARRAY);
 
         r = sd_bus_message_append_array_memfd(m, 'y', f);
         assert_se(r >= 0);
 
-        sd_memfd_free(f);
+        close(f);
 
         r = sd_bus_message_close_container(m);
         assert_se(r >= 0);
index e246f915ca9a1cb2ca19c1bb5cbe48ff3e9e2dc9..2b0d26d9eda3d8f46689537ce523628b28c66210 100644 (file)
 
 #include "sd-bus.h"
 
-struct sd_memfd {
-        int fd;
-        FILE *f;
-};
-
-int sd_memfd_new(sd_memfd **m, const char *name) {
+int memfd_new(int *fd, const char *name) {
 
         _cleanup_free_ char *g = NULL;
-        sd_memfd *n;
+        int n;
 
-        assert_return(m, -EINVAL);
+        assert_return(fd, -EINVAL);
 
         if (name) {
                 /* The kernel side is pretty picky about the character
@@ -81,105 +76,30 @@ int sd_memfd_new(sd_memfd **m, const char *name) {
                 }
         }
 
-        n = new0(struct sd_memfd, 1);
-        if (!n)
-                return -ENOMEM;
-
-        n->fd = memfd_create(name, MFD_ALLOW_SEALING);
-        if (n->fd < 0) {
-                free(n);
+        n = memfd_create(name, MFD_ALLOW_SEALING);
+        if (n < 0)
                 return -errno;
-        }
 
-        *m = n;
+        *fd = n;
         return 0;
 }
 
-int sd_memfd_new_from_fd(sd_memfd **m, int fd) {
-        sd_memfd *n;
-        int r;
-
-        assert_return(m, -EINVAL);
-        assert_return(fd >= 0, -EINVAL);
-
-        /* Check if this is a sealable fd. The kernel sets F_SEAL_SEAL on memfds
-         * that don't support sealing, so check for that, too. A file with
-         * *only* F_SEAL_SEAL set is the same as a random shmem file, so no
-         * reason to allow opening it as memfd. */
-        r = fcntl(fd, F_GET_SEALS);
-        if (r < 0 || r == F_SEAL_SEAL)
-                return -ENOTTY;
-
-        n = new0(struct sd_memfd, 1);
-        if (!n)
-                return -ENOMEM;
-
-        n->fd = fd;
-        *m = n;
-
-        return 0;
-}
-
-void sd_memfd_free(sd_memfd *m) {
-        if (!m)
-                return;
-
-        if (m->f)
-                fclose(m->f);
-        else
-                safe_close(m->fd);
-
-        free(m);
-}
-
-int sd_memfd_get_fd(sd_memfd *m) {
-        assert_return(m, -EINVAL);
-
-        return m->fd;
-}
-
-int sd_memfd_get_file(sd_memfd *m, FILE **f) {
-        assert_return(m, -EINVAL);
-        assert_return(f, -EINVAL);
-
-        if (!m->f) {
-                m->f = fdopen(m->fd, "r+");
-                if (!m->f)
-                        return -errno;
-        }
-
-        *f = m->f;
-        return 0;
-}
-
-int sd_memfd_dup_fd(sd_memfd *m) {
-        int fd;
-
-        assert_return(m, -EINVAL);
-
-        fd = fcntl(m->fd, F_DUPFD_CLOEXEC, 3);
-        if (fd < 0)
-                return -errno;
-
-        return fd;
-}
-
-int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
+int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
         void *q;
         int sealed;
 
-        assert_return(m, -EINVAL);
+        assert_return(fd >= 0, -EINVAL);
         assert_return(size > 0, -EINVAL);
         assert_return(p, -EINVAL);
 
-        sealed = sd_memfd_get_sealed(m);
+        sealed = memfd_get_sealed(fd);
         if (sealed < 0)
                 return sealed;
 
         if (sealed)
-                q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, m->fd, offset);
+                q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
         else
-                q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, offset);
+                q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
 
         if (q == MAP_FAILED)
                 return -errno;
@@ -188,24 +108,24 @@ int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
         return 0;
 }
 
-int sd_memfd_set_sealed(sd_memfd *m) {
+int memfd_set_sealed(int fd) {
         int r;
 
-        assert_return(m, -EINVAL);
+        assert_return(fd >= 0, -EINVAL);
 
-        r = fcntl(m->fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
+        r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
         if (r < 0)
                 return -errno;
 
         return 0;
 }
 
-int sd_memfd_get_sealed(sd_memfd *m) {
+int memfd_get_sealed(int fd) {
         int r;
 
-        assert_return(m, -EINVAL);
+        assert_return(fd >= 0, -EINVAL);
 
-        r = fcntl(m->fd, F_GET_SEALS);
+        r = fcntl(fd, F_GET_SEALS);
         if (r < 0)
                 return -errno;
 
@@ -213,14 +133,14 @@ int sd_memfd_get_sealed(sd_memfd *m) {
                     (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
 }
 
-int sd_memfd_get_size(sd_memfd *m, uint64_t *sz) {
+int memfd_get_size(int fd, uint64_t *sz) {
         int r;
         struct stat stat;
 
-        assert_return(m, -EINVAL);
+        assert_return(fd >= 0, -EINVAL);
         assert_return(sz, -EINVAL);
 
-        r = fstat(m->fd, &stat);
+        r = fstat(fd, &stat);
         if (r < 0)
                 return -errno;
 
@@ -228,49 +148,49 @@ int sd_memfd_get_size(sd_memfd *m, uint64_t *sz) {
         return r;
 }
 
-int sd_memfd_set_size(sd_memfd *m, uint64_t sz) {
+int memfd_set_size(int fd, uint64_t sz) {
         int r;
 
-        assert_return(m, -EINVAL);
+        assert_return(fd >= 0, -EINVAL);
 
-        r = ftruncate(m->fd, sz);
+        r = ftruncate(fd, sz);
         if (r < 0)
                 return -errno;
 
         return r;
 }
 
-int sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p) {
-        _cleanup_(sd_memfd_freep) sd_memfd *n = NULL;
+int memfd_new_and_map(int *fd, const char *name, size_t sz, void **p) {
+        _cleanup_close_ int n = -1;
         int r;
 
-        r = sd_memfd_new(&n, name);
+        r = memfd_new(&n, name);
         if (r < 0)
                 return r;
 
-        r = sd_memfd_set_size(n, sz);
+        r = memfd_set_size(n, sz);
         if (r < 0)
                 return r;
 
-        r = sd_memfd_map(n, 0, sz, p);
+        r = memfd_map(n, 0, sz, p);
         if (r < 0)
                 return r;
 
-        *m = n;
-        n = NULL;
+        *fd = n;
+        n = -1;
         return 0;
 }
 
-int sd_memfd_get_name(sd_memfd *m, char **name) {
+int memfd_get_name(int fd, char **name) {
         char path[sizeof("/proc/self/fd/") + DECIMAL_STR_MAX(int)], buf[FILENAME_MAX+1], *e;
         const char *delim, *end;
         _cleanup_free_ char *n = NULL;
         ssize_t k;
 
-        assert_return(m, -EINVAL);
+        assert_return(fd >= 0, -EINVAL);
         assert_return(name, -EINVAL);
 
-        sprintf(path, "/proc/self/fd/%i", m->fd);
+        sprintf(path, "/proc/self/fd/%i", fd);
 
         k = readlink(path, buf, sizeof(buf));
         if (k < 0)
index 452fb508f893f994c70fd759dcd6fea8c7acfce4..02cb3978fa985da53579275841caee9247a54208 100644 (file)
 #include "macro.h"
 #include "util.h"
 
-typedef struct sd_memfd sd_memfd;
+int memfd_new(int *fd, const char *name);
+int memfd_new_and_map(int *fd, const char *name, size_t sz, void **p);
 
-int sd_memfd_new(sd_memfd **m, const char *name);
-int sd_memfd_new_from_fd(sd_memfd **m, int fd);
-int sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p);
+int memfd_map(int fd, uint64_t offset, size_t size, void **p);
 
-void sd_memfd_free(sd_memfd *m);
+int memfd_set_sealed(int fd);
+int memfd_get_sealed(int fd);
 
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_memfd*, sd_memfd_free);
+int memfd_get_size(int fd, uint64_t *sz);
+int memfd_set_size(int fd, uint64_t sz);
 
-int sd_memfd_get_fd(sd_memfd *m);
-int sd_memfd_dup_fd(sd_memfd *n);
-int sd_memfd_get_file(sd_memfd *m, FILE **f);
-
-int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p);
-
-int sd_memfd_set_sealed(sd_memfd *m);
-int sd_memfd_get_sealed(sd_memfd *m);
-
-int sd_memfd_get_size(sd_memfd *m, uint64_t *sz);
-int sd_memfd_set_size(sd_memfd *m, uint64_t sz);
-
-int sd_memfd_get_name(sd_memfd *m, char **name);
+int memfd_get_name(int fd, char **name);
index a69cafbe84299709fbaf7d6299a162de71575d5d..c601093a85d0ffba3d1cfa3421e127bdd8b5d317 100644 (file)
@@ -226,10 +226,10 @@ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p);
 int sd_bus_message_append_array(sd_bus_message *m, char type, const void *ptr, size_t size);
 int sd_bus_message_append_array_space(sd_bus_message *m, char type, size_t size, void **ptr);
 int sd_bus_message_append_array_iovec(sd_bus_message *m, char type, const struct iovec *iov, unsigned n);
-int sd_bus_message_append_array_memfd(sd_bus_message *m, char type, sd_memfd *memfd);
+int sd_bus_message_append_array_memfd(sd_bus_message *m, char type, int memfd);
 int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s);
 int sd_bus_message_append_string_iovec(sd_bus_message *m, const struct iovec *iov, unsigned n);
-int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd* memfd);
+int sd_bus_message_append_string_memfd(sd_bus_message *m, int memfd);
 int sd_bus_message_append_strv(sd_bus_message *m, char **l);
 int sd_bus_message_open_container(sd_bus_message *m, char type, const char *contents);
 int sd_bus_message_close_container(sd_bus_message *m);