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