1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * This is a basic test about MSG_COPY flag.
7f08c3bdfSopenharmony_ci * This flag was added in 3.8 for the implementation of the kernel checkpoint
8f08c3bdfSopenharmony_ci * restore facility and is available only if the kernel was built with the
9f08c3bdfSopenharmony_ci * CONFIG_CHECKPOINT_RESTORE option.
10f08c3bdfSopenharmony_ci * On old kernel without this support, it only ignores this flag and doesn't
11f08c3bdfSopenharmony_ci * report ENOSYS/EINVAL error. The CONFIG_CHECKPOINT_RESTORE has existed
12f08c3bdfSopenharmony_ci * before kernel 3.8.
13f08c3bdfSopenharmony_ci * So for using this flag, kernel should greater than 3.8 and enable
14f08c3bdfSopenharmony_ci * CONFIG_CHECKPOINT_RESTORE together.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci * 1)msgrcv(2) fails and sets errno to EINVAL if IPC_NOWAIT was not specified
17f08c3bdfSopenharmony_ci *   in msgflag.
18f08c3bdfSopenharmony_ci * 2)msgrcv(2) fails and sets errno to EINVAL if IPC_EXCEPT was specified
19f08c3bdfSopenharmony_ci *   in msgflag.
20f08c3bdfSopenharmony_ci * 3)msgrcv(2) fails and set errno to ENOMSG if IPC_NOWAIT and MSG_COPY were
21f08c3bdfSopenharmony_ci *  specified in msgflg and the queue contains less than msgtyp messages.
22f08c3bdfSopenharmony_ci */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#define  _GNU_SOURCE
25f08c3bdfSopenharmony_ci#include <string.h>
26f08c3bdfSopenharmony_ci#include <sys/wait.h>
27f08c3bdfSopenharmony_ci#include <pwd.h>
28f08c3bdfSopenharmony_ci#include "tst_test.h"
29f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
30f08c3bdfSopenharmony_ci#include "libnewipc.h"
31f08c3bdfSopenharmony_ci#include "lapi/msg.h"
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic key_t msgkey;
34f08c3bdfSopenharmony_cistatic int queue_id = -1;
35f08c3bdfSopenharmony_cistatic struct buf {
36f08c3bdfSopenharmony_ci	long type;
37f08c3bdfSopenharmony_ci	char mtext[MSGSIZE];
38f08c3bdfSopenharmony_ci} rcv_buf, snd_buf = {MSGTYPE, "hello"};
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic struct tcase {
41f08c3bdfSopenharmony_ci	int exp_err;
42f08c3bdfSopenharmony_ci	int msg_flag;
43f08c3bdfSopenharmony_ci	int msg_type;
44f08c3bdfSopenharmony_ci	char *message;
45f08c3bdfSopenharmony_ci} tcases[] = {
46f08c3bdfSopenharmony_ci	{EINVAL, 0, MSGTYPE,
47f08c3bdfSopenharmony_ci	"EINVAL for MSG_COPY without IPC_NOWAIT"},
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	{EINVAL, MSG_EXCEPT, MSGTYPE,
50f08c3bdfSopenharmony_ci	"EINVAL for MSG_COPY with MSG_EXCEPT"},
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	{ENOMSG, IPC_NOWAIT, 2,
53f08c3bdfSopenharmony_ci	"ENOMSG with IPC_NOWAIT and MSG_COPY but with less than msgtyp messages"},
54f08c3bdfSopenharmony_ci};
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void verify_msgrcv(unsigned int n)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s", tc->message);
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(msgrcv(queue_id, &rcv_buf, MSGSIZE, tc->msg_type, MSG_COPY | tc->msg_flag), tc->exp_err,
63f08c3bdfSopenharmony_ci		"msgrcv(%i, %p, %i, %i, %i)", queue_id, &rcv_buf, MSGSIZE, tc->msg_type, MSG_COPY | tc->msg_flag);
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic void setup(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
69f08c3bdfSopenharmony_ci	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
70f08c3bdfSopenharmony_ci	SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic void cleanup(void)
74f08c3bdfSopenharmony_ci{
75f08c3bdfSopenharmony_ci	if (queue_id != -1)
76f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
77f08c3bdfSopenharmony_ci}
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_cistatic struct tst_test test = {
80f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
81f08c3bdfSopenharmony_ci	.needs_root = 1,
82f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
83f08c3bdfSopenharmony_ci		"CONFIG_CHECKPOINT_RESTORE",
84f08c3bdfSopenharmony_ci		NULL
85f08c3bdfSopenharmony_ci	},
86f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
87f08c3bdfSopenharmony_ci	.test = verify_msgrcv,
88f08c3bdfSopenharmony_ci	.setup = setup,
89f08c3bdfSopenharmony_ci	.cleanup = cleanup,
90f08c3bdfSopenharmony_ci};
91