]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fd-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
23 #include "alloc-util.h"
28 static void test_close_many(void) {
30 char name0
[] = "/tmp/test-close-many.XXXXXX";
31 char name1
[] = "/tmp/test-close-many.XXXXXX";
32 char name2
[] = "/tmp/test-close-many.XXXXXX";
34 fds
[0] = mkostemp_safe(name0
);
35 fds
[1] = mkostemp_safe(name1
);
36 fds
[2] = mkostemp_safe(name2
);
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);
51 static void test_close_nointr(void) {
52 char name
[] = "/tmp/test-test-close_nointr.XXXXXX";
55 fd
= mkostemp_safe(name
);
57 assert_se(close_nointr(fd
) >= 0);
58 assert_se(close_nointr(fd
) < 0);
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;
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);
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);
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);
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);
93 assert_se(same_fd(a
, b
) == 0);
94 assert_se(same_fd(b
, a
) == 0);
97 static void test_open_serialization_fd(void) {
98 _cleanup_close_
int fd
= -1;
100 fd
= open_serialization_fd("test");
103 write(fd
, "test\n", 5);
106 int main(int argc
, char *argv
[]) {
110 test_open_serialization_fd();