1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2004
4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2004-2017
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * DESCRIPTION
7f08c3bdfSopenharmony_ci *	hugeshmget05 - test for EACCES error
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * HISTORY
10f08c3bdfSopenharmony_ci *	03/2001 - Written by Wayne Boyer
11f08c3bdfSopenharmony_ci *	04/2004 - Updated by Robbie Williamson
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <sys/types.h>
15f08c3bdfSopenharmony_ci#include <sys/wait.h>
16f08c3bdfSopenharmony_ci#include <limits.h>
17f08c3bdfSopenharmony_ci#include "hugetlb.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic size_t shm_size;
20f08c3bdfSopenharmony_cistatic int shm_id_1 = -1;
21f08c3bdfSopenharmony_cistatic uid_t ltp_uid;
22f08c3bdfSopenharmony_cistatic char *ltp_user = "nobody";
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void do_child(void);
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void test_hugeshmget(void)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	pid_t pid;
29f08c3bdfSopenharmony_ci	int status;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	switch (pid = fork()) {
32f08c3bdfSopenharmony_ci	case -1:
33f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "fork");
34f08c3bdfSopenharmony_ci		break;
35f08c3bdfSopenharmony_ci	case 0:
36f08c3bdfSopenharmony_ci		/* set the user ID of the child to the non root user */
37f08c3bdfSopenharmony_ci		SAFE_SETUID(ltp_uid);
38f08c3bdfSopenharmony_ci		do_child();
39f08c3bdfSopenharmony_ci		exit(0);
40f08c3bdfSopenharmony_ci	default:
41f08c3bdfSopenharmony_ci		/* wait for the child to return */
42f08c3bdfSopenharmony_ci		SAFE_WAITPID(pid, &status, 0);
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void do_child(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	TEST(shmget(shmkey, shm_size, SHM_HUGETLB | SHM_RW));
49f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
50f08c3bdfSopenharmony_ci		tst_res(TFAIL, "shmget succeeded unexpectedly");
51f08c3bdfSopenharmony_ci		return;
52f08c3bdfSopenharmony_ci	}
53f08c3bdfSopenharmony_ci	if (TST_ERR == EACCES)
54f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "shmget failed as expected");
55f08c3bdfSopenharmony_ci	else
56f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "shmget failed unexpectedly "
57f08c3bdfSopenharmony_ci				"- expect errno=EACCES, got");
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_civoid setup(void)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	long hpage_size;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	if (tst_hugepages == 0)
65f08c3bdfSopenharmony_ci		tst_brk(TCONF, "No enough hugepages for testing.");
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	shm_size = hpage_size * tst_hugepages / 2;
70f08c3bdfSopenharmony_ci	update_shm_size(&shm_size);
71f08c3bdfSopenharmony_ci	shmkey = getipckey();
72f08c3bdfSopenharmony_ci	shm_id_1 = shmget(shmkey, shm_size,
73f08c3bdfSopenharmony_ci			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
74f08c3bdfSopenharmony_ci	if (shm_id_1 == -1)
75f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "shmget #setup");
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	/* get the userid for a non-root user */
78f08c3bdfSopenharmony_ci	ltp_uid = getuserid(ltp_user);
79f08c3bdfSopenharmony_ci}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_civoid cleanup(void)
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	rm_shm(shm_id_1);
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic struct tst_test test = {
87f08c3bdfSopenharmony_ci	.needs_root = 1,
88f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
89f08c3bdfSopenharmony_ci		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
90f08c3bdfSopenharmony_ci		{}
91f08c3bdfSopenharmony_ci	},
92f08c3bdfSopenharmony_ci	.setup = setup,
93f08c3bdfSopenharmony_ci	.cleanup = cleanup,
94f08c3bdfSopenharmony_ci	.test_all = test_hugeshmget,
95f08c3bdfSopenharmony_ci	.hugepages = {128, TST_REQUEST},
96f08c3bdfSopenharmony_ci};
97