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