X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=extras%2Fedd_id%2Fedd_id.c;h=4154387cb6ded8ab2fa9713067d7a3c981bedc41;hp=8ac50e9812f7b907c61dba3004805980d4440b75;hb=c3f0b654fc18b5f98bc65074e2118177e0f37ae9;hpb=4b12433517bfb1a4cdf4eecc5102f2cc5e1e6288 diff --git a/extras/edd_id/edd_id.c b/extras/edd_id/edd_id.c index 8ac50e981..4154387cb 100644 --- a/extras/edd_id/edd_id.c +++ b/extras/edd_id/edd_id.c @@ -7,7 +7,6 @@ * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation version 2 of the License. - * */ #ifndef _GNU_SOURCE @@ -19,40 +18,23 @@ #include #include #include +#include #include #include #include -#include "../../logging.h" -#include "../../udev_utils.h" +#include "../../udev/udev.h" -#ifdef USE_LOG -void log_message(int priority, const char *format, ...) +static void log_fn(struct udev *udev, int priority, + const char *file, int line, const char *fn, + const char *format, va_list args) { - va_list args; - static int udev_log = -1; - - if (udev_log == -1) { - const char *value; - - value = getenv("UDEV_LOG"); - if (value) - udev_log = log_priority(value); - else - udev_log = LOG_ERR; - } - - if (priority > udev_log) - return; - - va_start(args, format); vsyslog(priority, format, args); - va_end(args); } -#endif int main(int argc, char *argv[]) { + struct udev *udev; const char *node = NULL; int i; int export = 0; @@ -63,9 +45,14 @@ int main(int argc, char *argv[]) int sysfs_fd; DIR *dir = NULL; int rc = 1; - char *match = NULL; + char match[NAME_MAX]; + + udev = udev_new(); + if (udev == NULL) + goto exit; logging_init("edd_id"); + udev_set_log_fn(udev, log_fn); for (i = 1 ; i < argc; i++) { char *arg = argv[i]; @@ -75,16 +62,16 @@ int main(int argc, char *argv[]) } else node = arg; } - if (!node) { - err("no node specified"); + if (node == NULL) { + err(udev, "no node specified\n"); fprintf(stderr, "no node specified\n"); goto exit; } /* check for kernel support */ dir = opendir("/sys/firmware/edd"); - if (!dir) { - info("no kernel EDD support"); + if (dir == NULL) { + info(udev, "no kernel EDD support\n"); fprintf(stderr, "no kernel EDD support\n"); rc = 2; goto exit; @@ -92,7 +79,7 @@ int main(int argc, char *argv[]) disk_fd = open(node, O_RDONLY); if (disk_fd < 0) { - info("unable to open '%s'", node); + info(udev, "unable to open '%s'\n", node); fprintf(stderr, "unable to open '%s'\n", node); rc = 3; goto closedir; @@ -100,45 +87,46 @@ int main(int argc, char *argv[]) /* check for valid MBR signature */ if (lseek(disk_fd, 510, SEEK_SET) < 0) { - info("seek to MBR validity failed '%s'", node); + info(udev, "seek to MBR validity failed '%s'\n", node); rc = 4; goto close; } if (read(disk_fd, &mbr_valid, sizeof(mbr_valid)) != sizeof(mbr_valid)) { - info("read MBR validity failed '%s'", node); + info(udev, "read MBR validity failed '%s'\n", node); rc = 5; goto close; } if (mbr_valid != 0xAA55) { fprintf(stderr, "no valid MBR signature '%s'\n", node); - info("no valid MBR signature '%s'", node); + info(udev, "no valid MBR signature '%s'\n", node); rc=6; goto close; } /* read EDD signature */ if (lseek(disk_fd, 440, SEEK_SET) < 0) { - info("seek to signature failed '%s'", node); + info(udev, "seek to signature failed '%s'\n", node); rc = 7; goto close; } if (read(disk_fd, &disk_id, sizeof(disk_id)) != sizeof(disk_id)) { - info("read signature failed '%s'", node); + info(udev, "read signature failed '%s'\n", node); rc = 8; goto close; } /* all zero is invalid */ - info("read id 0x%08x from '%s'", disk_id, node); + info(udev, "read id 0x%08x from '%s'\n", disk_id, node); if (disk_id == 0) { fprintf(stderr, "no EDD signature '%s'\n", node); - info("'%s' signature is zero", node); + info(udev, "'%s' signature is zero\n", node); rc = 9; goto close; } /* lookup signature in sysfs to determine the name */ + match[0] = '\0'; for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { - char file[PATH_SIZE]; + char file[UTIL_PATH_SIZE]; char sysfs_id_buf[256]; uint32_t sysfs_id; ssize_t size; @@ -151,44 +139,49 @@ int main(int argc, char *argv[]) sysfs_fd = open(file, O_RDONLY); if (sysfs_fd < 0) { - info("unable to open sysfs '%s'", file); + info(udev, "unable to open sysfs '%s'\n", file); continue; } size = read(sysfs_fd, sysfs_id_buf, sizeof(sysfs_id_buf)-1); close(sysfs_fd); - if (size < 0) { - info("read sysfs '%s' failed", file); + if (size <= 0) { + info(udev, "read sysfs '%s' failed\n", file); continue; } sysfs_id_buf[size] = '\0'; - info("read '%s' from '%s'", sysfs_id_buf, file); - + info(udev, "read '%s' from '%s'\n", sysfs_id_buf, file); sysfs_id = strtoul(sysfs_id_buf, NULL, 16); + + /* look for matching value, that appears only once */ if (disk_id == sysfs_id) { - if (!match) { - match = dent->d_name; + if (match[0] == '\0') { + /* store id */ + util_strlcpy(match, dent->d_name, sizeof(match)); } else { - info("'%s' does not have a unique signature", node); + /* error, same signature for another device */ + info(udev, "'%s' does not have a unique signature\n", node); fprintf(stderr, "'%s' does not have a unique signature\n", node); - rc=10; + rc = 10; goto exit; } } - } - if (export) - printf("ID_EDD=%s\n", match); - else - printf("%s\n", match); - rc = 0; + if (match[0] != '\0') { + if (export) + printf("ID_EDD=%s\n", match); + else + printf("%s\n", match); + rc = 0; + } close: close(disk_fd); closedir: closedir(dir); exit: + udev_unref(udev); logging_close(); return rc; }