blob: 7ee231adfdf94d469e035ada096bb9eed802df08 [file] [log] [blame]
robbiew7a171362005-05-26 19:04:18 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080014 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew7a171362005-05-26 19:04:18 +000016 *
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@wipro.com>
30 * Robbie Williamson
31 * <robbiew@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>
robbiew7a171362005-05-26 19:04:18 +000040#include <errno.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <fcntl.h>
robbiew804899e2005-07-19 18:36:53 +000044#include <sys/time.h>
45#include <sys/resource.h>
46#include <unistd.h>
robbiew7a171362005-05-26 19:04:18 +000047#include "test.h"
robbiew7a171362005-05-26 19:04:18 +000048
vapieraa354722006-05-26 06:26:37 +000049void setup();
50void cleanup();
robbiew7a171362005-05-26 19:04:18 +000051
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020052char *TCID = "getdtablesize01";
53int TST_TOTAL = 1;
robbiew7a171362005-05-26 19:04:18 +000054
Mike Frysingerc57fba52014-04-09 18:56:30 -040055int main(void)
robbiew7a171362005-05-26 19:04:18 +000056{
subrata_modak56207ce2009-03-23 13:35:39 +000057 int table_size, loop, fd, count = 0;
robbiew804899e2005-07-19 18:36:53 +000058 int max_val_opfiles;
subrata_modak56207ce2009-03-23 13:35:39 +000059 struct rlimit rlp;
robbiew804899e2005-07-19 18:36:53 +000060
subrata_modak56207ce2009-03-23 13:35:39 +000061 setup();
62 table_size = getdtablesize();
63 getrlimit(RLIMIT_NOFILE, &rlp);
64 max_val_opfiles = (rlim_t) rlp.rlim_cur;
robbiew7a171362005-05-26 19:04:18 +000065
subrata_modak56207ce2009-03-23 13:35:39 +000066 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");
robbiew804899e2005-07-19 18:36:53 +000071
subrata_modak56207ce2009-03-23 13:35:39 +000072 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 }
robbiew7a171362005-05-26 19:04:18 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081 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);
robbiew7a171362005-05-26 19:04:18 +000085#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000086 printf("Opened file num %d\n", fd);
robbiew7a171362005-05-26 19:04:18 +000087#endif
subrata_modak56207ce2009-03-23 13:35:39 +000088 if (fd == -1)
89 break;
90 else
91 count = fd;
92 }
robbiew7a171362005-05-26 19:04:18 +000093
robbiew61fee652005-07-20 14:03:38 +000094//Now the max files opened should be RLIMIT_NOFILE - 1 , why ? read getdtablesize man page
robbiew7a171362005-05-26 19:04:18 +000095
subrata_modak56207ce2009-03-23 13:35:39 +000096 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));
robbiewe93a3242005-08-31 20:27:12 +0000102
Cyril Hrubis9e4b1172014-09-23 13:10:52 +0200103 cleanup();
Cyril Hrubis4dff8542014-09-23 12:11:59 +0200104 tst_exit();
robbiew7a171362005-05-26 19:04:18 +0000105}
subrata_modak56207ce2009-03-23 13:35:39 +0000106
Mike Frysingerc57fba52014-04-09 18:56:30 -0400107void setup(void)
robbiew7a171362005-05-26 19:04:18 +0000108{
subrata_modak56207ce2009-03-23 13:35:39 +0000109 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew7a171362005-05-26 19:04:18 +0000110
subrata_modak56207ce2009-03-23 13:35:39 +0000111 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800112}
robbiew7a171362005-05-26 19:04:18 +0000113
Mike Frysingerc57fba52014-04-09 18:56:30 -0400114void cleanup(void)
robbiew7a171362005-05-26 19:04:18 +0000115{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700116}