sort: add -x hex sort feature back
[minix.git] / test / ipc / semop / semop02.c
bloba842bfa7c9fb62db87f4920f1490ef0fc7f13dc2
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 * semop02.c
24 * DESCRIPTION
25 * semop02 - test for E2BIG, EACCES, EFAULT and EINVAL errors
27 * ALGORITHM
28 * create a semaphore set with read and alter permissions
29 * create a semaphore set without read and alter permissions
30 * loop if that option was specified
31 * call semop with five different invalid cases
32 * check the errno value
33 * issue a PASS message if we get E2BIG, EACCES, EFAULT or EINVAL
34 * otherwise, the tests fails
35 * issue a FAIL message
36 * call cleanup
38 * USAGE: <for command-line>
39 * semop02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
40 * where, -c n : Run n copies concurrently.
41 * -e : Turn on errno logging.
42 * -i n : Execute test n times.
43 * -I x : Execute test for x seconds.
44 * -P x : Pause for x seconds between iterations.
45 * -t : Turn on syscall timing.
47 * HISTORY
48 * 03/2001 - Written by Wayne Boyer
50 * RESTRICTIONS
51 * none
53 #include <pwd.h>
55 #include "ipcsem.h"
57 char *TCID = "semop02";
58 int TST_TOTAL = 4;
59 extern int Tst_count;
61 int exp_enos[] = {E2BIG, EACCES, EFAULT, EINVAL, 0};
63 int sem_id_1 = -1; /* a semaphore set with read & alter permissions */
64 int sem_id_2 = -1; /* a semaphore set without read & alter permissions */
65 int bad_id = -1;
66 char nobody_uid[] = "nobody";
67 struct passwd *ltpuser;
69 struct sembuf s_buf[PSEMS];
71 int badbuf = -1;
73 #define NSOPS 5 /* a resonable number of operations */
74 #define BIGOPS 1024 /* a value that is too large for the number */
75 /* of semop operations that are permitted */
77 struct test_case_t {
78 int *semid; /* the semaphore id */
79 struct sembuf *t_sbuf; /* the first in an array of sembuf structures */
80 unsigned t_ops; /* the number of elements in the above array */
81 int error; /* the expected error number */
82 } TC[] = {
83 /* E2BIG - the number of operations is too big */
84 {&sem_id_1, (struct sembuf *)&s_buf, BIGOPS, E2BIG},
86 /* EACCES - the semaphore set has no access permission */
87 {&sem_id_2, (struct sembuf *)&s_buf, NSOPS, EACCES},
89 /* EINVAL - the number of elments (t_ops) is 0 */
90 {&sem_id_1, (struct sembuf *)&s_buf, 0, EINVAL},
92 /* EINVAL - the semaphore set doesn't exist */
93 {&bad_id, (struct sembuf *)&s_buf, NSOPS, EINVAL}
96 int main(int ac, char **av)
98 int lc; /* loop counter */
99 char *msg; /* message returned from parse_opts */
100 int i;
102 /* parse standard options */
103 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
104 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
107 setup(); /* global setup */
109 /* The following loop checks looping state if -i option given */
111 for (lc = 0; TEST_LOOPING(lc); lc++) {
112 /* reset Tst_count in case we are looping */
113 Tst_count = 0;
115 for (i=0; i<TST_TOTAL; i++) {
117 * use the TEST macro to make the call
120 TEST(semop(*(TC[i].semid), TC[i].t_sbuf, TC[i].t_ops));
122 if (TEST_RETURN != -1) {
123 tst_resm(TFAIL, "call succeeded unexpectedly");
124 continue;
127 TEST_ERROR_LOG(TEST_ERRNO);
129 if (TEST_ERRNO == TC[i].error) {
130 tst_resm(TPASS, "expected failure - errno = "
131 "%d : %s", TEST_ERRNO,
132 strerror(TEST_ERRNO));
133 } else {
134 tst_resm(TFAIL, "unexpected error - "
135 "%d : %s", TEST_ERRNO,
136 strerror(TEST_ERRNO));
141 cleanup();
143 /*NOTREACHED*/
144 return(0);
148 * setup() - performs all the ONE TIME setup for this test.
150 void
151 setup(void)
153 /* Switch to nobody user for correct error code collection */
154 if (geteuid() != 0) {
155 tst_brkm(TBROK, tst_exit, "Test must be run as root");
158 /* capture signals */
159 tst_sig(NOFORK, DEF_HANDLER, cleanup);
161 /* Set up the expected error numbers for -e option */
162 TEST_EXP_ENOS(exp_enos);
164 /* Pause if that option was specified */
165 TEST_PAUSE;
168 * Create a temporary directory and cd into it.
169 * This helps to ensure that a unique msgkey is created.
170 * See ../lib/libipc.c for more information.
172 tst_tmpdir();
174 ltpuser = getpwnam(nobody_uid);
175 if (seteuid(ltpuser->pw_uid) == -1) {
176 tst_resm(TINFO, "setreuid failed to "
177 "to set the effective uid to %d",
178 ltpuser->pw_uid);
179 perror("setreuid");
182 /* get an IPC resource key */
183 semkey = getipckey();
185 /* create a semaphore set with read and alter permissions */
186 if ((sem_id_1 =
187 semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
188 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
191 /* increment the semkey */
192 semkey += 1;
194 /* create a semaphore set without read and alter permissions */
195 if ((sem_id_2 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL)) == -1) {
196 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
201 * cleanup() - performs all the ONE TIME cleanup for this test at completion
202 * or premature exit.
204 void
205 cleanup(void)
207 /* if they exist, remove the semaphore resources */
208 rm_sema(sem_id_1);
209 rm_sema(sem_id_2);
211 /* Remove the temporary directory */
212 seteuid(getuid());
213 tst_rmdir();
216 * print timing stats if that option was specified.
217 * print errno log if that option was specified.
219 TEST_CLEANUP;
221 /* exit with return code appropriate for results */
222 tst_exit();