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/>.
30 int main(int argc, char *argv[]) {
37 struct iovec iov[3] = {};
40 log_set_max_level(LOG_DEBUG);
44 return EXIT_TEST_SKIP;
46 r = sd_memfd_map(m, 0, 12, (void**) &s);
49 strcpy(s, "----- world");
51 r = sd_memfd_set_sealed(m, 1);
52 assert_se(r == -EPERM);
54 assert_se(write(sd_memfd_get_fd(m), "he", 2) == 2);
55 assert_se(write(sd_memfd_get_fd(m), "XXX", 3) == 3);
56 assert_se(streq(s, "heXXX world"));
59 assert_se(lseek(sd_memfd_get_fd(m), 2, SEEK_SET) == 2);
60 assert_se(write(sd_memfd_get_fd(m), "ll", 2) == 2);
62 assert_se(sd_memfd_get_file(m, &f) >= 0);
67 assert_se(streq(s, "hello world"));
69 assert_se(munmap(s, 12) == 0);
71 r = sd_memfd_get_sealed(m);
74 r = sd_memfd_get_size(m, &sz);
76 assert_se(sz = page_size());
79 r = sd_memfd_set_size(m, 6);
82 /* get back new value */
83 r = sd_memfd_get_size(m, &sz);
87 r = sd_memfd_set_sealed(m, 1);
90 r = sd_memfd_get_sealed(m);
93 fd = sd_memfd_dup_fd(m);
98 /* new sd_memfd, same underlying memfd */
99 r = sd_memfd_make(fd, &m);
102 /* we did truncate it to 6 */
103 r = sd_memfd_get_size(m, &sz);
106 /* map it, check content */
107 r = sd_memfd_map(m, 0, 12, (void **)&s);
110 /* we only see the truncated size */
111 assert_se(streq(s, "hello "));
113 /* it was already sealed */
114 r = sd_memfd_set_sealed(m, 1);
115 assert_se(r == -EALREADY);
117 /* we cannot break the seal, it is mapped */
118 r = sd_memfd_set_sealed(m, 0);
119 assert_se(r == -EPERM);
121 /* unmap it; become the single owner */
122 assert_se(munmap(s, 12) == 0);
124 /* now we can do flip the sealing */
125 r = sd_memfd_set_sealed(m, 0);
127 r = sd_memfd_get_sealed(m);
130 r = sd_memfd_set_sealed(m, 1);
132 r = sd_memfd_get_sealed(m);
135 r = sd_memfd_set_sealed(m, 0);
137 r = sd_memfd_get_sealed(m);
140 /* seek at 2, read() 2 bytes */
141 assert_se(lseek(fd, 2, SEEK_SET) == 2);
142 assert_se(read(fd, buf, 2) == 2);
145 assert_se(memcmp(buf, "ll", 2) == 0);
148 iov[0].iov_base = (char *)"ABC";
150 iov[1].iov_base = (char *)"DEF";
152 iov[2].iov_base = (char *)"GHI";
154 assert_se(lseek(fd, 0, SEEK_SET) == 0);
155 assert_se(writev(fd, iov, 3) == 9);
158 iov[0].iov_base = bufv[0];
160 iov[1].iov_base = bufv[1];
162 iov[2].iov_base = bufv[2];
164 assert_se(lseek(fd, 0, SEEK_SET) == 0);
165 assert_se(readv(fd, iov, 3) == 9);
168 assert_se(memcmp(bufv[0], "ABC", 3) == 0);
169 assert_se(memcmp(bufv[1], "DEF", 3) == 0);
170 assert_se(memcmp(bufv[2], "GHI", 3) == 0);