1 /*- 2 * Copyright (c) 1999 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 7 * NASA Ames Research Center. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 * 30 * Obtained from: $NetBSD: shmtest.c,v 1.3 2002/07/20 08:36:26 grant Exp $ 31 * $FreeBSD$ 32 */ 33 34 /* 35 * Test the SVID-compatible Shared Memory facility. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/ipc.h> 40 #include <sys/shm.h> 41 #include <sys/wait.h> 42 43 #include <err.h> 44 #include <errno.h> 45 #include <signal.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <time.h> 50 #include <unistd.h> 51 52 int main __P((int, char *[])); 53 void print_shmid_ds __P((struct shmid_ds *, mode_t)); 54 void sigsys_handler __P((int)); 55 void sigchld_handler __P((int)); 56 void cleanup __P((void)); 57 void receiver __P((void)); 58 void usage __P((void)); 59 60 const char *m_str = "The quick brown fox jumped over the lazy dog."; 61 62 int sender_shmid = -1; 63 pid_t child_pid; 64 65 key_t shmkey; 66 67 size_t pgsize; 68 69 int 70 main(argc, argv) 71 int argc; 72 char *argv[]; 73 { 74 struct sigaction sa; 75 struct shmid_ds s_ds; 76 sigset_t sigmask; 77 char *shm_buf; 78 79 if (argc != 2) 80 usage(); 81 82 /* 83 * Install a SIGSYS handler so that we can exit gracefully if 84 * System V Shared Memory support isn't in the kernel. 85 */ 86 sa.sa_handler = sigsys_handler; 87 sigemptyset(&sa.sa_mask); 88 sa.sa_flags = 0; 89 if (sigaction(SIGSYS, &sa, NULL) == -1) 90 err(1, "sigaction SIGSYS"); 91 92 /* 93 * Install and SIGCHLD handler to deal with all possible exit 94 * conditions of the receiver. 95 */ 96 sa.sa_handler = sigchld_handler; 97 sigemptyset(&sa.sa_mask); 98 sa.sa_flags = 0; 99 if (sigaction(SIGCHLD, &sa, NULL) == -1) 100 err(1, "sigaction SIGCHLD"); 101 102 pgsize = sysconf(_SC_PAGESIZE); 103 104 shmkey = ftok(argv[1], 4160); 105 106 /* 107 * Initialize child_pid to ourselves to that the cleanup function 108 * works before we create the receiver. 109 */ 110 child_pid = getpid(); 111 112 /* 113 * Make sure that when the sender exits, the message queue is 114 * removed. 115 */ 116 if (atexit(cleanup) == -1) 117 err(1, "atexit"); 118 119 if ((sender_shmid = shmget(shmkey, pgsize, IPC_CREAT | 0640)) == -1) 120 err(1, "shmget"); 121 122 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1) 123 err(1, "shmctl IPC_STAT"); 124 125 print_shmid_ds(&s_ds, 0640); 126 127 s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600; 128 129 if (shmctl(sender_shmid, IPC_SET, &s_ds) == -1) 130 err(1, "shmctl IPC_SET"); 131 132 memset(&s_ds, 0, sizeof(s_ds)); 133 134 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1) 135 err(1, "shmctl IPC_STAT"); 136 137 if ((s_ds.shm_perm.mode & 0777) != 0600) 138 err(1, "IPC_SET of mode didn't hold"); 139 140 print_shmid_ds(&s_ds, 0600); 141 142 if ((shm_buf = shmat(sender_shmid, NULL, 0)) == (void *) -1) 143 err(1, "sender: shmat"); 144 145 /* 146 * Write the test pattern into the shared memory buffer. 147 */ 148 strcpy(shm_buf, m_str); 149 150 switch ((child_pid = fork())) { 151 case -1: 152 err(1, "fork"); 153 /* NOTREACHED */ 154 155 case 0: 156 receiver(); 157 break; 158 159 default: 160 break; 161 } 162 163 /* 164 * Suspend forever; when we get SIGCHLD, the handler will exit. 165 */ 166 sigemptyset(&sigmask); 167 (void) sigsuspend(&sigmask); 168 169 /* 170 * ...and any other signal is an unexpected error. 171 */ 172 errx(1, "sender: received unexpected signal"); 173 } 174 175 void 176 sigsys_handler(signo) 177 int signo; 178 { 179 180 errx(1, "System V Shared Memory support is not present in the kernel"); 181 } 182 183 void 184 sigchld_handler(signo) 185 int signo; 186 { 187 struct shmid_ds s_ds; 188 int cstatus; 189 190 /* 191 * Reap the child; if it exited successfully, then the test passed! 192 */ 193 if (waitpid(child_pid, &cstatus, 0) != child_pid) 194 err(1, "waitpid"); 195 196 if (WIFEXITED(cstatus) == 0) 197 errx(1, "receiver exited abnormally"); 198 199 if (WEXITSTATUS(cstatus) != 0) 200 errx(1, "receiver exited with status %d", 201 WEXITSTATUS(cstatus)); 202 203 /* 204 * If we get here, the child has exited normally, and thus 205 * we should exit normally too. First, tho, we print out 206 * the final stats for the message queue. 207 */ 208 209 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1) 210 err(1, "shmctl IPC_STAT"); 211 212 print_shmid_ds(&s_ds, 0600); 213 214 exit(0); 215 } 216 217 void 218 cleanup() 219 { 220 221 /* 222 * If we're the sender, and it exists, remove the shared memory area. 223 */ 224 if (child_pid != 0 && sender_shmid != -1) { 225 if (shmctl(sender_shmid, IPC_RMID, NULL) == -1) 226 warn("shmctl IPC_RMID"); 227 } 228 } 229 230 void 231 print_shmid_ds(sp, mode) 232 struct shmid_ds *sp; 233 mode_t mode; 234 { 235 uid_t uid = geteuid(); 236 gid_t gid = getegid(); 237 238 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n", 239 sp->shm_perm.uid, sp->shm_perm.gid, 240 sp->shm_perm.cuid, sp->shm_perm.cgid, 241 sp->shm_perm.mode & 0777); 242 243 printf("segsz %lu, lpid %d, cpid %d, nattch %u\n", 244 (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid, 245 sp->shm_nattch); 246 247 printf("atime: %s", ctime(&sp->shm_atime)); 248 printf("dtime: %s", ctime(&sp->shm_dtime)); 249 printf("ctime: %s", ctime(&sp->shm_ctime)); 250 251 /* 252 * Sanity check a few things. 253 */ 254 255 if (sp->shm_perm.uid != uid || sp->shm_perm.cuid != uid) 256 errx(1, "uid mismatch"); 257 258 if (sp->shm_perm.gid != gid || sp->shm_perm.cgid != gid) 259 errx(1, "gid mismatch"); 260 261 if ((sp->shm_perm.mode & 0777) != mode) 262 errx(1, "mode mismatch"); 263 } 264 265 void 266 usage() 267 { 268 269 fprintf(stderr, "usage: %s keypath\n", getprogname()); 270 exit(1); 271 } 272 273 void 274 receiver() 275 { 276 int shmid; 277 void *shm_buf; 278 279 if ((shmid = shmget(shmkey, pgsize, 0)) == -1) 280 err(1, "receiver: shmget"); 281 282 if ((shm_buf = shmat(shmid, NULL, 0)) == (void *) -1) 283 err(1, "receiver: shmat"); 284 285 printf("%s\n", (const char *)shm_buf); 286 if (strcmp((const char *)shm_buf, m_str) != 0) 287 err(1, "receiver: data isn't correct"); 288 289 exit(0); 290 } 291