[f30e6a0b] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[c98e6ee] | 3 | * Copyright (c) 2008 Jiri Svoboda
|
---|
[1c635d6] | 4 | * Copyright (c) 2014 Martin Sucha
|
---|
[f30e6a0b] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[b2951e2] | 29 | */
|
---|
| 30 |
|
---|
[fadd381] | 31 | /** @addtogroup libc
|
---|
[b2951e2] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
[ee369f3] | 35 | */
|
---|
[f30e6a0b] | 36 |
|
---|
| 37 | #include <task.h>
|
---|
[45454e9b] | 38 | #include <loader/loader.h>
|
---|
[0485135] | 39 | #include <stdarg.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[ee369f3] | 41 | #include <ipc/ns.h>
|
---|
| 42 | #include <macros.h>
|
---|
[79ae36dd] | 43 | #include <assert.h>
|
---|
[ee369f3] | 44 | #include <async.h>
|
---|
[79ae36dd] | 45 | #include <errno.h>
|
---|
| 46 | #include <malloc.h>
|
---|
| 47 | #include <libc.h>
|
---|
| 48 | #include "private/ns.h"
|
---|
[df02460] | 49 | #include <vfs/vfs.h>
|
---|
[f30e6a0b] | 50 |
|
---|
[d3b8c1f] | 51 | task_id_t task_get_id(void)
|
---|
[f30e6a0b] | 52 | {
|
---|
[dd8d5a7] | 53 | #ifdef __32_BITS__
|
---|
[f30e6a0b] | 54 | task_id_t task_id;
|
---|
[d3b8c1f] | 55 | (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
|
---|
[ee369f3] | 56 |
|
---|
[f30e6a0b] | 57 | return task_id;
|
---|
[dd8d5a7] | 58 | #endif /* __32_BITS__ */
|
---|
| 59 |
|
---|
| 60 | #ifdef __64_BITS__
|
---|
| 61 | return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
|
---|
| 62 | #endif /* __64_BITS__ */
|
---|
[f30e6a0b] | 63 | }
|
---|
[b2951e2] | 64 |
|
---|
[bc18d63] | 65 | /** Set the task name.
|
---|
| 66 | *
|
---|
[ee369f3] | 67 | * @param name The new name, typically the command used to execute the
|
---|
| 68 | * program.
|
---|
| 69 | *
|
---|
| 70 | * @return Zero on success or negative error code.
|
---|
[bc18d63] | 71 | */
|
---|
| 72 | int task_set_name(const char *name)
|
---|
| 73 | {
|
---|
[79ae36dd] | 74 | assert(name);
|
---|
| 75 |
|
---|
[9eb3623] | 76 | return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
|
---|
[bc18d63] | 77 | }
|
---|
| 78 |
|
---|
[1e9f8ab] | 79 | /** Kill a task.
|
---|
| 80 | *
|
---|
| 81 | * @param task_id ID of task to kill.
|
---|
| 82 | *
|
---|
| 83 | * @return Zero on success or negative error code.
|
---|
| 84 | */
|
---|
| 85 |
|
---|
| 86 | int task_kill(task_id_t task_id)
|
---|
| 87 | {
|
---|
| 88 | return (int) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[45454e9b] | 91 | /** Create a new task by running an executable from the filesystem.
|
---|
| 92 | *
|
---|
| 93 | * This is really just a convenience wrapper over the more complicated
|
---|
[0485135] | 94 | * loader API. Arguments are passed as a null-terminated array of strings.
|
---|
[c98e6ee] | 95 | *
|
---|
[79ae36dd] | 96 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
[1c635d6] | 97 | * @param wait If not NULL, setup waiting for task's return value and store
|
---|
| 98 | * the information necessary for waiting here on success.
|
---|
[79ae36dd] | 99 | * @param path Pathname of the binary to execute.
|
---|
| 100 | * @param argv Command-line arguments.
|
---|
| 101 | *
|
---|
| 102 | * @return Zero on success or negative error code.
|
---|
[ee369f3] | 103 | *
|
---|
[c98e6ee] | 104 | */
|
---|
[1c635d6] | 105 | int task_spawnv(task_id_t *id, task_wait_t *wait, const char *path,
|
---|
| 106 | const char *const args[])
|
---|
[ae45201] | 107 | {
|
---|
| 108 | /* Send default files */
|
---|
[7171760] | 109 | int *files[4];
|
---|
| 110 | int fd_stdin;
|
---|
| 111 | int fd_stdout;
|
---|
| 112 | int fd_stderr;
|
---|
[ae45201] | 113 |
|
---|
[7171760] | 114 | if ((stdin != NULL) && (fhandle(stdin, &fd_stdin) == EOK))
|
---|
| 115 | files[0] = &fd_stdin;
|
---|
[ae45201] | 116 | else
|
---|
| 117 | files[0] = NULL;
|
---|
| 118 |
|
---|
[7171760] | 119 | if ((stdout != NULL) && (fhandle(stdout, &fd_stdout) == EOK))
|
---|
| 120 | files[1] = &fd_stdout;
|
---|
[ae45201] | 121 | else
|
---|
| 122 | files[1] = NULL;
|
---|
| 123 |
|
---|
[7171760] | 124 | if ((stderr != NULL) && (fhandle(stderr, &fd_stderr) == EOK))
|
---|
| 125 | files[2] = &fd_stderr;
|
---|
[ae45201] | 126 | else
|
---|
| 127 | files[2] = NULL;
|
---|
| 128 |
|
---|
| 129 | files[3] = NULL;
|
---|
| 130 |
|
---|
[1c635d6] | 131 | return task_spawnvf(id, wait, path, args, files);
|
---|
[ae45201] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /** Create a new task by running an executable from the filesystem.
|
---|
| 135 | *
|
---|
| 136 | * This is really just a convenience wrapper over the more complicated
|
---|
| 137 | * loader API. Arguments are passed as a null-terminated array of strings.
|
---|
| 138 | * Files are passed as null-terminated array of pointers to fdi_node_t.
|
---|
| 139 | *
|
---|
| 140 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
[1c635d6] | 141 | * @param wait If not NULL, setup waiting for task's return value and store
|
---|
| 142 | * the information necessary for waiting here on success.
|
---|
[ae45201] | 143 | * @param path Pathname of the binary to execute.
|
---|
| 144 | * @param argv Command-line arguments.
|
---|
| 145 | * @param files Standard files to use.
|
---|
| 146 | *
|
---|
| 147 | * @return Zero on success or negative error code.
|
---|
| 148 | *
|
---|
| 149 | */
|
---|
[1c635d6] | 150 | int task_spawnvf(task_id_t *id, task_wait_t *wait, const char *path,
|
---|
| 151 | const char *const args[], int *const files[])
|
---|
[c98e6ee] | 152 | {
|
---|
[bfd1546] | 153 | /* Connect to a program loader. */
|
---|
[79ae36dd] | 154 | loader_t *ldr = loader_connect();
|
---|
[0485135] | 155 | if (ldr == NULL)
|
---|
| 156 | return EREFUSED;
|
---|
[ee369f3] | 157 |
|
---|
[1c635d6] | 158 | bool wait_initialized = false;
|
---|
| 159 |
|
---|
[47e0a05b] | 160 | /* Get task ID. */
|
---|
[79ae36dd] | 161 | task_id_t task_id;
|
---|
| 162 | int rc = loader_get_task_id(ldr, &task_id);
|
---|
[45454e9b] | 163 | if (rc != EOK)
|
---|
[47e0a05b] | 164 | goto error;
|
---|
[ee369f3] | 165 |
|
---|
[622cdbe] | 166 | /* Send spawner's current working directory. */
|
---|
| 167 | rc = loader_set_cwd(ldr);
|
---|
| 168 | if (rc != EOK)
|
---|
| 169 | goto error;
|
---|
| 170 |
|
---|
[4470e26] | 171 | /* Send program pathname. */
|
---|
[45454e9b] | 172 | rc = loader_set_pathname(ldr, path);
|
---|
| 173 | if (rc != EOK)
|
---|
[17b2aac] | 174 | goto error;
|
---|
[ee369f3] | 175 |
|
---|
[4470e26] | 176 | /* Send arguments. */
|
---|
[ee369f3] | 177 | rc = loader_set_args(ldr, args);
|
---|
[17b2aac] | 178 | if (rc != EOK)
|
---|
| 179 | goto error;
|
---|
[ee369f3] | 180 |
|
---|
[ae45201] | 181 | /* Send files */
|
---|
[ee369f3] | 182 | rc = loader_set_files(ldr, files);
|
---|
| 183 | if (rc != EOK)
|
---|
| 184 | goto error;
|
---|
| 185 |
|
---|
[4470e26] | 186 | /* Load the program. */
|
---|
| 187 | rc = loader_load_program(ldr);
|
---|
| 188 | if (rc != EOK)
|
---|
| 189 | goto error;
|
---|
[ee369f3] | 190 |
|
---|
[1c635d6] | 191 | /* Setup waiting for return value if needed */
|
---|
| 192 | if (wait) {
|
---|
| 193 | rc = task_setup_wait(task_id, wait);
|
---|
| 194 | if (rc != EOK)
|
---|
| 195 | goto error;
|
---|
| 196 | wait_initialized = true;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[4470e26] | 199 | /* Run it. */
|
---|
| 200 | rc = loader_run(ldr);
|
---|
[17b2aac] | 201 | if (rc != EOK)
|
---|
| 202 | goto error;
|
---|
[ee369f3] | 203 |
|
---|
[c98e6ee] | 204 | /* Success */
|
---|
[0485135] | 205 | if (id != NULL)
|
---|
| 206 | *id = task_id;
|
---|
[d9fae235] | 207 |
|
---|
[0485135] | 208 | return EOK;
|
---|
[3815efb] | 209 |
|
---|
[c98e6ee] | 210 | error:
|
---|
[1c635d6] | 211 | if (wait_initialized)
|
---|
| 212 | task_cancel_wait(wait);
|
---|
| 213 |
|
---|
[ee369f3] | 214 | /* Error exit */
|
---|
[45454e9b] | 215 | loader_abort(ldr);
|
---|
[0485135] | 216 | return rc;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[9f5cf68] | 219 | /** Create a new task by running an executable from the filesystem.
|
---|
| 220 | *
|
---|
| 221 | * This is really just a convenience wrapper over the more complicated
|
---|
| 222 | * loader API. Arguments are passed in a va_list.
|
---|
| 223 | *
|
---|
| 224 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
[1c635d6] | 225 | * @param wait If not NULL, setup waiting for task's return value and store
|
---|
| 226 | * the information necessary for waiting here on success.
|
---|
[9f5cf68] | 227 | * @param path Pathname of the binary to execute.
|
---|
| 228 | * @param cnt Number of arguments.
|
---|
| 229 | * @param ap Command-line arguments.
|
---|
| 230 | *
|
---|
| 231 | * @return Zero on success or negative error code.
|
---|
| 232 | *
|
---|
| 233 | */
|
---|
[1c635d6] | 234 | int task_spawn(task_id_t *task_id, task_wait_t *wait, const char *path,
|
---|
| 235 | int cnt, va_list ap)
|
---|
[9f5cf68] | 236 | {
|
---|
| 237 | /* Allocate argument list. */
|
---|
| 238 | const char **arglist = malloc(cnt * sizeof(const char *));
|
---|
| 239 | if (arglist == NULL)
|
---|
| 240 | return ENOMEM;
|
---|
| 241 |
|
---|
| 242 | /* Fill in arguments. */
|
---|
| 243 | const char *arg;
|
---|
| 244 | cnt = 0;
|
---|
| 245 | do {
|
---|
| 246 | arg = va_arg(ap, const char *);
|
---|
| 247 | arglist[cnt++] = arg;
|
---|
| 248 | } while (arg != NULL);
|
---|
| 249 |
|
---|
| 250 | /* Spawn task. */
|
---|
[1c635d6] | 251 | int rc = task_spawnv(task_id, wait, path, arglist);
|
---|
[9f5cf68] | 252 |
|
---|
| 253 | /* Free argument list. */
|
---|
| 254 | free(arglist);
|
---|
| 255 | return rc;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[0485135] | 258 | /** Create a new task by running an executable from the filesystem.
|
---|
| 259 | *
|
---|
| 260 | * This is really just a convenience wrapper over the more complicated
|
---|
| 261 | * loader API. Arguments are passed as a null-terminated list of arguments.
|
---|
| 262 | *
|
---|
[79ae36dd] | 263 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
[1c635d6] | 264 | * @param wait If not NULL, setup waiting for task's return value and store
|
---|
| 265 | * the information necessary for waiting here on success.
|
---|
[79ae36dd] | 266 | * @param path Pathname of the binary to execute.
|
---|
| 267 | * @param ... Command-line arguments.
|
---|
| 268 | *
|
---|
| 269 | * @return Zero on success or negative error code.
|
---|
[0485135] | 270 | *
|
---|
| 271 | */
|
---|
[1c635d6] | 272 | int task_spawnl(task_id_t *task_id, task_wait_t *wait, const char *path, ...)
|
---|
[0485135] | 273 | {
|
---|
[79ae36dd] | 274 | /* Count the number of arguments. */
|
---|
| 275 |
|
---|
[0485135] | 276 | va_list ap;
|
---|
| 277 | const char *arg;
|
---|
[79ae36dd] | 278 | int cnt = 0;
|
---|
| 279 |
|
---|
[0485135] | 280 | va_start(ap, path);
|
---|
| 281 | do {
|
---|
| 282 | arg = va_arg(ap, const char *);
|
---|
| 283 | cnt++;
|
---|
| 284 | } while (arg != NULL);
|
---|
| 285 | va_end(ap);
|
---|
[79ae36dd] | 286 |
|
---|
[0485135] | 287 | va_start(ap, path);
|
---|
[1c635d6] | 288 | int rc = task_spawn(task_id, wait, path, cnt, ap);
|
---|
[0485135] | 289 | va_end(ap);
|
---|
[79ae36dd] | 290 |
|
---|
[0485135] | 291 | return rc;
|
---|
[adb4d6c2] | 292 | }
|
---|
| 293 |
|
---|
[1c635d6] | 294 | /** Setup waiting for a task.
|
---|
| 295 | *
|
---|
| 296 | * If the task finishes after this call succeeds, it is guaranteed that
|
---|
| 297 | * task_wait(wait, &texit, &retval) will return correct return value for
|
---|
| 298 | * the task.
|
---|
| 299 | *
|
---|
| 300 | * @param id ID of the task to setup waiting for.
|
---|
| 301 | * @param wait Information necessary for the later task_wait call is stored here.
|
---|
| 302 | *
|
---|
| 303 | * @return EOK on success, else error code.
|
---|
| 304 | */
|
---|
| 305 | int task_setup_wait(task_id_t id, task_wait_t *wait)
|
---|
[ee369f3] | 306 | {
|
---|
[79ae36dd] | 307 | async_exch_t *exch = async_exchange_begin(session_ns);
|
---|
[1c635d6] | 308 | wait->aid = async_send_2(exch, NS_TASK_WAIT, LOWER32(id), UPPER32(id),
|
---|
| 309 | &wait->result);
|
---|
[79ae36dd] | 310 | async_exchange_end(exch);
|
---|
[1c635d6] | 311 |
|
---|
| 312 | return EOK;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /** Cancel waiting for a task.
|
---|
| 316 | *
|
---|
| 317 | * This can be called *instead of* task_wait if the caller is not interested
|
---|
| 318 | * in waiting for the task anymore.
|
---|
| 319 | *
|
---|
| 320 | * This function cannot be called if the task_wait was already called.
|
---|
| 321 | *
|
---|
| 322 | * @param wait task_wait_t previously initialized by task_setup_wait.
|
---|
| 323 | */
|
---|
| 324 | void task_cancel_wait(task_wait_t *wait) {
|
---|
| 325 | async_forget(wait->aid);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /** Wait for a task to finish.
|
---|
| 329 | *
|
---|
| 330 | * This function returns correct values even if the task finished in
|
---|
| 331 | * between task_setup_wait and this task_wait call.
|
---|
| 332 | *
|
---|
| 333 | * This function cannot be called more than once with the same task_wait_t
|
---|
| 334 | * (it can be reused, but must be reinitialized with task_setup_wait first)
|
---|
| 335 | *
|
---|
| 336 | * @param wait task_wait_t previously initialized by task_setup_wait.
|
---|
| 337 | * @param texit Store type of task exit here.
|
---|
| 338 | * @param retval Store return value of the task here.
|
---|
| 339 | *
|
---|
| 340 | * @return EOK on success, else error code.
|
---|
| 341 | */
|
---|
| 342 | int task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
|
---|
| 343 | {
|
---|
| 344 | assert(texit);
|
---|
| 345 | assert(retval);
|
---|
| 346 |
|
---|
| 347 | sysarg_t rc;
|
---|
| 348 | async_wait_for(wait->aid, &rc);
|
---|
| 349 |
|
---|
| 350 | if (rc == EOK) {
|
---|
| 351 | *texit = IPC_GET_ARG1(wait->result);
|
---|
| 352 | *retval = IPC_GET_ARG2(wait->result);
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[7114d83] | 355 | return rc;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[1c635d6] | 358 | /** Wait for a task to finish by its id.
|
---|
| 359 | *
|
---|
| 360 | * Note that this will fail with ENOENT if the task id is not registered in ns
|
---|
| 361 | * (e.g. if the task finished). If you are spawning a task and need to wait
|
---|
| 362 | * for its completion, use wait parameter of the task_spawn* functions instead
|
---|
| 363 | * to prevent a race where the task exits before you may have a chance to wait
|
---|
| 364 | * wait for it.
|
---|
| 365 | *
|
---|
| 366 | * @param id ID of the task to wait for.
|
---|
| 367 | * @param texit Store type of task exit here.
|
---|
| 368 | * @param retval Store return value of the task here.
|
---|
| 369 | *
|
---|
| 370 | * @return EOK on success, else error code.
|
---|
| 371 | */
|
---|
| 372 | int task_wait_task_id(task_id_t id, task_exit_t *texit, int *retval)
|
---|
| 373 | {
|
---|
| 374 | task_wait_t wait;
|
---|
| 375 | int rc = task_setup_wait(id, &wait);
|
---|
| 376 | if (rc != EOK)
|
---|
| 377 | return rc;
|
---|
| 378 |
|
---|
| 379 | return task_wait(&wait, texit, retval);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[7114d83] | 382 | int task_retval(int val)
|
---|
| 383 | {
|
---|
[79ae36dd] | 384 | async_exch_t *exch = async_exchange_begin(session_ns);
|
---|
| 385 | int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
|
---|
| 386 | async_exchange_end(exch);
|
---|
| 387 |
|
---|
| 388 | return rc;
|
---|
[ee369f3] | 389 | }
|
---|
| 390 |
|
---|
[fadd381] | 391 | /** @}
|
---|
[b2951e2] | 392 | */
|
---|