source: mainline/uspace/lib/c/generic/task.c@ 035d7d8

Last change on this file since 035d7d8 was 456f7ae, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

libc: Separated task event functions

Conflicts:

uspace/lib/c/Makefile
uspace/lib/c/include/task.h

  • Property mode set to 100644
File size: 12.3 KB
Line 
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 <vfs/vfs.h>
52#include "private/ns.h"
53#include "private/task.h"
54
55static async_sess_t *session_taskman = NULL;
56
57task_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
71async_exch_t *taskman_exchange_begin(void)
72{
73 /* Lazy connection */
74 if (session_taskman == NULL) {
75 // TODO unify exchange mgmt with taskman_handshake/__init
76 session_taskman = service_connect_blocking(EXCHANGE_SERIALIZE,
77 SERVICE_TASKMAN,
78 TASKMAN_CONTROL,
79 0);
80 }
81
82 if (session_taskman == NULL) {
83 return NULL;
84 }
85
86 async_exch_t *exch = async_exchange_begin(session_taskman);
87 return exch;
88}
89
90void taskman_exchange_end(async_exch_t *exch)
91{
92 async_exchange_end(exch);
93}
94
95/** Set the task name.
96 *
97 * @param name The new name, typically the command used to execute the
98 * program.
99 *
100 * @return Zero on success or an error code.
101 */
102errno_t task_set_name(const char *name)
103{
104 assert(name);
105
106 return (errno_t) __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
107}
108
109/** Kill a task.
110 *
111 * @param task_id ID of task to kill.
112 *
113 * @return Zero on success or an error code.
114 */
115
116errno_t task_kill(task_id_t task_id)
117{
118 return (errno_t) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
119}
120
121/** Setup waiting for a task.
122 *
123 * If the task finishes after this call succeeds, it is guaranteed that
124 * task_wait(wait, &texit, &retval) will return correct return value for
125 * the task.
126 *
127 * @param id ID of the task to setup waiting for.
128 * @param wait Information necessary for the later task_wait call is stored here.
129 *
130 * @return EOK on success, else error code.
131 * @return TODO check this doesn't return EINVAL -- clash with task_wait
132 */
133static errno_t task_setup_wait(task_id_t id, task_wait_t *wait)
134{
135 assert(wait->flags);
136 if (wait->flags & TASK_WAIT_BOTH) {
137 wait->flags |= (TASK_WAIT_RETVAL | TASK_WAIT_EXIT);
138 }
139 wait->tid = id;
140 async_exch_t *exch = taskman_exchange_begin();
141 if (exch == NULL)
142 return EIO;
143
144 wait->aid = async_send_3(exch, TASKMAN_WAIT, LOWER32(id), UPPER32(id),
145 wait->flags, &wait->result);
146 taskman_exchange_end(exch);
147
148 return EOK;
149}
150
151/** Create a new task by running an executable from the filesystem.
152 *
153 * This is really just a convenience wrapper over the more complicated
154 * loader API. Arguments are passed as a null-terminated array of strings.
155 *
156 * @param id If not NULL, the ID of the task is stored here on success.
157 * @param wait If not NULL, setup waiting for task's return value and store
158 * the information necessary for waiting here on success.
159 * @param path Pathname of the binary to execute.
160 * @param argv Command-line arguments.
161 *
162 * @return Zero on success or an error code.
163 *
164 */
165errno_t task_spawnv(task_id_t *id, task_wait_t *wait, const char *path,
166 const char *const args[])
167{
168 /* Send default files */
169
170 int fd_stdin = -1;
171 int fd_stdout = -1;
172 int fd_stderr = -1;
173
174 if (stdin != NULL) {
175 (void) vfs_fhandle(stdin, &fd_stdin);
176 }
177
178 if (stdout != NULL) {
179 (void) vfs_fhandle(stdout, &fd_stdout);
180 }
181
182 if (stderr != NULL) {
183 (void) vfs_fhandle(stderr, &fd_stderr);
184 }
185
186 return task_spawnvf(id, wait, path, args, fd_stdin, fd_stdout,
187 fd_stderr);
188}
189
190/** Create a new task by running an executable from the filesystem.
191 *
192 * This is really just a convenience wrapper over the more complicated
193 * loader API. Arguments are passed as a null-terminated array of strings.
194 * Files are passed as null-terminated array of pointers to fdi_node_t.
195 *
196 * @param id If not NULL, the ID of the task is stored here on success.
197 * @param wait If not NULL, setup waiting for task's return value and store
198 * @param path Pathname of the binary to execute.
199 * @param argv Command-line arguments.
200 * @param std_in File to use as stdin.
201 * @param std_out File to use as stdout.
202 * @param std_err File to use as stderr.
203 *
204 * @return Zero on success or an error code.
205 *
206 */
207errno_t task_spawnvf(task_id_t *id, task_wait_t *wait, const char *path,
208 const char *const args[], int fd_stdin, int fd_stdout, int fd_stderr)
209{
210 /* Connect to a program loader. */
211 loader_t *ldr = loader_connect();
212 if (ldr == NULL)
213 return EREFUSED;
214
215 bool wait_initialized = false;
216
217 /* Get task ID. */
218 task_id_t task_id;
219 errno_t rc = loader_get_task_id(ldr, &task_id);
220 if (rc != EOK)
221 goto error;
222
223 /* Send spawner's current working directory. */
224 rc = loader_set_cwd(ldr);
225 if (rc != EOK)
226 goto error;
227
228 /* Send program binary. */
229 rc = loader_set_program_path(ldr, path);
230 if (rc != EOK)
231 goto error;
232
233 /* Send arguments. */
234 rc = loader_set_args(ldr, args);
235 if (rc != EOK)
236 goto error;
237
238 /* Send files */
239 int root = vfs_root();
240 if (root >= 0) {
241 rc = loader_add_inbox(ldr, "root", root);
242 vfs_put(root);
243 if (rc != EOK)
244 goto error;
245 }
246
247 if (fd_stdin >= 0) {
248 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
249 if (rc != EOK)
250 goto error;
251 }
252
253 if (fd_stdout >= 0) {
254 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
255 if (rc != EOK)
256 goto error;
257 }
258
259 if (fd_stderr >= 0) {
260 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
261 if (rc != EOK)
262 goto error;
263 }
264
265 /* Load the program. */
266 rc = loader_load_program(ldr);
267 if (rc != EOK)
268 goto error;
269
270 /* Setup waiting for return value if needed */
271 if (wait) {
272 rc = task_setup_wait(task_id, wait);
273 if (rc != EOK)
274 goto error;
275 wait_initialized = true;
276 }
277
278 /* Run it. */
279 rc = loader_run(ldr);
280 if (rc != EOK)
281 goto error;
282
283 /* Success */
284 if (id != NULL)
285 *id = task_id;
286
287 return EOK;
288
289error:
290 if (wait_initialized)
291 task_cancel_wait(wait);
292
293 /* Error exit */
294 loader_abort(ldr);
295 return rc;
296}
297
298/** Create a new task by running an executable from the filesystem.
299 *
300 * This is really just a convenience wrapper over the more complicated
301 * loader API. Arguments are passed in a va_list.
302 *
303 * @param id If not NULL, the ID of the task is stored here on success.
304 * @param wait If not NULL, setup waiting for task's return value and store
305 * the information necessary for waiting here on success.
306 * @param path Pathname of the binary to execute.
307 * @param cnt Number of arguments.
308 * @param ap Command-line arguments.
309 *
310 * @return Zero on success or an error code.
311 *
312 */
313errno_t task_spawn(task_id_t *task_id, task_wait_t *wait, const char *path,
314 int cnt, va_list ap)
315{
316 /* Allocate argument list. */
317 const char **arglist = malloc(cnt * sizeof(const char *));
318 if (arglist == NULL)
319 return ENOMEM;
320
321 /* Fill in arguments. */
322 const char *arg;
323 cnt = 0;
324 do {
325 arg = va_arg(ap, const char *);
326 arglist[cnt++] = arg;
327 } while (arg != NULL);
328
329 /* Spawn task. */
330 errno_t rc = task_spawnv(task_id, wait, path, arglist);
331
332 /* Free argument list. */
333 free(arglist);
334 return rc;
335}
336
337/** Create a new task by running an executable from the filesystem.
338 *
339 * This is really just a convenience wrapper over the more complicated
340 * loader API. Arguments are passed as a null-terminated list of arguments.
341 *
342 * @param id If not NULL, the ID of the task is stored here on success.
343 * @param wait If not NULL, setup waiting for task's return value and store
344 * the information necessary for waiting here on success.
345 * @param path Pathname of the binary to execute.
346 * @param ... Command-line arguments.
347 *
348 * @return Zero on success or an error code.
349 *
350 */
351errno_t task_spawnl(task_id_t *task_id, task_wait_t *wait, const char *path, ...)
352{
353 /* Count the number of arguments. */
354
355 va_list ap;
356 const char *arg;
357 int cnt = 0;
358
359 va_start(ap, path);
360 do {
361 arg = va_arg(ap, const char *);
362 cnt++;
363 } while (arg != NULL);
364 va_end(ap);
365
366 va_start(ap, path);
367 errno_t rc = task_spawn(task_id, wait, path, cnt, ap);
368 va_end(ap);
369
370 return rc;
371}
372
373/** Cancel waiting for a task.
374 *
375 * This can be called *instead of* task_wait if the caller is not interested
376 * in waiting for the task anymore.
377 *
378 * This function cannot be called if the task_wait was already called.
379 *
380 * @param wait task_wait_t previously initialized by task_setup_wait.
381 */
382void task_cancel_wait(task_wait_t *wait)
383{
384 async_forget(wait->aid);
385}
386
387/** Wait for a task to finish.
388 *
389 * This function returns correct values even if the task finished in
390 * between task_setup_wait and this task_wait call.
391 *
392 * This function cannot be called more than once with the same task_wait_t
393 * (it can be reused, but must be reinitialized with task_setup_wait first)
394 *
395 * @param wait task_wait_t previously initialized by task_setup_wait.
396 * @param texit Store type of task exit here.
397 * @param retval Store return value of the task here.
398 *
399 * @return EOK on success
400 * @return EINVAL on lost wait TODO other error codes
401 */
402errno_t task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
403{
404 errno_t rc;
405 async_wait_for(wait->aid, &rc);
406
407 if (rc == EOK || rc == EINVAL) {
408 if (wait->flags & TASK_WAIT_EXIT && texit)
409 *texit = ipc_get_arg1(wait->result);
410 if (wait->flags & TASK_WAIT_RETVAL && retval)
411 *retval = ipc_get_arg2(wait->result);
412
413 }
414
415 if (rc == EOK) {
416 /* Is there another wait to be done? Wait for it! */
417 int old_flags = wait->flags;
418 wait->flags = ipc_get_arg3(wait->result);
419 if (wait->flags != 0 && (old_flags & TASK_WAIT_BOTH)) {
420 rc = task_setup_wait(wait->tid, wait);
421 }
422 } else {
423 wait->flags = 0;
424 }
425
426 return rc;
427}
428
429/** Wait for a task to finish by its id.
430 *
431 * Note that this will fail with ENOENT if the task id is not registered in ns
432 * (e.g. if the task finished). If you are spawning a task and need to wait
433 * for its completion, use wait parameter of the task_spawn* functions instead
434 * to prevent a race where the task exits before you may have a chance to wait
435 * wait for it.
436 *
437 * @param id ID of the task to wait for.
438 * @param flags Specify for which task output we wait
439 * @param texit Store type of task exit here.
440 * @param retval Store return value of the task here.
441 *
442 * @return EOK on success, else error code.
443 */
444errno_t task_wait_task_id(task_id_t id, int flags, task_exit_t *texit, int *retval)
445{
446 task_wait_t wait;
447 wait.flags = flags;
448 errno_t rc = task_setup_wait(id, &wait);
449
450 if (rc != EOK)
451 return rc;
452
453 return task_wait(&wait, texit, retval);
454}
455
456errno_t task_retval_internal(int val, bool wait_for_exit)
457{
458 async_exch_t *exch = taskman_exchange_begin();
459 if (exch == NULL)
460 return EIO;
461
462 errno_t rc = (int) async_req_2_0(exch, TASKMAN_RETVAL, val, wait_for_exit);
463 taskman_exchange_end(exch);
464
465 return rc;
466}
467
468errno_t task_retval(int val)
469{
470 return task_retval_internal(val, false);
471}
472
473void __task_init(async_sess_t *sess)
474{
475 assert(session_taskman == NULL);
476 session_taskman = sess;
477}
478
479/** @}
480 */
Note: See TracBrowser for help on using the repository browser.