1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 5f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 6f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 7f08c3bdfSopenharmony_ci * (at your option) any later version. 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 15f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 16f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17f08c3bdfSopenharmony_ci * 18f08c3bdfSopenharmony_ci * NAME 19f08c3bdfSopenharmony_ci * fork09.c 20f08c3bdfSopenharmony_ci * 21f08c3bdfSopenharmony_ci * DESCRIPTION 22f08c3bdfSopenharmony_ci * Check that child has access to a full set of files. 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * ALGORITHM 25f08c3bdfSopenharmony_ci * Parent opens a maximum number of files 26f08c3bdfSopenharmony_ci * Child closes one and attempts to open another, it should be 27f08c3bdfSopenharmony_ci * available 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * USAGE 30f08c3bdfSopenharmony_ci * fork09 31f08c3bdfSopenharmony_ci * 32f08c3bdfSopenharmony_ci * HISTORY 33f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 34f08c3bdfSopenharmony_ci * 35f08c3bdfSopenharmony_ci * 10/2008 Suzuki K P <suzuki@in.ibm.com> 36f08c3bdfSopenharmony_ci * Fix maximum number of files open logic. 37f08c3bdfSopenharmony_ci * 38f08c3bdfSopenharmony_ci * RESTRICTIONS 39f08c3bdfSopenharmony_ci * None 40f08c3bdfSopenharmony_ci */ 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci#include <sys/types.h> 43f08c3bdfSopenharmony_ci#include <sys/wait.h> 44f08c3bdfSopenharmony_ci#include <sys/stat.h> 45f08c3bdfSopenharmony_ci#include <fcntl.h> 46f08c3bdfSopenharmony_ci#include <stdio.h> 47f08c3bdfSopenharmony_ci#include <errno.h> 48f08c3bdfSopenharmony_ci#include <unistd.h> /* for _SC_OPEN_MAX */ 49f08c3bdfSopenharmony_ci#include "test.h" 50f08c3bdfSopenharmony_ci#include "safe_macros.h" 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cichar *TCID = "fork09"; 53f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_cistatic void setup(void); 56f08c3bdfSopenharmony_cistatic void cleanup(void); 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_cistatic char filname[40], childfile[40]; 59f08c3bdfSopenharmony_cistatic int first; 60f08c3bdfSopenharmony_cistatic FILE **fildeses; /* file streams */ 61f08c3bdfSopenharmony_cistatic int mypid, nfiles; 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci#define OPEN_MAX (sysconf(_SC_OPEN_MAX)) 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ciint main(int ac, char **av) 66f08c3bdfSopenharmony_ci{ 67f08c3bdfSopenharmony_ci int pid, status, nf; 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci int lc; 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci setup(); 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci fildeses = malloc((OPEN_MAX + 10) * sizeof(FILE *)); 76f08c3bdfSopenharmony_ci if (fildeses == NULL) 77f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "malloc failed"); 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 80f08c3bdfSopenharmony_ci tst_count = 0; 81f08c3bdfSopenharmony_ci mypid = getpid(); 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci tst_resm(TINFO, "OPEN_MAX is %ld", OPEN_MAX); 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci /* establish first free file */ 86f08c3bdfSopenharmony_ci sprintf(filname, "fork09.%d", mypid); 87f08c3bdfSopenharmony_ci first = SAFE_CREAT(cleanup, filname, 0660); 88f08c3bdfSopenharmony_ci close(first); 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci tst_resm(TINFO, "first file descriptor is %d ", first); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci SAFE_UNLINK(cleanup, filname); 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci /* 95f08c3bdfSopenharmony_ci * now open all the files for the test 96f08c3bdfSopenharmony_ci */ 97f08c3bdfSopenharmony_ci for (nfiles = first; nfiles < OPEN_MAX; nfiles++) { 98f08c3bdfSopenharmony_ci sprintf(filname, "file%d.%d", nfiles, mypid); 99f08c3bdfSopenharmony_ci fildeses[nfiles] = fopen(filname, "a"); 100f08c3bdfSopenharmony_ci if (fildeses[nfiles] == NULL) { 101f08c3bdfSopenharmony_ci /* Did we already reach OPEN_MAX ? */ 102f08c3bdfSopenharmony_ci if (errno == EMFILE) 103f08c3bdfSopenharmony_ci break; 104f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "Parent: cannot open " 105f08c3bdfSopenharmony_ci "file %d %s errno = %d", nfiles, 106f08c3bdfSopenharmony_ci filname, errno); 107f08c3bdfSopenharmony_ci } 108f08c3bdfSopenharmony_ci#ifdef DEBUG 109f08c3bdfSopenharmony_ci tst_resm(TINFO, "filname: %s", filname); 110f08c3bdfSopenharmony_ci#endif 111f08c3bdfSopenharmony_ci } 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci tst_resm(TINFO, "Parent reporting %d files open", nfiles - 1); 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci pid = fork(); 116f08c3bdfSopenharmony_ci if (pid == -1) 117f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "Fork failed"); 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci if (pid == 0) { /* child */ 120f08c3bdfSopenharmony_ci nfiles--; 121f08c3bdfSopenharmony_ci if (fclose(fildeses[nfiles]) == -1) { 122f08c3bdfSopenharmony_ci tst_resm(TINFO, "Child could not close file " 123f08c3bdfSopenharmony_ci "#%d, errno = %d", nfiles, errno); 124f08c3bdfSopenharmony_ci exit(1); 125f08c3bdfSopenharmony_ci } else { 126f08c3bdfSopenharmony_ci sprintf(childfile, "cfile.%d", getpid()); 127f08c3bdfSopenharmony_ci fildeses[nfiles] = fopen(childfile, "a"); 128f08c3bdfSopenharmony_ci if (fildeses[nfiles] == NULL) { 129f08c3bdfSopenharmony_ci tst_resm(TINFO, "Child could not open " 130f08c3bdfSopenharmony_ci "file %s, errno = %d", 131f08c3bdfSopenharmony_ci childfile, errno); 132f08c3bdfSopenharmony_ci exit(1); 133f08c3bdfSopenharmony_ci } else { 134f08c3bdfSopenharmony_ci tst_resm(TINFO, "Child opened new " 135f08c3bdfSopenharmony_ci "file #%d", nfiles); 136f08c3bdfSopenharmony_ci unlink(childfile); 137f08c3bdfSopenharmony_ci exit(0); 138f08c3bdfSopenharmony_ci } 139f08c3bdfSopenharmony_ci } 140f08c3bdfSopenharmony_ci } else { /* parent */ 141f08c3bdfSopenharmony_ci wait(&status); 142f08c3bdfSopenharmony_ci if (status >> 8 != 0) 143f08c3bdfSopenharmony_ci tst_resm(TFAIL, "test 1 FAILED"); 144f08c3bdfSopenharmony_ci else 145f08c3bdfSopenharmony_ci tst_resm(TPASS, "test 1 PASSED"); 146f08c3bdfSopenharmony_ci } 147f08c3bdfSopenharmony_ci 148f08c3bdfSopenharmony_ci /* clean up things in case we are looping */ 149f08c3bdfSopenharmony_ci for (nf = first; nf < nfiles; nf++) { 150f08c3bdfSopenharmony_ci fclose(fildeses[nf]); 151f08c3bdfSopenharmony_ci sprintf(filname, "file%d.%d", nf, mypid); 152f08c3bdfSopenharmony_ci unlink(filname); 153f08c3bdfSopenharmony_ci } 154f08c3bdfSopenharmony_ci } 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_ci cleanup(); 157f08c3bdfSopenharmony_ci tst_exit(); 158f08c3bdfSopenharmony_ci} 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_cistatic void setup(void) 161f08c3bdfSopenharmony_ci{ 162f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 163f08c3bdfSopenharmony_ci umask(0); 164f08c3bdfSopenharmony_ci 165f08c3bdfSopenharmony_ci TEST_PAUSE; 166f08c3bdfSopenharmony_ci tst_tmpdir(); 167f08c3bdfSopenharmony_ci} 168f08c3bdfSopenharmony_ci 169f08c3bdfSopenharmony_cistatic void cleanup(void) 170f08c3bdfSopenharmony_ci{ 171f08c3bdfSopenharmony_ci tst_rmdir(); 172f08c3bdfSopenharmony_ci} 173