]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fd-util.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.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 static void test_same_fd(void) {
64 _cleanup_close_pair_ int p[2] = { -1, -1 };
65 _cleanup_close_ int a = -1, b = -1, c = -1;
66
67 assert_se(pipe2(p, O_CLOEXEC) >= 0);
68 assert_se((a = dup(p[0])) >= 0);
69 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
70 assert_se((c = dup(a)) >= 0);
71
72 assert_se(same_fd(p[0], p[0]) > 0);
73 assert_se(same_fd(p[1], p[1]) > 0);
74 assert_se(same_fd(a, a) > 0);
75 assert_se(same_fd(b, b) > 0);
76
77 assert_se(same_fd(a, p[0]) > 0);
78 assert_se(same_fd(p[0], a) > 0);
79 assert_se(same_fd(c, p[0]) > 0);
80 assert_se(same_fd(p[0], c) > 0);
81 assert_se(same_fd(a, c) > 0);
82 assert_se(same_fd(c, a) > 0);
83
84 assert_se(same_fd(p[0], p[1]) == 0);
85 assert_se(same_fd(p[1], p[0]) == 0);
86 assert_se(same_fd(p[0], b) == 0);
87 assert_se(same_fd(b, p[0]) == 0);
88 assert_se(same_fd(p[1], a) == 0);
89 assert_se(same_fd(a, p[1]) == 0);
90 assert_se(same_fd(p[1], b) == 0);
91 assert_se(same_fd(b, p[1]) == 0);
92
93 assert_se(same_fd(a, b) == 0);
94 assert_se(same_fd(b, a) == 0);
95 }
96
97 static void test_open_serialization_fd(void) {
98 _cleanup_close_ int fd = -1;
99
100 fd = open_serialization_fd("test");
101 assert_se(fd >= 0);
102
103 write(fd, "test\n", 5);
104 }
105
106 int main(int argc, char *argv[]) {
107 test_close_many();
108 test_close_nointr();
109 test_same_fd();
110 test_open_serialization_fd();
111
112 return 0;
113 }