blob: a031e16d2e2b7ec0dc615daeb7cdf09bc6d2c840 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubis11622c32014-06-11 11:59:42 +02003 * AUTHOR : Glen Overby
4 * CO-PILOT : William Roske
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080023 * with this program; if not, write the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000025 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
plars865695b2001-08-27 22:15:12 +000034 */
Cyril Hrubis11622c32014-06-11 11:59:42 +020035/*
36 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
Cyril Hrubis11622c32014-06-11 11:59:42 +020038 * 1. test close-on-exec with a regular file
39 * 2. test close-on-exec with a pipe
40 * 3. test close-on-exec with a fifo
41 */
plars865695b2001-08-27 22:15:12 +000042
43#include <errno.h>
44#include <string.h>
45#include <signal.h>
46#include <sys/types.h>
47#include <unistd.h>
48#include <stdlib.h>
49#include <fcntl.h>
mridgedb639212005-01-04 21:04:11 +000050#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000051#include <limits.h>
52
53#include "test.h"
Garrett Cooper0a643cb2010-12-21 11:21:19 -080054#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000055
Cyril Hrubis11622c32014-06-11 11:59:42 +020056static void setup(void);
57static void cleanup(void);
58static void help(void);
plars865695b2001-08-27 22:15:12 +000059
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020060char *TCID = "fcntl07";
plars865695b2001-08-27 22:15:12 +000061
Cyril Hrubis11622c32014-06-11 11:59:42 +020062static char *t_opt;
plars865695b2001-08-27 22:15:12 +000063
64option_t options[] = {
Cyril Hrubis11622c32014-06-11 11:59:42 +020065 {"T:", NULL, &t_opt},
subrata_modak56207ce2009-03-23 13:35:39 +000066 {NULL, NULL, NULL}
plars865695b2001-08-27 22:15:12 +000067};
68
Cyril Hrubis11622c32014-06-11 11:59:42 +020069static int file_fd, pipe_fds[2], fifo_fd;
plars865695b2001-08-27 22:15:12 +000070
Cyril Hrubis11622c32014-06-11 11:59:42 +020071#define FIFONAME "fifo"
plars865695b2001-08-27 22:15:12 +000072
Cyril Hrubis11622c32014-06-11 11:59:42 +020073static struct tcase {
74 int *fd;
75 const char *msg;
76} tcases[] = {
77 {&file_fd, "regular file"},
78 {pipe_fds, "pipe (write end)"},
79 {pipe_fds+1, "pipe (read end)"},
80 {&fifo_fd, "fifo"},
subrata_modak56207ce2009-03-23 13:35:39 +000081};
plars865695b2001-08-27 22:15:12 +000082
Cyril Hrubis11622c32014-06-11 11:59:42 +020083int TST_TOTAL = ARRAY_SIZE(tcases);
plars865695b2001-08-27 22:15:12 +000084
Cyril Hrubis11622c32014-06-11 11:59:42 +020085static int test_open(char *arg);
86
87static void verify_cloexec(struct tcase *tc)
88{
89 int fd = *(tc->fd);
90 char pidname[255];
91 int status, pid;
92
93 TEST(fcntl(fd, F_SETFD, FD_CLOEXEC));
94
95 if (TEST_RETURN == -1) {
96 tst_resm(TFAIL | TTERRNO,
97 "fcntl(%s[%d], F_SETFD, FD_CLOEXEC) failed",
98 tc->msg, fd);
99 return;
100 }
101
102 sprintf(pidname, "%d", fd);
103
104 switch (pid = FORK_OR_VFORK()) {
105 case -1:
106 tst_resm(TBROK | TERRNO, "fork() failed");
107 return;
108 case 0:
109 execlp(TCID, TCID, "-T", pidname, NULL);
110
111 /* the ONLY reason to do this is to get the errno printed out */
112 fprintf(stderr, "exec(%s, %s, -T, %s) failed. Errno %s [%d]\n",
113 TCID, TCID, pidname, strerror(errno), errno);
114 exit(2);
115 default:
116 break;
117 }
118
119 waitpid(pid, &status, 0);
120
121 if (!WIFEXITED(status)) {
122 tst_resm(TBROK, "waitpid return was 0%o", status);
123 return;
124 }
125
126 switch ((WEXITSTATUS(status))) {
127 case 2:
128 tst_resm(TBROK, "exec failed");
129 break;
130 case 0:
131 tst_resm(TPASS, "%s CLOEXEC fd was closed after exec()",
132 tc->msg);
133 break;
134 default:
135 tst_resm(TFAIL, "%s child exited non-zero, %d",
136 tc->msg, WEXITSTATUS(status));
137 }
138}
plars865695b2001-08-27 22:15:12 +0000139
subrata_modak56207ce2009-03-23 13:35:39 +0000140int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000141{
Cyril Hrubis11622c32014-06-11 11:59:42 +0200142 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200143 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000144
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800145 if ((msg = parse_opts(ac, av, options, &help)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +0000146 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000147
Cyril Hrubis11622c32014-06-11 11:59:42 +0200148 if (t_opt)
149 exit(test_open(t_opt));
subrata_modak56207ce2009-03-23 13:35:39 +0000150
Cyril Hrubis11622c32014-06-11 11:59:42 +0200151 setup();
subrata_modak56207ce2009-03-23 13:35:39 +0000152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800154 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000155
Cyril Hrubis11622c32014-06-11 11:59:42 +0200156 for (i = 0; i < TST_TOTAL; i++)
157 verify_cloexec(tcases + i);
Garrett Cooper2c282152010-12-16 00:55:50 -0800158 }
subrata_modak56207ce2009-03-23 13:35:39 +0000159
subrata_modak56207ce2009-03-23 13:35:39 +0000160 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800161 tst_exit();
162}
plars865695b2001-08-27 22:15:12 +0000163
Cyril Hrubis11622c32014-06-11 11:59:42 +0200164void setup(void)
plars865695b2001-08-27 22:15:12 +0000165{
subrata_modak56207ce2009-03-23 13:35:39 +0000166 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000167
subrata_modak56207ce2009-03-23 13:35:39 +0000168 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000169
subrata_modak56207ce2009-03-23 13:35:39 +0000170 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000171
Cyril Hrubis11622c32014-06-11 11:59:42 +0200172 file_fd = SAFE_OPEN(cleanup, "test_file", O_CREAT | O_RDWR, 0666);
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800173 SAFE_PIPE(cleanup, pipe_fds);
Cyril Hrubis11622c32014-06-11 11:59:42 +0200174 SAFE_MKFIFO(cleanup, FIFONAME, 0666);
175 fifo_fd = SAFE_OPEN(cleanup, FIFONAME, O_RDWR, 0666);
Garrett Cooper2c282152010-12-16 00:55:50 -0800176}
plars865695b2001-08-27 22:15:12 +0000177
Mike Frysingerc57fba52014-04-09 18:56:30 -0400178void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000179{
Cyril Hrubis11622c32014-06-11 11:59:42 +0200180 if (file_fd > 0 && close(file_fd))
181 tst_resm(TWARN | TERRNO, "close(file_fd) failed");
182
183 if (pipe_fds[0] > 0 && close(pipe_fds[0]))
184 tst_resm(TWARN | TERRNO, "close(pipe_fds[0]) failed");
185
186 if (pipe_fds[1] > 0 && close(pipe_fds[1]))
187 tst_resm(TWARN | TERRNO, "close(pipe_fds[1]) failed");
188
189 if (fifo_fd > 0 && close(fifo_fd))
190 tst_resm(TWARN | TERRNO, "close(fifo_fd) failed");
plars865695b2001-08-27 22:15:12 +0000191
subrata_modak56207ce2009-03-23 13:35:39 +0000192 tst_rmdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800193}
plars865695b2001-08-27 22:15:12 +0000194
Mike Frysingerc57fba52014-04-09 18:56:30 -0400195void help(void)
plars865695b2001-08-27 22:15:12 +0000196{
Cyril Hrubis11622c32014-06-11 11:59:42 +0200197 printf(" -T fd The program runs as 'test_open()'\n");
plars865695b2001-08-27 22:15:12 +0000198}
199
subrata_modak56207ce2009-03-23 13:35:39 +0000200int test_open(char *arg)
plars865695b2001-08-27 22:15:12 +0000201{
subrata_modak56207ce2009-03-23 13:35:39 +0000202 int fd, rc;
203 int status;
plars865695b2001-08-27 22:15:12 +0000204
subrata_modak56207ce2009-03-23 13:35:39 +0000205 fd = atoi(arg);
206
207 rc = fcntl(fd, F_GETFD, &status);
plars865695b2001-08-27 22:15:12 +0000208
Cyril Hrubis11622c32014-06-11 11:59:42 +0200209 if (rc == -1 && errno == EBADF)
210 return 0;
plars865695b2001-08-27 22:15:12 +0000211
Cyril Hrubis11622c32014-06-11 11:59:42 +0200212 fprintf(stderr, "fcntl() returned %i, errno %s(%i)\n",
213 rc, tst_strerrno(errno), errno);
plars865695b2001-08-27 22:15:12 +0000214
Cyril Hrubis11622c32014-06-11 11:59:42 +0200215 return 1;
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800216}