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 .udevadm = HOST_PROG_UDEVADM,
28 .umount = HOST_PROG_UMOUNT,
29 .wget = HOST_PROG_WGET,
32 int pb_mkdir_recursive(const char *dir)
41 if (!stat(dir, &statbuf)) {
42 if (!S_ISDIR(statbuf.st_mode)) {
43 pb_log("%s: %s exists, but isn't a directory\n",
50 str = talloc_strdup(NULL, dir);
51 sep = strchr(*str == '/' ? str + 1 : str, '/');
55 /* terminate the path at sep */
59 if (mkdir(str, mode) && errno != EEXIST) {
60 pb_log("mkdir(%s): %s\n", str, strerror(errno));
67 /* reset dir to the full path */
69 sep = strchr(sep + 1, '/');
77 int pb_rmdir_recursive(const char *base, const char *dir)
81 /* sanity check: make sure that dir is within base */
82 if (strncmp(base, dir, strlen(base)))
85 cur = talloc_strdup(NULL, dir);
87 while (strcmp(base, dir)) {
91 /* null-terminate at the last slash */
92 pos = strrchr(dir, '/');
105 * pb_run_cmd - Run the supplied command.
106 * @cmd_argv: An argument list array for execv.
107 * @wait: Wait for the child process to complete before returning.
108 * @dry_run: Don't actually fork and exec.
111 int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run)
122 const char *const *p = cmd_argv;
124 pb_log("%s: %s", __func__, (dry_run ? "(dry-run) " : ""));
132 pb_log("%s: %s%s\n", __func__, (dry_run ? "(dry-run) " : ""),
141 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
146 int log = fileno(pb_log_get_stream());
148 /* Redirect child output to log. */
150 status = dup2(log, STDOUT_FILENO);
151 assert(status != -1);
153 status = dup2(log, STDERR_FILENO);
154 assert(status != -1);
156 execvp(cmd_argv[0], (char *const *)cmd_argv);
157 pb_log("%s: exec failed: %s\n", __func__, strerror(errno));
161 if (!wait && !waitpid(pid, &status, WNOHANG))
164 if (waitpid(pid, &status, 0) == -1) {
165 pb_log("%s: waitpid failed: %s\n", __func__,
170 if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
171 pb_log("%s: signaled\n", __func__);
173 if (!WIFEXITED(status)) {
174 pb_log("%s: %s failed\n", __func__, cmd_argv[0]);
178 if (WEXITSTATUS(status))
179 pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status));
181 return WEXITSTATUS(status);