blob: 39cf64e118a7bb05ff21082e519c3155a98ea4ab [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
DAN LIbffa8862013-05-13 10:21:43 +08002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
DAN LIbffa8862013-05-13 10:21:43 +08004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +00008 *
DAN LIbffa8862013-05-13 10:21:43 +08009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000013 *
DAN LIbffa8862013-05-13 10:21:43 +080014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
20 * NAME
21 * semctl01.c
22 *
23 * DESCRIPTION
DAN LIbffa8862013-05-13 10:21:43 +080024 * semctl01 - test the 13 possible semctl() commands
plars865695b2001-08-27 22:15:12 +000025 *
26 * ALGORITHM
27 * create a semaphore set with read and alter permissions
28 * loop if that option was specified
29 * loop through the test cases
30 * do any setup required for the test case
31 * make the semctl() call using the TEST() macro
32 * check the return code
33 * if failure, issue a FAIL message.
34 * otherwise,
35 * if doing functionality testing
36 * call the appropriate test function
DAN LIbffa8862013-05-13 10:21:43 +080037 * if correct,
plars865695b2001-08-27 22:15:12 +000038 * issue a PASS message
39 * otherwise
40 * issue a FAIL message
41 * call cleanup
plars865695b2001-08-27 22:15:12 +000042 */
43
DAN LIa63f9332013-05-13 10:24:33 +080044
45#ifndef _GNU_SOURCE
46#define _GNU_SOURCE
47#endif
Jan Stancek7018c132015-11-13 14:05:28 +010048#include <sys/wait.h>
robbiew23499f02002-11-18 19:54:58 +000049#include "ipcsem.h"
plars865695b2001-08-27 22:15:12 +000050
51char *TCID = "semctl01";
plars865695b2001-08-27 22:15:12 +000052
DAN LIbffa8862013-05-13 10:21:43 +080053static int sem_id_1 = -1;
DAN LIf74f0a92013-05-22 09:52:09 +080054static int sem_index;
plars865695b2001-08-27 22:15:12 +000055
56/*
57 * These are the various setup and check functions for the 10 different
58 * commands that are available for the semctl() call.
59 */
DAN LIbffa8862013-05-13 10:21:43 +080060static void func_stat(void);
61static void set_setup(void), func_set(void);
62static void func_gall(void);
63static void cnt_setup(int), func_cnt(int);
64static void pid_setup(void), func_pid(int);
65static void func_gval(int);
66static void sall_setup(void), func_sall(void);
67static void func_sval(void);
68static void func_rmid(void);
69static void child_cnt(void);
70static void child_pid(void);
DAN LIa63f9332013-05-13 10:24:33 +080071static void func_iinfo(int);
72static void func_sinfo(void);
73static void func_sstat(int);
plars865695b2001-08-27 22:15:12 +000074
DAN LIbffa8862013-05-13 10:21:43 +080075static struct semid_ds buf;
DAN LIa63f9332013-05-13 10:24:33 +080076static struct seminfo ipc_buf;
DAN LIbffa8862013-05-13 10:21:43 +080077static unsigned short array[PSEMS];
78static struct sembuf sops;
plars865695b2001-08-27 22:15:12 +000079
DAN LIbffa8862013-05-13 10:21:43 +080080#define INCVAL 2
plars865695b2001-08-27 22:15:12 +000081#define NEWMODE 066
82#define NCHILD 5
DAN LIbffa8862013-05-13 10:21:43 +080083#define SEM2 2
84#define SEM4 4
plars865695b2001-08-27 22:15:12 +000085#define ONE 1
mridgef7298c72005-12-05 19:14:37 +000086#ifdef _XLC_COMPILER
subrata_modak4bb656a2009-02-26 12:02:09 +000087#define SEMUN_CAST
mridgef7298c72005-12-05 19:14:37 +000088#else
89#define SEMUN_CAST (union semun)
90#endif
91
DAN LIbffa8862013-05-13 10:21:43 +080092static int pid_arr[NCHILD];
plars865695b2001-08-27 22:15:12 +000093
robbiewd34d5812005-07-11 22:28:09 +000094#ifdef UCLINUX
subrata_modak4b13c892009-01-22 09:51:33 +000095#define PIPE_NAME "semctl01"
robbiewd34d5812005-07-11 22:28:09 +000096static char *argv0;
DAN LIbffa8862013-05-13 10:21:43 +080097static int sem_op;
robbiewd34d5812005-07-11 22:28:09 +000098#endif
99
DAN LIbffa8862013-05-13 10:21:43 +0800100static struct test_case_t {
DAN LIf74f0a92013-05-22 09:52:09 +0800101 int *semid;
DAN LIbffa8862013-05-13 10:21:43 +0800102 int semnum;
103 int cmd;
104 void (*func_test) ();
plars865695b2001-08-27 22:15:12 +0000105 union semun arg;
DAN LIbffa8862013-05-13 10:21:43 +0800106 void (*func_setup) ();
plars865695b2001-08-27 22:15:12 +0000107} TC[] = {
DAN LIf74f0a92013-05-22 09:52:09 +0800108 {&sem_id_1, 0, IPC_STAT, func_stat, SEMUN_CAST & buf, NULL},
109 {&sem_id_1, 0, IPC_SET, func_set, SEMUN_CAST & buf, set_setup},
110 {&sem_id_1, 0, GETALL, func_gall, SEMUN_CAST array, NULL},
111 {&sem_id_1, SEM4, GETNCNT, func_cnt, SEMUN_CAST & buf, cnt_setup},
112 {&sem_id_1, SEM2, GETPID, func_pid, SEMUN_CAST & buf, pid_setup},
113 {&sem_id_1, SEM2, GETVAL, func_gval, SEMUN_CAST & buf, NULL},
114 {&sem_id_1, SEM4, GETZCNT, func_cnt, SEMUN_CAST & buf, cnt_setup},
115 {&sem_id_1, 0, SETALL, func_sall, SEMUN_CAST array, sall_setup},
116 {&sem_id_1, SEM4, SETVAL, func_sval, SEMUN_CAST INCVAL, NULL},
117 {&sem_id_1, 0, IPC_INFO, func_iinfo, SEMUN_CAST & ipc_buf, NULL},
118 {&sem_id_1, 0, SEM_INFO, func_sinfo, SEMUN_CAST & ipc_buf, NULL},
119 {&sem_index, 0, SEM_STAT, func_sstat, SEMUN_CAST & buf, NULL},
120 {&sem_id_1, 0, IPC_RMID, func_rmid, SEMUN_CAST & buf, NULL},
plars865695b2001-08-27 22:15:12 +0000121};
122
Jan Stancek7018c132015-11-13 14:05:28 +0100123int TST_TOTAL = ARRAY_SIZE(TC);
124
125static void kill_all_children(void)
126{
127 int j, status;
128
129 for (j = 0; j < NCHILD; j++) {
130 if (kill(pid_arr[j], SIGKILL) == -1)
131 tst_brkm(TBROK | TERRNO, cleanup, "child kill failed");
132 }
133
134 /*
135 * make sure children finished before we proceed with next testcase
136 */
137 for (j = 0; j < NCHILD; j++) {
138 if (wait(&status) == -1)
139 tst_brkm(TBROK | TERRNO, cleanup, "wait() failed");
140 }
141}
142
DAN LIbffa8862013-05-13 10:21:43 +0800143int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000144{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200145 int lc;
Jan Stancek7018c132015-11-13 14:05:28 +0100146 int i;
plars865695b2001-08-27 22:15:12 +0000147
Cyril Hrubisd6d11d02015-03-09 17:35:43 +0100148 tst_parse_opts(argc, argv, NULL, NULL);
DAN LIbffa8862013-05-13 10:21:43 +0800149
robbiewd34d5812005-07-11 22:28:09 +0000150#ifdef UCLINUX
DAN LIbffa8862013-05-13 10:21:43 +0800151 argv0 = argv[0];
robbiewd34d5812005-07-11 22:28:09 +0000152 maybe_run_child(&child_pid, "nd", 1, &sem_id_1);
subrata_modak4b13c892009-01-22 09:51:33 +0000153 maybe_run_child(&child_cnt, "ndd", 2, &sem_id_1, &sem_op);
robbiewd34d5812005-07-11 22:28:09 +0000154#endif
155
DAN LIbffa8862013-05-13 10:21:43 +0800156 setup();
plars865695b2001-08-27 22:15:12 +0000157
158 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800159 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000160
subrata_modak56207ce2009-03-23 13:35:39 +0000161 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000162
163 /*
164 * Set up any conditions if needed
165 */
plars865695b2001-08-27 22:15:12 +0000166 if (TC[i].func_setup != NULL) {
167 /* call the setup function */
168 switch (TC[i].cmd) {
169 case GETNCNT:
subrata_modak56207ce2009-03-23 13:35:39 +0000170 (*TC[i].func_setup) (-ONE);
plars865695b2001-08-27 22:15:12 +0000171 break;
172 case GETZCNT:
subrata_modak56207ce2009-03-23 13:35:39 +0000173 (*TC[i].func_setup) (0);
plars865695b2001-08-27 22:15:12 +0000174 break;
175 default:
subrata_modak56207ce2009-03-23 13:35:39 +0000176 (*TC[i].func_setup) ();
plars865695b2001-08-27 22:15:12 +0000177 break;
178 }
179 }
180
DAN LIf74f0a92013-05-22 09:52:09 +0800181 TEST(semctl(*(TC[i].semid), TC[i].semnum, TC[i].cmd,
plars865695b2001-08-27 22:15:12 +0000182 TC[i].arg));
subrata_modakbdbaec52009-02-26 12:14:51 +0000183
plars865695b2001-08-27 22:15:12 +0000184 if (TEST_RETURN == -1) {
185 tst_resm(TFAIL, "%s call failed - errno = %d "
186 ": %s", TCID, TEST_ERRNO,
187 strerror(TEST_ERRNO));
188 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200189 /*
190 * call the appropriate test function
191 * and pass the return value where it
192 * is needed to perform certain tests.
193 */
194 switch (TC[i].cmd) {
195 case GETNCNT:
196 case GETZCNT:
197 case GETPID:
198 case GETVAL:
199 case IPC_INFO:
200 case SEM_STAT:
201 (*TC[i].func_test) (TEST_RETURN);
202 break;
203 default:
204 (*TC[i].func_test) ();
205 break;
plars865695b2001-08-27 22:15:12 +0000206 }
207 }
208
209 /*
210 * If testing GETNCNT or GETZCNT, clean up the children.
211 */
212 switch (TC[i].cmd) {
213 case GETNCNT:
DAN LIbffa8862013-05-13 10:21:43 +0800214 case GETZCNT:
Jan Stancek7018c132015-11-13 14:05:28 +0100215 kill_all_children();
plars865695b2001-08-27 22:15:12 +0000216 break;
217 }
218 }
219 /*
220 * recreate the semaphore resource if looping
221 */
222 if (TEST_LOOPING(lc)) {
DAN LIbffa8862013-05-13 10:21:43 +0800223 sem_id_1 = semget(semkey, PSEMS,
224 IPC_CREAT | IPC_EXCL | SEM_RA);
225 if (sem_id_1 == -1)
subrata_modak56207ce2009-03-23 13:35:39 +0000226 tst_brkm(TBROK, cleanup,
227 "couldn't recreate " "semaphore");
plars865695b2001-08-27 22:15:12 +0000228 }
229 }
230
231 cleanup();
232
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800233 tst_exit();
plars865695b2001-08-27 22:15:12 +0000234}
235
236/*
237 * func_stat() - check the functionality of the IPC_STAT command with semctl()
238 */
DAN LIbffa8862013-05-13 10:21:43 +0800239static void func_stat(void)
plars865695b2001-08-27 22:15:12 +0000240{
241 /* check the number of semaphores and the ipc_perm.mode value */
DAN LIbffa8862013-05-13 10:21:43 +0800242 if (buf.sem_nsems == PSEMS && buf.sem_perm.mode == (SEM_RA))
plars865695b2001-08-27 22:15:12 +0000243 tst_resm(TPASS, "buf.sem_nsems and buf.sem_perm.mode"
subrata_modak56207ce2009-03-23 13:35:39 +0000244 " are correct");
DAN LIbffa8862013-05-13 10:21:43 +0800245 else
plars865695b2001-08-27 22:15:12 +0000246 tst_resm(TFAIL, "semaphore STAT info is incorrect");
plars865695b2001-08-27 22:15:12 +0000247}
248
249/*
250 * set_setup() - set up for the IPC_SET command with semctl()
251 */
DAN LIbffa8862013-05-13 10:21:43 +0800252static void set_setup(void)
plars865695b2001-08-27 22:15:12 +0000253{
254 /* set up a new mode for the semaphore set */
255 buf.sem_perm.mode = SEM_RA | NEWMODE;
256}
257
258/*
259 * func_set() - check the functionality of the IPC_SET command with semctl()
260 */
DAN LIbffa8862013-05-13 10:21:43 +0800261static void func_set(void)
plars865695b2001-08-27 22:15:12 +0000262{
263 /* first stat the semaphore to get the new data */
264 if (semctl(sem_id_1, 0, IPC_STAT, (union semun)&buf) == -1) {
265 tst_resm(TBROK, "stat failed in func_set()");
266 return;
267 }
268
269 /* check that the new mode is what we set */
DAN LIbffa8862013-05-13 10:21:43 +0800270 if (buf.sem_perm.mode == (SEM_RA | NEWMODE))
plars865695b2001-08-27 22:15:12 +0000271 tst_resm(TPASS, "buf.sem_perm.mode is correct");
DAN LIbffa8862013-05-13 10:21:43 +0800272 else
plars865695b2001-08-27 22:15:12 +0000273 tst_resm(TFAIL, "semaphore mode info is incorrect");
plars865695b2001-08-27 22:15:12 +0000274}
275
276/*
277 * func_gall() - check the functionality of the GETALL command with semctl()
278 */
DAN LIbffa8862013-05-13 10:21:43 +0800279static void func_gall(void)
plars865695b2001-08-27 22:15:12 +0000280{
281 int i;
282
283 /* the initial value of the primitive semaphores should be zero */
subrata_modak56207ce2009-03-23 13:35:39 +0000284 for (i = 0; i < PSEMS; i++) {
plars865695b2001-08-27 22:15:12 +0000285 if (array[i] != 0) {
286 tst_resm(TFAIL, "semaphore %d has unexpected value", i);
287 return;
288 }
289 }
290 tst_resm(TPASS, "semaphores have expected values");
291}
292
293/*
294 * cnt_setup() - set up for the GETNCNT and GETZCNT commands with semctl()
295 */
DAN LIbffa8862013-05-13 10:21:43 +0800296static void cnt_setup(int opval)
plars865695b2001-08-27 22:15:12 +0000297{
298 int pid, i;
299
300 sops.sem_num = SEM4;
301 sops.sem_flg = 0;
302
303 /*
304 * if seting up for GETZCNT, the semaphore value needs to be positive
305 */
306 if (opval == 0) {
307 /* initialize the semaphore value to ONE */
308 sops.sem_op = ONE;
DAN LIbffa8862013-05-13 10:21:43 +0800309 if (semop(sem_id_1, &sops, 1) == -1)
plars865695b2001-08-27 22:15:12 +0000310 tst_brkm(TBROK, cleanup, "semop #1 failed - cnt_setup");
plars865695b2001-08-27 22:15:12 +0000311 }
312
DAN LIbffa8862013-05-13 10:21:43 +0800313 /* set the correct operation */
314 sops.sem_op = opval;
subrata_modak56207ce2009-03-23 13:35:39 +0000315 for (i = 0; i < NCHILD; i++) {
plars865695b2001-08-27 22:15:12 +0000316 /* fork five children to wait */
DAN LIbffa8862013-05-13 10:21:43 +0800317 pid = FORK_OR_VFORK();
318 if (pid == -1)
plars865695b2001-08-27 22:15:12 +0000319 tst_brkm(TBROK, cleanup, "fork failed in cnt_setup");
subrata_modakbdbaec52009-02-26 12:14:51 +0000320
DAN LIbffa8862013-05-13 10:21:43 +0800321 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000322#ifdef UCLINUX
subrata_modak4b13c892009-01-22 09:51:33 +0000323 sem_op = sops.sem_op;
DAN LIbffa8862013-05-13 10:21:43 +0800324 if (self_exec(argv0, "ndd", 2, sem_id_1, sem_op) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000325 tst_brkm(TBROK, cleanup, "self_exec failed "
326 "in cnt_setup");
robbiewd34d5812005-07-11 22:28:09 +0000327#else
328 child_cnt();
329#endif
DAN LIbffa8862013-05-13 10:21:43 +0800330 } else {
Cyril Hrubisf9791ef2015-03-05 16:09:16 +0100331 TST_PROCESS_STATE_WAIT(cleanup, pid, 'S');
plars865695b2001-08-27 22:15:12 +0000332 /* save the pid so we can kill it later */
333 pid_arr[i] = pid;
334 }
335 }
336}
337
DAN LIbffa8862013-05-13 10:21:43 +0800338static void child_cnt(void)
robbiewd34d5812005-07-11 22:28:09 +0000339{
subrata_modak4b13c892009-01-22 09:51:33 +0000340#ifdef UCLINUX
341 sops.sem_op = (short int)sem_op;
subrata_modak4b13c892009-01-22 09:51:33 +0000342#endif
343
robbiewd34d5812005-07-11 22:28:09 +0000344 sops.sem_num = SEM4;
345 sops.sem_flg = 0;
346
347 /*
348 * Do a semop that will cause the child to sleep.
349 * The child process will be killed in the func_ncnt
350 * routine which should cause an error to be return
351 * by the semop() call.
352 */
DAN LIbffa8862013-05-13 10:21:43 +0800353 if (semop(sem_id_1, &sops, 1) != -1)
robbiewd34d5812005-07-11 22:28:09 +0000354 tst_resm(TBROK, "semop succeeded - cnt_setup");
DAN LIbffa8862013-05-13 10:21:43 +0800355
robbiewd34d5812005-07-11 22:28:09 +0000356 exit(0);
357}
358
plars865695b2001-08-27 22:15:12 +0000359/*
360 * func_cnt() - check the functionality of the GETNCNT and GETZCNT commands
361 * with semctl()
362 */
DAN LIbffa8862013-05-13 10:21:43 +0800363static void func_cnt(int rval)
plars865695b2001-08-27 22:15:12 +0000364{
plars865695b2001-08-27 22:15:12 +0000365
DAN LIbffa8862013-05-13 10:21:43 +0800366 if (rval == NCHILD)
plars865695b2001-08-27 22:15:12 +0000367 tst_resm(TPASS, "number of sleeping processes is correct");
DAN LIbffa8862013-05-13 10:21:43 +0800368 else
plars865695b2001-08-27 22:15:12 +0000369 tst_resm(TFAIL, "number of sleeping processes is not correct");
plars865695b2001-08-27 22:15:12 +0000370}
371
372/*
373 * pid_setup() - set up for the GETPID command with semctl()
374 */
DAN LIbffa8862013-05-13 10:21:43 +0800375static void pid_setup(void)
plars865695b2001-08-27 22:15:12 +0000376{
377 int pid;
subrata_modak41399ed2008-03-14 19:16:23 +0000378
plars865695b2001-08-27 22:15:12 +0000379 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000380 * Fork a child to do a semop that will pass.
plars865695b2001-08-27 22:15:12 +0000381 */
DAN LIbffa8862013-05-13 10:21:43 +0800382 pid = FORK_OR_VFORK();
383 if (pid == -1)
plars865695b2001-08-27 22:15:12 +0000384 tst_brkm(TBROK, cleanup, "fork failed in pid_setup()");
robbiewd34d5812005-07-11 22:28:09 +0000385
plars865695b2001-08-27 22:15:12 +0000386 if (pid == 0) { /* child */
robbiewd34d5812005-07-11 22:28:09 +0000387#ifdef UCLINUX
DAN LIbffa8862013-05-13 10:21:43 +0800388 if (self_exec(argv0, "nd", 1, sem_id_1) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000389 tst_brkm(TBROK, cleanup, "self_exec failed "
390 "in pid_setup()");
robbiewd34d5812005-07-11 22:28:09 +0000391#else
392 child_pid();
393#endif
Cyril Hrubisf9791ef2015-03-05 16:09:16 +0100394 } else {
plars865695b2001-08-27 22:15:12 +0000395 pid_arr[SEM2] = pid;
Cyril Hrubisf9791ef2015-03-05 16:09:16 +0100396 TST_PROCESS_STATE_WAIT(cleanup, pid, 'Z');
plars865695b2001-08-27 22:15:12 +0000397 }
398}
399
DAN LIbffa8862013-05-13 10:21:43 +0800400static void child_pid(void)
robbiewd34d5812005-07-11 22:28:09 +0000401{
DAN LIbffa8862013-05-13 10:21:43 +0800402 sops.sem_num = SEM2;
403 sops.sem_op = ONE;
robbiewd34d5812005-07-11 22:28:09 +0000404 sops.sem_flg = 0;
robbiewd34d5812005-07-11 22:28:09 +0000405 /*
406 * Do a semop that will increment the semaphore.
407 */
DAN LIbffa8862013-05-13 10:21:43 +0800408 if (semop(sem_id_1, &sops, 1) == -1)
robbiewd34d5812005-07-11 22:28:09 +0000409 tst_resm(TBROK, "semop failed - pid_setup");
robbiewd34d5812005-07-11 22:28:09 +0000410 exit(0);
411}
412
plars865695b2001-08-27 22:15:12 +0000413/*
414 * func_pid() - check the functionality of the GETPID command with semctl()
415 */
DAN LIbffa8862013-05-13 10:21:43 +0800416static void func_pid(int rval)
plars865695b2001-08-27 22:15:12 +0000417{
418 /* compare the rval (pid) to the saved pid from the setup */
DAN LIbffa8862013-05-13 10:21:43 +0800419 if (rval == pid_arr[SEM2])
plars865695b2001-08-27 22:15:12 +0000420 tst_resm(TPASS, "last pid value is correct");
DAN LIbffa8862013-05-13 10:21:43 +0800421 else
plars865695b2001-08-27 22:15:12 +0000422 tst_resm(TFAIL, "last pid value is not correct");
plars865695b2001-08-27 22:15:12 +0000423}
424
425/*
426 * func_gval() - check the functionality of the GETVAL command with semctl()
427 */
DAN LIbffa8862013-05-13 10:21:43 +0800428static void func_gval(int rval)
plars865695b2001-08-27 22:15:12 +0000429{
430 /*
431 * This is a simple test. The semaphore value should be equal
432 * to ONE as it was set in the last test (GETPID).
433 */
DAN LIbffa8862013-05-13 10:21:43 +0800434 if (rval == 1)
plars865695b2001-08-27 22:15:12 +0000435 tst_resm(TPASS, "semaphore value is correct");
DAN LIbffa8862013-05-13 10:21:43 +0800436 else
plars865695b2001-08-27 22:15:12 +0000437 tst_resm(TFAIL, "semaphore value is not correct");
plars865695b2001-08-27 22:15:12 +0000438}
439
440/*
441 * all_setup() - set up for the SETALL command with semctl()
442 */
DAN LIbffa8862013-05-13 10:21:43 +0800443static void sall_setup(void)
plars865695b2001-08-27 22:15:12 +0000444{
445 int i;
446
subrata_modak56207ce2009-03-23 13:35:39 +0000447 for (i = 0; i < PSEMS; i++) {
plars865695b2001-08-27 22:15:12 +0000448 /* initialize the array values to 3 */
449 array[i] = 3;
450 }
451}
452
453/*
454 * func_sall() - check the functionality of the SETALL command with semctl()
455 */
DAN LIbffa8862013-05-13 10:21:43 +0800456static void func_sall(void)
plars865695b2001-08-27 22:15:12 +0000457{
458 int i;
459 unsigned short rarray[PSEMS];
460
461 /*
462 * do a GETALL and compare the values to those set above
463 */
464
DAN LIbffa8862013-05-13 10:21:43 +0800465 if (semctl(sem_id_1, 0, GETALL, (union semun)rarray) == -1)
plars865695b2001-08-27 22:15:12 +0000466 tst_brkm(TBROK, cleanup, "semctl failed in func_sall");
plars865695b2001-08-27 22:15:12 +0000467
subrata_modak56207ce2009-03-23 13:35:39 +0000468 for (i = 0; i < PSEMS; i++) {
plars865695b2001-08-27 22:15:12 +0000469 if (array[i] != rarray[i]) {
470 tst_resm(TFAIL, "semaphore values are not correct");
471 return;
472 }
473 }
474
475 tst_resm(TPASS, "semaphore values are correct");
476}
477
478/*
479 * func_sval() - check the functionality of the SETVAL command with semctl()
480 */
DAN LIbffa8862013-05-13 10:21:43 +0800481static void func_sval(void)
plars865695b2001-08-27 22:15:12 +0000482{
483 int semv;
iyermanoje2cb9cd2002-01-21 18:48:13 +0000484 union semun arr;
plars865695b2001-08-27 22:15:12 +0000485
486 /*
487 * do a GETVAL and compare it to the value set above
488 */
489
DAN LIbffa8862013-05-13 10:21:43 +0800490 semv = semctl(sem_id_1, SEM4, GETVAL, arr);
491 if (semv == -1)
plars865695b2001-08-27 22:15:12 +0000492 tst_brkm(TBROK, cleanup, "semctl failed in func_sval");
plars865695b2001-08-27 22:15:12 +0000493
DAN LIbffa8862013-05-13 10:21:43 +0800494 if (semv != INCVAL)
plars865695b2001-08-27 22:15:12 +0000495 tst_resm(TFAIL, "semaphore value is not what was set");
DAN LIbffa8862013-05-13 10:21:43 +0800496 else
plars865695b2001-08-27 22:15:12 +0000497 tst_resm(TPASS, "semaphore value is correct");
plars865695b2001-08-27 22:15:12 +0000498}
499
500/*
501 * func_rmid() - check the functionality of the IPC_RMID command with semctl()
502 */
DAN LIbffa8862013-05-13 10:21:43 +0800503static void func_rmid(void)
plars865695b2001-08-27 22:15:12 +0000504{
505
506 /*
507 * do a semop() - we should get EINVAL
508 */
DAN LIbffa8862013-05-13 10:21:43 +0800509 if (semop(sem_id_1, &sops, 1) != -1)
plars865695b2001-08-27 22:15:12 +0000510 tst_resm(TFAIL, "semop succeeded on expected fail");
plars865695b2001-08-27 22:15:12 +0000511
DAN LIbffa8862013-05-13 10:21:43 +0800512 if (errno != EINVAL)
plars865695b2001-08-27 22:15:12 +0000513 tst_resm(TFAIL, "returned errno - %d - is not expected", errno);
DAN LIbffa8862013-05-13 10:21:43 +0800514 else
plars865695b2001-08-27 22:15:12 +0000515 tst_resm(TPASS, "semaphore appears to be removed");
plars865695b2001-08-27 22:15:12 +0000516
517 sem_id_1 = -1;
518}
519
DAN LIa63f9332013-05-13 10:24:33 +0800520static void func_iinfo(int hidx)
521{
DAN LIf74f0a92013-05-22 09:52:09 +0800522 if (hidx >= 0) {
523 sem_index = hidx;
DAN LIa63f9332013-05-13 10:24:33 +0800524 tst_resm(TPASS, "the highest index is correct");
DAN LIf74f0a92013-05-22 09:52:09 +0800525 } else {
526 sem_index = 0;
DAN LIa63f9332013-05-13 10:24:33 +0800527 tst_resm(TFAIL, "the highest index is incorrect");
DAN LIf74f0a92013-05-22 09:52:09 +0800528 }
DAN LIa63f9332013-05-13 10:24:33 +0800529}
530
531static void func_sinfo(void)
532{
533 if (ipc_buf.semusz < 1)
534 tst_resm(TFAIL, "number of semaphore sets is incorrect");
535 else
536 tst_resm(TPASS, "number of semaphore sets is correct");
537}
538
539static void func_sstat(int semidx)
540{
541 if (semidx >= 0)
542 tst_resm(TPASS, "id of the semaphore set is correct");
543 else
544 tst_resm(TFAIL, "id of the semaphore set is incorrect");
545}
546
subrata_modak56207ce2009-03-23 13:35:39 +0000547void setup(void)
plars865695b2001-08-27 22:15:12 +0000548{
Garrett Cooper2c282152010-12-16 00:55:50 -0800549
plars865695b2001-08-27 22:15:12 +0000550 tst_sig(FORK, DEF_HANDLER, cleanup);
551
plars865695b2001-08-27 22:15:12 +0000552 TEST_PAUSE;
553
plars865695b2001-08-27 22:15:12 +0000554 tst_tmpdir();
555
Cyril Hrubisf9791ef2015-03-05 16:09:16 +0100556 TST_CHECKPOINT_INIT(tst_rmdir);
557
plars865695b2001-08-27 22:15:12 +0000558 /* get an IPC resource key */
559 semkey = getipckey();
560
561 /* create a semaphore set with read and alter permissions */
DAN LIbffa8862013-05-13 10:21:43 +0800562 sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
563 if (sem_id_1 == -1)
plars865695b2001-08-27 22:15:12 +0000564 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
plars865695b2001-08-27 22:15:12 +0000565}
566
subrata_modak56207ce2009-03-23 13:35:39 +0000567void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000568{
569 /* if it exists, remove the semaphore resource */
570 rm_sema(sem_id_1);
571
plars865695b2001-08-27 22:15:12 +0000572 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700573}