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