[823a929] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Petr Koupy
|
---|
| 3 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libposix
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
[4cf8ca6] | 33 | /** @file Support for waiting.
|
---|
[823a929] | 34 | */
|
---|
| 35 |
|
---|
| 36 | #include "../internal/common.h"
|
---|
[a3da2b2] | 37 | #include "posix/sys/wait.h"
|
---|
[823a929] | 38 |
|
---|
[a3da2b2] | 39 | #include "libc/task.h"
|
---|
[e8d3c6f5] | 40 | #include <assert.h>
|
---|
[0d0b319] | 41 |
|
---|
| 42 | #include <errno.h>
|
---|
| 43 |
|
---|
[a3da2b2] | 44 | #include "posix/limits.h"
|
---|
| 45 | #include "posix/signal.h"
|
---|
[eb1edb0] | 46 |
|
---|
[1433ecda] | 47 | int __posix_wifexited(int status)
|
---|
| 48 | {
|
---|
[eb1edb0] | 49 | return status != INT_MIN;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[1433ecda] | 52 | int __posix_wexitstatus(int status)
|
---|
| 53 | {
|
---|
[eb1edb0] | 54 | assert(__posix_wifexited(status));
|
---|
| 55 | return status;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1433ecda] | 58 | int __posix_wifsignaled(int status)
|
---|
| 59 | {
|
---|
[eb1edb0] | 60 | return status == INT_MIN;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[1433ecda] | 63 | int __posix_wtermsig(int status)
|
---|
| 64 | {
|
---|
[eb1edb0] | 65 | assert(__posix_wifsignaled(status));
|
---|
[7c3fb9b] | 66 | /*
|
---|
| 67 | * There is no way to distinguish reason
|
---|
[eb1edb0] | 68 | * for unexpected termination at the moment.
|
---|
| 69 | */
|
---|
| 70 | return SIGABRT;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[823a929] | 73 | /**
|
---|
[2a53f71] | 74 | * Wait for any child process to stop or terminate.
|
---|
[1b20da0] | 75 | *
|
---|
[2a53f71] | 76 | * @param stat_ptr Location of the final status code of the child process.
|
---|
| 77 | * @return ID of the child process for which status is reported,
|
---|
| 78 | * -1 on signal interrupt, (pid_t)-1 otherwise.
|
---|
[823a929] | 79 | */
|
---|
[7f9df7b9] | 80 | pid_t wait(int *stat_ptr)
|
---|
[823a929] | 81 | {
|
---|
[eb1edb0] | 82 | /* HelenOS does not support this. */
|
---|
| 83 | errno = ENOSYS;
|
---|
[7f9df7b9] | 84 | return (pid_t) -1;
|
---|
[823a929] | 85 | }
|
---|
| 86 |
|
---|
[e64b55a] | 87 | /**
|
---|
[2a53f71] | 88 | * Wait for a child process to stop or terminate.
|
---|
[1b20da0] | 89 | *
|
---|
[2a53f71] | 90 | * @param pid What child process shall the caller wait for. See POSIX manual
|
---|
| 91 | * for details.
|
---|
| 92 | * @param stat_ptr Location of the final status code of the child process.
|
---|
| 93 | * @param options Constraints of the waiting. See POSIX manual for details.
|
---|
| 94 | * @return ID of the child process for which status is reported,
|
---|
| 95 | * -1 on signal interrupt, 0 if non-blocking wait is requested but there is
|
---|
| 96 | * no child process whose status can be reported, (pid_t)-1 otherwise.
|
---|
[e64b55a] | 97 | */
|
---|
[7f9df7b9] | 98 | pid_t waitpid(pid_t pid, int *stat_ptr, int options)
|
---|
[e64b55a] | 99 | {
|
---|
[eb1edb0] | 100 | assert(stat_ptr != NULL);
|
---|
| 101 | assert(options == 0 /* None of the options are supported. */);
|
---|
[a35b458] | 102 |
|
---|
[eb1edb0] | 103 | task_exit_t texit;
|
---|
| 104 | int retval;
|
---|
[a35b458] | 105 |
|
---|
[0d0b319] | 106 | if (failed(task_wait_task_id((task_id_t) pid, &texit, &retval))) {
|
---|
[eb1edb0] | 107 | /* Unable to retrieve status. */
|
---|
[7f9df7b9] | 108 | return (pid_t) -1;
|
---|
[eb1edb0] | 109 | }
|
---|
[a35b458] | 110 |
|
---|
[eb1edb0] | 111 | if (texit == TASK_EXIT_NORMAL) {
|
---|
| 112 | // FIXME: relies on application not returning this value
|
---|
| 113 | assert(retval != INT_MIN);
|
---|
| 114 | *stat_ptr = retval;
|
---|
| 115 | } else {
|
---|
| 116 | /* Reserve the lowest value for unexpected termination. */
|
---|
| 117 | *stat_ptr = INT_MIN;
|
---|
| 118 | }
|
---|
[a35b458] | 119 |
|
---|
[eb1edb0] | 120 | return pid;
|
---|
[e64b55a] | 121 | }
|
---|
| 122 |
|
---|
[823a929] | 123 | /** @}
|
---|
| 124 | */
|
---|