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