]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0999c8ad RC |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
0999c8ad RC |
6 | ***/ |
7 | ||
8 | #include <fcntl.h> | |
9 | #include <unistd.h> | |
10 | ||
11 | #include "alloc-util.h" | |
12 | #include "fd-util.h" | |
13 | #include "fileio.h" | |
14 | #include "macro.h" | |
aa11e28b LP |
15 | #include "path-util.h" |
16 | #include "process-util.h" | |
a548e14d LP |
17 | #include "random-util.h" |
18 | #include "string-util.h" | |
19 | #include "util.h" | |
0999c8ad RC |
20 | |
21 | static void test_close_many(void) { | |
22 | int fds[3]; | |
23 | char name0[] = "/tmp/test-close-many.XXXXXX"; | |
24 | char name1[] = "/tmp/test-close-many.XXXXXX"; | |
25 | char name2[] = "/tmp/test-close-many.XXXXXX"; | |
26 | ||
646853bd TM |
27 | fds[0] = mkostemp_safe(name0); |
28 | fds[1] = mkostemp_safe(name1); | |
29 | fds[2] = mkostemp_safe(name2); | |
0999c8ad RC |
30 | |
31 | close_many(fds, 2); | |
32 | ||
33 | assert_se(fcntl(fds[0], F_GETFD) == -1); | |
34 | assert_se(fcntl(fds[1], F_GETFD) == -1); | |
35 | assert_se(fcntl(fds[2], F_GETFD) >= 0); | |
36 | ||
37 | safe_close(fds[2]); | |
38 | ||
39 | unlink(name0); | |
40 | unlink(name1); | |
41 | unlink(name2); | |
42 | } | |
43 | ||
44 | static void test_close_nointr(void) { | |
45 | char name[] = "/tmp/test-test-close_nointr.XXXXXX"; | |
46 | int fd; | |
47 | ||
646853bd | 48 | fd = mkostemp_safe(name); |
0999c8ad RC |
49 | assert_se(fd >= 0); |
50 | assert_se(close_nointr(fd) >= 0); | |
51 | assert_se(close_nointr(fd) < 0); | |
52 | ||
53 | unlink(name); | |
54 | } | |
55 | ||
56 | static void test_same_fd(void) { | |
57 | _cleanup_close_pair_ int p[2] = { -1, -1 }; | |
58 | _cleanup_close_ int a = -1, b = -1, c = -1; | |
59 | ||
60 | assert_se(pipe2(p, O_CLOEXEC) >= 0); | |
43dc7aa2 | 61 | assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0); |
0999c8ad | 62 | assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0); |
43dc7aa2 | 63 | assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0); |
0999c8ad RC |
64 | |
65 | assert_se(same_fd(p[0], p[0]) > 0); | |
66 | assert_se(same_fd(p[1], p[1]) > 0); | |
67 | assert_se(same_fd(a, a) > 0); | |
68 | assert_se(same_fd(b, b) > 0); | |
69 | ||
70 | assert_se(same_fd(a, p[0]) > 0); | |
71 | assert_se(same_fd(p[0], a) > 0); | |
72 | assert_se(same_fd(c, p[0]) > 0); | |
73 | assert_se(same_fd(p[0], c) > 0); | |
74 | assert_se(same_fd(a, c) > 0); | |
75 | assert_se(same_fd(c, a) > 0); | |
76 | ||
77 | assert_se(same_fd(p[0], p[1]) == 0); | |
78 | assert_se(same_fd(p[1], p[0]) == 0); | |
79 | assert_se(same_fd(p[0], b) == 0); | |
80 | assert_se(same_fd(b, p[0]) == 0); | |
81 | assert_se(same_fd(p[1], a) == 0); | |
82 | assert_se(same_fd(a, p[1]) == 0); | |
83 | assert_se(same_fd(p[1], b) == 0); | |
84 | assert_se(same_fd(b, p[1]) == 0); | |
85 | ||
86 | assert_se(same_fd(a, b) == 0); | |
87 | assert_se(same_fd(b, a) == 0); | |
88 | } | |
89 | ||
504afd7c ZJS |
90 | static void test_open_serialization_fd(void) { |
91 | _cleanup_close_ int fd = -1; | |
92 | ||
93 | fd = open_serialization_fd("test"); | |
94 | assert_se(fd >= 0); | |
95 | ||
ca9b2b4d | 96 | assert_se(write(fd, "test\n", 5) == 5); |
504afd7c ZJS |
97 | } |
98 | ||
a548e14d LP |
99 | static void test_acquire_data_fd_one(unsigned flags) { |
100 | char wbuffer[196*1024 - 7]; | |
101 | char rbuffer[sizeof(wbuffer)]; | |
102 | int fd; | |
103 | ||
104 | fd = acquire_data_fd("foo", 3, flags); | |
105 | assert_se(fd >= 0); | |
106 | ||
107 | zero(rbuffer); | |
108 | assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3); | |
109 | assert_se(streq(rbuffer, "foo")); | |
110 | ||
111 | fd = safe_close(fd); | |
112 | ||
113 | fd = acquire_data_fd("", 0, flags); | |
114 | assert_se(fd >= 0); | |
115 | ||
116 | zero(rbuffer); | |
117 | assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0); | |
118 | assert_se(streq(rbuffer, "")); | |
119 | ||
120 | fd = safe_close(fd); | |
121 | ||
122 | random_bytes(wbuffer, sizeof(wbuffer)); | |
123 | ||
124 | fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags); | |
125 | assert_se(fd >= 0); | |
126 | ||
127 | zero(rbuffer); | |
128 | assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer)); | |
129 | assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0); | |
130 | ||
131 | fd = safe_close(fd); | |
132 | } | |
133 | ||
134 | static void test_acquire_data_fd(void) { | |
135 | ||
136 | test_acquire_data_fd_one(0); | |
137 | test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL); | |
138 | test_acquire_data_fd_one(ACQUIRE_NO_MEMFD); | |
139 | test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD); | |
140 | test_acquire_data_fd_one(ACQUIRE_NO_PIPE); | |
141 | test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE); | |
142 | test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE); | |
143 | test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE); | |
144 | test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE); | |
145 | } | |
146 | ||
7fe2903c LP |
147 | static void test_fd_move_above_stdio(void) { |
148 | int original_stdin, new_fd; | |
149 | ||
150 | original_stdin = fcntl(0, F_DUPFD, 3); | |
151 | assert_se(original_stdin >= 3); | |
152 | assert_se(close_nointr(0) != EBADF); | |
153 | ||
154 | new_fd = open("/dev/null", O_RDONLY); | |
155 | assert_se(new_fd == 0); | |
156 | ||
157 | new_fd = fd_move_above_stdio(new_fd); | |
158 | assert_se(new_fd >= 3); | |
159 | ||
160 | assert_se(dup(original_stdin) == 0); | |
161 | assert_se(close_nointr(original_stdin) != EBADF); | |
162 | assert_se(close_nointr(new_fd) != EBADF); | |
163 | } | |
164 | ||
aa11e28b LP |
165 | static void test_rearrange_stdio(void) { |
166 | pid_t pid; | |
167 | int r; | |
168 | ||
169 | r = safe_fork("rearrange", FORK_WAIT|FORK_LOG, &pid); | |
170 | assert_se(r >= 0); | |
171 | ||
172 | if (r == 0) { | |
173 | _cleanup_free_ char *path = NULL; | |
174 | char buffer[10]; | |
175 | ||
176 | /* Child */ | |
177 | ||
178 | safe_close(STDERR_FILENO); /* Let's close an fd < 2, to make it more interesting */ | |
179 | ||
180 | assert_se(rearrange_stdio(-1, -1, -1) >= 0); | |
181 | ||
182 | assert_se(fd_get_path(STDIN_FILENO, &path) >= 0); | |
183 | assert_se(path_equal(path, "/dev/null")); | |
184 | path = mfree(path); | |
185 | ||
186 | assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0); | |
187 | assert_se(path_equal(path, "/dev/null")); | |
188 | path = mfree(path); | |
189 | ||
190 | assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0); | |
191 | assert_se(path_equal(path, "/dev/null")); | |
192 | path = mfree(path); | |
193 | ||
194 | safe_close(STDIN_FILENO); | |
195 | safe_close(STDOUT_FILENO); | |
196 | safe_close(STDERR_FILENO); | |
197 | ||
198 | { | |
199 | int pair[2]; | |
200 | assert_se(pipe(pair) >= 0); | |
201 | assert_se(pair[0] == 0); | |
202 | assert_se(pair[1] == 1); | |
203 | assert_se(fd_move_above_stdio(0) == 3); | |
204 | } | |
205 | assert_se(open("/dev/full", O_WRONLY|O_CLOEXEC) == 0); | |
206 | assert_se(acquire_data_fd("foobar", 6, 0) == 2); | |
207 | ||
208 | assert_se(rearrange_stdio(2, 0, 1) >= 0); | |
209 | ||
210 | assert_se(write(1, "x", 1) < 0 && errno == ENOSPC); | |
211 | assert_se(write(2, "z", 1) == 1); | |
212 | assert_se(read(3, buffer, sizeof(buffer)) == 1); | |
213 | assert_se(buffer[0] == 'z'); | |
214 | assert_se(read(0, buffer, sizeof(buffer)) == 6); | |
215 | assert_se(memcmp(buffer, "foobar", 6) == 0); | |
216 | ||
217 | assert_se(rearrange_stdio(-1, 1, 2) >= 0); | |
218 | assert_se(write(1, "a", 1) < 0 && errno == ENOSPC); | |
219 | assert_se(write(2, "y", 1) == 1); | |
220 | assert_se(read(3, buffer, sizeof(buffer)) == 1); | |
221 | assert_se(buffer[0] == 'y'); | |
222 | ||
223 | assert_se(fd_get_path(0, &path) >= 0); | |
224 | assert_se(path_equal(path, "/dev/null")); | |
225 | path = mfree(path); | |
226 | ||
227 | _exit(EXIT_SUCCESS); | |
228 | } | |
229 | } | |
230 | ||
0999c8ad RC |
231 | int main(int argc, char *argv[]) { |
232 | test_close_many(); | |
233 | test_close_nointr(); | |
234 | test_same_fd(); | |
504afd7c | 235 | test_open_serialization_fd(); |
a548e14d | 236 | test_acquire_data_fd(); |
7fe2903c | 237 | test_fd_move_above_stdio(); |
aa11e28b | 238 | test_rearrange_stdio(); |
0999c8ad RC |
239 | |
240 | return 0; | |
241 | } |