source: mainline/uspace/lib/c/generic/task.c@ 0a8f070

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

Create taskman server (extracts task-related operations from naming service)

  • Exploits initial phones connected to spawn parent instead of NS.
  • session_ns changed to session_primary (setup during taskman-loader handshake).
  • Task creation moved from NS to taskman (no clonable services anymore).
  • Other task-related operations implementation is to come (task_retval is temporarily dummy).
  • Async framework: implicit connections — create fibrils for calls that arrived through initial phone.

Conflicts:

abi/include/abi/ipc/methods.h
boot/Makefile.common
uspace/Makefile
uspace/app/trace/ipcp.c
uspace/lib/c/generic/async.c
uspace/lib/c/generic/libc.c
uspace/lib/c/generic/loader.c
uspace/lib/c/generic/ns.c
uspace/lib/c/generic/private/async.h
uspace/lib/c/generic/private/ns.h
uspace/lib/c/generic/task.c
uspace/lib/c/include/async.h
uspace/lib/c/include/ipc/services.h
uspace/lib/c/include/ipc/taskman.h
uspace/lib/c/include/loader/pcb.h
uspace/lib/c/include/ns.h
uspace/srv/loader/main.c
uspace/srv/ns/clonable.c
uspace/srv/ns/ns.c

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