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 <libc.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <loader/loader.h>
|
---|
41 | #include <stdarg.h>
|
---|
42 | #include <str.h>
|
---|
43 | #include <ipc/ns.h>
|
---|
44 | #include <macros.h>
|
---|
45 | #include <async.h>
|
---|
46 |
|
---|
47 | task_id_t task_get_id(void)
|
---|
48 | {
|
---|
49 | task_id_t task_id;
|
---|
50 | (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
|
---|
51 |
|
---|
52 | return task_id;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Set the task name.
|
---|
56 | *
|
---|
57 | * @param name The new name, typically the command used to execute the
|
---|
58 | * program.
|
---|
59 | *
|
---|
60 | * @return Zero on success or negative error code.
|
---|
61 | *
|
---|
62 | */
|
---|
63 | int task_set_name(const char *name)
|
---|
64 | {
|
---|
65 | return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Create a new task by running an executable from the filesystem.
|
---|
69 | *
|
---|
70 | * This is really just a convenience wrapper over the more complicated
|
---|
71 | * loader API. Arguments are passed as a null-terminated array of strings.
|
---|
72 | *
|
---|
73 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
74 | * @param path Pathname of the binary to execute.
|
---|
75 | * @param argv Command-line arguments.
|
---|
76 | *
|
---|
77 | * @return Zero on success or negative error code.
|
---|
78 | */
|
---|
79 | int task_spawnv(task_id_t *id, const char *path, const char *const args[])
|
---|
80 | {
|
---|
81 | loader_t *ldr;
|
---|
82 | task_id_t task_id;
|
---|
83 | int rc;
|
---|
84 |
|
---|
85 | /* Connect to a program loader. */
|
---|
86 | ldr = loader_connect();
|
---|
87 | if (ldr == NULL)
|
---|
88 | return EREFUSED;
|
---|
89 |
|
---|
90 | /* Get task ID. */
|
---|
91 | rc = loader_get_task_id(ldr, &task_id);
|
---|
92 | if (rc != EOK)
|
---|
93 | goto error;
|
---|
94 |
|
---|
95 | /* Send spawner's current working directory. */
|
---|
96 | rc = loader_set_cwd(ldr);
|
---|
97 | if (rc != EOK)
|
---|
98 | goto error;
|
---|
99 |
|
---|
100 | /* Send program pathname. */
|
---|
101 | rc = loader_set_pathname(ldr, path);
|
---|
102 | if (rc != EOK)
|
---|
103 | goto error;
|
---|
104 |
|
---|
105 | /* Send arguments. */
|
---|
106 | rc = loader_set_args(ldr, args);
|
---|
107 | if (rc != EOK)
|
---|
108 | goto error;
|
---|
109 |
|
---|
110 | /* Send default files */
|
---|
111 | fdi_node_t *files[4];
|
---|
112 | fdi_node_t stdin_node;
|
---|
113 | fdi_node_t stdout_node;
|
---|
114 | fdi_node_t stderr_node;
|
---|
115 |
|
---|
116 | if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
|
---|
117 | files[0] = &stdin_node;
|
---|
118 | else
|
---|
119 | files[0] = NULL;
|
---|
120 |
|
---|
121 | if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
|
---|
122 | files[1] = &stdout_node;
|
---|
123 | else
|
---|
124 | files[1] = NULL;
|
---|
125 |
|
---|
126 | if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
|
---|
127 | files[2] = &stderr_node;
|
---|
128 | else
|
---|
129 | files[2] = NULL;
|
---|
130 |
|
---|
131 | files[3] = NULL;
|
---|
132 |
|
---|
133 | rc = loader_set_files(ldr, files);
|
---|
134 | if (rc != EOK)
|
---|
135 | goto error;
|
---|
136 |
|
---|
137 | /* Load the program. */
|
---|
138 | rc = loader_load_program(ldr);
|
---|
139 | if (rc != EOK)
|
---|
140 | goto error;
|
---|
141 |
|
---|
142 | /* Run it. */
|
---|
143 | rc = loader_run(ldr);
|
---|
144 | if (rc != EOK)
|
---|
145 | goto error;
|
---|
146 |
|
---|
147 | /* Success */
|
---|
148 | free(ldr);
|
---|
149 |
|
---|
150 | if (id != NULL)
|
---|
151 | *id = task_id;
|
---|
152 |
|
---|
153 | return EOK;
|
---|
154 |
|
---|
155 | error:
|
---|
156 | /* Error exit */
|
---|
157 | loader_abort(ldr);
|
---|
158 | free(ldr);
|
---|
159 | return rc;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Create a new task by running an executable from the filesystem.
|
---|
163 | *
|
---|
164 | * This is really just a convenience wrapper over the more complicated
|
---|
165 | * loader API. Arguments are passed as a null-terminated list of arguments.
|
---|
166 | *
|
---|
167 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
168 | * @param path Pathname of the binary to execute.
|
---|
169 | * @param ... Command-line arguments.
|
---|
170 | *
|
---|
171 | * @return Zero on success or negative error code.
|
---|
172 | */
|
---|
173 | int task_spawnl(task_id_t *task_id, const char *path, ...)
|
---|
174 | {
|
---|
175 | va_list ap;
|
---|
176 | int rc, cnt;
|
---|
177 | const char *arg;
|
---|
178 | const char **arglist;
|
---|
179 |
|
---|
180 | /* Count the number of arguments. */
|
---|
181 | cnt = 0;
|
---|
182 | va_start(ap, path);
|
---|
183 | do {
|
---|
184 | arg = va_arg(ap, const char *);
|
---|
185 | cnt++;
|
---|
186 | } while (arg != NULL);
|
---|
187 | va_end(ap);
|
---|
188 |
|
---|
189 | /* Allocate argument list. */
|
---|
190 | arglist = malloc(cnt * sizeof(const char *));
|
---|
191 | if (arglist == NULL)
|
---|
192 | return ENOMEM;
|
---|
193 |
|
---|
194 | /* Fill in arguments. */
|
---|
195 | cnt = 0;
|
---|
196 | va_start(ap, path);
|
---|
197 | do {
|
---|
198 | arg = va_arg(ap, const char *);
|
---|
199 | arglist[cnt++] = arg;
|
---|
200 | } while (arg != NULL);
|
---|
201 | va_end(ap);
|
---|
202 |
|
---|
203 | /* Spawn task. */
|
---|
204 | rc = task_spawnv(task_id, path, arglist);
|
---|
205 |
|
---|
206 | /* Free argument list. */
|
---|
207 | free(arglist);
|
---|
208 | return rc;
|
---|
209 | }
|
---|
210 |
|
---|
211 | int task_wait(task_id_t id, task_exit_t *texit, int *retval)
|
---|
212 | {
|
---|
213 | ipcarg_t te, rv;
|
---|
214 | int rc;
|
---|
215 |
|
---|
216 | rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
|
---|
217 | UPPER32(id), &te, &rv);
|
---|
218 | *texit = te;
|
---|
219 | *retval = rv;
|
---|
220 |
|
---|
221 | return rc;
|
---|
222 | }
|
---|
223 |
|
---|
224 | int task_retval(int val)
|
---|
225 | {
|
---|
226 | return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** @}
|
---|
230 | */
|
---|