chiark / gitweb /
Fix service file to match installed elogind binary location
[elogind.git] / src / test / test-fd-util.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "alloc-util.h"
24 #include "fd-util.h"
25 #include "fileio.h"
26 #include "macro.h"
27
28 static void test_close_many(void) {
29         int fds[3];
30         char name0[] = "/tmp/test-close-many.XXXXXX";
31         char name1[] = "/tmp/test-close-many.XXXXXX";
32         char name2[] = "/tmp/test-close-many.XXXXXX";
33
34         fds[0] = mkostemp_safe(name0);
35         fds[1] = mkostemp_safe(name1);
36         fds[2] = mkostemp_safe(name2);
37
38         close_many(fds, 2);
39
40         assert_se(fcntl(fds[0], F_GETFD) == -1);
41         assert_se(fcntl(fds[1], F_GETFD) == -1);
42         assert_se(fcntl(fds[2], F_GETFD) >= 0);
43
44         safe_close(fds[2]);
45
46         unlink(name0);
47         unlink(name1);
48         unlink(name2);
49 }
50
51 static void test_close_nointr(void) {
52         char name[] = "/tmp/test-test-close_nointr.XXXXXX";
53         int fd;
54
55         fd = mkostemp_safe(name);
56         assert_se(fd >= 0);
57         assert_se(close_nointr(fd) >= 0);
58         assert_se(close_nointr(fd) < 0);
59
60         unlink(name);
61 }
62
63 #if 0 /// UNNEEDED by elogind
64 static void test_same_fd(void) {
65         _cleanup_close_pair_ int p[2] = { -1, -1 };
66         _cleanup_close_ int a = -1, b = -1, c = -1;
67
68         assert_se(pipe2(p, O_CLOEXEC) >= 0);
69         assert_se((a = dup(p[0])) >= 0);
70         assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
71         assert_se((c = dup(a)) >= 0);
72
73         assert_se(same_fd(p[0], p[0]) > 0);
74         assert_se(same_fd(p[1], p[1]) > 0);
75         assert_se(same_fd(a, a) > 0);
76         assert_se(same_fd(b, b) > 0);
77
78         assert_se(same_fd(a, p[0]) > 0);
79         assert_se(same_fd(p[0], a) > 0);
80         assert_se(same_fd(c, p[0]) > 0);
81         assert_se(same_fd(p[0], c) > 0);
82         assert_se(same_fd(a, c) > 0);
83         assert_se(same_fd(c, a) > 0);
84
85         assert_se(same_fd(p[0], p[1]) == 0);
86         assert_se(same_fd(p[1], p[0]) == 0);
87         assert_se(same_fd(p[0], b) == 0);
88         assert_se(same_fd(b, p[0]) == 0);
89         assert_se(same_fd(p[1], a) == 0);
90         assert_se(same_fd(a, p[1]) == 0);
91         assert_se(same_fd(p[1], b) == 0);
92         assert_se(same_fd(b, p[1]) == 0);
93
94         assert_se(same_fd(a, b) == 0);
95         assert_se(same_fd(b, a) == 0);
96 }
97 #endif // 0
98
99 static void test_open_serialization_fd(void) {
100         _cleanup_close_ int fd = -1;
101
102         fd = open_serialization_fd("test");
103         assert_se(fd >= 0);
104
105         write(fd, "test\n", 5);
106 }
107
108 int main(int argc, char *argv[]) {
109         test_close_many();
110         test_close_nointr();
111 #if 0 /// UNNEEDED by elogind
112         test_same_fd();
113 #endif // 0
114         test_open_serialization_fd();
115
116         return 0;
117 }