1 /* $NetBSD: semtest.c,v 1.3 2001/02/19 22:44:41 cgd Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 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. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Test the SVID-compatible Semaphore facility. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/ipc.h> 46 #include <sys/sem.h> 47 #include <sys/wait.h> 48 49 #include <err.h> 50 #include <errno.h> 51 #include <limits.h> 52 #include <signal.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 59 int main(int, char *[]); 60 void print_semid_ds(struct semid_ds *, mode_t); 61 void sigsys_handler(int); 62 void sigchld_handler(int); 63 void cleanup(void); 64 void waiter(void); 65 66 int sender_semid = -1; 67 pid_t child_pid; 68 int child_count; 69 volatile sig_atomic_t signal_was_sigchld; 70 71 key_t semkey; 72 73 char keyname[] = "/tmp/msgtestXXXXXXXX"; 74 75 int verbose; 76 77 union mysemun { 78 int val; /* value for SETVAL */ 79 struct semid_ds *buf; /* buffer for IPC_{STAT,SET} */ 80 u_short *array; /* array for GETALL & SETALL */ 81 }; 82 83 int 84 main(argc, argv) 85 int argc; 86 char *argv[]; 87 { 88 struct sigaction sa; 89 union mysemun sun; 90 struct semid_ds s_ds; 91 sigset_t sigmask; 92 int i; 93 94 int fd, ch; 95 96 if ((fd = mkstemp(keyname)) < 0) 97 err(1, "mkstemp"); 98 99 close(fd); 100 101 while ((ch = getopt(argc, argv, "v")) != -1) { 102 switch (ch) { 103 case 'v': 104 verbose = 1; 105 break; 106 default: 107 fprintf(stderr, "Usage: semtest [-v]\n"); 108 exit(1); 109 } 110 } 111 112 /* 113 * Install a SIGSYS handler so that we can exit gracefully if 114 * System V Semaphore support isn't in the kernel. 115 */ 116 sa.sa_handler = sigsys_handler; 117 sigemptyset(&sa.sa_mask); 118 sa.sa_flags = 0; 119 if (sigaction(SIGSYS, &sa, NULL) == -1) 120 err(1, "sigaction SIGSYS"); 121 122 /* 123 * Install and SIGCHLD handler to deal with all possible exit 124 * conditions of the receiver. 125 */ 126 sa.sa_handler = sigchld_handler; 127 sigemptyset(&sa.sa_mask); 128 sa.sa_flags = 0; 129 if (sigaction(SIGCHLD, &sa, NULL) == -1) 130 err(1, "sigaction SIGCHLD"); 131 132 semkey = ftok(keyname, arc4random() & INT_MAX); 133 134 /* 135 * Initialize child_pid to ourselves to that the cleanup function 136 * works before we create the receiver. 137 */ 138 child_pid = getpid(); 139 140 /* 141 * Make sure that when the sender exits, the message queue is 142 * removed. 143 */ 144 if (atexit(cleanup) == -1) 145 err(1, "atexit"); 146 147 sender_semid = semget(semkey, 1, IPC_CREAT | IPC_EXCL | 0640); 148 if (sender_semid == -1) 149 err(1, "semget"); 150 151 152 sun.buf = &s_ds; 153 if (semctl(sender_semid, 0, IPC_STAT, sun) == -1) 154 err(1, "semctl IPC_STAT"); 155 156 if (verbose) 157 print_semid_ds(&s_ds, 0640); 158 159 s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600; 160 161 sun.buf = &s_ds; 162 if (semctl(sender_semid, 0, IPC_SET, sun) == -1) 163 err(1, "semctl IPC_SET"); 164 165 memset(&s_ds, 0, sizeof(s_ds)); 166 167 sun.buf = &s_ds; 168 if (semctl(sender_semid, 0, IPC_STAT, sun) == -1) 169 err(1, "semctl IPC_STAT"); 170 171 if ((s_ds.sem_perm.mode & 0777) != 0600) 172 err(1, "IPC_SET of mode didn't hold"); 173 174 if (verbose) 175 print_semid_ds(&s_ds, 0600); 176 177 for (child_count = 0; child_count < 5; child_count++) { 178 switch ((child_pid = fork())) { 179 case -1: 180 err(1, "fork"); 181 /* NOTREACHED */ 182 183 case 0: 184 waiter(); 185 break; 186 187 default: 188 break; 189 } 190 } 191 192 /* 193 * Wait for all of the waiters to be attempting to acquire the 194 * semaphore. 195 */ 196 for (;;) { 197 i = semctl(sender_semid, 0, GETNCNT); 198 if (i == -1) 199 err(1, "semctl GETNCNT"); 200 if (i == 5) 201 break; 202 } 203 204 /* 205 * Now set the thundering herd in motion by initializing the 206 * semaphore to the value 1. 207 */ 208 sun.val = 1; 209 if (semctl(sender_semid, 0, SETVAL, sun) == -1) 210 err(1, "sender: semctl SETVAL to 1"); 211 212 /* 213 * Suspend forever; when we get SIGCHLD, the handler will exit. 214 */ 215 sigemptyset(&sigmask); 216 for (;;) { 217 (void) sigsuspend(&sigmask); 218 if (signal_was_sigchld) 219 signal_was_sigchld = 0; 220 else 221 break; 222 } 223 224 /* 225 * ...and any other signal is an unexpected error. 226 */ 227 errx(1, "sender: received unexpected signal"); 228 } 229 230 void 231 sigsys_handler(signo) 232 int signo; 233 { 234 235 errx(1, "System V Semaphore support is not present in the kernel"); 236 } 237 238 void 239 sigchld_handler(signo) 240 int signo; 241 { 242 union mysemun sun; 243 struct semid_ds s_ds; 244 int cstatus; 245 246 /* 247 * Reap the child; if it exited successfully, then we're on the 248 * right track! 249 */ 250 if (wait(&cstatus) == -1) 251 err(1, "wait"); 252 253 if (WIFEXITED(cstatus) == 0) 254 errx(1, "receiver exited abnormally"); 255 256 if (WEXITSTATUS(cstatus) != 0) 257 errx(1, "receiver exited with status %d", 258 WEXITSTATUS(cstatus)); 259 260 /* 261 * If we get here, the child has exited normally, and we should 262 * decrement the child count. If the child_count reaches 0, we 263 * should exit. 264 */ 265 266 sun.buf = &s_ds; 267 if (semctl(sender_semid, 0, IPC_STAT, sun) == -1) 268 err(1, "semctl IPC_STAT"); 269 270 if (verbose) 271 print_semid_ds(&s_ds, 0600); 272 273 if (--child_count != 0) { 274 signal_was_sigchld = 1; 275 return; 276 } 277 278 exit(0); 279 } 280 281 void 282 cleanup() 283 { 284 285 /* 286 * If we're the sender, and it exists, remove the message queue. 287 */ 288 if (child_pid != 0 && sender_semid != -1) { 289 if (semctl(sender_semid, 0, IPC_RMID) == -1) 290 warn("semctl IPC_RMID"); 291 } 292 remove(keyname); 293 } 294 295 void 296 print_semid_ds(sp, mode) 297 struct semid_ds *sp; 298 mode_t mode; 299 { 300 uid_t uid = geteuid(); 301 gid_t gid = getegid(); 302 303 printf("PERM: uid %u, gid %u, cuid %u, cgid %u, mode 0%o\n", 304 sp->sem_perm.uid, sp->sem_perm.gid, 305 sp->sem_perm.cuid, sp->sem_perm.cgid, 306 sp->sem_perm.mode & 0777); 307 308 printf("nsems %u\n", sp->sem_nsems); 309 310 printf("otime: %s", ctime(&sp->sem_otime)); 311 printf("ctime: %s", ctime(&sp->sem_ctime)); 312 313 /* 314 * Sanity check a few things. 315 */ 316 317 if (sp->sem_perm.uid != uid || sp->sem_perm.cuid != uid) 318 errx(1, "uid mismatch"); 319 320 if (sp->sem_perm.gid != gid || sp->sem_perm.cgid != gid) 321 errx(1, "gid mismatch"); 322 323 if ((sp->sem_perm.mode & 0777) != mode) 324 errx(1, "mode mismatch %o != %o", 325 (sp->sem_perm.mode & 0777), mode); 326 } 327 328 void 329 waiter() 330 { 331 struct sembuf s; 332 int semid; 333 334 if ((semid = semget(semkey, 1, 0)) == -1) 335 err(1, "waiter: semget"); 336 337 /* 338 * Attempt to acquire the semaphore. 339 */ 340 s.sem_num = 0; 341 s.sem_op = -1; 342 s.sem_flg = SEM_UNDO; 343 344 if (semop(semid, &s, 1) == -1) 345 err(1, "waiter: semop -1"); 346 347 if (verbose) 348 printf("WOO! GOT THE SEMAPHORE!\n"); 349 sleep(1); 350 351 /* 352 * Release the semaphore and exit. 353 */ 354 s.sem_num = 0; 355 s.sem_op = 1; 356 s.sem_flg = SEM_UNDO; 357 358 if (semop(semid, &s, 1) == -1) 359 err(1, "waiter: semop +1"); 360 361 exit(0); 362 } 363