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