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"
35 int memfd_new(int *fd, const char *name) {
37 _cleanup_free_ char *g = NULL;
40 assert_return(fd, -EINVAL);
43 /* The kernel side is pretty picky about the character
44 * set here, let's do the usual bus escaping to deal
47 g = bus_label_escape(name);
56 /* If no name is specified we generate one. We include
57 * a hint indicating our library implementation, and
58 * add the thread name to it */
60 assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
65 _cleanup_free_ char *e = NULL;
67 e = bus_label_escape(pr);
71 g = strappend("sd-", e);
79 n = memfd_create(name, MFD_ALLOW_SEALING);
87 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
91 assert_return(fd >= 0, -EINVAL);
92 assert_return(size > 0, -EINVAL);
93 assert_return(p, -EINVAL);
95 sealed = memfd_get_sealed(fd);
100 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
102 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
111 int memfd_set_sealed(int fd) {
114 assert_return(fd >= 0, -EINVAL);
116 r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
123 int memfd_get_sealed(int fd) {
126 assert_return(fd >= 0, -EINVAL);
128 r = fcntl(fd, F_GET_SEALS);
132 return (r & (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)) ==
133 (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
136 int memfd_get_size(int fd, uint64_t *sz) {
140 assert_return(fd >= 0, -EINVAL);
141 assert_return(sz, -EINVAL);
143 r = fstat(fd, &stat);
151 int memfd_set_size(int fd, uint64_t sz) {
154 assert_return(fd >= 0, -EINVAL);
156 r = ftruncate(fd, sz);
163 int memfd_new_and_map(int *fd, const char *name, size_t sz, void **p) {
164 _cleanup_close_ int n = -1;
167 r = memfd_new(&n, name);
171 r = memfd_set_size(n, sz);
175 r = memfd_map(n, 0, sz, p);
184 int memfd_get_name(int fd, char **name) {
185 char path[sizeof("/proc/self/fd/") + DECIMAL_STR_MAX(int)], buf[FILENAME_MAX+1], *e;
186 const char *delim, *end;
187 _cleanup_free_ char *n = NULL;
190 assert_return(fd >= 0, -EINVAL);
191 assert_return(name, -EINVAL);
193 sprintf(path, "/proc/self/fd/%i", fd);
195 k = readlink(path, buf, sizeof(buf));
199 if ((size_t) k >= sizeof(buf))
204 delim = strstr(buf, ":[");
208 delim = strchr(delim + 2, ':');
214 end = strchr(delim, ']');
218 n = strndup(delim, end - delim);
222 e = bus_label_unescape(n);