1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * msgrcv01 - test that msgrcv() receives the expected message
6 */
7
8 #include <string.h>
9 #include <sys/wait.h>
10 #include "tst_test.h"
11 #include "tst_safe_sysv_ipc.h"
12 #include "tst_clocks.h"
13 #include "libnewipc.h"
14
15 static key_t msgkey;
16 static int queue_id = -1, pid;
17 static struct buf {
18 long type;
19 char mtext[MSGSIZE];
20 } rcv_buf, snd_buf = {MSGTYPE, "hello"};
21
verify_msgrcv(void)22 static void verify_msgrcv(void)
23 {
24 struct msqid_ds qs_buf;
25 time_t before_rcv, after_rcv;
26
27 SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
28
29 before_rcv = tst_get_fs_timestamp();
30 TEST(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0));
31 if (TST_RET == -1) {
32 tst_res(TFAIL | TTERRNO, "msgrcv failed");
33 return;
34 }
35 after_rcv = tst_get_fs_timestamp();
36
37 if (strcmp(rcv_buf.mtext, snd_buf.mtext) == 0)
38 tst_res(TPASS, "message received(%s) = message sent(%s)",
39 rcv_buf.mtext, snd_buf.mtext);
40 else
41 tst_res(TFAIL, "message received(%s) != message sent(%s)",
42 rcv_buf.mtext, snd_buf.mtext);
43
44 SAFE_MSGCTL(queue_id, IPC_STAT, &qs_buf);
45 if (qs_buf.msg_cbytes == 0 && qs_buf.msg_qnum == 0)
46 tst_res(TPASS, "queue bytes and number of queues matched");
47 else
48 tst_res(TFAIL, "queue bytes or number of queues mismatched");
49 if (qs_buf.msg_lrpid == pid)
50 tst_res(TPASS, "PID of last msgrcv(2) matched");
51 else
52 tst_res(TFAIL, "PID of last msgrcv(2) mismatched");
53
54 if (qs_buf.msg_rtime >= before_rcv && qs_buf.msg_rtime <= after_rcv) {
55 tst_res(TPASS, "msg_rtime = %lu in [%lu, %lu]",
56 (unsigned long)qs_buf.msg_rtime,
57 (unsigned long)before_rcv, (unsigned long)after_rcv);
58 } else {
59 tst_res(TFAIL, "msg_rtime = %lu out of [%lu, %lu]",
60 (unsigned long)qs_buf.msg_rtime,
61 (unsigned long)before_rcv, (unsigned long)after_rcv);
62 }
63 }
64
setup(void)65 static void setup(void)
66 {
67 msgkey = GETIPCKEY();
68 queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
69 pid = getpid();
70 }
71
cleanup(void)72 static void cleanup(void)
73 {
74 if (queue_id != -1)
75 SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
76 }
77
78 static struct tst_test test = {
79 .setup = setup,
80 .cleanup = cleanup,
81 .test_all = verify_msgrcv,
82 .needs_tmpdir = 1,
83 };
84