1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2001
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/*
21 * NAME
22 *	semget01.c
23 *
24 * DESCRIPTION
25 *	semget01 - test that semget() correclty creates a semaphore set
26 *
27 * ALGORITHM
28 *	loop if that option was specified
29 *	call semget() to create the semaphore set
30 *	check the return code
31 *	  if failure, issue a FAIL message.
32 *	otherwise,
33 *	  if doing functionality testing
34 *		stat the semaphore set
35 *		if the number of primitive semaphores is correct and
36 *		   the semaphore uid == the process uid
37 *	  	then,
38 *			issue a PASS message
39 *		otherwise
40 *			issue a FAIL message
41 *	call cleanup
42 *
43 * USAGE:  <for command-line>
44 *  semget01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
45 *     where,  -c n : Run n copies concurrently.
46 *             -f   : Turn off functionality Testing.
47 *	       -i n : Execute test n times.
48 *	       -I x : Execute test for x seconds.
49 *	       -P x : Pause for x seconds between iterations.
50 *	       -t   : Turn on syscall timing.
51 *
52 * HISTORY
53 *	03/2001 - Written by Wayne Boyer
54 *
55 * RESTRICTIONS
56 *	none
57 */
58
59#include "../lib/ipcsem.h"
60
61char *TCID = "semget01";
62int TST_TOTAL = 1;
63
64int sem_id_1 = -1;
65
66int main(int ac, char **av)
67{
68	int lc;
69	void check_functionality(void);
70
71	tst_parse_opts(ac, av, NULL, NULL);
72
73	setup();		/* global setup */
74
75	/* The following loop checks looping state if -i option given */
76
77	for (lc = 0; TEST_LOOPING(lc); lc++) {
78		/* reset tst_count in case we are looping */
79		tst_count = 0;
80
81		/*
82		 * Use TEST macro to make the call
83		 */
84
85		TEST(semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA));
86
87		if (TEST_RETURN == -1) {
88			tst_resm(TFAIL, "%s call failed - errno = %d : %s",
89				 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
90		} else {
91			/* get the semaphore ID */
92			sem_id_1 = TEST_RETURN;
93
94			check_functionality();
95		}
96
97		/*
98		 * remove the semaphore that was created and mark the ID
99		 * as invalid.
100		 */
101		if (sem_id_1 != -1) {
102			rm_sema(sem_id_1);
103			sem_id_1 = -1;
104		}
105	}
106
107	cleanup();
108
109	tst_exit();
110}
111
112/*
113 * check_functionality() - check the functionality of the tested system call.
114 */
115void check_functionality(void)
116{
117	struct semid_ds semary;
118	union semun un_arg;	/* union defined in ipcsem.h */
119
120	/* STAT the semaphore */
121	un_arg.buf = &semary;
122	if (semctl(sem_id_1, 0, IPC_STAT, un_arg) == -1) {
123		tst_brkm(TBROK, cleanup, "Could not stat the semaphore");
124		return;
125	}
126
127	if (un_arg.buf->sem_nsems != PSEMS) {
128		tst_resm(TFAIL, "# of semaphores in set != # given to create");
129		return;
130	}
131
132	if (un_arg.buf->sem_perm.cuid != geteuid()) {
133		tst_resm(TFAIL, "semaphore uid != process uid");
134		return;
135	}
136
137	tst_resm(TPASS, "basic semaphore values are okay");
138}
139
140/*
141 * setup() - performs all the ONE TIME setup for this test.
142 */
143void setup(void)
144{
145
146	tst_sig(NOFORK, DEF_HANDLER, cleanup);
147
148	TEST_PAUSE;
149
150	/*
151	 * Create a temporary directory and cd into it.
152	 * This helps to ensure that a unique msgkey is created.
153	 * See ../lib/libipc.c for more information.
154	 */
155	tst_tmpdir();
156
157	/* get an IPC resource key */
158	semkey = getipckey();
159}
160
161/*
162 * cleanup() - performs all the ONE TIME cleanup for this test at completion
163 * 	       or premature exit.
164 */
165void cleanup(void)
166{
167	/* if it exists, remove the semaphore resouce */
168	rm_sema(sem_id_1);
169
170	tst_rmdir();
171
172}
173