Statistics
| Branch: | Revision:

root / tests / linux-test.c @ 7aaabde7

History | View | Annotate | Download (12.7 kB)

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