memfd-util.c revision 07630cea1f3a845c09309f197ac7c4f11edd3b62
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen This file is part of systemd.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Copyright 2013 Lennart Poettering
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is free software; you can redistribute it and/or modify it
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen under the terms of the GNU Lesser General Public License as published by
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen the Free Software Foundation; either version 2.1 of the License, or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is distributed in the hope that it will be useful, but
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Lesser General Public License for more details.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen You should have received a copy of the GNU Lesser General Public License
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint memfd_new(const char *name) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *g = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* If no name is specified we generate one. We include
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * a hint indicating our library implementation, and
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * add the thread name to it */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *e = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint memfd_map(int fd, uint64_t offset, size_t size, void **p) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen sealed = memfd_get_sealed(fd);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint memfd_set_sealed(int fd) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint memfd_get_sealed(int fd) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint memfd_new_and_map(const char *name, size_t sz, void **p) {