Home | History | Annotate | Download | only in semget
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * NAME
     22  *	semget06.c
     23  *
     24  * DESCRIPTION
     25  *	semget06 - test for EINVAL error
     26  *
     27  * ALGORITHM
     28  *	loop if that option was specified
     29  *	call semget() using two different invalid cases - too many and too
     30  *	   few primitive semaphores
     31  *	check the errno value
     32  *	  issue a PASS message if we get EINVAL
     33  *	otherwise, the tests fails
     34  *	  issue a FAIL message
     35  *	call cleanup
     36  *
     37  * USAGE:  <for command-line>
     38  *  semget06 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
     39  *     where,  -c n : Run n copies concurrently.
     40  *             -e   : Turn on errno logging.
     41  *	       -i n : Execute test n times.
     42  *	       -I x : Execute test for x seconds.
     43  *	       -P x : Pause for x seconds between iterations.
     44  *	       -t   : Turn on syscall timing.
     45  *
     46  * HISTORY
     47  *	03/2001 - Written by Wayne Boyer
     48  *
     49  * RESTRICTIONS
     50  *	none
     51  */
     52 
     53 #include "../lib/ipcsem.h"
     54 
     55 char *TCID = "semget06";
     56 int TST_TOTAL = 2;
     57 
     58 #define LARGENUM	1024 * 32
     59 #define SMALLNUM	-1
     60 
     61 int sem_id_1 = -1;
     62 
     63 int num_sems[] = { LARGENUM, SMALLNUM };
     64 
     65 int main(int ac, char **av)
     66 {
     67 	int lc;
     68 	int i;
     69 
     70 	tst_parse_opts(ac, av, NULL, NULL);
     71 
     72 	setup();		/* global setup */
     73 
     74 	/* The following loop checks looping state if -i option given */
     75 
     76 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     77 		/* reset tst_count in case we are looping */
     78 		tst_count = 0;
     79 
     80 		/* loop through the test cases */
     81 
     82 		for (i = 0; i < TST_TOTAL; i++) {
     83 			TEST(semget(semkey, num_sems[i],
     84 				    IPC_CREAT | IPC_EXCL | SEM_RA));
     85 
     86 			if (TEST_RETURN != -1) {
     87 				sem_id_1 = TEST_RETURN;
     88 				tst_resm(TFAIL, "call succeeded");
     89 				continue;
     90 			}
     91 
     92 			switch (TEST_ERRNO) {
     93 			case EINVAL:
     94 				tst_resm(TPASS, "expected failure - errno "
     95 					 "= %d : %s", TEST_ERRNO,
     96 					 strerror(TEST_ERRNO));
     97 				break;
     98 			default:
     99 				tst_resm(TFAIL, "unexpected error - %d : %s",
    100 					 TEST_ERRNO, strerror(TEST_ERRNO));
    101 				break;
    102 			}
    103 		}
    104 	}
    105 
    106 	cleanup();
    107 
    108 	tst_exit();
    109 }
    110 
    111 /*
    112  * setup() - performs all the ONE TIME setup for this test.
    113  */
    114 void setup(void)
    115 {
    116 
    117 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    118 
    119 	TEST_PAUSE;
    120 
    121 	/*
    122 	 * Create a temporary directory and cd into it.
    123 	 * This helps to ensure that a unique msgkey is created.
    124 	 * See ../lib/libipc.c for more information.
    125 	 */
    126 	tst_tmpdir();
    127 
    128 	/* get an IPC resource key */
    129 	semkey = getipckey();
    130 }
    131 
    132 /*
    133  * cleanup() - performs all the ONE TIME cleanup for this test at completion
    134  * 	       or premature exit.
    135  */
    136 void cleanup(void)
    137 {
    138 	/* if it exists, remove the semaphore resource */
    139 	rm_sema(sem_id_1);
    140 
    141 	tst_rmdir();
    142 
    143 }
    144