1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker * AUTHOR : Glen Overby
4*49cdfc7eSAndroid Build Coastguard Worker * CO-PILOT : William Roske
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014 Cyril Hrubis <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker *
7*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify it
8*49cdfc7eSAndroid Build Coastguard Worker * under the terms of version 2 of the GNU General Public License as
9*49cdfc7eSAndroid Build Coastguard Worker * published by the Free Software Foundation.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it would be useful, but
12*49cdfc7eSAndroid Build Coastguard Worker * WITHOUT ANY WARRANTY; without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * Further, this software is distributed without any warranty that it is
16*49cdfc7eSAndroid Build Coastguard Worker * free of the rightful claim of any third person regarding infringement
17*49cdfc7eSAndroid Build Coastguard Worker * or the like. Any license provided herein, whether implied or
18*49cdfc7eSAndroid Build Coastguard Worker * otherwise, applies only to this software file. Patent licenses, if
19*49cdfc7eSAndroid Build Coastguard Worker * any, provided herein do not apply to combinations of this program with
20*49cdfc7eSAndroid Build Coastguard Worker * other software, or any other product whatsoever.
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License along
23*49cdfc7eSAndroid Build Coastguard Worker * with this program; if not, write the Free Software Foundation, Inc.,
24*49cdfc7eSAndroid Build Coastguard Worker * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25*49cdfc7eSAndroid Build Coastguard Worker *
26*49cdfc7eSAndroid Build Coastguard Worker * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27*49cdfc7eSAndroid Build Coastguard Worker * Mountain View, CA 94043, or:
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * http://www.sgi.com
30*49cdfc7eSAndroid Build Coastguard Worker *
31*49cdfc7eSAndroid Build Coastguard Worker * For further information regarding this notice, see:
32*49cdfc7eSAndroid Build Coastguard Worker *
33*49cdfc7eSAndroid Build Coastguard Worker * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34*49cdfc7eSAndroid Build Coastguard Worker */
35*49cdfc7eSAndroid Build Coastguard Worker /*
36*49cdfc7eSAndroid Build Coastguard Worker * TEST CASES
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker * 1. test close-on-exec with a regular file
39*49cdfc7eSAndroid Build Coastguard Worker * 2. test close-on-exec with a pipe
40*49cdfc7eSAndroid Build Coastguard Worker * 3. test close-on-exec with a fifo
41*49cdfc7eSAndroid Build Coastguard Worker */
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
54*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
57*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
58*49cdfc7eSAndroid Build Coastguard Worker static void help(void);
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fcntl07";
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker static char *t_opt;
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker option_t options[] = {
65*49cdfc7eSAndroid Build Coastguard Worker {"T:", NULL, &t_opt},
66*49cdfc7eSAndroid Build Coastguard Worker {NULL, NULL, NULL}
67*49cdfc7eSAndroid Build Coastguard Worker };
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker static int file_fd, pipe_fds[2], fifo_fd;
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker #define FIFONAME "fifo"
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
74*49cdfc7eSAndroid Build Coastguard Worker int *fd;
75*49cdfc7eSAndroid Build Coastguard Worker const char *msg;
76*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
77*49cdfc7eSAndroid Build Coastguard Worker {&file_fd, "regular file"},
78*49cdfc7eSAndroid Build Coastguard Worker {pipe_fds, "pipe (write end)"},
79*49cdfc7eSAndroid Build Coastguard Worker {pipe_fds+1, "pipe (read end)"},
80*49cdfc7eSAndroid Build Coastguard Worker {&fifo_fd, "fifo"},
81*49cdfc7eSAndroid Build Coastguard Worker };
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(tcases);
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker static int test_open(char *arg);
86*49cdfc7eSAndroid Build Coastguard Worker
verify_cloexec(struct tcase * tc)87*49cdfc7eSAndroid Build Coastguard Worker static void verify_cloexec(struct tcase *tc)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker int fd = *(tc->fd);
90*49cdfc7eSAndroid Build Coastguard Worker char pidname[255];
91*49cdfc7eSAndroid Build Coastguard Worker int status, pid;
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker TEST(fcntl(fd, F_SETFD, FD_CLOEXEC));
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
96*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO,
97*49cdfc7eSAndroid Build Coastguard Worker "fcntl(%s[%d], F_SETFD, FD_CLOEXEC) failed",
98*49cdfc7eSAndroid Build Coastguard Worker tc->msg, fd);
99*49cdfc7eSAndroid Build Coastguard Worker return;
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker sprintf(pidname, "%d", fd);
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker switch (pid = tst_fork()) {
105*49cdfc7eSAndroid Build Coastguard Worker case -1:
106*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK | TERRNO, "fork() failed");
107*49cdfc7eSAndroid Build Coastguard Worker return;
108*49cdfc7eSAndroid Build Coastguard Worker case 0:
109*49cdfc7eSAndroid Build Coastguard Worker execlp(TCID, TCID, "-T", pidname, NULL);
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker /* the ONLY reason to do this is to get the errno printed out */
112*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "exec(%s, %s, -T, %s) failed. Errno %s [%d]\n",
113*49cdfc7eSAndroid Build Coastguard Worker TCID, TCID, pidname, strerror(errno), errno);
114*49cdfc7eSAndroid Build Coastguard Worker exit(2);
115*49cdfc7eSAndroid Build Coastguard Worker default:
116*49cdfc7eSAndroid Build Coastguard Worker break;
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker waitpid(pid, &status, 0);
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker if (!WIFEXITED(status)) {
122*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK, "waitpid return was 0%o", status);
123*49cdfc7eSAndroid Build Coastguard Worker return;
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker switch ((WEXITSTATUS(status))) {
127*49cdfc7eSAndroid Build Coastguard Worker case 2:
128*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK, "exec failed");
129*49cdfc7eSAndroid Build Coastguard Worker break;
130*49cdfc7eSAndroid Build Coastguard Worker case 0:
131*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "%s CLOEXEC fd was closed after exec()",
132*49cdfc7eSAndroid Build Coastguard Worker tc->msg);
133*49cdfc7eSAndroid Build Coastguard Worker break;
134*49cdfc7eSAndroid Build Coastguard Worker default:
135*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "%s child exited non-zero, %d",
136*49cdfc7eSAndroid Build Coastguard Worker tc->msg, WEXITSTATUS(status));
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)140*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
141*49cdfc7eSAndroid Build Coastguard Worker {
142*49cdfc7eSAndroid Build Coastguard Worker int lc, i;
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, options, &help);
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker if (t_opt)
147*49cdfc7eSAndroid Build Coastguard Worker exit(test_open(t_opt));
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker setup();
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
152*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
153*49cdfc7eSAndroid Build Coastguard Worker
154*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < TST_TOTAL; i++)
155*49cdfc7eSAndroid Build Coastguard Worker verify_cloexec(tcases + i);
156*49cdfc7eSAndroid Build Coastguard Worker }
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker cleanup();
159*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
160*49cdfc7eSAndroid Build Coastguard Worker }
161*49cdfc7eSAndroid Build Coastguard Worker
setup(void)162*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
163*49cdfc7eSAndroid Build Coastguard Worker {
164*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker file_fd = SAFE_OPEN(cleanup, "test_file", O_CREAT | O_RDWR, 0666);
171*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(cleanup, pipe_fds);
172*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKFIFO(cleanup, FIFONAME, 0666);
173*49cdfc7eSAndroid Build Coastguard Worker fifo_fd = SAFE_OPEN(cleanup, FIFONAME, O_RDWR, 0666);
174*49cdfc7eSAndroid Build Coastguard Worker }
175*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)176*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
177*49cdfc7eSAndroid Build Coastguard Worker {
178*49cdfc7eSAndroid Build Coastguard Worker if (file_fd > 0 && close(file_fd))
179*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO, "close(file_fd) failed");
180*49cdfc7eSAndroid Build Coastguard Worker
181*49cdfc7eSAndroid Build Coastguard Worker if (pipe_fds[0] > 0 && close(pipe_fds[0]))
182*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO, "close(pipe_fds[0]) failed");
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker if (pipe_fds[1] > 0 && close(pipe_fds[1]))
185*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO, "close(pipe_fds[1]) failed");
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker if (fifo_fd > 0 && close(fifo_fd))
188*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO, "close(fifo_fd) failed");
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
191*49cdfc7eSAndroid Build Coastguard Worker }
192*49cdfc7eSAndroid Build Coastguard Worker
help(void)193*49cdfc7eSAndroid Build Coastguard Worker void help(void)
194*49cdfc7eSAndroid Build Coastguard Worker {
195*49cdfc7eSAndroid Build Coastguard Worker printf(" -T fd The program runs as 'test_open()'\n");
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker
test_open(char * arg)198*49cdfc7eSAndroid Build Coastguard Worker int test_open(char *arg)
199*49cdfc7eSAndroid Build Coastguard Worker {
200*49cdfc7eSAndroid Build Coastguard Worker int fd, rc;
201*49cdfc7eSAndroid Build Coastguard Worker int status;
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker fd = atoi(arg);
204*49cdfc7eSAndroid Build Coastguard Worker
205*49cdfc7eSAndroid Build Coastguard Worker rc = fcntl(fd, F_GETFD, &status);
206*49cdfc7eSAndroid Build Coastguard Worker
207*49cdfc7eSAndroid Build Coastguard Worker if (rc == -1 && errno == EBADF)
208*49cdfc7eSAndroid Build Coastguard Worker return 0;
209*49cdfc7eSAndroid Build Coastguard Worker
210*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "fcntl() returned %i, errno %s(%i)\n",
211*49cdfc7eSAndroid Build Coastguard Worker rc, tst_strerrno(errno), errno);
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker return 1;
214*49cdfc7eSAndroid Build Coastguard Worker }
215