1 /* 2 * 3 * Copyright (c) International Business Machines Corp., 2002 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 /* 06/30/2001 Port to Linux nsharoff (at) us.ibm.com */ 21 /* 10/30/2002 Port to LTP dbarrera (at) us.ibm.com */ 22 23 /* 24 * NAME 25 * semctl07 26 * 27 * CALLS 28 * semctl(2) semget(2) 29 * 30 * ALGORITHM 31 * Get and manipulate a set of semaphores. 32 * 33 * RESTRICTIONS 34 * 35 * HISTORY 36 * 10/03/2008 Renaud Lottiaux (Renaud.Lottiaux (at) kerlabs.com) 37 * - Fix concurrency issue. A statically defined key was used. Leading 38 * to conflict with other instances of the same test. 39 */ 40 41 #include <sys/types.h> 42 #include <sys/ipc.h> 43 #include <sys/sem.h> 44 #include <signal.h> 45 #include <errno.h> 46 #include <stdio.h> 47 #include <wait.h> 48 #include "ipcsem.h" 49 #include "test.h" 50 51 void setup(void); 52 void cleanup(void); 53 54 char *TCID = "semctl07"; 55 int TST_TOTAL = 1; 56 57 key_t key; 58 int semid = -1, nsems; 59 60 int main(int argc, char *argv[]) 61 { 62 int status; 63 struct semid_ds buf_ds; 64 65 union semun arg; 66 67 setup(); 68 69 arg.buf = &buf_ds; 70 if ((status = semctl(semid, 0, IPC_STAT, arg)) == -1) { 71 tst_resm(TFAIL, "semctl() failed errno = %d", errno); 72 semctl(semid, 1, IPC_RMID, arg); 73 74 } 75 76 /* 77 * Check contents of semid_ds structure. 78 */ 79 80 if (arg.buf->sem_nsems != nsems) { 81 tst_resm(TFAIL, "error: unexpected number of sems %lu", 82 arg.buf->sem_nsems); 83 84 } 85 if (arg.buf->sem_perm.uid != getuid()) { 86 tst_resm(TFAIL, "error: unexpected uid %d", 87 arg.buf->sem_perm.uid); 88 89 } 90 if (arg.buf->sem_perm.gid != getgid()) { 91 tst_resm(TFAIL, "error: unexpected gid %d", 92 arg.buf->sem_perm.gid); 93 94 } 95 if (arg.buf->sem_perm.cuid != getuid()) { 96 tst_resm(TFAIL, "error: unexpected cuid %d", 97 arg.buf->sem_perm.cuid); 98 99 } 100 if (arg.buf->sem_perm.cgid != getgid()) { 101 tst_resm(TFAIL, "error: unexpected cgid %d", 102 arg.buf->sem_perm.cgid); 103 104 } 105 if ((status = semctl(semid, 0, GETVAL, arg)) == -1) { 106 tst_resm(TFAIL, "semctl(GETVAL) failed errno = %d", errno); 107 108 } 109 arg.val = 1; 110 if ((status = semctl(semid, 0, SETVAL, arg)) == -1) { 111 tst_resm(TFAIL, "SEMCTL(SETVAL) failed errno = %d", errno); 112 113 } 114 if ((status = semctl(semid, 0, GETVAL, arg)) == -1) { 115 tst_resm(TFAIL, "semctl(GETVAL) failed errno = %d", errno); 116 117 } 118 if (status != arg.val) { 119 tst_resm(TFAIL, "error: unexpected value %d", status); 120 121 } 122 if ((status = semctl(semid, 0, GETPID, arg)) == -1) { 123 tst_resm(TFAIL, "semctl(GETPID) failed errno = %d", errno); 124 125 } 126 status = getpid(); 127 if (status == 0) { 128 tst_resm(TFAIL, "error: unexpected pid %d", status); 129 130 } 131 if ((status = semctl(semid, 0, GETNCNT, arg)) == -1) { 132 tst_resm(TFAIL, "semctl(GETNCNT) failed errno = %d", errno); 133 134 } 135 if (status != 0) { 136 tst_resm(TFAIL, "error: unexpected semncnt %d", status); 137 138 } 139 if ((status = semctl(semid, 0, GETZCNT, arg)) == -1) { 140 tst_resm(TFAIL, "semctl(GETZCNT) failed errno = %d", errno); 141 142 } 143 if (status != 0) { 144 tst_resm(TFAIL, "error: unexpected semzcnt %d", status); 145 146 } 147 148 tst_resm(TPASS, "semctl07 ran successfully!"); 149 150 cleanup(); 151 tst_exit(); 152 } 153 154 void setup(void) 155 { 156 tst_sig(NOFORK, DEF_HANDLER, cleanup); 157 158 TEST_PAUSE; 159 160 tst_tmpdir(); 161 162 /* get an IPC resource key */ 163 key = getipckey(); 164 nsems = 1; 165 166 if ((semid = semget(key, nsems, SEM_RA | IPC_CREAT)) == -1) { 167 tst_brkm(TFAIL, NULL, "semget() failed errno = %d", errno); 168 } 169 } 170 171 void cleanup(void) 172 { 173 rm_sema(semid); 174 tst_rmdir(); 175 } 176