Rev 4491 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3469 | svoboda | 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 |
||
| 4476 | decky | 33 | */ |
| 3469 | svoboda | 34 | |
| 35 | #include <ipc/ipc.h> |
||
| 36 | #include <ipc/loader.h> |
||
| 3896 | svoboda | 37 | #include <ipc/services.h> |
| 3469 | svoboda | 38 | #include <libc.h> |
| 3475 | svoboda | 39 | #include <task.h> |
| 3469 | svoboda | 40 | #include <string.h> |
| 41 | #include <stdlib.h> |
||
| 42 | #include <async.h> |
||
| 43 | #include <errno.h> |
||
| 44 | #include <vfs/vfs.h> |
||
| 45 | #include <loader/loader.h> |
||
| 46 | |||
| 47 | /** Connect to a new program loader. |
||
| 48 | * |
||
| 49 | * Spawns a new program loader task and returns the connection structure. |
||
| 4476 | decky | 50 | * |
| 51 | * @param name Symbolic name to set on the newly created task. |
||
| 52 | * |
||
| 53 | * @return Pointer to the loader connection structure (should be |
||
| 54 | * deallocated using free() after use). |
||
| 55 | * |
||
| 3469 | svoboda | 56 | */ |
| 3896 | svoboda | 57 | int loader_spawn(const char *name) |
| 3469 | svoboda | 58 | { |
| 3896 | svoboda | 59 | return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER, |
| 4256 | svoboda | 60 | (sysarg_t) name, str_size(name)); |
| 3896 | svoboda | 61 | } |
| 62 | |||
| 63 | loader_t *loader_connect(void) |
||
| 64 | { |
||
| 4476 | decky | 65 | int phone_id = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_LOAD, 0, 0); |
| 3896 | svoboda | 66 | if (phone_id < 0) |
| 3469 | svoboda | 67 | return NULL; |
| 4476 | decky | 68 | |
| 69 | loader_t *ldr = malloc(sizeof(loader_t)); |
||
| 3469 | svoboda | 70 | if (ldr == NULL) |
| 71 | return NULL; |
||
| 4476 | decky | 72 | |
| 3469 | svoboda | 73 | ldr->phone_id = phone_id; |
| 4476 | decky | 74 | return ldr; |
| 3469 | svoboda | 75 | } |
| 76 | |||
| 77 | /** Get ID of the new task. |
||
| 78 | * |
||
| 79 | * Retrieves the ID of the new task from the loader. |
||
| 80 | * |
||
| 4476 | decky | 81 | * @param ldr Loader connection structure. |
| 82 | * @param task_id Points to a variable where the ID should be stored. |
||
| 83 | * |
||
| 84 | * @return Zero on success or negative error code. |
||
| 85 | * |
||
| 3469 | svoboda | 86 | */ |
| 87 | int loader_get_task_id(loader_t *ldr, task_id_t *task_id) |
||
| 88 | { |
||
| 4476 | decky | 89 | /* Get task ID. */ |
| 3469 | svoboda | 90 | ipc_call_t answer; |
| 4476 | decky | 91 | aid_t req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer); |
| 92 | int rc = ipc_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t)); |
||
| 3469 | svoboda | 93 | if (rc != EOK) { |
| 94 | async_wait_for(req, NULL); |
||
| 95 | return rc; |
||
| 96 | } |
||
| 4476 | decky | 97 | |
| 98 | ipcarg_t retval; |
||
| 3469 | svoboda | 99 | async_wait_for(req, &retval); |
| 4476 | decky | 100 | return (int) retval; |
| 3469 | svoboda | 101 | } |
| 102 | |||
| 103 | /** Set pathname of the program to load. |
||
| 104 | * |
||
| 105 | * Sets the name of the program file to load. The name can be relative |
||
| 106 | * to the current working directory (it will be absolutized before |
||
| 107 | * sending to the loader). |
||
| 108 | * |
||
| 4476 | decky | 109 | * @param ldr Loader connection structure. |
| 110 | * @param path Pathname of the program file. |
||
| 111 | * |
||
| 112 | * @return Zero on success or negative error code. |
||
| 113 | * |
||
| 3469 | svoboda | 114 | */ |
| 115 | int loader_set_pathname(loader_t *ldr, const char *path) |
||
| 116 | { |
||
| 117 | size_t pa_len; |
||
| 4476 | decky | 118 | char *pa = absolutize(path, &pa_len); |
| 3469 | svoboda | 119 | if (!pa) |
| 120 | return 0; |
||
| 4476 | decky | 121 | |
| 3469 | svoboda | 122 | /* Send program pathname */ |
| 4476 | decky | 123 | ipc_call_t answer; |
| 124 | aid_t req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer); |
||
| 125 | int rc = ipc_data_write_start(ldr->phone_id, (void *) pa, pa_len); |
||
| 3469 | svoboda | 126 | if (rc != EOK) { |
| 127 | async_wait_for(req, NULL); |
||
| 128 | return rc; |
||
| 129 | } |
||
| 4476 | decky | 130 | |
| 3469 | svoboda | 131 | free(pa); |
| 4476 | decky | 132 | |
| 133 | ipcarg_t retval; |
||
| 3469 | svoboda | 134 | async_wait_for(req, &retval); |
| 4476 | decky | 135 | return (int) retval; |
| 3469 | svoboda | 136 | } |
| 137 | |||
| 138 | /** Set command-line arguments for the program. |
||
| 139 | * |
||
| 140 | * Sets the vector of command-line arguments to be passed to the loaded |
||
| 141 | * program. By convention, the very first argument is typically the same as |
||
| 142 | * the command used to execute the program. |
||
| 143 | * |
||
| 4476 | decky | 144 | * @param ldr Loader connection structure. |
| 145 | * @param argv NULL-terminated array of pointers to arguments. |
||
| 146 | * |
||
| 147 | * @return Zero on success or negative error code. |
||
| 148 | * |
||
| 3469 | svoboda | 149 | */ |
| 150 | int loader_set_args(loader_t *ldr, char *const argv[]) |
||
| 151 | { |
||
| 4476 | decky | 152 | /* |
| 3469 | svoboda | 153 | * Serialize the arguments into a single array. First |
| 154 | * compute size of the buffer needed. |
||
| 155 | */ |
||
| 4476 | decky | 156 | char *const *ap = argv; |
| 157 | size_t buffer_size = 0; |
||
| 3469 | svoboda | 158 | while (*ap != NULL) { |
| 4256 | svoboda | 159 | buffer_size += str_size(*ap) + 1; |
| 4476 | decky | 160 | ap++; |
| 3469 | svoboda | 161 | } |
| 4476 | decky | 162 | |
| 163 | char *arg_buf = malloc(buffer_size); |
||
| 164 | if (arg_buf == NULL) |
||
| 165 | return ENOMEM; |
||
| 166 | |||
| 3469 | svoboda | 167 | /* Now fill the buffer with null-terminated argument strings */ |
| 168 | ap = argv; |
||
| 4476 | decky | 169 | char *dp = arg_buf; |
| 170 | |||
| 3469 | svoboda | 171 | while (*ap != NULL) { |
| 4268 | svoboda | 172 | str_cpy(dp, buffer_size - (dp - arg_buf), *ap); |
| 4256 | svoboda | 173 | dp += str_size(*ap) + 1; |
| 4476 | decky | 174 | ap++; |
| 3469 | svoboda | 175 | } |
| 4476 | decky | 176 | |
| 3469 | svoboda | 177 | /* Send serialized arguments to the loader */ |
| 4476 | decky | 178 | ipc_call_t answer; |
| 179 | aid_t req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer); |
||
| 180 | ipcarg_t rc = ipc_data_write_start(ldr->phone_id, (void *) arg_buf, buffer_size); |
||
| 3469 | svoboda | 181 | if (rc != EOK) { |
| 182 | async_wait_for(req, NULL); |
||
| 183 | return rc; |
||
| 184 | } |
||
| 4476 | decky | 185 | |
| 3469 | svoboda | 186 | async_wait_for(req, &rc); |
| 4476 | decky | 187 | if (rc != EOK) |
| 188 | return rc; |
||
| 189 | |||
| 3469 | svoboda | 190 | /* Free temporary buffer */ |
| 191 | free(arg_buf); |
||
| 4476 | decky | 192 | |
| 193 | return EOK; |
||
| 194 | } |
||
| 3469 | svoboda | 195 | |
| 4476 | decky | 196 | /** Set preset files for the program. |
| 197 | * |
||
| 198 | * Sets the vector of preset files to be passed to the loaded |
||
| 199 | * program. By convention, the first three files represent stdin, |
||
| 200 | * stdout and stderr respectively. |
||
| 201 | * |
||
| 202 | * @param ldr Loader connection structure. |
||
| 203 | * @param files NULL-terminated array of pointers to files. |
||
| 204 | * |
||
| 205 | * @return Zero on success or negative error code. |
||
| 206 | * |
||
| 207 | */ |
||
| 4492 | jermar | 208 | int loader_set_files(loader_t *ldr, fdi_node_t *const files[]) |
| 4476 | decky | 209 | { |
| 210 | /* |
||
| 211 | * Serialize the arguments into a single array. First |
||
| 212 | * compute size of the buffer needed. |
||
| 213 | */ |
||
| 4492 | jermar | 214 | fdi_node_t *const *ap = files; |
| 4476 | decky | 215 | size_t count = 0; |
| 216 | while (*ap != NULL) { |
||
| 217 | count++; |
||
| 218 | ap++; |
||
| 219 | } |
||
| 220 | |||
| 4492 | jermar | 221 | fdi_node_t *files_buf; |
| 222 | files_buf = (fdi_node_t *) malloc(count * sizeof(fdi_node_t)); |
||
| 4476 | decky | 223 | if (files_buf == NULL) |
| 224 | return ENOMEM; |
||
| 225 | |||
| 226 | /* Fill the buffer */ |
||
| 227 | size_t i; |
||
| 228 | for (i = 0; i < count; i++) |
||
| 229 | files_buf[i] = *files[i]; |
||
| 230 | |||
| 231 | /* Send serialized files to the loader */ |
||
| 232 | ipc_call_t answer; |
||
| 233 | aid_t req = async_send_0(ldr->phone_id, LOADER_SET_FILES, &answer); |
||
| 234 | ipcarg_t rc = ipc_data_write_start(ldr->phone_id, (void *) files_buf, |
||
| 4492 | jermar | 235 | count * sizeof(fdi_node_t)); |
| 4476 | decky | 236 | if (rc != EOK) { |
| 237 | async_wait_for(req, NULL); |
||
| 238 | return rc; |
||
| 239 | } |
||
| 240 | |||
| 241 | async_wait_for(req, &rc); |
||
| 242 | if (rc != EOK) |
||
| 243 | return rc; |
||
| 244 | |||
| 245 | /* Free temporary buffer */ |
||
| 246 | free(files_buf); |
||
| 247 | |||
| 3469 | svoboda | 248 | return EOK; |
| 249 | } |
||
| 250 | |||
| 3470 | svoboda | 251 | /** Instruct loader to load the program. |
| 252 | * |
||
| 253 | * If this function succeeds, the program has been successfully loaded |
||
| 254 | * and is ready to be executed. |
||
| 255 | * |
||
| 4476 | decky | 256 | * @param ldr Loader connection structure. |
| 257 | * |
||
| 258 | * @return Zero on success or negative error code. |
||
| 259 | * |
||
| 3470 | svoboda | 260 | */ |
| 261 | int loader_load_program(loader_t *ldr) |
||
| 262 | { |
||
| 4476 | decky | 263 | return (int) async_req_0_0(ldr->phone_id, LOADER_LOAD); |
| 3470 | svoboda | 264 | } |
| 265 | |||
| 3469 | svoboda | 266 | /** Instruct loader to execute the program. |
| 267 | * |
||
| 3470 | svoboda | 268 | * Note that this function blocks until the loader actually replies |
| 269 | * so you cannot expect this function to return if you are debugging |
||
| 270 | * the task and its thread is stopped. |
||
| 271 | * |
||
| 3469 | svoboda | 272 | * After using this function, no further operations must be performed |
| 273 | * on the loader structure. It should be de-allocated using free(). |
||
| 274 | * |
||
| 4476 | decky | 275 | * @param ldr Loader connection structure. |
| 276 | * |
||
| 277 | * @return Zero on success or negative error code. |
||
| 278 | * |
||
| 3469 | svoboda | 279 | */ |
| 3470 | svoboda | 280 | int loader_run(loader_t *ldr) |
| 3469 | svoboda | 281 | { |
| 4476 | decky | 282 | int rc = async_req_0_0(ldr->phone_id, LOADER_RUN); |
| 3469 | svoboda | 283 | if (rc != EOK) |
| 284 | return rc; |
||
| 4476 | decky | 285 | |
| 4241 | svoboda | 286 | ipc_hangup(ldr->phone_id); |
| 287 | ldr->phone_id = 0; |
||
| 3469 | svoboda | 288 | return EOK; |
| 289 | } |
||
| 290 | |||
| 291 | /** Cancel the loader session. |
||
| 292 | * |
||
| 293 | * Tells the loader not to load any program and terminate. |
||
| 294 | * After using this function, no further operations must be performed |
||
| 295 | * on the loader structure. It should be de-allocated using free(). |
||
| 296 | * |
||
| 4476 | decky | 297 | * @param ldr Loader connection structure. |
| 298 | * |
||
| 299 | * @return Zero on success or negative error code. |
||
| 300 | * |
||
| 3469 | svoboda | 301 | */ |
| 302 | void loader_abort(loader_t *ldr) |
||
| 303 | { |
||
| 304 | ipc_hangup(ldr->phone_id); |
||
| 305 | ldr->phone_id = 0; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** @} |
||
| 309 | */ |