blob: f51099b07fbca3133a1317f5dde221a1c570d3da [file] [log] [blame]
robbiew230a0a82002-12-05 20:28:33 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew230a0a82002-12-05 20:28:33 +000018 */
19
20/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
21/* 10/30/2002 Port to LTP dbarrera@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 *
subrata_modak68fbd252008-03-14 19:10:44 +000035 * HISTORY
36 * 10/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
37 * - Fix concurrency issue. A statically defined key was used. Leading
38 * to conflict with other instances of the same test.
robbiew230a0a82002-12-05 20:28:33 +000039 */
40
Cyril Hrubis9e4b1172014-09-23 13:10:52 +020041#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>
robbiew230a0a82002-12-05 20:28:33 +000048#include "ipcsem.h"
49#include "test.h"
robbiew230a0a82002-12-05 20:28:33 +000050
Mike Frysingerc57fba52014-04-09 18:56:30 -040051void setup(void);
52void cleanup(void);
robbiew230a0a82002-12-05 20:28:33 +000053
Cyril Hrubis9e4b1172014-09-23 13:10:52 +020054char *TCID = "semctl07";
55int TST_TOTAL = 1;
robbiew230a0a82002-12-05 20:28:33 +000056
subrata_modak68fbd252008-03-14 19:10:44 +000057key_t key;
58int semid = -1, nsems;
robbiew230a0a82002-12-05 20:28:33 +000059
Mike Frysingerc57fba52014-04-09 18:56:30 -040060int main(int argc, char *argv[])
robbiew230a0a82002-12-05 20:28:33 +000061{
subrata_modak68fbd252008-03-14 19:10:44 +000062 int status;
robbiew230a0a82002-12-05 20:28:33 +000063 struct semid_ds buf_ds;
64
65 union semun {
66 int val;
67 struct semid_ds *buf;
68 short *array;
69 };
70
71 union semun arg;
72
Cyril Hrubis9e4b1172014-09-23 13:10:52 +020073 setup();
subrata_modak68fbd252008-03-14 19:10:44 +000074
robbiew230a0a82002-12-05 20:28:33 +000075 arg.buf = &buf_ds;
76 if ((status = semctl(semid, 0, IPC_STAT, arg)) == -1) {
vapiercff4af02006-02-11 04:46:30 +000077 tst_resm(TFAIL, "semctl() failed errno = %d", errno);
robbiew230a0a82002-12-05 20:28:33 +000078 semctl(semid, 1, IPC_RMID, arg);
Garrett Cooper2c282152010-12-16 00:55:50 -080079
robbiew230a0a82002-12-05 20:28:33 +000080 }
81
82 /*
83 * Check contents of semid_ds structure.
84 */
85
86 if (arg.buf->sem_nsems != nsems) {
subrata_modak923b23f2009-11-02 13:57:16 +000087 tst_resm(TFAIL, "error: unexpected number of sems %lu",
subrata_modak56207ce2009-03-23 13:35:39 +000088 arg.buf->sem_nsems);
Garrett Cooper2c282152010-12-16 00:55:50 -080089
robbiew230a0a82002-12-05 20:28:33 +000090 }
91 if (arg.buf->sem_perm.uid != getuid()) {
subrata_modak56207ce2009-03-23 13:35:39 +000092 tst_resm(TFAIL, "error: unexpected uid %d",
93 arg.buf->sem_perm.uid);
Garrett Cooper2c282152010-12-16 00:55:50 -080094
robbiew230a0a82002-12-05 20:28:33 +000095 }
96 if (arg.buf->sem_perm.gid != getgid()) {
subrata_modak56207ce2009-03-23 13:35:39 +000097 tst_resm(TFAIL, "error: unexpected gid %d",
98 arg.buf->sem_perm.gid);
Garrett Cooper2c282152010-12-16 00:55:50 -080099
robbiew230a0a82002-12-05 20:28:33 +0000100 }
101 if (arg.buf->sem_perm.cuid != getuid()) {
subrata_modak56207ce2009-03-23 13:35:39 +0000102 tst_resm(TFAIL, "error: unexpected cuid %d",
103 arg.buf->sem_perm.cuid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800104
robbiew230a0a82002-12-05 20:28:33 +0000105 }
106 if (arg.buf->sem_perm.cgid != getgid()) {
subrata_modak56207ce2009-03-23 13:35:39 +0000107 tst_resm(TFAIL, "error: unexpected cgid %d",
108 arg.buf->sem_perm.cgid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800109
robbiew230a0a82002-12-05 20:28:33 +0000110 }
111 if ((status = semctl(semid, 0, GETVAL, arg)) == -1) {
vapiercff4af02006-02-11 04:46:30 +0000112 tst_resm(TFAIL, "semctl(GETVAL) failed errno = %d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800113
robbiew230a0a82002-12-05 20:28:33 +0000114 }
115 arg.val = 1;
116 if ((status = semctl(semid, 0, SETVAL, arg)) == -1) {
vapiercff4af02006-02-11 04:46:30 +0000117 tst_resm(TFAIL, "SEMCTL(SETVAL) failed errno = %d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800118
robbiew230a0a82002-12-05 20:28:33 +0000119 }
120 if ((status = semctl(semid, 0, GETVAL, arg)) == -1) {
vapiercff4af02006-02-11 04:46:30 +0000121 tst_resm(TFAIL, "semctl(GETVAL) failed errno = %d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
robbiew230a0a82002-12-05 20:28:33 +0000123 }
124 if (status != arg.val) {
vapiercff4af02006-02-11 04:46:30 +0000125 tst_resm(TFAIL, "error: unexpected value %d", status);
Garrett Cooper2c282152010-12-16 00:55:50 -0800126
robbiew230a0a82002-12-05 20:28:33 +0000127 }
128 if ((status = semctl(semid, 0, GETPID, arg)) == -1) {
vapiercff4af02006-02-11 04:46:30 +0000129 tst_resm(TFAIL, "semctl(GETPID) failed errno = %d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800130
robbiew230a0a82002-12-05 20:28:33 +0000131 }
132 status = getpid();
subrata_modak56207ce2009-03-23 13:35:39 +0000133 if (status == 0) {
vapiercff4af02006-02-11 04:46:30 +0000134 tst_resm(TFAIL, "error: unexpected pid %d", status);
Garrett Cooper2c282152010-12-16 00:55:50 -0800135
robbiew230a0a82002-12-05 20:28:33 +0000136 }
137 if ((status = semctl(semid, 0, GETNCNT, arg)) == -1) {
vapiercff4af02006-02-11 04:46:30 +0000138 tst_resm(TFAIL, "semctl(GETNCNT) failed errno = %d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800139
robbiew230a0a82002-12-05 20:28:33 +0000140 }
141 if (status != 0) {
vapiercff4af02006-02-11 04:46:30 +0000142 tst_resm(TFAIL, "error: unexpected semncnt %d", status);
Garrett Cooper2c282152010-12-16 00:55:50 -0800143
robbiew230a0a82002-12-05 20:28:33 +0000144 }
145 if ((status = semctl(semid, 0, GETZCNT, arg)) == -1) {
vapiercff4af02006-02-11 04:46:30 +0000146 tst_resm(TFAIL, "semctl(GETZCNT) failed errno = %d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800147
robbiew230a0a82002-12-05 20:28:33 +0000148 }
149 if (status != 0) {
vapiercff4af02006-02-11 04:46:30 +0000150 tst_resm(TFAIL, "error: unexpected semzcnt %d", status);
Garrett Cooper2c282152010-12-16 00:55:50 -0800151
robbiew230a0a82002-12-05 20:28:33 +0000152 }
robbiew230a0a82002-12-05 20:28:33 +0000153
154 tst_resm(TPASS, "semctl07 ran successfully!");
robbiew230a0a82002-12-05 20:28:33 +0000155
156 cleanup();
Cyril Hrubis4dff8542014-09-23 12:11:59 +0200157 tst_exit();
robbiew230a0a82002-12-05 20:28:33 +0000158}
subrata_modak56207ce2009-03-23 13:35:39 +0000159
Mike Frysingerc57fba52014-04-09 18:56:30 -0400160void setup(void)
robbiew230a0a82002-12-05 20:28:33 +0000161{
subrata_modak56207ce2009-03-23 13:35:39 +0000162 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew230a0a82002-12-05 20:28:33 +0000163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 TEST_PAUSE;
subrata_modak68fbd252008-03-14 19:10:44 +0000165
subrata_modak68fbd252008-03-14 19:10:44 +0000166 tst_tmpdir();
167
168 /* get an IPC resource key */
169 key = getipckey();
170 nsems = 1;
171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 if ((semid = semget(key, nsems, SEM_RA | IPC_CREAT)) == -1) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100173 tst_brkm(TFAIL, NULL, "semget() failed errno = %d", errno);
subrata_modak68fbd252008-03-14 19:10:44 +0000174 }
robbiew230a0a82002-12-05 20:28:33 +0000175}
176
Mike Frysingerc57fba52014-04-09 18:56:30 -0400177void cleanup(void)
robbiew230a0a82002-12-05 20:28:33 +0000178{
subrata_modak68fbd252008-03-14 19:10:44 +0000179 rm_sema(semid);
subrata_modak68fbd252008-03-14 19:10:44 +0000180 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700181}