3 * Copyright (c) International Business Machines Corp., 2002
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.
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.
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
20 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
21 /* 10/30/2002 Port to LTP dbarrera@us.ibm.com */
31 * Get and manipulate a set of semaphores.
38 #include <sys/types.h> /* needed for test */
39 #include <sys/ipc.h> /* needed for test */
40 #include <sys/sem.h> /* needed for test */
41 #include <signal.h> /* needed for test */
42 #include <errno.h> /* needed for test */
43 #include <stdio.h> /* needed by testhead.h */
44 #include <sys/wait.h> /* needed by testhead.h */
55 *These globals must be defined in the test.
59 char *TCID
="semctl07"; /* Test program identifier. */
60 int TST_TOTAL
=1; /* Total number of test cases. */
61 extern int Tst_count
; /* Test Case counter for tst_* routines */
63 int exp_enos
[]={0}; /* List must end with 0 */
67 /*--------------------------------------------------------------*/
69 int main(int argc
, char *argv
[])
72 int semid
, nsems
, status
;
73 struct semid_ds buf_ds
;
83 setup(); /* temp file is now open */
84 /*--------------------------------------------------------------*/
86 if ((semid
= semget(key
, nsems
, SEM_RA
|IPC_CREAT
)) == -1) {
87 tst_resm(TFAIL
, "semget() failed errno = %d", errno
);
91 if ((status
= semctl(semid
, 0, IPC_STAT
, arg
)) == -1) {
92 tst_resm(TFAIL
, "semctl() failed errno = %d", errno
);
93 semctl(semid
, 1, IPC_RMID
, arg
);
98 * Check contents of semid_ds structure.
101 if (arg
.buf
->sem_nsems
!= nsems
) {
102 tst_resm(TFAIL
, "error: unexpected number of sems %d", arg
.buf
->sem_nsems
);
105 if (arg
.buf
->sem_perm
.uid
!= getuid()) {
106 tst_resm(TFAIL
, "error: unexpected uid %d", arg
.buf
->sem_perm
.uid
);
109 if (arg
.buf
->sem_perm
.gid
!= getgid()) {
110 tst_resm(TFAIL
, "error: unexpected gid %d", arg
.buf
->sem_perm
.gid
);
113 if (arg
.buf
->sem_perm
.cuid
!= getuid()) {
114 tst_resm(TFAIL
, "error: unexpected cuid %d", arg
.buf
->sem_perm
.cuid
);
117 if (arg
.buf
->sem_perm
.cgid
!= getgid()) {
118 tst_resm(TFAIL
, "error: unexpected cgid %d", arg
.buf
->sem_perm
.cgid
);
121 if ((status
= semctl(semid
, 0, GETVAL
, arg
)) == -1) {
122 tst_resm(TFAIL
, "semctl(GETVAL) failed errno = %d", errno
);
126 if ((status
= semctl(semid
, 0, SETVAL
, arg
)) == -1) {
127 tst_resm(TFAIL
, "SEMCTL(SETVAL) failed errno = %d", errno
);
130 if ((status
= semctl(semid
, 0, GETVAL
, arg
)) == -1) {
131 tst_resm(TFAIL
, "semctl(GETVAL) failed errno = %d", errno
);
134 if (status
!= arg
.val
) {
135 tst_resm(TFAIL
, "error: unexpected value %d", status
);
138 if ((status
= semctl(semid
, 0, GETPID
, arg
)) == -1) {
139 tst_resm(TFAIL
, "semctl(GETPID) failed errno = %d", errno
);
145 tst_resm(TFAIL
, "error: unexpected pid %d", status
);
148 if ((status
= semctl(semid
, 0, GETNCNT
, arg
)) == -1) {
149 tst_resm(TFAIL
, "semctl(GETNCNT) failed errno = %d", errno
);
153 tst_resm(TFAIL
, "error: unexpected semncnt %d", status
);
156 if ((status
= semctl(semid
, 0, GETZCNT
, arg
)) == -1) {
157 tst_resm(TFAIL
, "semctl(GETZCNT) failed errno = %d", errno
);
161 tst_resm(TFAIL
, "error: unexpected semzcnt %d", status
);
165 semctl(semid
, 0, IPC_RMID
, arg
);
166 if ((status
= semctl(semid
, 0, GETPID
, arg
)) != -1) {
167 tst_resm(TFAIL
, "semctl(IPC_RMID) failed");
171 tst_resm(TPASS
, "semctl07 ran successfully!");
172 /*--------------------------------------------------------------*/
173 /* Clean up any files created by test before exit. */
174 /*--------------------------------------------------------------*/
179 /*--------------------------------------------------------------*/
181 /***************************************************************
182 * setup() - performs all ONE TIME setup for this test.
183 *****************************************************************/
187 /* You will want to enable some signal handling so you can capture
188 * unexpected signals like SIGSEGV.
190 tst_sig(NOFORK
, DEF_HANDLER
, cleanup
);
193 /* Pause if that option was specified */
194 /* One cavet that hasn't been fixed yet. TEST_PAUSE contains the code to
195 * fork the test with the -c option. You want to make sure you do this
196 * before you create your temporary directory.
202 /***************************************************************
203 * cleanup() - performs all ONE TIME cleanup for this test at
204 * completion or premature exit.
205 ****************************************************************/
210 * print timing stats if that option was specified.
211 * print errno log if that option was specified.
215 /* exit with return code appropriate for results */