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
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * Copyright (c) 2008 Jiri Svoboda
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.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/** @file
34 */
35
36#include <task.h>
37#include <loader/loader.h>
38#include <stdarg.h>
39#include <str.h>
40#include <ipc/ns.h>
41#include <macros.h>
42#include <assert.h>
43#include <async.h>
44#include <errno.h>
45#include <malloc.h>
46#include <libc.h>
47#include "private/ns.h"
48#include <vfs/vfs.h>
49
50task_id_t task_get_id(void)
51{
52#ifdef __32_BITS__
53 task_id_t task_id;
54 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
55
56 return task_id;
57#endif /* __32_BITS__ */
58
59#ifdef __64_BITS__
60 return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
61#endif /* __64_BITS__ */
62}
63
64/** Set the task name.
65 *
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.
70 */
71int task_set_name(const char *name)
72{
73 assert(name);
74
75 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
76}
77
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
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
93 * loader API. Arguments are passed as a null-terminated array of strings.
94 *
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.
100 *
101 */
102int task_spawnv(task_id_t *id, const char *path, const char *const args[])
103{
104 /* Send default files */
105 int *files[4];
106 int fd_stdin;
107 int fd_stdout;
108 int fd_stderr;
109
110 if ((stdin != NULL) && (fhandle(stdin, &fd_stdin) == EOK))
111 files[0] = &fd_stdin;
112 else
113 files[0] = NULL;
114
115 if ((stdout != NULL) && (fhandle(stdout, &fd_stdout) == EOK))
116 files[1] = &fd_stdout;
117 else
118 files[1] = NULL;
119
120 if ((stderr != NULL) && (fhandle(stderr, &fd_stderr) == EOK))
121 files[2] = &fd_stderr;
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[],
145 int *const files[])
146{
147 /* Connect to a program loader. */
148 loader_t *ldr = loader_connect();
149 if (ldr == NULL)
150 return EREFUSED;
151
152 /* Get task ID. */
153 task_id_t task_id;
154 int rc = loader_get_task_id(ldr, &task_id);
155 if (rc != EOK)
156 goto error;
157
158 /* Send spawner's current working directory. */
159 rc = loader_set_cwd(ldr);
160 if (rc != EOK)
161 goto error;
162
163 /* Send program pathname. */
164 rc = loader_set_pathname(ldr, path);
165 if (rc != EOK)
166 goto error;
167
168 /* Send arguments. */
169 rc = loader_set_args(ldr, args);
170 if (rc != EOK)
171 goto error;
172
173 /* Send files */
174 rc = loader_set_files(ldr, files);
175 if (rc != EOK)
176 goto error;
177
178 /* Load the program. */
179 rc = loader_load_program(ldr);
180 if (rc != EOK)
181 goto error;
182
183 /* Run it. */
184 rc = loader_run(ldr);
185 if (rc != EOK)
186 goto error;
187
188 /* Success */
189 if (id != NULL)
190 *id = task_id;
191
192 return EOK;
193
194error:
195 /* Error exit */
196 loader_abort(ldr);
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 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
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 *
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.
246 *
247 */
248int task_spawnl(task_id_t *task_id, const char *path, ...)
249{
250 /* Count the number of arguments. */
251
252 va_list ap;
253 const char *arg;
254 int cnt = 0;
255
256 va_start(ap, path);
257 do {
258 arg = va_arg(ap, const char *);
259 cnt++;
260 } while (arg != NULL);
261 va_end(ap);
262
263 va_start(ap, path);
264 int rc = task_spawn(task_id, path, cnt, ap);
265 va_end(ap);
266
267 return rc;
268}
269
270int task_wait(task_id_t id, task_exit_t *texit, int *retval)
271{
272 assert(texit);
273 assert(retval);
274
275 async_exch_t *exch = async_exchange_begin(session_ns);
276 sysarg_t te, rv;
277 int rc = (int) async_req_2_2(exch, NS_TASK_WAIT, LOWER32(id),
278 UPPER32(id), &te, &rv);
279 async_exchange_end(exch);
280
281 *texit = te;
282 *retval = rv;
283
284 return rc;
285}
286
287int task_retval(int val)
288{
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;
294}
295
296/** @}
297 */
Note: See TracBrowser for help on using the repository browser.