]> git.ozlabs.org Git - petitboot/blob - lib/system/system.c
test/parser: cleanup parser allocations on exit
[petitboot] / lib / system / system.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <assert.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14
15 #include "log/log.h"
16 #include <talloc/talloc.h>
17 #include "system.h"
18
19 const struct pb_system_apps pb_system_apps = {
20         .prefix         = PREFIX,
21         .cp             = HOST_PROG_CP,
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,
29 };
30
31 int pb_mkdir_recursive(const char *dir)
32 {
33         struct stat statbuf;
34         char *str, *sep;
35         int mode = 0755;
36
37         if (!*dir)
38                 return 0;
39
40         if (!stat(dir, &statbuf)) {
41                 if (!S_ISDIR(statbuf.st_mode)) {
42                         pb_log("%s: %s exists, but isn't a directory\n",
43                                         __func__, dir);
44                         return -1;
45                 }
46                 return 0;
47         }
48
49         str = talloc_strdup(NULL, dir);
50         sep = strchr(*str == '/' ? str + 1 : str, '/');
51
52         while (1) {
53
54                 /* terminate the path at sep */
55                 if (sep)
56                         *sep = '\0';
57
58                 if (mkdir(str, mode) && errno != EEXIST) {
59                         pb_log("mkdir(%s): %s\n", str, strerror(errno));
60                         return -1;
61                 }
62
63                 if (!sep)
64                         break;
65
66                 /* reset dir to the full path */
67                 strcpy(str, dir);
68                 sep = strchr(sep + 1, '/');
69         }
70
71         talloc_free(str);
72
73         return 0;
74 }
75
76 int pb_rmdir_recursive(const char *base, const char *dir)
77 {
78         char *cur, *pos;
79
80         /* sanity check: make sure that dir is within base */
81         if (strncmp(base, dir, strlen(base)))
82                 return -1;
83
84         cur = talloc_strdup(NULL, dir);
85
86         while (strcmp(base, dir)) {
87
88                 rmdir(dir);
89
90                 /* null-terminate at the last slash */
91                 pos = strrchr(dir, '/');
92                 if (!pos)
93                         break;
94
95                 *pos = '\0';
96         }
97
98         talloc_free(cur);
99
100         return 0;
101 }
102
103 /**
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.
108  */
109
110 int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run)
111 {
112 #if defined(DEBUG)
113         enum {do_debug = 1};
114 #else
115         enum {do_debug = 0};
116 #endif
117         int status;
118         pid_t pid;
119
120         if (do_debug) {
121                 const char *const *p = cmd_argv;
122
123                 pb_log("%s: %s", __func__, (dry_run ? "(dry-run) " : ""));
124
125                 while (*p) {
126                         pb_log("%s ", *p);
127                         p++;
128                 }
129                 pb_log("\n");
130         } else
131                 pb_log("%s: %s%s\n", __func__, (dry_run ? "(dry-run) " : ""),
132                         cmd_argv[0]);
133
134         if (dry_run)
135                 return 0;
136
137         pid = fork();
138
139         if (pid == -1) {
140                 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
141                 return -1;
142         }
143
144         if (pid == 0) {
145                 int log = fileno(pb_log_get_stream());
146
147                 /* Redirect child output to log. */
148
149                 status = dup2(log, STDOUT_FILENO);
150                 assert(status != -1);
151
152                 status = dup2(log, STDERR_FILENO);
153                 assert(status != -1);
154
155                 execvp(cmd_argv[0], (char *const *)cmd_argv);
156                 pb_log("%s: exec failed: %s\n", __func__, strerror(errno));
157                 exit(EXIT_FAILURE);
158         }
159
160         if (!wait && !waitpid(pid, &status, WNOHANG))
161                 return 0;
162
163         if (waitpid(pid, &status, 0) == -1) {
164                 pb_log("%s: waitpid failed: %s\n", __func__,
165                                 strerror(errno));
166                 return -1;
167         }
168
169         if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
170                 pb_log("%s: signaled\n", __func__);
171
172         if (!WIFEXITED(status)) {
173                 pb_log("%s: %s failed\n", __func__, cmd_argv[0]);
174                 return -1;
175         }
176
177         if (WEXITSTATUS(status))
178                 pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status));
179
180         return WEXITSTATUS(status);
181 }