[systemd-commits] src/libsystemd src/shared

Lennart Poettering lennart at kemper.freedesktop.org
Tue Aug 19 10:40:32 PDT 2014


 src/libsystemd/sd-bus/test-bus-zero-copy.c |    8 +--
 src/shared/memfd.c                         |   62 ++++++++++++++---------------
 src/shared/memfd.h                         |    5 +-
 3 files changed, 39 insertions(+), 36 deletions(-)

New commits:
commit 4531a9bc206c0c22e29b56ac4a7528afba2e9c83
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Aug 19 19:39:16 2014 +0200

    memfd: simplify API
    
    Now, that the memfd stuff is not exported anymore, we can simplify a few
    things:
    
    Use assert() instead of assert_return(), since this is used internally
    only, and we should be less permissive then.
    
    No need to pass an allocated fd back by call-by-reference, we can just
    directly return it.

diff --git a/src/libsystemd/sd-bus/test-bus-zero-copy.c b/src/libsystemd/sd-bus/test-bus-zero-copy.c
index 1d279e6..e3010fb 100644
--- a/src/libsystemd/sd-bus/test-bus-zero-copy.c
+++ b/src/libsystemd/sd-bus/test-bus-zero-copy.c
@@ -93,8 +93,8 @@ int main(int argc, char *argv[]) {
         memset(p+1, 'L', FIRST_ARRAY-2);
         p[FIRST_ARRAY-1] = '>';
 
-        r = memfd_new_and_map(&f, NULL, STRING_SIZE, (void**) &s);
-        assert_se(r >= 0);
+        f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
+        assert_se(f >= 0);
 
         s[0] = '<';
         for (i = 1; i < STRING_SIZE-2; i++)
@@ -112,8 +112,8 @@ int main(int argc, char *argv[]) {
 
         close(f);
 
-        r = memfd_new_and_map(&f, NULL, SECOND_ARRAY, (void**) &p);
-        assert_se(r >= 0);
+        f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
+        assert_se(f >= 0);
 
         p[0] = '<';
         memset(p+1, 'P', SECOND_ARRAY-2);
diff --git a/src/shared/memfd.c b/src/shared/memfd.c
index 2b0d26d..6e2319d 100644
--- a/src/shared/memfd.c
+++ b/src/shared/memfd.c
@@ -30,14 +30,12 @@
 #include "missing.h"
 #include "memfd.h"
 
-#include "sd-bus.h"
-
-int memfd_new(int *fd, const char *name) {
+int memfd_new(const char *name) {
 
         _cleanup_free_ char *g = NULL;
-        int n;
+        int fd;
 
-        assert_return(fd, -EINVAL);
+        assert(name);
 
         if (name) {
                 /* The kernel side is pretty picky about the character
@@ -76,21 +74,20 @@ int memfd_new(int *fd, const char *name) {
                 }
         }
 
-        n = memfd_create(name, MFD_ALLOW_SEALING);
-        if (n < 0)
+        fd = memfd_create(name, MFD_ALLOW_SEALING);
+        if (fd < 0)
                 return -errno;
 
-        *fd = n;
-        return 0;
+        return fd;
 }
 
 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
         void *q;
         int sealed;
 
-        assert_return(fd >= 0, -EINVAL);
-        assert_return(size > 0, -EINVAL);
-        assert_return(p, -EINVAL);
+        assert(fd >= 0);
+        assert(size > 0);
+        assert(p);
 
         sealed = memfd_get_sealed(fd);
         if (sealed < 0)
@@ -111,7 +108,7 @@ int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
 int memfd_set_sealed(int fd) {
         int r;
 
-        assert_return(fd >= 0, -EINVAL);
+        assert(fd >= 0);
 
         r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
         if (r < 0)
@@ -123,7 +120,7 @@ int memfd_set_sealed(int fd) {
 int memfd_get_sealed(int fd) {
         int r;
 
-        assert_return(fd >= 0, -EINVAL);
+        assert(fd >= 0);
 
         r = fcntl(fd, F_GET_SEALS);
         if (r < 0)
@@ -134,11 +131,11 @@ int memfd_get_sealed(int fd) {
 }
 
 int memfd_get_size(int fd, uint64_t *sz) {
-        int r;
         struct stat stat;
+        int r;
 
-        assert_return(fd >= 0, -EINVAL);
-        assert_return(sz, -EINVAL);
+        assert(fd >= 0);
+        assert(sz);
 
         r = fstat(fd, &stat);
         if (r < 0)
@@ -151,7 +148,7 @@ int memfd_get_size(int fd, uint64_t *sz) {
 int memfd_set_size(int fd, uint64_t sz) {
         int r;
 
-        assert_return(fd >= 0, -EINVAL);
+        assert(fd >= 0);
 
         r = ftruncate(fd, sz);
         if (r < 0)
@@ -160,25 +157,30 @@ int memfd_set_size(int fd, uint64_t sz) {
         return r;
 }
 
-int memfd_new_and_map(int *fd, const char *name, size_t sz, void **p) {
-        _cleanup_close_ int n = -1;
+int memfd_new_and_map(const char *name, size_t sz, void **p) {
+        _cleanup_close_ int fd = -1;
         int r;
 
-        r = memfd_new(&n, name);
-        if (r < 0)
-                return r;
+        assert(name);
+        assert(sz > 0);
+        assert(p);
+
+        fd = memfd_new(name);
+        if (fd < 0)
+                return fd;
 
-        r = memfd_set_size(n, sz);
+        r = memfd_set_size(fd, sz);
         if (r < 0)
                 return r;
 
-        r = memfd_map(n, 0, sz, p);
+        r = memfd_map(fd, 0, sz, p);
         if (r < 0)
                 return r;
 
-        *fd = n;
-        n = -1;
-        return 0;
+        r = fd;
+        fd = -1;
+
+        return r;
 }
 
 int memfd_get_name(int fd, char **name) {
@@ -187,8 +189,8 @@ int memfd_get_name(int fd, char **name) {
         _cleanup_free_ char *n = NULL;
         ssize_t k;
 
-        assert_return(fd >= 0, -EINVAL);
-        assert_return(name, -EINVAL);
+        assert(fd >= 0);
+        assert(name);
 
         sprintf(path, "/proc/self/fd/%i", fd);
 
diff --git a/src/shared/memfd.h b/src/shared/memfd.h
index 02cb397..8f02b0f 100644
--- a/src/shared/memfd.h
+++ b/src/shared/memfd.h
@@ -24,11 +24,12 @@
 #include <inttypes.h>
 #include <sys/types.h>
 #include <stdio.h>
+
 #include "macro.h"
 #include "util.h"
 
-int memfd_new(int *fd, const char *name);
-int memfd_new_and_map(int *fd, const char *name, size_t sz, void **p);
+int memfd_new(const char *name);
+int memfd_new_and_map(const char *name, size_t sz, void **p);
 
 int memfd_map(int fd, uint64_t offset, size_t size, void **p);
 



More information about the systemd-commits mailing list