[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>
|
---|
[0485135] | 41 | #include <stdarg.h>
|
---|
[19f857a] | 42 | #include <str.h>
|
---|
[ee369f3] | 43 | #include <ipc/ns.h>
|
---|
| 44 | #include <macros.h>
|
---|
| 45 | #include <async.h>
|
---|
[f30e6a0b] | 46 |
|
---|
[d3b8c1f] | 47 | task_id_t task_get_id(void)
|
---|
[f30e6a0b] | 48 | {
|
---|
[dd8d5a7] | 49 | #ifdef __32_BITS__
|
---|
[f30e6a0b] | 50 | task_id_t task_id;
|
---|
[d3b8c1f] | 51 | (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
|
---|
[ee369f3] | 52 |
|
---|
[f30e6a0b] | 53 | return task_id;
|
---|
[dd8d5a7] | 54 | #endif /* __32_BITS__ */
|
---|
| 55 |
|
---|
| 56 | #ifdef __64_BITS__
|
---|
| 57 | return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
|
---|
| 58 | #endif /* __64_BITS__ */
|
---|
[f30e6a0b] | 59 | }
|
---|
[b2951e2] | 60 |
|
---|
[bc18d63] | 61 | /** Set the task name.
|
---|
| 62 | *
|
---|
[ee369f3] | 63 | * @param name The new name, typically the command used to execute the
|
---|
| 64 | * program.
|
---|
| 65 | *
|
---|
| 66 | * @return Zero on success or negative error code.
|
---|
| 67 | *
|
---|
[bc18d63] | 68 | */
|
---|
| 69 | int task_set_name(const char *name)
|
---|
| 70 | {
|
---|
[9eb3623] | 71 | return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
|
---|
[bc18d63] | 72 | }
|
---|
| 73 |
|
---|
[45454e9b] | 74 | /** Create a new task by running an executable from the filesystem.
|
---|
| 75 | *
|
---|
| 76 | * This is really just a convenience wrapper over the more complicated
|
---|
[0485135] | 77 | * loader API. Arguments are passed as a null-terminated array of strings.
|
---|
[c98e6ee] | 78 | *
|
---|
[0485135] | 79 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
| 80 | * @param path Pathname of the binary to execute.
|
---|
| 81 | * @param argv Command-line arguments.
|
---|
[ee369f3] | 82 | *
|
---|
[0485135] | 83 | * @return Zero on success or negative error code.
|
---|
[c98e6ee] | 84 | */
|
---|
[0485135] | 85 | int task_spawnv(task_id_t *id, const char *path, const char *const args[])
|
---|
[c98e6ee] | 86 | {
|
---|
[0485135] | 87 | loader_t *ldr;
|
---|
| 88 | task_id_t task_id;
|
---|
| 89 | int rc;
|
---|
| 90 |
|
---|
[bfd1546] | 91 | /* Connect to a program loader. */
|
---|
[0485135] | 92 | ldr = loader_connect();
|
---|
| 93 | if (ldr == NULL)
|
---|
| 94 | return EREFUSED;
|
---|
[ee369f3] | 95 |
|
---|
[47e0a05b] | 96 | /* Get task ID. */
|
---|
[0485135] | 97 | rc = loader_get_task_id(ldr, &task_id);
|
---|
[45454e9b] | 98 | if (rc != EOK)
|
---|
[47e0a05b] | 99 | goto error;
|
---|
[ee369f3] | 100 |
|
---|
[622cdbe] | 101 | /* Send spawner's current working directory. */
|
---|
| 102 | rc = loader_set_cwd(ldr);
|
---|
| 103 | if (rc != EOK)
|
---|
| 104 | goto error;
|
---|
| 105 |
|
---|
[4470e26] | 106 | /* Send program pathname. */
|
---|
[45454e9b] | 107 | rc = loader_set_pathname(ldr, path);
|
---|
| 108 | if (rc != EOK)
|
---|
[17b2aac] | 109 | goto error;
|
---|
[ee369f3] | 110 |
|
---|
[4470e26] | 111 | /* Send arguments. */
|
---|
[ee369f3] | 112 | rc = loader_set_args(ldr, args);
|
---|
[17b2aac] | 113 | if (rc != EOK)
|
---|
| 114 | goto error;
|
---|
[ee369f3] | 115 |
|
---|
| 116 | /* Send default files */
|
---|
[99272a3] | 117 | fdi_node_t *files[4];
|
---|
| 118 | fdi_node_t stdin_node;
|
---|
| 119 | fdi_node_t stdout_node;
|
---|
| 120 | fdi_node_t stderr_node;
|
---|
[ee369f3] | 121 |
|
---|
[a68f737] | 122 | if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
|
---|
[ee369f3] | 123 | files[0] = &stdin_node;
|
---|
[a68f737] | 124 | else
|
---|
[ee369f3] | 125 | files[0] = NULL;
|
---|
| 126 |
|
---|
[a68f737] | 127 | if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
|
---|
[ee369f3] | 128 | files[1] = &stdout_node;
|
---|
[a68f737] | 129 | else
|
---|
[ee369f3] | 130 | files[1] = NULL;
|
---|
| 131 |
|
---|
[a68f737] | 132 | if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
|
---|
[ee369f3] | 133 | files[2] = &stderr_node;
|
---|
[a68f737] | 134 | else
|
---|
[ee369f3] | 135 | files[2] = NULL;
|
---|
| 136 |
|
---|
| 137 | files[3] = NULL;
|
---|
| 138 |
|
---|
| 139 | rc = loader_set_files(ldr, files);
|
---|
| 140 | if (rc != EOK)
|
---|
| 141 | goto error;
|
---|
| 142 |
|
---|
[4470e26] | 143 | /* Load the program. */
|
---|
| 144 | rc = loader_load_program(ldr);
|
---|
| 145 | if (rc != EOK)
|
---|
| 146 | goto error;
|
---|
[ee369f3] | 147 |
|
---|
[4470e26] | 148 | /* Run it. */
|
---|
| 149 | rc = loader_run(ldr);
|
---|
[17b2aac] | 150 | if (rc != EOK)
|
---|
| 151 | goto error;
|
---|
[ee369f3] | 152 |
|
---|
[c98e6ee] | 153 | /* Success */
|
---|
[4470e26] | 154 | free(ldr);
|
---|
[d9fae235] | 155 |
|
---|
[0485135] | 156 | if (id != NULL)
|
---|
| 157 | *id = task_id;
|
---|
[d9fae235] | 158 |
|
---|
[0485135] | 159 | return EOK;
|
---|
[ee369f3] | 160 |
|
---|
[c98e6ee] | 161 | error:
|
---|
[ee369f3] | 162 | /* Error exit */
|
---|
[45454e9b] | 163 | loader_abort(ldr);
|
---|
[4470e26] | 164 | free(ldr);
|
---|
[0485135] | 165 | return rc;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /** Create a new task by running an executable from the filesystem.
|
---|
| 169 | *
|
---|
| 170 | * This is really just a convenience wrapper over the more complicated
|
---|
| 171 | * loader API. Arguments are passed as a null-terminated list of arguments.
|
---|
| 172 | *
|
---|
| 173 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
| 174 | * @param path Pathname of the binary to execute.
|
---|
| 175 | * @param ... Command-line arguments.
|
---|
| 176 | *
|
---|
| 177 | * @return Zero on success or negative error code.
|
---|
| 178 | */
|
---|
| 179 | int task_spawnl(task_id_t *task_id, const char *path, ...)
|
---|
| 180 | {
|
---|
| 181 | va_list ap;
|
---|
| 182 | int rc, cnt;
|
---|
| 183 | const char *arg;
|
---|
| 184 | const char **arglist;
|
---|
| 185 |
|
---|
| 186 | /* Count the number of arguments. */
|
---|
| 187 | cnt = 0;
|
---|
| 188 | va_start(ap, path);
|
---|
| 189 | do {
|
---|
| 190 | arg = va_arg(ap, const char *);
|
---|
| 191 | cnt++;
|
---|
| 192 | } while (arg != NULL);
|
---|
| 193 | va_end(ap);
|
---|
| 194 |
|
---|
| 195 | /* Allocate argument list. */
|
---|
| 196 | arglist = malloc(cnt * sizeof(const char *));
|
---|
| 197 | if (arglist == NULL)
|
---|
| 198 | return ENOMEM;
|
---|
| 199 |
|
---|
| 200 | /* Fill in arguments. */
|
---|
| 201 | cnt = 0;
|
---|
| 202 | va_start(ap, path);
|
---|
| 203 | do {
|
---|
| 204 | arg = va_arg(ap, const char *);
|
---|
| 205 | arglist[cnt++] = arg;
|
---|
| 206 | } while (arg != NULL);
|
---|
| 207 | va_end(ap);
|
---|
| 208 |
|
---|
| 209 | /* Spawn task. */
|
---|
| 210 | rc = task_spawnv(task_id, path, arglist);
|
---|
| 211 |
|
---|
| 212 | /* Free argument list. */
|
---|
| 213 | free(arglist);
|
---|
| 214 | return rc;
|
---|
[adb4d6c2] | 215 | }
|
---|
| 216 |
|
---|
[adb49f58] | 217 | int task_wait(task_id_t id, task_exit_t *texit, int *retval)
|
---|
[ee369f3] | 218 | {
|
---|
[96b02eb9] | 219 | sysarg_t te, rv;
|
---|
[7114d83] | 220 | int rc;
|
---|
| 221 |
|
---|
[adb49f58] | 222 | rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
|
---|
| 223 | UPPER32(id), &te, &rv);
|
---|
| 224 | *texit = te;
|
---|
[7114d83] | 225 | *retval = rv;
|
---|
| 226 |
|
---|
| 227 | return rc;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | int task_retval(int val)
|
---|
| 231 | {
|
---|
[5d96851b] | 232 | return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
|
---|
[ee369f3] | 233 | }
|
---|
| 234 |
|
---|
[fadd381] | 235 | /** @}
|
---|
[b2951e2] | 236 | */
|
---|