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 .shutdown = "/sbin/shutdown",
24 .sftp = "/usr/bin/sftp",
25 .tftp = "/usr/bin/tftp",
26 .umount = "/bin/umount",
27 .wget = "/usr/bin/wget",
30 int pb_mkdir_recursive(const char *dir)
39 if (!stat(dir, &statbuf)) {
40 if (!S_ISDIR(statbuf.st_mode)) {
41 pb_log("%s: %s exists, but isn't a directory\n",
48 str = talloc_strdup(NULL, dir);
49 sep = strchr(*str == '/' ? str + 1 : str, '/');
53 /* terminate the path at sep */
57 if (mkdir(str, mode) && errno != EEXIST) {
58 pb_log("mkdir(%s): %s\n", str, strerror(errno));
65 /* reset dir to the full path */
67 sep = strchr(sep + 1, '/');
75 int pb_rmdir_recursive(const char *base, const char *dir)
79 /* sanity check: make sure that dir is within base */
80 if (strncmp(base, dir, strlen(base)))
83 cur = talloc_strdup(NULL, dir);
85 while (strcmp(base, dir)) {
89 /* null-terminate at the last slash */
90 pos = strrchr(dir, '/');
103 * pb_run_cmd - Run the supplied command.
104 * @cmd_argv: An argument list array for execv.
107 int pb_run_cmd(const char *const *cmd_argv)
118 const char *const *p = cmd_argv;
120 pb_log("%s: ", __func__);
127 pb_log("%s: %s\n", __func__, cmd_argv[0]);
132 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
137 int log = fileno(pb_log_get_stream());
139 /* Redirect child output to log. */
141 status = dup2(log, STDOUT_FILENO);
142 assert(status != -1);
144 status = dup2(log, STDERR_FILENO);
145 assert(status != -1);
147 execvp(cmd_argv[0], (char *const *)cmd_argv);
148 pb_log("%s: exec failed: %s\n", __func__, strerror(errno));
152 if (waitpid(pid, &status, 0) == -1) {
153 pb_log("%s: waitpid failed: %s\n", __func__,
158 if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
159 pb_log("%s: signaled\n", __func__);
161 if (!WIFEXITED(status)) {
162 pb_log("%s: %s failed\n", __func__, cmd_argv[0]);
166 if (WEXITSTATUS(status))
167 pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status));
169 return WEXITSTATUS(status);