You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exec.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <unistd.h>
  26. #include <getopt.h>
  27. #include <errno.h>
  28. #include <assert.h>
  29. #include <ipxe/tables.h>
  30. #include <ipxe/command.h>
  31. #include <ipxe/parseopt.h>
  32. #include <ipxe/settings.h>
  33. #include <ipxe/console.h>
  34. #include <ipxe/keys.h>
  35. #include <ipxe/process.h>
  36. #include <ipxe/nap.h>
  37. #include <ipxe/shell.h>
  38. /** @file
  39. *
  40. * Command execution
  41. *
  42. */
  43. /** Shell stop state */
  44. static int stop_state;
  45. /**
  46. * Execute command
  47. *
  48. * @v command Command name
  49. * @v argv Argument list
  50. * @ret rc Return status code
  51. *
  52. * Execute the named command. Unlike a traditional POSIX execv(),
  53. * this function returns the exit status of the command.
  54. */
  55. int execv ( const char *command, char * const argv[] ) {
  56. struct command *cmd;
  57. int argc;
  58. int rc;
  59. /* Count number of arguments */
  60. for ( argc = 0 ; argv[argc] ; argc++ ) {}
  61. /* An empty command is deemed to do nothing, successfully */
  62. if ( command == NULL ) {
  63. rc = 0;
  64. goto done;
  65. }
  66. /* Sanity checks */
  67. if ( argc == 0 ) {
  68. DBG ( "%s: empty argument list\n", command );
  69. rc = -EINVAL;
  70. goto done;
  71. }
  72. /* Reset getopt() library ready for use by the command. This
  73. * is an artefact of the POSIX getopt() API within the context
  74. * of Etherboot; see the documentation for reset_getopt() for
  75. * details.
  76. */
  77. reset_getopt();
  78. /* Hand off to command implementation */
  79. for_each_table_entry ( cmd, COMMANDS ) {
  80. if ( strcmp ( command, cmd->name ) == 0 ) {
  81. rc = cmd->exec ( argc, ( char ** ) argv );
  82. goto done;
  83. }
  84. }
  85. printf ( "%s: command not found\n", command );
  86. rc = -ENOEXEC;
  87. done:
  88. /* Store error number, if an error occurred */
  89. if ( rc ) {
  90. errno = rc;
  91. if ( errno < 0 )
  92. errno = -errno;
  93. }
  94. return rc;
  95. }
  96. /**
  97. * Split command line into tokens
  98. *
  99. * @v command Command line
  100. * @v tokens Token list to populate, or NULL
  101. * @ret count Number of tokens
  102. *
  103. * Splits the command line into whitespace-delimited tokens. If @c
  104. * tokens is non-NULL, any whitespace in the command line will be
  105. * replaced with NULs.
  106. */
  107. static int split_command ( char *command, char **tokens ) {
  108. int count = 0;
  109. while ( 1 ) {
  110. /* Skip over any whitespace / convert to NUL */
  111. while ( isspace ( *command ) ) {
  112. if ( tokens )
  113. *command = '\0';
  114. command++;
  115. }
  116. /* Check for end of line */
  117. if ( ! *command )
  118. break;
  119. /* We have found the start of the next argument */
  120. if ( tokens )
  121. tokens[count] = command;
  122. count++;
  123. /* Skip to start of next whitespace, if any */
  124. while ( *command && ! isspace ( *command ) ) {
  125. command++;
  126. }
  127. }
  128. return count;
  129. }
  130. /**
  131. * Process next command only if previous command succeeded
  132. *
  133. * @v rc Status of previous command
  134. * @ret process Process next command
  135. */
  136. static int process_on_success ( int rc ) {
  137. return ( rc == 0 );
  138. }
  139. /**
  140. * Process next command only if previous command failed
  141. *
  142. * @v rc Status of previous command
  143. * @ret process Process next command
  144. */
  145. static int process_on_failure ( int rc ) {
  146. return ( rc != 0 );
  147. }
  148. /**
  149. * Process next command regardless of status from previous command
  150. *
  151. * @v rc Status of previous command
  152. * @ret process Process next command
  153. */
  154. static int process_always ( int rc __unused ) {
  155. return 1;
  156. }
  157. /**
  158. * Find command terminator
  159. *
  160. * @v tokens Token list
  161. * @ret process_next "Should next command be processed?" function
  162. * @ret argc Argument count
  163. */
  164. static int command_terminator ( char **tokens,
  165. int ( **process_next ) ( int rc ) ) {
  166. unsigned int i;
  167. /* Find first terminating token */
  168. for ( i = 0 ; tokens[i] ; i++ ) {
  169. if ( tokens[i][0] == '#' ) {
  170. /* Start of a comment */
  171. break;
  172. } else if ( strcmp ( tokens[i], "||" ) == 0 ) {
  173. /* Short-circuit logical OR */
  174. *process_next = process_on_failure;
  175. return i;
  176. } else if ( strcmp ( tokens[i], "&&" ) == 0 ) {
  177. /* Short-circuit logical AND */
  178. *process_next = process_on_success;
  179. return i;
  180. } else if ( strcmp ( tokens[i], ";" ) == 0 ) {
  181. /* Process next command unconditionally */
  182. *process_next = process_always;
  183. return i;
  184. }
  185. }
  186. /* End of token list */
  187. *process_next = NULL;
  188. return i;
  189. }
  190. /**
  191. * Set shell stop state
  192. *
  193. * @v stop Shell stop state
  194. */
  195. void shell_stop ( int stop ) {
  196. stop_state = stop;
  197. }
  198. /**
  199. * Test and consume shell stop state
  200. *
  201. * @v stop Shell stop state to consume
  202. * @v stopped Shell had been stopped
  203. */
  204. int shell_stopped ( int stop ) {
  205. int stopped;
  206. /* Test to see if we need to stop */
  207. stopped = ( stop_state >= stop );
  208. /* Consume stop state */
  209. if ( stop_state <= stop )
  210. stop_state = 0;
  211. return stopped;
  212. }
  213. /**
  214. * Expand settings within a token list
  215. *
  216. * @v argc Argument count
  217. * @v tokens Token list
  218. * @v argv Argument list to fill in
  219. * @ret rc Return status code
  220. */
  221. static int expand_tokens ( int argc, char **tokens, char **argv ) {
  222. int i;
  223. /* Expand each token in turn */
  224. for ( i = 0 ; i < argc ; i++ ) {
  225. argv[i] = expand_settings ( tokens[i] );
  226. if ( ! argv[i] )
  227. goto err_expand_settings;
  228. }
  229. return 0;
  230. err_expand_settings:
  231. assert ( argv[i] == NULL );
  232. for ( ; i >= 0 ; i-- )
  233. free ( argv[i] );
  234. return -ENOMEM;
  235. }
  236. /**
  237. * Free an expanded token list
  238. *
  239. * @v argv Argument list
  240. */
  241. static void free_tokens ( char **argv ) {
  242. /* Free each expanded argument */
  243. while ( *argv )
  244. free ( *(argv++) );
  245. }
  246. /**
  247. * Execute command line
  248. *
  249. * @v command Command line
  250. * @ret rc Return status code
  251. *
  252. * Execute the named command and arguments.
  253. */
  254. int system ( const char *command ) {
  255. int count = split_command ( ( char * ) command, NULL );
  256. char *all_tokens[ count + 1 ];
  257. int ( * process_next ) ( int rc );
  258. char *command_copy;
  259. char **tokens;
  260. int argc;
  261. int process;
  262. int rc = 0;
  263. /* Create modifiable copy of command */
  264. command_copy = strdup ( command );
  265. if ( ! command_copy )
  266. return -ENOMEM;
  267. /* Split command into tokens */
  268. split_command ( command_copy, all_tokens );
  269. all_tokens[count] = NULL;
  270. /* Process individual commands */
  271. process = 1;
  272. for ( tokens = all_tokens ; ; tokens += ( argc + 1 ) ) {
  273. /* Find command terminator */
  274. argc = command_terminator ( tokens, &process_next );
  275. /* Expand tokens and execute command */
  276. if ( process ) {
  277. char *argv[ argc + 1 ];
  278. /* Expand tokens */
  279. if ( ( rc = expand_tokens ( argc, tokens, argv ) ) != 0)
  280. break;
  281. argv[argc] = NULL;
  282. /* Execute command */
  283. rc = execv ( argv[0], argv );
  284. /* Free tokens */
  285. free_tokens ( argv );
  286. }
  287. /* Stop processing, if applicable */
  288. if ( shell_stopped ( SHELL_STOP_COMMAND ) )
  289. break;
  290. /* Stop processing if we have reached the end of the
  291. * command.
  292. */
  293. if ( ! process_next )
  294. break;
  295. /* Determine whether or not to process next command */
  296. process = process_next ( rc );
  297. }
  298. /* Free modified copy of command */
  299. free ( command_copy );
  300. return rc;
  301. }
  302. /**
  303. * Concatenate arguments
  304. *
  305. * @v args Argument list (NULL-terminated)
  306. * @ret string Concatenated arguments
  307. *
  308. * The returned string is allocated with malloc(). The caller is
  309. * responsible for eventually free()ing this string.
  310. */
  311. char * concat_args ( char **args ) {
  312. char **arg;
  313. size_t len;
  314. char *string;
  315. char *ptr;
  316. /* Calculate total string length */
  317. len = 1 /* NUL */;
  318. for ( arg = args ; *arg ; arg++ )
  319. len += ( 1 /* possible space */ + strlen ( *arg ) );
  320. /* Allocate string */
  321. string = zalloc ( len );
  322. if ( ! string )
  323. return NULL;
  324. /* Populate string */
  325. ptr = string;
  326. for ( arg = args ; *arg ; arg++ ) {
  327. ptr += sprintf ( ptr, "%s%s",
  328. ( ( arg == args ) ? "" : " " ), *arg );
  329. }
  330. assert ( ptr < ( string + len ) );
  331. return string;
  332. }
  333. /** "echo" options */
  334. struct echo_options {
  335. /** Do not print trailing newline */
  336. int no_newline;
  337. };
  338. /** "echo" option list */
  339. static struct option_descriptor echo_opts[] = {
  340. OPTION_DESC ( "n", 'n', no_argument,
  341. struct echo_options, no_newline, parse_flag ),
  342. };
  343. /** "echo" command descriptor */
  344. static struct command_descriptor echo_cmd =
  345. COMMAND_DESC ( struct echo_options, echo_opts, 0, MAX_ARGUMENTS,
  346. "[-n] [...]" );
  347. /**
  348. * "echo" command
  349. *
  350. * @v argc Argument count
  351. * @v argv Argument list
  352. * @ret rc Return status code
  353. */
  354. static int echo_exec ( int argc, char **argv ) {
  355. struct echo_options opts;
  356. char *text;
  357. int rc;
  358. /* Parse options */
  359. if ( ( rc = parse_options ( argc, argv, &echo_cmd, &opts ) ) != 0 )
  360. return rc;
  361. /* Parse text */
  362. text = concat_args ( &argv[optind] );
  363. if ( ! text )
  364. return -ENOMEM;
  365. /* Print text */
  366. printf ( "%s%s", text, ( opts.no_newline ? "" : "\n" ) );
  367. free ( text );
  368. return 0;
  369. }
  370. /** "echo" command */
  371. struct command echo_command __command = {
  372. .name = "echo",
  373. .exec = echo_exec,
  374. };
  375. /** "exit" options */
  376. struct exit_options {};
  377. /** "exit" option list */
  378. static struct option_descriptor exit_opts[] = {};
  379. /** "exit" command descriptor */
  380. static struct command_descriptor exit_cmd =
  381. COMMAND_DESC ( struct exit_options, exit_opts, 0, 1, "[<status>]" );
  382. /**
  383. * "exit" command
  384. *
  385. * @v argc Argument count
  386. * @v argv Argument list
  387. * @ret rc Return status code
  388. */
  389. static int exit_exec ( int argc, char **argv ) {
  390. struct exit_options opts;
  391. unsigned int exit_code = 0;
  392. int rc;
  393. /* Parse options */
  394. if ( ( rc = parse_options ( argc, argv, &exit_cmd, &opts ) ) != 0 )
  395. return rc;
  396. /* Parse exit status, if present */
  397. if ( optind != argc ) {
  398. if ( ( rc = parse_integer ( argv[optind], &exit_code ) ) != 0 )
  399. return rc;
  400. }
  401. /* Stop shell processing */
  402. shell_stop ( SHELL_STOP_COMMAND_SEQUENCE );
  403. return exit_code;
  404. }
  405. /** "exit" command */
  406. struct command exit_command __command = {
  407. .name = "exit",
  408. .exec = exit_exec,
  409. };
  410. /** "isset" options */
  411. struct isset_options {};
  412. /** "isset" option list */
  413. static struct option_descriptor isset_opts[] = {};
  414. /** "isset" command descriptor */
  415. static struct command_descriptor isset_cmd =
  416. COMMAND_DESC ( struct isset_options, isset_opts, 1, 1, "<value>" );
  417. /**
  418. * "isset" command
  419. *
  420. * @v argc Argument count
  421. * @v argv Argument list
  422. * @ret rc Return status code
  423. */
  424. static int isset_exec ( int argc, char **argv ) {
  425. struct isset_options opts;
  426. int rc;
  427. /* Parse options */
  428. if ( ( rc = parse_options ( argc, argv, &isset_cmd, &opts ) ) != 0 )
  429. return rc;
  430. /* Return success iff argument is non-empty */
  431. return ( argv[optind][0] ? 0 : -ENOENT );
  432. }
  433. /** "isset" command */
  434. struct command isset_command __command = {
  435. .name = "isset",
  436. .exec = isset_exec,
  437. };
  438. /** "iseq" options */
  439. struct iseq_options {};
  440. /** "iseq" option list */
  441. static struct option_descriptor iseq_opts[] = {};
  442. /** "iseq" command descriptor */
  443. static struct command_descriptor iseq_cmd =
  444. COMMAND_DESC ( struct iseq_options, iseq_opts, 2, 2,
  445. "<value1> <value2>" );
  446. /**
  447. * "iseq" command
  448. *
  449. * @v argc Argument count
  450. * @v argv Argument list
  451. * @ret rc Return status code
  452. */
  453. static int iseq_exec ( int argc, char **argv ) {
  454. struct iseq_options opts;
  455. int rc;
  456. /* Parse options */
  457. if ( ( rc = parse_options ( argc, argv, &iseq_cmd, &opts ) ) != 0 )
  458. return rc;
  459. /* Return success iff arguments are equal */
  460. return ( ( strcmp ( argv[optind], argv[ optind + 1 ] ) == 0 ) ?
  461. 0 : -ERANGE );
  462. }
  463. /** "iseq" command */
  464. struct command iseq_command __command = {
  465. .name = "iseq",
  466. .exec = iseq_exec,
  467. };
  468. /** "sleep" options */
  469. struct sleep_options {};
  470. /** "sleep" option list */
  471. static struct option_descriptor sleep_opts[] = {};
  472. /** "sleep" command descriptor */
  473. static struct command_descriptor sleep_cmd =
  474. COMMAND_DESC ( struct sleep_options, sleep_opts, 1, 1, "<seconds>" );
  475. /**
  476. * "sleep" command
  477. *
  478. * @v argc Argument count
  479. * @v argv Argument list
  480. * @ret rc Return status code
  481. */
  482. static int sleep_exec ( int argc, char **argv ) {
  483. struct sleep_options opts;
  484. unsigned int seconds;
  485. unsigned long start;
  486. unsigned long delay;
  487. int rc;
  488. /* Parse options */
  489. if ( ( rc = parse_options ( argc, argv, &sleep_cmd, &opts ) ) != 0 )
  490. return rc;
  491. /* Parse number of seconds */
  492. if ( ( rc = parse_integer ( argv[optind], &seconds ) ) != 0 )
  493. return rc;
  494. /* Delay for specified number of seconds */
  495. start = currticks();
  496. delay = ( seconds * TICKS_PER_SEC );
  497. while ( ( currticks() - start ) <= delay ) {
  498. step();
  499. if ( iskey() && ( getchar() == CTRL_C ) )
  500. return -ECANCELED;
  501. cpu_nap();
  502. }
  503. return 0;
  504. }
  505. /** "sleep" command */
  506. struct command sleep_command __command = {
  507. .name = "sleep",
  508. .exec = sleep_exec,
  509. };