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;
20 extern struct parser yaboot_parser;
24 /* array of parsers, ordered by priority */
25 static struct parser *parsers[] = {
31 #define log(...) fprintf(logf, __VA_ARGS__)
33 static void iterate_parsers(const char *devpath, const char *mountpoint)
37 log("trying parsers for %s@%s\n", devpath, mountpoint);
39 for (i = 0; parsers[i]; i++) {
40 log("\ttrying parser '%s'\n", parsers[i]->name);
41 /* just use a dummy device path for now */
42 if (parsers[i]->parse(devpath, mountpoint))
45 log("\tno boot_options found\n");
48 static void print_boot_option(const struct boot_option *opt)
50 log("\tname: %s\n", opt->name);
51 log("\tdescription: %s\n", opt->description);
52 log("\tboot_image: %s\n", opt->boot_image_file);
53 log("\tinitrd: %s\n", opt->initrd_file);
54 log("\tboot_args: %s\n", opt->boot_args);
58 static void print_device(const struct device *dev)
60 log("\tid: %s\n", dev->id);
61 log("\tname: %s\n", dev->name);
62 log("\tdescription: %s\n", dev->description);
63 log("\tboot_image: %s\n", dev->icon_file);
67 void free_device(struct device *dev)
76 free(dev->description);
82 void free_boot_option(struct boot_option *opt)
89 free(opt->description);
92 if (opt->boot_image_file)
93 free(opt->boot_image_file);
95 free(opt->initrd_file);
101 static int write_action(int fd, enum device_action action)
103 uint8_t action_buf = action;
104 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
107 static int write_string(int fd, const char *str)
114 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
115 log("write failed: %s\n", strerror(errno));
122 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
123 log("string too large\n");
127 len_buf = __cpu_to_be32(len);
128 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
129 log("write failed: %s\n", strerror(errno));
134 int rc = write(fd, str, len - pos);
136 log("write failed: %s\n", strerror(errno));
146 int add_device(const struct device *dev)
150 log("device added:\n");
152 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
153 write_string(sock, dev->id) ||
154 write_string(sock, dev->name) ||
155 write_string(sock, dev->description) ||
156 write_string(sock, dev->icon_file);
159 log("error writing device %s to socket\n", dev->name);
164 int add_boot_option(const struct boot_option *opt)
168 log("boot option added:\n");
169 print_boot_option(opt);
171 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
172 write_string(sock, opt->id) ||
173 write_string(sock, opt->name) ||
174 write_string(sock, opt->description) ||
175 write_string(sock, opt->icon_file) ||
176 write_string(sock, opt->boot_image_file) ||
177 write_string(sock, opt->initrd_file) ||
178 write_string(sock, opt->boot_args);
181 log("error writing boot option %s to socket\n", opt->name);
186 int remove_device(const char *dev_path)
188 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
189 write_string(sock, dev_path);
192 int connect_to_socket()
196 struct sockaddr_un addr;
198 fd = socket(PF_UNIX, SOCK_STREAM, 0);
200 log("can't create socket: %s\n", strerror(errno));
204 addr.sun_family = AF_UNIX;
205 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
207 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
208 log("can't connect to %s: %s\n",
209 addr.sun_path, strerror(errno));
217 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
219 log("can't create output file: %s\n", strerror(errno));
227 int mount_device(const char *dev_path, char *mount_path)
230 const char *basename;
231 int pid, status, rc = -1;
233 basename = strrchr(dev_path, '/');
239 /* create a unique mountpoint */
240 dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
241 sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
244 log("failed to create temporary directory in %s: %s",
245 TMP_DIR, strerror(errno));
251 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
256 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
260 if (waitpid(pid, &status, 0) == -1) {
261 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
265 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
266 strcpy(mount_path, dir);
275 static int unmount_device(const char *dev_path)
282 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
287 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
291 if (waitpid(pid, &status, 0) == -1) {
292 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
296 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
301 const char *generic_icon_file(enum generic_icon_type type)
305 return artwork_pathname("hdd.png");
307 return artwork_pathname("usbpen.png");
308 case ICON_TYPE_OPTICAL:
309 return artwork_pathname("cdrom.png");
310 case ICON_TYPE_NETWORK:
311 case ICON_TYPE_UNKNOWN:
314 return artwork_pathname("hdd.png");
317 static const struct device fake_boot_devices[] =
322 .icon_file = artwork_pathname("hdd.png"),
326 .name = "PinkCat Linux CD",
327 .icon_file = artwork_pathname("cdrom.png"),
331 static const struct boot_option fake_boot_options[] =
335 .name = "Bloobuntu Linux",
336 .description = "Boot Bloobuntu Linux",
337 .icon_file = artwork_pathname("hdd.png"),
341 .name = "Pendora Gore 6",
342 .description = "Boot Pendora Gora 6",
343 .icon_file = artwork_pathname("hdd.png"),
347 .name = "Genfoo Minux",
348 .description = "Boot Genfoo Minux",
349 .icon_file = artwork_pathname("hdd.png"),
353 .name = "PinkCat Linux",
354 .description = "Install PinkCat Linux - Graphical install",
355 .icon_file = artwork_pathname("cdrom.png"),
359 enum generic_icon_type guess_device_type(void)
361 const char *bus = getenv("ID_BUS");
362 if (streq(bus, "usb"))
363 return ICON_TYPE_USB;
364 if (streq(bus, "ata") || streq(bus, "scsi"))
365 return ICON_TYPE_DISK;
366 return ICON_TYPE_UNKNOWN;
369 int main(int argc, char **argv)
371 char mountpoint[PATH_MAX];
372 char *dev_path, *action;
378 action = getenv("ACTION");
384 log("missing environment?\n");
388 if (connect_to_socket())
391 if (streq(action, "fake")) {
394 add_device(&fake_boot_devices[0]);
395 add_boot_option(&fake_boot_options[0]);
396 add_boot_option(&fake_boot_options[1]);
397 add_boot_option(&fake_boot_options[2]);
398 add_device(&fake_boot_devices[1]);
399 add_boot_option(&fake_boot_options[3]);
404 dev_path = getenv("DEVNAME");
406 log("missing environment?\n");
410 if (streq(action, "add")) {
411 if (mount_device(dev_path, mountpoint)) {
412 log("failed to mount %s\n", dev_path);
416 log("mounted %s at %s\n", dev_path, mountpoint);
418 iterate_parsers(dev_path, mountpoint);
420 } else if (streq(action, "remove")) {
421 log("%s removed\n", dev_path);
423 remove_device(dev_path);
425 /* Unmount it repeatedly, if needs be */
426 while (!unmount_device(dev_path))
430 log("invalid action '%s'\n", action);