sort: add -x hex sort feature back
[minix.git] / test / ipc / shmdt / shmdt01.c
blob1fd30f85249c77d751ff13d6d404ac9f427a4bd7
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 * shmdt01.c
24 * DESCRIPTION
25 * shmdt01 - check that shared memory is detached correctly
27 * ALGORITHM
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.
33 * otherwise,
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
37 * the signal handler
38 * if correct,
39 * issue a PASS message
40 * otherwise
41 * issue a FAIL message
42 * call cleanup
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.
52 * HISTORY
53 * 03/2001 - Written by Wayne Boyer
55 * RESTRICTIONS
56 * none
59 #include <setjmp.h>
60 #include "ipcshm.h"
62 char *TCID = "shmdt01";
63 int TST_TOTAL = 1;
64 extern int Tst_count;
66 void sighandler(int);
67 struct shmid_ds buf;
69 int shm_id_1 = -1;
70 int *shared; /* variable to use for shared memory attach */
71 int new;
72 int pass = 0;
73 sigjmp_buf env;
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 */
92 Tst_count = 0;
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));
103 } else {
104 if (STD_FUNCTIONAL_TEST) {
105 check_functionality();
106 } else {
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);
114 if (*shared == -1) {
115 tst_brkm(TBROK, cleanup, "memory reattach failed");
118 /* also reset pass */
119 pass = 0;
122 cleanup();
124 /*NOTREACHED*/
125 return(0);
129 * check_functionality() - make sure the memory is detached correctly
131 void
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");
142 return;
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) {
160 *shared = 2;
163 if (pass) {
164 tst_resm(TPASS, "shared memory detached correctly");
165 } else {
166 tst_resm(TFAIL, "shared memory was not detached correctly");
171 * sighandler()
173 void
174 sighandler(sig)
176 /* if we have received a SIGSEGV, we are almost done */
177 if (sig == SIGSEGV) {
178 /* set the global variable and jump back */
179 pass = 1;
180 siglongjmp(env, 1);
181 } else {
182 tst_brkm(TBROK, cleanup, "received an unexpected signal");
187 * setup() - performs all the ONE TIME setup for this test.
189 void
190 setup(void)
192 /* capture signals */
194 tst_sig(NOFORK, sighandler, cleanup);
196 /* Pause if that option was specified */
197 TEST_PAUSE;
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.
204 tst_tmpdir();
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 |
211 IPC_EXCL)) == -1) {
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);
219 if (*shared == -1) {
220 tst_brkm(TBROK, cleanup, "Couldn't attach shared memory");
223 /* give a value to the shared memory integer */
224 *shared = 4;
228 * cleanup() - performs all the ONE TIME cleanup for this test at completion
229 * or premature exit.
231 void
232 cleanup(void)
234 /* if it exists, delete the shared memory resource */
235 rm_shm(shm_id_1);
237 /* Remove the temporary directory */
238 tst_rmdir();
241 * print timing stats if that option was specified.
242 * print errno log if that option was specified.
244 TEST_CLEANUP;
246 /* exit with return code appropriate for results */
247 tst_exit();