chiark / gitweb /
man: add static device nodes and udevd debug options
[elogind.git] / extras / edd_id / edd_id.c
index 8ac50e9812f7b907c61dba3004805980d4440b75..15295ed31f40aee552172574b456d07c19b964b1 100644 (file)
@@ -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
 #include <unistd.h>
 #include <fcntl.h>
 #include <ctype.h>
+#include <string.h>
 #include <errno.h>
 #include <dirent.h>
 #include <stdint.h>
 
-#include "../../logging.h"
-#include "../../udev_utils.h"
+#include "libudev.h"
+#include "libudev-private.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 +46,15 @@ int main(int argc, char *argv[])
        int sysfs_fd;
        DIR *dir = NULL;
        int rc = 1;
-       char *match = NULL;
+       char filename[UTIL_PATH_SIZE];
+       char match[UTIL_PATH_SIZE];
 
-       logging_init("edd_id");
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
+       udev_log_init("edd_id");
+       udev_set_log_fn(udev, log_fn);
 
        for (i = 1 ; i < argc; i++) {
                char *arg = argv[i];
@@ -75,16 +64,17 @@ 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");
+       util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), "/firmware/edd", NULL);
+       dir = opendir(filename);
+       if (dir == NULL) {
+               info(udev, "no kernel EDD support\n");
                fprintf(stderr, "no kernel EDD support\n");
                rc = 2;
                goto exit;
@@ -92,7 +82,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 +90,45 @@ 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 sysfs_id_buf[256];
                uint32_t sysfs_id;
                ssize_t size;
@@ -146,49 +136,52 @@ int main(int argc, char *argv[])
                if (dent->d_name[0] == '.')
                        continue;
 
-               snprintf(file, sizeof(file), "/sys/firmware/edd/%s/mbr_signature", dent->d_name);
-               file[sizeof(file)-1] = '\0';
-
-               sysfs_fd = open(file, O_RDONLY);
+               util_strscpyl(filename, sizeof(filename), dent->d_name, "/mbr_signature", NULL);
+               sysfs_fd = openat(dirfd(dir), filename, O_RDONLY);
                if (sysfs_fd < 0) {
-                       info("unable to open sysfs '%s'", file);
+                       info(udev, "unable to open sysfs '%s'\n", filename);
                        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", filename);
                        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, filename);
                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_strscpy(match, sizeof(match), dent->d_name);
                        } 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:
-       logging_close();
+       udev_unref(udev);
+       udev_log_close();
        return rc;
 }