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