2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
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/>.
27 static void test_fdset_new_fill(void) {
29 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
30 char name
[] = "/tmp/test-fdset_new_fill.XXXXXX";
32 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
34 assert_se(fdset_new_fill(&fdset
) >= 0);
35 assert_se(fdset_contains(fdset
, fd
));
40 static void test_fdset_put_dup(void) {
41 _cleanup_close_
int fd
= -1;
43 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
44 char name
[] = "/tmp/test-fdset_put_dup.XXXXXX";
46 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
51 copyfd
= fdset_put_dup(fdset
, fd
);
52 assert_se(copyfd
>= 0 && copyfd
!= fd
);
53 assert_se(fdset_contains(fdset
, copyfd
));
54 assert_se(!fdset_contains(fdset
, fd
));
59 static void test_fdset_cloexec(void) {
61 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
63 char name
[] = "/tmp/test-fdset_cloexec.XXXXXX";
65 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
70 assert_se(fdset_put(fdset
, fd
));
72 assert_se(fdset_cloexec(fdset
, false) >= 0);
73 flags
= fcntl(fd
, F_GETFD
);
74 assert_se(flags
>= 0);
75 assert_se(!(flags
& FD_CLOEXEC
));
77 assert_se(fdset_cloexec(fdset
, true) >= 0);
78 flags
= fcntl(fd
, F_GETFD
);
79 assert_se(flags
>= 0);
80 assert_se(flags
& FD_CLOEXEC
);
85 static void test_fdset_close_others(void) {
88 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
90 char name
[] = "/tmp/test-fdset_close_others.XXXXXX";
92 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
97 copyfd
= fdset_put_dup(fdset
, fd
);
98 assert_se(copyfd
>= 0);
100 assert_se(fdset_close_others(fdset
) >= 0);
101 flags
= fcntl(fd
, F_GETFD
);
102 assert_se(flags
< 0);
103 flags
= fcntl(copyfd
, F_GETFD
);
104 assert_se(flags
>= 0);
109 static void test_fdset_remove(void) {
110 _cleanup_close_
int fd
= -1;
112 char name
[] = "/tmp/test-fdset_remove.XXXXXX";
114 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
119 assert_se(fdset_put(fdset
, fd
) >= 0);
120 assert_se(fdset_remove(fdset
, fd
) >= 0);
121 assert_se(!fdset_contains(fdset
, fd
));
124 assert_se(fcntl(fd
, F_GETFD
) >= 0);
129 static void test_fdset_iterate(void) {
132 char name
[] = "/tmp/test-fdset_iterate.XXXXXX";
137 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
142 assert_se(fdset_put(fdset
, fd
) >= 0);
143 assert_se(fdset_put(fdset
, fd
) >= 0);
144 assert_se(fdset_put(fdset
, fd
) >= 0);
146 FDSET_FOREACH(a
, fdset
, i
) {
157 int main(int argc
, char *argv
[]) {
158 test_fdset_new_fill();
159 test_fdset_put_dup();
160 test_fdset_cloexec();
161 test_fdset_close_others();
163 test_fdset_iterate();