blob: 1ae833af1a4af4604a0cf600b4617d8d06e3ebaf [file] [log] [blame]
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06001#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"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060010#include "exec-cmd.h"
Josh Poimboeuf096d3552015-12-15 09:39:35 -060011#include "subcmd-config.h"
Ingo Molnarf37a2912009-07-01 12:37:06 +020012
Ingo Molnar07800602009-04-20 15:00:56 +020013#define MAX_ARGS 32
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060014#define PATH_MAX 4096
Ingo Molnar07800602009-04-20 15:00:56 +020015
Ingo Molnar07800602009-04-20 15:00:56 +020016static const char *argv_exec_path;
17static const char *argv0_path;
18
Josh Poimboeuf096d3552015-12-15 09:39:35 -060019void exec_cmd_init(const char *exec_name, const char *prefix,
20 const char *exec_path, const char *exec_path_env)
21{
22 subcmd_config.exec_name = exec_name;
23 subcmd_config.prefix = prefix;
24 subcmd_config.exec_path = exec_path;
25 subcmd_config.exec_path_env = exec_path_env;
26}
27
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060028#define is_dir_sep(c) ((c) == '/')
29
30static int is_absolute_path(const char *path)
31{
32 return path[0] == '/';
33}
34
35static const char *get_pwd_cwd(void)
36{
37 static char cwd[PATH_MAX + 1];
38 char *pwd;
39 struct stat cwd_stat, pwd_stat;
40 if (getcwd(cwd, PATH_MAX) == NULL)
41 return NULL;
42 pwd = getenv("PWD");
43 if (pwd && strcmp(pwd, cwd)) {
44 stat(cwd, &cwd_stat);
45 if (!stat(pwd, &pwd_stat) &&
46 pwd_stat.st_dev == cwd_stat.st_dev &&
47 pwd_stat.st_ino == cwd_stat.st_ino) {
48 strlcpy(cwd, pwd, PATH_MAX);
49 }
50 }
51 return cwd;
52}
53
54static const char *make_nonrelative_path(const char *path)
55{
56 static char buf[PATH_MAX + 1];
57
58 if (is_absolute_path(path)) {
59 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
60 die("Too long path: %.*s", 60, path);
61 } else {
62 const char *cwd = get_pwd_cwd();
63 if (!cwd)
64 die("Cannot determine the current working directory");
65 if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
66 die("Too long path: %.*s", 60, path);
67 }
68 return buf;
69}
70
Masami Hiramatsuc4068f52015-11-19 15:04:53 +090071char *system_path(const char *path)
Ingo Molnar07800602009-04-20 15:00:56 +020072{
Josh Poimboeuf901421a2015-12-15 09:39:36 -060073 char *buf = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +020074
75 if (is_absolute_path(path))
Masami Hiramatsuc4068f52015-11-19 15:04:53 +090076 return strdup(path);
Ingo Molnar07800602009-04-20 15:00:56 +020077
Josh Poimboeuf901421a2015-12-15 09:39:36 -060078 astrcatf(&buf, "%s/%s", subcmd_config.prefix, path);
79
80 return buf;
Ingo Molnar07800602009-04-20 15:00:56 +020081}
82
Josh Poimboeuf46113a52015-12-15 09:39:37 -060083const char *extract_argv0_path(const char *argv0)
Ingo Molnar07800602009-04-20 15:00:56 +020084{
85 const char *slash;
86
87 if (!argv0 || !*argv0)
88 return NULL;
89 slash = argv0 + strlen(argv0);
90
91 while (argv0 <= slash && !is_dir_sep(*slash))
92 slash--;
93
94 if (slash >= argv0) {
Arnaldo Carvalho de Melo151f85a2010-05-18 18:54:30 -030095 argv0_path = strndup(argv0, slash - argv0);
96 return argv0_path ? slash + 1 : NULL;
Ingo Molnar07800602009-04-20 15:00:56 +020097 }
98
99 return argv0;
100}
101
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600102void set_argv_exec_path(const char *exec_path)
Ingo Molnar07800602009-04-20 15:00:56 +0200103{
104 argv_exec_path = exec_path;
105 /*
106 * Propagate this setting to external programs.
107 */
Josh Poimboeuf096d3552015-12-15 09:39:35 -0600108 setenv(subcmd_config.exec_path_env, exec_path, 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200109}
110
111
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600112/* Returns the highest-priority location to look for subprograms. */
113char *get_argv_exec_path(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200114{
Masami Hiramatsuc4068f52015-11-19 15:04:53 +0900115 char *env;
Ingo Molnar07800602009-04-20 15:00:56 +0200116
117 if (argv_exec_path)
Masami Hiramatsuc4068f52015-11-19 15:04:53 +0900118 return strdup(argv_exec_path);
Ingo Molnar07800602009-04-20 15:00:56 +0200119
Josh Poimboeuf096d3552015-12-15 09:39:35 -0600120 env = getenv(subcmd_config.exec_path_env);
Masami Hiramatsuc4068f52015-11-19 15:04:53 +0900121 if (env && *env)
122 return strdup(env);
Ingo Molnar07800602009-04-20 15:00:56 +0200123
Josh Poimboeuf096d3552015-12-15 09:39:35 -0600124 return system_path(subcmd_config.exec_path);
Ingo Molnar07800602009-04-20 15:00:56 +0200125}
126
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600127static void add_path(char **out, const char *path)
Ingo Molnar07800602009-04-20 15:00:56 +0200128{
129 if (path && *path) {
130 if (is_absolute_path(path))
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600131 astrcat(out, path);
Ingo Molnar07800602009-04-20 15:00:56 +0200132 else
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600133 astrcat(out, make_nonrelative_path(path));
Ingo Molnar07800602009-04-20 15:00:56 +0200134
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600135 astrcat(out, ":");
Ingo Molnar07800602009-04-20 15:00:56 +0200136 }
137}
138
139void setup_path(void)
140{
141 const char *old_path = getenv("PATH");
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600142 char *new_path = NULL;
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600143 char *tmp = get_argv_exec_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200144
Masami Hiramatsuc4068f52015-11-19 15:04:53 +0900145 add_path(&new_path, tmp);
Ingo Molnar07800602009-04-20 15:00:56 +0200146 add_path(&new_path, argv0_path);
Masami Hiramatsuc4068f52015-11-19 15:04:53 +0900147 free(tmp);
Ingo Molnar07800602009-04-20 15:00:56 +0200148
149 if (old_path)
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600150 astrcat(&new_path, old_path);
Ingo Molnar07800602009-04-20 15:00:56 +0200151 else
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600152 astrcat(&new_path, "/usr/local/bin:/usr/bin:/bin");
Ingo Molnar07800602009-04-20 15:00:56 +0200153
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600154 setenv("PATH", new_path, 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200155
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600156 free(new_path);
Ingo Molnar07800602009-04-20 15:00:56 +0200157}
158
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600159static const char **prepare_exec_cmd(const char **argv)
Ingo Molnar07800602009-04-20 15:00:56 +0200160{
161 int argc;
162 const char **nargv;
163
164 for (argc = 0; argv[argc]; argc++)
165 ; /* just counting */
166 nargv = malloc(sizeof(*nargv) * (argc + 2));
167
Josh Poimboeuf096d3552015-12-15 09:39:35 -0600168 nargv[0] = subcmd_config.exec_name;
Ingo Molnar07800602009-04-20 15:00:56 +0200169 for (argc = 0; argv[argc]; argc++)
170 nargv[argc + 1] = argv[argc];
171 nargv[argc + 1] = NULL;
172 return nargv;
173}
174
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600175int execv_cmd(const char **argv) {
176 const char **nargv = prepare_exec_cmd(argv);
Ingo Molnar07800602009-04-20 15:00:56 +0200177
178 /* execvp() can only ever return if it fails */
Josh Poimboeuf096d3552015-12-15 09:39:35 -0600179 execvp(subcmd_config.exec_name, (char **)nargv);
Ingo Molnar07800602009-04-20 15:00:56 +0200180
181 free(nargv);
182 return -1;
183}
184
185
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600186int execl_cmd(const char *cmd,...)
Ingo Molnar07800602009-04-20 15:00:56 +0200187{
188 int argc;
189 const char *argv[MAX_ARGS + 1];
190 const char *arg;
191 va_list param;
192
193 va_start(param, cmd);
194 argv[0] = cmd;
195 argc = 1;
196 while (argc < MAX_ARGS) {
197 arg = argv[argc++] = va_arg(param, char *);
198 if (!arg)
199 break;
200 }
201 va_end(param);
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600202 if (MAX_ARGS <= argc) {
203 fprintf(stderr, " Error: too many args to run %s\n", cmd);
204 return -1;
205 }
Ingo Molnar07800602009-04-20 15:00:56 +0200206
207 argv[argc] = NULL;
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600208 return execv_cmd(argv);
Ingo Molnar07800602009-04-20 15:00:56 +0200209}