blob: 835caadf6fbb0e635c43ae3b779d9b5a47306a7a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080017 *
plars865695b2001-08-27 22:15:12 +000018 * NAME
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080019 * fork09.c
plars865695b2001-08-27 22:15:12 +000020 *
21 * DESCRIPTION
22 * Check that child has access to a full set of files.
23 *
24 * ALGORITHM
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080025 * Parent opens a maximum number of files
26 * Child closes one and attempts to open another, it should be
27 * available
plars865695b2001-08-27 22:15:12 +000028 *
29 * USAGE
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080030 * fork09
plars865695b2001-08-27 22:15:12 +000031 *
32 * HISTORY
33 * 07/2001 Ported by Wayne Boyer
34 *
subrata_modakec047142008-10-22 12:12:45 +000035 * 10/2008 Suzuki K P <suzuki@in.ibm.com>
36 * Fix maximum number of files open logic.
37 *
plars865695b2001-08-27 22:15:12 +000038 * RESTRICTIONS
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080039 * None
plars865695b2001-08-27 22:15:12 +000040 */
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080041
plars74948ad2002-11-14 16:16:14 +000042#include <sys/types.h>
43#include <sys/wait.h>
44#include <sys/stat.h>
45#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000046#include <stdio.h>
47#include <errno.h>
subrata_modak56207ce2009-03-23 13:35:39 +000048#include <unistd.h> /* for _SC_OPEN_MAX */
plars865695b2001-08-27 22:15:12 +000049#include "test.h"
plars865695b2001-08-27 22:15:12 +000050
robbiew0422f202002-02-27 17:48:21 +000051char *TCID = "fork09";
plars865695b2001-08-27 22:15:12 +000052int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000053
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080054static void setup(void);
55static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000056
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080057static char filname[40], childfile[40];
58static int first;
59static FILE **fildeses; /* file streams */
60static int mypid, nfiles;
plars865695b2001-08-27 22:15:12 +000061
subrata_modak28e81492007-10-04 16:46:49 +000062#define OPEN_MAX (sysconf(_SC_OPEN_MAX))
63
plars74948ad2002-11-14 16:16:14 +000064int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000065{
plars74948ad2002-11-14 16:16:14 +000066 int pid, status, nf;
subrata_modakbdbaec52009-02-26 12:14:51 +000067
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080068 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
plars865695b2001-08-27 22:15:12 +000070
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080071 msg = parse_opts(ac, av, NULL, NULL);
72 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080073 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000074
plars865695b2001-08-27 22:15:12 +000075 setup();
76
Cyril Hrubisd218f342014-09-23 13:14:56 +020077 fildeses = malloc((OPEN_MAX + 10) * sizeof(FILE *));
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080078 if (fildeses == NULL)
plars865695b2001-08-27 22:15:12 +000079 tst_brkm(TBROK, cleanup, "malloc failed");
plars865695b2001-08-27 22:15:12 +000080
plars865695b2001-08-27 22:15:12 +000081 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080082 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000083 mypid = getpid();
84
subrata_modak923b23f2009-11-02 13:57:16 +000085 tst_resm(TINFO, "OPEN_MAX is %ld", OPEN_MAX);
plars865695b2001-08-27 22:15:12 +000086
87 /* establish first free file */
robbiew0422f202002-02-27 17:48:21 +000088 sprintf(filname, "fork09.%d", mypid);
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080089 first = creat(filname, 0660);
90 if (first == -1)
plars865695b2001-08-27 22:15:12 +000091 tst_brkm(TBROK, cleanup, "Cannot open first file %s, "
92 "errno = %d", filname, errno);
plars865695b2001-08-27 22:15:12 +000093 close(first);
94
95 tst_resm(TINFO, "first file descriptor is %d ", first);
96
Wanlong Gaoa91a4de2012-09-18 17:14:01 +080097 if (unlink(filname) == -1)
plars865695b2001-08-27 22:15:12 +000098 tst_brkm(TBROK, cleanup, "Cannot unlink file %s, "
99 "errno = %d", filname, errno);
plars865695b2001-08-27 22:15:12 +0000100
101 /*
102 * now open all the files for the test
103 */
104 for (nfiles = first; nfiles < OPEN_MAX; nfiles++) {
105 sprintf(filname, "file%d.%d", nfiles, mypid);
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800106 fildeses[nfiles] = fopen(filname, "a");
107 if (fildeses[nfiles] == NULL) {
subrata_modakec047142008-10-22 12:12:45 +0000108 /* Did we already reach OPEN_MAX ? */
109 if (errno == EMFILE)
110 break;
plars865695b2001-08-27 22:15:12 +0000111 tst_brkm(TBROK, cleanup, "Parent: cannot open "
112 "file %d %s errno = %d", nfiles,
113 filname, errno);
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800114 }
plars865695b2001-08-27 22:15:12 +0000115#ifdef DEBUG
116 tst_resm(TINFO, "filname: %s", filname);
117#endif
118 }
119
120 tst_resm(TINFO, "Parent reporting %d files open", nfiles - 1);
121
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800122 pid = fork();
123 if (pid == -1)
plars865695b2001-08-27 22:15:12 +0000124 tst_brkm(TBROK, cleanup, "Fork failed");
plars865695b2001-08-27 22:15:12 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +0000127 nfiles--;
128 if (fclose(fildeses[nfiles]) == -1) {
129 tst_resm(TINFO, "Child could not close file "
130 "#%d, errno = %d", nfiles, errno);
131 exit(1);
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800132 } else {
plars865695b2001-08-27 22:15:12 +0000133 sprintf(childfile, "cfile.%d", getpid());
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800134 fildeses[nfiles] = fopen(childfile, "a");
135 if (fildeses[nfiles] == NULL) {
plars865695b2001-08-27 22:15:12 +0000136 tst_resm(TINFO, "Child could not open "
137 "file %s, errno = %d",
138 childfile, errno);
139 exit(1);
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800140 } else {
plars865695b2001-08-27 22:15:12 +0000141 tst_resm(TINFO, "Child opened new "
subrata_modak4bb656a2009-02-26 12:02:09 +0000142 "file #%d", nfiles);
plars865695b2001-08-27 22:15:12 +0000143 unlink(childfile);
144 exit(0);
145 }
146 }
subrata_modak56207ce2009-03-23 13:35:39 +0000147 } else { /* parent */
plars865695b2001-08-27 22:15:12 +0000148 wait(&status);
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800149 if (status >> 8 != 0)
plars865695b2001-08-27 22:15:12 +0000150 tst_resm(TFAIL, "test 1 FAILED");
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800151 else
plars865695b2001-08-27 22:15:12 +0000152 tst_resm(TPASS, "test 1 PASSED");
plars865695b2001-08-27 22:15:12 +0000153 }
154
155 /* clean up things in case we are looping */
156 for (nf = first; nf < nfiles; nf++) {
157 fclose(fildeses[nf]);
158 sprintf(filname, "file%d.%d", nf, mypid);
159 unlink(filname);
160 }
161 }
plars865695b2001-08-27 22:15:12 +0000162
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800163 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800164 tst_exit();
plars865695b2001-08-27 22:15:12 +0000165}
subrata_modakbdbaec52009-02-26 12:14:51 +0000166
Mike Frysingerc57fba52014-04-09 18:56:30 -0400167static void setup(void)
plars865695b2001-08-27 22:15:12 +0000168{
plars865695b2001-08-27 22:15:12 +0000169 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000170 umask(0);
171
plars865695b2001-08-27 22:15:12 +0000172 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000173 tst_tmpdir();
174}
175
Mike Frysingerc57fba52014-04-09 18:56:30 -0400176static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000177{
plars865695b2001-08-27 22:15:12 +0000178 tst_rmdir();
Wanlong Gaoa91a4de2012-09-18 17:14:01 +0800179}