memfd.c revision 4632777024b7ba210e4efe5cfabc8cd0b5991045
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering This file is part of systemd.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering Copyright 2013 Lennart Poettering
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering systemd is free software; you can redistribute it and/or modify it
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering under the terms of the GNU Lesser General Public License as published by
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering (at your option) any later version.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering systemd is distributed in the hope that it will be useful, but
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering Lesser General Public License for more details.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering You should have received a copy of the GNU Lesser General Public License
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_new(sd_memfd **m, const char *name) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering kdbus = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering /* The kernel side is pretty picky about the character
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering * set here, let's do the usual bus escaping to deal
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering * with that. */
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering /* If no name is specified we generate one. We include
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering * a hint indicating our library implementation, and
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering * add the thread name to it */
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering n->fd = memfd_create(name, MFD_ALLOW_SEALING);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering if (n->fd < 0) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_new_from_fd(sd_memfd **m, int fd) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering /* Check if this is a sealable fd */
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_get_file(sd_memfd *m, FILE **f) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering q = mmap(NULL, size, sealed ? PROT_READ : PROT_READ|PROT_WRITE, MAP_PRIVATE, m->fd, offset);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering r = fcntl(m->fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering return (r & (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)) ==
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_get_size(sd_memfd *m, uint64_t *sz) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_set_size(sd_memfd *m, uint64_t sz) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poetteringint sd_memfd_get_name(sd_memfd *m, char **name) {
839a4a20d8b4f6ebf3a342fd05c821358be5a313Lennart Poettering char path[sizeof("/proc/self/fd/") + DECIMAL_STR_MAX(int)], buf[FILENAME_MAX+1], *e;