• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2004
3  * Copyright (c) Linux Test Project, 2004-2017
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  */
15 
16 /*
17  * DESCRIPTION
18  *	hugeshmat03 - test for EACCES error
19  *
20  * ALGORITHM
21  *	create a shared memory segment with root only read & write permissions
22  *	fork a child process
23  *	if child
24  *	  set the ID of the child process to that of "nobody"
25  *	  loop if that option was specified
26  *	    call shmat() using the TEST() macro
27  *	    check the errno value
28  *	      issue a PASS message if we get EACCES
29  *	    otherwise, the tests fails
30  *	      issue a FAIL message
31  *	  call cleanup
32  *	if parent
33  *	  wait for child to exit
34  *	  remove the shared memory segment
35  *
36  * HISTORY
37  *	03/2001 - Written by Wayne Boyer
38  *	04/2004 - Updated by Robbie Williamson
39  *
40  * RESTRICTIONS
41  *	test must be run at root
42  */
43 
44 #include <limits.h>
45 #include "hugetlb.h"
46 
47 static size_t shm_size;
48 static int shm_id_1 = -1;
49 static void *addr;
50 static uid_t ltp_uid;
51 static char *ltp_user = "nobody";
52 
53 static long hugepages = 128;
54 
55 static struct tst_option options[] = {
56 	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
57 	{NULL, NULL, NULL}
58 };
59 
verify_hugeshmat(void)60 static void verify_hugeshmat(void)
61 {
62 	int status;
63 	pid_t pid;
64 
65 	switch (pid = SAFE_FORK()) {
66 	case 0:
67 		SAFE_SETUID(ltp_uid);
68 
69 		addr = shmat(shm_id_1, NULL, 0);
70 		if (addr != (void *)-1) {
71 			tst_res(TFAIL, "shmat succeeded unexpectedly");
72 			return;
73 		}
74 		if (errno == EACCES) {
75 			tst_res(TPASS | TERRNO, "shmat failed as expected");
76 		} else {
77 			tst_res(TFAIL | TERRNO, "shmat failed unexpectedly "
78 					"- expect errno=EACCES, got");
79 		}
80 		break;
81 	default:
82 		SAFE_WAITPID(pid, &status, 0);
83 	}
84 }
85 
setup(void)86 static void setup(void)
87 {
88 	long hpage_size;
89 
90 	save_nr_hugepages();
91 	if (nr_opt)
92 		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
93 
94 	set_sys_tune("nr_hugepages", hugepages, 1);
95 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
96 
97 	shm_size = hpage_size * hugepages / 2;
98 	update_shm_size(&shm_size);
99 	shmkey = getipckey();
100 	shm_id_1 = shmget(shmkey, shm_size,
101 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
102 	if (shm_id_1 == -1)
103 		tst_brk(TBROK | TERRNO, "shmget");
104 
105 	ltp_uid = getuserid(ltp_user);
106 }
107 
cleanup(void)108 static void cleanup(void)
109 {
110 	rm_shm(shm_id_1);
111 	restore_nr_hugepages();
112 }
113 
114 static struct tst_test test = {
115 	.needs_root = 1,
116 	.forks_child = 1,
117 	.needs_tmpdir = 1,
118 	.options = options,
119 	.test_all = verify_hugeshmat,
120 	.setup = setup,
121 	.cleanup = cleanup,
122 };
123