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  *	shmget04.c
     23  *
     24  * DESCRIPTION
     25  *	shmget04 - test for EACCES error
     26  *
     27  * ALGORITHM
     28  *	create a shared memory segment without read or write permissions
     29  *	loop if that option was specified
     30  *	  call shmget() with SHM_RW flag using TEST() macro
     31  *	  check the errno value
     32  *	    issue a PASS message if we get EACCES
     33  *	  otherwise, the tests fails
     34  *	    issue a FAIL message
     35  *	call cleanup
     36  *
     37  * USAGE:  <for command-line>
     38  *  shmget04 [-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 #include <pwd.h>
     53 #include "ipcshm.h"
     54 
     55 char *TCID = "shmget04";
     56 int TST_TOTAL = 1;
     57 
     58 char nobody_uid[] = "nobody";
     59 struct passwd *ltpuser;
     60 
     61 int shm_id_1 = -1;
     62 
     63 int main(int ac, char **av)
     64 {
     65 	int lc;
     66 
     67 	tst_parse_opts(ac, av, NULL, NULL);
     68 
     69 	setup();		/* global setup */
     70 
     71 	/* The following loop checks looping state if -i option given */
     72 
     73 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     74 		/* reset tst_count in case we are looping */
     75 		tst_count = 0;
     76 
     77 		/*
     78 		 * use the TEST() macro to make the call
     79 		 */
     80 
     81 		TEST(shmget(shmkey, SHM_SIZE, SHM_RW));
     82 
     83 		if (TEST_RETURN != -1) {
     84 			tst_resm(TFAIL, "call succeeded when error expected");
     85 			continue;
     86 		}
     87 
     88 		switch (TEST_ERRNO) {
     89 		case EACCES:
     90 			tst_resm(TPASS, "expected failure - errno = "
     91 				 "%d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
     92 			break;
     93 		default:
     94 			tst_resm(TFAIL, "call failed with an "
     95 				 "unexpected error - %d : %s",
     96 				 TEST_ERRNO, strerror(TEST_ERRNO));
     97 			break;
     98 		}
     99 	}
    100 
    101 	cleanup();
    102 
    103 	tst_exit();
    104 }
    105 
    106 /*
    107  * setup() - performs all the ONE TIME setup for this test.
    108  */
    109 void setup(void)
    110 {
    111 	tst_require_root();
    112 
    113 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    114 
    115 	TEST_PAUSE;
    116 
    117 	/* Switch to nobody user for correct error code collection */
    118 	ltpuser = getpwnam(nobody_uid);
    119 	if (setuid(ltpuser->pw_uid) == -1) {
    120 		tst_resm(TINFO, "setuid failed to "
    121 			 "to set the effective uid to %d", ltpuser->pw_uid);
    122 		perror("setuid");
    123 	}
    124 
    125 	/*
    126 	 * Create a temporary directory and cd into it.
    127 	 * This helps to ensure that a unique msgkey is created.
    128 	 * See ../lib/libipc.c for more information.
    129 	 */
    130 	tst_tmpdir();
    131 
    132 	/* get an IPC resource key */
    133 	shmkey = getipckey();
    134 
    135 	/* create a shared memory segment without read or access permissions */
    136 	if ((shm_id_1 = shmget(shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL)) == -1) {
    137 		tst_brkm(TBROK, cleanup, "Failed to create shared memory "
    138 			 "segment in setup");
    139 	}
    140 }
    141 
    142 /*
    143  * cleanup() - performs all the ONE TIME cleanup for this test at completion
    144  * 	       or premature exit.
    145  */
    146 void cleanup(void)
    147 {
    148 	/* if it exists, remove the shared memory resource */
    149 	rm_shm(shm_id_1);
    150 
    151 	tst_rmdir();
    152 
    153 }
    154