[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
|
---|
[f30e6a0b] | 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <task.h>
|
---|
| 37 | #include <libc.h>
|
---|
[c98e6ee] | 38 | #include <stdlib.h>
|
---|
| 39 | #include <errno.h>
|
---|
[45454e9b] | 40 | #include <loader/loader.h>
|
---|
[bc18d63] | 41 | #include <string.h>
|
---|
[f30e6a0b] | 42 |
|
---|
[d3b8c1f] | 43 | task_id_t task_get_id(void)
|
---|
[f30e6a0b] | 44 | {
|
---|
| 45 | task_id_t task_id;
|
---|
| 46 |
|
---|
[d3b8c1f] | 47 | (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
|
---|
[f30e6a0b] | 48 |
|
---|
| 49 | return task_id;
|
---|
| 50 | }
|
---|
[b2951e2] | 51 |
|
---|
[bc18d63] | 52 | /** Set the task name.
|
---|
| 53 | *
|
---|
| 54 | * @param name The new name, typically the command used to execute the
|
---|
| 55 | * program.
|
---|
| 56 | * @return Zero on success or negative error code.
|
---|
| 57 | */
|
---|
| 58 | int task_set_name(const char *name)
|
---|
| 59 | {
|
---|
| 60 | return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, strlen(name));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[45454e9b] | 63 | /** Create a new task by running an executable from the filesystem.
|
---|
| 64 | *
|
---|
| 65 | * This is really just a convenience wrapper over the more complicated
|
---|
| 66 | * loader API.
|
---|
[c98e6ee] | 67 | *
|
---|
| 68 | * @param path pathname of the binary to execute
|
---|
| 69 | * @param argv command-line arguments
|
---|
| 70 | * @return ID of the newly created task or zero on error.
|
---|
| 71 | */
|
---|
[2e3355a] | 72 | task_id_t task_spawn(const char *path, char *const argv[])
|
---|
[c98e6ee] | 73 | {
|
---|
[45454e9b] | 74 | loader_t *ldr;
|
---|
[47e0a05b] | 75 | task_id_t task_id;
|
---|
[45454e9b] | 76 | int rc;
|
---|
[17b2aac] | 77 |
|
---|
[bfd1546] | 78 | /* Connect to a program loader. */
|
---|
| 79 | ldr = loader_connect();
|
---|
[45454e9b] | 80 | if (ldr == NULL)
|
---|
[17b2aac] | 81 | return 0;
|
---|
[c98e6ee] | 82 |
|
---|
[47e0a05b] | 83 | /* Get task ID. */
|
---|
[45454e9b] | 84 | rc = loader_get_task_id(ldr, &task_id);
|
---|
| 85 | if (rc != EOK)
|
---|
[47e0a05b] | 86 | goto error;
|
---|
| 87 |
|
---|
[4470e26] | 88 | /* Send program pathname. */
|
---|
[45454e9b] | 89 | rc = loader_set_pathname(ldr, path);
|
---|
| 90 | if (rc != EOK)
|
---|
[17b2aac] | 91 | goto error;
|
---|
[c98e6ee] | 92 |
|
---|
[4470e26] | 93 | /* Send arguments. */
|
---|
[45454e9b] | 94 | rc = loader_set_args(ldr, argv);
|
---|
[17b2aac] | 95 | if (rc != EOK)
|
---|
| 96 | goto error;
|
---|
[c98e6ee] | 97 |
|
---|
[4470e26] | 98 | /* Load the program. */
|
---|
| 99 | rc = loader_load_program(ldr);
|
---|
| 100 | if (rc != EOK)
|
---|
| 101 | goto error;
|
---|
| 102 |
|
---|
| 103 | /* Run it. */
|
---|
| 104 | rc = loader_run(ldr);
|
---|
[17b2aac] | 105 | if (rc != EOK)
|
---|
| 106 | goto error;
|
---|
[c98e6ee] | 107 |
|
---|
| 108 | /* Success */
|
---|
[4470e26] | 109 |
|
---|
| 110 | free(ldr);
|
---|
[47e0a05b] | 111 | return task_id;
|
---|
[c98e6ee] | 112 |
|
---|
| 113 | /* Error exit */
|
---|
| 114 | error:
|
---|
[45454e9b] | 115 | loader_abort(ldr);
|
---|
[4470e26] | 116 | free(ldr);
|
---|
| 117 |
|
---|
[c98e6ee] | 118 | return 0;
|
---|
[adb4d6c2] | 119 | }
|
---|
| 120 |
|
---|
[fadd381] | 121 | /** @}
|
---|
[b2951e2] | 122 | */
|
---|