1 /* $NetBSD: shmtest.c,v 1.4 2005/02/06 06:05:20 perry Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Test the SVID-compatible Shared Memory facility.
37 #include <sys/param.h>
51 int main(int, char *[]);
52 void print_shmid_ds(struct shmid_ds
*, mode_t
);
53 void sigsys_handler(int);
54 void sigchld_handler(int);
59 const char *m_str
= "The quick brown fox jumped over the lazy dog.";
61 int sender_shmid
= -1;
82 * Install a SIGSYS handler so that we can exit gracefully if
83 * System V Shared Memory support isn't in the kernel.
85 sa
.sa_handler
= sigsys_handler
;
86 sigemptyset(&sa
.sa_mask
);
88 if (sigaction(SIGSYS
, &sa
, NULL
) == -1)
89 err(1, "sigaction SIGSYS");
92 * Install and SIGCHLD handler to deal with all possible exit
93 * conditions of the receiver.
95 sa
.sa_handler
= sigchld_handler
;
96 sigemptyset(&sa
.sa_mask
);
98 if (sigaction(SIGCHLD
, &sa
, NULL
) == -1)
99 err(1, "sigaction SIGCHLD");
101 pgsize
= sysconf(_SC_PAGESIZE
);
103 shmkey
= ftok(argv
[1], 4160);
106 * Initialize child_pid to ourselves to that the cleanup function
107 * works before we create the receiver.
109 child_pid
= getpid();
112 * Make sure that when the sender exits, the message queue is
115 if (atexit(cleanup
) == -1)
118 if ((sender_shmid
= shmget(shmkey
, pgsize
, IPC_CREAT
| 0640)) == -1)
121 if (shmctl(sender_shmid
, IPC_STAT
, &s_ds
) == -1)
122 err(1, "shmctl IPC_STAT");
124 print_shmid_ds(&s_ds
, 0640);
126 s_ds
.shm_perm
.mode
= (s_ds
.shm_perm
.mode
& ~0777) | 0600;
128 if (shmctl(sender_shmid
, IPC_SET
, &s_ds
) == -1)
129 err(1, "shmctl IPC_SET");
131 memset(&s_ds
, 0, sizeof(s_ds
));
133 if (shmctl(sender_shmid
, IPC_STAT
, &s_ds
) == -1)
134 err(1, "shmctl IPC_STAT");
136 if ((s_ds
.shm_perm
.mode
& 0777) != 0600)
137 err(1, "IPC_SET of mode didn't hold");
139 print_shmid_ds(&s_ds
, 0600);
141 if ((shm_buf
= shmat(sender_shmid
, NULL
, 0)) == (void *) -1)
142 err(1, "sender: shmat");
145 * Write the test pattern into the shared memory buffer.
147 strcpy(shm_buf
, m_str
);
149 switch ((child_pid
= fork())) {
163 * Suspend forever; when we get SIGCHLD, the handler will exit.
165 sigemptyset(&sigmask
);
166 (void) sigsuspend(&sigmask
);
169 * ...and any other signal is an unexpected error.
171 errx(1, "sender: received unexpected signal");
175 sigsys_handler(signo
)
179 errx(1, "System V Shared Memory support is not present in the kernel");
183 sigchld_handler(signo
)
186 struct shmid_ds s_ds
;
190 * Reap the child; if it exited successfully, then the test passed!
192 if (waitpid(child_pid
, &cstatus
, 0) != child_pid
)
195 if (WIFEXITED(cstatus
) == 0)
196 errx(1, "receiver exited abnormally");
198 if (WEXITSTATUS(cstatus
) != 0)
199 errx(1, "receiver exited with status %d",
200 WEXITSTATUS(cstatus
));
203 * If we get here, the child has exited normally, and thus
204 * we should exit normally too. First, tho, we print out
205 * the final stats for the message queue.
208 if (shmctl(sender_shmid
, IPC_STAT
, &s_ds
) == -1)
209 err(1, "shmctl IPC_STAT");
211 print_shmid_ds(&s_ds
, 0600);
221 * If we're the sender, and it exists, remove the shared memory area.
223 if (child_pid
!= 0 && sender_shmid
!= -1) {
224 if (shmctl(sender_shmid
, IPC_RMID
, NULL
) == -1)
225 warn("shmctl IPC_RMID");
230 print_shmid_ds(sp
, mode
)
234 uid_t uid
= geteuid();
235 gid_t gid
= getegid();
237 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
238 sp
->shm_perm
.uid
, sp
->shm_perm
.gid
,
239 sp
->shm_perm
.cuid
, sp
->shm_perm
.cgid
,
240 sp
->shm_perm
.mode
& 0777);
242 printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
243 (u_long
)sp
->shm_segsz
, sp
->shm_lpid
, sp
->shm_cpid
,
246 printf("atime: %s", ctime(&sp
->shm_atime
));
247 printf("dtime: %s", ctime(&sp
->shm_dtime
));
248 printf("ctime: %s", ctime(&sp
->shm_ctime
));
251 * Sanity check a few things.
254 if (sp
->shm_perm
.uid
!= uid
|| sp
->shm_perm
.cuid
!= uid
)
255 errx(1, "uid mismatch");
257 if (sp
->shm_perm
.gid
!= gid
|| sp
->shm_perm
.cgid
!= gid
)
258 errx(1, "gid mismatch");
260 if ((sp
->shm_perm
.mode
& 0777) != mode
)
261 errx(1, "mode mismatch");
268 fprintf(stderr
, "usage: %s keypath\n", getprogname());
278 if ((shmid
= shmget(shmkey
, pgsize
, 0)) == -1)
279 err(1, "receiver: shmget");
281 if ((shm_buf
= shmat(shmid
, NULL
, 0)) == (void *) -1)
282 err(1, "receiver: shmat");
284 printf("%s\n", (const char *)shm_buf
);
285 if (strcmp((const char *)shm_buf
, m_str
) != 0)
286 err(1, "receiver: data isn't correct");