1 /* 2 * Copyright (C) 2012 Linux Test Project, Inc. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of version 2 of the GNU General Public 6 * License as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it 13 * is free of the rightful claim of any third person regarding 14 * infringement or the like. Any license provided herein, whether 15 * implied or otherwise, applies only to this software file. Patent 16 * licenses, if any, provided herein do not apply to combinations of 17 * this program with other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 22 * 02110-1301, USA. 23 */ 24 25 /* 26 * errno tests for migrate_pages() syscall 27 */ 28 #include <sys/types.h> 29 #include <sys/syscall.h> 30 #include <sys/wait.h> 31 #include <sys/mman.h> 32 #include <errno.h> 33 #if HAVE_NUMA_H 34 #include <numa.h> 35 #endif 36 #if HAVE_NUMAIF_H 37 #include <numaif.h> 38 #endif 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <pwd.h> 44 #include "config.h" 45 #include "test.h" 46 #include "safe_macros.h" 47 #include "lapi/syscalls.h" 48 #include "numa_helper.h" 49 #include "migrate_pages_common.h" 50 51 char *TCID = "migrate_pages01"; 52 int TST_TOTAL = 1; 53 54 option_t options[] = { 55 {NULL, NULL, NULL} 56 }; 57 58 #if defined(HAVE_NUMA_V2) && defined(__NR_migrate_pages) 59 60 static unsigned long *sane_old_nodes; 61 static unsigned long *sane_new_nodes; 62 static int sane_nodemask_size; 63 static int sane_max_node; 64 65 static void setup(void); 66 static void cleanup(void); 67 68 static void test_sane_nodes(void) 69 { 70 tst_resm(TINFO, "test_empty_mask"); 71 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, 72 sane_old_nodes, sane_new_nodes)); 73 check_ret(0); 74 } 75 76 static void test_invalid_pid(void) 77 { 78 pid_t invalid_pid = -1; 79 80 tst_resm(TINFO, "test_invalid_pid -1"); 81 TEST(ltp_syscall(__NR_migrate_pages, invalid_pid, sane_max_node, 82 sane_old_nodes, sane_new_nodes)); 83 check_ret(-1); 84 check_errno(ESRCH); 85 86 tst_resm(TINFO, "test_invalid_pid unused pid"); 87 invalid_pid = tst_get_unused_pid(cleanup); 88 TEST(ltp_syscall(__NR_migrate_pages, invalid_pid, sane_max_node, 89 sane_old_nodes, sane_new_nodes)); 90 check_ret(-1); 91 check_errno(ESRCH); 92 } 93 94 static void test_invalid_masksize(void) 95 { 96 tst_resm(TINFO, "test_invalid_masksize"); 97 TEST(ltp_syscall(__NR_migrate_pages, 0, -1, sane_old_nodes, 98 sane_new_nodes)); 99 check_ret(-1); 100 check_errno(EINVAL); 101 } 102 103 static void test_invalid_mem(void) 104 { 105 unsigned long *p; 106 107 tst_resm(TINFO, "test_invalid_mem -1"); 108 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, -1, -1)); 109 check_ret(-1); 110 check_errno(EFAULT); 111 112 tst_resm(TINFO, "test_invalid_mem invalid prot"); 113 p = mmap(NULL, getpagesize(), PROT_NONE, 114 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); 115 if (p == MAP_FAILED) 116 tst_brkm(TBROK | TERRNO, cleanup, "mmap"); 117 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, p, p)); 118 check_ret(-1); 119 check_errno(EFAULT); 120 121 SAFE_MUNMAP(cleanup, p, getpagesize()); 122 tst_resm(TINFO, "test_invalid_mem unmmaped"); 123 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, p, p)); 124 check_ret(-1); 125 check_errno(EFAULT); 126 } 127 128 static void test_invalid_nodes(void) 129 { 130 int *nodes; 131 int num_nodes, ret, i; 132 int invalid_node = 0; 133 unsigned long *old_nodes, *new_nodes; 134 135 tst_resm(TINFO, "test_invalid_nodes"); 136 ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes); 137 if (ret < 0) 138 tst_brkm(TBROK | TERRNO, cleanup, 139 "get_allowed_nodes_arr: %d", ret); 140 141 /* get first node which is not in nodes */ 142 for (i = 0; i < num_nodes; i++, invalid_node++) 143 if (invalid_node != nodes[i]) 144 break; 145 if (invalid_node < sane_max_node) { 146 old_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 147 new_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 148 memcpy(old_nodes, sane_old_nodes, sane_nodemask_size); 149 memset(new_nodes, 0, sane_nodemask_size); 150 set_bit(new_nodes, invalid_node, 1); 151 152 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, 153 old_nodes, new_nodes)); 154 check_ret(-1); 155 check_errno(EINVAL); 156 free(old_nodes); 157 free(new_nodes); 158 } else { 159 tst_resm(TCONF, "All possible nodes are present"); 160 } 161 162 free(nodes); 163 } 164 165 static void test_invalid_perm(void) 166 { 167 char nobody_uid[] = "nobody"; 168 struct passwd *ltpuser; 169 int status; 170 pid_t child_pid; 171 pid_t parent_pid; 172 int ret = 0; 173 174 tst_resm(TINFO, "test_invalid_perm"); 175 parent_pid = getpid(); 176 fflush(stdout); 177 child_pid = fork(); 178 switch (child_pid) { 179 case -1: 180 tst_brkm(TBROK | TERRNO, cleanup, "fork"); 181 break; 182 case 0: 183 ltpuser = getpwnam(nobody_uid); 184 if (ltpuser == NULL) 185 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed"); 186 SAFE_SETUID(NULL, ltpuser->pw_uid); 187 TEST(ltp_syscall(__NR_migrate_pages, parent_pid, 188 sane_max_node, sane_old_nodes, sane_new_nodes)); 189 ret |= check_ret(-1); 190 ret |= check_errno(EPERM); 191 exit(ret); 192 default: 193 SAFE_WAITPID(cleanup, child_pid, &status, 0); 194 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 195 tst_resm(TFAIL, "child returns %d", status); 196 } 197 } 198 199 int main(int argc, char *argv[]) 200 { 201 int lc; 202 203 tst_parse_opts(argc, argv, options, NULL); 204 205 setup(); 206 for (lc = 0; TEST_LOOPING(lc); lc++) { 207 tst_count = 0; 208 test_sane_nodes(); 209 test_invalid_pid(); 210 test_invalid_masksize(); 211 test_invalid_mem(); 212 test_invalid_nodes(); 213 test_invalid_perm(); 214 } 215 cleanup(); 216 tst_exit(); 217 } 218 219 static void setup(void) 220 { 221 int node, ret; 222 223 tst_require_root(); 224 TEST(ltp_syscall(__NR_migrate_pages, 0, 0, NULL, NULL)); 225 226 if (!is_numa(NULL, NH_MEMS, 1)) 227 tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node"); 228 229 ret = get_allowed_nodes(NH_MEMS, 1, &node); 230 if (ret < 0) 231 tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes_arr: %d", 232 ret); 233 234 sane_max_node = LTP_ALIGN(get_max_node(), sizeof(unsigned long)*8); 235 sane_nodemask_size = sane_max_node / 8; 236 sane_old_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 237 sane_new_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 238 memset(sane_old_nodes, 0, sane_nodemask_size); 239 memset(sane_new_nodes, 0, sane_nodemask_size); 240 241 set_bit(sane_old_nodes, node, 1); 242 set_bit(sane_new_nodes, node, 1); 243 244 TEST_PAUSE; 245 } 246 247 static void cleanup(void) 248 { 249 free(sane_old_nodes); 250 free(sane_new_nodes); 251 } 252 253 #else 254 int main(void) 255 { 256 tst_brkm(TCONF, NULL, "System doesn't support __NR_migrate_pages or " 257 "libnuma or libnuma development packages are not available"); 258 } 259 #endif 260