2013-05-10 14:34:57 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <errno.h>
|
2013-05-10 14:34:57 +02:00
|
|
|
#include <fcntl.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_LINUX_MEMFD_H
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <linux/memfd.h>
|
2014-10-06 00:41:31 +02:00
|
|
|
#endif
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/prctl.h>
|
2014-10-06 00:41:31 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "macro.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "memfd-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "missing.h"
|
|
|
|
#include "string-util.h"
|
|
|
|
#include "utf8.h"
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
int memfd_new(const char *name) {
|
2014-01-22 13:06:04 +01:00
|
|
|
_cleanup_free_ char *g = NULL;
|
2014-08-19 19:39:16 +02:00
|
|
|
int fd;
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-19 22:35:04 +02:00
|
|
|
if (!name) {
|
2014-01-22 13:06:04 +01:00
|
|
|
char pr[17] = {};
|
|
|
|
|
|
|
|
/* If no name is specified we generate one. We include
|
|
|
|
* a hint indicating our library implementation, and
|
|
|
|
* add the thread name to it */
|
|
|
|
|
|
|
|
assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
|
|
|
|
|
|
|
|
if (isempty(pr))
|
|
|
|
name = "sd";
|
|
|
|
else {
|
2014-08-19 22:35:04 +02:00
|
|
|
_cleanup_free_ char *e = NULL;
|
|
|
|
|
|
|
|
e = utf8_escape_invalid(pr);
|
|
|
|
if (!e)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
g = strappend("sd-", e);
|
2014-01-22 13:06:04 +01:00
|
|
|
if (!g)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
name = g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 16:23:34 +01:00
|
|
|
fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
|
2014-08-19 19:39:16 +02:00
|
|
|
if (fd < 0)
|
2014-05-13 01:28:09 +02:00
|
|
|
return -errno;
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
return fd;
|
2013-05-10 14:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
|
2013-05-10 14:34:57 +02:00
|
|
|
void *q;
|
|
|
|
int sealed;
|
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
assert(fd >= 0);
|
|
|
|
assert(size > 0);
|
|
|
|
assert(p);
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
sealed = memfd_get_sealed(fd);
|
2013-05-10 14:34:57 +02:00
|
|
|
if (sealed < 0)
|
|
|
|
return sealed;
|
|
|
|
|
2014-08-18 13:05:48 +02:00
|
|
|
if (sealed)
|
2014-08-18 13:28:43 +02:00
|
|
|
q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
|
2014-08-18 13:05:48 +02:00
|
|
|
else
|
2014-08-18 13:28:43 +02:00
|
|
|
q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
|
2014-08-18 13:05:48 +02:00
|
|
|
|
2013-05-10 14:34:57 +02:00
|
|
|
if (q == MAP_FAILED)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
*p = q;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
int memfd_set_sealed(int fd) {
|
2013-05-10 14:34:57 +02:00
|
|
|
int r;
|
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
assert(fd >= 0);
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-11-04 18:52:31 +01:00
|
|
|
r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
|
2013-05-10 14:34:57 +02:00
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
int memfd_get_sealed(int fd) {
|
2014-05-13 01:28:09 +02:00
|
|
|
int r;
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
assert(fd >= 0);
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
r = fcntl(fd, F_GET_SEALS);
|
2013-05-10 14:34:57 +02:00
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2014-11-04 18:52:31 +01:00
|
|
|
return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
|
2013-05-10 14:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
int memfd_get_size(int fd, uint64_t *sz) {
|
2014-05-13 01:28:09 +02:00
|
|
|
struct stat stat;
|
2014-08-19 19:39:16 +02:00
|
|
|
int r;
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
assert(fd >= 0);
|
|
|
|
assert(sz);
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
r = fstat(fd, &stat);
|
2013-05-10 14:34:57 +02:00
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2014-05-13 01:28:09 +02:00
|
|
|
*sz = stat.st_size;
|
2014-08-19 22:35:04 +02:00
|
|
|
return 0;
|
2013-05-10 14:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
int memfd_set_size(int fd, uint64_t sz) {
|
2013-05-10 14:34:57 +02:00
|
|
|
int r;
|
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
assert(fd >= 0);
|
2013-05-10 14:34:57 +02:00
|
|
|
|
2014-08-18 13:28:43 +02:00
|
|
|
r = ftruncate(fd, sz);
|
2013-05-10 14:34:57 +02:00
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2014-08-19 22:35:04 +02:00
|
|
|
return 0;
|
2013-05-10 14:34:57 +02:00
|
|
|
}
|
2013-05-15 19:45:05 +02:00
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
int memfd_new_and_map(const char *name, size_t sz, void **p) {
|
|
|
|
_cleanup_close_ int fd = -1;
|
2013-05-15 19:45:05 +02:00
|
|
|
int r;
|
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
assert(sz > 0);
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
fd = memfd_new(name);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
2013-05-15 19:45:05 +02:00
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
r = memfd_set_size(fd, sz);
|
2014-08-18 13:07:43 +02:00
|
|
|
if (r < 0)
|
2013-05-15 19:45:05 +02:00
|
|
|
return r;
|
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
r = memfd_map(fd, 0, sz, p);
|
2014-08-18 13:07:43 +02:00
|
|
|
if (r < 0)
|
2013-05-15 19:45:05 +02:00
|
|
|
return r;
|
|
|
|
|
2014-08-19 19:39:16 +02:00
|
|
|
r = fd;
|
|
|
|
fd = -1;
|
|
|
|
|
|
|
|
return r;
|
2013-05-15 19:45:05 +02:00
|
|
|
}
|