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);
43 r = sd_memfd_new(&m, NULL);
45 return EXIT_TEST_SKIP;
49 assert_se(sd_memfd_get_name(m, &name) >= 0);
50 log_info("name: %s", name);
53 r = sd_memfd_map(m, 0, 12, (void**) &s);
56 strcpy(s, "----- world");
58 r = sd_memfd_set_sealed(m, 1);
59 assert_se(r == -ETXTBSY);
61 assert_se(write(sd_memfd_get_fd(m), "he", 2) == 2);
62 assert_se(write(sd_memfd_get_fd(m), "XXX", 3) == 3);
63 assert_se(streq(s, "heXXX world"));
66 assert_se(lseek(sd_memfd_get_fd(m), 2, SEEK_SET) == 2);
67 assert_se(write(sd_memfd_get_fd(m), "ll", 2) == 2);
69 assert_se(sd_memfd_get_file(m, &f) >= 0);
74 assert_se(streq(s, "hello world"));
76 assert_se(munmap(s, 12) == 0);
78 r = sd_memfd_get_sealed(m);
81 r = sd_memfd_get_size(m, &sz);
83 assert_se(sz = page_size());
86 r = sd_memfd_set_size(m, 6);
89 /* get back new value */
90 r = sd_memfd_get_size(m, &sz);
94 r = sd_memfd_set_sealed(m, 1);
97 r = sd_memfd_get_sealed(m);
100 fd = sd_memfd_dup_fd(m);
105 /* new sd_memfd, same underlying memfd */
106 r = sd_memfd_new_from_fd(&m, fd);
109 /* we did truncate it to 6 */
110 r = sd_memfd_get_size(m, &sz);
111 assert_se(r >= 0 && sz == 6);
113 /* map it, check content */
114 r = sd_memfd_map(m, 0, 12, (void **)&s);
117 /* we only see the truncated size */
118 assert_se(streq(s, "hello "));
120 /* it was already sealed */
121 r = sd_memfd_set_sealed(m, 1);
122 assert_se(r == -EALREADY);
124 /* we cannot break the seal, it is mapped */
125 r = sd_memfd_set_sealed(m, 0);
126 assert_se(r == -ETXTBSY);
128 /* unmap it; become the single owner */
129 assert_se(munmap(s, 12) == 0);
131 /* now we can do flip the sealing */
132 r = sd_memfd_set_sealed(m, 0);
134 r = sd_memfd_get_sealed(m);
137 r = sd_memfd_set_sealed(m, 1);
139 r = sd_memfd_get_sealed(m);
142 r = sd_memfd_set_sealed(m, 0);
144 r = sd_memfd_get_sealed(m);
147 /* seek at 2, read() 2 bytes */
148 assert_se(lseek(fd, 2, SEEK_SET) == 2);
149 assert_se(read(fd, buf, 2) == 2);
152 assert_se(memcmp(buf, "ll", 2) == 0);
155 iov[0].iov_base = (char *)"ABC";
157 iov[1].iov_base = (char *)"DEF";
159 iov[2].iov_base = (char *)"GHI";
161 assert_se(pwritev(fd, iov, 3, 0) == 9);
164 iov[0].iov_base = bufv[0];
166 iov[1].iov_base = bufv[1];
168 iov[2].iov_base = bufv[2];
170 assert_se(preadv(fd, iov, 3, 0) == 9);
173 assert_se(memcmp(bufv[0], "ABC", 3) == 0);
174 assert_se(memcmp(bufv[1], "DEF", 3) == 0);
175 assert_se(memcmp(bufv[2], "GHI", 3) == 0);