[45454e9b] | 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
|
---|
[937aeee] | 33 | */
|
---|
[45454e9b] | 34 |
|
---|
| 35 | #include <ipc/loader.h>
|
---|
[bfd1546] | 36 | #include <ipc/services.h>
|
---|
[79ae36dd] | 37 | #include <ns.h>
|
---|
[45454e9b] | 38 | #include <libc.h>
|
---|
[0993087] | 39 | #include <task.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[45454e9b] | 41 | #include <stdlib.h>
|
---|
| 42 | #include <async.h>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <vfs/vfs.h>
|
---|
| 45 | #include <loader/loader.h>
|
---|
[79ae36dd] | 46 | #include "private/loader.h"
|
---|
[45454e9b] | 47 |
|
---|
| 48 | /** Connect to a new program loader.
|
---|
| 49 | *
|
---|
| 50 | * Spawns a new program loader task and returns the connection structure.
|
---|
[937aeee] | 51 | *
|
---|
| 52 | * @param name Symbolic name to set on the newly created task.
|
---|
| 53 | *
|
---|
| 54 | * @return Pointer to the loader connection structure (should be
|
---|
| 55 | * deallocated using free() after use).
|
---|
| 56 | *
|
---|
[45454e9b] | 57 | */
|
---|
[bfd1546] | 58 | int loader_spawn(const char *name)
|
---|
[45454e9b] | 59 | {
|
---|
[bfd1546] | 60 | return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER,
|
---|
[9eb3623] | 61 | (sysarg_t) name, str_size(name));
|
---|
[bfd1546] | 62 | }
|
---|
[45454e9b] | 63 |
|
---|
[bfd1546] | 64 | loader_t *loader_connect(void)
|
---|
| 65 | {
|
---|
[937aeee] | 66 | loader_t *ldr = malloc(sizeof(loader_t));
|
---|
[45454e9b] | 67 | if (ldr == NULL)
|
---|
| 68 | return NULL;
|
---|
[937aeee] | 69 |
|
---|
[79ae36dd] | 70 | async_sess_t *sess =
|
---|
[566992e1] | 71 | service_connect_blocking_iface_extended(SERVICE_LOADER,
|
---|
| 72 | INTERFACE_LOADER, 0);
|
---|
[79ae36dd] | 73 | if (sess == NULL) {
|
---|
| 74 | free(ldr);
|
---|
| 75 | return NULL;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | ldr->sess = sess;
|
---|
[937aeee] | 79 | return ldr;
|
---|
[45454e9b] | 80 | }
|
---|
| 81 |
|
---|
| 82 | /** Get ID of the new task.
|
---|
| 83 | *
|
---|
| 84 | * Retrieves the ID of the new task from the loader.
|
---|
| 85 | *
|
---|
[937aeee] | 86 | * @param ldr Loader connection structure.
|
---|
| 87 | * @param task_id Points to a variable where the ID should be stored.
|
---|
| 88 | *
|
---|
| 89 | * @return Zero on success or negative error code.
|
---|
| 90 | *
|
---|
[45454e9b] | 91 | */
|
---|
| 92 | int loader_get_task_id(loader_t *ldr, task_id_t *task_id)
|
---|
| 93 | {
|
---|
| 94 | /* Get task ID. */
|
---|
[79ae36dd] | 95 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
| 96 |
|
---|
[937aeee] | 97 | ipc_call_t answer;
|
---|
[79ae36dd] | 98 | aid_t req = async_send_0(exch, LOADER_GET_TASKID, &answer);
|
---|
| 99 | sysarg_t rc = async_data_read_start(exch, task_id, sizeof(task_id_t));
|
---|
| 100 |
|
---|
| 101 | async_exchange_end(exch);
|
---|
| 102 |
|
---|
[45454e9b] | 103 | if (rc != EOK) {
|
---|
[50b581d] | 104 | async_forget(req);
|
---|
[79ae36dd] | 105 | return (int) rc;
|
---|
[45454e9b] | 106 | }
|
---|
[937aeee] | 107 |
|
---|
[79ae36dd] | 108 | async_wait_for(req, &rc);
|
---|
| 109 | return (int) rc;
|
---|
[45454e9b] | 110 | }
|
---|
| 111 |
|
---|
[622cdbe] | 112 | /** Set current working directory for the loaded task.
|
---|
| 113 | *
|
---|
| 114 | * Sets the current working directory for the loaded task.
|
---|
| 115 | *
|
---|
| 116 | * @param ldr Loader connection structure.
|
---|
| 117 | *
|
---|
| 118 | * @return Zero on success or negative error code.
|
---|
| 119 | *
|
---|
| 120 | */
|
---|
| 121 | int loader_set_cwd(loader_t *ldr)
|
---|
| 122 | {
|
---|
[79ae36dd] | 123 | char *cwd = (char *) malloc(MAX_PATH_LEN + 1);
|
---|
[622cdbe] | 124 | if (!cwd)
|
---|
| 125 | return ENOMEM;
|
---|
[79ae36dd] | 126 |
|
---|
[622cdbe] | 127 | if (!getcwd(cwd, MAX_PATH_LEN + 1))
|
---|
[79ae36dd] | 128 | str_cpy(cwd, MAX_PATH_LEN + 1, "/");
|
---|
| 129 |
|
---|
| 130 | size_t len = str_length(cwd);
|
---|
| 131 |
|
---|
| 132 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
[622cdbe] | 133 |
|
---|
| 134 | ipc_call_t answer;
|
---|
[79ae36dd] | 135 | aid_t req = async_send_0(exch, LOADER_SET_CWD, &answer);
|
---|
| 136 | sysarg_t rc = async_data_write_start(exch, cwd, len);
|
---|
| 137 |
|
---|
| 138 | async_exchange_end(exch);
|
---|
[622cdbe] | 139 | free(cwd);
|
---|
[79ae36dd] | 140 |
|
---|
[622cdbe] | 141 | if (rc != EOK) {
|
---|
[50b581d] | 142 | async_forget(req);
|
---|
[79ae36dd] | 143 | return (int) rc;
|
---|
[622cdbe] | 144 | }
|
---|
| 145 |
|
---|
[79ae36dd] | 146 | async_wait_for(req, &rc);
|
---|
| 147 | return (int) rc;
|
---|
[622cdbe] | 148 | }
|
---|
| 149 |
|
---|
[45454e9b] | 150 | /** Set pathname of the program to load.
|
---|
| 151 | *
|
---|
| 152 | * Sets the name of the program file to load. The name can be relative
|
---|
| 153 | * to the current working directory (it will be absolutized before
|
---|
| 154 | * sending to the loader).
|
---|
| 155 | *
|
---|
[937aeee] | 156 | * @param ldr Loader connection structure.
|
---|
| 157 | * @param path Pathname of the program file.
|
---|
| 158 | *
|
---|
| 159 | * @return Zero on success or negative error code.
|
---|
| 160 | *
|
---|
[45454e9b] | 161 | */
|
---|
| 162 | int loader_set_pathname(loader_t *ldr, const char *path)
|
---|
| 163 | {
|
---|
| 164 | size_t pa_len;
|
---|
[937aeee] | 165 | char *pa = absolutize(path, &pa_len);
|
---|
[45454e9b] | 166 | if (!pa)
|
---|
[79ae36dd] | 167 | return ENOMEM;
|
---|
[937aeee] | 168 |
|
---|
[45454e9b] | 169 | /* Send program pathname */
|
---|
[79ae36dd] | 170 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
| 171 |
|
---|
[937aeee] | 172 | ipc_call_t answer;
|
---|
[79ae36dd] | 173 | aid_t req = async_send_0(exch, LOADER_SET_PATHNAME, &answer);
|
---|
| 174 | sysarg_t rc = async_data_write_start(exch, (void *) pa, pa_len);
|
---|
| 175 |
|
---|
| 176 | async_exchange_end(exch);
|
---|
| 177 | free(pa);
|
---|
| 178 |
|
---|
[45454e9b] | 179 | if (rc != EOK) {
|
---|
[50b581d] | 180 | async_forget(req);
|
---|
[79ae36dd] | 181 | return (int) rc;
|
---|
[45454e9b] | 182 | }
|
---|
[937aeee] | 183 |
|
---|
[79ae36dd] | 184 | async_wait_for(req, &rc);
|
---|
| 185 | return (int) rc;
|
---|
[45454e9b] | 186 | }
|
---|
| 187 |
|
---|
| 188 | /** Set command-line arguments for the program.
|
---|
| 189 | *
|
---|
| 190 | * Sets the vector of command-line arguments to be passed to the loaded
|
---|
| 191 | * program. By convention, the very first argument is typically the same as
|
---|
| 192 | * the command used to execute the program.
|
---|
| 193 | *
|
---|
[937aeee] | 194 | * @param ldr Loader connection structure.
|
---|
| 195 | * @param argv NULL-terminated array of pointers to arguments.
|
---|
| 196 | *
|
---|
| 197 | * @return Zero on success or negative error code.
|
---|
| 198 | *
|
---|
[45454e9b] | 199 | */
|
---|
[a000878c] | 200 | int loader_set_args(loader_t *ldr, const char *const argv[])
|
---|
[45454e9b] | 201 | {
|
---|
[937aeee] | 202 | /*
|
---|
[45454e9b] | 203 | * Serialize the arguments into a single array. First
|
---|
| 204 | * compute size of the buffer needed.
|
---|
| 205 | */
|
---|
[a000878c] | 206 | const char *const *ap = argv;
|
---|
[937aeee] | 207 | size_t buffer_size = 0;
|
---|
[45454e9b] | 208 | while (*ap != NULL) {
|
---|
[9eb3623] | 209 | buffer_size += str_size(*ap) + 1;
|
---|
[937aeee] | 210 | ap++;
|
---|
[45454e9b] | 211 | }
|
---|
[937aeee] | 212 |
|
---|
| 213 | char *arg_buf = malloc(buffer_size);
|
---|
| 214 | if (arg_buf == NULL)
|
---|
| 215 | return ENOMEM;
|
---|
| 216 |
|
---|
[45454e9b] | 217 | /* Now fill the buffer with null-terminated argument strings */
|
---|
| 218 | ap = argv;
|
---|
[937aeee] | 219 | char *dp = arg_buf;
|
---|
| 220 |
|
---|
[45454e9b] | 221 | while (*ap != NULL) {
|
---|
[6eb2e96] | 222 | str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
|
---|
[9eb3623] | 223 | dp += str_size(*ap) + 1;
|
---|
[937aeee] | 224 | ap++;
|
---|
[45454e9b] | 225 | }
|
---|
[937aeee] | 226 |
|
---|
[45454e9b] | 227 | /* Send serialized arguments to the loader */
|
---|
[79ae36dd] | 228 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
| 229 |
|
---|
[937aeee] | 230 | ipc_call_t answer;
|
---|
[79ae36dd] | 231 | aid_t req = async_send_0(exch, LOADER_SET_ARGS, &answer);
|
---|
| 232 | sysarg_t rc = async_data_write_start(exch, (void *) arg_buf,
|
---|
| 233 | buffer_size);
|
---|
| 234 |
|
---|
| 235 | async_exchange_end(exch);
|
---|
| 236 | free(arg_buf);
|
---|
| 237 |
|
---|
[45454e9b] | 238 | if (rc != EOK) {
|
---|
[50b581d] | 239 | async_forget(req);
|
---|
[79ae36dd] | 240 | return (int) rc;
|
---|
[45454e9b] | 241 | }
|
---|
[937aeee] | 242 |
|
---|
[45454e9b] | 243 | async_wait_for(req, &rc);
|
---|
[79ae36dd] | 244 | return (int) rc;
|
---|
[937aeee] | 245 | }
|
---|
[45454e9b] | 246 |
|
---|
[937aeee] | 247 | /** Set preset files for the program.
|
---|
| 248 | *
|
---|
| 249 | * Sets the vector of preset files to be passed to the loaded
|
---|
| 250 | * program. By convention, the first three files represent stdin,
|
---|
| 251 | * stdout and stderr respectively.
|
---|
| 252 | *
|
---|
| 253 | * @param ldr Loader connection structure.
|
---|
| 254 | * @param files NULL-terminated array of pointers to files.
|
---|
| 255 | *
|
---|
| 256 | * @return Zero on success or negative error code.
|
---|
| 257 | *
|
---|
| 258 | */
|
---|
[7171760] | 259 | int loader_set_files(loader_t *ldr, int * const files[])
|
---|
[937aeee] | 260 | {
|
---|
| 261 | /* Send serialized files to the loader */
|
---|
[79ae36dd] | 262 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
[7171760] | 263 | async_exch_t *vfs_exch = vfs_exchange_begin();
|
---|
[79ae36dd] | 264 |
|
---|
[7171760] | 265 | int i;
|
---|
[0fe52ef] | 266 | for (i = 0; files[i]; i++);
|
---|
[7171760] | 267 |
|
---|
[937aeee] | 268 | ipc_call_t answer;
|
---|
[7171760] | 269 | aid_t req = async_send_1(exch, LOADER_SET_FILES, i, &answer);
|
---|
| 270 |
|
---|
[d894fbd] | 271 | sysarg_t rc = EOK;
|
---|
[79ae36dd] | 272 |
|
---|
[7171760] | 273 | for (i = 0; files[i]; i++) {
|
---|
| 274 | rc = async_state_change_start(exch, VFS_PASS_HANDLE, *files[i],
|
---|
| 275 | 0, vfs_exch);
|
---|
| 276 | if (rc != EOK)
|
---|
| 277 | break;
|
---|
| 278 | }
|
---|
[79ae36dd] | 279 |
|
---|
[7171760] | 280 | vfs_exchange_end(vfs_exch);
|
---|
| 281 | async_exchange_end(exch);
|
---|
| 282 |
|
---|
[937aeee] | 283 | if (rc != EOK) {
|
---|
[50b581d] | 284 | async_forget(req);
|
---|
[79ae36dd] | 285 | return (int) rc;
|
---|
[937aeee] | 286 | }
|
---|
| 287 |
|
---|
| 288 | async_wait_for(req, &rc);
|
---|
[79ae36dd] | 289 | return (int) rc;
|
---|
[45454e9b] | 290 | }
|
---|
| 291 |
|
---|
[4470e26] | 292 | /** Instruct loader to load the program.
|
---|
| 293 | *
|
---|
| 294 | * If this function succeeds, the program has been successfully loaded
|
---|
| 295 | * and is ready to be executed.
|
---|
| 296 | *
|
---|
[937aeee] | 297 | * @param ldr Loader connection structure.
|
---|
| 298 | *
|
---|
| 299 | * @return Zero on success or negative error code.
|
---|
| 300 | *
|
---|
[4470e26] | 301 | */
|
---|
| 302 | int loader_load_program(loader_t *ldr)
|
---|
| 303 | {
|
---|
[79ae36dd] | 304 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
| 305 | int rc = async_req_0_0(exch, LOADER_LOAD);
|
---|
| 306 | async_exchange_end(exch);
|
---|
| 307 |
|
---|
| 308 | return rc;
|
---|
[4470e26] | 309 | }
|
---|
| 310 |
|
---|
[45454e9b] | 311 | /** Instruct loader to execute the program.
|
---|
[4470e26] | 312 | *
|
---|
| 313 | * Note that this function blocks until the loader actually replies
|
---|
| 314 | * so you cannot expect this function to return if you are debugging
|
---|
| 315 | * the task and its thread is stopped.
|
---|
[45454e9b] | 316 | *
|
---|
[79ae36dd] | 317 | * After using this function, no further operations can be performed
|
---|
| 318 | * on the loader structure and it is deallocated.
|
---|
[45454e9b] | 319 | *
|
---|
[937aeee] | 320 | * @param ldr Loader connection structure.
|
---|
| 321 | *
|
---|
| 322 | * @return Zero on success or negative error code.
|
---|
| 323 | *
|
---|
[45454e9b] | 324 | */
|
---|
[4470e26] | 325 | int loader_run(loader_t *ldr)
|
---|
[45454e9b] | 326 | {
|
---|
[79ae36dd] | 327 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
| 328 | int rc = async_req_0_0(exch, LOADER_RUN);
|
---|
| 329 | async_exchange_end(exch);
|
---|
| 330 |
|
---|
[45454e9b] | 331 | if (rc != EOK)
|
---|
| 332 | return rc;
|
---|
[937aeee] | 333 |
|
---|
[79ae36dd] | 334 | async_hangup(ldr->sess);
|
---|
| 335 | free(ldr);
|
---|
| 336 |
|
---|
[45454e9b] | 337 | return EOK;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | /** Cancel the loader session.
|
---|
| 341 | *
|
---|
[79ae36dd] | 342 | * Tell the loader not to load any program and terminate.
|
---|
| 343 | * After using this function, no further operations can be performed
|
---|
| 344 | * on the loader structure and it is deallocated.
|
---|
[45454e9b] | 345 | *
|
---|
[937aeee] | 346 | * @param ldr Loader connection structure.
|
---|
| 347 | *
|
---|
| 348 | * @return Zero on success or negative error code.
|
---|
| 349 | *
|
---|
[45454e9b] | 350 | */
|
---|
| 351 | void loader_abort(loader_t *ldr)
|
---|
| 352 | {
|
---|
[79ae36dd] | 353 | async_hangup(ldr->sess);
|
---|
| 354 | free(ldr);
|
---|
[45454e9b] | 355 | }
|
---|
| 356 |
|
---|
| 357 | /** @}
|
---|
| 358 | */
|
---|