Home | History | Annotate | Download | only in getdtablesize
      1 /*
      2  * Copyright (c) International Business Machines  Corp., 2005
      3  * Copyright (c) Wipro Technologies Ltd, 2005.  All Rights Reserved.
      4  *
      5  * This program is free software; you can redistribute it and/or modify it
      6  * under the terms of version 2 of the GNU General Public License as
      7  * published by the Free Software Foundation.
      8  *
      9  * This program is distributed in the hope that it would be useful, but
     10  * WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12  *
     13  * You should have received a copy of the GNU General Public License along
     14  * with this program; if not, write the Free Software Foundation, Inc.,
     15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16  *
     17  */
     18 /**********************************************************
     19  *
     20  *    TEST IDENTIFIER   : getdtablesize01
     21  *
     22  *    EXECUTED BY       : root / superuser
     23  *
     24  *    TEST TITLE        : Basic tests for getdtablesize01(2)
     25  *
     26  *    TEST CASE TOTAL   : 1
     27  *
     28  *    AUTHOR            : Prashant P Yendigeri
     29  *                        <prashant.yendigeri (at) wipro.com>
     30  *                        Robbie Williamson
     31  *                        <robbiew (at) us.ibm.com>
     32  *
     33  *    DESCRIPTION
     34  *      This is a Phase I test for the getdtablesize01(2) system call.
     35  *      It is intended to provide a limited exposure of the system call.
     36  *
     37  **********************************************************/
     38 
     39 #include <stdio.h>
     40 #include <errno.h>
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <fcntl.h>
     44 #include <sys/time.h>
     45 #include <sys/resource.h>
     46 #include <unistd.h>
     47 #include "test.h"
     48 
     49 void setup();
     50 void cleanup();
     51 
     52 char *TCID = "getdtablesize01";
     53 int TST_TOTAL = 1;
     54 
     55 int main(void)
     56 {
     57 	int table_size, fd = 0, count = 0;
     58 	int max_val_opfiles;
     59 	struct rlimit rlp;
     60 
     61 	setup();
     62 	table_size = getdtablesize();
     63 	getrlimit(RLIMIT_NOFILE, &rlp);
     64 	max_val_opfiles = (rlim_t) rlp.rlim_cur;
     65 
     66 	tst_resm(TINFO,
     67 		 "Maximum number of files a process can have opened is %d",
     68 		 table_size);
     69 	tst_resm(TINFO,
     70 		 "Checking with the value returned by getrlimit...RLIMIT_NOFILE");
     71 
     72 	if (table_size == max_val_opfiles)
     73 		tst_resm(TPASS, "got correct dtablesize, value is %d",
     74 			 max_val_opfiles);
     75 	else {
     76 		tst_resm(TFAIL, "got incorrect table size, value is %d",
     77 			 max_val_opfiles);
     78 		cleanup();
     79 	}
     80 
     81 	tst_resm(TINFO,
     82 		 "Checking Max num of files that can be opened by a process.Should be: RLIMIT_NOFILE - 1");
     83 	for (;;) {
     84 		fd = open("/etc/hosts", O_RDONLY);
     85 
     86 		if (fd == -1)
     87 			break;
     88 		count = fd;
     89 
     90 #ifdef DEBUG
     91 		printf("Opened file num %d\n", fd);
     92 #endif
     93 	}
     94 
     95 //Now the max files opened should be RLIMIT_NOFILE - 1 , why ? read getdtablesize man page
     96 
     97 	if (count > 0)
     98 		close(count);
     99 	if (count == (max_val_opfiles - 1))
    100 		tst_resm(TPASS, "%d = %d", count, (max_val_opfiles - 1));
    101 	else if (fd < 0 && errno == ENFILE)
    102 		tst_brkm(TCONF, cleanup, "Reached maximum number of open files for the system");
    103 	else
    104 		tst_resm(TFAIL, "%d != %d", count, (max_val_opfiles - 1));
    105 
    106 	cleanup();
    107 	tst_exit();
    108 }
    109 
    110 void setup(void)
    111 {
    112 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    113 
    114 	TEST_PAUSE;
    115 }
    116 
    117 void cleanup(void)
    118 {
    119 }
    120