chiark / gitweb /
Major cleanup of all leftovers after rebasing on master.
[elogind.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 <stdio.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25 #include <sys/prctl.h>
26
27 #ifdef HAVE_LINUX_MEMFD_H
28 #  include <linux/memfd.h>
29 #endif
30
31 #include "util.h"
32 #include "memfd-util.h"
33 #include "utf8.h"
34 #include "missing.h"
35
36 int memfd_new(const char *name) {
37         _cleanup_free_ char *g = NULL;
38         int fd;
39
40         if (!name) {
41                 char pr[17] = {};
42
43                 /* If no name is specified we generate one. We include
44                  * a hint indicating our library implementation, and
45                  * add the thread name to it */
46
47                 assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
48
49                 if (isempty(pr))
50                         name = "sd";
51                 else {
52                         _cleanup_free_ char *e = NULL;
53
54                         e = utf8_escape_invalid(pr);
55                         if (!e)
56                                 return -ENOMEM;
57
58                         g = strappend("sd-", e);
59                         if (!g)
60                                 return -ENOMEM;
61
62                         name = g;
63                 }
64         }
65
66         fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
67         if (fd < 0)
68                 return -errno;
69
70         return fd;
71 }
72
73 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
74         void *q;
75         int sealed;
76
77         assert(fd >= 0);
78         assert(size > 0);
79         assert(p);
80
81         sealed = memfd_get_sealed(fd);
82         if (sealed < 0)
83                 return sealed;
84
85         if (sealed)
86                 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
87         else
88                 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
89
90         if (q == MAP_FAILED)
91                 return -errno;
92
93         *p = q;
94         return 0;
95 }
96
97 int memfd_set_sealed(int fd) {
98         int r;
99
100         assert(fd >= 0);
101
102         r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
103         if (r < 0)
104                 return -errno;
105
106         return 0;
107 }
108
109 int memfd_get_sealed(int fd) {
110         int r;
111
112         assert(fd >= 0);
113
114         r = fcntl(fd, F_GET_SEALS);
115         if (r < 0)
116                 return -errno;
117
118         return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
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 0;
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 0;
146 }
147
148 /// UNNEEDED by elogind
149 #if 0
150 int memfd_new_and_map(const char *name, size_t sz, void **p) {
151         _cleanup_close_ int fd = -1;
152         int r;
153
154         assert(sz > 0);
155         assert(p);
156
157         fd = memfd_new(name);
158         if (fd < 0)
159                 return fd;
160
161         r = memfd_set_size(fd, sz);
162         if (r < 0)
163                 return r;
164
165         r = memfd_map(fd, 0, sz, p);
166         if (r < 0)
167                 return r;
168
169         r = fd;
170         fd = -1;
171
172         return r;
173 }
174 #endif // 0