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