1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

memfd-util: simplify memfd_new_and_seal()

Let's use pwrite() to write the contents of the memfd. This has the
benefit of not moving the file offset, which means we don't have to
reset it after at all.
This commit is contained in:
Lennart Poettering 2024-12-13 18:55:00 +01:00
parent a87a9625f8
commit db5381c49c

View File

@ -171,8 +171,6 @@ int memfd_new_and_map(const char *name, size_t sz, void **p) {
int memfd_new_and_seal(const char *name, const void *data, size_t sz) { int memfd_new_and_seal(const char *name, const void *data, size_t sz) {
_cleanup_close_ int fd = -EBADF; _cleanup_close_ int fd = -EBADF;
ssize_t n;
off_t f;
int r; int r;
assert(data || sz == 0); assert(data || sz == 0);
@ -185,15 +183,11 @@ int memfd_new_and_seal(const char *name, const void *data, size_t sz) {
return fd; return fd;
if (sz > 0) { if (sz > 0) {
n = write(fd, data, sz); ssize_t n = pwrite(fd, data, sz, 0);
if (n < 0) if (n < 0)
return -errno; return -errno;
if ((size_t) n != sz) if ((size_t) n != sz)
return -EIO; return -EIO;
f = lseek(fd, 0, SEEK_SET);
if (f != 0)
return -errno;
} }
r = memfd_set_sealed(fd); r = memfd_set_sealed(fd);