]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
memfd-util: simplify memfd_new_and_seal()
authorLennart Poettering <lennart@poettering.net>
Fri, 13 Dec 2024 17:55:00 +0000 (18:55 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 17 Dec 2024 17:26:15 +0000 (18:26 +0100)
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.

src/basic/memfd-util.c

index 0ae9a4f777f4f1ae743fdcc5a230cd47d2d4f6b5..9ceb18cc2f3b51ac28afe561ad97fa748c7f766c 100644 (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) {
         _cleanup_close_ int fd = -EBADF;
-        ssize_t n;
-        off_t f;
         int r;
 
         assert(data || sz == 0);
@@ -185,15 +183,11 @@ int memfd_new_and_seal(const char *name, const void *data, size_t sz) {
                 return fd;
 
         if (sz > 0) {
-                n = write(fd, data, sz);
+                ssize_t n = pwrite(fd, data, sz, 0);
                 if (n < 0)
                         return -errno;
                 if ((size_t) n != sz)
                         return -EIO;
-
-                f = lseek(fd, 0, SEEK_SET);
-                if (f != 0)
-                        return -errno;
         }
 
         r = memfd_set_sealed(fd);