4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
27 #include <sys/types.h>
34 #include <sys/resource.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
45 #define STACK_SIZE 16384
47 static void error1(const char *filename
, int line
, const char *fmt
, ...)
51 fprintf(stderr
, "%s:%d: ", filename
, line
);
52 vfprintf(stderr
, fmt
, ap
);
53 fprintf(stderr
, "\n");
58 static int __chk_error(const char *filename
, int line
, int ret
)
61 error1(filename
, line
, "%m (ret=%d, errno=%d/%s)",
62 ret
, errno
, strerror(errno
));
67 #define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
69 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
71 /*******************************************************/
73 #define FILE_BUF_SIZE 300
75 static void test_file(void)
78 uint8_t buf
[FILE_BUF_SIZE
];
79 uint8_t buf2
[FILE_BUF_SIZE
];
80 uint8_t buf3
[FILE_BUF_SIZE
];
87 /* TODO: make common tempdir creation for tcg tests */
88 char template[] = "/tmp/linux-test-XXXXXX";
89 char *tmpdir
= mkdtemp(template);
93 if (getcwd(cur_dir
, sizeof(cur_dir
)) == NULL
)
96 chk_error(chdir(tmpdir
));
98 /* open/read/write/close/readv/writev/lseek */
100 fd
= chk_error(open("file1", O_WRONLY
| O_TRUNC
| O_CREAT
, 0644));
101 for(i
=0;i
< FILE_BUF_SIZE
; i
++)
103 len
= chk_error(write(fd
, buf
, FILE_BUF_SIZE
/ 2));
104 if (len
!= (FILE_BUF_SIZE
/ 2))
106 vecs
[0].iov_base
= buf
+ (FILE_BUF_SIZE
/ 2);
107 vecs
[0].iov_len
= 16;
108 vecs
[1].iov_base
= buf
+ (FILE_BUF_SIZE
/ 2) + 16;
109 vecs
[1].iov_len
= (FILE_BUF_SIZE
/ 2) - 16;
110 len
= chk_error(writev(fd
, vecs
, 2));
111 if (len
!= (FILE_BUF_SIZE
/ 2))
113 chk_error(close(fd
));
115 chk_error(rename("file1", "file2"));
117 fd
= chk_error(open("file2", O_RDONLY
));
119 len
= chk_error(read(fd
, buf2
, FILE_BUF_SIZE
));
120 if (len
!= FILE_BUF_SIZE
)
122 if (memcmp(buf
, buf2
, FILE_BUF_SIZE
) != 0)
126 ret
= chk_error(lseek(fd
, FOFFSET
, SEEK_SET
));
129 vecs
[0].iov_base
= buf3
;
130 vecs
[0].iov_len
= 32;
131 vecs
[1].iov_base
= buf3
+ 32;
132 vecs
[1].iov_len
= FILE_BUF_SIZE
- FOFFSET
- 32;
133 len
= chk_error(readv(fd
, vecs
, 2));
134 if (len
!= FILE_BUF_SIZE
- FOFFSET
)
136 if (memcmp(buf
+ FOFFSET
, buf3
, FILE_BUF_SIZE
- FOFFSET
) != 0)
139 chk_error(close(fd
));
142 chk_error(access("file2", R_OK
));
144 /* stat/chmod/utime/truncate */
146 chk_error(chmod("file2", 0600));
149 chk_error(truncate("file2", 100));
150 chk_error(utime("file2", &tbuf
));
151 chk_error(stat("file2", &st
));
152 if (st
.st_size
!= 100)
154 if (!S_ISREG(st
.st_mode
))
156 if ((st
.st_mode
& 0777) != 0600)
158 if (st
.st_atime
!= 1001 ||
162 chk_error(stat(tmpdir
, &st
));
163 if (!S_ISDIR(st
.st_mode
))
167 fd
= chk_error(open("file2", O_RDWR
));
168 chk_error(ftruncate(fd
, 50));
169 chk_error(fstat(fd
, &st
));
170 chk_error(close(fd
));
172 if (st
.st_size
!= 50)
174 if (!S_ISREG(st
.st_mode
))
178 chk_error(symlink("file2", "file3"));
179 chk_error(lstat("file3", &st
));
180 if (!S_ISLNK(st
.st_mode
))
184 dir
= opendir(tmpdir
);
192 if (strcmp(de
->d_name
, ".") != 0 &&
193 strcmp(de
->d_name
, "..") != 0 &&
194 strcmp(de
->d_name
, "file2") != 0 &&
195 strcmp(de
->d_name
, "file3") != 0)
203 chk_error(unlink("file3"));
204 chk_error(unlink("file2"));
205 chk_error(chdir(cur_dir
));
206 chk_error(rmdir(tmpdir
));
209 static void test_fork(void)
213 pid
= chk_error(fork());
219 chk_error(waitpid(pid
, &status
, 0));
220 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 2)
221 error("waitpid status=0x%x", status
);
224 static void test_time(void)
226 struct timeval tv
, tv2
;
227 struct timespec ts
, rem
;
228 struct rusage rusg1
, rusg2
;
231 chk_error(gettimeofday(&tv
, NULL
));
234 ts
.tv_nsec
= 20 * 1000000;
235 chk_error(nanosleep(&ts
, &rem
));
238 chk_error(gettimeofday(&tv2
, NULL
));
239 ti
= tv2
.tv_sec
- tv
.tv_sec
;
241 error("gettimeofday");
243 chk_error(getrusage(RUSAGE_SELF
, &rusg1
));
244 for(i
= 0;i
< 10000; i
++);
245 chk_error(getrusage(RUSAGE_SELF
, &rusg2
));
246 if ((rusg2
.ru_utime
.tv_sec
- rusg1
.ru_utime
.tv_sec
) < 0 ||
247 (rusg2
.ru_stime
.tv_sec
- rusg1
.ru_stime
.tv_sec
) < 0)
251 static int server_socket(void)
254 struct sockaddr_in sockaddr
;
257 fd
= chk_error(socket(PF_INET
, SOCK_STREAM
, 0));
260 chk_error(setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)));
262 sockaddr
.sin_family
= AF_INET
;
263 sockaddr
.sin_port
= htons(0); /* choose random ephemeral port) */
264 sockaddr
.sin_addr
.s_addr
= 0;
265 chk_error(bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
)));
266 chk_error(listen(fd
, 0));
271 static int client_socket(uint16_t port
)
274 struct sockaddr_in sockaddr
;
277 fd
= chk_error(socket(PF_INET
, SOCK_STREAM
, 0));
278 sockaddr
.sin_family
= AF_INET
;
279 sockaddr
.sin_port
= htons(port
);
280 inet_aton("127.0.0.1", &sockaddr
.sin_addr
);
281 chk_error(connect(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
)));
285 static const char socket_msg
[] = "hello socket\n";
287 static void test_socket(void)
289 int server_fd
, client_fd
, fd
, pid
, ret
, val
;
290 struct sockaddr_in sockaddr
;
291 struct sockaddr_in server_addr
;
292 socklen_t len
, socklen
;
293 uint16_t server_port
;
296 server_fd
= server_socket();
297 /* find out what port we got */
298 socklen
= sizeof(server_addr
);
299 ret
= getsockname(server_fd
, &server_addr
, &socklen
);
301 server_port
= ntohs(server_addr
.sin_port
);
303 /* test a few socket options */
305 chk_error(getsockopt(server_fd
, SOL_SOCKET
, SO_TYPE
, &val
, &len
));
306 if (val
!= SOCK_STREAM
)
309 pid
= chk_error(fork());
311 client_fd
= client_socket(server_port
);
312 send(client_fd
, socket_msg
, sizeof(socket_msg
), 0);
316 len
= sizeof(sockaddr
);
317 fd
= chk_error(accept(server_fd
, (struct sockaddr
*)&sockaddr
, &len
));
319 ret
= chk_error(recv(fd
, buf
, sizeof(buf
), 0));
320 if (ret
!= sizeof(socket_msg
))
322 if (memcmp(buf
, socket_msg
, sizeof(socket_msg
)) != 0)
324 chk_error(close(fd
));
325 chk_error(close(server_fd
));
328 #define WCOUNT_MAX 512
330 static void test_pipe(void)
333 int fds
[2], fd_max
, ret
;
337 chk_error(pipe(fds
));
338 chk_error(fcntl(fds
[0], F_SETFL
, O_NONBLOCK
));
339 chk_error(fcntl(fds
[1], F_SETFL
, O_NONBLOCK
));
345 FD_SET(fds
[0], &rfds
);
348 FD_SET(fds
[1], &wfds
);
352 ret
= chk_error(select(fd_max
+ 1, &rfds
, &wfds
, NULL
, NULL
));
354 if (FD_ISSET(fds
[0], &rfds
)) {
355 chk_error(read(fds
[0], &ch
, 1));
357 if (rcount
>= WCOUNT_MAX
)
360 if (FD_ISSET(fds
[1], &wfds
)) {
362 chk_error(write(fds
[1], &ch
, 1));
367 chk_error(close(fds
[0]));
368 chk_error(close(fds
[1]));
371 static int thread1_res
;
372 static int thread2_res
;
374 static int thread1_func(void *arg
)
384 static int thread2_func(void *arg
)
394 static void wait_for_child(pid_t pid
)
397 chk_error(waitpid(pid
, &status
, 0));
400 /* For test_clone we must match the clone flags used by glibc, see
401 * CLONE_THREAD_FLAGS in the QEMU source code.
403 static void test_clone(void)
405 uint8_t *stack1
, *stack2
;
408 stack1
= malloc(STACK_SIZE
);
409 pid1
= chk_error(clone(thread1_func
, stack1
+ STACK_SIZE
,
410 CLONE_VM
| CLONE_FS
| CLONE_FILES
|
411 CLONE_SIGHAND
| CLONE_THREAD
| CLONE_SYSVSEM
,
414 stack2
= malloc(STACK_SIZE
);
415 pid2
= chk_error(clone(thread2_func
, stack2
+ STACK_SIZE
,
416 CLONE_VM
| CLONE_FS
| CLONE_FILES
|
417 CLONE_SIGHAND
| CLONE_THREAD
| CLONE_SYSVSEM
,
420 wait_for_child(pid1
);
422 wait_for_child(pid2
);
425 if (thread1_res
!= 5 ||
430 /***********************************/
432 volatile int alarm_count
;
435 static void sig_alarm(int sig
)
442 static void sig_segv(int sig
, siginfo_t
*info
, void *puc
)
449 static void test_signal(void)
451 struct sigaction act
;
452 struct itimerval it
, oit
;
458 act
.sa_handler
= sig_alarm
;
459 sigemptyset(&act
.sa_mask
);
461 chk_error(sigaction(SIGALRM
, &act
, NULL
));
463 it
.it_interval
.tv_sec
= 0;
464 it
.it_interval
.tv_usec
= 10 * 1000;
465 it
.it_value
.tv_sec
= 0;
466 it
.it_value
.tv_usec
= 10 * 1000;
467 chk_error(setitimer(ITIMER_REAL
, &it
, NULL
));
468 chk_error(getitimer(ITIMER_REAL
, &oit
));
470 while (alarm_count
< 5) {
472 getitimer(ITIMER_REAL
, &oit
);
475 it
.it_interval
.tv_sec
= 0;
476 it
.it_interval
.tv_usec
= 0;
477 it
.it_value
.tv_sec
= 0;
478 it
.it_value
.tv_usec
= 0;
479 memset(&oit
, 0xff, sizeof(oit
));
480 chk_error(setitimer(ITIMER_REAL
, &it
, &oit
));
483 act
.sa_sigaction
= sig_segv
;
484 sigemptyset(&act
.sa_mask
);
485 act
.sa_flags
= SA_SIGINFO
;
486 chk_error(sigaction(SIGSEGV
, &act
, NULL
));
487 if (setjmp(jmp_env
) == 0) {
491 act
.sa_handler
= SIG_DFL
;
492 sigemptyset(&act
.sa_mask
);
494 chk_error(sigaction(SIGSEGV
, &act
, NULL
));
497 #define SHM_SIZE 32768
499 static void test_shm(void)
504 shmid
= chk_error(shmget(IPC_PRIVATE
, SHM_SIZE
, IPC_CREAT
| 0777));
505 ptr
= shmat(shmid
, NULL
, 0);
509 memset(ptr
, 0, SHM_SIZE
);
511 chk_error(shmctl(shmid
, IPC_RMID
, 0));
512 chk_error(shmdt(ptr
));
515 int main(int argc
, char **argv
)
524 printf("test_clone still considered buggy\n");