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/>.
31 int main(int argc, char *argv[]) {
38 struct iovec iov[3] = {};
41 log_set_max_level(LOG_DEBUG);
45 return EXIT_TEST_SKIP;
47 r = sd_memfd_map(m, 0, 12, (void**) &s);
50 strcpy(s, "----- world");
52 r = sd_memfd_set_sealed(m, 1);
53 assert_se(r == -ETXTBSY);
55 assert_se(write(sd_memfd_get_fd(m), "he", 2) == 2);
56 assert_se(write(sd_memfd_get_fd(m), "XXX", 3) == 3);
57 assert_se(streq(s, "heXXX world"));
60 assert_se(lseek(sd_memfd_get_fd(m), 2, SEEK_SET) == 2);
61 assert_se(write(sd_memfd_get_fd(m), "ll", 2) == 2);
63 assert_se(sd_memfd_get_file(m, &f) >= 0);
68 assert_se(streq(s, "hello world"));
70 assert_se(munmap(s, 12) == 0);
72 r = sd_memfd_get_sealed(m);
75 r = sd_memfd_get_size(m, &sz);
77 assert_se(sz = page_size());
80 r = sd_memfd_set_size(m, 6);
83 /* get back new value */
84 r = sd_memfd_get_size(m, &sz);
88 r = sd_memfd_set_sealed(m, 1);
91 r = sd_memfd_get_sealed(m);
94 fd = sd_memfd_dup_fd(m);
99 /* new sd_memfd, same underlying memfd */
100 r = sd_memfd_make(fd, &m);
103 /* we did truncate it to 6 */
104 r = sd_memfd_get_size(m, &sz);
105 assert_se(r >= 0 && sz == 6);
107 /* map it, check content */
108 r = sd_memfd_map(m, 0, 12, (void **)&s);
111 /* we only see the truncated size */
112 assert_se(streq(s, "hello "));
114 /* it was already sealed */
115 r = sd_memfd_set_sealed(m, 1);
116 assert_se(r == -EALREADY);
118 /* we cannot break the seal, it is mapped */
119 r = sd_memfd_set_sealed(m, 0);
120 assert_se(r == -ETXTBSY);
122 /* unmap it; become the single owner */
123 assert_se(munmap(s, 12) == 0);
125 /* now we can do flip the sealing */
126 r = sd_memfd_set_sealed(m, 0);
128 r = sd_memfd_get_sealed(m);
131 r = sd_memfd_set_sealed(m, 1);
133 r = sd_memfd_get_sealed(m);
136 r = sd_memfd_set_sealed(m, 0);
138 r = sd_memfd_get_sealed(m);
141 /* seek at 2, read() 2 bytes */
142 assert_se(lseek(fd, 2, SEEK_SET) == 2);
143 assert_se(read(fd, buf, 2) == 2);
146 assert_se(memcmp(buf, "ll", 2) == 0);
149 iov[0].iov_base = (char *)"ABC";
151 iov[1].iov_base = (char *)"DEF";
153 iov[2].iov_base = (char *)"GHI";
155 assert_se(pwritev(fd, iov, 3, 0) == 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(preadv(fd, iov, 3, 0) == 9);
167 assert_se(memcmp(bufv[0], "ABC", 3) == 0);
168 assert_se(memcmp(bufv[1], "DEF", 3) == 0);
169 assert_se(memcmp(bufv[2], "GHI", 3) == 0);