1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <linux/memfd.h>
12 #include <sys/prctl.h>
14 #include "alloc-util.h"
17 #include "memfd-util.h"
19 #include "string-util.h"
22 int memfd_new(const char *name) {
23 _cleanup_free_ char *g = NULL;
29 /* If no name is specified we generate one. We include
30 * a hint indicating our library implementation, and
31 * add the thread name to it */
33 assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
38 _cleanup_free_ char *e = NULL;
40 e = utf8_escape_invalid(pr);
44 g = strappend("sd-", e);
52 fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
59 #if 0 /// UNNEEDED by elogind
60 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
68 sealed = memfd_get_sealed(fd);
73 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
75 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
85 int memfd_set_sealed(int fd) {
90 r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
97 #if 0 /// UNNEEDED by elogind
98 int memfd_get_sealed(int fd) {
103 r = fcntl(fd, F_GET_SEALS);
107 return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
111 int memfd_get_size(int fd, uint64_t *sz) {
118 r = fstat(fd, &stat);
126 int memfd_set_size(int fd, uint64_t sz) {
131 r = ftruncate(fd, sz);
138 #if 0 /// UNNEEDED by elogind
139 int memfd_new_and_map(const char *name, size_t sz, void **p) {
140 _cleanup_close_ int fd = -1;
146 fd = memfd_new(name);
150 r = memfd_set_size(fd, sz);
154 r = memfd_map(fd, 0, sz, p);