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 = "/sbin/kexec",
23 .mount = "/bin/mount",
24 .shutdown = "/sbin/shutdown",
25 .sftp = "/usr/bin/sftp",
26 .tftp = "/usr/bin/tftp",
27 .umount = "/bin/umount",
28 .wget = "/usr/bin/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.
108 int pb_run_cmd(const char *const *cmd_argv, int wait)
119 const char *const *p = cmd_argv;
121 pb_log("%s: ", __func__);
128 pb_log("%s: %s\n", __func__, cmd_argv[0]);
133 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
138 int log = fileno(pb_log_get_stream());
140 /* Redirect child output to log. */
142 status = dup2(log, STDOUT_FILENO);
143 assert(status != -1);
145 status = dup2(log, STDERR_FILENO);
146 assert(status != -1);
148 execvp(cmd_argv[0], (char *const *)cmd_argv);
149 pb_log("%s: exec failed: %s\n", __func__, strerror(errno));
153 if (!wait && !waitpid(pid, &status, WNOHANG))
156 if (waitpid(pid, &status, 0) == -1) {
157 pb_log("%s: waitpid failed: %s\n", __func__,
162 if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
163 pb_log("%s: signaled\n", __func__);
165 if (!WIFEXITED(status)) {
166 pb_log("%s: %s failed\n", __func__, cmd_argv[0]);
170 if (WEXITSTATUS(status))
171 pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status));
173 return WEXITSTATUS(status);