1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
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.
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.
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/>.
24 #include <sys/ioctl.h>
37 int sd_memfd_new(sd_memfd **m) {
38 _cleanup_close_ int kdbus = -1;
45 kdbus = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
49 if (ioctl(kdbus, KDBUS_CMD_MEMFD_NEW, &fd) < 0)
52 n = new0(struct sd_memfd, 1);
61 int sd_memfd_make(int fd, sd_memfd **m) {
70 /* Check if this is a valid memfd */
71 if (ioctl(fd, KDBUS_CMD_MEMFD_SIZE_GET, &sz) < 0)
74 n = new0(struct sd_memfd, 1);
84 void sd_memfd_free(sd_memfd *m) {
91 close_nointr_nofail(m->fd);
96 int sd_memfd_get_fd(sd_memfd *m) {
103 int sd_memfd_get_file(sd_memfd *m, FILE **f) {
110 m->f = fdopen(m->fd, "r+");
119 int sd_memfd_dup_fd(sd_memfd *m) {
125 fd = fcntl(m->fd, F_DUPFD_CLOEXEC, 3);
132 int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
143 sealed = sd_memfd_get_sealed(m);
147 q = mmap(NULL, size, sealed ? PROT_READ : PROT_READ|PROT_WRITE, MAP_SHARED, m->fd, offset);
155 int sd_memfd_set_sealed(sd_memfd *m, int b) {
161 r = ioctl(m->fd, KDBUS_CMD_MEMFD_SEAL_SET, b);
168 int sd_memfd_get_sealed(sd_memfd *m) {
174 r = ioctl(m->fd, KDBUS_CMD_MEMFD_SEAL_GET, &b);
181 int sd_memfd_get_size(sd_memfd *m, uint64_t *sz) {
189 r = ioctl(m->fd, KDBUS_CMD_MEMFD_SIZE_GET, sz);
196 int sd_memfd_set_size(sd_memfd *m, uint64_t sz) {
202 r = ioctl(m->fd, KDBUS_CMD_MEMFD_SIZE_SET, &sz);
209 int sd_memfd_new_and_map(sd_memfd **m, size_t sz, void **p) {
213 r = sd_memfd_new(&n);
217 r = sd_memfd_set_size(n, sz);
223 r = sd_memfd_map(n, 0, sz, p);