source: mainline/uspace/lib/c/generic/task.c@ 70d28e8

Last change on this file since 70d28e8 was 1be7bee, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Move task retval and waiting logic to taskman (partially)

  • two important sessions: NS and taskman
  • depending on boot task vs spawned task those sessions are initiated differently

Conflicts:

uspace/lib/c/generic/async.c
uspace/lib/c/generic/libc.c
uspace/lib/c/generic/task.c
uspace/lib/c/include/ipc/ns.h
uspace/lib/c/include/task.h
uspace/lib/posix/source/sys/wait.c
uspace/srv/loader/main.c
uspace/srv/ns/ns.c

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[f30e6a0b]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[c98e6ee]3 * Copyright (c) 2008 Jiri Svoboda
[1c635d6]4 * Copyright (c) 2014 Martin Sucha
[f30e6a0b]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.
[b2951e2]29 */
30
[fadd381]31/** @addtogroup libc
[b2951e2]32 * @{
33 */
34/** @file
[ee369f3]35 */
[f30e6a0b]36
[79ae36dd]37#include <assert.h>
[ee369f3]38#include <async.h>
[79ae36dd]39#include <errno.h>
[7b616e2]40#include <ns.h>
[38d150e]41#include <stdlib.h>
[1be7bee]42#include <ipc/taskman.h>
[79ae36dd]43#include <libc.h>
[1be7bee]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>
[df02460]50#include <vfs/vfs.h>
[1be7bee]51#include "private/ns.h"
52#include "private/task.h"
53
54static async_sess_t *session_taskman = NULL;
[f30e6a0b]55
[d3b8c1f]56task_id_t task_get_id(void)
[f30e6a0b]57{
[dd8d5a7]58#ifdef __32_BITS__
[f30e6a0b]59 task_id_t task_id;
[d3b8c1f]60 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
[a35b458]61
[f30e6a0b]62 return task_id;
[dd8d5a7]63#endif /* __32_BITS__ */
[a35b458]64
[dd8d5a7]65#ifdef __64_BITS__
66 return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
67#endif /* __64_BITS__ */
[f30e6a0b]68}
[b2951e2]69
[1be7bee]70static 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
89static void taskman_exchange_end(async_exch_t *exch)
90{
91 async_exchange_end(exch);
92}
93
[bc18d63]94/** Set the task name.
95 *
[ee369f3]96 * @param name The new name, typically the command used to execute the
97 * program.
98 *
[cde999a]99 * @return Zero on success or an error code.
[bc18d63]100 */
[b7fd2a0]101errno_t task_set_name(const char *name)
[bc18d63]102{
[79ae36dd]103 assert(name);
[a35b458]104
[b7fd2a0]105 return (errno_t) __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
[bc18d63]106}
107
[1e9f8ab]108/** Kill a task.
109 *
110 * @param task_id ID of task to kill.
111 *
[cde999a]112 * @return Zero on success or an error code.
[1e9f8ab]113 */
114
[b7fd2a0]115errno_t task_kill(task_id_t task_id)
[1e9f8ab]116{
[b7fd2a0]117 return (errno_t) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
[1e9f8ab]118}
119
[1be7bee]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 */
131static errno_t task_setup_wait(task_id_t id, task_wait_t *wait)
132{
133 async_exch_t *exch = taskman_exchange_begin();
134 if (exch == NULL)
135 return EIO;
136
137 wait->aid = async_send_3(exch, TASKMAN_WAIT, LOWER32(id), UPPER32(id),
138 wait->flags, &wait->result);
139 taskman_exchange_end(exch);
140
141 return EOK;
142}
143
[45454e9b]144/** Create a new task by running an executable from the filesystem.
145 *
146 * This is really just a convenience wrapper over the more complicated
[0485135]147 * loader API. Arguments are passed as a null-terminated array of strings.
[c98e6ee]148 *
[79ae36dd]149 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]150 * @param wait If not NULL, setup waiting for task's return value and store
151 * the information necessary for waiting here on success.
[79ae36dd]152 * @param path Pathname of the binary to execute.
153 * @param argv Command-line arguments.
154 *
[cde999a]155 * @return Zero on success or an error code.
[ee369f3]156 *
[c98e6ee]157 */
[b7fd2a0]158errno_t task_spawnv(task_id_t *id, task_wait_t *wait, const char *path,
[1c635d6]159 const char *const args[])
[ae45201]160{
161 /* Send default files */
[a35b458]162
[bb9ec2d]163 int fd_stdin = -1;
164 int fd_stdout = -1;
165 int fd_stderr = -1;
[a35b458]166
[bb9ec2d]167 if (stdin != NULL) {
168 (void) vfs_fhandle(stdin, &fd_stdin);
169 }
[a35b458]170
[bb9ec2d]171 if (stdout != NULL) {
172 (void) vfs_fhandle(stdout, &fd_stdout);
173 }
174
175 if (stderr != NULL) {
176 (void) vfs_fhandle(stderr, &fd_stderr);
177 }
[a35b458]178
[bb9ec2d]179 return task_spawnvf(id, wait, path, args, fd_stdin, fd_stdout,
180 fd_stderr);
[ae45201]181}
182
183/** Create a new task by running an executable from the filesystem.
184 *
185 * This is really just a convenience wrapper over the more complicated
186 * loader API. Arguments are passed as a null-terminated array of strings.
187 * Files are passed as null-terminated array of pointers to fdi_node_t.
188 *
[bb9ec2d]189 * @param id If not NULL, the ID of the task is stored here on success.
190 * @param wait If not NULL, setup waiting for task's return value and store
191 * @param path Pathname of the binary to execute.
192 * @param argv Command-line arguments.
193 * @param std_in File to use as stdin.
194 * @param std_out File to use as stdout.
195 * @param std_err File to use as stderr.
[ae45201]196 *
[cde999a]197 * @return Zero on success or an error code.
[ae45201]198 *
199 */
[b7fd2a0]200errno_t task_spawnvf(task_id_t *id, task_wait_t *wait, const char *path,
[bb9ec2d]201 const char *const args[], int fd_stdin, int fd_stdout, int fd_stderr)
[c98e6ee]202{
[bfd1546]203 /* Connect to a program loader. */
[79ae36dd]204 loader_t *ldr = loader_connect();
[0485135]205 if (ldr == NULL)
206 return EREFUSED;
[a35b458]207
[1c635d6]208 bool wait_initialized = false;
[a35b458]209
[47e0a05b]210 /* Get task ID. */
[79ae36dd]211 task_id_t task_id;
[b7fd2a0]212 errno_t rc = loader_get_task_id(ldr, &task_id);
[45454e9b]213 if (rc != EOK)
[47e0a05b]214 goto error;
[a35b458]215
[622cdbe]216 /* Send spawner's current working directory. */
217 rc = loader_set_cwd(ldr);
218 if (rc != EOK)
219 goto error;
[a35b458]220
[bb9ec2d]221 /* Send program binary. */
222 rc = loader_set_program_path(ldr, path);
[45454e9b]223 if (rc != EOK)
[17b2aac]224 goto error;
[a35b458]225
[4470e26]226 /* Send arguments. */
[ee369f3]227 rc = loader_set_args(ldr, args);
[17b2aac]228 if (rc != EOK)
229 goto error;
[a35b458]230
[ae45201]231 /* Send files */
[5126f80]232 int root = vfs_root();
233 if (root >= 0) {
234 rc = loader_add_inbox(ldr, "root", root);
[9c4cf0d]235 vfs_put(root);
[5126f80]236 if (rc != EOK)
237 goto error;
238 }
[a35b458]239
[bb9ec2d]240 if (fd_stdin >= 0) {
241 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
242 if (rc != EOK)
243 goto error;
244 }
[a35b458]245
[bb9ec2d]246 if (fd_stdout >= 0) {
247 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
248 if (rc != EOK)
249 goto error;
250 }
[a35b458]251
[bb9ec2d]252 if (fd_stderr >= 0) {
253 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
254 if (rc != EOK)
255 goto error;
[1b20da0]256 }
[a35b458]257
[4470e26]258 /* Load the program. */
259 rc = loader_load_program(ldr);
260 if (rc != EOK)
261 goto error;
[a35b458]262
[1c635d6]263 /* Setup waiting for return value if needed */
264 if (wait) {
265 rc = task_setup_wait(task_id, wait);
266 if (rc != EOK)
267 goto error;
268 wait_initialized = true;
269 }
[a35b458]270
[4470e26]271 /* Run it. */
272 rc = loader_run(ldr);
[17b2aac]273 if (rc != EOK)
274 goto error;
[a35b458]275
[c98e6ee]276 /* Success */
[0485135]277 if (id != NULL)
278 *id = task_id;
[a35b458]279
[0485135]280 return EOK;
[a35b458]281
[c98e6ee]282error:
[1c635d6]283 if (wait_initialized)
284 task_cancel_wait(wait);
[a35b458]285
[ee369f3]286 /* Error exit */
[45454e9b]287 loader_abort(ldr);
[0485135]288 return rc;
289}
290
[9f5cf68]291/** Create a new task by running an executable from the filesystem.
292 *
293 * This is really just a convenience wrapper over the more complicated
294 * loader API. Arguments are passed in a va_list.
295 *
296 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]297 * @param wait If not NULL, setup waiting for task's return value and store
298 * the information necessary for waiting here on success.
[9f5cf68]299 * @param path Pathname of the binary to execute.
300 * @param cnt Number of arguments.
301 * @param ap Command-line arguments.
302 *
[cde999a]303 * @return Zero on success or an error code.
[9f5cf68]304 *
305 */
[b7fd2a0]306errno_t task_spawn(task_id_t *task_id, task_wait_t *wait, const char *path,
[1c635d6]307 int cnt, va_list ap)
[9f5cf68]308{
309 /* Allocate argument list. */
310 const char **arglist = malloc(cnt * sizeof(const char *));
311 if (arglist == NULL)
312 return ENOMEM;
[a35b458]313
[9f5cf68]314 /* Fill in arguments. */
315 const char *arg;
316 cnt = 0;
317 do {
318 arg = va_arg(ap, const char *);
319 arglist[cnt++] = arg;
320 } while (arg != NULL);
[a35b458]321
[9f5cf68]322 /* Spawn task. */
[b7fd2a0]323 errno_t rc = task_spawnv(task_id, wait, path, arglist);
[a35b458]324
[9f5cf68]325 /* Free argument list. */
326 free(arglist);
327 return rc;
328}
329
[0485135]330/** Create a new task by running an executable from the filesystem.
331 *
332 * This is really just a convenience wrapper over the more complicated
333 * loader API. Arguments are passed as a null-terminated list of arguments.
334 *
[79ae36dd]335 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]336 * @param wait If not NULL, setup waiting for task's return value and store
337 * the information necessary for waiting here on success.
[79ae36dd]338 * @param path Pathname of the binary to execute.
339 * @param ... Command-line arguments.
340 *
[cde999a]341 * @return Zero on success or an error code.
[0485135]342 *
343 */
[b7fd2a0]344errno_t task_spawnl(task_id_t *task_id, task_wait_t *wait, const char *path, ...)
[0485135]345{
[79ae36dd]346 /* Count the number of arguments. */
[a35b458]347
[0485135]348 va_list ap;
349 const char *arg;
[79ae36dd]350 int cnt = 0;
[a35b458]351
[0485135]352 va_start(ap, path);
353 do {
354 arg = va_arg(ap, const char *);
355 cnt++;
356 } while (arg != NULL);
357 va_end(ap);
[a35b458]358
[0485135]359 va_start(ap, path);
[b7fd2a0]360 errno_t rc = task_spawn(task_id, wait, path, cnt, ap);
[0485135]361 va_end(ap);
[a35b458]362
[0485135]363 return rc;
[adb4d6c2]364}
365
[1c635d6]366/** Cancel waiting for a task.
367 *
368 * This can be called *instead of* task_wait if the caller is not interested
369 * in waiting for the task anymore.
370 *
371 * This function cannot be called if the task_wait was already called.
372 *
373 * @param wait task_wait_t previously initialized by task_setup_wait.
374 */
[1433ecda]375void task_cancel_wait(task_wait_t *wait)
376{
[1c635d6]377 async_forget(wait->aid);
378}
379
380/** Wait for a task to finish.
381 *
382 * This function returns correct values even if the task finished in
383 * between task_setup_wait and this task_wait call.
384 *
385 * This function cannot be called more than once with the same task_wait_t
386 * (it can be reused, but must be reinitialized with task_setup_wait first)
387 *
388 * @param wait task_wait_t previously initialized by task_setup_wait.
389 * @param texit Store type of task exit here.
390 * @param retval Store return value of the task here.
[1b20da0]391 *
[1c635d6]392 * @return EOK on success, else error code.
393 */
[b7fd2a0]394errno_t task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
[1c635d6]395{
396 assert(texit);
397 assert(retval);
398
[b7fd2a0]399 errno_t rc;
[1c635d6]400 async_wait_for(wait->aid, &rc);
401
402 if (rc == EOK) {
[fafb8e5]403 *texit = ipc_get_arg1(&wait->result);
404 *retval = ipc_get_arg2(&wait->result);
[1c635d6]405 }
406
[7114d83]407 return rc;
408}
409
[1c635d6]410/** Wait for a task to finish by its id.
411 *
412 * Note that this will fail with ENOENT if the task id is not registered in ns
413 * (e.g. if the task finished). If you are spawning a task and need to wait
414 * for its completion, use wait parameter of the task_spawn* functions instead
415 * to prevent a race where the task exits before you may have a chance to wait
416 * wait for it.
417 *
418 * @param id ID of the task to wait for.
[1be7bee]419 * @param flags Specify for which task output we wait
[1c635d6]420 * @param texit Store type of task exit here.
421 * @param retval Store return value of the task here.
[1b20da0]422 *
[1c635d6]423 * @return EOK on success, else error code.
424 */
[1be7bee]425errno_t task_wait_task_id(task_id_t id, int flags, task_exit_t *texit, int *retval)
[1c635d6]426{
427 task_wait_t wait;
[1be7bee]428 wait.flags = flags;
[b7fd2a0]429 errno_t rc = task_setup_wait(id, &wait);
[1be7bee]430
[1c635d6]431 if (rc != EOK)
432 return rc;
[a35b458]433
[1c635d6]434 return task_wait(&wait, texit, retval);
435}
436
[b7fd2a0]437errno_t task_retval(int val)
[7114d83]438{
[1be7bee]439 async_exch_t *exch = taskman_exchange_begin();
440 if (exch == NULL)
[7b616e2]441 return EIO;
442
[1be7bee]443 int rc = (int) async_req_1_0(exch, TASKMAN_RETVAL, val);
444 taskman_exchange_end(exch);
445
[79ae36dd]446 return rc;
[ee369f3]447}
448
[1be7bee]449
450void __task_init(async_sess_t *sess)
451{
452 assert(session_taskman == NULL);
453 session_taskman = sess;
454}
455
[fadd381]456/** @}
[b2951e2]457 */
Note: See TracBrowser for help on using the repository browser.