]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/memfd-util.c
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 int memfd_map(int fd
, uint64_t offset
, size_t size
, void **p
) {
67 sealed
= memfd_get_sealed(fd
);
72 q
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, offset
);
74 q
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, offset
);
83 int memfd_set_sealed(int fd
) {
88 r
= fcntl(fd
, F_ADD_SEALS
, F_SEAL_SHRINK
| F_SEAL_GROW
| F_SEAL_WRITE
| F_SEAL_SEAL
);
95 int memfd_get_sealed(int fd
) {
100 r
= fcntl(fd
, F_GET_SEALS
);
104 return r
== (F_SEAL_SHRINK
| F_SEAL_GROW
| F_SEAL_WRITE
| F_SEAL_SEAL
);
107 int memfd_get_size(int fd
, uint64_t *sz
) {
114 r
= fstat(fd
, &stat
);
122 int memfd_set_size(int fd
, uint64_t sz
) {
127 r
= ftruncate(fd
, sz
);
134 int memfd_new_and_map(const char *name
, size_t sz
, void **p
) {
135 _cleanup_close_
int fd
= -1;
141 fd
= memfd_new(name
);
145 r
= memfd_set_size(fd
, sz
);
149 r
= memfd_map(fd
, 0, sz
, p
);