2 #if defined(HAVE_CONFIG_H)
12 #include <sys/types.h>
16 #include <talloc/talloc.h>
19 const struct pb_system_apps pb_system_apps = {
21 .kexec = "/sbin/kexec",
22 .mount = "/bin/mount",
23 .sftp = "/usr/bin/sftp",
24 .tftp = "/usr/bin/tftp",
25 .umount = "/bin/umount",
26 .wget = "/usr/bin/wget",
29 int pb_mkdir_recursive(const char *dir)
38 if (!stat(dir, &statbuf)) {
39 if (!S_ISDIR(statbuf.st_mode)) {
40 pb_log("%s: %s exists, but isn't a directory\n",
47 str = talloc_strdup(NULL, dir);
48 sep = strchr(*str == '/' ? str + 1 : str, '/');
52 /* terminate the path at sep */
56 if (mkdir(str, mode) && errno != EEXIST) {
57 pb_log("mkdir(%s): %s\n", str, strerror(errno));
64 /* reset dir to the full path */
66 sep = strchr(sep + 1, '/');
74 int pb_rmdir_recursive(const char *base, const char *dir)
78 /* sanity check: make sure that dir is within base */
79 if (strncmp(base, dir, strlen(base)))
82 cur = talloc_strdup(NULL, dir);
84 while (strcmp(base, dir)) {
88 /* null-terminate at the last slash */
89 pos = strrchr(dir, '/');
102 * pb_run_cmd - Run the supplied command.
103 * @cmd_argv: An argument list array for execv.
106 int pb_run_cmd(const char *const *cmd_argv)
117 const char *const *p = cmd_argv;
119 pb_log("%s: ", __func__);
126 pb_log("%s: %s\n", __func__, cmd_argv[0]);
131 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
136 int log = fileno(pb_log_get_stream());
138 /* Redirect child output to log. */
140 status = dup2(log, STDOUT_FILENO);
141 assert(status != -1);
143 status = dup2(log, STDERR_FILENO);
144 assert(status != -1);
146 execvp(cmd_argv[0], (char *const *)cmd_argv);
147 pb_log("%s: exec failed: %s\n", __func__, strerror(errno));
151 if (waitpid(pid, &status, 0) == -1) {
152 pb_log("%s: waitpid failed: %s\n", __func__,
157 if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
158 pb_log("%s: signaled\n", __func__);
160 if (!WIFEXITED(status)) {
161 pb_log("%s: %s failed\n", __func__, cmd_argv[0]);
165 if (WEXITSTATUS(status))
166 pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status));
168 return WEXITSTATUS(status);