Bug Summary

File:build-scan/../src/test/test-fd-util.c
Warning:line 280, column 9
Value stored to 'fd2' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test-fd-util.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I test-fd-util.p -I . -I .. -I src/basic -I ../src/basic -I src/shared -I ../src/shared -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/test/test-fd-util.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <fcntl.h>
4#include <unistd.h>
5
6#include "alloc-util.h"
7#include "fd-util.h"
8#include "fileio.h"
9#include "macro.h"
10#include "path-util.h"
11#include "process-util.h"
12#include "random-util.h"
13#include "string-util.h"
14#include "util.h"
15
16static void test_close_many(void) {
17 int fds[3];
18 char name0[] = "/tmp/test-close-many.XXXXXX";
19 char name1[] = "/tmp/test-close-many.XXXXXX";
20 char name2[] = "/tmp/test-close-many.XXXXXX";
21
22 fds[0] = mkostemp_safe(name0);
23 fds[1] = mkostemp_safe(name1);
24 fds[2] = mkostemp_safe(name2);
25
26 close_many(fds, 2);
27
28 assert_se(fcntl(fds[0], F_GETFD) == -1)do { if ((__builtin_expect(!!(!(fcntl(fds[0], 1) == -1)),0)))
log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fcntl(fds[0], F_GETFD) == -1"
), "../src/test/test-fd-util.c", 28, __PRETTY_FUNCTION__); } while
(0)
;
29 assert_se(fcntl(fds[1], F_GETFD) == -1)do { if ((__builtin_expect(!!(!(fcntl(fds[1], 1) == -1)),0)))
log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fcntl(fds[1], F_GETFD) == -1"
), "../src/test/test-fd-util.c", 29, __PRETTY_FUNCTION__); } while
(0)
;
30 assert_se(fcntl(fds[2], F_GETFD) >= 0)do { if ((__builtin_expect(!!(!(fcntl(fds[2], 1) >= 0)),0)
)) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fcntl(fds[2], F_GETFD) >= 0"
), "../src/test/test-fd-util.c", 30, __PRETTY_FUNCTION__); } while
(0)
;
31
32 safe_close(fds[2]);
33
34 unlink(name0);
35 unlink(name1);
36 unlink(name2);
37}
38
39static void test_close_nointr(void) {
40 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
41 int fd;
42
43 fd = mkostemp_safe(name);
44 assert_se(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/test/test-fd-util.c"
, 44, __PRETTY_FUNCTION__); } while (0)
;
45 assert_se(close_nointr(fd) >= 0)do { if ((__builtin_expect(!!(!(close_nointr(fd) >= 0)),0)
)) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("close_nointr(fd) >= 0"
), "../src/test/test-fd-util.c", 45, __PRETTY_FUNCTION__); } while
(0)
;
46 assert_se(close_nointr(fd) < 0)do { if ((__builtin_expect(!!(!(close_nointr(fd) < 0)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("close_nointr(fd) < 0"
), "../src/test/test-fd-util.c", 46, __PRETTY_FUNCTION__); } while
(0)
;
47
48 unlink(name);
49}
50
51static void test_same_fd(void) {
52 _cleanup_close_pair___attribute__((cleanup(close_pairp))) int p[2] = { -1, -1 };
53 _cleanup_close___attribute__((cleanup(closep))) int a = -1, b = -1, c = -1;
54
55 assert_se(pipe2(p, O_CLOEXEC) >= 0)do { if ((__builtin_expect(!!(!(pipe2(p, 02000000) >= 0)),
0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("pipe2(p, O_CLOEXEC) >= 0"
), "../src/test/test-fd-util.c", 55, __PRETTY_FUNCTION__); } while
(0)
;
56 assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0)do { if ((__builtin_expect(!!(!((a = fcntl(p[0], 0, 3)) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("(a = fcntl(p[0], F_DUPFD, 3)) >= 0"
), "../src/test/test-fd-util.c", 56, __PRETTY_FUNCTION__); } while
(0)
;
57 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0)do { if ((__builtin_expect(!!(!((b = open("/dev/null", 00|02000000
)) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, (
"(b = open(\"/dev/null\", O_RDONLY|O_CLOEXEC)) >= 0"), "../src/test/test-fd-util.c"
, 57, __PRETTY_FUNCTION__); } while (0)
;
58 assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0)do { if ((__builtin_expect(!!(!((c = fcntl(a, 0, 3)) >= 0)
),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("(c = fcntl(a, F_DUPFD, 3)) >= 0"
), "../src/test/test-fd-util.c", 58, __PRETTY_FUNCTION__); } while
(0)
;
59
60 assert_se(same_fd(p[0], p[0]) > 0)do { if ((__builtin_expect(!!(!(same_fd(p[0], p[0]) > 0)),
0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(p[0], p[0]) > 0"
), "../src/test/test-fd-util.c", 60, __PRETTY_FUNCTION__); } while
(0)
;
61 assert_se(same_fd(p[1], p[1]) > 0)do { if ((__builtin_expect(!!(!(same_fd(p[1], p[1]) > 0)),
0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(p[1], p[1]) > 0"
), "../src/test/test-fd-util.c", 61, __PRETTY_FUNCTION__); } while
(0)
;
62 assert_se(same_fd(a, a) > 0)do { if ((__builtin_expect(!!(!(same_fd(a, a) > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(a, a) > 0"), "../src/test/test-fd-util.c"
, 62, __PRETTY_FUNCTION__); } while (0)
;
63 assert_se(same_fd(b, b) > 0)do { if ((__builtin_expect(!!(!(same_fd(b, b) > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(b, b) > 0"), "../src/test/test-fd-util.c"
, 63, __PRETTY_FUNCTION__); } while (0)
;
64
65 assert_se(same_fd(a, p[0]) > 0)do { if ((__builtin_expect(!!(!(same_fd(a, p[0]) > 0)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(a, p[0]) > 0"
), "../src/test/test-fd-util.c", 65, __PRETTY_FUNCTION__); } while
(0)
;
66 assert_se(same_fd(p[0], a) > 0)do { if ((__builtin_expect(!!(!(same_fd(p[0], a) > 0)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(p[0], a) > 0"
), "../src/test/test-fd-util.c", 66, __PRETTY_FUNCTION__); } while
(0)
;
67 assert_se(same_fd(c, p[0]) > 0)do { if ((__builtin_expect(!!(!(same_fd(c, p[0]) > 0)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(c, p[0]) > 0"
), "../src/test/test-fd-util.c", 67, __PRETTY_FUNCTION__); } while
(0)
;
68 assert_se(same_fd(p[0], c) > 0)do { if ((__builtin_expect(!!(!(same_fd(p[0], c) > 0)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(p[0], c) > 0"
), "../src/test/test-fd-util.c", 68, __PRETTY_FUNCTION__); } while
(0)
;
69 assert_se(same_fd(a, c) > 0)do { if ((__builtin_expect(!!(!(same_fd(a, c) > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(a, c) > 0"), "../src/test/test-fd-util.c"
, 69, __PRETTY_FUNCTION__); } while (0)
;
70 assert_se(same_fd(c, a) > 0)do { if ((__builtin_expect(!!(!(same_fd(c, a) > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(c, a) > 0"), "../src/test/test-fd-util.c"
, 70, __PRETTY_FUNCTION__); } while (0)
;
71
72 assert_se(same_fd(p[0], p[1]) == 0)do { if ((__builtin_expect(!!(!(same_fd(p[0], p[1]) == 0)),0)
)) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(p[0], p[1]) == 0"
), "../src/test/test-fd-util.c", 72, __PRETTY_FUNCTION__); } while
(0)
;
73 assert_se(same_fd(p[1], p[0]) == 0)do { if ((__builtin_expect(!!(!(same_fd(p[1], p[0]) == 0)),0)
)) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("same_fd(p[1], p[0]) == 0"
), "../src/test/test-fd-util.c", 73, __PRETTY_FUNCTION__); } while
(0)
;
74 assert_se(same_fd(p[0], b) == 0)do { if ((__builtin_expect(!!(!(same_fd(p[0], b) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(p[0], b) == 0"), "../src/test/test-fd-util.c"
, 74, __PRETTY_FUNCTION__); } while (0)
;
75 assert_se(same_fd(b, p[0]) == 0)do { if ((__builtin_expect(!!(!(same_fd(b, p[0]) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(b, p[0]) == 0"), "../src/test/test-fd-util.c"
, 75, __PRETTY_FUNCTION__); } while (0)
;
76 assert_se(same_fd(p[1], a) == 0)do { if ((__builtin_expect(!!(!(same_fd(p[1], a) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(p[1], a) == 0"), "../src/test/test-fd-util.c"
, 76, __PRETTY_FUNCTION__); } while (0)
;
77 assert_se(same_fd(a, p[1]) == 0)do { if ((__builtin_expect(!!(!(same_fd(a, p[1]) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(a, p[1]) == 0"), "../src/test/test-fd-util.c"
, 77, __PRETTY_FUNCTION__); } while (0)
;
78 assert_se(same_fd(p[1], b) == 0)do { if ((__builtin_expect(!!(!(same_fd(p[1], b) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(p[1], b) == 0"), "../src/test/test-fd-util.c"
, 78, __PRETTY_FUNCTION__); } while (0)
;
79 assert_se(same_fd(b, p[1]) == 0)do { if ((__builtin_expect(!!(!(same_fd(b, p[1]) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(b, p[1]) == 0"), "../src/test/test-fd-util.c"
, 79, __PRETTY_FUNCTION__); } while (0)
;
80
81 assert_se(same_fd(a, b) == 0)do { if ((__builtin_expect(!!(!(same_fd(a, b) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(a, b) == 0"), "../src/test/test-fd-util.c"
, 81, __PRETTY_FUNCTION__); } while (0)
;
82 assert_se(same_fd(b, a) == 0)do { if ((__builtin_expect(!!(!(same_fd(b, a) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("same_fd(b, a) == 0"), "../src/test/test-fd-util.c"
, 82, __PRETTY_FUNCTION__); } while (0)
;
83}
84
85static void test_open_serialization_fd(void) {
86 _cleanup_close___attribute__((cleanup(closep))) int fd = -1;
87
88 fd = open_serialization_fd("test");
89 assert_se(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/test/test-fd-util.c"
, 89, __PRETTY_FUNCTION__); } while (0)
;
90
91 assert_se(write(fd, "test\n", 5) == 5)do { if ((__builtin_expect(!!(!(write(fd, "test\n", 5) == 5))
,0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("write(fd, \"test\\n\", 5) == 5"
), "../src/test/test-fd-util.c", 91, __PRETTY_FUNCTION__); } while
(0)
;
92}
93
94static void test_acquire_data_fd_one(unsigned flags) {
95 char wbuffer[196*1024 - 7];
96 char rbuffer[sizeof(wbuffer)];
97 int fd;
98
99 fd = acquire_data_fd("foo", 3, flags);
100 assert_se(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/test/test-fd-util.c"
, 100, __PRETTY_FUNCTION__); } while (0)
;
101
102 zero(rbuffer)(({ size_t _l_ = (sizeof(rbuffer)); void *_x_ = (&(rbuffer
)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); }))
;
103 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3)do { if ((__builtin_expect(!!(!(read(fd, rbuffer, sizeof(rbuffer
)) == 3)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("read(fd, rbuffer, sizeof(rbuffer)) == 3"
), "../src/test/test-fd-util.c", 103, __PRETTY_FUNCTION__); }
while (0)
;
104 assert_se(streq(rbuffer, "foo"))do { if ((__builtin_expect(!!(!((strcmp((rbuffer),("foo")) ==
0))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(rbuffer, \"foo\")"
), "../src/test/test-fd-util.c", 104, __PRETTY_FUNCTION__); }
while (0)
;
105
106 fd = safe_close(fd);
107
108 fd = acquire_data_fd("", 0, flags);
109 assert_se(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/test/test-fd-util.c"
, 109, __PRETTY_FUNCTION__); } while (0)
;
110
111 zero(rbuffer)(({ size_t _l_ = (sizeof(rbuffer)); void *_x_ = (&(rbuffer
)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); }))
;
112 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0)do { if ((__builtin_expect(!!(!(read(fd, rbuffer, sizeof(rbuffer
)) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("read(fd, rbuffer, sizeof(rbuffer)) == 0"
), "../src/test/test-fd-util.c", 112, __PRETTY_FUNCTION__); }
while (0)
;
113 assert_se(streq(rbuffer, ""))do { if ((__builtin_expect(!!(!((strcmp((rbuffer),("")) == 0)
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(rbuffer, \"\")"
), "../src/test/test-fd-util.c", 113, __PRETTY_FUNCTION__); }
while (0)
;
114
115 fd = safe_close(fd);
116
117 random_bytes(wbuffer, sizeof(wbuffer));
118
119 fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags);
120 assert_se(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/test/test-fd-util.c"
, 120, __PRETTY_FUNCTION__); } while (0)
;
121
122 zero(rbuffer)(({ size_t _l_ = (sizeof(rbuffer)); void *_x_ = (&(rbuffer
)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); }))
;
123 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer))do { if ((__builtin_expect(!!(!(read(fd, rbuffer, sizeof(rbuffer
)) == sizeof(rbuffer))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer)"), "../src/test/test-fd-util.c"
, 123, __PRETTY_FUNCTION__); } while (0)
;
124 assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0)do { if ((__builtin_expect(!!(!(memcmp(rbuffer, wbuffer, sizeof
(rbuffer)) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0"), "../src/test/test-fd-util.c"
, 124, __PRETTY_FUNCTION__); } while (0)
;
125
126 fd = safe_close(fd);
127}
128
129static void test_acquire_data_fd(void) {
130
131 test_acquire_data_fd_one(0);
132 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL);
133 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD);
134 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD);
135 test_acquire_data_fd_one(ACQUIRE_NO_PIPE);
136 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE);
137 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
138 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
139 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE);
140}
141
142static void test_fd_move_above_stdio(void) {
143 int original_stdin, new_fd;
144
145 original_stdin = fcntl(0, F_DUPFD0, 3);
146 assert_se(original_stdin >= 3)do { if ((__builtin_expect(!!(!(original_stdin >= 3)),0)))
log_assert_failed_realm(LOG_REALM_SYSTEMD, ("original_stdin >= 3"
), "../src/test/test-fd-util.c", 146, __PRETTY_FUNCTION__); }
while (0)
;
147 assert_se(close_nointr(0) != EBADF)do { if ((__builtin_expect(!!(!(close_nointr(0) != 9)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("close_nointr(0) != EBADF"), "../src/test/test-fd-util.c"
, 147, __PRETTY_FUNCTION__); } while (0)
;
148
149 new_fd = open("/dev/null", O_RDONLY00);
150 assert_se(new_fd == 0)do { if ((__builtin_expect(!!(!(new_fd == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("new_fd == 0"), "../src/test/test-fd-util.c"
, 150, __PRETTY_FUNCTION__); } while (0)
;
151
152 new_fd = fd_move_above_stdio(new_fd);
153 assert_se(new_fd >= 3)do { if ((__builtin_expect(!!(!(new_fd >= 3)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("new_fd >= 3"), "../src/test/test-fd-util.c"
, 153, __PRETTY_FUNCTION__); } while (0)
;
154
155 assert_se(dup(original_stdin) == 0)do { if ((__builtin_expect(!!(!(dup(original_stdin) == 0)),0)
)) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("dup(original_stdin) == 0"
), "../src/test/test-fd-util.c", 155, __PRETTY_FUNCTION__); }
while (0)
;
156 assert_se(close_nointr(original_stdin) != EBADF)do { if ((__builtin_expect(!!(!(close_nointr(original_stdin) !=
9)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("close_nointr(original_stdin) != EBADF"
), "../src/test/test-fd-util.c", 156, __PRETTY_FUNCTION__); }
while (0)
;
157 assert_se(close_nointr(new_fd) != EBADF)do { if ((__builtin_expect(!!(!(close_nointr(new_fd) != 9)),0
))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("close_nointr(new_fd) != EBADF"
), "../src/test/test-fd-util.c", 157, __PRETTY_FUNCTION__); }
while (0)
;
158}
159
160static void test_rearrange_stdio(void) {
161 pid_t pid;
162 int r;
163
164 r = safe_fork("rearrange", FORK_WAIT|FORK_LOG, &pid);
165 assert_se(r >= 0)do { if ((__builtin_expect(!!(!(r >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("r >= 0"), "../src/test/test-fd-util.c"
, 165, __PRETTY_FUNCTION__); } while (0)
;
166
167 if (r == 0) {
168 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0);
169 char buffer[10];
170
171 /* Child */
172
173 safe_close(STDERR_FILENO2); /* Let's close an fd < 2, to make it more interesting */
174
175 assert_se(rearrange_stdio(-1, -1, -1) >= 0)do { if ((__builtin_expect(!!(!(rearrange_stdio(-1, -1, -1) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("rearrange_stdio(-1, -1, -1) >= 0"
), "../src/test/test-fd-util.c", 175, __PRETTY_FUNCTION__); }
while (0)
;
176
177 assert_se(fd_get_path(STDIN_FILENO, &path) >= 0)do { if ((__builtin_expect(!!(!(fd_get_path(0, &path) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fd_get_path(STDIN_FILENO, &path) >= 0"
), "../src/test/test-fd-util.c", 177, __PRETTY_FUNCTION__); }
while (0)
;
178 assert_se(path_equal(path, "/dev/null"))do { if ((__builtin_expect(!!(!(path_equal(path, "/dev/null")
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("path_equal(path, \"/dev/null\")"
), "../src/test/test-fd-util.c", 178, __PRETTY_FUNCTION__); }
while (0)
;
179 path = mfree(path);
180
181 assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0)do { if ((__builtin_expect(!!(!(fd_get_path(1, &path) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fd_get_path(STDOUT_FILENO, &path) >= 0"
), "../src/test/test-fd-util.c", 181, __PRETTY_FUNCTION__); }
while (0)
;
182 assert_se(path_equal(path, "/dev/null"))do { if ((__builtin_expect(!!(!(path_equal(path, "/dev/null")
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("path_equal(path, \"/dev/null\")"
), "../src/test/test-fd-util.c", 182, __PRETTY_FUNCTION__); }
while (0)
;
183 path = mfree(path);
184
185 assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0)do { if ((__builtin_expect(!!(!(fd_get_path(1, &path) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fd_get_path(STDOUT_FILENO, &path) >= 0"
), "../src/test/test-fd-util.c", 185, __PRETTY_FUNCTION__); }
while (0)
;
186 assert_se(path_equal(path, "/dev/null"))do { if ((__builtin_expect(!!(!(path_equal(path, "/dev/null")
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("path_equal(path, \"/dev/null\")"
), "../src/test/test-fd-util.c", 186, __PRETTY_FUNCTION__); }
while (0)
;
187 path = mfree(path);
188
189 safe_close(STDIN_FILENO0);
190 safe_close(STDOUT_FILENO1);
191 safe_close(STDERR_FILENO2);
192
193 {
194 int pair[2];
195 assert_se(pipe(pair) >= 0)do { if ((__builtin_expect(!!(!(pipe(pair) >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("pipe(pair) >= 0"), "../src/test/test-fd-util.c"
, 195, __PRETTY_FUNCTION__); } while (0)
;
196 assert_se(pair[0] == 0)do { if ((__builtin_expect(!!(!(pair[0] == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("pair[0] == 0"), "../src/test/test-fd-util.c"
, 196, __PRETTY_FUNCTION__); } while (0)
;
197 assert_se(pair[1] == 1)do { if ((__builtin_expect(!!(!(pair[1] == 1)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("pair[1] == 1"), "../src/test/test-fd-util.c"
, 197, __PRETTY_FUNCTION__); } while (0)
;
198 assert_se(fd_move_above_stdio(0) == 3)do { if ((__builtin_expect(!!(!(fd_move_above_stdio(0) == 3))
,0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fd_move_above_stdio(0) == 3"
), "../src/test/test-fd-util.c", 198, __PRETTY_FUNCTION__); }
while (0)
;
199 }
200 assert_se(open("/dev/full", O_WRONLY|O_CLOEXEC) == 0)do { if ((__builtin_expect(!!(!(open("/dev/full", 01|02000000
) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("open(\"/dev/full\", O_WRONLY|O_CLOEXEC) == 0"
), "../src/test/test-fd-util.c", 200, __PRETTY_FUNCTION__); }
while (0)
;
201 assert_se(acquire_data_fd("foobar", 6, 0) == 2)do { if ((__builtin_expect(!!(!(acquire_data_fd("foobar", 6, 0
) == 2)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("acquire_data_fd(\"foobar\", 6, 0) == 2"
), "../src/test/test-fd-util.c", 201, __PRETTY_FUNCTION__); }
while (0)
;
202
203 assert_se(rearrange_stdio(2, 0, 1) >= 0)do { if ((__builtin_expect(!!(!(rearrange_stdio(2, 0, 1) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("rearrange_stdio(2, 0, 1) >= 0"
), "../src/test/test-fd-util.c", 203, __PRETTY_FUNCTION__); }
while (0)
;
204
205 assert_se(write(1, "x", 1) < 0 && errno == ENOSPC)do { if ((__builtin_expect(!!(!(write(1, "x", 1) < 0 &&
(*__errno_location ()) == 28)),0))) log_assert_failed_realm(
LOG_REALM_SYSTEMD, ("write(1, \"x\", 1) < 0 && errno == ENOSPC"
), "../src/test/test-fd-util.c", 205, __PRETTY_FUNCTION__); }
while (0)
;
206 assert_se(write(2, "z", 1) == 1)do { if ((__builtin_expect(!!(!(write(2, "z", 1) == 1)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("write(2, \"z\", 1) == 1"), "../src/test/test-fd-util.c"
, 206, __PRETTY_FUNCTION__); } while (0)
;
207 assert_se(read(3, buffer, sizeof(buffer)) == 1)do { if ((__builtin_expect(!!(!(read(3, buffer, sizeof(buffer
)) == 1)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("read(3, buffer, sizeof(buffer)) == 1"
), "../src/test/test-fd-util.c", 207, __PRETTY_FUNCTION__); }
while (0)
;
208 assert_se(buffer[0] == 'z')do { if ((__builtin_expect(!!(!(buffer[0] == 'z')),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("buffer[0] == 'z'"), "../src/test/test-fd-util.c"
, 208, __PRETTY_FUNCTION__); } while (0)
;
209 assert_se(read(0, buffer, sizeof(buffer)) == 6)do { if ((__builtin_expect(!!(!(read(0, buffer, sizeof(buffer
)) == 6)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("read(0, buffer, sizeof(buffer)) == 6"
), "../src/test/test-fd-util.c", 209, __PRETTY_FUNCTION__); }
while (0)
;
210 assert_se(memcmp(buffer, "foobar", 6) == 0)do { if ((__builtin_expect(!!(!(memcmp(buffer, "foobar", 6) ==
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("memcmp(buffer, \"foobar\", 6) == 0"
), "../src/test/test-fd-util.c", 210, __PRETTY_FUNCTION__); }
while (0)
;
211
212 assert_se(rearrange_stdio(-1, 1, 2) >= 0)do { if ((__builtin_expect(!!(!(rearrange_stdio(-1, 1, 2) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("rearrange_stdio(-1, 1, 2) >= 0"
), "../src/test/test-fd-util.c", 212, __PRETTY_FUNCTION__); }
while (0)
;
213 assert_se(write(1, "a", 1) < 0 && errno == ENOSPC)do { if ((__builtin_expect(!!(!(write(1, "a", 1) < 0 &&
(*__errno_location ()) == 28)),0))) log_assert_failed_realm(
LOG_REALM_SYSTEMD, ("write(1, \"a\", 1) < 0 && errno == ENOSPC"
), "../src/test/test-fd-util.c", 213, __PRETTY_FUNCTION__); }
while (0)
;
214 assert_se(write(2, "y", 1) == 1)do { if ((__builtin_expect(!!(!(write(2, "y", 1) == 1)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("write(2, \"y\", 1) == 1"), "../src/test/test-fd-util.c"
, 214, __PRETTY_FUNCTION__); } while (0)
;
215 assert_se(read(3, buffer, sizeof(buffer)) == 1)do { if ((__builtin_expect(!!(!(read(3, buffer, sizeof(buffer
)) == 1)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("read(3, buffer, sizeof(buffer)) == 1"
), "../src/test/test-fd-util.c", 215, __PRETTY_FUNCTION__); }
while (0)
;
216 assert_se(buffer[0] == 'y')do { if ((__builtin_expect(!!(!(buffer[0] == 'y')),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("buffer[0] == 'y'"), "../src/test/test-fd-util.c"
, 216, __PRETTY_FUNCTION__); } while (0)
;
217
218 assert_se(fd_get_path(0, &path) >= 0)do { if ((__builtin_expect(!!(!(fd_get_path(0, &path) >=
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("fd_get_path(0, &path) >= 0"
), "../src/test/test-fd-util.c", 218, __PRETTY_FUNCTION__); }
while (0)
;
219 assert_se(path_equal(path, "/dev/null"))do { if ((__builtin_expect(!!(!(path_equal(path, "/dev/null")
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("path_equal(path, \"/dev/null\")"
), "../src/test/test-fd-util.c", 219, __PRETTY_FUNCTION__); }
while (0)
;
220 path = mfree(path);
221
222 _exit(EXIT_SUCCESS0);
223 }
224}
225
226static void assert_equal_fd(int fd1, int fd2) {
227
228 for (;;) {
229 uint8_t a[4096], b[4096];
230 ssize_t x, y;
231
232 x = read(fd1, a, sizeof(a));
233 assert(x >= 0)do { if ((__builtin_expect(!!(!(x >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("x >= 0"), "../src/test/test-fd-util.c"
, 233, __PRETTY_FUNCTION__); } while (0)
;
234
235 y = read(fd2, b, sizeof(b));
236 assert(y >= 0)do { if ((__builtin_expect(!!(!(y >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("y >= 0"), "../src/test/test-fd-util.c"
, 236, __PRETTY_FUNCTION__); } while (0)
;
237
238 assert(x == y)do { if ((__builtin_expect(!!(!(x == y)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("x == y"), "../src/test/test-fd-util.c",
238, __PRETTY_FUNCTION__); } while (0)
;
239
240 if (x == 0)
241 break;
242
243 assert(memcmp(a, b, x) == 0)do { if ((__builtin_expect(!!(!(memcmp(a, b, x) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("memcmp(a, b, x) == 0"), "../src/test/test-fd-util.c"
, 243, __PRETTY_FUNCTION__); } while (0)
;
244 }
245}
246
247static void test_fd_duplicate_data_fd(void) {
248 _cleanup_close___attribute__((cleanup(closep))) int fd1 = -1, fd2 = -1;
249 _cleanup_(close_pairp)__attribute__((cleanup(close_pairp))) int sfd[2] = { -1, -1 };
250 _cleanup_(sigkill_waitp)__attribute__((cleanup(sigkill_waitp))) pid_t pid = -1;
251 uint64_t i, j;
252 int r;
253
254 fd1 = open("/etc/fstab", O_RDONLY00|O_CLOEXEC02000000);
255 if (fd1 >= 0) {
256
257 fd2 = fd_duplicate_data_fd(fd1);
258 assert_se(fd2 >= 0)do { if ((__builtin_expect(!!(!(fd2 >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd2 >= 0"), "../src/test/test-fd-util.c"
, 258, __PRETTY_FUNCTION__); } while (0)
;
259
260 assert_se(lseek(fd1, 0, SEEK_SET) == 0)do { if ((__builtin_expect(!!(!(lseek(fd1, 0, 0) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("lseek(fd1, 0, SEEK_SET) == 0"), "../src/test/test-fd-util.c"
, 260, __PRETTY_FUNCTION__); } while (0)
;
261 assert_equal_fd(fd1, fd2);
262 }
263
264 fd1 = safe_close(fd1);
265 fd2 = safe_close(fd2);
266
267 fd1 = acquire_data_fd("hallo", 6, 0);
268 assert_se(fd1 >= 0)do { if ((__builtin_expect(!!(!(fd1 >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd1 >= 0"), "../src/test/test-fd-util.c"
, 268, __PRETTY_FUNCTION__); } while (0)
;
269
270 fd2 = fd_duplicate_data_fd(fd1);
271 assert_se(fd2 >= 0)do { if ((__builtin_expect(!!(!(fd2 >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd2 >= 0"), "../src/test/test-fd-util.c"
, 271, __PRETTY_FUNCTION__); } while (0)
;
272
273 safe_close(fd1);
274 fd1 = acquire_data_fd("hallo", 6, 0);
275 assert_se(fd1 >= 0)do { if ((__builtin_expect(!!(!(fd1 >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd1 >= 0"), "../src/test/test-fd-util.c"
, 275, __PRETTY_FUNCTION__); } while (0)
;
276
277 assert_equal_fd(fd1, fd2);
278
279 fd1 = safe_close(fd1);
280 fd2 = safe_close(fd2);
Value stored to 'fd2' is never read
281
282 assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sfd) >= 0)do { if ((__builtin_expect(!!(!(socketpair(1, SOCK_STREAM|SOCK_CLOEXEC
, 0, sfd) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sfd) >= 0"
), "../src/test/test-fd-util.c", 282, __PRETTY_FUNCTION__); }
while (0)
;
283
284 r = safe_fork("(sd-pipe)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
285 assert_se(r >= 0)do { if ((__builtin_expect(!!(!(r >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("r >= 0"), "../src/test/test-fd-util.c"
, 285, __PRETTY_FUNCTION__); } while (0)
;
286
287 if (r == 0) {
288 /* child */
289
290 sfd[0] = safe_close(sfd[0]);
291
292 for (i = 0; i < 1536*1024 / sizeof(uint64_t); i++)
293 assert_se(write(sfd[1], &i, sizeof(i)) == sizeof(i))do { if ((__builtin_expect(!!(!(write(sfd[1], &i, sizeof(
i)) == sizeof(i))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("write(sfd[1], &i, sizeof(i)) == sizeof(i)"), "../src/test/test-fd-util.c"
, 293, __PRETTY_FUNCTION__); } while (0)
;
294
295 sfd[1] = safe_close(sfd[1]);
296
297 _exit(EXIT_SUCCESS0);
298 }
299
300 sfd[1] = safe_close(sfd[1]);
301
302 fd2 = fd_duplicate_data_fd(sfd[0]);
303 assert_se(fd2 >= 0)do { if ((__builtin_expect(!!(!(fd2 >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd2 >= 0"), "../src/test/test-fd-util.c"
, 303, __PRETTY_FUNCTION__); } while (0)
;
304
305 for (i = 0; i < 1536*1024 / sizeof(uint64_t); i++) {
306 assert_se(read(fd2, &j, sizeof(j)) == sizeof(j))do { if ((__builtin_expect(!!(!(read(fd2, &j, sizeof(j)) ==
sizeof(j))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, (
"read(fd2, &j, sizeof(j)) == sizeof(j)"), "../src/test/test-fd-util.c"
, 306, __PRETTY_FUNCTION__); } while (0)
;
307 assert_se(i == j)do { if ((__builtin_expect(!!(!(i == j)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("i == j"), "../src/test/test-fd-util.c",
307, __PRETTY_FUNCTION__); } while (0)
;
308 }
309
310 assert_se(read(fd2, &j, sizeof(j)) == 0)do { if ((__builtin_expect(!!(!(read(fd2, &j, sizeof(j)) ==
0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("read(fd2, &j, sizeof(j)) == 0"
), "../src/test/test-fd-util.c", 310, __PRETTY_FUNCTION__); }
while (0)
;
311}
312
313static void test_read_nr_open(void) {
314 log_info("nr-open: %i", read_nr_open())({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/test/test-fd-util.c", 314, __func__, "nr-open: %i",
read_nr_open()) : -abs(_e); })
;
315}
316
317int main(int argc, char *argv[]) {
318
319 log_set_max_level(LOG_DEBUG)log_set_max_level_realm(LOG_REALM_SYSTEMD, (7));
320
321 test_close_many();
322 test_close_nointr();
323 test_same_fd();
324 test_open_serialization_fd();
325 test_acquire_data_fd();
326 test_fd_move_above_stdio();
327 test_rearrange_stdio();
328 test_fd_duplicate_data_fd();
329 test_read_nr_open();
330
331 return 0;
332}