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
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Test for EEXIST, ENOENT, EACCES errors.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * -  msgget(2) fails if a message queue exists for key and msgflg
12f08c3bdfSopenharmony_ci *    specified both IPC_CREAT and IPC_EXCL.
13f08c3bdfSopenharmony_ci * -  msgget(2) fails if no message queue exists for key and msgflg
14f08c3bdfSopenharmony_ci *    did not specify IPC_CREAT.
15f08c3bdfSopenharmony_ci * -  msgget(2) fails if a message queue exists for key, but the
16f08c3bdfSopenharmony_ci *    calling process does not have permission to access the queue,
17f08c3bdfSopenharmony_ci *    and does not have the CAP_IPC_OWNER capability.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci#include <errno.h>
21f08c3bdfSopenharmony_ci#include <stdlib.h>
22f08c3bdfSopenharmony_ci#include <sys/types.h>
23f08c3bdfSopenharmony_ci#include <sys/ipc.h>
24f08c3bdfSopenharmony_ci#include <sys/msg.h>
25f08c3bdfSopenharmony_ci#include <pwd.h>
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include "tst_test.h"
28f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
29f08c3bdfSopenharmony_ci#include "libnewipc.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic key_t msgkey, msgkey1;
32f08c3bdfSopenharmony_cistatic int queue_id = -1;
33f08c3bdfSopenharmony_cistatic struct passwd *pw;
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic struct tcase {
36f08c3bdfSopenharmony_ci	int *key;
37f08c3bdfSopenharmony_ci	int flags;
38f08c3bdfSopenharmony_ci	int exp_err;
39f08c3bdfSopenharmony_ci	/*1: nobody expected  0: root expected */
40f08c3bdfSopenharmony_ci	int exp_user;
41f08c3bdfSopenharmony_ci} tcases[] = {
42f08c3bdfSopenharmony_ci	{&msgkey, IPC_CREAT | IPC_EXCL, EEXIST, 0},
43f08c3bdfSopenharmony_ci	{&msgkey1, IPC_PRIVATE, ENOENT, 0},
44f08c3bdfSopenharmony_ci	{&msgkey1, IPC_EXCL, ENOENT, 0},
45f08c3bdfSopenharmony_ci	{&msgkey, MSG_RD, EACCES, 1},
46f08c3bdfSopenharmony_ci	{&msgkey, MSG_WR, EACCES, 1},
47f08c3bdfSopenharmony_ci	{&msgkey, MSG_RW, EACCES, 1}
48f08c3bdfSopenharmony_ci};
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void verify_msgget(struct tcase *tc)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(msgget(*tc->key, tc->flags), tc->exp_err, "msgget(%i, %i)",
53f08c3bdfSopenharmony_ci		*tc->key, tc->flags);
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void do_test(unsigned int n)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	pid_t pid;
59f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	if (tc->exp_user == 0) {
62f08c3bdfSopenharmony_ci		verify_msgget(tc);
63f08c3bdfSopenharmony_ci	} else {
64f08c3bdfSopenharmony_ci		pid = SAFE_FORK();
65f08c3bdfSopenharmony_ci		if (pid) {
66f08c3bdfSopenharmony_ci			tst_reap_children();
67f08c3bdfSopenharmony_ci		} else {
68f08c3bdfSopenharmony_ci			SAFE_SETUID(pw->pw_uid);
69f08c3bdfSopenharmony_ci			verify_msgget(tc);
70f08c3bdfSopenharmony_ci			exit(0);
71f08c3bdfSopenharmony_ci		}
72f08c3bdfSopenharmony_ci	}
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cistatic void setup(void)
76f08c3bdfSopenharmony_ci{
77f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
78f08c3bdfSopenharmony_ci	msgkey1 = GETIPCKEY();
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM("nobody");
83f08c3bdfSopenharmony_ci}
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_cistatic void cleanup(void)
86f08c3bdfSopenharmony_ci{
87f08c3bdfSopenharmony_ci	if (queue_id != -1)
88f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
89f08c3bdfSopenharmony_ci}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_cistatic struct tst_test test = {
92f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
93f08c3bdfSopenharmony_ci	.needs_root = 1,
94f08c3bdfSopenharmony_ci	.forks_child = 1,
95f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
96f08c3bdfSopenharmony_ci	.setup = setup,
97f08c3bdfSopenharmony_ci	.cleanup = cleanup,
98f08c3bdfSopenharmony_ci	.test = do_test
99f08c3bdfSopenharmony_ci};
100