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