chiark / gitweb /
basic: parse_timestamp UTC and fractional seconds support
[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 /// UNNEEDED by elogind
74 #if 0
75 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
76         void *q;
77         int sealed;
78
79         assert(fd >= 0);
80         assert(size > 0);
81         assert(p);
82
83         sealed = memfd_get_sealed(fd);
84         if (sealed < 0)
85                 return sealed;
86
87         if (sealed)
88                 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
89         else
90                 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
91
92         if (q == MAP_FAILED)
93                 return -errno;
94
95         *p = q;
96         return 0;
97 }
98 #endif // 0
99
100 int memfd_set_sealed(int fd) {
101         int r;
102
103         assert(fd >= 0);
104
105         r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
106         if (r < 0)
107                 return -errno;
108
109         return 0;
110 }
111
112 int memfd_get_sealed(int fd) {
113         int r;
114
115         assert(fd >= 0);
116
117         r = fcntl(fd, F_GET_SEALS);
118         if (r < 0)
119                 return -errno;
120
121         return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
122 }
123
124 /// UNNEEDED by elogind
125 #if 0
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 /// UNNEEDED by elogind
155 #if 0
156 int memfd_new_and_map(const char *name, size_t sz, void **p) {
157         _cleanup_close_ int fd = -1;
158         int r;
159
160         assert(sz > 0);
161         assert(p);
162
163         fd = memfd_new(name);
164         if (fd < 0)
165                 return fd;
166
167         r = memfd_set_size(fd, sz);
168         if (r < 0)
169                 return r;
170
171         r = memfd_map(fd, 0, sz, p);
172         if (r < 0)
173                 return r;
174
175         r = fd;
176         fd = -1;
177
178         return r;
179 }
180 #endif // 0