source: mainline/uspace/lib/c/generic/task.c@ 2f44fafd

Last change on this file since 2f44fafd was 2f44fafd, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

taskman: Implement task_wait API to pass all tests

  • different behavior for different wait flags
  • add locking to fibril code in taskman

Conflicts:

uspace/lib/c/generic/libc.c
uspace/lib/c/generic/task.c

  • Property mode set to 100644
File size: 11.7 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{
[2f44fafd]133 assert(wait->flags);
[1be7bee]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
[45454e9b]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
[0485135]148 * loader API. Arguments are passed as a null-terminated array of strings.
[c98e6ee]149 *
[79ae36dd]150 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]151 * @param wait If not NULL, setup waiting for task's return value and store
152 * the information necessary for waiting here on success.
[79ae36dd]153 * @param path Pathname of the binary to execute.
154 * @param argv Command-line arguments.
155 *
[cde999a]156 * @return Zero on success or an error code.
[ee369f3]157 *
[c98e6ee]158 */
[b7fd2a0]159errno_t task_spawnv(task_id_t *id, task_wait_t *wait, const char *path,
[1c635d6]160 const char *const args[])
[ae45201]161{
162 /* Send default files */
[a35b458]163
[bb9ec2d]164 int fd_stdin = -1;
165 int fd_stdout = -1;
166 int fd_stderr = -1;
[a35b458]167
[bb9ec2d]168 if (stdin != NULL) {
169 (void) vfs_fhandle(stdin, &fd_stdin);
170 }
[a35b458]171
[bb9ec2d]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 }
[a35b458]179
[bb9ec2d]180 return task_spawnvf(id, wait, path, args, fd_stdin, fd_stdout,
181 fd_stderr);
[ae45201]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 *
[bb9ec2d]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.
[ae45201]197 *
[cde999a]198 * @return Zero on success or an error code.
[ae45201]199 *
200 */
[b7fd2a0]201errno_t task_spawnvf(task_id_t *id, task_wait_t *wait, const char *path,
[bb9ec2d]202 const char *const args[], int fd_stdin, int fd_stdout, int fd_stderr)
[c98e6ee]203{
[bfd1546]204 /* Connect to a program loader. */
[79ae36dd]205 loader_t *ldr = loader_connect();
[0485135]206 if (ldr == NULL)
207 return EREFUSED;
[a35b458]208
[1c635d6]209 bool wait_initialized = false;
[a35b458]210
[47e0a05b]211 /* Get task ID. */
[79ae36dd]212 task_id_t task_id;
[b7fd2a0]213 errno_t rc = loader_get_task_id(ldr, &task_id);
[45454e9b]214 if (rc != EOK)
[47e0a05b]215 goto error;
[a35b458]216
[622cdbe]217 /* Send spawner's current working directory. */
218 rc = loader_set_cwd(ldr);
219 if (rc != EOK)
220 goto error;
[a35b458]221
[bb9ec2d]222 /* Send program binary. */
223 rc = loader_set_program_path(ldr, path);
[45454e9b]224 if (rc != EOK)
[17b2aac]225 goto error;
[a35b458]226
[4470e26]227 /* Send arguments. */
[ee369f3]228 rc = loader_set_args(ldr, args);
[17b2aac]229 if (rc != EOK)
230 goto error;
[a35b458]231
[ae45201]232 /* Send files */
[5126f80]233 int root = vfs_root();
234 if (root >= 0) {
235 rc = loader_add_inbox(ldr, "root", root);
[9c4cf0d]236 vfs_put(root);
[5126f80]237 if (rc != EOK)
238 goto error;
239 }
[a35b458]240
[bb9ec2d]241 if (fd_stdin >= 0) {
242 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
243 if (rc != EOK)
244 goto error;
245 }
[a35b458]246
[bb9ec2d]247 if (fd_stdout >= 0) {
248 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
249 if (rc != EOK)
250 goto error;
251 }
[a35b458]252
[bb9ec2d]253 if (fd_stderr >= 0) {
254 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
255 if (rc != EOK)
256 goto error;
[1b20da0]257 }
[a35b458]258
[4470e26]259 /* Load the program. */
260 rc = loader_load_program(ldr);
261 if (rc != EOK)
262 goto error;
[a35b458]263
[1c635d6]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 }
[a35b458]271
[4470e26]272 /* Run it. */
273 rc = loader_run(ldr);
[17b2aac]274 if (rc != EOK)
275 goto error;
[a35b458]276
[c98e6ee]277 /* Success */
[0485135]278 if (id != NULL)
279 *id = task_id;
[a35b458]280
[0485135]281 return EOK;
[a35b458]282
[c98e6ee]283error:
[1c635d6]284 if (wait_initialized)
285 task_cancel_wait(wait);
[a35b458]286
[ee369f3]287 /* Error exit */
[45454e9b]288 loader_abort(ldr);
[0485135]289 return rc;
290}
291
[9f5cf68]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.
[1c635d6]298 * @param wait If not NULL, setup waiting for task's return value and store
299 * the information necessary for waiting here on success.
[9f5cf68]300 * @param path Pathname of the binary to execute.
301 * @param cnt Number of arguments.
302 * @param ap Command-line arguments.
303 *
[cde999a]304 * @return Zero on success or an error code.
[9f5cf68]305 *
306 */
[b7fd2a0]307errno_t task_spawn(task_id_t *task_id, task_wait_t *wait, const char *path,
[1c635d6]308 int cnt, va_list ap)
[9f5cf68]309{
310 /* Allocate argument list. */
311 const char **arglist = malloc(cnt * sizeof(const char *));
312 if (arglist == NULL)
313 return ENOMEM;
[a35b458]314
[9f5cf68]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);
[a35b458]322
[9f5cf68]323 /* Spawn task. */
[b7fd2a0]324 errno_t rc = task_spawnv(task_id, wait, path, arglist);
[a35b458]325
[9f5cf68]326 /* Free argument list. */
327 free(arglist);
328 return rc;
329}
330
[0485135]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 *
[79ae36dd]336 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]337 * @param wait If not NULL, setup waiting for task's return value and store
338 * the information necessary for waiting here on success.
[79ae36dd]339 * @param path Pathname of the binary to execute.
340 * @param ... Command-line arguments.
341 *
[cde999a]342 * @return Zero on success or an error code.
[0485135]343 *
344 */
[b7fd2a0]345errno_t task_spawnl(task_id_t *task_id, task_wait_t *wait, const char *path, ...)
[0485135]346{
[79ae36dd]347 /* Count the number of arguments. */
[a35b458]348
[0485135]349 va_list ap;
350 const char *arg;
[79ae36dd]351 int cnt = 0;
[a35b458]352
[0485135]353 va_start(ap, path);
354 do {
355 arg = va_arg(ap, const char *);
356 cnt++;
357 } while (arg != NULL);
358 va_end(ap);
[a35b458]359
[0485135]360 va_start(ap, path);
[b7fd2a0]361 errno_t rc = task_spawn(task_id, wait, path, cnt, ap);
[0485135]362 va_end(ap);
[a35b458]363
[0485135]364 return rc;
[adb4d6c2]365}
366
[1c635d6]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 */
[1433ecda]376void task_cancel_wait(task_wait_t *wait)
377{
[1c635d6]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.
[1b20da0]392 *
[1c635d6]393 * @return EOK on success, else error code.
394 */
[b7fd2a0]395errno_t task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
[1c635d6]396{
397 assert(texit);
398 assert(retval);
399
[b7fd2a0]400 errno_t rc;
[1c635d6]401 async_wait_for(wait->aid, &rc);
402
403 if (rc == EOK) {
[fafb8e5]404 *texit = ipc_get_arg1(&wait->result);
405 *retval = ipc_get_arg2(&wait->result);
[1c635d6]406 }
407
[7114d83]408 return rc;
409}
410
[1c635d6]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.
[1be7bee]420 * @param flags Specify for which task output we wait
[1c635d6]421 * @param texit Store type of task exit here.
422 * @param retval Store return value of the task here.
[1b20da0]423 *
[1c635d6]424 * @return EOK on success, else error code.
425 */
[1be7bee]426errno_t task_wait_task_id(task_id_t id, int flags, task_exit_t *texit, int *retval)
[1c635d6]427{
428 task_wait_t wait;
[1be7bee]429 wait.flags = flags;
[b7fd2a0]430 errno_t rc = task_setup_wait(id, &wait);
[1be7bee]431
[1c635d6]432 if (rc != EOK)
433 return rc;
[a35b458]434
[1c635d6]435 return task_wait(&wait, texit, retval);
436}
437
[2f44fafd]438errno_t task_retval_internal(int val, bool wait_for_exit)
[7114d83]439{
[1be7bee]440 async_exch_t *exch = taskman_exchange_begin();
441 if (exch == NULL)
[7b616e2]442 return EIO;
443
[2f44fafd]444 errno_t rc = (int) async_req_2_0(exch, TASKMAN_RETVAL, val, wait_for_exit);
[1be7bee]445 taskman_exchange_end(exch);
446
[79ae36dd]447 return rc;
[ee369f3]448}
449
[2f44fafd]450errno_t task_retval(int val)
451{
452 return task_retval_internal(val, false);
453}
454
[1be7bee]455
456void __task_init(async_sess_t *sess)
457{
458 assert(session_taskman == NULL);
459 session_taskman = sess;
460}
461
[fadd381]462/** @}
[b2951e2]463 */
Note: See TracBrowser for help on using the repository browser.