source: mainline/uspace/lib/c/generic/task.c@ 7e3826d9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7e3826d9 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

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