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>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
44 #define TESTPATH "/tmp/linux-test.tmp"
46 #define STACK_SIZE 16384
48 void error1(const char *filename
, int line
, const char *fmt
, ...)
52 fprintf(stderr
, "%s:%d: ", filename
, line
);
53 vfprintf(stderr
, fmt
, ap
);
54 fprintf(stderr
, "\n");
59 int __chk_error(const char *filename
, int line
, int ret
)
62 error1(filename
, line
, "%m (ret=%d, errno=%d)",
68 #define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
70 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
72 /*******************************************************/
74 #define FILE_BUF_SIZE 300
79 uint8_t buf
[FILE_BUF_SIZE
];
80 uint8_t buf2
[FILE_BUF_SIZE
];
81 uint8_t buf3
[FILE_BUF_SIZE
];
89 /* clean up, just in case */
90 unlink(TESTPATH
"/file1");
91 unlink(TESTPATH
"/file2");
92 unlink(TESTPATH
"/file3");
95 if (getcwd(cur_dir
, sizeof(cur_dir
)) == NULL
)
98 chk_error(mkdir(TESTPATH
, 0755));
100 chk_error(chdir(TESTPATH
));
102 /* open/read/write/close/readv/writev/lseek */
104 fd
= chk_error(open("file1", O_WRONLY
| O_TRUNC
| O_CREAT
, 0644));
105 for(i
=0;i
< FILE_BUF_SIZE
; i
++)
107 len
= chk_error(write(fd
, buf
, FILE_BUF_SIZE
/ 2));
108 if (len
!= (FILE_BUF_SIZE
/ 2))
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))
117 chk_error(close(fd
));
119 chk_error(rename("file1", "file2"));
121 fd
= chk_error(open("file2", O_RDONLY
));
123 len
= chk_error(read(fd
, buf2
, FILE_BUF_SIZE
));
124 if (len
!= FILE_BUF_SIZE
)
126 if (memcmp(buf
, buf2
, FILE_BUF_SIZE
) != 0)
130 ret
= chk_error(lseek(fd
, FOFFSET
, SEEK_SET
));
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
)
140 if (memcmp(buf
+ FOFFSET
, buf3
, FILE_BUF_SIZE
- FOFFSET
) != 0)
143 chk_error(close(fd
));
146 chk_error(access("file2", R_OK
));
148 /* stat/chmod/utime/truncate */
150 chk_error(chmod("file2", 0600));
153 chk_error(truncate("file2", 100));
154 chk_error(utime("file2", &tbuf
));
155 chk_error(stat("file2", &st
));
156 if (st
.st_size
!= 100)
158 if (!S_ISREG(st
.st_mode
))
160 if ((st
.st_mode
& 0777) != 0600)
162 if (st
.st_atime
!= 1001 ||
166 chk_error(stat(TESTPATH
, &st
));
167 if (!S_ISDIR(st
.st_mode
))
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
));
176 if (st
.st_size
!= 50)
178 if (!S_ISREG(st
.st_mode
))
182 chk_error(symlink("file2", "file3"));
183 chk_error(lstat("file3", &st
));
184 if (!S_ISLNK(st
.st_mode
))
188 dir
= opendir(TESTPATH
);
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)
207 chk_error(unlink("file3"));
208 chk_error(unlink("file2"));
209 chk_error(chdir(cur_dir
));
210 chk_error(rmdir(TESTPATH
));
217 pid
= chk_error(fork());
222 chk_error(waitpid(pid
, &status
, 0));
223 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 2)
224 error("waitpid status=0x%x", status
);
229 struct timeval tv
, tv2
;
230 struct timespec ts
, rem
;
231 struct rusage rusg1
, rusg2
;
234 chk_error(gettimeofday(&tv
, NULL
));
237 ts
.tv_nsec
= 20 * 1000000;
238 chk_error(nanosleep(&ts
, &rem
));
241 chk_error(gettimeofday(&tv2
, NULL
));
242 ti
= tv2
.tv_sec
- tv
.tv_sec
;
244 error("gettimeofday");
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)
254 void pstrcpy(char *buf
, int buf_size
, const char *str
)
264 if (c
== 0 || q
>= buf
+ buf_size
- 1)
271 /* strcat and truncate. */
272 char *pstrcat(char *buf
, int buf_size
, const char *s
)
277 pstrcpy(buf
+ len
, buf_size
- len
, s
);
281 int server_socket(void)
284 struct sockaddr_in sockaddr
;
287 fd
= chk_error(socket(PF_INET
, SOCK_STREAM
, 0));
290 chk_error(setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)));
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));
301 int client_socket(void)
304 struct sockaddr_in sockaddr
;
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
)));
315 const char socket_msg
[] = "hello socket\n";
317 void test_socket(void)
319 int server_fd
, client_fd
, fd
, pid
, ret
, val
;
320 struct sockaddr_in sockaddr
;
324 server_fd
= server_socket();
326 /* test a few socket options */
328 chk_error(getsockopt(server_fd
, SOL_SOCKET
, SO_TYPE
, &val
, &len
));
329 if (val
!= SOCK_STREAM
)
332 pid
= chk_error(fork());
334 client_fd
= client_socket();
335 send(client_fd
, socket_msg
, sizeof(socket_msg
), 0);
339 len
= sizeof(sockaddr
);
340 fd
= chk_error(accept(server_fd
, (struct sockaddr
*)&sockaddr
, &len
));
342 ret
= chk_error(recv(fd
, buf
, sizeof(buf
), 0));
343 if (ret
!= sizeof(socket_msg
))
345 if (memcmp(buf
, socket_msg
, sizeof(socket_msg
)) != 0)
347 chk_error(close(fd
));
348 chk_error(close(server_fd
));
351 #define WCOUNT_MAX 512
356 int fds
[2], fd_max
, ret
;
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
));
368 FD_SET(fds
[0], &rfds
);
371 FD_SET(fds
[1], &wfds
);
375 ret
= chk_error(select(fd_max
+ 1, &rfds
, &wfds
, NULL
, NULL
));
377 if (FD_ISSET(fds
[0], &rfds
)) {
378 chk_error(read(fds
[0], &ch
, 1));
380 if (rcount
>= WCOUNT_MAX
)
383 if (FD_ISSET(fds
[1], &wfds
)) {
385 chk_error(write(fds
[0], &ch
, 1));
390 chk_error(close(fds
[0]));
391 chk_error(close(fds
[1]));
397 int thread1_func(void *arg
)
407 int thread2_func(void *arg
)
417 void test_clone(void)
419 uint8_t *stack1
, *stack2
;
420 int pid1
, pid2
, status1
, status2
;
422 stack1
= malloc(STACK_SIZE
);
423 pid1
= chk_error(clone(thread1_func
, stack1
+ STACK_SIZE
,
424 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, "hello1"));
426 stack2
= malloc(STACK_SIZE
);
427 pid2
= chk_error(clone(thread2_func
, stack2
+ STACK_SIZE
,
428 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, "hello2"));
430 while (waitpid(pid1
, &status1
, 0) != pid1
);
432 while (waitpid(pid2
, &status2
, 0) != pid2
);
434 if (thread1_res
!= 5 ||
439 /***********************************/
441 volatile int alarm_count
;
444 void sig_alarm(int sig
)
451 void sig_segv(int sig
, siginfo_t
*info
, void *puc
)
458 void test_signal(void)
460 struct sigaction act
;
461 struct itimerval it
, oit
;
467 act
.sa_handler
= sig_alarm
;
468 sigemptyset(&act
.sa_mask
);
470 chk_error(sigaction(SIGALRM
, &act
, NULL
));
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
)
482 while (alarm_count
< 5) {
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)
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) {
505 act
.sa_handler
= SIG_DFL
;
506 sigemptyset(&act
.sa_mask
);
508 chk_error(sigaction(SIGSEGV
, &act
, NULL
));
511 #define SHM_SIZE 32768
518 shmid
= chk_error(shmget(IPC_PRIVATE
, SHM_SIZE
, IPC_CREAT
| 0777));
519 ptr
= shmat(shmid
, NULL
, 0);
523 memset(ptr
, 0, SHM_SIZE
);
525 chk_error(shmctl(shmid
, IPC_RMID
, 0));
526 chk_error(shmdt(ptr
));
529 int main(int argc
, char **argv
)