semctl02.c revision 43337a3cf6f8809647cf9fc6c0054241f44b1fb1
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20/* 21 * NAME 22 * semctl02.c 23 * 24 * DESCRIPTION 25 * semctl02 - test for EACCES error 26 * 27 * ALGORITHM 28 * create a semaphore set without read/alter permissions 29 * loop if that option was specified 30 * call semctl() attempting an IPC_STAT command 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 * semctl02 [-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#include <pwd.h> 55 56char *TCID = "semctl02"; 57int TST_TOTAL = 1; 58extern int Tst_count; 59 60int exp_enos[] = {EACCES, 0}; /* 0 terminated list of expected errnos */ 61 62char nobody_uid[] = "nobody"; 63struct passwd *ltpuser; 64 65int sem_id_1 = -1; 66 67int main(int ac, char **av) 68{ 69 int lc; /* loop counter */ 70 char *msg; /* message returned from parse_opts */ 71 72 struct semid_ds sem_ds; 73 union semun un_arg; 74 75 /* parse standard options */ 76 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ 77 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); 78 } 79 80 setup(); /* global setup */ 81 82 /* The following loop checks looping state if -i option given */ 83 84 for (lc = 0; TEST_LOOPING(lc); lc++) { 85 /* reset Tst_count in case we are looping */ 86 Tst_count = 0; 87 88 un_arg.buf = &sem_ds; 89 90 /* 91 * use the TEST macro to make the call 92 */ 93 94 TEST(semctl(sem_id_1, 0, IPC_STAT, un_arg)); 95 96 if (TEST_RETURN != -1) { 97 tst_resm(TFAIL, "call succeeded when error expected"); 98 continue; 99 } 100 101 TEST_ERROR_LOG(TEST_ERRNO); 102 103 switch(TEST_ERRNO) { 104 case EACCES: 105 tst_resm(TPASS, "expected failure - errno = %d : %s", 106 TEST_ERRNO, strerror(TEST_ERRNO)); 107 break; 108 default: 109 tst_resm(TFAIL, "unexpected error - %d : %s", 110 TEST_ERRNO, strerror(TEST_ERRNO)); 111 break; 112 } 113 } 114 115 cleanup(); 116 117 /*NOTREACHED*/ 118 return 0; 119} 120 121/* 122 * setup() - performs all the ONE TIME setup for this test. 123 */ 124void 125setup(void) 126{ 127 /* capture signals */ 128 tst_sig(NOFORK, DEF_HANDLER, cleanup); 129 130 /* Set up the expected error numbers for -e option */ 131 TEST_EXP_ENOS(exp_enos); 132 133 /* Pause if that option was specified */ 134 TEST_PAUSE; 135 136 /* Switch to nobody user for correct error code collection */ 137 if (geteuid() != 0) { 138 tst_brkm(TBROK, tst_exit, "Test must be run as root"); 139 } 140 ltpuser = getpwnam(nobody_uid); 141 if (setuid(ltpuser->pw_uid) == -1) { 142 tst_resm(TINFO, "setuid failed to " 143 "to set the effective uid to %d", 144 ltpuser->pw_uid); 145 perror("setuid"); 146 } 147 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 semkey = getipckey(); 158 159 /* create a semaphore set without read or alter permissions */ 160 if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL)) == -1) { 161 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup"); 162 } 163} 164 165/* 166 * cleanup() - performs all the ONE TIME cleanup for this test at completion 167 * or premature exit. 168 */ 169void 170cleanup(void) 171{ 172 /* if it exists, remove the semaphore resouce */ 173 rm_sema(sem_id_1); 174 175 /* Remove the temporary directory */ 176 tst_rmdir(); 177 178 /* 179 * print timing stats if that option was specified. 180 * print errno log if that option was specified. 181 */ 182 TEST_CLEANUP; 183 184 /* exit with return code appropriate for results */ 185 tst_exit(); 186} 187 188