| 1 | /* |
| 2 | * linux and CPU test |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | #define _FILE_OFFSET_BITS 64 |
| 20 | #define _GNU_SOURCE |
| 21 | #include <stdarg.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <inttypes.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <errno.h> |
| 32 | #include <utime.h> |
| 33 | #include <time.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <sys/resource.h> |
| 36 | #include <sys/uio.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <netinet/in.h> |
| 39 | #include <arpa/inet.h> |
| 40 | #include <sched.h> |
| 41 | #include <dirent.h> |
| 42 | #include <setjmp.h> |
| 43 | #include <sys/shm.h> |
| 44 | #include <assert.h> |
| 45 | |
| 46 | #define STACK_SIZE 16384 |
| 47 | |
| 48 | static void error1(const char *filename, int line, const char *fmt, ...) |
| 49 | { |
| 50 | va_list ap; |
| 51 | va_start(ap, fmt); |
| 52 | fprintf(stderr, "%s:%d: ", filename, line); |
| 53 | vfprintf(stderr, fmt, ap); |
| 54 | fprintf(stderr, "\n"); |
| 55 | va_end(ap); |
| 56 | exit(1); |
| 57 | } |
| 58 | |
| 59 | static int __chk_error(const char *filename, int line, int ret) |
| 60 | { |
| 61 | if (ret < 0) { |
| 62 | error1(filename, line, "%m (ret=%d, errno=%d/%s)", |
| 63 | ret, errno, strerror(errno)); |
| 64 | } |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | #define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__) |
| 69 | |
| 70 | #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret)) |
| 71 | |
| 72 | /*******************************************************/ |
| 73 | |
| 74 | #define FILE_BUF_SIZE 300 |
| 75 | |
| 76 | static void test_file(void) |
| 77 | { |
| 78 | int fd, i, len, ret; |
| 79 | uint8_t buf[FILE_BUF_SIZE]; |
| 80 | uint8_t buf2[FILE_BUF_SIZE]; |
| 81 | uint8_t buf3[FILE_BUF_SIZE]; |
| 82 | char cur_dir[1024]; |
| 83 | struct stat st; |
| 84 | struct utimbuf tbuf; |
| 85 | struct iovec vecs[2]; |
| 86 | DIR *dir; |
| 87 | struct dirent *de; |
| 88 | /* TODO: make common tempdir creation for tcg tests */ |
| 89 | char template[] = "/tmp/linux-test-XXXXXX"; |
| 90 | char *tmpdir = mkdtemp(template); |
| 91 | |
| 92 | assert(tmpdir); |
| 93 | |
| 94 | if (getcwd(cur_dir, sizeof(cur_dir)) == NULL) |
| 95 | error("getcwd"); |
| 96 | |
| 97 | chk_error(chdir(tmpdir)); |
| 98 | |
| 99 | /* open/read/write/close/readv/writev/lseek */ |
| 100 | |
| 101 | fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644)); |
| 102 | for(i=0;i < FILE_BUF_SIZE; i++) |
| 103 | buf[i] = i; |
| 104 | len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2)); |
| 105 | if (len != (FILE_BUF_SIZE / 2)) |
| 106 | error("write"); |
| 107 | vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2); |
| 108 | vecs[0].iov_len = 16; |
| 109 | vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16; |
| 110 | vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16; |
| 111 | len = chk_error(writev(fd, vecs, 2)); |
| 112 | if (len != (FILE_BUF_SIZE / 2)) |
| 113 | error("writev"); |
| 114 | chk_error(close(fd)); |
| 115 | |
| 116 | chk_error(rename("file1", "file2")); |
| 117 | |
| 118 | fd = chk_error(open("file2", O_RDONLY)); |
| 119 | |
| 120 | len = chk_error(read(fd, buf2, FILE_BUF_SIZE)); |
| 121 | if (len != FILE_BUF_SIZE) |
| 122 | error("read"); |
| 123 | if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0) |
| 124 | error("memcmp"); |
| 125 | |
| 126 | #define FOFFSET 16 |
| 127 | ret = chk_error(lseek(fd, FOFFSET, SEEK_SET)); |
| 128 | if (ret != 16) |
| 129 | error("lseek"); |
| 130 | vecs[0].iov_base = buf3; |
| 131 | vecs[0].iov_len = 32; |
| 132 | vecs[1].iov_base = buf3 + 32; |
| 133 | vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32; |
| 134 | len = chk_error(readv(fd, vecs, 2)); |
| 135 | if (len != FILE_BUF_SIZE - FOFFSET) |
| 136 | error("readv"); |
| 137 | if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0) |
| 138 | error("memcmp"); |
| 139 | |
| 140 | chk_error(close(fd)); |
| 141 | |
| 142 | /* access */ |
| 143 | chk_error(access("file2", R_OK)); |
| 144 | |
| 145 | /* stat/chmod/utime/truncate */ |
| 146 | |
| 147 | chk_error(chmod("file2", 0600)); |
| 148 | tbuf.actime = 1001; |
| 149 | tbuf.modtime = 1000; |
| 150 | chk_error(truncate("file2", 100)); |
| 151 | chk_error(utime("file2", &tbuf)); |
| 152 | chk_error(stat("file2", &st)); |
| 153 | if (st.st_size != 100) |
| 154 | error("stat size"); |
| 155 | if (!S_ISREG(st.st_mode)) |
| 156 | error("stat mode"); |
| 157 | if ((st.st_mode & 0777) != 0600) |
| 158 | error("stat mode2"); |
| 159 | /* |
| 160 | * Only check mtime, not atime: other processes such as |
| 161 | * virus scanners might race with this test program and get |
| 162 | * in and update the atime, causing random failures. |
| 163 | */ |
| 164 | if (st.st_mtime != 1000) { |
| 165 | error("stat time"); |
| 166 | } |
| 167 | |
| 168 | chk_error(stat(tmpdir, &st)); |
| 169 | if (!S_ISDIR(st.st_mode)) |
| 170 | error("stat mode"); |
| 171 | |
| 172 | /* fstat */ |
| 173 | fd = chk_error(open("file2", O_RDWR)); |
| 174 | chk_error(ftruncate(fd, 50)); |
| 175 | chk_error(fstat(fd, &st)); |
| 176 | chk_error(close(fd)); |
| 177 | |
| 178 | if (st.st_size != 50) |
| 179 | error("stat size"); |
| 180 | if (!S_ISREG(st.st_mode)) |
| 181 | error("stat mode"); |
| 182 | |
| 183 | /* symlink/lstat */ |
| 184 | chk_error(symlink("file2", "file3")); |
| 185 | chk_error(lstat("file3", &st)); |
| 186 | if (!S_ISLNK(st.st_mode)) |
| 187 | error("stat mode"); |
| 188 | |
| 189 | /* getdents */ |
| 190 | dir = opendir(tmpdir); |
| 191 | if (!dir) |
| 192 | error("opendir"); |
| 193 | len = 0; |
| 194 | for(;;) { |
| 195 | de = readdir(dir); |
| 196 | if (!de) |
| 197 | break; |
| 198 | if (strcmp(de->d_name, ".") != 0 && |
| 199 | strcmp(de->d_name, "..") != 0 && |
| 200 | strcmp(de->d_name, "file2") != 0 && |
| 201 | strcmp(de->d_name, "file3") != 0) |
| 202 | error("readdir"); |
| 203 | len++; |
| 204 | } |
| 205 | closedir(dir); |
| 206 | if (len != 4) |
| 207 | error("readdir"); |
| 208 | |
| 209 | chk_error(unlink("file3")); |
| 210 | chk_error(unlink("file2")); |
| 211 | chk_error(chdir(cur_dir)); |
| 212 | chk_error(rmdir(tmpdir)); |
| 213 | } |
| 214 | |
| 215 | static void test_fork(void) |
| 216 | { |
| 217 | int pid, status; |
| 218 | |
| 219 | pid = chk_error(fork()); |
| 220 | if (pid == 0) { |
| 221 | /* child */ |
| 222 | sleep(2); |
| 223 | exit(2); |
| 224 | } |
| 225 | chk_error(waitpid(pid, &status, 0)); |
| 226 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 2) |
| 227 | error("waitpid status=0x%x", status); |
| 228 | } |
| 229 | |
| 230 | static void test_time(void) |
| 231 | { |
| 232 | struct timeval tv, tv2; |
| 233 | struct timespec ts, rem; |
| 234 | struct rusage rusg1, rusg2; |
| 235 | int ti, i; |
| 236 | |
| 237 | chk_error(gettimeofday(&tv, NULL)); |
| 238 | rem.tv_sec = 1; |
| 239 | ts.tv_sec = 0; |
| 240 | ts.tv_nsec = 20 * 1000000; |
| 241 | chk_error(nanosleep(&ts, &rem)); |
| 242 | if (rem.tv_sec != 1) |
| 243 | error("nanosleep"); |
| 244 | chk_error(gettimeofday(&tv2, NULL)); |
| 245 | ti = tv2.tv_sec - tv.tv_sec; |
| 246 | if (ti >= 2) |
| 247 | error("gettimeofday"); |
| 248 | |
| 249 | chk_error(getrusage(RUSAGE_SELF, &rusg1)); |
| 250 | for(i = 0;i < 10000; i++); |
| 251 | chk_error(getrusage(RUSAGE_SELF, &rusg2)); |
| 252 | if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 || |
| 253 | (rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0) |
| 254 | error("getrusage"); |
| 255 | } |
| 256 | |
| 257 | static int server_socket(void) |
| 258 | { |
| 259 | int val, fd; |
| 260 | struct sockaddr_in sockaddr = {}; |
| 261 | |
| 262 | /* server socket */ |
| 263 | fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); |
| 264 | |
| 265 | val = 1; |
| 266 | chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))); |
| 267 | |
| 268 | sockaddr.sin_family = AF_INET; |
| 269 | sockaddr.sin_port = htons(0); /* choose random ephemeral port) */ |
| 270 | sockaddr.sin_addr.s_addr = 0; |
| 271 | chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); |
| 272 | chk_error(listen(fd, 1)); |
| 273 | return fd; |
| 274 | |
| 275 | } |
| 276 | |
| 277 | static int client_socket(uint16_t port) |
| 278 | { |
| 279 | int fd; |
| 280 | struct sockaddr_in sockaddr = {}; |
| 281 | |
| 282 | /* server socket */ |
| 283 | fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); |
| 284 | sockaddr.sin_family = AF_INET; |
| 285 | sockaddr.sin_port = htons(port); |
| 286 | inet_aton("127.0.0.1", &sockaddr.sin_addr); |
| 287 | chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); |
| 288 | return fd; |
| 289 | } |
| 290 | |
| 291 | static const char socket_msg[] = "hello socket\n"; |
| 292 | |
| 293 | static void test_socket(void) |
| 294 | { |
| 295 | int server_fd, client_fd, fd, pid, ret, val; |
| 296 | struct sockaddr_in sockaddr; |
| 297 | struct sockaddr_in server_addr; |
| 298 | socklen_t len, socklen; |
| 299 | uint16_t server_port; |
| 300 | char buf[512]; |
| 301 | |
| 302 | server_fd = server_socket(); |
| 303 | /* find out what port we got */ |
| 304 | socklen = sizeof(server_addr); |
| 305 | ret = getsockname(server_fd, (struct sockaddr *)&server_addr, &socklen); |
| 306 | chk_error(ret); |
| 307 | server_port = ntohs(server_addr.sin_port); |
| 308 | |
| 309 | /* test a few socket options */ |
| 310 | len = sizeof(val); |
| 311 | chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len)); |
| 312 | if (val != SOCK_STREAM) |
| 313 | error("getsockopt"); |
| 314 | |
| 315 | pid = chk_error(fork()); |
| 316 | if (pid == 0) { |
| 317 | client_fd = client_socket(server_port); |
| 318 | send(client_fd, socket_msg, sizeof(socket_msg), 0); |
| 319 | close(client_fd); |
| 320 | exit(0); |
| 321 | } |
| 322 | len = sizeof(sockaddr); |
| 323 | fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len)); |
| 324 | |
| 325 | ret = chk_error(recv(fd, buf, sizeof(buf), 0)); |
| 326 | if (ret != sizeof(socket_msg)) |
| 327 | error("recv"); |
| 328 | if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0) |
| 329 | error("socket_msg"); |
| 330 | chk_error(close(fd)); |
| 331 | chk_error(close(server_fd)); |
| 332 | } |
| 333 | |
| 334 | #define WCOUNT_MAX 512 |
| 335 | |
| 336 | static void test_pipe(void) |
| 337 | { |
| 338 | fd_set rfds, wfds; |
| 339 | int fds[2], fd_max, ret; |
| 340 | uint8_t ch; |
| 341 | int wcount, rcount; |
| 342 | |
| 343 | chk_error(pipe(fds)); |
| 344 | chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK)); |
| 345 | chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK)); |
| 346 | wcount = 0; |
| 347 | rcount = 0; |
| 348 | for(;;) { |
| 349 | FD_ZERO(&rfds); |
| 350 | fd_max = fds[0]; |
| 351 | FD_SET(fds[0], &rfds); |
| 352 | |
| 353 | FD_ZERO(&wfds); |
| 354 | FD_SET(fds[1], &wfds); |
| 355 | if (fds[1] > fd_max) |
| 356 | fd_max = fds[1]; |
| 357 | |
| 358 | ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL)); |
| 359 | if (ret > 0) { |
| 360 | if (FD_ISSET(fds[0], &rfds)) { |
| 361 | chk_error(read(fds[0], &ch, 1)); |
| 362 | rcount++; |
| 363 | if (rcount >= WCOUNT_MAX) { |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | if (FD_ISSET(fds[1], &wfds)) { |
| 368 | ch = 'a'; |
| 369 | chk_error(write(fds[1], &ch, 1)); |
| 370 | wcount++; |
| 371 | if (wcount >= WCOUNT_MAX) { |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | chk_error(close(fds[0])); |
| 378 | chk_error(close(fds[1])); |
| 379 | } |
| 380 | |
| 381 | static int thread1_res; |
| 382 | static int thread2_res; |
| 383 | |
| 384 | static int thread1_func(void *arg) |
| 385 | { |
| 386 | int i; |
| 387 | for(i=0;i<5;i++) { |
| 388 | thread1_res++; |
| 389 | usleep(10 * 1000); |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int thread2_func(void *arg) |
| 395 | { |
| 396 | int i; |
| 397 | for(i=0;i<6;i++) { |
| 398 | thread2_res++; |
| 399 | usleep(10 * 1000); |
| 400 | } |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static void wait_for_child(pid_t pid) |
| 405 | { |
| 406 | int status; |
| 407 | chk_error(waitpid(pid, &status, 0)); |
| 408 | } |
| 409 | |
| 410 | /* For test_clone we must match the clone flags used by glibc, see |
| 411 | * CLONE_THREAD_FLAGS in the QEMU source code. |
| 412 | */ |
| 413 | static void test_clone(void) |
| 414 | { |
| 415 | uint8_t *stack1, *stack2; |
| 416 | pid_t pid1, pid2; |
| 417 | |
| 418 | stack1 = malloc(STACK_SIZE); |
| 419 | pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE, |
| 420 | CLONE_VM | CLONE_FS | CLONE_FILES | |
| 421 | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, |
| 422 | "hello1")); |
| 423 | |
| 424 | stack2 = malloc(STACK_SIZE); |
| 425 | pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE, |
| 426 | CLONE_VM | CLONE_FS | CLONE_FILES | |
| 427 | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, |
| 428 | "hello2")); |
| 429 | |
| 430 | wait_for_child(pid1); |
| 431 | free(stack1); |
| 432 | wait_for_child(pid2); |
| 433 | free(stack2); |
| 434 | |
| 435 | if (thread1_res != 5 || |
| 436 | thread2_res != 6) |
| 437 | error("clone"); |
| 438 | } |
| 439 | |
| 440 | /***********************************/ |
| 441 | |
| 442 | volatile int alarm_count; |
| 443 | jmp_buf jmp_env; |
| 444 | |
| 445 | static void sig_alarm(int sig) |
| 446 | { |
| 447 | if (sig != SIGALRM) |
| 448 | error("signal"); |
| 449 | alarm_count++; |
| 450 | } |
| 451 | |
| 452 | static void sig_segv(int sig, siginfo_t *info, void *puc) |
| 453 | { |
| 454 | if (sig != SIGSEGV) |
| 455 | error("signal"); |
| 456 | longjmp(jmp_env, 1); |
| 457 | } |
| 458 | |
| 459 | static void test_signal(void) |
| 460 | { |
| 461 | struct sigaction act; |
| 462 | struct itimerval it, oit; |
| 463 | |
| 464 | /* timer test */ |
| 465 | |
| 466 | alarm_count = 0; |
| 467 | |
| 468 | act.sa_handler = sig_alarm; |
| 469 | sigemptyset(&act.sa_mask); |
| 470 | act.sa_flags = 0; |
| 471 | chk_error(sigaction(SIGALRM, &act, NULL)); |
| 472 | |
| 473 | it.it_interval.tv_sec = 0; |
| 474 | it.it_interval.tv_usec = 10 * 1000; |
| 475 | it.it_value.tv_sec = 0; |
| 476 | it.it_value.tv_usec = 10 * 1000; |
| 477 | chk_error(setitimer(ITIMER_REAL, &it, NULL)); |
| 478 | chk_error(getitimer(ITIMER_REAL, &oit)); |
| 479 | |
| 480 | while (alarm_count < 5) { |
| 481 | usleep(10 * 1000); |
| 482 | getitimer(ITIMER_REAL, &oit); |
| 483 | } |
| 484 | |
| 485 | it.it_interval.tv_sec = 0; |
| 486 | it.it_interval.tv_usec = 0; |
| 487 | it.it_value.tv_sec = 0; |
| 488 | it.it_value.tv_usec = 0; |
| 489 | memset(&oit, 0xff, sizeof(oit)); |
| 490 | chk_error(setitimer(ITIMER_REAL, &it, &oit)); |
| 491 | |
| 492 | /* SIGSEGV test */ |
| 493 | act.sa_sigaction = sig_segv; |
| 494 | sigemptyset(&act.sa_mask); |
| 495 | act.sa_flags = SA_SIGINFO; |
| 496 | chk_error(sigaction(SIGSEGV, &act, NULL)); |
| 497 | if (setjmp(jmp_env) == 0) { |
| 498 | /* |
| 499 | * clang requires volatile or it will turn this into a |
| 500 | * call to abort() instead of forcing a SIGSEGV. |
| 501 | */ |
| 502 | *(volatile uint8_t *)0 = 0; |
| 503 | } |
| 504 | |
| 505 | act.sa_handler = SIG_DFL; |
| 506 | sigemptyset(&act.sa_mask); |
| 507 | act.sa_flags = 0; |
| 508 | chk_error(sigaction(SIGSEGV, &act, NULL)); |
| 509 | |
| 510 | if (sigaction(SIGKILL, &act, NULL) == 0) { |
| 511 | error("sigaction(SIGKILL, &act, NULL) must not succeed"); |
| 512 | } |
| 513 | if (sigaction(SIGSTOP, &act, NULL) == 0) { |
| 514 | error("sigaction(SIGSTOP, &act, NULL) must not succeed"); |
| 515 | } |
| 516 | chk_error(sigaction(SIGKILL, NULL, &act)); |
| 517 | chk_error(sigaction(SIGSTOP, NULL, &act)); |
| 518 | } |
| 519 | |
| 520 | #define SHM_SIZE 32768 |
| 521 | |
| 522 | static void test_shm(void) |
| 523 | { |
| 524 | void *ptr; |
| 525 | int shmid; |
| 526 | |
| 527 | shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777)); |
| 528 | ptr = shmat(shmid, NULL, 0); |
| 529 | if (ptr == (void *)-1) { |
| 530 | error("shmat"); |
| 531 | } |
| 532 | |
| 533 | memset(ptr, 0, SHM_SIZE); |
| 534 | |
| 535 | chk_error(shmctl(shmid, IPC_RMID, 0)); |
| 536 | chk_error(shmdt(ptr)); |
| 537 | } |
| 538 | |
| 539 | int main(int argc, char **argv) |
| 540 | { |
| 541 | test_file(); |
| 542 | test_pipe(); |
| 543 | test_fork(); |
| 544 | test_time(); |
| 545 | test_socket(); |
| 546 | |
| 547 | if (argc > 1) { |
| 548 | printf("test_clone still considered buggy\n"); |
| 549 | test_clone(); |
| 550 | } |
| 551 | |
| 552 | test_signal(); |
| 553 | test_shm(); |
| 554 | return 0; |
| 555 | } |