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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
43 #define TESTPATH "/tmp/linux-test.tmp"
45 #define STACK_SIZE 16384
47 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 int __chk_error(const char *filename
, int line
, int ret
)
61 error1(filename
, line
, "%m (ret=%d, errno=%d)",
67 #define error(fmt, args...) error1(__FILE__, __LINE__, fmt, ##args)
69 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
71 /*******************************************************/
73 #define FILE_BUF_SIZE 300
78 uint8_t buf
[FILE_BUF_SIZE
];
79 uint8_t buf2
[FILE_BUF_SIZE
];
80 uint8_t buf3
[FILE_BUF_SIZE
];
88 /* clean up, just in case */
89 unlink(TESTPATH
"/file1");
90 unlink(TESTPATH
"/file2");
91 unlink(TESTPATH
"/file3");
94 if (getcwd(cur_dir
, sizeof(cur_dir
)) == NULL
)
97 chk_error(mkdir(TESTPATH
, 0755));
99 chk_error(chdir(TESTPATH
));
101 /* open/read/write/close/readv/writev/lseek */
103 fd
= chk_error(open("file1", O_WRONLY
| O_TRUNC
| O_CREAT
, 0644));
104 for(i
=0;i
< FILE_BUF_SIZE
; i
++)
106 len
= chk_error(write(fd
, buf
, FILE_BUF_SIZE
/ 2));
107 if (len
!= (FILE_BUF_SIZE
/ 2))
109 vecs
[0].iov_base
= buf
+ (FILE_BUF_SIZE
/ 2);
110 vecs
[0].iov_len
= 16;
111 vecs
[1].iov_base
= buf
+ (FILE_BUF_SIZE
/ 2) + 16;
112 vecs
[1].iov_len
= (FILE_BUF_SIZE
/ 2) - 16;
113 len
= chk_error(writev(fd
, vecs
, 2));
114 if (len
!= (FILE_BUF_SIZE
/ 2))
116 chk_error(close(fd
));
118 chk_error(rename("file1", "file2"));
120 fd
= chk_error(open("file2", O_RDONLY
));
122 len
= chk_error(read(fd
, buf2
, FILE_BUF_SIZE
));
123 if (len
!= FILE_BUF_SIZE
)
125 if (memcmp(buf
, buf2
, FILE_BUF_SIZE
) != 0)
129 ret
= chk_error(lseek(fd
, FOFFSET
, SEEK_SET
));
132 vecs
[0].iov_base
= buf3
;
133 vecs
[0].iov_len
= 32;
134 vecs
[1].iov_base
= buf3
+ 32;
135 vecs
[1].iov_len
= FILE_BUF_SIZE
- FOFFSET
- 32;
136 len
= chk_error(readv(fd
, vecs
, 2));
137 if (len
!= FILE_BUF_SIZE
- FOFFSET
)
139 if (memcmp(buf
+ FOFFSET
, buf3
, FILE_BUF_SIZE
- FOFFSET
) != 0)
142 chk_error(close(fd
));
145 chk_error(access("file2", R_OK
));
147 /* stat/chmod/utime/truncate */
149 chk_error(chmod("file2", 0600));
152 chk_error(truncate("file2", 100));
153 chk_error(utime("file2", &tbuf
));
154 chk_error(stat("file2", &st
));
155 if (st
.st_size
!= 100)
157 if (!S_ISREG(st
.st_mode
))
159 if ((st
.st_mode
& 0777) != 0600)
161 if (st
.st_atime
!= 1001 ||
165 chk_error(stat(TESTPATH
, &st
));
166 if (!S_ISDIR(st
.st_mode
))
170 fd
= chk_error(open("file2", O_RDWR
));
171 chk_error(ftruncate(fd
, 50));
172 chk_error(fstat(fd
, &st
));
173 chk_error(close(fd
));
175 if (st
.st_size
!= 50)
177 if (!S_ISREG(st
.st_mode
))
181 chk_error(symlink("file2", "file3"));
182 chk_error(lstat("file3", &st
));
183 if (!S_ISLNK(st
.st_mode
))
187 dir
= opendir(TESTPATH
);
195 if (strcmp(de
->d_name
, ".") != 0 &&
196 strcmp(de
->d_name
, "..") != 0 &&
197 strcmp(de
->d_name
, "file2") != 0 &&
198 strcmp(de
->d_name
, "file3") != 0)
206 chk_error(unlink("file3"));
207 chk_error(unlink("file2"));
208 chk_error(chdir(cur_dir
));
209 chk_error(rmdir(TESTPATH
));
216 pid
= chk_error(fork());
221 chk_error(waitpid(pid
, &status
, 0));
222 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 2)
223 error("waitpid status=0x%x", status
);
228 struct timeval tv
, tv2
;
229 struct timespec ts
, rem
;
230 struct rusage rusg1
, rusg2
;
233 chk_error(gettimeofday(&tv
, NULL
));
236 ts
.tv_nsec
= 20 * 1000000;
237 chk_error(nanosleep(&ts
, &rem
));
240 chk_error(gettimeofday(&tv2
, NULL
));
241 ti
= tv2
.tv_sec
- tv
.tv_sec
;
243 error("gettimeofday");
245 chk_error(getrusage(RUSAGE_SELF
, &rusg1
));
246 for(i
= 0;i
< 10000; i
++);
247 chk_error(getrusage(RUSAGE_SELF
, &rusg2
));
248 if ((rusg2
.ru_utime
.tv_sec
- rusg1
.ru_utime
.tv_sec
) < 0 ||
249 (rusg2
.ru_stime
.tv_sec
- rusg1
.ru_stime
.tv_sec
) < 0)
253 void pstrcpy(char *buf
, int buf_size
, const char *str
)
263 if (c
== 0 || q
>= buf
+ buf_size
- 1)
270 /* strcat and truncate. */
271 char *pstrcat(char *buf
, int buf_size
, const char *s
)
276 pstrcpy(buf
+ len
, buf_size
- len
, s
);
280 int server_socket(void)
283 struct sockaddr_in sockaddr
;
286 fd
= chk_error(socket(PF_INET
, SOCK_STREAM
, 0));
289 chk_error(setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)));
291 sockaddr
.sin_family
= AF_INET
;
292 sockaddr
.sin_port
= htons(TESTPORT
);
293 sockaddr
.sin_addr
.s_addr
= 0;
294 chk_error(bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
)));
295 chk_error(listen(fd
, 0));
300 int client_socket(void)
303 struct sockaddr_in sockaddr
;
306 fd
= chk_error(socket(PF_INET
, SOCK_STREAM
, 0));
307 sockaddr
.sin_family
= AF_INET
;
308 sockaddr
.sin_port
= htons(TESTPORT
);
309 inet_aton("127.0.0.1", &sockaddr
.sin_addr
);
310 chk_error(connect(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
)));
314 const char socket_msg
[] = "hello socket\n";
316 void test_socket(void)
318 int server_fd
, client_fd
, fd
, pid
, ret
, val
;
319 struct sockaddr_in sockaddr
;
323 server_fd
= server_socket();
325 /* test a few socket options */
327 chk_error(getsockopt(server_fd
, SOL_SOCKET
, SO_TYPE
, &val
, &len
));
328 if (val
!= SOCK_STREAM
)
331 pid
= chk_error(fork());
333 client_fd
= client_socket();
334 send(client_fd
, socket_msg
, sizeof(socket_msg
), 0);
338 len
= sizeof(sockaddr
);
339 fd
= chk_error(accept(server_fd
, (struct sockaddr
*)&sockaddr
, &len
));
341 ret
= chk_error(recv(fd
, buf
, sizeof(buf
), 0));
342 if (ret
!= sizeof(socket_msg
))
344 if (memcmp(buf
, socket_msg
, sizeof(socket_msg
)) != 0)
346 chk_error(close(fd
));
347 chk_error(close(server_fd
));
350 #define WCOUNT_MAX 512
355 int fds
[2], fd_max
, ret
;
359 chk_error(pipe(fds
));
360 chk_error(fcntl(fds
[0], F_SETFL
, O_NONBLOCK
));
361 chk_error(fcntl(fds
[1], F_SETFL
, O_NONBLOCK
));
367 FD_SET(fds
[0], &rfds
);
370 FD_SET(fds
[1], &wfds
);
374 ret
= chk_error(select(fd_max
+ 1, &rfds
, &wfds
, NULL
, NULL
));
376 if (FD_ISSET(fds
[0], &rfds
)) {
377 chk_error(read(fds
[0], &ch
, 1));
379 if (rcount
>= WCOUNT_MAX
)
382 if (FD_ISSET(fds
[1], &wfds
)) {
384 chk_error(write(fds
[0], &ch
, 1));
389 chk_error(close(fds
[0]));
390 chk_error(close(fds
[1]));
396 int thread1_func(void *arg
)
406 int thread2_func(void *arg
)
416 void test_clone(void)
418 uint8_t *stack1
, *stack2
;
419 int pid1
, pid2
, status1
, status2
;
421 stack1
= malloc(STACK_SIZE
);
422 pid1
= chk_error(clone(thread1_func
, stack1
+ STACK_SIZE
,
423 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, "hello1"));
425 stack2
= malloc(STACK_SIZE
);
426 pid2
= chk_error(clone(thread2_func
, stack2
+ STACK_SIZE
,
427 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, "hello2"));
429 while (waitpid(pid1
, &status1
, 0) != pid1
);
430 while (waitpid(pid2
, &status2
, 0) != pid2
);
431 if (thread1_res
!= 5 ||
436 /***********************************/
438 volatile int alarm_count
;
441 void sig_alarm(int sig
)
448 void sig_segv(int sig
, siginfo_t
*info
, void *puc
)
455 void test_signal(void)
457 struct sigaction act
;
458 struct itimerval it
, oit
;
464 act
.sa_handler
= sig_alarm
;
465 sigemptyset(&act
.sa_mask
);
467 chk_error(sigaction(SIGALRM
, &act
, NULL
));
469 it
.it_interval
.tv_sec
= 0;
470 it
.it_interval
.tv_usec
= 10 * 1000;
471 it
.it_value
.tv_sec
= 0;
472 it
.it_value
.tv_usec
= 10 * 1000;
473 chk_error(setitimer(ITIMER_REAL
, &it
, NULL
));
474 chk_error(getitimer(ITIMER_REAL
, &oit
));
475 if (oit
.it_value
.tv_sec
!= it
.it_value
.tv_sec
||
476 oit
.it_value
.tv_usec
!= it
.it_value
.tv_usec
)
479 while (alarm_count
< 5) {
483 it
.it_interval
.tv_sec
= 0;
484 it
.it_interval
.tv_usec
= 0;
485 it
.it_value
.tv_sec
= 0;
486 it
.it_value
.tv_usec
= 0;
487 memset(&oit
, 0xff, sizeof(oit
));
488 chk_error(setitimer(ITIMER_REAL
, &it
, &oit
));
489 if (oit
.it_value
.tv_sec
!= 0 ||
490 oit
.it_value
.tv_usec
!= 10 * 1000)
494 act
.sa_sigaction
= sig_segv
;
495 sigemptyset(&act
.sa_mask
);
496 act
.sa_flags
= SA_SIGINFO
;
497 chk_error(sigaction(SIGSEGV
, &act
, NULL
));
498 if (setjmp(jmp_env
) == 0) {
502 act
.sa_handler
= SIG_DFL
;
503 sigemptyset(&act
.sa_mask
);
505 chk_error(sigaction(SIGSEGV
, &act
, NULL
));
508 #define SHM_SIZE 32768
515 shmid
= chk_error(shmget(IPC_PRIVATE
, SHM_SIZE
, IPC_CREAT
| 0777));
516 ptr
= shmat(shmid
, NULL
, 0);
520 memset(ptr
, 0, SHM_SIZE
);
522 chk_error(shmctl(shmid
, IPC_RMID
, 0));
523 chk_error(shmdt(ptr
));
526 int main(int argc
, char **argv
)