1 /* 2 * Copyright (c) 2015 Elvira Khabirova <lineprinter0 (at) gmail.com> 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/shm.h> 34 35 #include "xlat.h" 36 #include "xlat/shm_resource_flags.h" 37 38 static int id = -1; 39 40 static void 41 cleanup(void) 42 { 43 shmctl(id, IPC_RMID, NULL); 44 printf("shmctl\\(%d, (IPC_64\\|)?IPC_RMID, NULL\\) += 0\n", id); 45 id = -1; 46 } 47 48 int 49 main(void) 50 { 51 static const key_t private_key = 52 (key_t) (0xffffffff00000000ULL | IPC_PRIVATE); 53 static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL; 54 static const int bogus_id = 0xdefaced1; 55 static const int bogus_cmd = 0xdefaced2; 56 static void * const bogus_addr = (void *) -1L; 57 static const size_t bogus_size = 58 /* 59 * musl sets size to SIZE_MAX if size argument is greater than 60 * PTRDIFF_MAX - musl/src/ipc/shmget.c 61 */ 62 #ifdef __GLIBC__ 63 (size_t) 0xdec0ded1dec0ded2ULL; 64 #else 65 (size_t) 0x1e55c0de5dec0dedULL; 66 #endif 67 static const int bogus_flags = 0xface1e55; 68 69 int rc; 70 struct shmid_ds ds; 71 72 rc = shmget(bogus_key, bogus_size, bogus_flags); 73 printf("shmget\\(%#llx, %zu, %s%s%s%#x\\|%#04o\\) += %s\n", 74 zero_extend_signed_to_ull(bogus_key), bogus_size, 75 IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "", 76 IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "", 77 SHM_HUGETLB & bogus_flags ? "SHM_HUGETLB\\|" : "", 78 bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | SHM_HUGETLB), 79 bogus_flags & 0777, sprintrc_grep(rc)); 80 81 id = shmget(private_key, 1, 0600); 82 if (id < 0) 83 perror_msg_and_skip("shmget"); 84 printf("shmget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id); 85 atexit(cleanup); 86 87 rc = shmctl(bogus_id, bogus_cmd, NULL); 88 printf("shmctl\\(%d, (IPC_64\\|)?%#x /\\* SHM_\\?\\?\\? \\*/, NULL\\)" 89 " += %s\n", bogus_id, bogus_cmd, sprintrc_grep(rc)); 90 91 rc = shmctl(bogus_id, IPC_STAT, bogus_addr); 92 printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, %p\\) += %s\n", 93 bogus_id, bogus_addr, sprintrc_grep(rc)); 94 95 if (shmctl(id, IPC_STAT, &ds)) 96 perror_msg_and_skip("shmctl IPC_STAT"); 97 printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{shm_perm=\\{uid=%u, gid=%u, " 98 "mode=%#o, key=%u, cuid=%u, cgid=%u\\}, shm_segsz=%u, shm_cpid=%u, " 99 "shm_lpid=%u, shm_nattch=%u, shm_atime=%u, shm_dtime=%u, " 100 "shm_ctime=%u\\}\\) += 0\n", 101 id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid, 102 (unsigned) ds.shm_perm.mode, (unsigned) ds.shm_perm.__key, 103 (unsigned) ds.shm_perm.cuid, (unsigned) ds.shm_perm.cgid, 104 (unsigned) ds.shm_segsz, (unsigned) ds.shm_cpid, 105 (unsigned) ds.shm_lpid, (unsigned) ds.shm_nattch, 106 (unsigned) ds.shm_atime, (unsigned) ds.shm_dtime, 107 (unsigned) ds. shm_ctime); 108 109 if (shmctl(id, IPC_SET, &ds)) 110 perror_msg_and_skip("shmctl IPC_SET"); 111 printf("shmctl\\(%d, (IPC_64\\|)?IPC_SET, \\{shm_perm=\\{uid=%u, gid=%u" 112 ", mode=%#o\\}, ...\\}\\) += 0\n", 113 id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid, 114 (unsigned) ds.shm_perm.mode); 115 116 rc = shmctl(0, SHM_INFO, &ds); 117 printf("shmctl\\(0, (IPC_64\\|)?SHM_INFO, %p\\) += %s\n", 118 &ds, sprintrc_grep(rc)); 119 120 rc = shmctl(id, SHM_STAT, &ds); 121 printf("shmctl\\(%d, (IPC_64\\|)?SHM_STAT, %p\\) += %s\n", 122 id, &ds, sprintrc_grep(rc)); 123 124 return 0; 125 } 126