chiark / gitweb /
memfd: skip utf-8 escaping if we use a name that was passed in
[elogind.git] / src / shared / memfd.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 <stdio.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <sys/mman.h>
26 #include <sys/prctl.h>
27
28 #include "util.h"
29 #include "bus-label.h"
30 #include "missing.h"
31 #include "memfd.h"
32 #include "utf8.h"
33
34 int memfd_new(const char *name) {
35
36         _cleanup_free_ char *g = NULL;
37         int fd;
38
39         if (name) {
40                 g = utf8_escape_invalid(name);
41                 if (!g)
42                         return -ENOMEM;
43
44                 name = g;
45         } else {
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                         g = strappend("sd-", pr);
58                         if (!g)
59                                 return -ENOMEM;
60
61                         name = g;
62                 }
63         }
64
65         fd = memfd_create(name, MFD_ALLOW_SEALING);
66         if (fd < 0)
67                 return -errno;
68
69         return fd;
70 }
71
72 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
73         void *q;
74         int sealed;
75
76         assert(fd >= 0);
77         assert(size > 0);
78         assert(p);
79
80         sealed = memfd_get_sealed(fd);
81         if (sealed < 0)
82                 return sealed;
83
84         if (sealed)
85                 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
86         else
87                 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
88
89         if (q == MAP_FAILED)
90                 return -errno;
91
92         *p = q;
93         return 0;
94 }
95
96 int memfd_set_sealed(int fd) {
97         int r;
98
99         assert(fd >= 0);
100
101         r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
102         if (r < 0)
103                 return -errno;
104
105         return 0;
106 }
107
108 int memfd_get_sealed(int fd) {
109         int r;
110
111         assert(fd >= 0);
112
113         r = fcntl(fd, F_GET_SEALS);
114         if (r < 0)
115                 return -errno;
116
117         return (r & (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)) ==
118                     (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
119 }
120
121 int memfd_get_size(int fd, uint64_t *sz) {
122         struct stat stat;
123         int r;
124
125         assert(fd >= 0);
126         assert(sz);
127
128         r = fstat(fd, &stat);
129         if (r < 0)
130                 return -errno;
131
132         *sz = stat.st_size;
133         return r;
134 }
135
136 int memfd_set_size(int fd, uint64_t sz) {
137         int r;
138
139         assert(fd >= 0);
140
141         r = ftruncate(fd, sz);
142         if (r < 0)
143                 return -errno;
144
145         return r;
146 }
147
148 int memfd_new_and_map(const char *name, size_t sz, void **p) {
149         _cleanup_close_ int fd = -1;
150         int r;
151
152         assert(sz > 0);
153         assert(p);
154
155         fd = memfd_new(name);
156         if (fd < 0)
157                 return fd;
158
159         r = memfd_set_size(fd, sz);
160         if (r < 0)
161                 return r;
162
163         r = memfd_map(fd, 0, sz, p);
164         if (r < 0)
165                 return r;
166
167         r = fd;
168         fd = -1;
169
170         return r;
171 }
172
173 int memfd_get_name(int fd, char **name) {
174         char path[sizeof("/proc/self/fd/") + DECIMAL_STR_MAX(int)], buf[FILENAME_MAX+1], *e;
175         const char *delim, *end;
176         _cleanup_free_ char *n = NULL;
177         ssize_t k;
178
179         assert(fd >= 0);
180         assert(name);
181
182         sprintf(path, "/proc/self/fd/%i", fd);
183
184         k = readlink(path, buf, sizeof(buf));
185         if (k < 0)
186                 return -errno;
187
188         if ((size_t) k >= sizeof(buf))
189                 return -E2BIG;
190
191         buf[k] = 0;
192
193         delim = strstr(buf, ":[");
194         if (!delim)
195                 return -EIO;
196
197         delim = strchr(delim + 2, ':');
198         if (!delim)
199                 return -EIO;
200
201         delim++;
202
203         end = strchr(delim, ']');
204         if (!end)
205                 return -EIO;
206
207         n = strndup(delim, end - delim);
208         if (!n)
209                 return -ENOMEM;
210
211         e = utf8_escape_invalid(n);
212         if (!e)
213                 return -ENOMEM;
214
215         *name = e;
216
217         return 0;
218 }