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