robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 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@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> |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 40 | #include <errno.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <fcntl.h> |
robbiew | 804899e | 2005-07-19 18:36:53 +0000 | [diff] [blame] | 44 | #include <sys/time.h> |
| 45 | #include <sys/resource.h> |
| 46 | #include <unistd.h> |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 47 | #include "test.h" |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 48 | |
vapier | aa35472 | 2006-05-26 06:26:37 +0000 | [diff] [blame] | 49 | void setup(); |
| 50 | void cleanup(); |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 51 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 52 | char *TCID = "getdtablesize01"; |
| 53 | int TST_TOTAL = 1; |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 54 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 55 | int main(void) |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 56 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | int table_size, loop, fd, count = 0; |
robbiew | 804899e | 2005-07-19 18:36:53 +0000 | [diff] [blame] | 58 | int max_val_opfiles; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | struct rlimit rlp; |
robbiew | 804899e | 2005-07-19 18:36:53 +0000 | [diff] [blame] | 60 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | setup(); |
| 62 | table_size = getdtablesize(); |
| 63 | getrlimit(RLIMIT_NOFILE, &rlp); |
| 64 | max_val_opfiles = (rlim_t) rlp.rlim_cur; |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 65 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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"); |
robbiew | 804899e | 2005-07-19 18:36:53 +0000 | [diff] [blame] | 71 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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 | } |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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); |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 85 | #ifdef DEBUG |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | printf("Opened file num %d\n", fd); |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 87 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | if (fd == -1) |
| 89 | break; |
| 90 | else |
| 91 | count = fd; |
| 92 | } |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 93 | |
robbiew | 61fee65 | 2005-07-20 14:03:38 +0000 | [diff] [blame] | 94 | //Now the max files opened should be RLIMIT_NOFILE - 1 , why ? read getdtablesize man page |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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)); |
robbiew | e93a324 | 2005-08-31 20:27:12 +0000 | [diff] [blame] | 102 | |
Cyril Hrubis | 9e4b117 | 2014-09-23 13:10:52 +0200 | [diff] [blame] | 103 | cleanup(); |
Cyril Hrubis | 4dff854 | 2014-09-23 12:11:59 +0200 | [diff] [blame] | 104 | tst_exit(); |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 105 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 107 | void setup(void) |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 108 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 110 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | } |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 113 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 114 | void cleanup(void) |
robbiew | 7a17136 | 2005-05-26 19:04:18 +0000 | [diff] [blame] | 115 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 116 | } |