]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/memfd-util.c
meson: check for sys/auxv.h
[thirdparty/systemd.git] / src / basic / memfd-util.c
CommitLineData
ddeb4241
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
11c3a366 20#include <errno.h>
ddeb4241 21#include <fcntl.h>
11c3a366
TA
22#include <sys/stat.h>
23#include <unistd.h>
27c64db6 24#ifdef HAVE_LINUX_MEMFD_H
07630cea 25#include <linux/memfd.h>
27c64db6 26#endif
07630cea
LP
27#include <stdio.h>
28#include <sys/mman.h>
29#include <sys/prctl.h>
27c64db6 30
b5efdb8a 31#include "alloc-util.h"
3ffd4af2 32#include "fd-util.h"
93cc7779 33#include "macro.h"
3ffd4af2 34#include "memfd-util.h"
07630cea
LP
35#include "missing.h"
36#include "string-util.h"
37#include "utf8.h"
ddeb4241 38
4531a9bc 39int memfd_new(const char *name) {
7f96b1d8 40 _cleanup_free_ char *g = NULL;
4531a9bc 41 int fd;
ddeb4241 42
5755381f 43 if (!name) {
7f96b1d8
LP
44 char pr[17] = {};
45
46 /* If no name is specified we generate one. We include
47 * a hint indicating our library implementation, and
48 * add the thread name to it */
49
50 assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
51
52 if (isempty(pr))
53 name = "sd";
54 else {
5755381f
LP
55 _cleanup_free_ char *e = NULL;
56
57 e = utf8_escape_invalid(pr);
58 if (!e)
59 return -ENOMEM;
60
61 g = strappend("sd-", e);
7f96b1d8
LP
62 if (!g)
63 return -ENOMEM;
64
65 name = g;
66 }
67 }
68
45071fca 69 fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
4531a9bc 70 if (fd < 0)
a6082d77 71 return -errno;
ddeb4241 72
4531a9bc 73 return fd;
ddeb4241
LP
74}
75
fac9c0d5 76int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
ddeb4241
LP
77 void *q;
78 int sealed;
79
4531a9bc
LP
80 assert(fd >= 0);
81 assert(size > 0);
82 assert(p);
ddeb4241 83
fac9c0d5 84 sealed = memfd_get_sealed(fd);
ddeb4241
LP
85 if (sealed < 0)
86 return sealed;
87
23972f42 88 if (sealed)
fac9c0d5 89 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
23972f42 90 else
fac9c0d5 91 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
23972f42 92
ddeb4241
LP
93 if (q == MAP_FAILED)
94 return -errno;
95
96 *p = q;
97 return 0;
98}
99
fac9c0d5 100int memfd_set_sealed(int fd) {
ddeb4241
LP
101 int r;
102
4531a9bc 103 assert(fd >= 0);
ddeb4241 104
db74cc0d 105 r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
ddeb4241
LP
106 if (r < 0)
107 return -errno;
108
109 return 0;
110}
111
fac9c0d5 112int memfd_get_sealed(int fd) {
a6082d77 113 int r;
ddeb4241 114
4531a9bc 115 assert(fd >= 0);
ddeb4241 116
fac9c0d5 117 r = fcntl(fd, F_GET_SEALS);
ddeb4241
LP
118 if (r < 0)
119 return -errno;
120
db74cc0d 121 return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
ddeb4241
LP
122}
123
fac9c0d5 124int memfd_get_size(int fd, uint64_t *sz) {
a6082d77 125 struct stat stat;
4531a9bc 126 int r;
ddeb4241 127
4531a9bc
LP
128 assert(fd >= 0);
129 assert(sz);
ddeb4241 130
fac9c0d5 131 r = fstat(fd, &stat);
ddeb4241
LP
132 if (r < 0)
133 return -errno;
134
a6082d77 135 *sz = stat.st_size;
5755381f 136 return 0;
ddeb4241
LP
137}
138
fac9c0d5 139int memfd_set_size(int fd, uint64_t sz) {
ddeb4241
LP
140 int r;
141
4531a9bc 142 assert(fd >= 0);
ddeb4241 143
fac9c0d5 144 r = ftruncate(fd, sz);
ddeb4241
LP
145 if (r < 0)
146 return -errno;
147
5755381f 148 return 0;
ddeb4241 149}
453a0c29 150
4531a9bc
LP
151int memfd_new_and_map(const char *name, size_t sz, void **p) {
152 _cleanup_close_ int fd = -1;
453a0c29
LP
153 int r;
154
4531a9bc
LP
155 assert(sz > 0);
156 assert(p);
157
158 fd = memfd_new(name);
159 if (fd < 0)
160 return fd;
453a0c29 161
4531a9bc 162 r = memfd_set_size(fd, sz);
8a02deca 163 if (r < 0)
453a0c29 164 return r;
453a0c29 165
4531a9bc 166 r = memfd_map(fd, 0, sz, p);
8a02deca 167 if (r < 0)
453a0c29 168 return r;
453a0c29 169
4531a9bc
LP
170 r = fd;
171 fd = -1;
172
173 return r;
453a0c29 174}