]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
29 static void test_close_many(void) {
31 char name0
[] = "/tmp/test-close-many.XXXXXX";
32 char name1
[] = "/tmp/test-close-many.XXXXXX";
33 char name2
[] = "/tmp/test-close-many.XXXXXX";
35 fds
[0] = mkostemp_safe(name0
);
36 fds
[1] = mkostemp_safe(name1
);
37 fds
[2] = mkostemp_safe(name2
);
41 assert_se(fcntl(fds
[0], F_GETFD
) == -1);
42 assert_se(fcntl(fds
[1], F_GETFD
) == -1);
43 assert_se(fcntl(fds
[2], F_GETFD
) >= 0);
52 static void test_close_nointr(void) {
53 char name
[] = "/tmp/test-test-close_nointr.XXXXXX";
56 fd
= mkostemp_safe(name
);
58 assert_se(close_nointr(fd
) >= 0);
59 assert_se(close_nointr(fd
) < 0);
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;
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);
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);
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);
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);
94 assert_se(same_fd(a
, b
) == 0);
95 assert_se(same_fd(b
, a
) == 0);
98 static void test_open_serialization_fd(void) {
99 _cleanup_close_
int fd
= -1;
101 fd
= open_serialization_fd("test");
104 write(fd
, "test\n", 5);
107 int main(int argc
, char *argv
[]) {
111 test_open_serialization_fd();