source: mainline/uspace/lib/c/generic/task.c@ 38db6288

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 38db6288 was 9f5cf68, checked in by Martin Decky <martin@…>, 13 years ago

implement va_list variant of task_spawn()

  • Property mode set to 100644
File size: 7.3 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>
[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]50task_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 */
71int 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
85int 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]102int 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 */
144int 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]194error:
[ee369f3]195 /* Error exit */
[45454e9b]196 loader_abort(ldr);
[0485135]197 return rc;
198}
199
[9f5cf68]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 in a va_list.
204 *
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 cnt Number of arguments.
208 * @param ap Command-line arguments.
209 *
210 * @return Zero on success or negative error code.
211 *
212 */
213int task_spawn(task_id_t *task_id, const char *path, int cnt, va_list ap)
214{
215 /* Allocate argument list. */
216 const char **arglist = malloc(cnt * sizeof(const char *));
217 if (arglist == NULL)
218 return ENOMEM;
219
220 /* Fill in arguments. */
221 const char *arg;
222 cnt = 0;
223 do {
224 arg = va_arg(ap, const char *);
225 arglist[cnt++] = arg;
226 } while (arg != NULL);
227
228 /* Spawn task. */
229 int rc = task_spawnv(task_id, path, arglist);
230
231 /* Free argument list. */
232 free(arglist);
233 return rc;
234}
235
[0485135]236/** Create a new task by running an executable from the filesystem.
237 *
238 * This is really just a convenience wrapper over the more complicated
239 * loader API. Arguments are passed as a null-terminated list of arguments.
240 *
[79ae36dd]241 * @param id If not NULL, the ID of the task is stored here on success.
242 * @param path Pathname of the binary to execute.
243 * @param ... Command-line arguments.
244 *
245 * @return Zero on success or negative error code.
[0485135]246 *
247 */
248int task_spawnl(task_id_t *task_id, const char *path, ...)
249{
[79ae36dd]250 /* Count the number of arguments. */
251
[0485135]252 va_list ap;
253 const char *arg;
[79ae36dd]254 int cnt = 0;
255
[0485135]256 va_start(ap, path);
257 do {
258 arg = va_arg(ap, const char *);
259 cnt++;
260 } while (arg != NULL);
261 va_end(ap);
[79ae36dd]262
[0485135]263 va_start(ap, path);
[9f5cf68]264 int rc = task_spawn(task_id, path, cnt, ap);
[0485135]265 va_end(ap);
[79ae36dd]266
[0485135]267 return rc;
[adb4d6c2]268}
269
[adb49f58]270int task_wait(task_id_t id, task_exit_t *texit, int *retval)
[ee369f3]271{
[79ae36dd]272 assert(texit);
273 assert(retval);
274
275 async_exch_t *exch = async_exchange_begin(session_ns);
[96b02eb9]276 sysarg_t te, rv;
[79ae36dd]277 int rc = (int) async_req_2_2(exch, NS_TASK_WAIT, LOWER32(id),
[adb49f58]278 UPPER32(id), &te, &rv);
[79ae36dd]279 async_exchange_end(exch);
280
[adb49f58]281 *texit = te;
[7114d83]282 *retval = rv;
[79ae36dd]283
[7114d83]284 return rc;
285}
286
287int task_retval(int val)
288{
[79ae36dd]289 async_exch_t *exch = async_exchange_begin(session_ns);
290 int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
291 async_exchange_end(exch);
292
293 return rc;
[ee369f3]294}
295
[fadd381]296/** @}
[b2951e2]297 */
Note: See TracBrowser for help on using the repository browser.