PageRenderTime 1373ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/shell/shell-exec.c

https://bitbucket.org/sieben/contiki
C | 138 lines | 83 code | 13 blank | 42 comment | 12 complexity | e419a40987420482ac6e383433084aec MD5 | raw file
  1. /*
  2. * Copyright (c) 2008, Swedish Institute of Computer Science.
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the Contiki operating system.
  30. *
  31. */
  32. /**
  33. * \file
  34. * A brief description of what this file is.
  35. * \author
  36. * Adam Dunkels <adam@sics.se>
  37. */
  38. #include "contiki.h"
  39. #include "shell-exec.h"
  40. #include "loader/elfloader.h"
  41. #include <stdio.h>
  42. #include <string.h>
  43. /*---------------------------------------------------------------------------*/
  44. PROCESS(shell_exec_process, "exec");
  45. SHELL_COMMAND(exec_command,
  46. "exec",
  47. "exec <filename>: load and execute the ELF file filename",
  48. &shell_exec_process);
  49. /*---------------------------------------------------------------------------*/
  50. PROCESS_THREAD(shell_exec_process, ev, data)
  51. {
  52. char *name;
  53. int fd;
  54. PROCESS_BEGIN();
  55. name = data;
  56. if(name == NULL || strlen(name) == 0) {
  57. shell_output_str(&exec_command,
  58. "exec <file>: filename must be given", "");
  59. PROCESS_EXIT();
  60. }
  61. /* Kill any old processes. */
  62. if(elfloader_autostart_processes != NULL) {
  63. autostart_exit(elfloader_autostart_processes);
  64. }
  65. fd = cfs_open(name, CFS_READ | CFS_WRITE);
  66. if(fd < 0) {
  67. shell_output_str(&exec_command,
  68. "exec: could not open ", name);
  69. } else {
  70. int ret;
  71. char *print, *symbol;
  72. ret = elfloader_load(fd);
  73. cfs_close(fd);
  74. symbol = "";
  75. switch(ret) {
  76. case ELFLOADER_OK:
  77. print = "OK";
  78. break;
  79. case ELFLOADER_BAD_ELF_HEADER:
  80. print = "Bad ELF header";
  81. break;
  82. case ELFLOADER_NO_SYMTAB:
  83. print = "No symbol table";
  84. break;
  85. case ELFLOADER_NO_STRTAB:
  86. print = "No string table";
  87. break;
  88. case ELFLOADER_NO_TEXT:
  89. print = "No text segment";
  90. break;
  91. case ELFLOADER_SYMBOL_NOT_FOUND:
  92. print = "Symbol not found: ";
  93. symbol = elfloader_unknown;
  94. break;
  95. case ELFLOADER_SEGMENT_NOT_FOUND:
  96. print = "Segment not found: ";
  97. symbol = elfloader_unknown;
  98. break;
  99. case ELFLOADER_NO_STARTPOINT:
  100. print = "No starting point";
  101. break;
  102. default:
  103. print = "Unknown return code from the ELF loader (internal bug)";
  104. break;
  105. }
  106. shell_output_str(&exec_command, print, symbol);
  107. if(ret == ELFLOADER_OK) {
  108. int i;
  109. for(i = 0; elfloader_autostart_processes[i] != NULL; ++i) {
  110. shell_output_str(&exec_command, "exec: starting process ",
  111. elfloader_autostart_processes[i]->name);
  112. }
  113. autostart_start(elfloader_autostart_processes);
  114. }
  115. }
  116. PROCESS_END();
  117. }
  118. /*---------------------------------------------------------------------------*/
  119. void
  120. shell_exec_init(void)
  121. {
  122. elfloader_init();
  123. shell_register_command(&exec_command);
  124. }
  125. /*---------------------------------------------------------------------------*/