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