source: mainline/uspace/lib/c/generic/task.c@ 3a4b3ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3a4b3ba was 0485135, checked in by Jiri Svoboda <jiri@…>, 15 years ago

For more convenience, replace task_spawn() with task_spawnv() and task_spawnl().

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