1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2015   Author: Gabriellla Schmidt <gsc@bruker.de>
4 *                      Modify: Li Wang <liwang@redhat.com>
5 * A regression test for:
6 *      commit e7ca2552369c1dfe0216c626baf82c3d83ec36bb
7 *      Author: Mateusz Guzik <mguzik@redhat.com>
8 *      Date:   Mon Jan 27 17:07:11 2014 -0800
9 *
10 *           ipc: fix compat msgrcv with negative msgtyp
11 *
12 * Reproduce:
13 *
14 *      32-bit application using the msgrcv() system call
15 *      gives the error message:
16 *
17 *           msgrcv: No message of desired type
18 *
19 *      If this progarm is compiled as 64-bit application it works.
20 */
21
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/ipc.h>
27#include <sys/msg.h>
28#include "tst_test.h"
29#include "tst_safe_sysv_ipc.h"
30#include "libnewipc.h"
31
32static long mtype = 121;
33static key_t msgkey;
34static int queue_id = -1;
35static struct mbuf {
36	long mtype;
37	char mtext[16];
38} rcv_buf, snd_buf = {121, "hello"};
39
40static void verify_msgrcv(void)
41{
42	memset(&rcv_buf, 0, sizeof(rcv_buf));
43	SAFE_MSGSND(queue_id, &snd_buf, sizeof(snd_buf.mtext), IPC_NOWAIT);
44
45	TEST(msgrcv(queue_id, &rcv_buf, sizeof(rcv_buf.mtext), -mtype, IPC_NOWAIT | MSG_NOERROR));
46	if (TST_RET == -1) {
47		tst_res(TFAIL, "Bug: No message of desired type.");
48		return;
49	}
50
51	if (rcv_buf.mtype != mtype) {
52		tst_res(TFAIL, "found mtype %ld, expected %ld", rcv_buf.mtype, mtype);
53		return;
54	}
55	if ((size_t)TST_RET != sizeof(snd_buf.mtext)) {
56		tst_res(TFAIL, "received %zi, expected %zu", (size_t)TST_RET, sizeof(snd_buf.mtext));
57		return;
58	}
59
60	tst_res(TPASS, "No regression found.");
61}
62
63static void setup(void)
64{
65	msgkey = GETIPCKEY();
66	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
67}
68
69static void cleanup(void)
70{
71	if (queue_id != -1)
72		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
73}
74
75static struct tst_test test = {
76	.needs_tmpdir = 1,
77	.setup = setup,
78	.cleanup = cleanup,
79	.test_all = verify_msgrcv,
80	.tags = (const struct tst_tag[]) {
81		{"linux-git", "e7ca2552369c"},
82		{}
83	}
84};
85