1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 03/2001 - Written by Wayne Boyer
4 * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * Test that IPC_STAT command succeeds and the the buffer is filled with
23 * correct data.
24 */
25 #include <errno.h>
26
27 #include "tst_test.h"
28 #include "tst_safe_sysv_ipc.h"
29 #include "libnewipc.h"
30
31 static int msg_id = -1;
32 static time_t creat_time;
33 static key_t msgkey;
34 static uid_t uid;
35 static gid_t gid;
36 unsigned short mode = 0660;
37
verify_msgctl(void)38 static void verify_msgctl(void)
39 {
40 struct msqid_ds buf;
41
42 memset(&buf, 'a', sizeof(buf));
43 TEST(msgctl(msg_id, IPC_STAT, &buf));
44
45 if (TST_RET != 0) {
46 tst_res(TFAIL | TTERRNO, "msgctl() returned %li", TST_RET);
47 return;
48 }
49
50 tst_res(TPASS, "msgctl(IPC_STAT)");
51
52 if (buf.msg_stime == 0)
53 tst_res(TPASS, "msg_stime = 0");
54 else
55 tst_res(TFAIL, "msg_stime = %lu", (unsigned long)buf.msg_stime);
56
57 if (buf.msg_rtime == 0)
58 tst_res(TPASS, "msg_rtime = 0");
59 else
60 tst_res(TFAIL, "msg_rtime = %lu", (unsigned long)buf.msg_rtime);
61
62 if (buf.msg_ctime <= creat_time && buf.msg_ctime >= creat_time - 2) {
63 tst_res(TPASS, "msg_ctime = %lu, expected %lu",
64 (unsigned long)buf.msg_ctime, (unsigned long)creat_time);
65 } else {
66 tst_res(TFAIL, "msg_ctime = %lu, expected %lu",
67 (unsigned long)buf.msg_ctime, (unsigned long)creat_time);
68 }
69
70 if (buf.msg_qnum == 0)
71 tst_res(TPASS, "msg_qnum = 0");
72 else
73 tst_res(TFAIL, "msg_qnum = %li", (long)buf.msg_qnum);
74
75 if (buf.msg_qbytes > 0)
76 tst_res(TPASS, "msg_qbytes = %li", (long)buf.msg_qbytes);
77 else
78 tst_res(TFAIL, "msg_qbytes = %li", (long)buf.msg_qbytes);
79
80 if (buf.msg_lspid == 0)
81 tst_res(TPASS, "msg_lspid = 0");
82 else
83 tst_res(TFAIL, "msg_lspid = %u", (unsigned)buf.msg_lspid);
84
85 if (buf.msg_lrpid == 0)
86 tst_res(TPASS, "msg_lrpid = 0");
87 else
88 tst_res(TFAIL, "msg_lrpid = %u", (unsigned)buf.msg_lrpid);
89
90 if (buf.msg_perm.__key == msgkey) {
91 tst_res(TPASS, "msg_perm.__key == %u", (unsigned)msgkey);
92 } else {
93 tst_res(TFAIL, "msg_perm.__key == %u, expected %u",
94 (unsigned)buf.msg_perm.__key, (unsigned)msgkey);
95 }
96
97 if (buf.msg_perm.uid == uid) {
98 tst_res(TPASS, "msg_perm.uid = %u", (unsigned)uid);
99 } else {
100 tst_res(TFAIL, "msg_perm.uid = %u, expected %u",
101 (unsigned)buf.msg_perm.uid, (unsigned)uid);
102 }
103
104 if (buf.msg_perm.gid == gid) {
105 tst_res(TPASS, "msg_perm.gid = %u", (unsigned)gid);
106 } else {
107 tst_res(TFAIL, "msg_perm.gid = %u, expected %u",
108 (unsigned)buf.msg_perm.gid, (unsigned)gid);
109 }
110
111 if (buf.msg_perm.cuid == uid) {
112 tst_res(TPASS, "msg_perm.cuid = %u", (unsigned)uid);
113 } else {
114 tst_res(TFAIL, "msg_perm.cuid = %u, expected %u",
115 (unsigned)buf.msg_perm.cuid, (unsigned)uid);
116 }
117
118 if (buf.msg_perm.cgid == gid) {
119 tst_res(TPASS, "msg_perm.cgid = %u", (unsigned)gid);
120 } else {
121 tst_res(TFAIL, "msg_perm.cgid = %u, expected %u",
122 (unsigned)buf.msg_perm.cgid, (unsigned)gid);
123 }
124
125 if ((buf.msg_perm.mode & MODE_MASK) == (mode & MODE_MASK)) {
126 tst_res(TPASS, "msg_perm.mode = 0%ho", mode & MODE_MASK);
127 } else {
128 tst_res(TFAIL, "msg_perm.mode = 0%ho, expected %hx",
129 buf.msg_perm.mode, (mode & MODE_MASK));
130 }
131 }
132
setup(void)133 static void setup(void)
134 {
135 msgkey = GETIPCKEY();
136
137 msg_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW | mode);
138 time(&creat_time);
139
140 uid = geteuid();
141 gid = getegid();
142 }
143
cleanup(void)144 static void cleanup(void)
145 {
146 if (msg_id >= 0)
147 SAFE_MSGCTL(msg_id, IPC_RMID, NULL);
148 }
149
150 static struct tst_test test = {
151 .setup = setup,
152 .cleanup = cleanup,
153 .test_all = verify_msgctl,
154 .needs_tmpdir = 1
155 };
156