]> git.ozlabs.org Git - petitboot/blob - lib/system/system.c
lib/system: Add vgscan, vgchange utilities
[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         .ip             = HOST_PROG_IP,
30         .udhcpc         = HOST_PROG_UDHCPC,
31         .vgscan         = HOST_PROG_VGSCAN,
32         .vgchange       = HOST_PROG_VGCHANGE,
33 };
34
35 #ifndef TFTP_TYPE
36 #define TFTP_TYPE TFTP_TYPE_UNKNOWN
37 #endif
38
39 enum tftp_type tftp_type = TFTP_TYPE;
40
41 int pb_mkdir_recursive(const char *dir)
42 {
43         struct stat statbuf;
44         int rc, mode = 0755;
45         char *str, *sep;
46
47         if (!*dir)
48                 return 0;
49
50         if (!stat(dir, &statbuf)) {
51                 if (!S_ISDIR(statbuf.st_mode)) {
52                         pb_log("%s: %s exists, but isn't a directory\n",
53                                         __func__, dir);
54                         return -1;
55                 }
56                 return 0;
57         }
58
59         str = talloc_strdup(NULL, dir);
60         sep = strchr(*str == '/' ? str + 1 : str, '/');
61
62         rc = 0;
63
64         while (1) {
65
66                 /* terminate the path at sep */
67                 if (sep)
68                         *sep = '\0';
69
70                 if (mkdir(str, mode) && errno != EEXIST) {
71                         pb_log("mkdir(%s): %s\n", str, strerror(errno));
72                         rc = -1;
73                         break;
74                 }
75
76                 if (!sep)
77                         break;
78
79                 /* reset dir to the full path */
80                 strcpy(str, dir);
81                 sep = strchr(sep + 1, '/');
82         }
83
84         talloc_free(str);
85
86         return rc;
87 }
88
89 int pb_rmdir_recursive(const char *base, const char *dir)
90 {
91         char *cur, *pos;
92
93         /* sanity check: make sure that dir is within base */
94         if (strncmp(base, dir, strlen(base)))
95                 return -1;
96
97         cur = talloc_strdup(NULL, dir);
98
99         while (strcmp(base, cur)) {
100
101                 rmdir(cur);
102
103                 /* null-terminate at the last slash */
104                 pos = strrchr(cur, '/');
105                 if (!pos)
106                         break;
107
108                 *pos = '\0';
109         }
110
111         talloc_free(cur);
112
113         return 0;
114 }