source: mainline/uspace/lib/c/generic/task.c@ a737667e

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

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5/ revision 1941-1944

Original commit messages:

1944: Jiri Zarevucky 2013-08-06 Replace legacy file descriptor presetting with inbox.
1943: Jiri Zarevucky 2013-08-06 Do not preserve open state when passing file descriptor to another task. Allow receiver to specify, whether the descriptor is low or high.
1942: Jiri Zarevucky 2013-08-06 C style.
1941: Jiri Zarevucky 2013-08-06 Make loader accept file reference instead of a pathname.

Modifications:

  • Keep version of elf_load_file() that accepts file name
  • Changes required for loading dynamically linked executables
  • Update to newer list_foreach
  • Property mode set to 100644
File size: 10.6 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 if (fd_stdin >= 0) {
180 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
181 if (rc != EOK)
182 goto error;
183 }
184
185 if (fd_stdout >= 0) {
186 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
187 if (rc != EOK)
188 goto error;
189 }
190
191 if (fd_stderr >= 0) {
192 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
193 if (rc != EOK)
194 goto error;
195 }
196
197 /* Load the program. */
198 rc = loader_load_program(ldr);
199 if (rc != EOK)
200 goto error;
201
202 /* Setup waiting for return value if needed */
203 if (wait) {
204 rc = task_setup_wait(task_id, wait);
205 if (rc != EOK)
206 goto error;
207 wait_initialized = true;
208 }
209
210 /* Run it. */
211 rc = loader_run(ldr);
212 if (rc != EOK)
213 goto error;
214
215 /* Success */
216 if (id != NULL)
217 *id = task_id;
218
219 return EOK;
220
221error:
222 if (wait_initialized)
223 task_cancel_wait(wait);
224
225 /* Error exit */
226 loader_abort(ldr);
227 return rc;
228}
229
230/** Create a new task by running an executable from the filesystem.
231 *
232 * This is really just a convenience wrapper over the more complicated
233 * loader API. Arguments are passed in a va_list.
234 *
235 * @param id If not NULL, the ID of the task is stored here on success.
236 * @param wait If not NULL, setup waiting for task's return value and store
237 * the information necessary for waiting here on success.
238 * @param path Pathname of the binary to execute.
239 * @param cnt Number of arguments.
240 * @param ap Command-line arguments.
241 *
242 * @return Zero on success or negative error code.
243 *
244 */
245int task_spawn(task_id_t *task_id, task_wait_t *wait, const char *path,
246 int cnt, va_list ap)
247{
248 /* Allocate argument list. */
249 const char **arglist = malloc(cnt * sizeof(const char *));
250 if (arglist == NULL)
251 return ENOMEM;
252
253 /* Fill in arguments. */
254 const char *arg;
255 cnt = 0;
256 do {
257 arg = va_arg(ap, const char *);
258 arglist[cnt++] = arg;
259 } while (arg != NULL);
260
261 /* Spawn task. */
262 int rc = task_spawnv(task_id, wait, path, arglist);
263
264 /* Free argument list. */
265 free(arglist);
266 return rc;
267}
268
269/** Create a new task by running an executable from the filesystem.
270 *
271 * This is really just a convenience wrapper over the more complicated
272 * loader API. Arguments are passed as a null-terminated list of arguments.
273 *
274 * @param id If not NULL, the ID of the task is stored here on success.
275 * @param wait If not NULL, setup waiting for task's return value and store
276 * the information necessary for waiting here on success.
277 * @param path Pathname of the binary to execute.
278 * @param ... Command-line arguments.
279 *
280 * @return Zero on success or negative error code.
281 *
282 */
283int task_spawnl(task_id_t *task_id, task_wait_t *wait, const char *path, ...)
284{
285 /* Count the number of arguments. */
286
287 va_list ap;
288 const char *arg;
289 int cnt = 0;
290
291 va_start(ap, path);
292 do {
293 arg = va_arg(ap, const char *);
294 cnt++;
295 } while (arg != NULL);
296 va_end(ap);
297
298 va_start(ap, path);
299 int rc = task_spawn(task_id, wait, path, cnt, ap);
300 va_end(ap);
301
302 return rc;
303}
304
305/** Setup waiting for a task.
306 *
307 * If the task finishes after this call succeeds, it is guaranteed that
308 * task_wait(wait, &texit, &retval) will return correct return value for
309 * the task.
310 *
311 * @param id ID of the task to setup waiting for.
312 * @param wait Information necessary for the later task_wait call is stored here.
313 *
314 * @return EOK on success, else error code.
315 */
316int task_setup_wait(task_id_t id, task_wait_t *wait)
317{
318 async_exch_t *exch = async_exchange_begin(session_ns);
319 wait->aid = async_send_2(exch, NS_TASK_WAIT, LOWER32(id), UPPER32(id),
320 &wait->result);
321 async_exchange_end(exch);
322
323 return EOK;
324}
325
326/** Cancel waiting for a task.
327 *
328 * This can be called *instead of* task_wait if the caller is not interested
329 * in waiting for the task anymore.
330 *
331 * This function cannot be called if the task_wait was already called.
332 *
333 * @param wait task_wait_t previously initialized by task_setup_wait.
334 */
335void task_cancel_wait(task_wait_t *wait) {
336 async_forget(wait->aid);
337}
338
339/** Wait for a task to finish.
340 *
341 * This function returns correct values even if the task finished in
342 * between task_setup_wait and this task_wait call.
343 *
344 * This function cannot be called more than once with the same task_wait_t
345 * (it can be reused, but must be reinitialized with task_setup_wait first)
346 *
347 * @param wait task_wait_t previously initialized by task_setup_wait.
348 * @param texit Store type of task exit here.
349 * @param retval Store return value of the task here.
350 *
351 * @return EOK on success, else error code.
352 */
353int task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
354{
355 assert(texit);
356 assert(retval);
357
358 sysarg_t rc;
359 async_wait_for(wait->aid, &rc);
360
361 if (rc == EOK) {
362 *texit = IPC_GET_ARG1(wait->result);
363 *retval = IPC_GET_ARG2(wait->result);
364 }
365
366 return rc;
367}
368
369/** Wait for a task to finish by its id.
370 *
371 * Note that this will fail with ENOENT if the task id is not registered in ns
372 * (e.g. if the task finished). If you are spawning a task and need to wait
373 * for its completion, use wait parameter of the task_spawn* functions instead
374 * to prevent a race where the task exits before you may have a chance to wait
375 * wait for it.
376 *
377 * @param id ID of the task to wait for.
378 * @param texit Store type of task exit here.
379 * @param retval Store return value of the task here.
380 *
381 * @return EOK on success, else error code.
382 */
383int task_wait_task_id(task_id_t id, task_exit_t *texit, int *retval)
384{
385 task_wait_t wait;
386 int rc = task_setup_wait(id, &wait);
387 if (rc != EOK)
388 return rc;
389
390 return task_wait(&wait, texit, retval);
391}
392
393int task_retval(int val)
394{
395 async_exch_t *exch = async_exchange_begin(session_ns);
396 int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
397 async_exchange_end(exch);
398
399 return rc;
400}
401
402/** @}
403 */
Note: See TracBrowser for help on using the repository browser.