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, loop, fd, 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 (loop = 1; loop <= max_val_opfiles; loop++) {
     84 		fd = open("/etc/hosts", O_RDONLY);
     85 #ifdef DEBUG
     86 		printf("Opened file num %d\n", fd);
     87 #endif
     88 		if (fd == -1)
     89 			break;
     90 		else
     91 			count = fd;
     92 	}
     93 
     94 //Now the max files opened should be RLIMIT_NOFILE - 1 , why ? read getdtablesize man page
     95 
     96 	if (count > 0)
     97 		close(count);
     98 	if (count == (max_val_opfiles - 1))
     99 		tst_resm(TPASS, "%d = %d", count, (max_val_opfiles - 1));
    100 	else
    101 		tst_resm(TFAIL, "%d != %d", count, (max_val_opfiles - 1));
    102 
    103 	cleanup();
    104 	tst_exit();
    105 }
    106 
    107 void setup(void)
    108 {
    109 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    110 
    111 	TEST_PAUSE;
    112 }
    113 
    114 void cleanup(void)
    115 {
    116 }
    117