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/>.
26
#include <sys/types.h>
34
#include <sys/socket.h>
35
#include <netinet/in.h>
36
#include <arpa/inet.h>
42
#define TESTPATH "/tmp/linux-test.tmp"
44
#define STACK_SIZE 16384
46
void error1(const char *filename, int line, const char *fmt, ...)
50
fprintf(stderr, "%s:%d: ", filename, line);
51
vfprintf(stderr, fmt, ap);
52
fprintf(stderr, "\n");
57
int __chk_error(const char *filename, int line, int ret)
60
error1(filename, line, "%m (ret=%d, errno=%d)",
66
#define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
68
#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
70
/*******************************************************/
72
#define FILE_BUF_SIZE 300
77
uint8_t buf[FILE_BUF_SIZE];
78
uint8_t buf2[FILE_BUF_SIZE];
79
uint8_t buf3[FILE_BUF_SIZE];
87
/* clean up, just in case */
88
unlink(TESTPATH "/file1");
89
unlink(TESTPATH "/file2");
90
unlink(TESTPATH "/file3");
93
if (getcwd(cur_dir, sizeof(cur_dir)) == NULL)
96
chk_error(mkdir(TESTPATH, 0755));
98
chk_error(chdir(TESTPATH));
100
/* open/read/write/close/readv/writev/lseek */
102
fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644));
103
for(i=0;i < FILE_BUF_SIZE; i++)
105
len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2));
106
if (len != (FILE_BUF_SIZE / 2))
108
vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2);
109
vecs[0].iov_len = 16;
110
vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16;
111
vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16;
112
len = chk_error(writev(fd, vecs, 2));
113
if (len != (FILE_BUF_SIZE / 2))
115
chk_error(close(fd));
117
chk_error(rename("file1", "file2"));
119
fd = chk_error(open("file2", O_RDONLY));
121
len = chk_error(read(fd, buf2, FILE_BUF_SIZE));
122
if (len != FILE_BUF_SIZE)
124
if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0)
128
ret = chk_error(lseek(fd, FOFFSET, SEEK_SET));
131
vecs[0].iov_base = buf3;
132
vecs[0].iov_len = 32;
133
vecs[1].iov_base = buf3 + 32;
134
vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32;
135
len = chk_error(readv(fd, vecs, 2));
136
if (len != FILE_BUF_SIZE - FOFFSET)
138
if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0)
141
chk_error(close(fd));
144
chk_error(access("file2", R_OK));
146
/* stat/chmod/utime/truncate */
148
chk_error(chmod("file2", 0600));
151
chk_error(truncate("file2", 100));
152
chk_error(utime("file2", &tbuf));
153
chk_error(stat("file2", &st));
154
if (st.st_size != 100)
156
if (!S_ISREG(st.st_mode))
158
if ((st.st_mode & 0777) != 0600)
160
if (st.st_atime != 1001 ||
164
chk_error(stat(TESTPATH, &st));
165
if (!S_ISDIR(st.st_mode))
169
fd = chk_error(open("file2", O_RDWR));
170
chk_error(ftruncate(fd, 50));
171
chk_error(fstat(fd, &st));
172
chk_error(close(fd));
174
if (st.st_size != 50)
176
if (!S_ISREG(st.st_mode))
180
chk_error(symlink("file2", "file3"));
181
chk_error(lstat("file3", &st));
182
if (!S_ISLNK(st.st_mode))
186
dir = opendir(TESTPATH);
194
if (strcmp(de->d_name, ".") != 0 &&
195
strcmp(de->d_name, "..") != 0 &&
196
strcmp(de->d_name, "file2") != 0 &&
197
strcmp(de->d_name, "file3") != 0)
205
chk_error(unlink("file3"));
206
chk_error(unlink("file2"));
207
chk_error(chdir(cur_dir));
208
chk_error(rmdir(TESTPATH));
215
pid = chk_error(fork());
220
chk_error(waitpid(pid, &status, 0));
221
if (!WIFEXITED(status) || WEXITSTATUS(status) != 2)
222
error("waitpid status=0x%x", status);
227
struct timeval tv, tv2;
228
struct timespec ts, rem;
229
struct rusage rusg1, rusg2;
232
chk_error(gettimeofday(&tv, NULL));
235
ts.tv_nsec = 20 * 1000000;
236
chk_error(nanosleep(&ts, &rem));
239
chk_error(gettimeofday(&tv2, NULL));
240
ti = tv2.tv_sec - tv.tv_sec;
242
error("gettimeofday");
244
chk_error(getrusage(RUSAGE_SELF, &rusg1));
245
for(i = 0;i < 10000; i++);
246
chk_error(getrusage(RUSAGE_SELF, &rusg2));
247
if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 ||
248
(rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0)
252
void pstrcpy(char *buf, int buf_size, const char *str)
262
if (c == 0 || q >= buf + buf_size - 1)
269
/* strcat and truncate. */
270
char *pstrcat(char *buf, int buf_size, const char *s)
275
pstrcpy(buf + len, buf_size - len, s);
279
int server_socket(void)
282
struct sockaddr_in sockaddr;
285
fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
288
chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)));
290
sockaddr.sin_family = AF_INET;
291
sockaddr.sin_port = htons(TESTPORT);
292
sockaddr.sin_addr.s_addr = 0;
293
chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
294
chk_error(listen(fd, 0));
299
int client_socket(void)
302
struct sockaddr_in sockaddr;
305
fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
306
sockaddr.sin_family = AF_INET;
307
sockaddr.sin_port = htons(TESTPORT);
308
inet_aton("127.0.0.1", &sockaddr.sin_addr);
309
chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
313
const char socket_msg[] = "hello socket\n";
315
void test_socket(void)
317
int server_fd, client_fd, fd, pid, ret, val;
318
struct sockaddr_in sockaddr;
322
server_fd = server_socket();
324
/* test a few socket options */
326
chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len));
327
if (val != SOCK_STREAM)
330
pid = chk_error(fork());
332
client_fd = client_socket();
333
send(client_fd, socket_msg, sizeof(socket_msg), 0);
337
len = sizeof(sockaddr);
338
fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len));
340
ret = chk_error(recv(fd, buf, sizeof(buf), 0));
341
if (ret != sizeof(socket_msg))
343
if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0)
345
chk_error(close(fd));
346
chk_error(close(server_fd));
349
#define WCOUNT_MAX 512
354
int fds[2], fd_max, ret;
358
chk_error(pipe(fds));
359
chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK));
360
chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK));
366
FD_SET(fds[0], &rfds);
369
FD_SET(fds[1], &wfds);
373
ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL));
375
if (FD_ISSET(fds[0], &rfds)) {
376
chk_error(read(fds[0], &ch, 1));
378
if (rcount >= WCOUNT_MAX)
381
if (FD_ISSET(fds[1], &wfds)) {
383
chk_error(write(fds[0], &ch, 1));
388
chk_error(close(fds[0]));
389
chk_error(close(fds[1]));
395
int thread1_func(void *arg)
405
int thread2_func(void *arg)
415
void test_clone(void)
417
uint8_t *stack1, *stack2;
418
int pid1, pid2, status1, status2;
420
stack1 = malloc(STACK_SIZE);
421
pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE,
422
CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello1"));
424
stack2 = malloc(STACK_SIZE);
425
pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE,
426
CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello2"));
428
while (waitpid(pid1, &status1, 0) != pid1);
430
while (waitpid(pid2, &status2, 0) != pid2);
432
if (thread1_res != 5 ||
437
/***********************************/
439
volatile int alarm_count;
442
void sig_alarm(int sig)
449
void sig_segv(int sig, siginfo_t *info, void *puc)
456
void test_signal(void)
458
struct sigaction act;
459
struct itimerval it, oit;
465
act.sa_handler = sig_alarm;
466
sigemptyset(&act.sa_mask);
468
chk_error(sigaction(SIGALRM, &act, NULL));
470
it.it_interval.tv_sec = 0;
471
it.it_interval.tv_usec = 10 * 1000;
472
it.it_value.tv_sec = 0;
473
it.it_value.tv_usec = 10 * 1000;
474
chk_error(setitimer(ITIMER_REAL, &it, NULL));
475
chk_error(getitimer(ITIMER_REAL, &oit));
476
if (oit.it_value.tv_sec != it.it_value.tv_sec ||
477
oit.it_value.tv_usec != it.it_value.tv_usec)
480
while (alarm_count < 5) {
484
it.it_interval.tv_sec = 0;
485
it.it_interval.tv_usec = 0;
486
it.it_value.tv_sec = 0;
487
it.it_value.tv_usec = 0;
488
memset(&oit, 0xff, sizeof(oit));
489
chk_error(setitimer(ITIMER_REAL, &it, &oit));
490
if (oit.it_value.tv_sec != 0 ||
491
oit.it_value.tv_usec != 10 * 1000)
495
act.sa_sigaction = sig_segv;
496
sigemptyset(&act.sa_mask);
497
act.sa_flags = SA_SIGINFO;
498
chk_error(sigaction(SIGSEGV, &act, NULL));
499
if (setjmp(jmp_env) == 0) {
503
act.sa_handler = SIG_DFL;
504
sigemptyset(&act.sa_mask);
506
chk_error(sigaction(SIGSEGV, &act, NULL));
509
#define SHM_SIZE 32768
516
shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777));
517
ptr = shmat(shmid, NULL, 0);
521
memset(ptr, 0, SHM_SIZE);
523
chk_error(shmctl(shmid, IPC_RMID, 0));
524
chk_error(shmdt(ptr));
527
int main(int argc, char **argv)