qemu/tests/tcg/linux-test.c
<<
>>
Prefs
   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 _GNU_SOURCE
  20#include <stdarg.h>
  21#include <stdlib.h>
  22#include <stdio.h>
  23#include <unistd.h>
  24#include <fcntl.h>
  25#include <inttypes.h>
  26#include <string.h>
  27#include <sys/types.h>
  28#include <sys/stat.h>
  29#include <sys/wait.h>
  30#include <errno.h>
  31#include <utime.h>
  32#include <time.h>
  33#include <sys/time.h>
  34#include <sys/uio.h>
  35#include <sys/socket.h>
  36#include <netinet/in.h>
  37#include <arpa/inet.h>
  38#include <sched.h>
  39#include <dirent.h>
  40#include <setjmp.h>
  41#include <sys/shm.h>
  42#include "qemu/cutils.h"
  43
  44#define TESTPATH "/tmp/linux-test.tmp"
  45#define TESTPORT 7654
  46#define STACK_SIZE 16384
  47
  48void 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
  59int __chk_error(const char *filename, int line, int ret)
  60{
  61    if (ret < 0) {
  62        error1(filename, line, "%m (ret=%d, errno=%d)",
  63               ret, 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
  76void 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
  89    /* clean up, just in case */
  90    unlink(TESTPATH "/file1");
  91    unlink(TESTPATH "/file2");
  92    unlink(TESTPATH "/file3");
  93    rmdir(TESTPATH);
  94
  95    if (getcwd(cur_dir, sizeof(cur_dir)) == NULL)
  96        error("getcwd");
  97
  98    chk_error(mkdir(TESTPATH, 0755));
  99
 100    chk_error(chdir(TESTPATH));
 101
 102    /* open/read/write/close/readv/writev/lseek */
 103
 104    fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644));
 105    for(i=0;i < FILE_BUF_SIZE; i++)
 106        buf[i] = i;
 107    len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2));
 108    if (len != (FILE_BUF_SIZE / 2))
 109        error("write");
 110    vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2);
 111    vecs[0].iov_len = 16;
 112    vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16;
 113    vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16;
 114    len = chk_error(writev(fd, vecs, 2));
 115    if (len != (FILE_BUF_SIZE / 2))
 116     error("writev");
 117    chk_error(close(fd));
 118
 119    chk_error(rename("file1", "file2"));
 120
 121    fd = chk_error(open("file2", O_RDONLY));
 122
 123    len = chk_error(read(fd, buf2, FILE_BUF_SIZE));
 124    if (len != FILE_BUF_SIZE)
 125        error("read");
 126    if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0)
 127        error("memcmp");
 128
 129#define FOFFSET 16
 130    ret = chk_error(lseek(fd, FOFFSET, SEEK_SET));
 131    if (ret != 16)
 132        error("lseek");
 133    vecs[0].iov_base = buf3;
 134    vecs[0].iov_len = 32;
 135    vecs[1].iov_base = buf3 + 32;
 136    vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32;
 137    len = chk_error(readv(fd, vecs, 2));
 138    if (len != FILE_BUF_SIZE - FOFFSET)
 139        error("readv");
 140    if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0)
 141        error("memcmp");
 142
 143    chk_error(close(fd));
 144
 145    /* access */
 146    chk_error(access("file2", R_OK));
 147
 148    /* stat/chmod/utime/truncate */
 149
 150    chk_error(chmod("file2", 0600));
 151    tbuf.actime = 1001;
 152    tbuf.modtime = 1000;
 153    chk_error(truncate("file2", 100));
 154    chk_error(utime("file2", &tbuf));
 155    chk_error(stat("file2", &st));
 156    if (st.st_size != 100)
 157        error("stat size");
 158    if (!S_ISREG(st.st_mode))
 159        error("stat mode");
 160    if ((st.st_mode & 0777) != 0600)
 161        error("stat mode2");
 162    if (st.st_atime != 1001 ||
 163        st.st_mtime != 1000)
 164        error("stat time");
 165
 166    chk_error(stat(TESTPATH, &st));
 167    if (!S_ISDIR(st.st_mode))
 168        error("stat mode");
 169
 170    /* fstat */
 171    fd = chk_error(open("file2", O_RDWR));
 172    chk_error(ftruncate(fd, 50));
 173    chk_error(fstat(fd, &st));
 174    chk_error(close(fd));
 175
 176    if (st.st_size != 50)
 177        error("stat size");
 178    if (!S_ISREG(st.st_mode))
 179        error("stat mode");
 180
 181    /* symlink/lstat */
 182    chk_error(symlink("file2", "file3"));
 183    chk_error(lstat("file3", &st));
 184    if (!S_ISLNK(st.st_mode))
 185        error("stat mode");
 186
 187    /* getdents */
 188    dir = opendir(TESTPATH);
 189    if (!dir)
 190        error("opendir");
 191    len = 0;
 192    for(;;) {
 193        de = readdir(dir);
 194        if (!de)
 195            break;
 196        if (strcmp(de->d_name, ".") != 0 &&
 197            strcmp(de->d_name, "..") != 0 &&
 198            strcmp(de->d_name, "file2") != 0 &&
 199            strcmp(de->d_name, "file3") != 0)
 200            error("readdir");
 201        len++;
 202    }
 203    closedir(dir);
 204    if (len != 4)
 205        error("readdir");
 206
 207    chk_error(unlink("file3"));
 208    chk_error(unlink("file2"));
 209    chk_error(chdir(cur_dir));
 210    chk_error(rmdir(TESTPATH));
 211}
 212
 213void test_fork(void)
 214{
 215    int pid, status;
 216
 217    pid = chk_error(fork());
 218    if (pid == 0) {
 219        /* child */
 220        exit(2);
 221    }
 222    chk_error(waitpid(pid, &status, 0));
 223    if (!WIFEXITED(status) || WEXITSTATUS(status) != 2)
 224        error("waitpid status=0x%x", status);
 225}
 226
 227void test_time(void)
 228{
 229    struct timeval tv, tv2;
 230    struct timespec ts, rem;
 231    struct rusage rusg1, rusg2;
 232    int ti, i;
 233
 234    chk_error(gettimeofday(&tv, NULL));
 235    rem.tv_sec = 1;
 236    ts.tv_sec = 0;
 237    ts.tv_nsec = 20 * 1000000;
 238    chk_error(nanosleep(&ts, &rem));
 239    if (rem.tv_sec != 1)
 240        error("nanosleep");
 241    chk_error(gettimeofday(&tv2, NULL));
 242    ti = tv2.tv_sec - tv.tv_sec;
 243    if (ti >= 2)
 244        error("gettimeofday");
 245
 246    chk_error(getrusage(RUSAGE_SELF, &rusg1));
 247    for(i = 0;i < 10000; i++);
 248    chk_error(getrusage(RUSAGE_SELF, &rusg2));
 249    if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 ||
 250        (rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0)
 251        error("getrusage");
 252}
 253
 254void pstrcpy(char *buf, int buf_size, const char *str)
 255{
 256    int c;
 257    char *q = buf;
 258
 259    if (buf_size <= 0)
 260        return;
 261
 262    for(;;) {
 263        c = *str++;
 264        if (c == 0 || q >= buf + buf_size - 1)
 265            break;
 266        *q++ = c;
 267    }
 268    *q = '\0';
 269}
 270
 271/* strcat and truncate. */
 272char *pstrcat(char *buf, int buf_size, const char *s)
 273{
 274    int len;
 275    len = strlen(buf);
 276    if (len < buf_size)
 277        pstrcpy(buf + len, buf_size - len, s);
 278    return buf;
 279}
 280
 281int server_socket(void)
 282{
 283    int val, fd;
 284    struct sockaddr_in sockaddr;
 285
 286    /* server socket */
 287    fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
 288
 289    val = 1;
 290    chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)));
 291
 292    sockaddr.sin_family = AF_INET;
 293    sockaddr.sin_port = htons(TESTPORT);
 294    sockaddr.sin_addr.s_addr = 0;
 295    chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
 296    chk_error(listen(fd, 0));
 297    return fd;
 298
 299}
 300
 301int client_socket(void)
 302{
 303    int fd;
 304    struct sockaddr_in sockaddr;
 305
 306    /* server socket */
 307    fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
 308    sockaddr.sin_family = AF_INET;
 309    sockaddr.sin_port = htons(TESTPORT);
 310    inet_aton("127.0.0.1", &sockaddr.sin_addr);
 311    chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
 312    return fd;
 313}
 314
 315const char socket_msg[] = "hello socket\n";
 316
 317void test_socket(void)
 318{
 319    int server_fd, client_fd, fd, pid, ret, val;
 320    struct sockaddr_in sockaddr;
 321    socklen_t len;
 322    char buf[512];
 323
 324    server_fd = server_socket();
 325
 326    /* test a few socket options */
 327    len = sizeof(val);
 328    chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len));
 329    if (val != SOCK_STREAM)
 330        error("getsockopt");
 331
 332    pid = chk_error(fork());
 333    if (pid == 0) {
 334        client_fd = client_socket();
 335        send(client_fd, socket_msg, sizeof(socket_msg), 0);
 336        close(client_fd);
 337        exit(0);
 338    }
 339    len = sizeof(sockaddr);
 340    fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len));
 341
 342    ret = chk_error(recv(fd, buf, sizeof(buf), 0));
 343    if (ret != sizeof(socket_msg))
 344        error("recv");
 345    if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0)
 346        error("socket_msg");
 347    chk_error(close(fd));
 348    chk_error(close(server_fd));
 349}
 350
 351#define WCOUNT_MAX 512
 352
 353void test_pipe(void)
 354{
 355    fd_set rfds, wfds;
 356    int fds[2], fd_max, ret;
 357    uint8_t ch;
 358    int wcount, rcount;
 359
 360    chk_error(pipe(fds));
 361    chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK));
 362    chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK));
 363    wcount = 0;
 364    rcount = 0;
 365    for(;;) {
 366        FD_ZERO(&rfds);
 367        fd_max = fds[0];
 368        FD_SET(fds[0], &rfds);
 369
 370        FD_ZERO(&wfds);
 371        FD_SET(fds[1], &wfds);
 372        if (fds[1] > fd_max)
 373            fd_max = fds[1];
 374
 375        ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL));
 376        if (ret > 0) {
 377            if (FD_ISSET(fds[0], &rfds)) {
 378                chk_error(read(fds[0], &ch, 1));
 379                rcount++;
 380                if (rcount >= WCOUNT_MAX)
 381                    break;
 382            }
 383            if (FD_ISSET(fds[1], &wfds)) {
 384                ch = 'a';
 385                chk_error(write(fds[0], &ch, 1));
 386                wcount++;
 387            }
 388        }
 389    }
 390    chk_error(close(fds[0]));
 391    chk_error(close(fds[1]));
 392}
 393
 394int thread1_res;
 395int thread2_res;
 396
 397int thread1_func(void *arg)
 398{
 399    int i;
 400    for(i=0;i<5;i++) {
 401        thread1_res++;
 402        usleep(10 * 1000);
 403    }
 404    return 0;
 405}
 406
 407int thread2_func(void *arg)
 408{
 409    int i;
 410    for(i=0;i<6;i++) {
 411        thread2_res++;
 412        usleep(10 * 1000);
 413    }
 414    return 0;
 415}
 416
 417void test_clone(void)
 418{
 419    uint8_t *stack1, *stack2;
 420    int pid1, pid2, status1, status2;
 421
 422    stack1 = malloc(STACK_SIZE);
 423    pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE,
 424                           CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello1"));
 425
 426    stack2 = malloc(STACK_SIZE);
 427    pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE,
 428                           CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello2"));
 429
 430    while (waitpid(pid1, &status1, 0) != pid1);
 431    free(stack1);
 432    while (waitpid(pid2, &status2, 0) != pid2);
 433    free(stack2);
 434    if (thread1_res != 5 ||
 435        thread2_res != 6)
 436        error("clone");
 437}
 438
 439/***********************************/
 440
 441volatile int alarm_count;
 442jmp_buf jmp_env;
 443
 444void sig_alarm(int sig)
 445{
 446    if (sig != SIGALRM)
 447        error("signal");
 448    alarm_count++;
 449}
 450
 451void sig_segv(int sig, siginfo_t *info, void *puc)
 452{
 453    if (sig != SIGSEGV)
 454        error("signal");
 455    longjmp(jmp_env, 1);
 456}
 457
 458void test_signal(void)
 459{
 460    struct sigaction act;
 461    struct itimerval it, oit;
 462
 463    /* timer test */
 464
 465    alarm_count = 0;
 466
 467    act.sa_handler = sig_alarm;
 468    sigemptyset(&act.sa_mask);
 469    act.sa_flags = 0;
 470    chk_error(sigaction(SIGALRM, &act, NULL));
 471
 472    it.it_interval.tv_sec = 0;
 473    it.it_interval.tv_usec = 10 * 1000;
 474    it.it_value.tv_sec = 0;
 475    it.it_value.tv_usec = 10 * 1000;
 476    chk_error(setitimer(ITIMER_REAL, &it, NULL));
 477    chk_error(getitimer(ITIMER_REAL, &oit));
 478    if (oit.it_value.tv_sec != it.it_value.tv_sec ||
 479        oit.it_value.tv_usec != it.it_value.tv_usec)
 480        error("itimer");
 481
 482    while (alarm_count < 5) {
 483        usleep(10 * 1000);
 484    }
 485
 486    it.it_interval.tv_sec = 0;
 487    it.it_interval.tv_usec = 0;
 488    it.it_value.tv_sec = 0;
 489    it.it_value.tv_usec = 0;
 490    memset(&oit, 0xff, sizeof(oit));
 491    chk_error(setitimer(ITIMER_REAL, &it, &oit));
 492    if (oit.it_value.tv_sec != 0 ||
 493        oit.it_value.tv_usec != 10 * 1000)
 494        error("setitimer");
 495
 496    /* SIGSEGV test */
 497    act.sa_sigaction = sig_segv;
 498    sigemptyset(&act.sa_mask);
 499    act.sa_flags = SA_SIGINFO;
 500    chk_error(sigaction(SIGSEGV, &act, NULL));
 501    if (setjmp(jmp_env) == 0) {
 502        *(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
 511#define SHM_SIZE 32768
 512
 513void test_shm(void)
 514{
 515    void *ptr;
 516    int shmid;
 517
 518    shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777));
 519    ptr = shmat(shmid, NULL, 0);
 520    if (!ptr)
 521        error("shmat");
 522
 523    memset(ptr, 0, SHM_SIZE);
 524
 525    chk_error(shmctl(shmid, IPC_RMID, 0));
 526    chk_error(shmdt(ptr));
 527}
 528
 529int main(int argc, char **argv)
 530{
 531    test_file();
 532    test_fork();
 533    test_time();
 534    test_socket();
 535    //    test_clone();
 536    test_signal();
 537    test_shm();
 538    return 0;
 539}
 540