1 // SPDX-License-Identifier: GPL-2.0-or-late
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 struct tst_option options[] = {
25 {"s:", &nr_opt, "-s num Set the number of the been allocated hugepages"},
26 {NULL, NULL, NULL}
27 };
28
29 static void do_child(void);
30
test_hugeshmget(void)31 static void test_hugeshmget(void)
32 {
33 pid_t pid;
34 int status;
35
36 switch (pid = fork()) {
37 case -1:
38 tst_brk(TBROK | TERRNO, "fork");
39 break;
40 case 0:
41 /* set the user ID of the child to the non root user */
42 SAFE_SETUID(ltp_uid);
43 do_child();
44 exit(0);
45 default:
46 /* wait for the child to return */
47 SAFE_WAITPID(pid, &status, 0);
48 }
49 }
50
do_child(void)51 static void do_child(void)
52 {
53 TEST(shmget(shmkey, shm_size, SHM_HUGETLB | SHM_RW));
54 if (TST_RET != -1) {
55 tst_res(TFAIL, "shmget succeeded unexpectedly");
56 return;
57 }
58 if (TST_ERR == EACCES)
59 tst_res(TPASS | TTERRNO, "shmget failed as expected");
60 else
61 tst_res(TFAIL | TTERRNO, "shmget failed unexpectedly "
62 "- expect errno=EACCES, got");
63 }
64
setup(void)65 void setup(void)
66 {
67 long hpage_size;
68
69 if (tst_hugepages == 0)
70 tst_brk(TCONF, "No enough hugepages for testing.");
71
72 hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
73
74 shm_size = hpage_size * tst_hugepages / 2;
75 update_shm_size(&shm_size);
76 shmkey = getipckey();
77 shm_id_1 = shmget(shmkey, shm_size,
78 SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
79 if (shm_id_1 == -1)
80 tst_brk(TBROK | TERRNO, "shmget #setup");
81
82 /* get the userid for a non-root user */
83 ltp_uid = getuserid(ltp_user);
84 }
85
cleanup(void)86 void cleanup(void)
87 {
88 rm_shm(shm_id_1);
89 }
90
91 static struct tst_test test = {
92 .needs_root = 1,
93 .options = options,
94 .setup = setup,
95 .cleanup = cleanup,
96 .test_all = test_hugeshmget,
97 .request_hugepages = 128,
98 };
99