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