Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org> |
| 3 | * Copyright (c) 2015 Elvira Khabirova <lineprinter0@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 | |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 31 | #include <sys/msg.h> |
| 32 | |
| 33 | int |
| 34 | main(void) |
| 35 | { |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 36 | int rc, id; |
| 37 | struct msqid_ds ds; |
| 38 | |
| 39 | id = msgget(IPC_PRIVATE, 0600); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 40 | if (id < 0) |
| 41 | return 77; |
| 42 | printf("msgget\\(IPC_PRIVATE, 0600\\) += %d\n", id); |
| 43 | |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 44 | if (msgctl(id, IPC_STAT, &ds)) |
| 45 | goto fail; |
Elvira Khabirova | d0a6187 | 2015-08-18 02:01:26 +0300 | [diff] [blame] | 46 | printf("msgctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{msg_perm=\\{uid=%u, gid=%u, " |
| 47 | "mode=%#o, key=%u, cuid=%u, cgid=%u\\}, msg_stime=%u, msg_rtime=%u, " |
| 48 | "msg_ctime=%u, msg_qnum=%u, msg_qbytes=%u, msg_lspid=%u, " |
| 49 | "msg_lrpid=%u\\}\\) += 0\n", |
| 50 | id, (unsigned) ds.msg_perm.uid, (unsigned) ds.msg_perm.gid, |
| 51 | (unsigned) ds.msg_perm.mode, (unsigned) ds.msg_perm.__key, |
| 52 | (unsigned) ds.msg_perm.cuid, (unsigned) ds.msg_perm.cgid, |
| 53 | (unsigned) ds.msg_stime, (unsigned) ds.msg_rtime, |
| 54 | (unsigned) ds.msg_ctime, (unsigned) ds.msg_qnum, |
| 55 | (unsigned) ds.msg_qbytes, (unsigned) ds.msg_lspid, |
| 56 | (unsigned) ds.msg_lrpid); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 57 | |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 58 | int max = msgctl(0, MSG_INFO, &ds); |
| 59 | if (max < 0) |
| 60 | goto fail; |
Andreas Schwab | 3aa45f3 | 2015-03-11 17:47:56 +0100 | [diff] [blame] | 61 | printf("msgctl\\(0, (IPC_64\\|)?MSG_INFO, %p\\) += %d\n", &ds, max); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 62 | |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 63 | rc = msgctl(id, MSG_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; |
Andreas Schwab | 3aa45f3 | 2015-03-11 17:47:56 +0100 | [diff] [blame] | 71 | printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += -1 EINVAL \\(Invalid argument\\)\n", id, &ds); |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 72 | } else { |
Andreas Schwab | 3aa45f3 | 2015-03-11 17:47:56 +0100 | [diff] [blame] | 73 | printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += %d\n", id, &ds, id); |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 74 | } |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 75 | |
| 76 | rc = 0; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 77 | done: |
Dmitry V. Levin | 2370d53 | 2015-07-20 10:11:33 +0000 | [diff] [blame] | 78 | if (msgctl(id, IPC_RMID, NULL) < 0) |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 79 | return 1; |
Dmitry V. Levin | 2370d53 | 2015-07-20 10:11:33 +0000 | [diff] [blame] | 80 | printf("msgctl\\(%d, (IPC_64\\|)?IPC_RMID, NULL\\) += 0\n", id); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 81 | return rc; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 82 | |
| 83 | fail: |
| 84 | rc = 1; |
| 85 | goto done; |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 86 | } |