1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * msgrcv error test for EINTR.
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci#include <sys/types.h>
9f08c3bdfSopenharmony_ci#include <sys/wait.h>
10f08c3bdfSopenharmony_ci#include <signal.h>
11f08c3bdfSopenharmony_ci#include <stdlib.h>
12f08c3bdfSopenharmony_ci#include "tst_test.h"
13f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
14f08c3bdfSopenharmony_ci#include "libnewipc.h"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic key_t msgkey;
17f08c3bdfSopenharmony_cistatic int queue_id = -1;
18f08c3bdfSopenharmony_cistatic struct buf {
19f08c3bdfSopenharmony_ci	long type;
20f08c3bdfSopenharmony_ci	char mtext[MSGSIZE];
21f08c3bdfSopenharmony_ci} rcv_buf;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic void sighandler(int sig)
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	if (sig == SIGHUP)
26f08c3bdfSopenharmony_ci		return;
27f08c3bdfSopenharmony_ci	else
28f08c3bdfSopenharmony_ci		_exit(TBROK);
29f08c3bdfSopenharmony_ci}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void verify_msgrcv(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0), EINTR,
34f08c3bdfSopenharmony_ci		"msgrcv(%i, %p, %d, 1, 0)", queue_id, &rcv_buf, MSGSIZE);
35f08c3bdfSopenharmony_ci}
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic void do_test(void)
38f08c3bdfSopenharmony_ci{
39f08c3bdfSopenharmony_ci	int pid;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
42f08c3bdfSopenharmony_ci	if (pid == 0) {
43f08c3bdfSopenharmony_ci		verify_msgrcv();
44f08c3bdfSopenharmony_ci		exit(0);
45f08c3bdfSopenharmony_ci	}
46f08c3bdfSopenharmony_ci	TST_PROCESS_STATE_WAIT(pid, 'S', 0);
47f08c3bdfSopenharmony_ci	SAFE_KILL(pid, SIGHUP);
48f08c3bdfSopenharmony_ci	tst_reap_children();
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
54f08c3bdfSopenharmony_ci	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
55f08c3bdfSopenharmony_ci	SAFE_SIGNAL(SIGHUP, sighandler);
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void cleanup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	if (queue_id != -1)
61f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
62f08c3bdfSopenharmony_ci}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_cistatic struct tst_test test = {
65f08c3bdfSopenharmony_ci	.setup = setup,
66f08c3bdfSopenharmony_ci	.cleanup = cleanup,
67f08c3bdfSopenharmony_ci	.test_all = do_test,
68f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
69f08c3bdfSopenharmony_ci	.forks_child = 1,
70f08c3bdfSopenharmony_ci};
71