Home | History | Annotate | Download | only in shmget
      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  *	shmget01.c
     23  *
     24  * DESCRIPTION
     25  *	shmget01 - test that shmget() correctly creates a shared memory segment
     26  *
     27  * ALGORITHM
     28  *	loop if that option was specified
     29  *	use the TEST() macro to call shmget()
     30  *	check the return code
     31  *	  if failure, issue a FAIL message.
     32  *	otherwise,
     33  *	  if doing functionality testing
     34  *		stat the shared memory resource
     35  *		check the size, creator pid and mode
     36  *	  	if correct,
     37  *			issue a PASS message
     38  *		otherwise
     39  *			issue a FAIL message
     40  *	  else issue a PASS message
     41  *	call cleanup
     42  *
     43  * USAGE:  <for command-line>
     44  *  shmget01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
     45  *     where,  -c n : Run n copies concurrently.
     46  *             -f   : Turn off functionality Testing.
     47  *	       -i n : Execute test n times.
     48  *	       -I x : Execute test for x seconds.
     49  *	       -P x : Pause for x seconds between iterations.
     50  *	       -t   : Turn on syscall timing.
     51  *
     52  * HISTORY
     53  *	03/2001 - Written by Wayne Boyer
     54  *
     55  * RESTRICTIONS
     56  *	none
     57  */
     58 
     59 #include "ipcshm.h"
     60 
     61 char *TCID = "shmget01";
     62 int TST_TOTAL = 1;
     63 
     64 int shm_id_1 = -1;
     65 
     66 int main(int ac, char **av)
     67 {
     68 	int lc;
     69 	struct shmid_ds buf;
     70 
     71 	tst_parse_opts(ac, av, NULL, NULL);
     72 
     73 	setup();		/* global setup */
     74 
     75 	/* The following loop checks looping state if -i option given */
     76 
     77 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     78 		/* reset tst_count in case we are looping */
     79 		tst_count = 0;
     80 
     81 		/*
     82 		 * Use TEST macro to make the call
     83 		 */
     84 
     85 		TEST(shmget(shmkey, SHM_SIZE, (IPC_CREAT | IPC_EXCL | SHM_RW)));
     86 
     87 		if (TEST_RETURN == -1) {
     88 			tst_resm(TFAIL, "%s call failed - errno = %d : %s",
     89 				 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
     90 		} else {
     91 			shm_id_1 = TEST_RETURN;
     92 			/* do a STAT and check some info */
     93 			if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
     94 				tst_resm(TBROK, "shmctl failed in "
     95 					 "functional test");
     96 				continue;
     97 			}
     98 			/* check the seqment size */
     99 			if (buf.shm_segsz != SHM_SIZE) {
    100 				tst_resm(TFAIL, "seqment size is not "
    101 					 "correct");
    102 				continue;
    103 			}
    104 			/* check the pid of the creator */
    105 			if (buf.shm_cpid != getpid()) {
    106 				tst_resm(TFAIL, "creator pid is not "
    107 					 "correct");
    108 				continue;
    109 			}
    110 			/*
    111 			 * check the mode of the seqment
    112 			 * mask out all but the lower 9 bits
    113 			 */
    114 			if ((buf.shm_perm.mode & MODE_MASK) !=
    115 			    ((SHM_RW) & MODE_MASK)) {
    116 				tst_resm(TFAIL, "segment mode is not "
    117 					 "correct");
    118 				continue;
    119 			}
    120 			/* if we get here, everything looks good */
    121 			tst_resm(TPASS, "size, pid & mode are correct");
    122 		}
    123 
    124 		/*
    125 		 * clean up things in case we are looping
    126 		 */
    127 		if (shmctl(shm_id_1, IPC_RMID, NULL) == -1) {
    128 			tst_resm(TBROK, "couldn't remove shared memory");
    129 		} else {
    130 			shm_id_1 = -1;
    131 		}
    132 	}
    133 
    134 	cleanup();
    135 
    136 	tst_exit();
    137 }
    138 
    139 /*
    140  * setup() - performs all the ONE TIME setup for this test.
    141  */
    142 void setup(void)
    143 {
    144 
    145 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    146 
    147 	TEST_PAUSE;
    148 
    149 	/*
    150 	 * Create a temporary directory and cd into it.
    151 	 * This helps to ensure that a unique msgkey is created.
    152 	 * See ../lib/libipc.c for more information.
    153 	 */
    154 	tst_tmpdir();
    155 
    156 	/* get an IPC resource key */
    157 	shmkey = getipckey();
    158 }
    159 
    160 /*
    161  * cleanup() - performs all the ONE TIME cleanup for this test at completion
    162  * 	       or premature exit.
    163  */
    164 void cleanup(void)
    165 {
    166 	/* if it exists, remove the shared memory resource */
    167 	rm_shm(shm_id_1);
    168 
    169 	tst_rmdir();
    170 
    171 }
    172