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 * shmdt01 - check that shared memory is detached correctly
28 * create a shared memory resource of size sizeof(int)
29 * attach it to the current process and give it a value
30 * call shmdt() using the TEST macro
31 * check the return code
32 * if failure, issue a FAIL message.
34 * if doing functionality testing
35 * attempt to write a value to the shared memory address
36 * this should generate a SIGSEGV which will be caught in
39 * issue a PASS message
41 * issue a FAIL message
44 * USAGE: <for command-line>
45 * shmdt01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
46 * where, -c n : Run n copies concurrently.
47 * -f : Turn off functionality Testing.
48 * -I x : Execute test for x seconds.
49 * -P x : Pause for x seconds between iterations.
50 * -t : Turn on syscall timing.
53 * 03/2001 - Written by Wayne Boyer
62 char *TCID
= "shmdt01";
70 int *shared
; /* variable to use for shared memory attach */
75 int main(int ac
, char **av
)
77 int lc
; /* loop counter */
78 char *msg
; /* message returned from parse_opts */
79 void check_functionality(void);
81 /* parse standard options */
82 if ((msg
= parse_opts(ac
, av
, (option_t
*)NULL
, NULL
)) != (char *)NULL
){
83 tst_brkm(TBROK
, cleanup
, "OPTION PARSING ERROR - %s", msg
);
86 setup(); /* global setup */
88 /* The following loop checks looping state if -i option given */
90 for (lc
= 0; TEST_LOOPING(lc
); lc
++) {
91 /* reset Tst_count in case we are looping */
95 * Use TEST macro to make the shmdt() call
98 TEST(shmdt((const void *)shared
));
100 if (TEST_RETURN
== -1) {
101 tst_resm(TFAIL
, "%s call failed - errno = %d : %s",
102 TCID
, TEST_ERRNO
, strerror(TEST_ERRNO
));
104 if (STD_FUNCTIONAL_TEST
) {
105 check_functionality();
107 tst_resm(TPASS
, "call succeeded");
111 /* reattach the shared memory segment in case we are looping */
112 shared
= (int *)shmat(shm_id_1
, 0, 0);
115 tst_brkm(TBROK
, cleanup
, "memory reattach failed");
118 /* also reset pass */
129 * check_functionality() - make sure the memory is detached correctly
132 check_functionality(void)
134 /* stat the shared memory segment */
135 if (shmctl(shm_id_1
, IPC_STAT
, &buf
) == -1) {
136 tst_resm(TINFO
, "error = %d : %s", errno
, strerror(errno
));
137 tst_brkm(TBROK
, cleanup
, "could not stat in signal handler");
140 if (buf
.shm_nattch
!= 0) {
141 tst_resm(TFAIL
, "# of attaches is incorrect");
146 * Try writing to the shared memory. This should generate a
147 * SIGSEGV which will be caught below.
149 * This is wrapped by the sigsetjmp() call that will take care of
150 * restoring the program's context in an elegant way in conjunction
151 * with the call to siglongjmp() in the signal handler.
153 * An attempt to do the assignment without using the sigsetjmp()
154 * and siglongjmp() calls will result in an infinite loop. Program
155 * control is returned to the assignment statement after the execution
156 * of the signal handler and another SIGSEGV will be generated.
159 if (sigsetjmp(env
, 1) == 0) {
164 tst_resm(TPASS
, "shared memory detached correctly");
166 tst_resm(TFAIL
, "shared memory was not detached correctly");
176 /* if we have received a SIGSEGV, we are almost done */
177 if (sig
== SIGSEGV
) {
178 /* set the global variable and jump back */
182 tst_brkm(TBROK
, cleanup
, "received an unexpected signal");
187 * setup() - performs all the ONE TIME setup for this test.
192 /* capture signals */
194 tst_sig(NOFORK
, sighandler
, cleanup
);
196 /* Pause if that option was specified */
200 * Create a temporary directory and cd into it.
201 * This helps to ensure that a unique msgkey is created.
202 * See ../lib/libipc.c for more information.
206 /* get an IPC resource key */
207 shmkey
= getipckey();
209 /* create a shared memory resource with read and write permissions */
210 if ((shm_id_1
= shmget(shmkey
, INT_SIZE
, SHM_RW
| IPC_CREAT
|
212 tst_brkm(TBROK
, cleanup
, "Failed to create shared memory "
213 "resource in setup()");
216 /* attach the shared memory segment */
217 shared
= (int *)shmat(shm_id_1
, 0, 0);
220 tst_brkm(TBROK
, cleanup
, "Couldn't attach shared memory");
223 /* give a value to the shared memory integer */
228 * cleanup() - performs all the ONE TIME cleanup for this test at completion
234 /* if it exists, delete the shared memory resource */
237 /* Remove the temporary directory */
241 * print timing stats if that option was specified.
242 * print errno log if that option was specified.
246 /* exit with return code appropriate for results */