semctl01.c revision 74225622077af37664b156b18aaa12f3bda9939e
1/*
2 * Copyright (c) International Business Machines  Corp., 2001
3 *
4 * 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.
8 *
9 * 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.
13 *
14 * 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
17 */
18
19/*
20 * NAME
21 *	semctl01.c
22 *
23 * DESCRIPTION
24 *	semctl01 - test the 13 possible semctl() commands
25 *
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
37 *		if correct,
38 *			issue a PASS message
39 *		otherwise
40 *			issue a FAIL message
41 *	call cleanup
42 */
43
44
45#ifndef _GNU_SOURCE
46#define _GNU_SOURCE
47#endif
48#include "ipcsem.h"
49#include "libtestsuite.h"
50
51char *TCID = "semctl01";
52int TST_TOTAL = 13;
53
54static int sem_id_1 = -1;
55static int sem_index;
56
57static int sync_pipes[2];
58
59/*
60 * These are the various setup and check functions for the 10 different
61 * commands that are available for the semctl() call.
62 */
63static void func_stat(void);
64static void set_setup(void), func_set(void);
65static void func_gall(void);
66static void cnt_setup(int), func_cnt(int);
67static void pid_setup(void), func_pid(int);
68static void func_gval(int);
69static void sall_setup(void), func_sall(void);
70static void func_sval(void);
71static void func_rmid(void);
72static void child_cnt(void);
73static void child_pid(void);
74static void func_iinfo(int);
75static void func_sinfo(void);
76static void func_sstat(int);
77
78static struct semid_ds buf;
79static struct seminfo ipc_buf;
80static unsigned short array[PSEMS];
81static struct sembuf sops;
82
83#define INCVAL 2
84#define NEWMODE	066
85#define NCHILD	5
86#define SEM2	2
87#define SEM4	4
88#define ONE	1
89#ifdef _XLC_COMPILER
90#define SEMUN_CAST
91#else
92#define SEMUN_CAST (union semun)
93#endif
94
95static int pid_arr[NCHILD];
96
97#ifdef UCLINUX
98#define PIPE_NAME	"semctl01"
99static char *argv0;
100static int sem_op;
101#endif
102
103static struct test_case_t {
104	int *semid;
105	int semnum;
106	int cmd;
107	void (*func_test) ();
108	union semun arg;
109	void (*func_setup) ();
110} TC[] = {
111	{&sem_id_1, 0, IPC_STAT, func_stat, SEMUN_CAST & buf, NULL},
112	{&sem_id_1, 0, IPC_SET, func_set, SEMUN_CAST & buf, set_setup},
113	{&sem_id_1, 0, GETALL, func_gall, SEMUN_CAST array, NULL},
114	{&sem_id_1, SEM4, GETNCNT, func_cnt, SEMUN_CAST & buf, cnt_setup},
115	{&sem_id_1, SEM2, GETPID, func_pid, SEMUN_CAST & buf, pid_setup},
116	{&sem_id_1, SEM2, GETVAL, func_gval, SEMUN_CAST & buf, NULL},
117	{&sem_id_1, SEM4, GETZCNT, func_cnt, SEMUN_CAST & buf, cnt_setup},
118	{&sem_id_1, 0, SETALL, func_sall, SEMUN_CAST array, sall_setup},
119	{&sem_id_1, SEM4, SETVAL, func_sval, SEMUN_CAST INCVAL, NULL},
120	{&sem_id_1, 0, IPC_INFO, func_iinfo, SEMUN_CAST & ipc_buf, NULL},
121	{&sem_id_1, 0, SEM_INFO, func_sinfo, SEMUN_CAST & ipc_buf, NULL},
122	{&sem_index, 0, SEM_STAT, func_sstat, SEMUN_CAST & buf, NULL},
123	{&sem_id_1, 0, IPC_RMID, func_rmid, SEMUN_CAST & buf, NULL},
124};
125
126int main(int argc, char *argv[])
127{
128	int lc;
129	const char *msg;
130	int i, j;
131
132	msg = parse_opts(argc, argv, NULL, NULL);
133	if (msg != NULL)
134		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
135
136#ifdef UCLINUX
137	argv0 = argv[0];
138	maybe_run_child(&child_pid, "nd", 1, &sem_id_1);
139	maybe_run_child(&child_cnt, "ndd", 2, &sem_id_1, &sem_op);
140#endif
141
142	setup();
143
144	for (lc = 0; TEST_LOOPING(lc); lc++) {
145		tst_count = 0;
146
147		for (i = 0; i < TST_TOTAL; i++) {
148
149			/*
150			 * Set up any conditions if needed
151			 */
152			if (TC[i].func_setup != NULL) {
153				/* call the setup function */
154				switch (TC[i].cmd) {
155				case GETNCNT:
156					(*TC[i].func_setup) (-ONE);
157					break;
158				case GETZCNT:
159					(*TC[i].func_setup) (0);
160					break;
161				default:
162					(*TC[i].func_setup) ();
163					break;
164				}
165			}
166
167			TEST(semctl(*(TC[i].semid), TC[i].semnum, TC[i].cmd,
168				    TC[i].arg));
169
170			if (TEST_RETURN == -1) {
171				tst_resm(TFAIL, "%s call failed - errno = %d "
172					 ": %s", TCID, TEST_ERRNO,
173					 strerror(TEST_ERRNO));
174			} else {
175				/*
176				 * call the appropriate test function
177				 * and pass the return value where it
178				 * is needed to perform certain tests.
179				 */
180				switch (TC[i].cmd) {
181				case GETNCNT:
182				case GETZCNT:
183				case GETPID:
184				case GETVAL:
185				case IPC_INFO:
186				case SEM_STAT:
187					(*TC[i].func_test) (TEST_RETURN);
188					break;
189				default:
190					(*TC[i].func_test) ();
191					break;
192				}
193			}
194
195			/*
196			 * If testing GETNCNT or GETZCNT, clean up the children.
197			 */
198			switch (TC[i].cmd) {
199			case GETNCNT:
200			case GETZCNT:
201				for (j = 0; j < NCHILD; j++) {
202					if (kill(pid_arr[j], SIGKILL) == -1)
203						tst_brkm(TBROK, cleanup,
204							 "child kill failed");
205				}
206				break;
207			}
208		}
209		/*
210		 * recreate the semaphore resource if looping
211		 */
212		if (TEST_LOOPING(lc)) {
213			sem_id_1 = semget(semkey, PSEMS,
214					  IPC_CREAT | IPC_EXCL | SEM_RA);
215			if (sem_id_1 == -1)
216				tst_brkm(TBROK, cleanup,
217					 "couldn't recreate " "semaphore");
218		}
219	}
220
221	cleanup();
222
223	tst_exit();
224}
225
226/*
227 * func_stat() - check the functionality of the IPC_STAT command with semctl()
228 */
229static void func_stat(void)
230{
231	/* check the number of semaphores and the ipc_perm.mode value */
232	if (buf.sem_nsems == PSEMS && buf.sem_perm.mode == (SEM_RA))
233		tst_resm(TPASS, "buf.sem_nsems and buf.sem_perm.mode"
234			 " are correct");
235	else
236		tst_resm(TFAIL, "semaphore STAT info is incorrect");
237}
238
239/*
240 * set_setup() - set up for the IPC_SET command with semctl()
241 */
242static void set_setup(void)
243{
244	/* set up a new mode for the semaphore set */
245	buf.sem_perm.mode = SEM_RA | NEWMODE;
246}
247
248/*
249 * func_set() - check the functionality of the IPC_SET command with semctl()
250 */
251static void func_set(void)
252{
253	/* first stat the semaphore to get the new data */
254	if (semctl(sem_id_1, 0, IPC_STAT, (union semun)&buf) == -1) {
255		tst_resm(TBROK, "stat failed in func_set()");
256		return;
257	}
258
259	/* check that the new mode is what we set */
260	if (buf.sem_perm.mode == (SEM_RA | NEWMODE))
261		tst_resm(TPASS, "buf.sem_perm.mode is correct");
262	else
263		tst_resm(TFAIL, "semaphore mode info is incorrect");
264}
265
266/*
267 * func_gall() - check the functionality of the GETALL command with semctl()
268 */
269static void func_gall(void)
270{
271	int i;
272
273	/* the initial value of the primitive semaphores should be zero */
274	for (i = 0; i < PSEMS; i++) {
275		if (array[i] != 0) {
276			tst_resm(TFAIL, "semaphore %d has unexpected value", i);
277			return;
278		}
279	}
280	tst_resm(TPASS, "semaphores have expected values");
281}
282
283/*
284 * cnt_setup() - set up for the GETNCNT and GETZCNT commands with semctl()
285 */
286static void cnt_setup(int opval)
287{
288	int pid, i;
289
290	sops.sem_num = SEM4;
291	sops.sem_flg = 0;
292
293	/*
294	 * if seting up for GETZCNT, the semaphore value needs to be positive
295	 */
296	if (opval == 0) {
297		/* initialize the semaphore value to ONE */
298		sops.sem_op = ONE;
299		if (semop(sem_id_1, &sops, 1) == -1)
300			tst_brkm(TBROK, cleanup, "semop #1 failed - cnt_setup");
301	}
302
303	/* set the correct operation */
304	sops.sem_op = opval;
305	for (i = 0; i < NCHILD; i++) {
306		if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
307			tst_brkm(TBROK, cleanup, "sync_pipe_create failed");
308
309		/* fork five children to wait */
310		pid = FORK_OR_VFORK();
311		if (pid == -1)
312			tst_brkm(TBROK, cleanup, "fork failed in cnt_setup");
313
314		if (pid == 0) {
315#ifdef UCLINUX
316			sem_op = sops.sem_op;
317			if (self_exec(argv0, "ndd", 2, sem_id_1, sem_op) < 0)
318				tst_brkm(TBROK, cleanup, "self_exec failed "
319					 "in cnt_setup");
320#else
321			child_cnt();
322#endif
323		} else {
324			if (sync_pipe_wait(sync_pipes) == -1)
325				tst_brkm(TBROK, cleanup,
326					 "sync_pipe_wait failed");
327
328			if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
329				tst_brkm(TBROK, cleanup,
330					 "sync_pipe_close failed");
331
332			/* save the pid so we can kill it later */
333			pid_arr[i] = pid;
334		}
335	}
336	/* After last son has been created, give it a chance to execute the
337	 * semop command before we continue. Without this sleep, on SMP machine
338	 * the father semctl could be executed before the son semop.
339	 */
340	sleep(1);
341}
342
343static void child_cnt(void)
344{
345#ifdef UCLINUX
346	sops.sem_op = (short int)sem_op;
347	if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
348		tst_brkm(TBROK, cleanup, "sync_pipe_create failed");
349#endif
350
351	if (sync_pipe_notify(sync_pipes) == -1)
352		tst_brkm(TBROK, cleanup, "sync_pipe_notify failed");
353
354#ifdef UCLINUX
355	if (sync_pipe_close(sync_pipes, NULL) == -1)
356#else
357	if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
358#endif
359		tst_brkm(TBROK, cleanup, "sync_pipe_close failed");
360
361	sops.sem_num = SEM4;
362	sops.sem_flg = 0;
363
364	/*
365	 * Do a semop that will cause the child to sleep.
366	 * The child process will be killed in the func_ncnt
367	 * routine which should cause an error to be return
368	 * by the semop() call.
369	 */
370	if (semop(sem_id_1, &sops, 1) != -1)
371		tst_resm(TBROK, "semop succeeded - cnt_setup");
372
373	exit(0);
374}
375
376/*
377 * func_cnt() - check the functionality of the GETNCNT and GETZCNT commands
378 *	        with semctl()
379 */
380static void func_cnt(int rval)
381{
382
383	if (rval == NCHILD)
384		tst_resm(TPASS, "number of sleeping processes is correct");
385	else
386		tst_resm(TFAIL, "number of sleeping processes is not correct");
387}
388
389/*
390 * pid_setup() - set up for the GETPID command with semctl()
391 */
392static void pid_setup(void)
393{
394	int pid;
395
396	if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
397		tst_brkm(TBROK, cleanup, "sync_pipe_create failed");
398
399	/*
400	 * Fork a child to do a semop that will pass.
401	 */
402	pid = FORK_OR_VFORK();
403	if (pid == -1)
404		tst_brkm(TBROK, cleanup, "fork failed in pid_setup()");
405
406	if (pid == 0) {		/* child */
407#ifdef UCLINUX
408		if (self_exec(argv0, "nd", 1, sem_id_1) < 0)
409			tst_brkm(TBROK, cleanup, "self_exec failed "
410				 "in pid_setup()");
411#else
412		child_pid();
413#endif
414	} else {		/* parent */
415		if (sync_pipe_wait(sync_pipes) == -1)
416			tst_brkm(TBROK, cleanup, "sync_pipe_wait failed");
417
418		if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
419			tst_brkm(TBROK, cleanup, "sync_pipe_close failed");
420		sleep(1);
421		pid_arr[SEM2] = pid;
422	}
423}
424
425static void child_pid(void)
426{
427#ifdef UCLINUX
428	if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
429		tst_brkm(TBROK, cleanup, "sync_pipe_create failed");
430#endif
431
432	if (sync_pipe_notify(sync_pipes) == -1)
433		tst_brkm(TBROK, cleanup, "sync_pipe_notify failed");
434
435	if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
436		tst_brkm(TBROK, cleanup, "sync_pipe_close failed");
437
438	sops.sem_num = SEM2;
439	sops.sem_op = ONE;
440	sops.sem_flg = 0;
441
442	/*
443	 * Do a semop that will increment the semaphore.
444	 */
445	if (semop(sem_id_1, &sops, 1) == -1)
446		tst_resm(TBROK, "semop failed - pid_setup");
447
448	exit(0);
449}
450
451/*
452 * func_pid() - check the functionality of the GETPID command with semctl()
453 */
454static void func_pid(int rval)
455{
456	/* compare the rval (pid) to the saved pid from the setup */
457	if (rval == pid_arr[SEM2])
458		tst_resm(TPASS, "last pid value is correct");
459	else
460		tst_resm(TFAIL, "last pid value is not correct");
461}
462
463/*
464 * func_gval() - check the functionality of the GETVAL command with semctl()
465 */
466static void func_gval(int rval)
467{
468	/*
469	 * This is a simple test.  The semaphore value should be equal
470	 * to ONE as it was set in the last test (GETPID).
471	 */
472	if (rval == 1)
473		tst_resm(TPASS, "semaphore value is correct");
474	else
475		tst_resm(TFAIL, "semaphore value is not correct");
476}
477
478/*
479 * all_setup() - set up for the SETALL command with semctl()
480 */
481static void sall_setup(void)
482{
483	int i;
484
485	for (i = 0; i < PSEMS; i++) {
486		/* initialize the array values to 3 */
487		array[i] = 3;
488	}
489}
490
491/*
492 * func_sall() - check the functionality of the SETALL command with semctl()
493 */
494static void func_sall(void)
495{
496	int i;
497	unsigned short rarray[PSEMS];
498
499	/*
500	 * do a GETALL and compare the values to those set above
501	 */
502
503	if (semctl(sem_id_1, 0, GETALL, (union semun)rarray) == -1)
504		tst_brkm(TBROK, cleanup, "semctl failed in func_sall");
505
506	for (i = 0; i < PSEMS; i++) {
507		if (array[i] != rarray[i]) {
508			tst_resm(TFAIL, "semaphore values are not correct");
509			return;
510		}
511	}
512
513	tst_resm(TPASS, "semaphore values are correct");
514}
515
516/*
517 * func_sval() - check the functionality of the SETVAL command with semctl()
518 */
519static void func_sval(void)
520{
521	int semv;
522	union semun arr;
523
524	/*
525	 * do a GETVAL and compare it to the value set above
526	 */
527
528	semv = semctl(sem_id_1, SEM4, GETVAL, arr);
529	if (semv == -1)
530		tst_brkm(TBROK, cleanup, "semctl failed in func_sval");
531
532	if (semv != INCVAL)
533		tst_resm(TFAIL, "semaphore value is not what was set");
534	else
535		tst_resm(TPASS, "semaphore value is correct");
536}
537
538/*
539 * func_rmid() - check the functionality of the IPC_RMID command with semctl()
540 */
541static void func_rmid(void)
542{
543
544	/*
545	 * do a semop() - we should get EINVAL
546	 */
547	if (semop(sem_id_1, &sops, 1) != -1)
548		tst_resm(TFAIL, "semop succeeded on expected fail");
549
550	if (errno != EINVAL)
551		tst_resm(TFAIL, "returned errno - %d - is not expected", errno);
552	else
553		tst_resm(TPASS, "semaphore appears to be removed");
554
555	sem_id_1 = -1;
556}
557
558static void func_iinfo(int hidx)
559{
560	if (hidx >= 0) {
561		sem_index = hidx;
562		tst_resm(TPASS, "the highest index is correct");
563	} else {
564		sem_index = 0;
565		tst_resm(TFAIL, "the highest index is incorrect");
566	}
567}
568
569static void func_sinfo(void)
570{
571	if (ipc_buf.semusz < 1)
572		tst_resm(TFAIL, "number of semaphore sets is incorrect");
573	else
574		tst_resm(TPASS, "number of semaphore sets is correct");
575}
576
577static void func_sstat(int semidx)
578{
579	if (semidx >= 0)
580		tst_resm(TPASS, "id of the semaphore set is correct");
581	else
582		tst_resm(TFAIL, "id of the semaphore set is incorrect");
583}
584
585void setup(void)
586{
587
588	tst_sig(FORK, DEF_HANDLER, cleanup);
589
590	TEST_PAUSE;
591
592	tst_tmpdir();
593
594	/* get an IPC resource key */
595	semkey = getipckey();
596
597	/* create a semaphore set with read and alter permissions */
598	sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
599	if (sem_id_1 == -1)
600		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
601}
602
603void cleanup(void)
604{
605	/* if it exists, remove the semaphore resource */
606	rm_sema(sem_id_1);
607
608	tst_rmdir();
609
610	TEST_CLEANUP;
611}
612