exec-cmd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <linux/compiler.h>
  2. #include <linux/string.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "subcmd-util.h"
  10. #include "exec-cmd.h"
  11. #include "subcmd-config.h"
  12. #define MAX_ARGS 32
  13. #define PATH_MAX 4096
  14. static const char *argv_exec_path;
  15. static const char *argv0_path;
  16. void exec_cmd_init(const char *exec_name, const char *prefix,
  17. const char *exec_path, const char *exec_path_env)
  18. {
  19. subcmd_config.exec_name = exec_name;
  20. subcmd_config.prefix = prefix;
  21. subcmd_config.exec_path = exec_path;
  22. subcmd_config.exec_path_env = exec_path_env;
  23. }
  24. #define is_dir_sep(c) ((c) == '/')
  25. static int is_absolute_path(const char *path)
  26. {
  27. return path[0] == '/';
  28. }
  29. static const char *get_pwd_cwd(void)
  30. {
  31. static char cwd[PATH_MAX + 1];
  32. char *pwd;
  33. struct stat cwd_stat, pwd_stat;
  34. if (getcwd(cwd, PATH_MAX) == NULL)
  35. return NULL;
  36. pwd = getenv("PWD");
  37. if (pwd && strcmp(pwd, cwd)) {
  38. stat(cwd, &cwd_stat);
  39. if (!stat(pwd, &pwd_stat) &&
  40. pwd_stat.st_dev == cwd_stat.st_dev &&
  41. pwd_stat.st_ino == cwd_stat.st_ino) {
  42. strlcpy(cwd, pwd, PATH_MAX);
  43. }
  44. }
  45. return cwd;
  46. }
  47. static const char *make_nonrelative_path(const char *path)
  48. {
  49. static char buf[PATH_MAX + 1];
  50. if (is_absolute_path(path)) {
  51. if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
  52. die("Too long path: %.*s", 60, path);
  53. } else {
  54. const char *cwd = get_pwd_cwd();
  55. if (!cwd)
  56. die("Cannot determine the current working directory");
  57. if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
  58. die("Too long path: %.*s", 60, path);
  59. }
  60. return buf;
  61. }
  62. char *system_path(const char *path)
  63. {
  64. char *buf = NULL;
  65. if (is_absolute_path(path))
  66. return strdup(path);
  67. astrcatf(&buf, "%s/%s", subcmd_config.prefix, path);
  68. return buf;
  69. }
  70. const char *extract_argv0_path(const char *argv0)
  71. {
  72. const char *slash;
  73. if (!argv0 || !*argv0)
  74. return NULL;
  75. slash = argv0 + strlen(argv0);
  76. while (argv0 <= slash && !is_dir_sep(*slash))
  77. slash--;
  78. if (slash >= argv0) {
  79. argv0_path = strndup(argv0, slash - argv0);
  80. return argv0_path ? slash + 1 : NULL;
  81. }
  82. return argv0;
  83. }
  84. void set_argv_exec_path(const char *exec_path)
  85. {
  86. argv_exec_path = exec_path;
  87. /*
  88. * Propagate this setting to external programs.
  89. */
  90. setenv(subcmd_config.exec_path_env, exec_path, 1);
  91. }
  92. /* Returns the highest-priority location to look for subprograms. */
  93. char *get_argv_exec_path(void)
  94. {
  95. char *env;
  96. if (argv_exec_path)
  97. return strdup(argv_exec_path);
  98. env = getenv(subcmd_config.exec_path_env);
  99. if (env && *env)
  100. return strdup(env);
  101. return system_path(subcmd_config.exec_path);
  102. }
  103. static void add_path(char **out, const char *path)
  104. {
  105. if (path && *path) {
  106. if (is_absolute_path(path))
  107. astrcat(out, path);
  108. else
  109. astrcat(out, make_nonrelative_path(path));
  110. astrcat(out, ":");
  111. }
  112. }
  113. void setup_path(void)
  114. {
  115. const char *old_path = getenv("PATH");
  116. char *new_path = NULL;
  117. char *tmp = get_argv_exec_path();
  118. add_path(&new_path, tmp);
  119. add_path(&new_path, argv0_path);
  120. free(tmp);
  121. if (old_path)
  122. astrcat(&new_path, old_path);
  123. else
  124. astrcat(&new_path, "/usr/local/bin:/usr/bin:/bin");
  125. setenv("PATH", new_path, 1);
  126. free(new_path);
  127. }
  128. static const char **prepare_exec_cmd(const char **argv)
  129. {
  130. int argc;
  131. const char **nargv;
  132. for (argc = 0; argv[argc]; argc++)
  133. ; /* just counting */
  134. nargv = malloc(sizeof(*nargv) * (argc + 2));
  135. nargv[0] = subcmd_config.exec_name;
  136. for (argc = 0; argv[argc]; argc++)
  137. nargv[argc + 1] = argv[argc];
  138. nargv[argc + 1] = NULL;
  139. return nargv;
  140. }
  141. int execv_cmd(const char **argv) {
  142. const char **nargv = prepare_exec_cmd(argv);
  143. /* execvp() can only ever return if it fails */
  144. execvp(subcmd_config.exec_name, (char **)nargv);
  145. free(nargv);
  146. return -1;
  147. }
  148. int execl_cmd(const char *cmd,...)
  149. {
  150. int argc;
  151. const char *argv[MAX_ARGS + 1];
  152. const char *arg;
  153. va_list param;
  154. va_start(param, cmd);
  155. argv[0] = cmd;
  156. argc = 1;
  157. while (argc < MAX_ARGS) {
  158. arg = argv[argc++] = va_arg(param, char *);
  159. if (!arg)
  160. break;
  161. }
  162. va_end(param);
  163. if (MAX_ARGS <= argc) {
  164. fprintf(stderr, " Error: too many args to run %s\n", cmd);
  165. return -1;
  166. }
  167. argv[argc] = NULL;
  168. return execv_cmd(argv);
  169. }