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