chiark / gitweb /
bus: extend memfd test
[elogind.git] / src / libsystemd-bus / test-bus-memfd.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/mman.h>
23
24 #include "log.h"
25 #include "macro.h"
26 #include "util.h"
27
28 #include "sd-memfd.h"
29
30 int main(int argc, char *argv[]) {
31         sd_memfd *m;
32         char *s;
33         uint64_t sz;
34         int r, fd;
35         FILE *f;
36
37         log_set_max_level(LOG_DEBUG);
38
39         r = sd_memfd_new(&m);
40         if (r == -ENOENT)
41                 return EXIT_TEST_SKIP;
42
43         r = sd_memfd_map(m, 0, 6, (void**) &s);
44         assert_se(r >= 0);
45
46         strcpy(s, "hallo");
47
48         r = sd_memfd_set_sealed(m, 1);
49         assert_se(r == -EPERM);
50
51         assert_se(write(sd_memfd_get_fd(m), "he", 2) == 2);
52         assert_se(write(sd_memfd_get_fd(m), "HE", 2) == 2);
53
54         log_error("lseek = %llu", (unsigned long long) lseek(sd_memfd_get_fd(m), 0, SEEK_CUR));
55
56         log_info("<%s>", s);
57
58         access("HUHU", F_OK);
59
60         assert_se(sd_memfd_get_file(m, &f) >= 0);
61         fputc('L', f);
62         fflush(f);
63
64         access("HAHA", F_OK);
65
66         log_info("<%s>", s);
67
68         assert_se(munmap(s, 6) == 0);
69
70         r = sd_memfd_get_sealed(m);
71         assert_se(r == 0);
72
73         r = sd_memfd_get_size(m, &sz);
74         assert_se(r >= 0);
75         assert_se(sz = page_size());
76
77         r = sd_memfd_set_size(m, 6);
78         assert_se(r >= 0);
79
80         r = sd_memfd_set_sealed(m, 1);
81         assert_se(r >= 0);
82
83         r = sd_memfd_get_sealed(m);
84         assert_se(r == 1);
85
86         fd = sd_memfd_dup_fd(m);
87         assert_se(fd >= 0);
88
89         sd_memfd_free(m);
90
91         r = sd_memfd_make(fd, &m);
92         assert_se(r >= 0);
93
94         r = sd_memfd_get_size(m, &sz);
95         assert_se(r >= 0);
96         assert_se(sz = 6);
97
98         r = sd_memfd_map(m, 0, 6, (void**) &s);
99         assert_se(r >= 0);
100
101         r = sd_memfd_set_sealed(m, 1);
102         assert_se(r == -EALREADY);
103
104         r = sd_memfd_set_sealed(m, 0);
105         assert_se(r == -EPERM);
106
107         log_info("<%s>", s);
108
109         assert_se(streq(s, "heLlo"));
110         assert_se(munmap(s, 6) == 0);
111
112         r = sd_memfd_set_sealed(m, 0);
113         assert_se(r >= 0);
114
115         sd_memfd_free(m);
116
117         return 0;
118 }