[f30e6a0b] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[c98e6ee] | 3 | * Copyright (c) 2008 Jiri Svoboda
|
---|
[f30e6a0b] | 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.
|
---|
[b2951e2] | 28 | */
|
---|
| 29 |
|
---|
[fadd381] | 30 | /** @addtogroup libc
|
---|
[b2951e2] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
[ee369f3] | 34 | */
|
---|
[f30e6a0b] | 35 |
|
---|
| 36 | #include <task.h>
|
---|
| 37 | #include <libc.h>
|
---|
[c98e6ee] | 38 | #include <stdlib.h>
|
---|
| 39 | #include <errno.h>
|
---|
[45454e9b] | 40 | #include <loader/loader.h>
|
---|
[19f857a] | 41 | #include <str.h>
|
---|
[ee369f3] | 42 | #include <ipc/ns.h>
|
---|
| 43 | #include <macros.h>
|
---|
| 44 | #include <async.h>
|
---|
[f30e6a0b] | 45 |
|
---|
[d3b8c1f] | 46 | task_id_t task_get_id(void)
|
---|
[f30e6a0b] | 47 | {
|
---|
| 48 | task_id_t task_id;
|
---|
[d3b8c1f] | 49 | (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
|
---|
[ee369f3] | 50 |
|
---|
[f30e6a0b] | 51 | return task_id;
|
---|
| 52 | }
|
---|
[b2951e2] | 53 |
|
---|
[bc18d63] | 54 | /** Set the task name.
|
---|
| 55 | *
|
---|
[ee369f3] | 56 | * @param name The new name, typically the command used to execute the
|
---|
| 57 | * program.
|
---|
| 58 | *
|
---|
| 59 | * @return Zero on success or negative error code.
|
---|
| 60 | *
|
---|
[bc18d63] | 61 | */
|
---|
| 62 | int task_set_name(const char *name)
|
---|
| 63 | {
|
---|
[9eb3623] | 64 | return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
|
---|
[bc18d63] | 65 | }
|
---|
| 66 |
|
---|
[45454e9b] | 67 | /** Create a new task by running an executable from the filesystem.
|
---|
| 68 | *
|
---|
| 69 | * This is really just a convenience wrapper over the more complicated
|
---|
| 70 | * loader API.
|
---|
[c98e6ee] | 71 | *
|
---|
[d9fae235] | 72 | * @param path Pathname of the binary to execute.
|
---|
| 73 | * @param argv Command-line arguments.
|
---|
| 74 | * @param err If not NULL, the error value is stored here.
|
---|
[ee369f3] | 75 | *
|
---|
| 76 | * @return ID of the newly created task or zero on error.
|
---|
| 77 | *
|
---|
[c98e6ee] | 78 | */
|
---|
[d9fae235] | 79 | task_id_t task_spawn(const char *path, const char *const args[], int *err)
|
---|
[c98e6ee] | 80 | {
|
---|
[bfd1546] | 81 | /* Connect to a program loader. */
|
---|
[ee369f3] | 82 | loader_t *ldr = loader_connect();
|
---|
[d9fae235] | 83 | if (ldr == NULL) {
|
---|
| 84 | if (err != NULL)
|
---|
| 85 | *err = EREFUSED;
|
---|
| 86 |
|
---|
[17b2aac] | 87 | return 0;
|
---|
[d9fae235] | 88 | }
|
---|
[ee369f3] | 89 |
|
---|
[47e0a05b] | 90 | /* Get task ID. */
|
---|
[ee369f3] | 91 | task_id_t task_id;
|
---|
| 92 | int rc = loader_get_task_id(ldr, &task_id);
|
---|
[45454e9b] | 93 | if (rc != EOK)
|
---|
[47e0a05b] | 94 | goto error;
|
---|
[ee369f3] | 95 |
|
---|
[622cdbe] | 96 | /* Send spawner's current working directory. */
|
---|
| 97 | rc = loader_set_cwd(ldr);
|
---|
| 98 | if (rc != EOK)
|
---|
| 99 | goto error;
|
---|
| 100 |
|
---|
[4470e26] | 101 | /* Send program pathname. */
|
---|
[45454e9b] | 102 | rc = loader_set_pathname(ldr, path);
|
---|
| 103 | if (rc != EOK)
|
---|
[17b2aac] | 104 | goto error;
|
---|
[ee369f3] | 105 |
|
---|
[4470e26] | 106 | /* Send arguments. */
|
---|
[ee369f3] | 107 | rc = loader_set_args(ldr, args);
|
---|
[17b2aac] | 108 | if (rc != EOK)
|
---|
| 109 | goto error;
|
---|
[ee369f3] | 110 |
|
---|
| 111 | /* Send default files */
|
---|
[99272a3] | 112 | fdi_node_t *files[4];
|
---|
| 113 | fdi_node_t stdin_node;
|
---|
| 114 | fdi_node_t stdout_node;
|
---|
| 115 | fdi_node_t stderr_node;
|
---|
[ee369f3] | 116 |
|
---|
[a68f737] | 117 | if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
|
---|
[ee369f3] | 118 | files[0] = &stdin_node;
|
---|
[a68f737] | 119 | else
|
---|
[ee369f3] | 120 | files[0] = NULL;
|
---|
| 121 |
|
---|
[a68f737] | 122 | if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
|
---|
[ee369f3] | 123 | files[1] = &stdout_node;
|
---|
[a68f737] | 124 | else
|
---|
[ee369f3] | 125 | files[1] = NULL;
|
---|
| 126 |
|
---|
[a68f737] | 127 | if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
|
---|
[ee369f3] | 128 | files[2] = &stderr_node;
|
---|
[a68f737] | 129 | else
|
---|
[ee369f3] | 130 | files[2] = NULL;
|
---|
| 131 |
|
---|
| 132 | files[3] = NULL;
|
---|
| 133 |
|
---|
| 134 | rc = loader_set_files(ldr, files);
|
---|
| 135 | if (rc != EOK)
|
---|
| 136 | goto error;
|
---|
| 137 |
|
---|
[4470e26] | 138 | /* Load the program. */
|
---|
| 139 | rc = loader_load_program(ldr);
|
---|
| 140 | if (rc != EOK)
|
---|
| 141 | goto error;
|
---|
[ee369f3] | 142 |
|
---|
[4470e26] | 143 | /* Run it. */
|
---|
| 144 | rc = loader_run(ldr);
|
---|
[17b2aac] | 145 | if (rc != EOK)
|
---|
| 146 | goto error;
|
---|
[ee369f3] | 147 |
|
---|
[c98e6ee] | 148 | /* Success */
|
---|
[4470e26] | 149 | free(ldr);
|
---|
[d9fae235] | 150 |
|
---|
| 151 | if (err != NULL)
|
---|
| 152 | *err = EOK;
|
---|
| 153 |
|
---|
[47e0a05b] | 154 | return task_id;
|
---|
[ee369f3] | 155 |
|
---|
[c98e6ee] | 156 | error:
|
---|
[ee369f3] | 157 | /* Error exit */
|
---|
[45454e9b] | 158 | loader_abort(ldr);
|
---|
[4470e26] | 159 | free(ldr);
|
---|
[ee369f3] | 160 |
|
---|
[d9fae235] | 161 | if (err != NULL)
|
---|
| 162 | *err = rc;
|
---|
| 163 |
|
---|
[c98e6ee] | 164 | return 0;
|
---|
[adb4d6c2] | 165 | }
|
---|
| 166 |
|
---|
[adb49f58] | 167 | int task_wait(task_id_t id, task_exit_t *texit, int *retval)
|
---|
[ee369f3] | 168 | {
|
---|
[adb49f58] | 169 | ipcarg_t te, rv;
|
---|
[7114d83] | 170 | int rc;
|
---|
| 171 |
|
---|
[adb49f58] | 172 | rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
|
---|
| 173 | UPPER32(id), &te, &rv);
|
---|
| 174 | *texit = te;
|
---|
[7114d83] | 175 | *retval = rv;
|
---|
| 176 |
|
---|
| 177 | return rc;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | int task_retval(int val)
|
---|
| 181 | {
|
---|
[5d96851b] | 182 | return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
|
---|
[ee369f3] | 183 | }
|
---|
| 184 |
|
---|
[fadd381] | 185 | /** @}
|
---|
[b2951e2] | 186 | */
|
---|