From 8ed57276426392f961d557ea0bf54d9ba6ac4a5f Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 25 Feb 2009 14:24:02 +0000 Subject: [PATCH] By Hans-Christian Egtvedt (hans-christian.egtvedt AT atmel.com): strace.c: suppress "warning: unused static" message by adding #ifdef's around a variable .gitignore: trivial test/*.c: cleanup (suppress warnings, much better style). --- .gitignore | 2 + strace.c | 3 +- test/childthread.c | 77 +++++++++++++++++++------------------ test/clone.c | 13 ++++--- test/fork.c | 10 +++-- test/leaderkill.c | 76 ++++++++++++++++++------------------ test/many_looping_threads.c | 4 +- test/procpollable.c | 11 +++++- test/sfd.c | 11 +++++- test/sig.c | 16 ++++---- test/sigkill_rain.c | 30 +++++++++++---- test/skodic.c | 39 +++++++++++-------- test/vfork.c | 10 +++-- 13 files changed, 178 insertions(+), 124 deletions(-) diff --git a/.gitignore b/.gitignore index 7977f446..96160779 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ +*~ *.o .deps .libs +.*.swp Makefile Makefile.in diff --git a/strace.c b/strace.c index cded401e..1acecf28 100644 --- a/strace.c +++ b/strace.c @@ -108,8 +108,9 @@ int not_failing_only = 0; static int exit_code = 0; static int strace_child = 0; static int ptrace_stop_sig = SIGTRAP; +#if defined LINUX && (defined PTRACE_SETOPTIONS || defined PT_SETOPTIONS) static bool ptrace_opts_set; - +#endif static char *username = NULL; uid_t run_uid; gid_t run_gid; diff --git a/test/childthread.c b/test/childthread.c index bd0174b8..e89fb145 100644 --- a/test/childthread.c +++ b/test/childthread.c @@ -13,47 +13,48 @@ #include #include -static void *start0 (void *arg) +static void *start0(void *arg) { - pause (); - /* NOTREACHED */ - assert (0); + pause(); + /* NOTREACHED */ + assert(0); } -int main (void) +int main(int argc, char *argv[]) { - pthread_t thread0; - int i; - pid_t child, got_pid; - int status; + pthread_t thread0; + pid_t child, got_pid; + int status; + int i; - child = fork (); - switch (child) - { - case -1: - assert (0); - case 0: - i = pthread_create (&thread0, NULL, start0, NULL); - assert (i == 0); - /* The thread must be initialized, it becomes thread-child of this - process-child (child of a child of the toplevel process). */ - sleep (1); - /* Here the child TCB cannot be deallocated as there still exist - * children (the thread child in START0). */ - exit (42); - /* NOTREACHED */ - assert (0); - default: - /* We must not be waiting in WAITPID when the child double-exits. */ - sleep (2); - /* PID must be -1. */ - got_pid = waitpid (-1, &status, 0); - assert (got_pid == child); - assert (WIFEXITED (status)); - assert (WEXITSTATUS (status) == 42); - puts ("OK"); - exit (0); - } - /* NOTREACHED */ - assert (0); + child = fork(); + + switch(child) { + case -1: + assert(0); + case 0: + i = pthread_create(&thread0, NULL, start0, NULL); + assert(i == 0); + /* The thread must be initialized, it becomes thread-child of this + process-child (child of a child of the toplevel process). */ + sleep(1); + /* Here the child TCB cannot be deallocated as there still exist + * children (the thread child in START0). */ + exit(42); + /* NOTREACHED */ + assert(0); + default: + /* We must not be waiting in WAITPID when the child double-exits. */ + sleep(2); + /* PID must be -1. */ + got_pid = waitpid(-1, &status, 0); + assert(got_pid == child); + assert(WIFEXITED(status)); + assert(WEXITSTATUS(status) == 42); + puts("OK"); + exit(0); + } + + /* NOTREACHED */ + assert(0); } diff --git a/test/clone.c b/test/clone.c index 75bc5451..335086fc 100644 --- a/test/clone.c +++ b/test/clone.c @@ -1,15 +1,16 @@ #include +#include #include -int child(void* arg) { - write(1, "clone\n", 6); - return 0; +int child(void* arg) +{ + write(1, "clone\n", 6); + return 0; } -int -main() +int main(int argc, char *argv[]) { - char stack[4096]; + char stack[4096]; clone(child, stack+4000, CLONE_VM|CLONE_FS|CLONE_FILES, NULL); write(1, "original\n", 9); exit(0); diff --git a/test/fork.c b/test/fork.c index 3f68f672..9bed1fe4 100644 --- a/test/fork.c +++ b/test/fork.c @@ -1,10 +1,14 @@ -main() +#include +#include + +int main(int argc, char *argv[]) { - if (fork() == 0) + if (fork() == 0) { write(1, "child\n", 6); - else { + } else { wait(0); write(1, "parent\n", 7); } + exit(0); } diff --git a/test/leaderkill.c b/test/leaderkill.c index 18d6e123..ebb6ad1c 100644 --- a/test/leaderkill.c +++ b/test/leaderkill.c @@ -13,52 +13,52 @@ #include #include -static void *start0 (void *arg) +static void *start0(void *arg) { - sleep (1); - - exit (42); + sleep(1); + exit(42); } -static void *start1 (void *arg) +static void *start1(void *arg) { - pause (); - /* NOTREACHED */ - assert (0); + pause(); + /* NOTREACHED */ + assert(0); } -int main (void) +int main(int argc, char *argv[]) { - pthread_t thread0; - pthread_t thread1; - int i; - pid_t child, got_pid; - int status; + pthread_t thread0; + pthread_t thread1; + pid_t child, got_pid; + int status; + int i; + + sleep(2); + + child = fork(); - sleep (2); + switch(child) { + case -1: + abort(); + case 0: + i = pthread_create(&thread0, NULL, start0, NULL); + assert(i == 0); + i = pthread_create(&thread1, NULL, start1, NULL); + assert(i == 0); + pause(); + /* NOTREACHED */ + assert(0); + break; + default: + got_pid = waitpid(child, &status, 0); + assert(got_pid == child); + assert(WIFEXITED(status)); + assert(WEXITSTATUS(status) == 42); + puts("OK"); + exit(0); + } - child = fork (); - switch (child) - { - case -1: - abort (); - case 0: - i = pthread_create (&thread0, NULL, start0, NULL); - assert (i == 0); - i = pthread_create (&thread1, NULL, start1, NULL); - assert (i == 0); - pause (); /* NOTREACHED */ - assert (0); - break; - default: - got_pid = waitpid (child, &status, 0); - assert (got_pid == child); - assert (WIFEXITED (status)); - assert (WEXITSTATUS (status) == 42); - puts ("OK"); - exit (0); - } - /* NOTREACHED */ - abort (); + abort(); } diff --git a/test/many_looping_threads.c b/test/many_looping_threads.c index d8f7f8d8..1f1fe052 100644 --- a/test/many_looping_threads.c +++ b/test/many_looping_threads.c @@ -17,7 +17,7 @@ static void *sub_thd(void *c) return NULL; } -int main(int argc, char **argv) +int main(int argc, char *argv[]) { int i; pthread_t *thd; @@ -28,10 +28,12 @@ int main(int argc, char **argv) thd = malloc(num_threads * sizeof(thd[0])); fprintf(stderr, "test start, num_threads:%d...\n", num_threads); + for (i = 0; i < num_threads; i++) { pthread_create(&thd[i], NULL, sub_thd, NULL); fprintf(stderr, "after pthread_create\n"); } + /* Exit. This kills all threads */ return 0; } diff --git a/test/procpollable.c b/test/procpollable.c index fc599b56..a841af14 100644 --- a/test/procpollable.c +++ b/test/procpollable.c @@ -1,9 +1,11 @@ #include +#include #include #include #include #include -main() + +int main(int argc, char *argv[]) { int pid; char proc[32]; @@ -14,17 +16,24 @@ main() pause(); exit(0); } + sprintf(proc, "/proc/%d", pid); + if ((pfp = fopen(proc, "r+")) == NULL) goto fail; + if (ioctl(fileno(pfp), PIOCSTOP, NULL) < 0) goto fail; + pfd.fd = fileno(pfp); pfd.events = POLLPRI; + if (poll(&pfd, 1, 0) < 0) goto fail; + if (!(pfd.revents & POLLPRI)) goto fail; + kill(pid, SIGKILL); exit(0); fail: diff --git a/test/sfd.c b/test/sfd.c index 081de01e..b5ff847a 100644 --- a/test/sfd.c +++ b/test/sfd.c @@ -1,6 +1,7 @@ #include #include -main(int argc, char *argv[]) + +int main(int argc, char *argv[]) { int pid = atoi(argv[1]); int sfd; @@ -11,22 +12,28 @@ main(int argc, char *argv[]) int signal, blocked, ignore, caught; sprintf(sname, "/proc/%d/stat", pid); + if ((sfd = open(sname, O_RDONLY)) == -1) { perror(sname); return 1; } + i = read(sfd, buf, 1024); buf[i] = '\0'; + for (i = 0, s = buf; i < 30; i++) { while (*++s != ' ') { if (!*s) break; } } + if (sscanf(s, "%d%d%d%d", &signal, &blocked, &ignore, &caught) != 4) { fprintf(stderr, "/proc/pid/stat format error\n"); return 1; } + printf("%8x %8x %8x %8x\n", signal, blocked, ignore, caught); - return 1; + + return 0; } diff --git a/test/sig.c b/test/sig.c index 930a177f..60a9dc9e 100644 --- a/test/sig.c +++ b/test/sig.c @@ -1,16 +1,18 @@ +#include #include -main() + +void interrupt() +{ + write(2, "xyzzy\n", 6); +} + +int main(int argc, char *argv[]) { char buf[1024]; - void interrupt(); signal(SIGINT, interrupt); read(0, buf, 1024); write(2, "qwerty\n", 7); - exit(0); -} -interrupt() -{ - write(2, "xyzzy\n", 6); + return 0; } diff --git a/test/sigkill_rain.c b/test/sigkill_rain.c index 4306a8ac..dd83a692 100644 --- a/test/sigkill_rain.c +++ b/test/sigkill_rain.c @@ -1,14 +1,16 @@ /* This test is not yet added to Makefile */ +#include #include #include #include #include #include + static const struct sockaddr sa; -int main(int argc, char **argv) +int main(int argc, char *argv[]) { int loops; int pid; @@ -24,7 +26,10 @@ int main(int argc, char **argv) while (--loops >= 0) { pid = fork(); - if (pid < 0) _exit(1); + + if (pid < 0) + exit(1); + if (!pid) { /* child */ int child = getpid(); @@ -32,12 +37,16 @@ int main(int argc, char **argv) loops = 99; while (--loops) { pid = fork(); - if (pid < 0) _exit(1); + + if (pid < 0) + exit(1); + if (!pid) { /* grandchild: kill child */ kill(child, SIGKILL); - _exit(0); + exit(0); } + /* Add various syscalls you want to test here. * strace will decode them and suddenly find * process disappearing. @@ -47,16 +56,23 @@ int main(int argc, char **argv) * decode syscall number before process dies. */ switch (loops & 1) { - case 0: /* empty */ break; - case 1: sendto(-1, "Hello cruel world", 17, 0, &sa, sizeof(sa)); break; + case 0: + break; /* intentional empty */ + case 1: + sendto(-1, "Hello cruel world", 17, 0, &sa, sizeof(sa)); + break; } + /* kill grandchild */ kill(pid, SIGKILL); } - _exit(0); + + exit(0); } + /* parent */ wait(NULL); } + return 0; } diff --git a/test/skodic.c b/test/skodic.c index 14955783..09967bdd 100644 --- a/test/skodic.c +++ b/test/skodic.c @@ -6,28 +6,33 @@ #include #include +#include #include #include #include #include #include -int -main(void) +int main(int argc, char *argv[]) { - char *c = (char*)0x94000000; - int fd; - open( "/tmp/delme", O_RDWR ); - mmap( c, 4096, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, 3, 0 ); - *c = 0; - if (fork()) { - while(1) { - strcpy( c, "/etc/passwd" ); - strcpy( c, "/etc/shadow" ); - } - } else - while (1) - if ((fd=open( c, 0 ))!=-1) - close(fd); - return 0; + /* XXX: x86 specific stuff? */ + char *c = (char*) 0x94000000; + int fd; + + open("/tmp/delme", O_RDWR); + mmap(c, 4096, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, 3, 0); + *c = 0; + + if (fork()) { + while(1) { + strcpy(c, "/etc/passwd"); + strcpy(c, "/etc/shadow"); + } + } else { + while (1) + if ((fd = open(c, 0)) != -1) + close(fd); + } + + return 0; } diff --git a/test/vfork.c b/test/vfork.c index 2c2d6032..367dc019 100644 --- a/test/vfork.c +++ b/test/vfork.c @@ -1,10 +1,14 @@ -main() +#include +#include + +int main(int argc, char *argv[]) { - if (vfork() == 0) + if (vfork() == 0) { write(1, "child\n", 6); - else { + } else { wait(0); write(1, "parent\n", 7); } + exit(0); } -- 2.40.0