1 /* 2 * Copyright (c) 2015 Andreas Schwab <schwab (at) suse.de> 3 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv (at) altlinux.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "tests.h" 30 #include <errno.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <sys/sem.h> 34 35 #include "xlat.h" 36 #include "xlat/resource_flags.h" 37 38 union semun { 39 int val; /* Value for SETVAL */ 40 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ 41 unsigned short *array; /* Array for GETALL, SETALL */ 42 struct seminfo *__buf; /* Buffer for IPC_INFO 43 (Linux-specific) */ 44 }; 45 46 static int id = -1; 47 48 static void 49 cleanup(void) 50 { 51 semctl(id, 0, IPC_RMID, 0); 52 printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_RMID, \\[?NULL\\]?\\) += 0\n", 53 id); 54 id = -1; 55 } 56 57 int 58 main(void) 59 { 60 static const key_t private_key = 61 (key_t) (0xffffffff00000000ULL | IPC_PRIVATE); 62 static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL; 63 static const int bogus_semid = 0xfdb97531; 64 static const int bogus_semnum = 0xeca86420; 65 static const int bogus_size = 0xdec0ded1; 66 static const int bogus_flags = 0xface1e55; 67 static const int bogus_cmd = 0xdeadbeef; 68 static const unsigned long bogus_arg = 69 (unsigned long) 0xbadc0dedfffffaceULL; 70 71 int rc; 72 union semun un; 73 struct semid_ds ds; 74 struct seminfo info; 75 76 rc = semget(bogus_key, bogus_size, bogus_flags); 77 printf("semget\\(%#llx, %d, %s%s%s%#x\\|%#04o\\) += %s\n", 78 zero_extend_signed_to_ull(bogus_key), bogus_size, 79 IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "", 80 IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "", 81 IPC_NOWAIT & bogus_flags ? "IPC_NOWAIT\\|" : "", 82 bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | IPC_NOWAIT), 83 bogus_flags & 0777, sprintrc_grep(rc)); 84 85 id = semget(private_key, 1, 0600); 86 if (id < 0) 87 perror_msg_and_skip("semget"); 88 printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id); 89 atexit(cleanup); 90 91 rc = semctl(bogus_semid, bogus_semnum, bogus_cmd, bogus_arg); 92 #define SEMCTL_BOGUS_ARG_FMT "(%#lx|\\[(%#lx|NULL)\\]|NULL)" 93 printf("semctl\\(%d, %d, (IPC_64\\|)?%#x /\\* SEM_\\?\\?\\? \\*/" 94 ", " SEMCTL_BOGUS_ARG_FMT "\\) += %s\n", 95 bogus_semid, bogus_semnum, bogus_cmd, 96 bogus_arg, bogus_arg, sprintrc_grep(rc)); 97 98 un.buf = &ds; 99 if (semctl(id, 0, IPC_STAT, un)) 100 perror_msg_and_skip("semctl IPC_STAT"); 101 printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_STAT, \\[?%p\\]?\\) += 0\n", 102 id, &ds); 103 104 un.__buf = &info; 105 rc = semctl(0, 0, SEM_INFO, un); 106 printf("semctl\\(0, 0, (IPC_64\\|)?SEM_INFO, \\[?%p\\]?\\) += %s\n", 107 &info, sprintrc_grep(rc)); 108 109 un.buf = &ds; 110 rc = semctl(id, 0, SEM_STAT, un); 111 printf("semctl\\(%d, 0, (IPC_64\\|)?SEM_STAT, \\[?%p\\]?\\) += %s\n", 112 id, &ds, sprintrc_grep(rc)); 113 114 return 0; 115 } 116