test-bus-memfd.c revision 607553f9306286fdccf0b356bc3d1087adfe21c4
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
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/>.
***/
#include "log.h"
#include "macro.h"
#include "util.h"
#include "sd-memfd.h"
sd_memfd *m;
char *s;
int r, fd;
FILE *f;
char buf[3] = {};
r = sd_memfd_new(&m);
if (r == -ENOENT)
return EXIT_TEST_SKIP;
r = sd_memfd_map(m, 0, 12, (void**) &s);
assert_se(r >= 0);
strcpy(s, "----- world");
r = sd_memfd_set_sealed(m, 1);
/* fix "hello" */
assert_se(sd_memfd_get_file(m, &f) >= 0);
fputc('o', f);
fflush(f);
/* check content */
r = sd_memfd_get_sealed(m);
assert_se(r == 0);
r = sd_memfd_get_size(m, &sz);
assert_se(r >= 0);
/* truncate it */
r = sd_memfd_set_size(m, 6);
assert_se(r >= 0);
/* get back new value */
r = sd_memfd_get_size(m, &sz);
assert_se(r >= 0);
r = sd_memfd_set_sealed(m, 1);
assert_se(r >= 0);
r = sd_memfd_get_sealed(m);
assert_se(r == 1);
fd = sd_memfd_dup_fd(m);
sd_memfd_free(m);
/* new sd_memfd, same underlying memfd */
r = sd_memfd_make(fd, &m);
assert_se(r >= 0);
/* we did truncate it to 6 */
r = sd_memfd_get_size(m, &sz);
/* map it, check content */
r = sd_memfd_map(m, 0, 12, (void **)&s);
assert_se(r >= 0);
/* we only see the truncated size */
/* it was already sealed */
r = sd_memfd_set_sealed(m, 1);
/* we cannot break the seal, it is mapped */
r = sd_memfd_set_sealed(m, 0);
/* unmap it; become the single owner */
/* now we can do flip the sealing */
r = sd_memfd_set_sealed(m, 0);
assert_se(r == 0);
r = sd_memfd_get_sealed(m);
assert_se(r == 0);
r = sd_memfd_set_sealed(m, 1);
assert_se(r == 0);
r = sd_memfd_get_sealed(m);
assert_se(r == 1);
r = sd_memfd_set_sealed(m, 0);
assert_se(r == 0);
r = sd_memfd_get_sealed(m);
assert_se(r == 0);
/* seek at 2, read() 2 bytes */
/* check content */
/* writev it out*/
/* readv it back */
/* check content */
sd_memfd_free(m);
return 0;
}