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