Home | History | Annotate | Download | only in semctl
      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  *	semctl05.c
     23  *
     24  * DESCRIPTION
     25  *	semctl05 - test for ERANGE error
     26  *
     27  * ALGORITHM
     28  *	create a semaphore set with read and alter permissions
     29  *	loop if that option was specified
     30  *	call semctl() with three different invalid cases
     31  *	check the errno value
     32  *	  issue a PASS message if we get ERANGE
     33  *	otherwise, the tests fails
     34  *	  issue a FAIL message
     35  *	call cleanup
     36  *
     37  * USAGE:  <for command-line>
     38  *  semctl05 [-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 "ipcsem.h"
     54 
     55 char *TCID = "semctl05";
     56 int TST_TOTAL = 3;
     57 
     58 #ifdef _XLC_COMPILER
     59 #define SEMUN_CAST
     60 #else
     61 #define SEMUN_CAST (union semun)
     62 #endif
     63 
     64 int sem_id_1 = -1;
     65 
     66 #define BIGV	65535		/* a number ((2^16)-1) that should be larger */
     67 				/* than the maximum for a semaphore value    */
     68 
     69 #ifdef _XLC_COMPILER
     70 #define SEMUN_CAST
     71 #else
     72 #define SEMUN_CAST (union semun)
     73 #endif
     74 
     75 unsigned short big_arr[] = { BIGV, BIGV, BIGV, BIGV, BIGV, BIGV, BIGV, BIGV,
     76 	BIGV, BIGV
     77 };
     78 
     79 struct test_case_t {
     80 	int count;
     81 	int cmd;
     82 	union semun t_arg;
     83 } TC[] = {
     84 	/* ERANGE - the value to set is less than zero - SETVAL */
     85 	{
     86 	5, SETVAL, SEMUN_CAST - 1},
     87 	    /* ERANGE - the values to set are too large, > semaphore max value */
     88 	{
     89 	0, SETALL, SEMUN_CAST big_arr},
     90 	    /* ERANGE - the value to set is too large, > semaphore max value */
     91 	{
     92 	5, SETVAL, SEMUN_CAST BIGV}
     93 };
     94 
     95 int main(int ac, char **av)
     96 {
     97 	int lc;
     98 	int i;
     99 
    100 	tst_parse_opts(ac, av, NULL, NULL);
    101 
    102 	setup();		/* global setup */
    103 
    104 	/* The following loop checks looping state if -i option given */
    105 
    106 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    107 		/* reset tst_count in case we are looping */
    108 		tst_count = 0;
    109 
    110 		for (i = 0; i < TST_TOTAL; i++) {
    111 
    112 			TEST(semctl(sem_id_1, TC[i].count,
    113 				    TC[i].cmd, TC[i].t_arg));
    114 
    115 			if (TEST_RETURN != -1) {
    116 				tst_resm(TFAIL, "call succeeded unexpectedly");
    117 				continue;
    118 			}
    119 
    120 			switch (TEST_ERRNO) {
    121 			case ERANGE:
    122 				tst_resm(TPASS, "expected failure - errno = "
    123 					 "%d : %s", TEST_ERRNO,
    124 					 strerror(TEST_ERRNO));
    125 				break;
    126 			default:
    127 				tst_resm(TFAIL, "unexpected error "
    128 					 "- %d : %s", TEST_ERRNO,
    129 					 strerror(TEST_ERRNO));
    130 				break;
    131 			}
    132 		}
    133 	}
    134 
    135 	cleanup();
    136 
    137 	tst_exit();
    138 }
    139 
    140 /*
    141  * setup() - performs all the ONE TIME setup for this test.
    142  */
    143 void setup(void)
    144 {
    145 
    146 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    147 
    148 	TEST_PAUSE;
    149 
    150 	/*
    151 	 * Create a temporary directory and cd into it.
    152 	 * This helps to ensure that a unique msgkey is created.
    153 	 * See ../lib/libipc.c for more information.
    154 	 */
    155 	tst_tmpdir();
    156 
    157 	/* get an IPC resource key */
    158 	semkey = getipckey();
    159 
    160 	/* create a semaphore set with read and alter permissions */
    161 	if ((sem_id_1 =
    162 	     semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
    163 		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
    164 	}
    165 }
    166 
    167 /*
    168  * cleanup() - performs all the ONE TIME cleanup for this test at completion
    169  * 	       or premature exit.
    170  */
    171 void cleanup(void)
    172 {
    173 	/* if it exists, remove the semaphore resouce */
    174 	rm_sema(sem_id_1);
    175 
    176 	tst_rmdir();
    177 
    178 }
    179