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