plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 3 | * AUTHOR : Glen Overby |
| 4 | * CO-PILOT : William Roske |
| 5 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 6 | * |
| 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 Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 23 | * with this program; if not, write the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | * |
| 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/ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | */ |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 35 | /* |
| 36 | * TEST CASES |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 37 | * |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 38 | * 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 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
| 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> |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 50 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | #include <limits.h> |
| 52 | |
| 53 | #include "test.h" |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 54 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 56 | static void setup(void); |
| 57 | static void cleanup(void); |
| 58 | static void help(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 60 | char *TCID = "fcntl07"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 62 | static char *t_opt; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
| 64 | option_t options[] = { |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 65 | {"T:", NULL, &t_opt}, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | {NULL, NULL, NULL} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 69 | static int file_fd, pipe_fds[2], fifo_fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 71 | #define FIFONAME "fifo" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 73 | static 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 83 | int TST_TOTAL = ARRAY_SIZE(tcases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 85 | static int test_open(char *arg); |
| 86 | |
| 87 | static 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | { |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 142 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 143 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 145 | if ((msg = parse_opts(ac, av, options, &help)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 148 | if (t_opt) |
| 149 | exit(test_open(t_opt)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 151 | setup(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 154 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 155 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 156 | for (i = 0; i < TST_TOTAL; i++) |
| 157 | verify_cloexec(tcases + i); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 158 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 161 | tst_exit(); |
| 162 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 164 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 172 | file_fd = SAFE_OPEN(cleanup, "test_file", O_CREAT | O_RDWR, 0666); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 173 | SAFE_PIPE(cleanup, pipe_fds); |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 174 | SAFE_MKFIFO(cleanup, FIFONAME, 0666); |
| 175 | fifo_fd = SAFE_OPEN(cleanup, FIFONAME, O_RDWR, 0666); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 176 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 178 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | { |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 180 | 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"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | tst_rmdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 193 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 195 | void help(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | { |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 197 | printf(" -T fd The program runs as 'test_open()'\n"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | } |
| 199 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | int test_open(char *arg) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | int fd, rc; |
| 203 | int status; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | fd = atoi(arg); |
| 206 | |
| 207 | rc = fcntl(fd, F_GETFD, &status); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 209 | if (rc == -1 && errno == EBADF) |
| 210 | return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 212 | fprintf(stderr, "fcntl() returned %i, errno %s(%i)\n", |
| 213 | rc, tst_strerrno(errno), errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | |
Cyril Hrubis | 11622c3 | 2014-06-11 11:59:42 +0200 | [diff] [blame] | 215 | return 1; |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 216 | } |