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