lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * clone test for uClibc |
| 4 | * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org> |
| 5 | * |
| 6 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <unistd.h> |
| 12 | #include <signal.h> |
| 13 | #include <sched.h> |
| 14 | #include <sys/wait.h> |
| 15 | #include "clone_cruft.h" |
| 16 | |
| 17 | #define GOT1 (1 << 1) |
| 18 | #define GOT2 (1 << 2) |
| 19 | #define GOT3 (1 << 3) |
| 20 | #define ALLGOT (GOT1|GOT2|GOT3) |
| 21 | |
| 22 | static void child_handler(int sig) |
| 23 | { |
| 24 | printf("I got a SIGCHLD\n"); |
| 25 | } |
| 26 | |
| 27 | static int clone_main(void *arg) |
| 28 | { |
| 29 | unsigned long input = (unsigned long)arg; |
| 30 | int secs = (input / 10) * 4; |
| 31 | printf("Clone got %lu, sleeping for %i secs\n", input, secs); |
| 32 | sleep(secs); |
| 33 | return input + 20; |
| 34 | } |
| 35 | |
| 36 | int main(void) |
| 37 | { |
| 38 | int clone1, clone2, clone3; |
| 39 | char clone1_stack[8192], clone2_stack[8192], clone3_stack[8192]; |
| 40 | int status, nostatus, result, wpid; |
| 41 | |
| 42 | signal(SIGCHLD, child_handler); |
| 43 | |
| 44 | if ((clone1 = do_clone(clone_main, clone1_stack, 0, (void*)11)) == -1) { |
| 45 | perror("Clone 1 failed"); |
| 46 | exit(-1); |
| 47 | } |
| 48 | if ((clone2 = do_clone(clone_main, clone2_stack, 0, (void*)22)) == -1) { |
| 49 | perror("Clone 2 failed"); |
| 50 | exit(-2); |
| 51 | } |
| 52 | if ((clone3 = do_clone(clone_main, clone3_stack, 0, (void*)33)) == -1) { |
| 53 | perror("Clone 3 failed"); |
| 54 | exit(-3); |
| 55 | } |
| 56 | |
| 57 | sleep(1); |
| 58 | printf("Parent: waiting for the clones to die.\n"); |
| 59 | nostatus = status = 0; |
| 60 | while (1) { |
| 61 | if ((wpid = waitpid(clone1, &result, WNOHANG|__WCLONE)) == -1) |
| 62 | nostatus |= GOT1; |
| 63 | if (wpid == clone1) { |
| 64 | status |= GOT1; |
| 65 | printf("Clone1 gave back %i\n", WEXITSTATUS(result)); |
| 66 | } |
| 67 | |
| 68 | if ((wpid = waitpid(clone2, &result, WNOHANG|__WCLONE)) == -1) |
| 69 | nostatus |= GOT2; |
| 70 | if (wpid == clone2) { |
| 71 | status |= GOT2; |
| 72 | printf("Clone2 gave back %i\n", WEXITSTATUS(result)); |
| 73 | } |
| 74 | |
| 75 | if ((wpid = waitpid(clone3, &result, WNOHANG|__WCLONE)) == -1) |
| 76 | nostatus |= GOT3; |
| 77 | if (wpid == clone3) { |
| 78 | status |= GOT3; |
| 79 | printf("Clone3 gave back %i\n", WEXITSTATUS(result)); |
| 80 | } |
| 81 | |
| 82 | if (status == ALLGOT || nostatus == ALLGOT) |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | if (status == ALLGOT) { |
| 87 | printf("Clones exited.\nGoodbye.\n"); |
| 88 | return EXIT_SUCCESS; |
| 89 | } else { |
| 90 | perror("Waiting for clones failed"); |
| 91 | return EXIT_FAILURE; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | Local Variables: |
| 97 | c-file-style: "linux" |
| 98 | c-basic-offset: 4 |
| 99 | tab-width: 4 |
| 100 | End: |
| 101 | */ |