chiark / gitweb /
bus: fix parsing of AcquireName() response
[elogind.git] / src / libsystemd-bus / sd-memfd.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/ioctl.h>
25 #include <sys/mman.h>
26
27 #include "util.h"
28 #include "kdbus.h"
29
30 #include "sd-memfd.h"
31
32 struct sd_memfd {
33         int fd;
34         FILE *f;
35 };
36
37 _public_ int sd_memfd_new(sd_memfd **m) {
38         _cleanup_close_ int kdbus = -1;
39         sd_memfd *n;
40         int fd;
41
42         if (!m)
43                 return -EINVAL;
44
45         kdbus = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
46         if (kdbus < 0)
47                 return -errno;
48
49         if (ioctl(kdbus, KDBUS_CMD_MEMFD_NEW, &fd) < 0)
50                 return -errno;
51
52         n = new0(struct sd_memfd, 1);
53         if (!n)
54                 return -ENOMEM;
55
56         n->fd = fd;
57         *m = n;
58         return 0;
59 }
60
61 _public_ int sd_memfd_make(int fd, sd_memfd **m) {
62         sd_memfd *n;
63         uint64_t sz;
64
65         if (!m)
66                 return -EINVAL;
67         if (fd < 0)
68                 return -EINVAL;
69
70         /* Check if this is a valid memfd */
71         if (ioctl(fd, KDBUS_CMD_MEMFD_SIZE_GET, &sz) < 0)
72                 return -ENOTTY;
73
74         n = new0(struct sd_memfd, 1);
75         if (!n)
76                 return -ENOMEM;
77
78         n->fd = fd;
79         *m = n;
80
81         return 0;
82 }
83
84 _public_ void sd_memfd_free(sd_memfd *m) {
85         if (!m)
86                 return;
87
88         if (m->f)
89                 fclose(m->f);
90         else
91                 close_nointr_nofail(m->fd);
92
93         free(m);
94 }
95
96 _public_ int sd_memfd_get_fd(sd_memfd *m) {
97         if (!m)
98                 return -EINVAL;
99
100         return m->fd;
101 }
102
103 _public_ int sd_memfd_get_file(sd_memfd *m, FILE **f) {
104         if (!m)
105                 return -EINVAL;
106         if (!f)
107                 return -EINVAL;
108
109         if (!m->f) {
110                 m->f = fdopen(m->fd, "r+");
111                 if (!m->f)
112                         return -errno;
113         }
114
115         *f = m->f;
116         return 0;
117 }
118
119 _public_ int sd_memfd_dup_fd(sd_memfd *m) {
120         int fd;
121
122         if (!m)
123                 return -EINVAL;
124
125         fd = fcntl(m->fd, F_DUPFD_CLOEXEC, 3);
126         if (fd < 0)
127                 return -errno;
128
129         return fd;
130 }
131
132 _public_ int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
133         void *q;
134         int sealed;
135
136         if (!m)
137                 return -EINVAL;
138         if (size <= 0)
139                 return -EINVAL;
140         if (!p)
141                 return -EINVAL;
142
143         sealed = sd_memfd_get_sealed(m);
144         if (sealed < 0)
145                 return sealed;
146
147         q = mmap(NULL, size, sealed ? PROT_READ : PROT_READ|PROT_WRITE, MAP_SHARED, m->fd, offset);
148         if (q == MAP_FAILED)
149                 return -errno;
150
151         *p = q;
152         return 0;
153 }
154
155 _public_ int sd_memfd_set_sealed(sd_memfd *m, int b) {
156         int r;
157
158         if (!m)
159                 return -EINVAL;
160
161         r = ioctl(m->fd, KDBUS_CMD_MEMFD_SEAL_SET, b);
162         if (r < 0)
163                 return -errno;
164
165         return 0;
166 }
167
168 _public_ int sd_memfd_get_sealed(sd_memfd *m) {
169         int r, b;
170
171         if (!m)
172                 return -EINVAL;
173
174         r = ioctl(m->fd, KDBUS_CMD_MEMFD_SEAL_GET, &b);
175         if (r < 0)
176                 return -errno;
177
178         return !!b;
179 }
180
181 _public_ int sd_memfd_get_size(sd_memfd *m, uint64_t *sz) {
182         int r;
183
184         if (!m)
185                 return -EINVAL;
186         if (!sz)
187                 return -EINVAL;
188
189         r = ioctl(m->fd, KDBUS_CMD_MEMFD_SIZE_GET, sz);
190         if (r < 0)
191                 return -errno;
192
193         return r;
194 }
195
196 _public_ int sd_memfd_set_size(sd_memfd *m, uint64_t sz) {
197         int r;
198
199         if (!m)
200                 return -EINVAL;
201
202         r = ioctl(m->fd, KDBUS_CMD_MEMFD_SIZE_SET, &sz);
203         if (r < 0)
204                 return -errno;
205
206         return r;
207 }
208
209 _public_ int sd_memfd_new_and_map(sd_memfd **m, size_t sz, void **p) {
210         sd_memfd *n;
211         int r;
212
213         r = sd_memfd_new(&n);
214         if (r < 0)
215                 return r;
216
217         r = sd_memfd_set_size(n, sz);
218         if (r < 0) {
219                 sd_memfd_free(n);
220                 return r;
221         }
222
223         r = sd_memfd_map(n, 0, sz, p);
224         if (r < 0) {
225                 sd_memfd_free(n);
226                 return r;
227         }
228
229         *m = n;
230         return 0;
231 }