Subversion Repositories HelenOS

Rev

Rev 3773 | Rev 4343 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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 loader
  30.  * @brief   Loads and runs programs from VFS.
  31.  * @{
  32.  */
  33. /**
  34.  * @file
  35.  * @brief   Loads and runs programs from VFS.
  36.  *
  37.  * The program loader is a special init binary. Its image is used
  38.  * to create a new task upon a @c task_spawn syscall. The syscall
  39.  * returns the id of a phone connected to the newly created task.
  40.  *
  41.  * The caller uses this phone to send the pathname and various other
  42.  * information to the loader. This is normally done by the C library
  43.  * and completely hidden from applications.
  44.  */
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <unistd.h>
  49. #include <bool.h>
  50. #include <fcntl.h>
  51. #include <sys/types.h>
  52. #include <ipc/ipc.h>
  53. #include <ipc/loader.h>
  54. #include <loader/pcb.h>
  55. #include <errno.h>
  56. #include <async.h>
  57. #include <as.h>
  58.  
  59. #include <elf.h>
  60. #include <elf_load.h>
  61.  
  62. #define DPRINTF(...)
  63.  
  64. void program_run(void *entry, pcb_t *pcb);
  65.  
  66. /** Pathname of the file that will be loaded */
  67. static char *pathname = NULL;
  68.  
  69. /** The Program control block */
  70. static pcb_t pcb;
  71.  
  72. /** Number of arguments */
  73. static int argc = 0;
  74. /** Argument vector */
  75. static char **argv = NULL;
  76. /** Buffer holding all arguments */
  77. static char *arg_buf = NULL;
  78.  
  79. static elf_info_t prog_info;
  80. static elf_info_t interp_info;
  81.  
  82. static bool is_dyn_linked;
  83.  
  84.  
  85. static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request)
  86. {
  87.     ipc_callid_t callid;
  88.     task_id_t task_id;
  89.     size_t len;
  90.  
  91.     task_id = task_get_id();
  92.  
  93.     if (!ipc_data_read_receive(&callid, &len)) {
  94.         ipc_answer_0(callid, EINVAL);
  95.         ipc_answer_0(rid, EINVAL);
  96.         return;
  97.     }
  98.  
  99.     if (len > sizeof(task_id)) len = sizeof(task_id);
  100.  
  101.     ipc_data_read_finalize(callid, &task_id, len);
  102.     ipc_answer_0(rid, EOK);
  103. }
  104.  
  105.  
  106. /** Receive a call setting pathname of the program to execute.
  107.  *
  108.  * @param rid
  109.  * @param request
  110.  */
  111. static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
  112. {
  113.     ipc_callid_t callid;
  114.     size_t len;
  115.     char *name_buf;
  116.  
  117.     if (!ipc_data_write_receive(&callid, &len)) {
  118.         ipc_answer_0(callid, EINVAL);
  119.         ipc_answer_0(rid, EINVAL);
  120.         return;
  121.     }
  122.  
  123.     name_buf = malloc(len + 1);
  124.     if (!name_buf) {
  125.         ipc_answer_0(callid, ENOMEM);
  126.         ipc_answer_0(rid, ENOMEM);
  127.         return;
  128.     }
  129.  
  130.     ipc_data_write_finalize(callid, name_buf, len);
  131.     ipc_answer_0(rid, EOK);
  132.  
  133.     if (pathname != NULL) {
  134.         free(pathname);
  135.         pathname = NULL;
  136.     }
  137.  
  138.     name_buf[len] = '\0';
  139.     pathname = name_buf;
  140. }
  141.  
  142. /** Receive a call setting arguments of the program to execute.
  143.  *
  144.  * @param rid
  145.  * @param request
  146.  */
  147. static void loader_set_args(ipc_callid_t rid, ipc_call_t *request)
  148. {
  149.     ipc_callid_t callid;
  150.     size_t buf_len, arg_len;
  151.     char *p;
  152.     int n;
  153.  
  154.     if (!ipc_data_write_receive(&callid, &buf_len)) {
  155.         ipc_answer_0(callid, EINVAL);
  156.         ipc_answer_0(rid, EINVAL);
  157.         return;
  158.     }
  159.  
  160.     if (arg_buf != NULL) {
  161.         free(arg_buf);
  162.         arg_buf = NULL;
  163.     }
  164.  
  165.     if (argv != NULL) {
  166.         free(argv);
  167.         argv = NULL;
  168.     }
  169.  
  170.     arg_buf = malloc(buf_len + 1);
  171.     if (!arg_buf) {
  172.         ipc_answer_0(callid, ENOMEM);
  173.         ipc_answer_0(rid, ENOMEM);
  174.         return;
  175.     }
  176.  
  177.     ipc_data_write_finalize(callid, arg_buf, buf_len);
  178.     ipc_answer_0(rid, EOK);
  179.  
  180.     arg_buf[buf_len] = '\0';
  181.  
  182.     /*
  183.      * Count number of arguments
  184.      */
  185.     p = arg_buf;
  186.     n = 0;
  187.     while (p < arg_buf + buf_len) {
  188.         arg_len = strlen(p);
  189.         p = p + arg_len + 1;
  190.         ++n;
  191.     }
  192.  
  193.     /* Allocate argv */
  194.     argv = malloc((n + 1) * sizeof(char *));
  195.  
  196.     if (argv == NULL) {
  197.         free(arg_buf);
  198.         ipc_answer_0(callid, ENOMEM);
  199.         ipc_answer_0(rid, ENOMEM);
  200.         return;
  201.     }
  202.  
  203.     /*
  204.      * Fill argv with argument pointers
  205.      */
  206.     p = arg_buf;
  207.     n = 0;
  208.     while (p < arg_buf + buf_len) {
  209.         argv[n] = p;
  210.  
  211.         arg_len = strlen(p);
  212.         p = p + arg_len + 1;
  213.         ++n;
  214.     }
  215.  
  216.     argc = n;
  217.     argv[n] = NULL;
  218. }
  219.  
  220. /** Load the previously selected program.
  221.  *
  222.  * @param rid
  223.  * @param request
  224.  * @return 0 on success, !0 on error.
  225.  */
  226. static int loader_load(ipc_callid_t rid, ipc_call_t *request)
  227. {
  228.     int rc;
  229.  
  230.     rc = elf_load_file(pathname, 0, 0, &prog_info);
  231.     if (rc < 0) {
  232.         DPRINTF("Failed to load executable '%s'.\n", pathname);
  233.         ipc_answer_0(rid, EINVAL);
  234.         return 1;
  235.     }
  236.  
  237.     elf_create_pcb(&prog_info, &pcb);
  238.  
  239.     pcb.argc = argc;
  240.     pcb.argv = argv;
  241.  
  242.     if (prog_info.interp == NULL) {
  243.         /* Statically linked program */
  244.         is_dyn_linked = false;
  245.         ipc_answer_0(rid, EOK);
  246.         return 0;
  247.     }
  248.  
  249.     printf("Load ELF interpreter '%s'\n", prog_info.interp);
  250.     rc = elf_load_file(prog_info.interp, 0, 0, &interp_info);
  251.     if (rc < 0) {
  252.         DPRINTF("Failed to load interpreter '%s.'\n",
  253.             prog_info.interp);
  254.         ipc_answer_0(rid, EINVAL);
  255.         return 1;
  256.     }
  257.  
  258.     printf("Run interpreter.\n");
  259.     printf("entry point: 0x%lx\n", interp_info.entry);
  260.     printf("pcb address: 0x%lx\n", &pcb);
  261.     printf("prog dynamic: 0x%lx\n", prog_info.dynamic);
  262.  
  263.     is_dyn_linked = true;
  264.     ipc_answer_0(rid, EOK);
  265.  
  266.     return 0;
  267. }
  268.  
  269.  
  270. /** Run the previously loaded program.
  271.  *
  272.  * @param rid
  273.  * @param request
  274.  * @return 0 on success, !0 on error.
  275.  */
  276. static void loader_run(ipc_callid_t rid, ipc_call_t *request)
  277. {
  278.     if (is_dyn_linked == true) {
  279.         /* Dynamically linked program */
  280.         DPRINTF("Run ELF interpreter.\n");
  281.         DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
  282.         close_console();
  283.  
  284.         ipc_answer_0(rid, EOK);
  285.         program_run(interp_info.entry, &pcb);
  286.  
  287.     } else {
  288.         /* Statically linked program */
  289.         close_console();
  290.         ipc_answer_0(rid, EOK);
  291.         program_run(prog_info.entry, &pcb);
  292.     }
  293.  
  294.     /* Not reached */
  295. }
  296.  
  297. /** Handle loader connection.
  298.  *
  299.  * Receive and carry out commands (of which the last one should be
  300.  * to execute the loaded program).
  301.  */
  302. static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
  303. {
  304.     ipc_callid_t callid;
  305.     ipc_call_t call;
  306.     int retval;
  307.  
  308.     /* Ignore parameters, the connection is already open */
  309.     (void)iid; (void)icall;
  310.  
  311.     while (1) {
  312.         callid = async_get_call(&call);
  313.  
  314.         switch (IPC_GET_METHOD(call)) {
  315.         case IPC_M_PHONE_HUNGUP:
  316.             exit(0);
  317.         case LOADER_GET_TASKID:
  318.             loader_get_taskid(callid, &call);
  319.             continue;
  320.         case LOADER_SET_PATHNAME:
  321.             loader_set_pathname(callid, &call);
  322.             continue;
  323.         case LOADER_SET_ARGS:
  324.             loader_set_args(callid, &call);
  325.             continue;
  326.         case LOADER_LOAD:
  327.             loader_load(callid, &call);
  328.             continue;
  329.         case LOADER_RUN:
  330.             loader_run(callid, &call);
  331.             /* Not reached */
  332.         default:
  333.             retval = ENOENT;
  334.             break;
  335.         }
  336.         if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
  337.             IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
  338.             DPRINTF("Responding EINVAL to method %d.\n",
  339.                 IPC_GET_METHOD(call));
  340.             ipc_answer_0(callid, EINVAL);
  341.         }
  342.     }
  343. }
  344.  
  345. /** Program loader main function.
  346.  */
  347. int main(int argc, char *argv[])
  348. {
  349.     ipc_callid_t callid;
  350.     ipc_call_t call;
  351.     ipcarg_t phone_hash;
  352.  
  353.     /* The first call only communicates the incoming phone hash */
  354.     callid = ipc_wait_for_call(&call);
  355.  
  356.     if (IPC_GET_METHOD(call) != LOADER_HELLO) {
  357.         if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
  358.             ipc_answer_0(callid, EINVAL);
  359.         return 1;
  360.     }
  361.  
  362.     ipc_answer_0(callid, EOK);
  363.     phone_hash = call.in_phone_hash;
  364.  
  365.     /*
  366.      * Up until now async must not be used as it couldn't
  367.      * handle incoming requests. (Which means e.g. printf()
  368.      * cannot be used)
  369.      */
  370.     async_new_connection(phone_hash, 0, NULL, loader_connection);
  371.     async_manager();
  372.  
  373.     /* not reached */
  374.     return 0;
  375. }
  376.  
  377. /** @}
  378.  */
  379.