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
25 * semctl04 - test for EPERM error
28 * create a semaphore set without read or alter permissions
29 * get the user id for "nobody"
30 * fork a child process
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
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.
54 * 03/2001 - Written by Wayne Boyer
57 * test must be run as root
65 char *TCID
= "semctl04";
69 int exp_enos
[] = {EPERM
, 0}; /* 0 terminated list of expected errnos */
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 */
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");
105 if (waitpid(pid
, NULL
, 0) == -1) {
106 tst_resm(TBROK
, "waitpid() failed");
107 tst_resm(TINFO
, "waitpid() error = %d : %s", errno
,
111 /* if it exists, remove the semaphore resouce */
114 /* Remove the temporary directory */
123 * do_child() - make the TEST call as the child process
128 int lc
; /* loop counter */
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 */
139 for (i
=0; i
<TST_TOTAL
; i
++) {
141 if (TC
[i
] == IPC_SET
) {
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");
157 TEST_ERROR_LOG(TEST_ERRNO
);
161 tst_resm(TPASS
, "expected failure - errno ="
162 " %d : %s", TEST_ERRNO
,
163 strerror(TEST_ERRNO
));
166 tst_resm(TFAIL
, "unexpected error "
167 "- %d : %s", TEST_ERRNO
,
168 strerror(TEST_ERRNO
));
176 * setup() - performs all the ONE TIME setup for this test.
181 /* check for root as user id of process */
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 */
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.
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
220 * print timing stats if that option was specified.
221 * print errno log if that option was specified.
225 /* exit with return code appropriate for results */