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