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.

141 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: xio-exec.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* this file contains the source for opening addresses of exec type */
  5. #include "xiosysincludes.h"
  6. #include "xioopen.h"
  7. #include "nestlex.h"
  8. #include "xio-progcall.h"
  9. #include "xio-exec.h"
  10. #if WITH_EXEC
  11. static int xioopen_exec(int argc, const char *argv[], struct opt *opts,
  12. int xioflags, /* XIO_RDONLY etc. */
  13. xiofile_t *fd,
  14. unsigned groups,
  15. int dummy1, int dummy2, int dummy3
  16. );
  17. const struct addrdesc addr_exec = { "exec", 3, xioopen_exec, GROUP_FD|GROUP_FORK|GROUP_EXEC|GROUP_SOCKET|GROUP_SOCK_UNIX|GROUP_TERMIOS|GROUP_FIFO|GROUP_PTY|GROUP_PARENT, 0, 0, 0 HELP(":<command-line>") };
  18. const struct optdesc opt_dash = { "dash", "login", OPT_DASH, GROUP_EXEC, PH_PREEXEC, TYPE_BOOL, OFUNC_SPEC };
  19. static int xioopen_exec(int argc, const char *argv[], struct opt *opts,
  20. int xioflags, /* XIO_RDONLY, XIO_MAYCHILD etc. */
  21. xiofile_t *fd,
  22. unsigned groups,
  23. int dummy1, int dummy2, int dummy3
  24. ) {
  25. int status;
  26. bool dash = false;
  27. int duptostderr;
  28. if (argc != 2) {
  29. Error3("\"%s:%s\": wrong number of parameters (%d instead of 1)", argv[0], argv[1], argc-1);
  30. }
  31. retropt_bool(opts, OPT_DASH, &dash);
  32. status = _xioopen_foxec(xioflags, &fd->stream, groups, &opts, &duptostderr);
  33. if (status < 0) return status;
  34. if (status == 0) { /* child */
  35. const char *ends[] = { " ", NULL };
  36. const char *hquotes[] = { "'", NULL };
  37. const char *squotes[] = { "\"", NULL };
  38. const char *nests[] = {
  39. "'", "'",
  40. "(", ")",
  41. "[", "]",
  42. "{", "}",
  43. NULL
  44. } ;
  45. char **pargv = NULL;
  46. int pargc;
  47. size_t len;
  48. const char *strp;
  49. char *token; /*! */
  50. char *tokp;
  51. char *path = NULL;
  52. char *tmp;
  53. int numleft;
  54. /*! Close(something) */
  55. /* parse command line */
  56. Debug1("child: args = \"%s\"", argv[1]);
  57. pargv = Malloc(8*sizeof(char *));
  58. if (pargv == NULL) return STAT_RETRYLATER;
  59. len = strlen(argv[1])+1;
  60. strp = argv[1];
  61. token = Malloc(len); /*! */
  62. tokp = token;
  63. if (nestlex(&strp, &tokp, &len, ends, hquotes, squotes, nests,
  64. true, true, false) < 0) {
  65. Error("internal: miscalculated string lengths");
  66. }
  67. *tokp++ = '\0';
  68. pargv[0] = strrchr(tokp-1, '/');
  69. if (pargv[0] == NULL) pargv[0] = token; else ++pargv[0];
  70. pargc = 1;
  71. while (*strp == ' ') {
  72. while (*++strp == ' ') ;
  73. if ((pargc & 0x07) == 0) {
  74. pargv = Realloc(pargv, (pargc+8)*sizeof(char *));
  75. if (pargv == NULL) return STAT_RETRYLATER;
  76. }
  77. pargv[pargc++] = tokp;
  78. if (nestlex(&strp, &tokp, &len, ends, hquotes, squotes, nests,
  79. true, true, false) < 0) {
  80. Error("internal: miscalculated string lengths");
  81. }
  82. *tokp++ = '\0';
  83. }
  84. pargv[pargc] = NULL;
  85. if ((tmp = Malloc(strlen(pargv[0])+2)) == NULL) {
  86. return STAT_RETRYLATER;
  87. }
  88. if (dash) {
  89. tmp[0] = '-';
  90. strcpy(tmp+1, pargv[0]);
  91. } else {
  92. strcpy(tmp, pargv[0]);
  93. }
  94. pargv[0] = tmp;
  95. if (setopt_path(opts, &path) < 0) {
  96. /* this could be dangerous, so let us abort this child... */
  97. Exit(1);
  98. }
  99. if ((numleft = leftopts(opts)) > 0) {
  100. Error1("%d option(s) could not be used", numleft);
  101. showleft(opts);
  102. return STAT_NORETRY;
  103. }
  104. /* only now redirect stderr */
  105. if (duptostderr >= 0) {
  106. diag_dup();
  107. Dup2(duptostderr, 2);
  108. }
  109. Notice1("execvp'ing \"%s\"", token);
  110. Execvp(token, pargv);
  111. /* here we come only if execvp() failed */
  112. switch (pargc) {
  113. case 1: Error3("execvp(\"%s\", \"%s\"): %s", token, pargv[0], strerror(errno)); break;
  114. case 2: Error4("execvp(\"%s\", \"%s\", \"%s\"): %s", token, pargv[0], pargv[1], strerror(errno)); break;
  115. case 3:
  116. default:
  117. Error5("execvp(\"%s\", \"%s\", \"%s\", \"%s\", ...): %s", token, pargv[0], pargv[1], pargv[2], strerror(errno)); break;
  118. }
  119. Exit(1); /* this child process */
  120. }
  121. /* parent */
  122. return 0;
  123. }
  124. #endif /* WITH_EXEC */