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