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
29 #include <stdio.h>
30 #include <errno.h>
31 #include <sys/msg.h>
32
33 int
main(void)34 main(void)
35 {
36 int rc, id;
37 struct msqid_ds ds;
38
39 id = msgget(IPC_PRIVATE, 0600);
40 if (id < 0)
41 return 77;
42 printf("msgget\\(IPC_PRIVATE, 0600\\) += %d\n", id);
43
44 if (msgctl(id, IPC_STAT, &ds))
45 goto fail;
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);
57
58 int max = msgctl(0, MSG_INFO, &ds);
59 if (max < 0)
60 goto fail;
61 printf("msgctl\\(0, (IPC_64\\|)?MSG_INFO, %p\\) += %d\n", &ds, max);
62
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;
71 printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += -1 EINVAL \\(Invalid argument\\)\n", id, &ds);
72 } else {
73 printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += %d\n", id, &ds, id);
74 }
75
76 rc = 0;
77 done:
78 if (msgctl(id, IPC_RMID, NULL) < 0)
79 return 1;
80 printf("msgctl\\(%d, (IPC_64\\|)?IPC_RMID, NULL\\) += 0\n", id);
81 return rc;
82
83 fail:
84 rc = 1;
85 goto done;
86 }
87