remove extra mkfs.1
[minix.git] / test / ipc / semop / semop01.c
blob1574e14046776815a995309dbc18f9211728d065
1 /*
3 * Copyright (c) International Business Machines Corp., 2001
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
21 * NAME
22 * semop01.c
24 * DESCRIPTION
25 * semop01 - test that semop() basic functionality is correct
27 * ALGORITHM
28 * create a semaphore set and initialize some values
29 * loop if that option was specified
30 * call semop() to set values for the primitive semaphores
31 * check the return code
32 * if failure, issue a FAIL message.
33 * otherwise,
34 * if doing functionality testing
35 * get the semaphore values and compare with expected values
36 * if correct,
37 * issue a PASS message
38 * otherwise
39 * issue a FAIL message
40 * else issue a PASS message
41 * call cleanup
43 * USAGE: <for command-line>
44 * semop01 [-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.
52 * HISTORY
53 * 03/2001 - Written by Wayne Boyer
54 * 17/01/02 - Modified. Manoj Iyer, IBM Austin. TX. manjo@austin.ibm.com
55 * 4th argument to semctl() system call was modified according
56 * to man pages.
57 * In my opinion The test should not even have compiled but
58 * it was working due to some mysterious reason.
60 * RESTRICTIONS
61 * none
64 #include "ipcsem.h"
66 #define NSEMS 4 /* the number of primitive semaphores to test */
68 char *TCID = "semop01";
69 int TST_TOTAL = 1;
70 extern int Tst_count;
72 int sem_id_1 = -1; /* a semaphore set with read & alter permissions */
75 struct sembuf sops[PSEMS]; /* an array of sembuf structures */
78 int main(int ac, char **av)
80 union semun get_arr;
81 int lc; /* loop counter */
82 char *msg; /* message returned from parse_opts */
83 int i;
84 int fail = 0;
86 get_arr.array = malloc(sizeof(unsigned short int) * PSEMS);
87 /* parse standard options */
88 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
89 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
92 setup(); /* global setup */
94 /* The following loop checks looping state if -i option given */
96 for (lc = 0; TEST_LOOPING(lc); lc++) {
97 /* reset Tst_count in case we are looping */
98 Tst_count = 0;
101 * Use TEST macro to make the call
104 TEST(semop(sem_id_1, sops, NSEMS));
106 if (TEST_RETURN == -1) {
107 tst_resm(TFAIL, "%s call failed - errno = %d "
108 ": %s", TCID, TEST_ERRNO,
109 strerror(TEST_ERRNO));
110 } else {
111 if (STD_FUNCTIONAL_TEST) {
113 /* get the values and make sure they */
114 /* are the same as what was set */
115 if (semctl(sem_id_1, 0, GETALL, get_arr) ==
116 -1) {
117 tst_brkm(TBROK, cleanup, "semctl() "
118 "failed in functional test");
121 for (i=0; i<NSEMS; i++) {
122 if (get_arr.array[i] != i*i) {
123 fail = 1;
126 if (fail) {
127 tst_resm(TFAIL, "semaphore values"
128 " are not expected");
129 } else {
130 tst_resm(TPASS, "semaphore values"
131 " are correct");
134 } else {
135 tst_resm(TPASS, "call succeeded");
141 * clean up things in case we are looping
143 get_arr.val = 0;
144 for (i=0; i<NSEMS; i++) {
145 if(semctl(sem_id_1, i, SETVAL, get_arr) == -1) {
146 tst_brkm(TBROK, cleanup, "semctl failed");
151 cleanup();
153 /*NOTREACHED*/
154 return(0);
158 * setup() - performs all the ONE TIME setup for this test.
160 void
161 setup(void)
163 int i;
165 /* capture signals */
166 tst_sig(NOFORK, DEF_HANDLER, cleanup);
168 /* Pause if that option was specified */
169 TEST_PAUSE;
172 * Create a temporary directory and cd into it.
173 * This helps to ensure that a unique msgkey is created.
174 * See ../lib/libipc.c for more information.
176 tst_tmpdir();
178 /* get an IPC resource key */
179 semkey = getipckey();
181 /* create a semaphore set with read and alter permissions */
182 if ((sem_id_1 =
183 semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
184 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
187 /* set up some values for the first four primitive semaphores */
188 for (i=0; i<NSEMS; i++){
189 sops[i].sem_num = i;
190 sops[i].sem_op = i*i; /* 0, 1, 4, 9, */
191 sops[i].sem_flg = SEM_UNDO;
196 * cleanup() - performs all the ONE TIME cleanup for this test at completion
197 * or premature exit.
199 void
200 cleanup(void)
202 /* if it exists, remove the semaphore resouce */
203 rm_sema(sem_id_1);
205 /* Remove the temporary directory */
206 tst_rmdir();
209 * print timing stats if that option was specified.
210 * print errno log if that option was specified.
212 TEST_CLEANUP;
214 /* exit with return code appropriate for results */
215 tst_exit();