source: mainline/uspace/lib/c/generic/loader.c@ 1be7bee

Last change on this file since 1be7bee 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: 8.7 KB
Line 
1/*
2 * Copyright (c) 2008 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#include <async.h>
36#include <errno.h>
37#include <ipc/loader.h>
38#include <ipc/services.h>
39#include <ipc/taskman.h>
40#include <libc.h>
41#include <loader/loader.h>
42#include <ns.h>
43#include <stdlib.h>
44#include <str.h>
45#include <task.h>
46#include <vfs/vfs.h>
47#include "private/loader.h"
48
49/** Connect to a new program loader.
50 *
51 * Spawns a new program loader task and returns the connection structure.
52 *
53 * @param name Symbolic name to set on the newly created task.
54 *
55 * @return Error code.
56 */
57errno_t loader_spawn(const char *name)
58{
59 return (errno_t) __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER,
60 (sysarg_t) name, str_size(name));
61}
62
63loader_t *loader_connect(void)
64{
65 loader_t *ldr = malloc(sizeof(loader_t));
66 if (ldr == NULL)
67 return NULL;
68
69 async_sess_t *sess =
70 service_connect_blocking(SERVICE_TASKMAN, TASKMAN_CONNECT_TO_LOADER, 0);
71 if (sess == NULL) {
72 free(ldr);
73 return NULL;
74 }
75
76 ldr->sess = sess;
77 return ldr;
78}
79
80/** Get ID of the new task.
81 *
82 * Retrieves the ID of the new task from the loader.
83 *
84 * @param ldr Loader connection structure.
85 * @param task_id Points to a variable where the ID should be stored.
86 *
87 * @return Zero on success or an error code.
88 *
89 */
90errno_t loader_get_task_id(loader_t *ldr, task_id_t *task_id)
91{
92 /* Get task ID. */
93 async_exch_t *exch = async_exchange_begin(ldr->sess);
94
95 ipc_call_t answer;
96 aid_t req = async_send_0(exch, LOADER_GET_TASKID, &answer);
97 errno_t rc = async_data_read_start(exch, task_id, sizeof(task_id_t));
98
99 async_exchange_end(exch);
100
101 if (rc != EOK) {
102 async_forget(req);
103 return (errno_t) rc;
104 }
105
106 async_wait_for(req, &rc);
107 return (errno_t) rc;
108}
109
110/** Set current working directory for the loaded task.
111 *
112 * Sets the current working directory for the loaded task.
113 *
114 * @param ldr Loader connection structure.
115 *
116 * @return Zero on success or an error code.
117 *
118 */
119errno_t loader_set_cwd(loader_t *ldr)
120{
121 char *cwd = (char *) malloc(MAX_PATH_LEN + 1);
122 if (!cwd)
123 return ENOMEM;
124
125 if (vfs_cwd_get(cwd, MAX_PATH_LEN + 1) != EOK)
126 str_cpy(cwd, MAX_PATH_LEN + 1, "/");
127
128 size_t len = str_length(cwd);
129
130 async_exch_t *exch = async_exchange_begin(ldr->sess);
131
132 ipc_call_t answer;
133 aid_t req = async_send_0(exch, LOADER_SET_CWD, &answer);
134 errno_t rc = async_data_write_start(exch, cwd, len);
135
136 async_exchange_end(exch);
137 free(cwd);
138
139 if (rc != EOK) {
140 async_forget(req);
141 return (errno_t) rc;
142 }
143
144 async_wait_for(req, &rc);
145 return (errno_t) rc;
146}
147
148/** Set the program to load.
149 *
150 * @param ldr Loader connection structure.
151 * @param name Name to set for the spawned program.
152 * @param file Program file.
153 *
154 * @return Zero on success or an error code.
155 *
156 */
157errno_t loader_set_program(loader_t *ldr, const char *name, int file)
158{
159 async_exch_t *exch = async_exchange_begin(ldr->sess);
160
161 ipc_call_t answer;
162 aid_t req = async_send_0(exch, LOADER_SET_PROGRAM, &answer);
163
164 errno_t rc = async_data_write_start(exch, name, str_size(name) + 1);
165 if (rc == EOK) {
166 async_exch_t *vfs_exch = vfs_exchange_begin();
167 rc = vfs_pass_handle(vfs_exch, file, exch);
168 vfs_exchange_end(vfs_exch);
169 }
170
171 async_exchange_end(exch);
172
173 if (rc != EOK) {
174 async_forget(req);
175 return (errno_t) rc;
176 }
177
178 async_wait_for(req, &rc);
179 return (errno_t) rc;
180}
181
182/** Set the program to load by path.
183 *
184 * @param ldr Loader connection structure.
185 * @param path Program path.
186 *
187 * @return Zero on success or an error code.
188 *
189 */
190errno_t loader_set_program_path(loader_t *ldr, const char *path)
191{
192 size_t abslen;
193 char *abspath = vfs_absolutize(path, &abslen);
194 if (!abspath)
195 return ENOMEM;
196
197 int fd;
198 errno_t rc = vfs_lookup(path, 0, &fd);
199 if (rc != EOK) {
200 return rc;
201 }
202
203 rc = loader_set_program(ldr, abspath, fd);
204 vfs_put(fd);
205 return rc;
206}
207
208/** Set command-line arguments for the program.
209 *
210 * Sets the vector of command-line arguments to be passed to the loaded
211 * program. By convention, the very first argument is typically the same as
212 * the command used to execute the program.
213 *
214 * @param ldr Loader connection structure.
215 * @param argv NULL-terminated array of pointers to arguments.
216 *
217 * @return Zero on success or an error code.
218 *
219 */
220errno_t loader_set_args(loader_t *ldr, const char *const argv[])
221{
222 /*
223 * Serialize the arguments into a single array. First
224 * compute size of the buffer needed.
225 */
226 const char *const *ap = argv;
227 size_t buffer_size = 0;
228 while (*ap != NULL) {
229 buffer_size += str_size(*ap) + 1;
230 ap++;
231 }
232
233 char *arg_buf = malloc(buffer_size);
234 if (arg_buf == NULL)
235 return ENOMEM;
236
237 /* Now fill the buffer with null-terminated argument strings */
238 ap = argv;
239 char *dp = arg_buf;
240
241 while (*ap != NULL) {
242 str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
243 dp += str_size(*ap) + 1;
244 ap++;
245 }
246
247 /* Send serialized arguments to the loader */
248 async_exch_t *exch = async_exchange_begin(ldr->sess);
249
250 ipc_call_t answer;
251 aid_t req = async_send_0(exch, LOADER_SET_ARGS, &answer);
252 errno_t rc = async_data_write_start(exch, (void *) arg_buf,
253 buffer_size);
254
255 async_exchange_end(exch);
256 free(arg_buf);
257
258 if (rc != EOK) {
259 async_forget(req);
260 return (errno_t) rc;
261 }
262
263 async_wait_for(req, &rc);
264 return (errno_t) rc;
265}
266
267/** Add a file to the task's inbox.
268 *
269 * @param ldr Loader connection structure.
270 * @param name Identification of the file.
271 * @param file The file's descriptor.
272 *
273 * @return Zero on success or an error code.
274 *
275 */
276errno_t loader_add_inbox(loader_t *ldr, const char *name, int file)
277{
278 async_exch_t *exch = async_exchange_begin(ldr->sess);
279 async_exch_t *vfs_exch = vfs_exchange_begin();
280
281 aid_t req = async_send_0(exch, LOADER_ADD_INBOX, NULL);
282
283 errno_t rc = async_data_write_start(exch, name, str_size(name) + 1);
284 if (rc == EOK) {
285 rc = vfs_pass_handle(vfs_exch, file, exch);
286 }
287
288 async_exchange_end(vfs_exch);
289 async_exchange_end(exch);
290
291 if (rc == EOK) {
292 async_wait_for(req, &rc);
293 } else {
294 async_forget(req);
295 }
296
297 return (errno_t) rc;
298}
299
300/** Instruct loader to load the program.
301 *
302 * If this function succeeds, the program has been successfully loaded
303 * and is ready to be executed.
304 *
305 * @param ldr Loader connection structure.
306 *
307 * @return Zero on success or an error code.
308 *
309 */
310errno_t loader_load_program(loader_t *ldr)
311{
312 async_exch_t *exch = async_exchange_begin(ldr->sess);
313 errno_t rc = async_req_0_0(exch, LOADER_LOAD);
314 async_exchange_end(exch);
315
316 return rc;
317}
318
319/** Instruct loader to execute the program.
320 *
321 * Note that this function blocks until the loader actually replies
322 * so you cannot expect this function to return if you are debugging
323 * the task and its thread is stopped.
324 *
325 * After using this function, no further operations can be performed
326 * on the loader structure and it is deallocated.
327 *
328 * @param ldr Loader connection structure.
329 *
330 * @return Zero on success or an error code.
331 *
332 */
333errno_t loader_run(loader_t *ldr)
334{
335 async_exch_t *exch = async_exchange_begin(ldr->sess);
336 errno_t rc = async_req_0_0(exch, LOADER_RUN);
337 async_exchange_end(exch);
338
339 if (rc != EOK)
340 return rc;
341
342 async_hangup(ldr->sess);
343 free(ldr);
344
345 return EOK;
346}
347
348/** Cancel the loader session.
349 *
350 * Tell the loader not to load any program and terminate.
351 * After using this function, no further operations can be performed
352 * on the loader structure and it is deallocated.
353 *
354 * @param ldr Loader connection structure.
355 *
356 * @return Zero on success or an error code.
357 *
358 */
359void loader_abort(loader_t *ldr)
360{
361 async_hangup(ldr->sess);
362 free(ldr);
363}
364
365/** @}
366 */
Note: See TracBrowser for help on using the repository browser.