]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/memfd-util.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / basic / memfd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2013 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #if HAVE_LINUX_MEMFD_H
11 #include <linux/memfd.h>
12 #endif
13 #include <stdio.h>
14 #include <sys/mman.h>
15 #include <sys/prctl.h>
16
17 #include "alloc-util.h"
18 #include "fd-util.h"
19 #include "macro.h"
20 #include "memfd-util.h"
21 #include "missing.h"
22 #include "string-util.h"
23 #include "utf8.h"
24
25 int memfd_new(const char *name) {
26 _cleanup_free_ char *g = NULL;
27 int fd;
28
29 if (!name) {
30 char pr[17] = {};
31
32 /* If no name is specified we generate one. We include
33 * a hint indicating our library implementation, and
34 * add the thread name to it */
35
36 assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
37
38 if (isempty(pr))
39 name = "sd";
40 else {
41 _cleanup_free_ char *e = NULL;
42
43 e = utf8_escape_invalid(pr);
44 if (!e)
45 return -ENOMEM;
46
47 g = strappend("sd-", e);
48 if (!g)
49 return -ENOMEM;
50
51 name = g;
52 }
53 }
54
55 fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
56 if (fd < 0)
57 return -errno;
58
59 return fd;
60 }
61
62 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
63 void *q;
64 int sealed;
65
66 assert(fd >= 0);
67 assert(size > 0);
68 assert(p);
69
70 sealed = memfd_get_sealed(fd);
71 if (sealed < 0)
72 return sealed;
73
74 if (sealed)
75 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
76 else
77 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
78
79 if (q == MAP_FAILED)
80 return -errno;
81
82 *p = q;
83 return 0;
84 }
85
86 int memfd_set_sealed(int fd) {
87 int r;
88
89 assert(fd >= 0);
90
91 r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
92 if (r < 0)
93 return -errno;
94
95 return 0;
96 }
97
98 int memfd_get_sealed(int fd) {
99 int r;
100
101 assert(fd >= 0);
102
103 r = fcntl(fd, F_GET_SEALS);
104 if (r < 0)
105 return -errno;
106
107 return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
108 }
109
110 int memfd_get_size(int fd, uint64_t *sz) {
111 struct stat stat;
112 int r;
113
114 assert(fd >= 0);
115 assert(sz);
116
117 r = fstat(fd, &stat);
118 if (r < 0)
119 return -errno;
120
121 *sz = stat.st_size;
122 return 0;
123 }
124
125 int memfd_set_size(int fd, uint64_t sz) {
126 int r;
127
128 assert(fd >= 0);
129
130 r = ftruncate(fd, sz);
131 if (r < 0)
132 return -errno;
133
134 return 0;
135 }
136
137 int memfd_new_and_map(const char *name, size_t sz, void **p) {
138 _cleanup_close_ int fd = -1;
139 int r;
140
141 assert(sz > 0);
142 assert(p);
143
144 fd = memfd_new(name);
145 if (fd < 0)
146 return fd;
147
148 r = memfd_set_size(fd, sz);
149 if (r < 0)
150 return r;
151
152 r = memfd_map(fd, 0, sz, p);
153 if (r < 0)
154 return r;
155
156 return TAKE_FD(fd);
157 }