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 = {
22 .kexec = HOST_PROG_KEXEC,
23 .mount = HOST_PROG_MOUNT,
24 .shutdown = HOST_PROG_SHUTDOWN,
25 .sftp = HOST_PROG_SFTP,
26 .tftp = HOST_PROG_TFTP,
27 .umount = HOST_PROG_UMOUNT,
28 .wget = HOST_PROG_WGET,
31 int pb_mkdir_recursive(const char *dir)
40 if (!stat(dir, &statbuf)) {
41 if (!S_ISDIR(statbuf.st_mode)) {
42 pb_log("%s: %s exists, but isn't a directory\n",
49 str = talloc_strdup(NULL, dir);
50 sep = strchr(*str == '/' ? str + 1 : str, '/');
54 /* terminate the path at sep */
58 if (mkdir(str, mode) && errno != EEXIST) {
59 pb_log("mkdir(%s): %s\n", str, strerror(errno));
66 /* reset dir to the full path */
68 sep = strchr(sep + 1, '/');
76 int pb_rmdir_recursive(const char *base, const char *dir)
80 /* sanity check: make sure that dir is within base */
81 if (strncmp(base, dir, strlen(base)))
84 cur = talloc_strdup(NULL, dir);
86 while (strcmp(base, dir)) {
90 /* null-terminate at the last slash */
91 pos = strrchr(dir, '/');
104 * pb_run_cmd - Run the supplied command.
105 * @cmd_argv: An argument list array for execv.
106 * @wait: Wait for the child process to complete before returning.
107 * @dry_run: Don't actually fork and exec.
110 int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run)
121 const char *const *p = cmd_argv;
123 pb_log("%s: %s", __func__, (dry_run ? "(dry-run) " : ""));
131 pb_log("%s: %s%s\n", __func__, (dry_run ? "(dry-run) " : ""),
140 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
145 int log = fileno(pb_log_get_stream());
147 /* Redirect child output to log. */
149 status = dup2(log, STDOUT_FILENO);
150 assert(status != -1);
152 status = dup2(log, STDERR_FILENO);
153 assert(status != -1);
155 execvp(cmd_argv[0], (char *const *)cmd_argv);
156 pb_log("%s: exec failed: %s\n", __func__, strerror(errno));
160 if (!wait && !waitpid(pid, &status, WNOHANG))
163 if (waitpid(pid, &status, 0) == -1) {
164 pb_log("%s: waitpid failed: %s\n", __func__,
169 if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
170 pb_log("%s: signaled\n", __func__);
172 if (!WIFEXITED(status)) {
173 pb_log("%s: %s failed\n", __func__, cmd_argv[0]);
177 if (WEXITSTATUS(status))
178 pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status));
180 return WEXITSTATUS(status);