1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
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.
9 *
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.
14 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * shmat03.c
23 *
24 * DESCRIPTION
25 * shmat03 - test for EACCES error
26 *
27 * ALGORITHM
28 * create a shared memory segment with root only read & write permissions
29 * fork a child process
30 * if child
31 * set the ID of the child process to that of "nobody"
32 * loop if that option was specified
33 * call shmat() using the TEST() macro
34 * check the errno value
35 * issue a PASS message if we get EACCES
36 * otherwise, the tests fails
37 * issue a FAIL message
38 * call cleanup
39 * if parent
40 * wait for child to exit
41 * remove the shared memory segment
42 *
43 * USAGE: <for command-line>
44 * shmat03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
45 * where, -c n : Run n copies concurrently.
46 * -e : Turn on errno logging.
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.
51 *
52 * HISTORY
53 * 03/2001 - Written by Wayne Boyer
54 *
55 * RESTRICTIONS
56 * test must be run at root
57 */
58
59 #include "ipcshm.h"
60
61 char *TCID = "shmat03";
62 int TST_TOTAL = 1;
63
64 int shm_id_1 = -1;
65
66 void *addr; /* for result of shmat-call */
67
68 uid_t ltp_uid;
69 char *ltp_user = "nobody";
70
71 static void do_child(void);
72
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 int pid;
76
77 tst_parse_opts(ac, av, NULL, NULL);
78
79 setup(); /* global setup */
80
81 pid = FORK_OR_VFORK();
82 if (pid == -1)
83 tst_brkm(TBROK, cleanup, "could not fork");
84
85 if (pid == 0) { /* child */
86 /* set the user ID of the child to the non root user */
87 if (setuid(ltp_uid) == -1) {
88 perror("setuid() failed");
89 exit(1);
90 }
91
92 do_child();
93
94 } else { /* parent */
95 /* wait for the child to return */
96 if (waitpid(pid, NULL, 0) == -1)
97 tst_brkm(TBROK, cleanup, "waitpid failed");
98
99 /* if it exists, remove the shared memory resource */
100 rm_shm(shm_id_1);
101
102 tst_rmdir();
103 }
104
105 cleanup();
106 tst_exit();
107 }
108
109 /*
110 * do_child - make the TEST call as the child process
111 */
do_child(void)112 static void do_child(void)
113 {
114 int lc;
115
116 /* The following loop checks looping state if -i option given */
117
118 for (lc = 0; TEST_LOOPING(lc); lc++) {
119 /* reset tst_count in case we are looping */
120 tst_count = 0;
121
122 /*
123 * use TEST macro to make the call
124 */
125 errno = 0;
126 addr = shmat(shm_id_1, NULL, 0);
127 TEST_ERRNO = errno;
128
129 if (addr != (char *)-1) {
130 tst_resm(TFAIL, "call succeeded unexpectedly");
131 continue;
132 }
133
134 switch (TEST_ERRNO) {
135 case EACCES:
136 tst_resm(TPASS | TTERRNO, "expected failure");
137 break;
138 default:
139 tst_resm(TFAIL | TTERRNO,
140 "call failed with an unexpected error");
141 break;
142 }
143 }
144 }
145
146 /*
147 * setup() - performs all the ONE TIME setup for this test.
148 */
setup(void)149 void setup(void)
150 {
151 tst_require_root();
152
153 tst_sig(FORK, DEF_HANDLER, cleanup);
154
155 TEST_PAUSE;
156
157 /*
158 * Create a temporary directory and cd into it.
159 * This helps to ensure that a unique msgkey is created.
160 * See ../lib/libipc.c for more information.
161 */
162 tst_tmpdir();
163
164 /* get an IPC resource key */
165 shmkey = getipckey();
166
167 /* create a shared memory segment with read and write permissions */
168 shm_id_1 = shmget(shmkey, SHM_SIZE, SHM_RW | IPC_CREAT | IPC_EXCL);
169 if (shm_id_1 == -1)
170 tst_brkm(TBROK, cleanup, "Failed to create shared memory "
171 "segment in setup");
172
173 /* get the userid for a non root user */
174 ltp_uid = getuserid(ltp_user);
175 }
176
177 /*
178 * cleanup() - performs all the ONE TIME cleanup for this test at completion
179 * or premature exit.
180 */
cleanup(void)181 void cleanup(void)
182 {
183
184 }
185