summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-08-19 19:39:16 +0200
committerLennart Poettering <lennart@poettering.net>2014-08-19 19:39:16 +0200
commit4531a9bc206c0c22e29b56ac4a7528afba2e9c83 (patch)
tree6ea5ee56ccd2d9b16234e83e1326bef649962471
parent8a0dec98a3d18666da739c984f8952865d6e0ce3 (diff)
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.
-rw-r--r--src/libsystemd/sd-bus/test-bus-zero-copy.c8
-rw-r--r--src/shared/memfd.c62
-rw-r--r--src/shared/memfd.h5
3 files changed, 39 insertions, 36 deletions
diff --git a/src/libsystemd/sd-bus/test-bus-zero-copy.c b/src/libsystemd/sd-bus/test-bus-zero-copy.c
index 1d279e6032..e3010fbf7e 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 2b0d26d9ed..6e2319d6ee 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 02cb3978fa..8f02b0ff55 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);