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"
28 #include "random-util.h"
29 #include "string-util.h"
32 static void test_close_many(void) {
34 char name0[] = "/tmp/test-close-many.XXXXXX";
35 char name1[] = "/tmp/test-close-many.XXXXXX";
36 char name2[] = "/tmp/test-close-many.XXXXXX";
38 fds[0] = mkostemp_safe(name0);
39 fds[1] = mkostemp_safe(name1);
40 fds[2] = mkostemp_safe(name2);
44 assert_se(fcntl(fds[0], F_GETFD) == -1);
45 assert_se(fcntl(fds[1], F_GETFD) == -1);
46 assert_se(fcntl(fds[2], F_GETFD) >= 0);
55 static void test_close_nointr(void) {
56 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
59 fd = mkostemp_safe(name);
61 assert_se(close_nointr(fd) >= 0);
62 assert_se(close_nointr(fd) < 0);
67 #if 0 /// UNNEEDED by elogind
68 static void test_same_fd(void) {
69 _cleanup_close_pair_ int p[2] = { -1, -1 };
70 _cleanup_close_ int a = -1, b = -1, c = -1;
72 assert_se(pipe2(p, O_CLOEXEC) >= 0);
73 assert_se((a = dup(p[0])) >= 0);
74 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
75 assert_se((c = dup(a)) >= 0);
77 assert_se(same_fd(p[0], p[0]) > 0);
78 assert_se(same_fd(p[1], p[1]) > 0);
79 assert_se(same_fd(a, a) > 0);
80 assert_se(same_fd(b, b) > 0);
82 assert_se(same_fd(a, p[0]) > 0);
83 assert_se(same_fd(p[0], a) > 0);
84 assert_se(same_fd(c, p[0]) > 0);
85 assert_se(same_fd(p[0], c) > 0);
86 assert_se(same_fd(a, c) > 0);
87 assert_se(same_fd(c, a) > 0);
89 assert_se(same_fd(p[0], p[1]) == 0);
90 assert_se(same_fd(p[1], p[0]) == 0);
91 assert_se(same_fd(p[0], b) == 0);
92 assert_se(same_fd(b, p[0]) == 0);
93 assert_se(same_fd(p[1], a) == 0);
94 assert_se(same_fd(a, p[1]) == 0);
95 assert_se(same_fd(p[1], b) == 0);
96 assert_se(same_fd(b, p[1]) == 0);
98 assert_se(same_fd(a, b) == 0);
99 assert_se(same_fd(b, a) == 0);
103 static void test_open_serialization_fd(void) {
104 _cleanup_close_ int fd = -1;
106 fd = open_serialization_fd("test");
109 assert_se(write(fd, "test\n", 5) == 5);
112 static void test_acquire_data_fd_one(unsigned flags) {
113 char wbuffer[196*1024 - 7];
114 char rbuffer[sizeof(wbuffer)];
117 fd = acquire_data_fd("foo", 3, flags);
121 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3);
122 assert_se(streq(rbuffer, "foo"));
126 fd = acquire_data_fd("", 0, flags);
130 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0);
131 assert_se(streq(rbuffer, ""));
135 random_bytes(wbuffer, sizeof(wbuffer));
137 fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags);
141 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer));
142 assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0);
147 static void test_acquire_data_fd(void) {
149 test_acquire_data_fd_one(0);
150 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL);
151 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD);
152 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD);
153 test_acquire_data_fd_one(ACQUIRE_NO_PIPE);
154 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE);
155 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
156 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
157 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE);
160 static void test_fd_move_above_stdio(void) {
161 int original_stdin, new_fd;
163 original_stdin = fcntl(0, F_DUPFD, 3);
164 assert_se(original_stdin >= 3);
165 assert_se(close_nointr(0) != EBADF);
167 new_fd = open("/dev/null", O_RDONLY);
168 assert_se(new_fd == 0);
170 new_fd = fd_move_above_stdio(new_fd);
171 assert_se(new_fd >= 3);
173 assert_se(dup(original_stdin) == 0);
174 assert_se(close_nointr(original_stdin) != EBADF);
175 assert_se(close_nointr(new_fd) != EBADF);
178 int main(int argc, char *argv[]) {
181 #if 0 /// UNNEEDED by elogind
184 test_open_serialization_fd();
185 test_acquire_data_fd();
186 test_fd_move_above_stdio();