8 #include <sys/socket.h>
14 #include <asm/byteorder.h>
16 #include "udev-helper.h"
17 #include "petitboot-paths.h"
19 extern struct parser native_parser;
23 /* array of parsers, ordered by priority */
24 static struct parser *parsers[] = {
29 #define log(...) fprintf(logf, __VA_ARGS__)
31 static void iterate_parsers(const char *devpath, const char *mountpoint)
35 log("trying parsers for %s@%s\n", devpath, mountpoint);
37 for (i = 0; parsers[i]; i++) {
38 log("\ttrying parser '%s'\n", parsers[i]->name);
39 /* just use a dummy device path for now */
40 if (parsers[i]->parse(devpath, mountpoint))
43 log("\tno boot_options found\n");
46 static void print_boot_option(const struct boot_option *opt)
48 log("\tname: %s\n", opt->name);
49 log("\tdescription: %s\n", opt->description);
50 log("\tboot_image: %s\n", opt->boot_image_file);
51 log("\tboot_args: %s\n", opt->boot_args);
55 static void print_device(const struct device *dev)
57 log("\tid: %s\n", dev->name);
58 log("\tname: %s\n", dev->name);
59 log("\tdescription: %s\n", dev->description);
60 log("\tboot_image: %s\n", dev->icon_file);
64 void free_device(struct device *dev)
73 free(dev->description);
79 void free_boot_option(struct boot_option *opt)
86 free(opt->description);
89 if (opt->boot_image_file)
90 free(opt->boot_image_file);
92 free(opt->initrd_file);
98 static int write_action(int fd, enum device_action action)
100 uint8_t action_buf = action;
101 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
104 static int write_string(int fd, const char *str)
111 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
112 log("write failed: %s\n", strerror(errno));
119 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
120 log("string too large\n");
124 len_buf = __cpu_to_be32(len);
125 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
126 log("write failed: %s\n", strerror(errno));
131 int rc = write(fd, str, len - pos);
133 log("write failed: %s\n", strerror(errno));
143 int add_device(const struct device *dev)
147 log("device added:\n");
149 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
150 write_string(sock, dev->id) ||
151 write_string(sock, dev->name) ||
152 write_string(sock, dev->description) ||
153 write_string(sock, dev->icon_file);
156 log("error writing device %s to socket\n", dev->name);
161 int add_boot_option(const struct boot_option *opt)
165 log("boot option added:\n");
166 print_boot_option(opt);
168 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
169 write_string(sock, opt->id) ||
170 write_string(sock, opt->name) ||
171 write_string(sock, opt->description) ||
172 write_string(sock, opt->icon_file) ||
173 write_string(sock, opt->boot_image_file) ||
174 write_string(sock, opt->initrd_file) ||
175 write_string(sock, opt->boot_args);
178 log("error writing boot option %s to socket\n", opt->name);
183 int remove_device(const char *dev_path)
185 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
186 write_string(sock, dev_path);
189 int connect_to_socket()
193 struct sockaddr_un addr;
195 fd = socket(PF_UNIX, SOCK_STREAM, 0);
197 log("can't create socket: %s\n", strerror(errno));
201 addr.sun_family = AF_UNIX;
202 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
204 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
205 log("can't connect to %s: %s\n",
206 addr.sun_path, strerror(errno));
214 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
216 log("can't create output file: %s\n", strerror(errno));
224 #define template "mnt-XXXXXX"
226 static int mount_device(const char *dev_path, char *mount_path)
229 int pid, status, rc = -1;
231 /* create a unique mountpoint */
232 dir = malloc(strlen(TMP_DIR) + 2 + strlen(template));
233 sprintf(dir, "%s/%s", TMP_DIR, template);
236 log("failed to create temporary directory in %s: %s",
237 TMP_DIR, strerror(errno));
243 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
248 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
252 if (waitpid(pid, &status, 0) == -1) {
253 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
257 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
258 strcpy(mount_path, dir);
267 static int unmount_device(const char *dev_path)
274 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
279 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
283 if (waitpid(pid, &status, 0) == -1) {
284 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
288 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
293 const char *generic_icon_file(enum generic_icon_type type)
297 return artwork_pathname("hdd.png");
299 return artwork_pathname("usbpen.png");
300 case ICON_TYPE_OPTICAL:
301 return artwork_pathname("cdrom.png");
302 case ICON_TYPE_NETWORK:
303 case ICON_TYPE_UNKNOWN:
306 return artwork_pathname("hdd.png");
309 static const struct device fake_boot_devices[] =
314 .icon_file = artwork_pathname("hdd.png"),
318 .name = "PinkCat Linux CD",
319 .icon_file = artwork_pathname("cdrom.png"),
323 static const struct boot_option fake_boot_options[] =
327 .name = "Bloobuntu Linux",
328 .description = "Boot Bloobuntu Linux",
329 .icon_file = artwork_pathname("hdd.png"),
333 .name = "Pendora Gore 6",
334 .description = "Boot Pendora Gora 6",
335 .icon_file = artwork_pathname("hdd.png"),
339 .name = "Genfoo Minux",
340 .description = "Boot Genfoo Minux",
341 .icon_file = artwork_pathname("hdd.png"),
345 .name = "PinkCat Linux",
346 .description = "Install PinkCat Linux - Graphical install",
347 .icon_file = artwork_pathname("cdrom.png"),
351 enum generic_icon_type guess_device_type(void)
353 const char *bus = getenv("ID_BUS");
354 if (streq(bus, "usb"))
355 return ICON_TYPE_USB;
356 if (streq(bus, "ata") || streq(bus, "scsi"))
357 return ICON_TYPE_DISK;
358 return ICON_TYPE_UNKNOWN;
361 int main(int argc, char **argv)
363 char mountpoint[PATH_MAX];
364 char *dev_path, *action;
370 action = getenv("ACTION");
376 log("missing environment?\n");
380 if (connect_to_socket())
383 if (streq(action, "fake")) {
386 add_device(&fake_boot_devices[0]);
387 add_boot_option(&fake_boot_options[0]);
388 add_boot_option(&fake_boot_options[1]);
389 add_boot_option(&fake_boot_options[2]);
390 add_device(&fake_boot_devices[1]);
391 add_boot_option(&fake_boot_options[3]);
396 dev_path = getenv("DEVNAME");
398 log("missing environment?\n");
402 if (streq(action, "add")) {
403 if (mount_device(dev_path, mountpoint)) {
404 log("failed to mount %s\n", dev_path);
408 log("mounted %s at %s\n", dev_path, mountpoint);
410 iterate_parsers(dev_path, mountpoint);
412 } else if (streq(action, "remove")) {
413 log("%s removed\n", dev_path);
415 remove_device(dev_path);
417 unmount_device(dev_path);
420 log("invalid action '%s'\n", action);