summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mack <zonque@gmail.com>2014-08-18 13:28:43 +0200
committerDaniel Mack <zonque@gmail.com>2014-08-18 13:32:08 +0200
commitfac9c0d508f72cc5d469c969a4acc3694247c03b (patch)
treede438cd330880b7b99771780dc82deaecf3f03f6
parent8a02decaf1e81bad3c06752e998734c96ab11260 (diff)
memfd: internalize functions, drop sd_memfd type
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.
-rw-r--r--man/sd_bus_message_append_array.xml2
-rw-r--r--man/sd_bus_message_append_string_memfd.xml2
-rw-r--r--src/libsystemd/sd-bus/bus-message.c20
-rw-r--r--src/libsystemd/sd-bus/test-bus-zero-copy.c14
-rw-r--r--src/shared/memfd.c146
-rw-r--r--src/shared/memfd.h27
-rw-r--r--src/systemd/sd-bus.h4
7 files changed, 62 insertions, 153 deletions
diff --git a/man/sd_bus_message_append_array.xml b/man/sd_bus_message_append_array.xml
index e0f6767ec2..ab1fcd26cb 100644
--- a/man/sd_bus_message_append_array.xml
+++ b/man/sd_bus_message_append_array.xml
@@ -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>
diff --git a/man/sd_bus_message_append_string_memfd.xml b/man/sd_bus_message_append_string_memfd.xml
index fd857ccb19..d18ca1a858 100644
--- a/man/sd_bus_message_append_string_memfd.xml
+++ b/man/sd_bus_message_append_string_memfd.xml
@@ -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>
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
index 79dc471d41..3e60842172 100644
--- a/src/libsystemd/sd-bus/bus-message.c
+++ b/src/libsystemd/sd-bus/bus-message.c
@@ -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;
diff --git a/src/libsystemd/sd-bus/test-bus-zero-copy.c b/src/libsystemd/sd-bus/test-bus-zero-copy.c
index e4a87ab3c5..1d279e6032 100644
--- a/src/libsystemd/sd-bus/test-bus-zero-copy.c
+++ b/src/libsystemd/sd-bus/test-bus-zero-copy.c
@@ -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);
diff --git a/src/shared/memfd.c b/src/shared/memfd.c
index e246f915ca..2b0d26d9ed 100644
--- a/src/shared/memfd.c
+++ b/src/shared/memfd.c
@@ -32,17 +32,12 @@
#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)
diff --git a/src/shared/memfd.h b/src/shared/memfd.h
index 452fb508f8..02cb3978fa 100644
--- a/src/shared/memfd.h
+++ b/src/shared/memfd.h
@@ -27,26 +27,15 @@
#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);
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index a69cafbe84..c601093a85 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -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);