source: mainline/uspace/lib/c/generic/task.c@ 79ae36dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ae36dd was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * Copyright (c) 2008 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/** @file
34 */
35
36#include <task.h>
37#include <loader/loader.h>
38#include <stdarg.h>
39#include <str.h>
40#include <ipc/ns.h>
41#include <macros.h>
42#include <assert.h>
43#include <async.h>
44#include <errno.h>
45#include <malloc.h>
46#include <libc.h>
47#include "private/ns.h"
48
49task_id_t task_get_id(void)
50{
51#ifdef __32_BITS__
52 task_id_t task_id;
53 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
54
55 return task_id;
56#endif /* __32_BITS__ */
57
58#ifdef __64_BITS__
59 return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
60#endif /* __64_BITS__ */
61}
62
63/** Set the task name.
64 *
65 * @param name The new name, typically the command used to execute the
66 * program.
67 *
68 * @return Zero on success or negative error code.
69 */
70int task_set_name(const char *name)
71{
72 assert(name);
73
74 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
75}
76
77/** Kill a task.
78 *
79 * @param task_id ID of task to kill.
80 *
81 * @return Zero on success or negative error code.
82 */
83
84int task_kill(task_id_t task_id)
85{
86 return (int) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
87}
88
89/** Create a new task by running an executable from the filesystem.
90 *
91 * This is really just a convenience wrapper over the more complicated
92 * loader API. Arguments are passed as a null-terminated array of strings.
93 *
94 * @param id If not NULL, the ID of the task is stored here on success.
95 * @param path Pathname of the binary to execute.
96 * @param argv Command-line arguments.
97 *
98 * @return Zero on success or negative error code.
99 *
100 */
101int task_spawnv(task_id_t *id, const char *path, const char *const args[])
102{
103 /* Connect to a program loader. */
104 loader_t *ldr = loader_connect();
105 if (ldr == NULL)
106 return EREFUSED;
107
108 /* Get task ID. */
109 task_id_t task_id;
110 int rc = loader_get_task_id(ldr, &task_id);
111 if (rc != EOK)
112 goto error;
113
114 /* Send spawner's current working directory. */
115 rc = loader_set_cwd(ldr);
116 if (rc != EOK)
117 goto error;
118
119 /* Send program pathname. */
120 rc = loader_set_pathname(ldr, path);
121 if (rc != EOK)
122 goto error;
123
124 /* Send arguments. */
125 rc = loader_set_args(ldr, args);
126 if (rc != EOK)
127 goto error;
128
129 /* Send default files */
130 fdi_node_t *files[4];
131 fdi_node_t stdin_node;
132 fdi_node_t stdout_node;
133 fdi_node_t stderr_node;
134
135 if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
136 files[0] = &stdin_node;
137 else
138 files[0] = NULL;
139
140 if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
141 files[1] = &stdout_node;
142 else
143 files[1] = NULL;
144
145 if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
146 files[2] = &stderr_node;
147 else
148 files[2] = NULL;
149
150 files[3] = NULL;
151
152 rc = loader_set_files(ldr, files);
153 if (rc != EOK)
154 goto error;
155
156 /* Load the program. */
157 rc = loader_load_program(ldr);
158 if (rc != EOK)
159 goto error;
160
161 /* Run it. */
162 rc = loader_run(ldr);
163 if (rc != EOK)
164 goto error;
165
166 /* Success */
167 if (id != NULL)
168 *id = task_id;
169
170 return EOK;
171
172error:
173 /* Error exit */
174 loader_abort(ldr);
175 free(ldr);
176 return rc;
177}
178
179/** Create a new task by running an executable from the filesystem.
180 *
181 * This is really just a convenience wrapper over the more complicated
182 * loader API. Arguments are passed as a null-terminated list of arguments.
183 *
184 * @param id If not NULL, the ID of the task is stored here on success.
185 * @param path Pathname of the binary to execute.
186 * @param ... Command-line arguments.
187 *
188 * @return Zero on success or negative error code.
189 *
190 */
191int task_spawnl(task_id_t *task_id, const char *path, ...)
192{
193 /* Count the number of arguments. */
194
195 va_list ap;
196 const char *arg;
197 const char **arglist;
198 int cnt = 0;
199
200 va_start(ap, path);
201 do {
202 arg = va_arg(ap, const char *);
203 cnt++;
204 } while (arg != NULL);
205 va_end(ap);
206
207 /* Allocate argument list. */
208 arglist = malloc(cnt * sizeof(const char *));
209 if (arglist == NULL)
210 return ENOMEM;
211
212 /* Fill in arguments. */
213 cnt = 0;
214 va_start(ap, path);
215 do {
216 arg = va_arg(ap, const char *);
217 arglist[cnt++] = arg;
218 } while (arg != NULL);
219 va_end(ap);
220
221 /* Spawn task. */
222 int rc = task_spawnv(task_id, path, arglist);
223
224 /* Free argument list. */
225 free(arglist);
226 return rc;
227}
228
229int task_wait(task_id_t id, task_exit_t *texit, int *retval)
230{
231 assert(texit);
232 assert(retval);
233
234 async_exch_t *exch = async_exchange_begin(session_ns);
235 sysarg_t te, rv;
236 int rc = (int) async_req_2_2(exch, NS_TASK_WAIT, LOWER32(id),
237 UPPER32(id), &te, &rv);
238 async_exchange_end(exch);
239
240 *texit = te;
241 *retval = rv;
242
243 return rc;
244}
245
246int task_retval(int val)
247{
248 async_exch_t *exch = async_exchange_begin(session_ns);
249 int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
250 async_exchange_end(exch);
251
252 return rc;
253}
254
255/** @}
256 */
Note: See TracBrowser for help on using the repository browser.