Subversion Repositories HelenOS

Rev

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