source: mainline/uspace/lib/c/generic/task.c@ 8dab988

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8dab988 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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