1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/ioctl.h>
26 #include <sys/prctl.h>
29 #include "bus-label.h"
40 int sd_memfd_new(sd_memfd **m, const char *name) {
42 _cleanup_free_ char *g = NULL;
45 assert_return(m, -EINVAL);
48 /* The kernel side is pretty picky about the character
49 * set here, let's do the usual bus escaping to deal
52 g = bus_label_escape(name);
61 /* If no name is specified we generate one. We include
62 * a hint indicating our library implementation, and
63 * add the thread name to it */
65 assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
70 _cleanup_free_ char *e = NULL;
72 e = bus_label_escape(pr);
76 g = strappend("sd-", e);
84 n = new0(struct sd_memfd, 1);
88 n->fd = memfd_create(name, MFD_ALLOW_SEALING);
98 int sd_memfd_new_from_fd(sd_memfd **m, int fd) {
102 assert_return(m, -EINVAL);
103 assert_return(fd >= 0, -EINVAL);
105 /* Check if this is a sealable fd. The kernel sets F_SEAL_SEAL on memfds
106 * that don't support sealing, so check for that, too. A file with
107 * *only* F_SEAL_SEAL set is the same as a random shmem file, so no
108 * reason to allow opening it as memfd. */
109 r = fcntl(fd, F_GET_SEALS);
110 if (r < 0 || r == F_SEAL_SEAL)
113 n = new0(struct sd_memfd, 1);
123 void sd_memfd_free(sd_memfd *m) {
135 int sd_memfd_get_fd(sd_memfd *m) {
136 assert_return(m, -EINVAL);
141 int sd_memfd_get_file(sd_memfd *m, FILE **f) {
142 assert_return(m, -EINVAL);
143 assert_return(f, -EINVAL);
146 m->f = fdopen(m->fd, "r+");
155 int sd_memfd_dup_fd(sd_memfd *m) {
158 assert_return(m, -EINVAL);
160 fd = fcntl(m->fd, F_DUPFD_CLOEXEC, 3);
167 int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
171 assert_return(m, -EINVAL);
172 assert_return(size > 0, -EINVAL);
173 assert_return(p, -EINVAL);
175 sealed = sd_memfd_get_sealed(m);
180 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, m->fd, offset);
182 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, offset);
191 int sd_memfd_set_sealed(sd_memfd *m) {
194 assert_return(m, -EINVAL);
196 r = fcntl(m->fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
203 int sd_memfd_get_sealed(sd_memfd *m) {
206 assert_return(m, -EINVAL);
208 r = fcntl(m->fd, F_GET_SEALS);
212 return (r & (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)) ==
213 (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
216 int sd_memfd_get_size(sd_memfd *m, uint64_t *sz) {
220 assert_return(m, -EINVAL);
221 assert_return(sz, -EINVAL);
223 r = fstat(m->fd, &stat);
231 int sd_memfd_set_size(sd_memfd *m, uint64_t sz) {
234 assert_return(m, -EINVAL);
236 r = ftruncate(m->fd, sz);
243 int sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p) {
247 r = sd_memfd_new(&n, name);
251 r = sd_memfd_set_size(n, sz);
257 r = sd_memfd_map(n, 0, sz, p);
267 int sd_memfd_get_name(sd_memfd *m, char **name) {
268 char path[sizeof("/proc/self/fd/") + DECIMAL_STR_MAX(int)], buf[FILENAME_MAX+1], *e;
269 const char *delim, *end;
270 _cleanup_free_ char *n = NULL;
273 assert_return(m, -EINVAL);
274 assert_return(name, -EINVAL);
276 sprintf(path, "/proc/self/fd/%i", m->fd);
278 k = readlink(path, buf, sizeof(buf));
282 if ((size_t) k >= sizeof(buf))
287 delim = strstr(buf, ":[");
291 delim = strchr(delim + 2, ':');
297 end = strchr(delim, ']');
301 n = strndup(delim, end - delim);
305 e = bus_label_unescape(n);