unstack, sort: cleanup and improvement
[minix.git] / test / ipc / semctl / semctl04.c
blob871f6c4ec04756e868cc4b40511b37365949a0fc
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 * semctl04.c
24 * DESCRIPTION
25 * semctl04 - test for EPERM error
27 * ALGORITHM
28 * create a semaphore set without read or alter permissions
29 * get the user id for "nobody"
30 * fork a child process
31 * if child
32 * set the ID of the child process to that of "nobody"
33 * loop if that option was specified
34 * call semctl() with two different invalid cases
35 * check the errno value
36 * issue a PASS message if we get EPERM
37 * otherwise, the tests fails
38 * issue a FAIL message
39 * call cleanup
40 * if parent
41 * wait for child to exit
42 * remove the semaphore set
44 * USAGE: <for command-line>
45 * semctl04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
46 * where, -c n : Run n copies concurrently.
47 * -e : Turn on errno logging.
48 * -i n : Execute test n times.
49 * -I x : Execute test for x seconds.
50 * -P x : Pause for x seconds between iterations.
51 * -t : Turn on syscall timing.
53 * HISTORY
54 * 03/2001 - Written by Wayne Boyer
56 * RESTRICTIONS
57 * test must be run as root
60 #include "ipcsem.h"
62 #include <pwd.h>
63 #include <sys/wait.h>
65 char *TCID = "semctl04";
66 int TST_TOTAL = 2;
67 extern int Tst_count;
69 int exp_enos[] = {EPERM, 0}; /* 0 terminated list of expected errnos */
71 int sem_id_1 = -1;
73 uid_t ltp_uid;
74 char *ltp_user = "nobody";
76 int TC[] = {IPC_SET, IPC_RMID};
78 int main(int ac, char **av)
80 char *msg; /* message returned from parse_opts */
81 pid_t pid;
82 void do_child(void);
84 /* parse standard options */
85 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
86 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
89 setup(); /* global setup */
91 if ((pid = FORK_OR_VFORK()) == -1) {
92 tst_brkm(TBROK, cleanup, "could not fork");
95 if (pid == 0) { /* child */
96 /* set the user ID of the child to the non root user */
97 if (setuid(ltp_uid) == -1) {
98 tst_resm(TBROK, "setuid() failed");
99 exit(1);
102 do_child();
104 } else {
105 if (waitpid(pid, NULL, 0) == -1) {
106 tst_resm(TBROK, "waitpid() failed");
107 tst_resm(TINFO, "waitpid() error = %d : %s", errno,
108 strerror(errno));
111 /* if it exists, remove the semaphore resouce */
112 rm_sema(sem_id_1);
114 /* Remove the temporary directory */
115 tst_rmdir();
117 cleanup();
119 return(0);
123 * do_child() - make the TEST call as the child process
125 void
126 do_child(void)
128 int lc; /* loop counter */
129 int i;
130 union semun arg;
131 struct semid_ds perm;
133 /* The following loop checks looping state if -i option given */
135 for (lc = 0; TEST_LOOPING(lc); lc++) {
136 /* reset Tst_count in case we are looping */
137 Tst_count = 0;
139 for (i=0; i<TST_TOTAL; i++) {
141 if (TC[i] == IPC_SET) {
142 arg.buf = &perm;
143 memset(&perm, 0, sizeof perm);
144 perm.sem_perm.uid = getuid() + 1;
145 perm.sem_perm.gid = getgid() + 1;
146 perm.sem_perm.mode = 0666;
150 TEST(semctl(sem_id_1, 0, TC[i], arg));
152 if (TEST_RETURN != -1) {
153 tst_resm(TFAIL, "call succeeded unexpectedly");
154 continue;
157 TEST_ERROR_LOG(TEST_ERRNO);
159 switch(TEST_ERRNO) {
160 case EPERM:
161 tst_resm(TPASS, "expected failure - errno ="
162 " %d : %s", TEST_ERRNO,
163 strerror(TEST_ERRNO));
164 break;
165 default:
166 tst_resm(TFAIL, "unexpected error "
167 "- %d : %s", TEST_ERRNO,
168 strerror(TEST_ERRNO));
169 break;
176 * setup() - performs all the ONE TIME setup for this test.
178 void
179 setup(void)
181 /* check for root as user id of process */
182 check_root();
184 /* capture signals */
185 tst_sig(FORK, DEF_HANDLER, cleanup);
187 /* Set up the expected error numbers for -e option */
188 TEST_EXP_ENOS(exp_enos);
190 /* Pause if that option was specified */
191 TEST_PAUSE;
194 * Create a temporary directory and cd into it.
195 * This helps to ensure that a unique msgkey is created.
196 * See ../lib/libipc.c for more information.
198 tst_tmpdir();
200 /* get an IPC resource key */
201 semkey = getipckey();
203 /* create a semaphore set without read or alter permissions */
204 if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL)) == -1) {
205 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
208 /* get the userid for a non root user */
209 ltp_uid = getuserid(ltp_user);
213 * cleanup() - performs all the ONE TIME cleanup for this test at completion
214 * or premature exit.
216 void
217 cleanup(void)
220 * print timing stats if that option was specified.
221 * print errno log if that option was specified.
223 TEST_CLEANUP;
225 /* exit with return code appropriate for results */
226 tst_exit();