1 /* 2 * Copyright (c) 2015 Dmitry V. Levin <ldv (at) altlinux.org> 3 * Copyright (c) 2015 Elvira Khabirova <lineprinter0 (at) gmail.com> 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 <stdio.h> 30 #include <errno.h> 31 #include <sys/shm.h> 32 33 int 34 main(void) 35 { 36 int rc, id; 37 struct shmid_ds ds; 38 39 id = shmget(IPC_PRIVATE, 1, 0600); 40 if (id < 0) 41 return 77; 42 printf("shmget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id); 43 44 if (shmctl(id, IPC_STAT, &ds)) 45 goto fail; 46 printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{shm_perm=\\{uid=%u, gid=%u, " 47 "mode=%#o, key=%u, cuid=%u, cgid=%u\\}, shm_segsz=%u, shm_cpid=%u, " 48 "shm_lpid=%u, shm_nattch=%u, shm_atime=%u, shm_dtime=%u, " 49 "shm_ctime=%u\\}\\) += 0\n", 50 id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid, 51 (unsigned) ds.shm_perm.mode, (unsigned) ds.shm_perm.__key, 52 (unsigned) ds.shm_perm.cuid, (unsigned) ds.shm_perm.cgid, 53 (unsigned) ds.shm_segsz, (unsigned) ds.shm_cpid, 54 (unsigned) ds.shm_lpid, (unsigned) ds.shm_nattch, 55 (unsigned) ds.shm_atime, (unsigned) ds.shm_dtime, 56 (unsigned) ds. shm_ctime); 57 58 int max = shmctl(0, SHM_INFO, &ds); 59 if (max < 0) 60 goto fail; 61 printf("shmctl\\(0, (IPC_64\\|)?SHM_INFO, %p\\) += %d\n", &ds, max); 62 63 rc = shmctl(id, SHM_STAT, &ds); 64 if (rc != id) { 65 /* 66 * In linux < v2.6.24-rc1 the first argument must be 67 * an index in the kernel's internal array. 68 */ 69 if (-1 != rc || EINVAL != errno) 70 goto fail; 71 printf("shmctl\\(%d, (IPC_64\\|)?SHM_STAT, %p\\) += -1 EINVAL \\(Invalid argument\\)\n", id, &ds); 72 } else { 73 printf("shmctl\\(%d, (IPC_64\\|)?SHM_STAT, %p\\) += %d\n", id, &ds, id); 74 } 75 76 rc = 0; 77 done: 78 if (shmctl(id, IPC_RMID, NULL) < 0) 79 return 1; 80 printf("shmctl\\(%d, (IPC_64\\|)?IPC_RMID, NULL\\) += 0\n", id); 81 return rc; 82 83 fail: 84 rc = 1; 85 goto done; 86 } 87