tar: use utime() to restore timestamps
[minix.git] / test / ipc / shmat / shmat02.c
blobccaca9e90fded6322a626ce33245afe2350124de
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 * shmat02.c
24 * DESCRIPTION
25 * shmat02 - check for EINVAL and EACCES errors
27 * ALGORITHM
28 * loop if that option was specified
29 * call shmat() using three invalid test cases
30 * check the errno value
31 * issue a PASS message if we get EINVAL or EACCES
32 * otherwise, the tests fails
33 * issue a FAIL message
34 * call cleanup
36 * USAGE: <for command-line>
37 * shmat02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
38 * where, -c n : Run n copies concurrently.
39 * -e : Turn on errno logging.
40 * -i n : Execute test n times.
41 * -I x : Execute test for x seconds.
42 * -P x : Pause for x seconds between iterations.
43 * -t : Turn on syscall timing.
45 * HISTORY
46 * 03/2001 - Written by Wayne Boyer
48 * RESTRICTIONS
49 * Must be ran as non-root
52 #include "ipcshm.h"
53 #include <pwd.h>
55 char *TCID = "shmat02";
56 int TST_TOTAL = 3;
57 extern int Tst_count;
58 char nobody_uid[] = "nobody";
59 struct passwd *ltpuser;
62 int exp_enos[] = {EINVAL, EACCES, 0}; /* 0 terminated list of */
63 /* expected errnos */
65 int shm_id_1 = -1;
66 int shm_id_2 = -1;
67 int shm_id_3 = -1;
69 void *addr; /* for result of shmat-call */
71 #define NADDR 0x40FFFEE5 /* a non alligned address value */
72 struct test_case_t {
73 int *shmid;
74 void *addr;
75 int error;
76 } TC[] = {
77 /* EINVAL - the shared memory ID is not valid */
78 {&shm_id_1, 0, EINVAL},
80 /* EINVAL - the address is not page aligned and SHM_RND is not given */
81 {&shm_id_2, (void *)NADDR, EINVAL},
83 /* EACCES - the shared memory resource has no read/write permission */
84 {&shm_id_3, 0, EACCES}
87 int main(int ac, char **av)
89 int lc; /* loop counter */
90 char *msg; /* message returned from parse_opts */
91 int i;
93 /* parse standard options */
94 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
95 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
98 setup(); /* global setup */
100 /* The following loop checks looping state if -i option given */
102 for (lc = 0; TEST_LOOPING(lc); lc++) {
103 /* reset Tst_count in case we are looping */
104 Tst_count = 0;
106 /* loop through the test cases */
107 for (i=0; i<TST_TOTAL; i++) {
109 * make the call using the TEST() macro - attempt
110 * various invalid shared memory attaches
112 errno = 0;
113 addr = shmat(*(TC[i].shmid), (const void *)TC[i].addr, 0);
114 TEST_ERRNO = errno;
116 if (addr != (void *)-1) {
117 tst_resm(TFAIL, "call succeeded unexpectedly");
118 continue;
121 TEST_ERROR_LOG(TEST_ERRNO);
123 if (TEST_ERRNO == TC[i].error) {
124 tst_resm(TPASS, "expected failure - errno = "
125 "%d : %s", TEST_ERRNO,
126 strerror(TEST_ERRNO));
127 } else {
128 tst_resm(TFAIL, "call failed with an "
129 "unexpected error - %d : %s",
130 TEST_ERRNO, strerror(TEST_ERRNO));
136 cleanup();
138 /*NOTREACHED*/
139 return(0);
143 * setup() - performs all the ONE TIME setup for this test.
145 void
146 setup(void)
148 /* Switch to nobody user for correct error code collection */
149 if (geteuid() != 0) {
150 tst_brkm(TBROK, tst_exit, "Test must be run as root");
153 /* capture signals */
154 tst_sig(NOFORK, DEF_HANDLER, cleanup);
156 /* Set up the expected error numbers for -e option */
157 TEST_EXP_ENOS(exp_enos);
159 /* Pause if that option was specified */
160 TEST_PAUSE;
163 * Create a temporary directory and cd into it.
164 * This helps to ensure that a unique msgkey is created.
165 * See ../lib/libipc.c for more information.
167 tst_tmpdir();
169 ltpuser = getpwnam(nobody_uid);
170 if (seteuid(ltpuser->pw_uid) == -1) {
171 tst_resm(TINFO, "setuid failed to "
172 "to set the effective uid to %d",
173 ltpuser->pw_uid);
174 perror("seteuid");
177 /* get an IPC resource key */
178 shmkey = getipckey();
180 /* create a shared memory resource with read and write permissions */
181 /* also post increment the shmkey for the next shmget call */
182 if ((shm_id_2 = shmget(shmkey++, INT_SIZE, SHM_RW | IPC_CREAT |
183 IPC_EXCL)) == -1) {
184 tst_brkm(TBROK, cleanup, "Failed to create shared memory "
185 "resource #1 in setup()");
188 /* create a shared memory resource without read and write permissions */
189 if ((shm_id_3 = shmget(shmkey, INT_SIZE, IPC_CREAT | IPC_EXCL)) == -1) {
190 tst_brkm(TBROK, cleanup, "Failed to create shared memory "
191 "resource #2 in setup()");
196 * cleanup() - performs all the ONE TIME cleanup for this test at completion
197 * or premature exit.
199 void
200 cleanup(void)
202 /* if they exist, remove the shared memory resources */
203 rm_shm(shm_id_2);
204 rm_shm(shm_id_3);
206 if (seteuid(0) == -1) {
207 tst_resm(TINFO, "setuid failed to "
208 "to set the effective uid to root");
209 perror("seteuid");
212 /* Remove the temporary directory */
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();