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) {
101 assert_return(m, -EINVAL);
102 assert_return(fd >= 0, -EINVAL);
104 /* Check if this is a sealable fd */
105 if (fcntl(fd, F_GET_SEALS) < 0)
108 n = new0(struct sd_memfd, 1);
118 void sd_memfd_free(sd_memfd *m) {
130 int sd_memfd_get_fd(sd_memfd *m) {
131 assert_return(m, -EINVAL);
136 int sd_memfd_get_file(sd_memfd *m, FILE **f) {
137 assert_return(m, -EINVAL);
138 assert_return(f, -EINVAL);
141 m->f = fdopen(m->fd, "r+");
150 int sd_memfd_dup_fd(sd_memfd *m) {
153 assert_return(m, -EINVAL);
155 fd = fcntl(m->fd, F_DUPFD_CLOEXEC, 3);
162 int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
166 assert_return(m, -EINVAL);
167 assert_return(size > 0, -EINVAL);
168 assert_return(p, -EINVAL);
170 sealed = sd_memfd_get_sealed(m);
174 q = mmap(NULL, size, sealed ? PROT_READ : PROT_READ|PROT_WRITE, MAP_PRIVATE, m->fd, offset);
182 int sd_memfd_set_sealed(sd_memfd *m) {
185 assert_return(m, -EINVAL);
187 r = fcntl(m->fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
194 int sd_memfd_get_sealed(sd_memfd *m) {
197 assert_return(m, -EINVAL);
199 r = fcntl(m->fd, F_GET_SEALS);
203 return (r & (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)) ==
204 (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
207 int sd_memfd_get_size(sd_memfd *m, uint64_t *sz) {
211 assert_return(m, -EINVAL);
212 assert_return(sz, -EINVAL);
214 r = fstat(m->fd, &stat);
222 int sd_memfd_set_size(sd_memfd *m, uint64_t sz) {
225 assert_return(m, -EINVAL);
227 r = ftruncate(m->fd, sz);
234 int sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p) {
238 r = sd_memfd_new(&n, name);
242 r = sd_memfd_set_size(n, sz);
248 r = sd_memfd_map(n, 0, sz, p);
258 int sd_memfd_get_name(sd_memfd *m, char **name) {
259 char path[sizeof("/proc/self/fd/") + DECIMAL_STR_MAX(int)], buf[FILENAME_MAX+1], *e;
260 const char *delim, *end;
261 _cleanup_free_ char *n = NULL;
264 assert_return(m, -EINVAL);
265 assert_return(name, -EINVAL);
267 sprintf(path, "/proc/self/fd/%i", m->fd);
269 k = readlink(path, buf, sizeof(buf));
273 if ((size_t) k >= sizeof(buf))
278 delim = strstr(buf, ":[");
282 delim = strchr(delim + 2, ':');
288 end = strchr(delim, ']');
292 n = strndup(delim, end - delim);
296 e = bus_label_unescape(n);