chiark / gitweb /
use libudev code, unify logging, pass udev context around everywhere
authorKay Sievers <kay.sievers@vrfy.org>
Sat, 6 Sep 2008 13:45:31 +0000 (15:45 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Sat, 6 Sep 2008 13:45:31 +0000 (15:45 +0200)
56 files changed:
extras/ata_id/Makefile.am
extras/ata_id/ata_id.c
extras/cdrom_id/Makefile.am
extras/cdrom_id/cdrom_id.c
extras/edd_id/Makefile.am
extras/edd_id/edd_id.c
extras/floppy/Makefile.am
extras/floppy/create_floppy_devices.c
extras/fstab_import/79-fstab_import.rules
extras/fstab_import/Makefile.am
extras/fstab_import/fstab_import.c
extras/scsi_id/Makefile.am
extras/scsi_id/scsi_id.c
extras/scsi_id/scsi_id.h
extras/scsi_id/scsi_serial.c
extras/usb_id/Makefile.am
extras/usb_id/usb_id.c
extras/volume_id/Makefile.am
extras/volume_id/vol_id.c
udev/Makefile.am
udev/lib/Makefile.am
udev/lib/exported_symbols
udev/lib/libudev-device.c
udev/lib/libudev-enumerate.c
udev/lib/libudev-monitor.c
udev/lib/libudev-private.h
udev/lib/libudev.c
udev/lib/libudev.h
udev/logging.h [deleted file]
udev/test-udev.c
udev/udev-control.c
udev/udev.h
udev/udev_config.c [deleted file]
udev/udev_db.c
udev/udev_device.c
udev/udev_device_event.c
udev/udev_node.c
udev/udev_rules.c
udev/udev_rules.h
udev/udev_rules_parse.c
udev/udev_selinux.c
udev/udev_selinux.h
udev/udev_sysfs.c
udev/udev_utils.c
udev/udev_utils_file.c
udev/udev_utils_string.c
udev/udevadm-control.c
udev/udevadm-info.c
udev/udevadm-monitor.c
udev/udevadm-settle.c
udev/udevadm-test.c
udev/udevadm-trigger.c
udev/udevadm.c
udev/udevadm.xml
udev/udevd.c
udev/udevd.h

index 81332dd..7e7fd2b 100644 (file)
@@ -4,10 +4,14 @@ udevhome_PROGRAMS = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 ata_id_SOURCES = \
        ata_id.c \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
+       ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 125c3f3..43d9516 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * ata_id - reads product/serial number from ATA drives
  *
- * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
  *
  *     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
 
 #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
 
 static void set_str(char *to, const char *from, size_t count)
 {
@@ -87,6 +69,7 @@ static void set_str(char *to, const char *from, size_t count)
 
 int main(int argc, char *argv[])
 {
+       struct udev *udev;
        struct hd_driveid id;
        char model[41];
        char serial[21];
@@ -101,7 +84,12 @@ int main(int argc, char *argv[])
                {}
        };
 
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
        logging_init("ata_id");
+       udev_set_log_fn(udev, log_fn);
 
        while (1) {
                int option;
@@ -126,24 +114,24 @@ int main(int argc, char *argv[])
 
        node = argv[optind];
        if (node == NULL) {
-               err("no node specified\n");
+               err(udev, "no node specified\n");
                rc = 1;
                goto exit;
        }
 
        fd = open(node, O_RDONLY|O_NONBLOCK);
        if (fd < 0) {
-               err("unable to open '%s'\n", node);
+               err(udev, "unable to open '%s'\n", node);
                rc = 1;
                goto exit;
        }
 
        if (ioctl(fd, HDIO_GET_IDENTITY, &id)) {
                if (errno == ENOTTY) {
-                       info("HDIO_GET_IDENTITY unsupported for '%s'\n", node);
+                       info(udev, "HDIO_GET_IDENTITY unsupported for '%s'\n", node);
                        rc = 2;
                } else {
-                       err("HDIO_GET_IDENTITY failed for '%s'\n", node);
+                       err(udev, "HDIO_GET_IDENTITY failed for '%s'\n", node);
                        rc = 3;
                }
                goto close;
@@ -190,6 +178,7 @@ int main(int argc, char *argv[])
 close:
        close(fd);
 exit:
+       udev_unref(udev);
        logging_close();
        return rc;
 }
index 88de137..dfc9885 100644 (file)
@@ -8,10 +8,14 @@ dist_udevrules_DATA = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 cdrom_id_SOURCES = \
        cdrom_id.c \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
+       ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 9e27471..dbe80ea 100644 (file)
 
 static int debug;
 
-#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 (debug && udev_log < LOG_INFO)
-                       udev_log = LOG_INFO;
-       }
-
-       if (priority > udev_log)
-               return;
-
-       va_start(args, format);
        if (debug) {
-               fprintf(stderr, "[%d] ", (int) getpid());
+               fprintf(stderr, "%s: ", fn);
                vfprintf(stderr, format, args);
-       } else
+       } else {
                vsyslog(priority, format, args);
-       va_end(args);
+       }
 }
-#endif
 
 /* device info */
 static unsigned int cd_cd_rom;
@@ -135,13 +115,13 @@ static unsigned long long int cd_media_session_last_offset;
 #define ASC(errcode)   (((errcode) >> 8) & 0xFF)
 #define ASCQ(errcode)  ((errcode) & 0xFF)
 
-static void info_scsi_cmd_err(const char *cmd, int err)
+static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
 {
        if (err == -1) {
-               info("%s failed\n", cmd);
+               info(udev, "%s failed\n", cmd);
                return;
        }
-       info("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
+       info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
 }
 
 struct scsi_cmd {
@@ -153,7 +133,7 @@ struct scsi_cmd {
        struct sg_io_hdr sg_io;
 };
 
-static void scsi_cmd_set(struct scsi_cmd *cmd, size_t i, int arg)
+static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, int arg)
 {
        if (i == 0) {
                memset(cmd, 0x00, sizeof(struct scsi_cmd));
@@ -172,7 +152,7 @@ static void scsi_cmd_set(struct scsi_cmd *cmd, size_t i, int arg)
 
 #define CHECK_CONDITION 0x01
 
-static int scsi_cmd_run(struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
+static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
 {
        int ret = 0;
 
@@ -194,13 +174,13 @@ static int scsi_cmd_run(struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t
        return ret;
 }
 
-static int cd_capability_compat(int fd)
+static int cd_capability_compat(struct udev *udev, int fd)
 {
        int capabilty;
 
        capabilty = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
        if (capabilty < 0) {
-               info("CDROM_GET_CAPABILITY failed\n");
+               info(udev, "CDROM_GET_CAPABILITY failed\n");
                return -1;
        }
 
@@ -221,30 +201,30 @@ static int cd_capability_compat(int fd)
        return 0;
 }
 
-static int cd_inquiry(int fd) {
+static int cd_inquiry(struct udev *udev, int fd) {
        struct scsi_cmd sc;
        unsigned char inq[128];
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x12);
-       scsi_cmd_set(&sc, 4, 36);
-       scsi_cmd_set(&sc, 5, 0);
-       err = scsi_cmd_run(&sc, fd, inq, 36);
+       scsi_cmd_set(udev, &sc, 0, 0x12);
+       scsi_cmd_set(udev, &sc, 4, 36);
+       scsi_cmd_set(udev, &sc, 5, 0);
+       err = scsi_cmd_run(udev, &sc, fd, inq, 36);
        if ((err < 0)) {
-               info_scsi_cmd_err("INQUIRY", err);
+               info_scsi_cmd_err(udev, "INQUIRY", err);
                return -1;
        }
 
        if ((inq[0] & 0x1F) != 5) {
-               info("not an MMC unit\n");
+               info(udev, "not an MMC unit\n");
                return -1;
        }
 
-       info("INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
+       info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
        return 0;
 }
 
-static int cd_profiles(int fd)
+static int cd_profiles(struct udev *udev, int fd)
 {
        struct scsi_cmd sc;
        unsigned char header[8];
@@ -254,32 +234,32 @@ static int cd_profiles(int fd)
        unsigned int i;
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x46);
-       scsi_cmd_set(&sc, 1, 0);
-       scsi_cmd_set(&sc, 8, sizeof(header));
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x46);
+       scsi_cmd_set(udev, &sc, 1, 0);
+       scsi_cmd_set(udev, &sc, 8, sizeof(header));
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("GET CONFIGURATION", err);
+               info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
                return -1;
        }
 
        len = 4 + (header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3]);
-       info("GET CONFIGURATION: number of profiles %i\n", len);
+       info(udev, "GET CONFIGURATION: number of profiles %i\n", len);
        if (len > sizeof(profiles)) {
-               info("invalid number of profiles\n");
+               info(udev, "invalid number of profiles\n");
                return -1;
        }
 
-       scsi_cmd_set(&sc, 0, 0x46);
-       scsi_cmd_set(&sc, 1, 1);
-       scsi_cmd_set(&sc, 6, len >> 16);
-       scsi_cmd_set(&sc, 7, len >> 8);
-       scsi_cmd_set(&sc, 8, len);
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, profiles, len);
+       scsi_cmd_set(udev, &sc, 0, 0x46);
+       scsi_cmd_set(udev, &sc, 1, 1);
+       scsi_cmd_set(udev, &sc, 6, len >> 16);
+       scsi_cmd_set(udev, &sc, 7, len >> 8);
+       scsi_cmd_set(udev, &sc, 8, len);
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, profiles, len);
        if ((err < 0)) {
-               info_scsi_cmd_err("GET CONFIGURATION", err);
+               info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
                return -1;
        }
 
@@ -288,7 +268,7 @@ static int cd_profiles(int fd)
                unsigned int profile = (profiles[i] << 8 | profiles[i + 1]);
                if (profile == 0)
                        continue;
-               info("profile 0x%02x\n", profile);
+               info(udev, "profile 0x%02x\n", profile);
 
                switch (profile) {
                case 0x03:
@@ -344,9 +324,9 @@ static int cd_profiles(int fd)
 
        /* current media profile */
        cur_profile = header[6] << 8 | header[7];
-       info("current profile 0x%02x\n", cur_profile);
+       info(udev, "current profile 0x%02x\n", cur_profile);
        if (cur_profile == 0) {
-               info("no current profile, assuming no media\n");
+               info(udev, "no current profile, assuming no media\n");
                return -1;
        }
 
@@ -415,7 +395,7 @@ static int cd_profiles(int fd)
        return 0;
 }
 
-static int cd_media_info(int fd)
+static int cd_media_info(struct udev *udev, int fd)
 {
        struct scsi_cmd sc;
        unsigned char header[32];
@@ -427,16 +407,16 @@ static int cd_media_info(int fd)
        };
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x51);
-       scsi_cmd_set(&sc, 8, sizeof(header));
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x51);
+       scsi_cmd_set(udev, &sc, 8, sizeof(header));
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("READ DISC INFORMATION", err);
+               info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
                return -1;
        };
 
-       info("disk type %02x\n", header[8]);
+       info(udev, "disk type %02x\n", header[8]);
 
        if ((header[2] & 3) < 4)
                cd_media_state = media_status[header[2] & 3];
@@ -448,7 +428,7 @@ static int cd_media_info(int fd)
        return 0;
 }
 
-static int cd_media_toc(int fd)
+static int cd_media_toc(struct udev *udev, int fd)
 {
        struct scsi_cmd sc;
        unsigned char header[12];
@@ -457,18 +437,18 @@ static int cd_media_toc(int fd)
        unsigned char *p;
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x43);
-       scsi_cmd_set(&sc, 6, 1);
-       scsi_cmd_set(&sc, 8, sizeof(header));
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x43);
+       scsi_cmd_set(udev, &sc, 6, 1);
+       scsi_cmd_set(udev, &sc, 8, sizeof(header));
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("READ TOC", err);
+               info_scsi_cmd_err(udev, "READ TOC", err);
                return -1;
        }
 
        len = (header[0] << 8 | header[1]) + 2;
-       info("READ TOC: len: %d\n", len);
+       info(udev, "READ TOC: len: %d\n", len);
        if (len > sizeof(toc))
                return -1;
        if (len < 2)
@@ -478,14 +458,14 @@ static int cd_media_toc(int fd)
        if (len < 8)
                return 0;
 
-       scsi_cmd_set(&sc, 0, 0x43);
-       scsi_cmd_set(&sc, 6, header[2]); /* First Track/Session Number */
-       scsi_cmd_set(&sc, 7, len >> 8);
-       scsi_cmd_set(&sc, 8, len);
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, toc, len);
+       scsi_cmd_set(udev, &sc, 0, 0x43);
+       scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
+       scsi_cmd_set(udev, &sc, 7, len >> 8);
+       scsi_cmd_set(udev, &sc, 8, len);
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, toc, len);
        if ((err < 0)) {
-               info_scsi_cmd_err("READ TOC (tracks)", err);
+               info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
                return -1;
        }
 
@@ -496,7 +476,7 @@ static int cd_media_toc(int fd)
                is_data_track = (p[1] & 0x04) != 0;
 
                block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
-               info("track=%u info=0x%x(%s) start_block=%u\n",
+               info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
                     p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
 
                if (is_data_track)
@@ -505,23 +485,24 @@ static int cd_media_toc(int fd)
                        cd_media_track_count_audio++;
        }
 
-       scsi_cmd_set(&sc, 0, 0x43);
-       scsi_cmd_set(&sc, 2, 1); /* Session Info */
-       scsi_cmd_set(&sc, 8, 12);
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x43);
+       scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
+       scsi_cmd_set(udev, &sc, 8, 12);
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("READ TOC (multi session)", err);
+               info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
                return -1;
        }
        len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
-       info("last track %u starts at block %u\n", header[4+2], len);
+       info(udev, "last track %u starts at block %u\n", header[4+2], len);
        cd_media_session_last_offset = (unsigned long long int)len * 2048;
        return 0;
 }
 
 int main(int argc, char *argv[])
 {
+       struct udev *udev;
        static const struct option options[] = {
                { "export", 0, NULL, 'x' },
                { "debug", 0, NULL, 'd' },
@@ -533,7 +514,12 @@ int main(int argc, char *argv[])
        int fd = -1;
        int rc = 0;
 
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
        logging_init("cdrom_id");
+       udev_set_log_fn(udev, log_fn);
 
        while (1) {
                int option;
@@ -545,6 +531,8 @@ int main(int argc, char *argv[])
                switch (option) {
                case 'd':
                        debug = 1;
+                       if (udev_get_log_priority(udev) < LOG_INFO)
+                               udev_set_log_priority(udev, LOG_INFO);
                        break;
                case 'x':
                        export = 1;
@@ -563,7 +551,7 @@ int main(int argc, char *argv[])
 
        node = argv[optind];
        if (!node) {
-               err("no device\n");
+               err(udev, "no device\n");
                fprintf(stderr, "no device\n");
                rc = 1;
                goto exit;
@@ -571,34 +559,34 @@ int main(int argc, char *argv[])
 
        fd = open(node, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
-               info("unable to open '%s'\n", node);
+               info(udev, "unable to open '%s'\n", node);
                rc = 1;
                goto exit;
        }
-       info("probing: '%s'\n", node);
+       info(udev, "probing: '%s'\n", node);
 
        /* same data as original cdrom_id */
-       if (cd_capability_compat(fd) < 0) {
+       if (cd_capability_compat(udev, fd) < 0) {
                rc = 1;
                goto exit;
        }
 
        /* check drive */
-       if (cd_inquiry(fd) < 0) {
+       if (cd_inquiry(udev, fd) < 0) {
                rc = 2;
                goto exit;
        }
 
        /* read drive and possibly current profile */
-       if (cd_profiles(fd) < 0)
+       if (cd_profiles(udev, fd) < 0)
                goto print;
 
        /* get session/track info */
-       if (cd_media_toc(fd) < 0)
+       if (cd_media_toc(udev, fd) < 0)
                goto print;
 
        /* get writable media state */
-       if (cd_media_info(fd) < 0)
+       if (cd_media_info(udev, fd) < 0)
                goto print;
 
 print:
@@ -702,6 +690,7 @@ print:
 exit:
        if (fd >= 0)
                close(fd);
+       udev_unref(udev);
        logging_close();
        return rc;
 }
index fa4f18c..cc69396 100644 (file)
@@ -8,10 +8,14 @@ dist_udevrules_DATA = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 edd_id_SOURCES = \
        edd_id.c \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
+       ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 9d745c5..b53700e 100644 (file)
 
 #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;
@@ -64,7 +47,12 @@ int main(int argc, char *argv[])
        int rc = 1;
        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,7 +63,7 @@ int main(int argc, char *argv[])
                        node = arg;
        }
        if (node == NULL) {
-               err("no node specified\n");
+               err(udev, "no node specified\n");
                fprintf(stderr, "no node specified\n");
                goto exit;
        }
@@ -83,7 +71,7 @@ int main(int argc, char *argv[])
        /* check for kernel support */
        dir = opendir("/sys/firmware/edd");
        if (dir == NULL) {
-               info("no kernel EDD support\n");
+               info(udev, "no kernel EDD support\n");
                fprintf(stderr, "no kernel EDD support\n");
                rc = 2;
                goto exit;
@@ -91,7 +79,7 @@ int main(int argc, char *argv[])
 
        disk_fd = open(node, O_RDONLY);
        if (disk_fd < 0) {
-               info("unable to open '%s'\n", node);
+               info(udev, "unable to open '%s'\n", node);
                fprintf(stderr, "unable to open '%s'\n", node);
                rc = 3;
                goto closedir;
@@ -99,38 +87,38 @@ 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'\n", 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'\n", 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'\n", 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'\n", 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'\n", node);
+               info(udev, "read signature failed '%s'\n", node);
                rc = 8;
                goto close;
        }
        /* all zero is invalid */
-       info("read id 0x%08x from '%s'\n", 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\n", node);
+               info(udev, "'%s' signature is zero\n", node);
                rc = 9;
                goto close;
        }
@@ -150,18 +138,18 @@ int main(int argc, char *argv[])
 
                sysfs_fd = open(file, O_RDONLY);
                if (sysfs_fd < 0) {
-                       info("unable to open sysfs '%s'\n", 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\n", file);
+                       info(udev, "read sysfs '%s' failed\n", file);
                        continue;
                }
                sysfs_id_buf[size] = '\0';
-               info("read '%s' from '%s'\n", 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 */
@@ -171,7 +159,7 @@ int main(int argc, char *argv[])
                                strlcpy(match, dent->d_name, sizeof(match));
                        } else {
                                /* error, same signature for another device */
-                               info("'%s' does not have a unique signature\n", node);
+                               info(udev, "'%s' does not have a unique signature\n", node);
                                fprintf(stderr, "'%s' does not have a unique signature\n", node);
                                rc = 10;
                                goto exit;
@@ -192,6 +180,7 @@ close:
 closedir:
        closedir(dir);
 exit:
+       udev_unref(udev);
        logging_close();
        return rc;
 }
index 4854d52..7879e7c 100644 (file)
@@ -4,11 +4,14 @@ udevhome_PROGRAMS = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 create_floppy_devices_SOURCES = \
        create_floppy_devices.c \
-       ../../udev/udev_config.c \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
+       ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c \
index 3e73aa8..2c7d44b 100644 (file)
@@ -41,33 +41,16 @@ static int t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23, 0 };
 static int t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13, 17, 21, 22, 30, 0 };
 static int *table_sup[] = { NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in };
 
-#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;
        char *dev;
        char *devname;
        char node[64];
@@ -80,6 +63,13 @@ int main(int argc, char **argv)
        int print_nodes = 0;
        int is_err = 0;
 
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
+       logging_init("create_floppy_devices");
+       udev_set_log_fn(udev, log_fn);
+
        while ((c = getopt(argc, argv, "cudm:U:G:M:t:")) != -1) {
                switch (c) {
                case 'c':
@@ -89,10 +79,10 @@ int main(int argc, char **argv)
                        print_nodes = 1;
                        break;
                case 'U':
-                       uid = lookup_user(optarg);
+                       uid = lookup_user(udev, optarg);
                        break;
                case 'G':
-                       gid = lookup_group(optarg);
+                       gid = lookup_group(udev, optarg);
                        break;
                case 'M':
                        mode = strtol(optarg, NULL, 0);
@@ -155,8 +145,7 @@ int main(int argc, char **argv)
        if (type == 0)
                return 0;
 
-       udev_config_init();
-       selinux_init();
+       selinux_init(udev);
 
        i = 0;
        while (table_sup[type][i]) {
@@ -166,15 +155,17 @@ int main(int argc, char **argv)
                        printf("%s b %d %d %d\n", node, mode, major, minor);
                if (create_nodes) {
                        unlink(node);
-                       selinux_setfscreatecon(node, NULL, S_IFBLK | mode);
+                       selinux_setfscreatecon(udev, node, NULL, S_IFBLK | mode);
                        mknod(node, S_IFBLK | mode, makedev(major,minor));
-                       selinux_resetfscreatecon();
+                       selinux_resetfscreatecon(udev);
                        chown(node, uid, gid);
                        chmod(node, S_IFBLK | mode);
                }
                i++;
        }
 
-       selinux_exit();
+       selinux_exit(udev);
+       udev_unref(udev);
+exit:
        return 0;
 }
index ccd718c..44accde 100644 (file)
@@ -1,2 +1,2 @@
-ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem|other", IMPORT="fstab_import --root=$root $name $links mapper/$env{DM_NAME}"
+ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem|other", IMPORT="fstab_import $name $links mapper/$env{DM_NAME}"
 
index 0ee7efb..902ab1e 100644 (file)
@@ -8,10 +8,14 @@ dist_udevrules_DATA = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 fstab_import_SOURCES = \
        fstab_import.c \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
+       ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 2d24cfb..0f4685a 100644 (file)
@@ -13,6 +13,8 @@
 #define _GNU_SOURCE 1
 #endif
 
+#include "config.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include "../../udev/udev.h"
 
 static int debug;
-static char root[PATH_SIZE] = "/dev";
-static char **devices;
 
-#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 (debug && udev_log < LOG_INFO)
-                       udev_log = LOG_INFO;
-       }
-
-       if (priority > udev_log)
-               return;
-
-       va_start(args, format);
        if (debug) {
-               fprintf(stderr, "[%d] ", (int) getpid());
+               fprintf(stderr, "%s: ", fn);
                vfprintf(stderr, format, args);
-       } else
+       } else {
                vsyslog(priority, format, args);
-       va_end(args);
+       }
 }
-#endif
 
-static int matches_device_list(const char *name)
+static int matches_device_list(struct udev *udev, char **devices, const char *name)
 {
        int i;
 
        for (i = 0; devices[i] != NULL; i++) {
-               info("compare '%s' == '%s'\n", name, devices[i]);
+               info(udev, "compare '%s' == '%s'\n", name, devices[i]);
                if (strcmp(devices[i], name) == 0)
                        return 1;
        }
        return 0;
 }
 
-static void print_fstab_entry(struct mntent *mnt)
+static void print_fstab_entry(struct udev *udev, struct mntent *mnt)
 {
        printf("FSTAB_NAME=%s\n", mnt->mnt_fsname);
        printf("FSTAB_DIR=%s\n", mnt->mnt_dir);
@@ -86,42 +66,48 @@ static void print_fstab_entry(struct mntent *mnt)
 
 int main(int argc, char *argv[])
 {
+       struct udev *udev;
        static const struct option options[] = {
                { "export", 0, NULL, 'x' },
-               { "root", 1, NULL, 'r' },
                { "debug", 0, NULL, 'd' },
                { "help", 0, NULL, 'h' },
                {}
        };
-
+       char **devices;
        FILE *fp;
        struct mntent *mnt;
-       int rc = 0;
+       int rc = 1;
+
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
 
        logging_init("fstab_id");
+       udev_set_log_fn(udev, log_fn);
 
        while (1) {
                int option;
 
-               option = getopt_long(argc, argv, "dr:xh", options, NULL);
+               option = getopt_long(argc, argv, "dxh", options, NULL);
                if (option == -1)
                        break;
 
                switch (option) {
-               case 'r':
-                       strlcpy(root, optarg, sizeof(root));
-                       break;
                case 'd':
                        debug = 1;
+                       if (udev_get_log_priority(udev) < LOG_INFO)
+                               udev_set_log_priority(udev, LOG_INFO);
                        break;
                case 'h':
                        printf("Usage: fstab_id [OPTIONS] name [...]\n"
                               "  --export        print environment keys\n"
-                              "  --root          device node root (default /dev)\n"
                               "  --debug         debug to stderr\n"
                               "  --help          print this help text\n\n");
+                       goto exit;
+               case 'x':
+                       break;
                default:
-                       rc = 1;
+                       rc = 2;
                        goto exit;
                }
        }
@@ -129,14 +115,14 @@ int main(int argc, char *argv[])
        devices = &argv[optind];
        if (devices[0] == NULL) {
                fprintf(stderr, "error: missing device(s) to match\n");
-               rc = 2;
+               rc = 3;
                goto exit;
        }
 
        fp = setmntent ("/etc/fstab", "r");
        if (fp == NULL) {
                fprintf(stderr, "error: opening fstab: %s\n", strerror(errno));
-               rc = 2;
+               rc = 4;
                goto exit;
        }
 
@@ -145,6 +131,8 @@ int main(int argc, char *argv[])
                if (mnt == NULL)
                        break;
 
+               info(udev, "found '%s'@'%s'\n", mnt->mnt_fsname, mnt->mnt_dir);
+
                /* skip root device */
                if (strcmp(mnt->mnt_dir, "/") == 0)
                        continue;
@@ -165,8 +153,9 @@ int main(int argc, char *argv[])
                                pos[0] = '\0';
                                label = str;
                        }
-                       if (matches_device_list(str)) {
-                               print_fstab_entry(mnt);
+                       if (matches_device_list(udev, devices, str)) {
+                               print_fstab_entry(udev, mnt);
+                               rc = 0;
                                break;
                        }
                        continue;
@@ -188,26 +177,28 @@ int main(int argc, char *argv[])
                                pos[0] = '\0';
                                uuid = str;
                        }
-                       if (matches_device_list(str)) {
-                               print_fstab_entry(mnt);
+                       if (matches_device_list(udev, devices, str)) {
+                               print_fstab_entry(udev, mnt);
+                               rc = 0;
                                break;
                        }
                        continue;
                }
 
                /* only devices */
-               if (strncmp(mnt->mnt_fsname, root, strlen(root)) != 0)
+               if (strncmp(mnt->mnt_fsname, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) != 0)
                        continue;
 
-               if (matches_device_list(&mnt->mnt_fsname[strlen(root)+1])) {
-                       print_fstab_entry(mnt);
+               if (matches_device_list(udev, devices, &mnt->mnt_fsname[strlen(udev_get_dev_path(udev))+1])) {
+                       print_fstab_entry(udev, mnt);
+                       rc = 0;
                        break;
                }
        }
-
        endmntent(fp);
 
 exit:
+       udev_unref(udev);
        logging_close();
        return rc;
 }
index 7f890b0..fc5c477 100644 (file)
@@ -4,7 +4,8 @@ udevhome_PROGRAMS = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 scsi_id_SOURCES = \
        scsi_id.c \
@@ -12,6 +13,9 @@ scsi_id_SOURCES = \
        scsi.h \
        scsi_id.h \
        bsg.h \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
+       ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 23bcb94..d692a86 100644 (file)
@@ -64,30 +64,12 @@ static char model_str[64];
 static char revision_str[16];
 static char type_str[16];
 
-#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
 
 static void set_str(char *to, const char *from, size_t count)
 {
@@ -215,7 +197,8 @@ static int argc_count(char *opts)
  *
  * vendor and model can end in '\n'.
  */
-static int get_file_options(const char *vendor, const char *model,
+static int get_file_options(struct udev *udev,
+                           const char *vendor, const char *model,
                            int *argc, char ***newargv)
 {
        char *buffer;
@@ -227,14 +210,14 @@ static int get_file_options(const char *vendor, const char *model,
        int c;
        int retval = 0;
 
-       dbg("vendor='%s'; model='%s'\n", vendor, model);
+       dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
        fd = fopen(config_file, "r");
        if (fd == NULL) {
-               dbg("can't open %s\n", config_file);
+               dbg(udev, "can't open %s\n", config_file);
                if (errno == ENOENT) {
                        return 1;
                } else {
-                       err("can't open %s: %s\n", config_file, strerror(errno));
+                       err(udev, "can't open %s: %s\n", config_file, strerror(errno));
                        return -1;
                }
        }
@@ -246,7 +229,7 @@ static int get_file_options(const char *vendor, const char *model,
         */
        buffer = malloc(MAX_BUFFER_LEN);
        if (!buffer) {
-               err("can't allocate memory\n");
+               err(udev, "can't allocate memory\n");
                return -1;
        }
 
@@ -260,7 +243,7 @@ static int get_file_options(const char *vendor, const char *model,
                        break;
                lineno++;
                if (buf[strlen(buffer) - 1] != '\n') {
-                       err("Config file line %d too long\n", lineno);
+                       err(udev, "Config file line %d too long\n", lineno);
                        break;
                }
 
@@ -312,7 +295,7 @@ static int get_file_options(const char *vendor, const char *model,
                 * Only allow: [vendor=foo[,model=bar]]options=stuff
                 */
                if (!options_in || (!vendor_in && model_in)) {
-                       err("Error parsing config file line %d '%s'\n", lineno, buffer);
+                       err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
                        retval = -1;
                        break;
                }
@@ -350,7 +333,7 @@ static int get_file_options(const char *vendor, const char *model,
                        c = argc_count(buffer) + 2;
                        *newargv = calloc(c, sizeof(**newargv));
                        if (!*newargv) {
-                               err("can't allocate memory\n");
+                               err(udev, "can't allocate memory\n");
                                retval = -1;
                        } else {
                                *argc = c;
@@ -375,7 +358,8 @@ static int get_file_options(const char *vendor, const char *model,
        return retval;
 }
 
-static int set_options(int argc, char **argv, const char *short_opts,
+static int set_options(struct udev *udev,
+                      int argc, char **argv, const char *short_opts,
                       char *maj_min_dev)
 {
        int option;
@@ -392,9 +376,9 @@ static int set_options(int argc, char **argv, const char *short_opts,
                        break;
 
                if (optarg)
-                       dbg("option '%c' arg '%s'\n", option, optarg);
+                       dbg(udev, "option '%c' arg '%s'\n", option, optarg);
                else
-                       dbg("option '%c'\n", option);
+                       dbg(udev, "option '%c'\n", option);
 
                switch (option) {
                case 'b':
@@ -443,7 +427,7 @@ static int set_options(int argc, char **argv, const char *short_opts,
                        } else if (strcmp(optarg, "pre-spc3-83") == 0) {
                                default_page_code = PAGE_83_PRE_SPC3; 
                        } else {
-                               err("Unknown page code '%s'\n", optarg);
+                               err(udev, "Unknown page code '%s'\n", optarg);
                                return -1;
                        }
                        break;
@@ -451,7 +435,7 @@ static int set_options(int argc, char **argv, const char *short_opts,
                case 's':
                        sg_version = atoi(optarg);
                        if (sg_version < 3 || sg_version > 4) {
-                               err("Unknown SG version '%s'\n", optarg);
+                               err(udev, "Unknown SG version '%s'\n", optarg);
                                return -1;
                        }
                        break;
@@ -485,7 +469,8 @@ static int set_options(int argc, char **argv, const char *short_opts,
        return 0;
 }
 
-static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
+static int per_dev_options(struct udev *udev,
+                          struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
 {
        int retval;
        int newargc;
@@ -495,7 +480,7 @@ static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *
        *good_bad = all_good;
        *page_code = default_page_code;
 
-       retval = get_file_options(vendor_str, model_str, &newargc, &newargv);
+       retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
 
        optind = 1; /* reset this global extern */
        while (retval == 0) {
@@ -504,9 +489,9 @@ static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *
                        break;
 
                if (optarg)
-                       dbg("option '%c' arg '%s'\n", option, optarg);
+                       dbg(udev, "option '%c' arg '%s'\n", option, optarg);
                else
-                       dbg("option '%c'\n", option);
+                       dbg(udev, "option '%c'\n", option);
 
                switch (option) {
                case 'b':
@@ -525,13 +510,13 @@ static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *
                        } else if (strcmp(optarg, "pre-spc3-83") == 0) {
                                *page_code = PAGE_83_PRE_SPC3; 
                        } else {
-                               err("Unknown page code '%s'\n", optarg);
+                               err(udev, "Unknown page code '%s'\n", optarg);
                                retval = -1;
                        }
                        break;
 
                default:
-                       err("Unknown or bad option '%c' (0x%x)\n", option, option);
+                       err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
                        retval = -1;
                        break;
                }
@@ -544,13 +529,13 @@ static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *
        return retval;
 }
 
-static int set_inq_values(struct scsi_id_device *dev_scsi, const char *path)
+static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
 {
        int retval;
 
        dev_scsi->use_sg = sg_version;
 
-       retval = scsi_std_inquiry(dev_scsi, path);
+       retval = scsi_std_inquiry(udev, dev_scsi, path);
        if (retval)
                return retval;
 
@@ -594,7 +579,7 @@ static void format_serial(char *serial)
  * memory etc. return 2, and return 1 for expected cases (like broken
  * device found) that do not print an id.
  */
-static int scsi_id(char *maj_min_dev)
+static int scsi_id(struct udev *udev, char *maj_min_dev)
 {
        int retval;
        struct scsi_id_device dev_scsi;
@@ -602,15 +587,16 @@ static int scsi_id(char *maj_min_dev)
        int page_code;
        char serial_short[MAX_SERIAL_LEN] = "";
 
-       set_inq_values(&dev_scsi, maj_min_dev);
+       set_inq_values(udev, &dev_scsi, maj_min_dev);
 
        /* get per device (vendor + model) options from the config file */
-       retval = per_dev_options(&dev_scsi, &good_dev, &page_code);
+       retval = per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
        dbg("per dev options: good %d; page code 0x%x\n", good_dev, page_code);
 
        if (!good_dev) {
                retval = 1;
-       } else if (scsi_get_serial(&dev_scsi, maj_min_dev, page_code,
+       } else if (scsi_get_serial(udev,
+                                  &dev_scsi, maj_min_dev, page_code,
                                   serial_short, MAX_SERIAL_LEN)) {
                retval = 1;
        } else {
@@ -642,25 +628,30 @@ static int scsi_id(char *maj_min_dev)
 
 int main(int argc, char **argv)
 {
+       struct udev *udev;
        int retval = 0;
        char maj_min_dev[MAX_PATH_LEN];
        int newargc;
        char **newargv;
 
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
        logging_init("scsi_id");
-       dbg("argc is %d\n", argc);
+       udev_set_log_fn(udev, log_fn);
 
        /*
         * Get config file options.
         */
        newargv = NULL;
-       retval = get_file_options(NULL, NULL, &newargc, &newargv);
+       retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
        if (retval < 0) {
                retval = 1;
                goto exit;
        }
        if (newargv && (retval == 0)) {
-               if (set_options(newargc, newargv, short_options, maj_min_dev) < 0) {
+               if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
                        retval = 2;
                        goto exit;
                }
@@ -670,18 +661,19 @@ int main(int argc, char **argv)
        /*
         * Get command line options (overriding any config file settings).
         */
-       if (set_options(argc, argv, short_options, maj_min_dev) < 0)
+       if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
                exit(1);
 
        if (!dev_specified) {
-               err("no device specified\n");
+               err(udev, "no device specified\n");
                retval = 1;
                goto exit;
        }
 
-       retval = scsi_id(maj_min_dev);
+       retval = scsi_id(udev, maj_min_dev);
 
 exit:
+       udev_unref(udev);
        logging_close();
        return retval;
 }
index 680c39d..48e4a58 100644 (file)
@@ -40,8 +40,8 @@ struct scsi_id_device {
        int use_sg;
 };
 
-extern int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname);
-extern int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
+extern int scsi_std_inquiry(struct udev *udev, struct scsi_id_device *dev_scsi, const char *devname);
+extern int scsi_get_serial (struct udev *udev, struct scsi_id_device *dev_scsi, const char *devname,
                            int page_code, char *serial_short, int len);
 
 /*
index 104db3d..757f41d 100644 (file)
@@ -89,7 +89,8 @@ static const char hex_str[]="0123456789abcdef";
 #define SG_ERR_CAT_SENSE               98      /* Something else in the sense buffer */
 #define SG_ERR_CAT_OTHER               99      /* Some other error/warning */
 
-static int sg_err_category_new(int scsi_status, int msg_status, int
+static int sg_err_category_new(struct udev *udev,
+                              int scsi_status, int msg_status, int
                               host_status, int driver_status, const
                               unsigned char *sense_buffer, int sb_len)
 {
@@ -143,22 +144,24 @@ static int sg_err_category_new(int scsi_status, int msg_status, int
        return SG_ERR_CAT_OTHER;
 }
 
-static int sg_err_category3(struct sg_io_hdr *hp)
+static int sg_err_category3(struct udev *udev, struct sg_io_hdr *hp)
 {
-       return sg_err_category_new(hp->status, hp->msg_status,
+       return sg_err_category_new(udev,
+                                  hp->status, hp->msg_status,
                                   hp->host_status, hp->driver_status,
                                   hp->sbp, hp->sb_len_wr);
 }
 
-static int sg_err_category4(struct sg_io_v4 *hp)
+static int sg_err_category4(struct udev *udev, struct sg_io_v4 *hp)
 {
-       return sg_err_category_new(hp->device_status, 0,
+       return sg_err_category_new(udev, hp->device_status, 0,
                                   hp->transport_status, hp->driver_status,
                                   (unsigned char *)hp->response,
                                   hp->response_len);
 }
 
-static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
+static int scsi_dump_sense(struct udev *udev,
+                          struct scsi_id_device *dev_scsi,
                           unsigned char *sense_buffer, int sb_len)
 {
        int s;
@@ -184,10 +187,10 @@ static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
         * we'll retry the command.
         */
 
-       dbg("got check condition\n");
+       dbg(udev, "got check condition\n");
 
        if (sb_len < 1) {
-               info("%s: sense buffer empty\n", dev_scsi->kernel);
+               info(udev, "%s: sense buffer empty\n", dev_scsi->kernel);
                return -1;
        }
 
@@ -200,7 +203,7 @@ static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
                 */
                s = sense_buffer[7] + 8;
                if (sb_len < s) {
-                       info("%s: sense buffer too small %d bytes, %d bytes too short\n",
+                       info(udev, "%s: sense buffer too small %d bytes, %d bytes too short\n",
                            dev_scsi->kernel, sb_len, s - sb_len);
                        return -1;
                }
@@ -211,7 +214,7 @@ static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
                                /*
                                 * Possible?
                                 */
-                               info("%s: sense result too" " small %d bytes\n",
+                               info(udev, "%s: sense result too" " small %d bytes\n",
                                    dev_scsi->kernel, s);
                                return -1;
                        }
@@ -223,85 +226,88 @@ static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
                        asc = sense_buffer[2];
                        ascq = sense_buffer[3];
                } else {
-                       info("%s: invalid sense code 0x%x\n",
+                       info(udev, "%s: invalid sense code 0x%x\n",
                            dev_scsi->kernel, code);
                        return -1;
                }
-               info("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x\n",
+               info(udev, "%s: sense key 0x%x ASC 0x%x ASCQ 0x%x\n",
                    dev_scsi->kernel, sense_key, asc, ascq);
        } else {
                if (sb_len < 4) {
-                       info("%s: sense buffer too small %d bytes, %d bytes too short\n",
+                       info(udev, "%s: sense buffer too small %d bytes, %d bytes too short\n",
                            dev_scsi->kernel, sb_len, 4 - sb_len);
                        return -1;
                }
 
                if (sense_buffer[0] < 15)
-                       info("%s: old sense key: 0x%x\n", dev_scsi->kernel, sense_buffer[0] & 0x0f);
+                       info(udev, "%s: old sense key: 0x%x\n", dev_scsi->kernel, sense_buffer[0] & 0x0f);
                else
-                       info("%s: sense = %2x %2x\n",
+                       info(udev, "%s: sense = %2x %2x\n",
                            dev_scsi->kernel, sense_buffer[0], sense_buffer[2]);
-               info("%s: non-extended sense class %d code 0x%0x\n",
+               info(udev, "%s: non-extended sense class %d code 0x%0x\n",
                    dev_scsi->kernel, sense_class, code);
 
        }
 
 #ifdef DUMP_SENSE
        for (i = 0, j = 0; (i < s) && (j < 254); i++) {
-               dbg("i %d, j %d\n", i, j);
+               dbg(udev, "i %d, j %d\n", i, j);
                out_buffer[j++] = hex_str[(sense_buffer[i] & 0xf0) >> 4];
                out_buffer[j++] = hex_str[sense_buffer[i] & 0x0f];
                out_buffer[j++] = ' ';
        }
        out_buffer[j] = '\0';
-       info("%s: sense dump:\n", dev_scsi->kernel);
-       info("%s: %s\n", dev_scsi->kernel, out_buffer);
+       info(udev, "%s: sense dump:\n", dev_scsi->kernel);
+       info(udev, "%s: %s\n", dev_scsi->kernel, out_buffer);
 
 #endif
        return -1;
 }
 
-static int scsi_dump(struct scsi_id_device *dev_scsi, struct sg_io_hdr *io)
+static int scsi_dump(struct udev *udev,
+                    struct scsi_id_device *dev_scsi, struct sg_io_hdr *io)
 {
        if (!io->status && !io->host_status && !io->msg_status &&
            !io->driver_status) {
                /*
                 * Impossible, should not be called.
                 */
-               info("%s: called with no error\n", __FUNCTION__);
+               info(udev, "%s: called with no error\n", __FUNCTION__);
                return -1;
        }
 
-       info("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x\n",
+       info(udev, "%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x\n",
            dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status);
        if (io->status == SCSI_CHECK_CONDITION)
-               return scsi_dump_sense(dev_scsi, io->sbp, io->sb_len_wr);
+               return scsi_dump_sense(udev, dev_scsi, io->sbp, io->sb_len_wr);
        else
                return -1;
 }
 
-static int scsi_dump_v4(struct scsi_id_device *dev_scsi, struct sg_io_v4 *io)
+static int scsi_dump_v4(struct udev *udev,
+                       struct scsi_id_device *dev_scsi, struct sg_io_v4 *io)
 {
        if (!io->device_status && !io->transport_status &&
            !io->driver_status) {
                /*
                 * Impossible, should not be called.
                 */
-               info("%s: called with no error\n", __FUNCTION__);
+               info(udev, "%s: called with no error\n", __FUNCTION__);
                return -1;
        }
 
-       info("%s: sg_io failed status 0x%x 0x%x 0x%x\n",
+       info(udev, "%s: sg_io failed status 0x%x 0x%x 0x%x\n",
            dev_scsi->kernel, io->driver_status, io->transport_status,
             io->device_status);
        if (io->device_status == SCSI_CHECK_CONDITION)
-               return scsi_dump_sense(dev_scsi, (unsigned char *)io->response,
+               return scsi_dump_sense(udev, dev_scsi, (unsigned char *)io->response,
                                       io->response_len);
        else
                return -1;
 }
 
-static int scsi_inquiry(struct scsi_id_device *dev_scsi, int fd,
+static int scsi_inquiry(struct udev *udev,
+                       struct scsi_id_device *dev_scsi, int fd,
                        unsigned char evpd, unsigned char page,
                        unsigned char *buf, unsigned int buflen)
 {
@@ -313,12 +319,12 @@ static int scsi_inquiry(struct scsi_id_device *dev_scsi, int fd,
        int retry = 3; /* rather random */
 
        if (buflen > SCSI_INQ_BUFF_LEN) {
-               info("buflen %d too long\n", buflen);
+               info(udev, "buflen %d too long\n", buflen);
                return -1;
        }
 
 resend:
-       dbg("%s evpd %d, page 0x%x\n", dev_scsi->kernel, evpd, page);
+       dbg(udev, "%s evpd %d, page 0x%x\n", dev_scsi->kernel, evpd, page);
 
        if (dev_scsi->use_sg == 4) {
                struct sg_io_v4 io_v4;
@@ -356,14 +362,14 @@ resend:
                        dev_scsi->use_sg = 3;
                        goto resend;
                }
-               info("%s: ioctl failed: %s\n", dev_scsi->kernel, strerror(errno));
+               info(udev, "%s: ioctl failed: %s\n", dev_scsi->kernel, strerror(errno));
                goto error;
        }
 
        if (dev_scsi->use_sg == 4)
-               retval = sg_err_category4(io_buf);
+               retval = sg_err_category4(udev, io_buf);
        else
-               retval = sg_err_category3(io_buf);
+               retval = sg_err_category3(udev, io_buf);
 
        switch (retval) {
                case SG_ERR_CAT_NOTSUPPORTED:
@@ -376,16 +382,16 @@ resend:
 
                default:
                        if (dev_scsi->use_sg == 4)
-                               retval = scsi_dump_v4(dev_scsi, io_buf);
+                               retval = scsi_dump_v4(udev, dev_scsi, io_buf);
                        else
-                               retval = scsi_dump(dev_scsi, io_buf);
+                               retval = scsi_dump(udev, dev_scsi, io_buf);
        }
 
        if (!retval) {
                retval = buflen;
        } else if (retval > 0) {
                if (--retry > 0) {
-                       dbg("%s: Retrying ...\n", dev_scsi->kernel);
+                       dbg(udev, "%s: Retrying ...\n", dev_scsi->kernel);
                        goto resend;
                }
                retval = -1;
@@ -393,29 +399,30 @@ resend:
 
 error:
        if (retval < 0)
-               info("%s: Unable to get INQUIRY vpd %d page 0x%x.\n",
+               info(udev, "%s: Unable to get INQUIRY vpd %d page 0x%x.\n",
                    dev_scsi->kernel, evpd, page);
 
        return retval;
 }
 
 /* Get list of supported EVPD pages */
-static int do_scsi_page0_inquiry(struct scsi_id_device *dev_scsi, int fd,
+static int do_scsi_page0_inquiry(struct udev *udev,
+                                struct scsi_id_device *dev_scsi, int fd,
                                 unsigned char *buffer, unsigned int len)
 {
        int retval;
 
        memset(buffer, 0, len);
-       retval = scsi_inquiry(dev_scsi, fd, 1, 0x0, buffer, len);
+       retval = scsi_inquiry(udev, dev_scsi, fd, 1, 0x0, buffer, len);
        if (retval < 0)
                return 1;
 
        if (buffer[1] != 0) {
-               info("%s: page 0 not available.\n", dev_scsi->kernel);
+               info(udev, "%s: page 0 not available.\n", dev_scsi->kernel);
                return 1;
        }
        if (buffer[3] > len) {
-               info("%s: page 0 buffer too long %d\n", dev_scsi->kernel,  buffer[3]);
+               info(udev, "%s: page 0 buffer too long %d\n", dev_scsi->kernel,  buffer[3]);
                return 1;
        }
 
@@ -432,7 +439,7 @@ static int do_scsi_page0_inquiry(struct scsi_id_device *dev_scsi, int fd,
                 * invalid.
                 */
                if (!strncmp((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) {
-                       info("%s: invalid page0 data\n", dev_scsi->kernel);
+                       info(udev, "%s: invalid page0 data\n", dev_scsi->kernel);
                        return 1;
                }
        }
@@ -443,7 +450,8 @@ static int do_scsi_page0_inquiry(struct scsi_id_device *dev_scsi, int fd,
  * The caller checks that serial is long enough to include the vendor +
  * model.
  */
-static int prepend_vendor_model(struct scsi_id_device *dev_scsi, char *serial)
+static int prepend_vendor_model(struct udev *udev,
+                               struct scsi_id_device *dev_scsi, char *serial)
 {
        int ind;
 
@@ -459,7 +467,7 @@ static int prepend_vendor_model(struct scsi_id_device *dev_scsi, char *serial)
         * above, ind will never be too large.
         */
        if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) {
-               info("%s: expected length %d, got length %d\n",
+               info(udev, "%s: expected length %d, got length %d\n",
                     dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind);
                return 1;
        }
@@ -470,7 +478,8 @@ static int prepend_vendor_model(struct scsi_id_device *dev_scsi, char *serial)
  * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
  * serial number.
  **/
-static int check_fill_0x83_id(struct scsi_id_device *dev_scsi,
+static int check_fill_0x83_id(struct udev *udev,
+                             struct scsi_id_device *dev_scsi,
                              unsigned char *page_83,
                              const struct scsi_id_search_values
                              *id_search, char *serial, char *serial_short, int max_len)
@@ -517,7 +526,7 @@ static int check_fill_0x83_id(struct scsi_id_device *dev_scsi,
                len += VENDOR_LENGTH + MODEL_LENGTH;
 
        if (max_len < len) {
-               info("%s: length %d too short - need %d\n",
+               info(udev, "%s: length %d too short - need %d\n",
                    dev_scsi->kernel, max_len, len);
                return 1;
        }
@@ -531,8 +540,8 @@ static int check_fill_0x83_id(struct scsi_id_device *dev_scsi,
         * included in the identifier.
         */
        if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
-               if (prepend_vendor_model(dev_scsi, &serial[1]) < 0) {
-                       dbg("prepend failed\n");
+               if (prepend_vendor_model(udev, dev_scsi, &serial[1]) < 0) {
+                       dbg(udev, "prepend failed\n");
                        return 1;
                }
 
@@ -561,14 +570,15 @@ static int check_fill_0x83_id(struct scsi_id_device *dev_scsi,
 }
 
 /* Extract the raw binary from VPD 0x83 pre-SPC devices */
-static int check_fill_0x83_prespc3(struct scsi_id_device *dev_scsi,
+static int check_fill_0x83_prespc3(struct udev *udev,
+                                  struct scsi_id_device *dev_scsi,
                                   unsigned char *page_83,
                                   const struct scsi_id_search_values
                                   *id_search, char *serial, char *serial_short, int max_len)
 {
        int i, j;
 
-       dbg("using pre-spc3-83 for %s\n", dev_scsi->kernel);
+       dbg(udev, "using pre-spc3-83 for %s\n", dev_scsi->kernel);
        serial[0] = hex_str[id_search->id_type];
        /* serial has been memset to zero before */
        j = strlen(serial);     /* j = 1; */
@@ -583,7 +593,8 @@ static int check_fill_0x83_prespc3(struct scsi_id_device *dev_scsi,
 
 
 /* Get device identification VPD page */
-static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
+static int do_scsi_page83_inquiry(struct udev *udev,
+                                 struct scsi_id_device *dev_scsi, int fd,
                                  char *serial, char *serial_short, int len)
 {
        int retval;
@@ -591,13 +602,13 @@ static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
        unsigned char page_83[SCSI_INQ_BUFF_LEN];
 
        memset(page_83, 0, SCSI_INQ_BUFF_LEN);
-       retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83,
+       retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83,
                              SCSI_INQ_BUFF_LEN);
        if (retval < 0)
                return 1;
 
        if (page_83[1] != PAGE_83) {
-               info("%s: Invalid page 0x83\n", dev_scsi->kernel);
+               info(udev, "%s: Invalid page 0x83\n", dev_scsi->kernel);
                return 1;
        }
        
@@ -631,7 +642,8 @@ static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
         */
        
        if (page_83[6] != 0) 
-               return check_fill_0x83_prespc3(dev_scsi, page_83, id_search_list,
+               return check_fill_0x83_prespc3(udev,
+                                              dev_scsi, page_83, id_search_list,
                                               serial, serial_short, len);
 
        /*
@@ -645,21 +657,22 @@ static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
                 * one or a small number of descriptors.
                 */
                for (j = 4; j <= (unsigned int)page_83[3] + 3; j += page_83[j + 3] + 4) {
-                       retval = check_fill_0x83_id(dev_scsi, &page_83[j],
+                       retval = check_fill_0x83_id(udev,
+                                                   dev_scsi, &page_83[j],
                                                    &id_search_list[id_ind],
                                                    serial, serial_short, len);
-                       dbg("%s id desc %d/%d/%d\n", dev_scsi->kernel,
+                       dbg(udev, "%s id desc %d/%d/%d\n", dev_scsi->kernel,
                                id_search_list[id_ind].id_type,
                                id_search_list[id_ind].naa_type,
                                id_search_list[id_ind].code_set);
                        if (!retval) {
-                               dbg("  used\n");
+                               dbg(udev, "  used\n");
                                return retval;
                        } else if (retval < 0) {
-                               dbg("  failed\n");
+                               dbg(udev, "  failed\n");
                                return retval;
                        } else {
-                               dbg("  not used\n");
+                               dbg(udev, "  not used\n");
                        }
                }
        }
@@ -673,20 +686,21 @@ static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
  * Return the hard coded error code value 2 if the page 83 reply is not
  * conformant to the SCSI-2 format.
  */
-static int do_scsi_page83_prespc3_inquiry(struct scsi_id_device *dev_scsi, int fd,
-                                         char *serial, char *serial_short, int len)
+static int do_scsi_page83_prespc3_inquiry(struct udev *udev,
+                                         struct scsi_id_device *dev_scsi, int fd,
+                                         char *serial, char *serial_short, int len)
 {
        int retval;
        int i, j;
        unsigned char page_83[SCSI_INQ_BUFF_LEN];
 
        memset(page_83, 0, SCSI_INQ_BUFF_LEN);
-       retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN);
+       retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN);
        if (retval < 0)
                return 1;
 
        if (page_83[1] != PAGE_83) {
-               info("%s: Invalid page 0x83\n", dev_scsi->kernel);
+               info(udev, "%s: Invalid page 0x83\n", dev_scsi->kernel);
                return 1;
        }
        /*
@@ -730,12 +744,13 @@ static int do_scsi_page83_prespc3_inquiry(struct scsi_id_device *dev_scsi, int f
                serial[j++] = hex_str[page_83[i] & 0x0f];
                i++;
        }
-       dbg("using pre-spc3-83 for %s\n", dev_scsi->kernel);
+       dbg(udev, "using pre-spc3-83 for %s\n", dev_scsi->kernel);
        return 0;
 }
 
 /* Get unit serial number VPD page */
-static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
+static int do_scsi_page80_inquiry(struct udev *udev,
+                                 struct scsi_id_device *dev_scsi, int fd,
                                  char *serial, char *serial_short, int max_len)
 {
        int retval;
@@ -745,18 +760,18 @@ static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
        unsigned char buf[SCSI_INQ_BUFF_LEN];
 
        memset(buf, 0, SCSI_INQ_BUFF_LEN);
-       retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN);
+       retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN);
        if (retval < 0)
                return retval;
 
        if (buf[1] != PAGE_80) {
-               info("%s: Invalid page 0x80\n", dev_scsi->kernel);
+               info(udev, "%s: Invalid page 0x80\n", dev_scsi->kernel);
                return 1;
        }
 
        len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3];
        if (max_len < len) {
-               info("%s: length %d too short - need %d\n",
+               info(udev, "%s: length %d too short - need %d\n",
                    dev_scsi->kernel, max_len, len);
                return 1;
        }
@@ -765,7 +780,7 @@ static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
         * specific type where we prepend '0' + vendor + model.
         */
        serial[0] = 'S';
-       ser_ind = prepend_vendor_model(dev_scsi, &serial[1]);
+       ser_ind = prepend_vendor_model(udev, dev_scsi, &serial[1]);
        if (ser_ind < 0)
                return 1;
        len = buf[3];
@@ -776,23 +791,24 @@ static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
        return 0;
 }
 
-int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname)
+int scsi_std_inquiry(struct udev *udev,
+                    struct scsi_id_device *dev_scsi, const char *devname)
 {
        int fd;
        unsigned char buf[SCSI_INQ_BUFF_LEN];
        struct stat statbuf;
        int err = 0;
 
-       dbg("opening %s\n", devname);
+       dbg(udev, "opening %s\n", devname);
        fd = open(devname, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
-               info("scsi_id: cannot open %s: %s\n",
+               info(udev, "scsi_id: cannot open %s: %s\n",
                     devname, strerror(errno));
                return 1;
        }
 
        if (fstat(fd, &statbuf) < 0) {
-               info("scsi_id: cannot stat %s: %s\n",
+               info(udev, "scsi_id: cannot stat %s: %s\n",
                     devname, strerror(errno));
                err = 2;
                goto out;
@@ -801,7 +817,7 @@ int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname)
                minor(statbuf.st_rdev));
 
        memset(buf, 0, SCSI_INQ_BUFF_LEN);
-       err = scsi_inquiry(dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN);
+       err = scsi_inquiry(udev, dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN);
        if (err < 0)
                goto out;
 
@@ -819,8 +835,9 @@ out:
        return err;
 }
 
-int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
-                    int page_code, char *serial_short, int len)
+int scsi_get_serial(struct udev *udev,
+                   struct scsi_id_device *dev_scsi, const char *devname,
+                   int page_code, char *serial_short, int len)
 {
        unsigned char page0[SCSI_INQ_BUFF_LEN];
        int fd = -1;
@@ -829,12 +846,12 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
        int retval;
 
        memset(dev_scsi->serial, 0, len);
-       dbg("opening %s\n", devname);
+       dbg(udev, "opening %s\n", devname);
        while (--cnt) {
                fd = open(devname, O_RDONLY | O_NONBLOCK);
                if (fd >= 0)
                        break;
-               info("%s: cannot open %s: %s\n", dev_scsi->kernel, devname, strerror(errno));
+               info(udev, "%s: cannot open %s: %s\n", dev_scsi->kernel, devname, strerror(errno));
                if (errno != EBUSY)
                        break;
                usleep(500000 + (rand() % 100000) );
@@ -843,7 +860,7 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
                return 1;
 
        if (page_code == PAGE_80) {
-               if (do_scsi_page80_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+               if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
                        retval = 1;
                        goto completed;
                } else  {
@@ -851,7 +868,7 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
                        goto completed;
                }
        } else if (page_code == PAGE_83) {
-               if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
                        retval = 1;
                        goto completed;
                } else  {
@@ -859,7 +876,7 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
                        goto completed;
                }
        } else if (page_code == PAGE_83_PRE_SPC3) {
-               retval = do_scsi_page83_prespc3_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len);
+               retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len);
                if (retval) {
                        /*
                         * Fallback to servicing a SPC-2/3 compliant page 83
@@ -867,7 +884,7 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
                         * conform to pre-SPC3 expectations.
                         */
                        if (retval == 2) {
-                               if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+                               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
                                        retval = 1;
                                        goto completed;
                                } else  {
@@ -884,7 +901,7 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
                        goto completed;
                }
        } else if (page_code != 0x00) {
-               info("%s: unsupported page code 0x%d\n", dev_scsi->kernel, page_code);
+               info(udev, "%s: unsupported page code 0x%d\n", dev_scsi->kernel, page_code);
                return 1;
        }
 
@@ -892,7 +909,7 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
         * Get page 0, the page of the pages. By default, try from best to
         * worst of supported pages: 0x83 then 0x80.
         */
-       if (do_scsi_page0_inquiry(dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) {
+       if (do_scsi_page0_inquiry(udev, dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) {
                /*
                 * Don't try anything else. Black list if a specific page
                 * should be used for this vendor+model, or maybe have an
@@ -902,11 +919,11 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
                goto completed;
        }
 
-       dbg("%s: Checking page0\n", dev_scsi->kernel);
+       dbg(udev, "%s: Checking page0\n", dev_scsi->kernel);
 
        for (ind = 4; ind <= page0[3] + 3; ind++)
                if (page0[ind] == PAGE_83)
-                       if (!do_scsi_page83_inquiry(dev_scsi, fd,
+                       if (!do_scsi_page83_inquiry(udev, dev_scsi, fd,
                                                    dev_scsi->serial, serial_short, len)) {
                                /*
                                 * Success
@@ -917,7 +934,7 @@ int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
 
        for (ind = 4; ind <= page0[3] + 3; ind++)
                if (page0[ind] == PAGE_80)
-                       if (!do_scsi_page80_inquiry(dev_scsi, fd,
+                       if (!do_scsi_page80_inquiry(udev, dev_scsi, fd,
                                                    dev_scsi->serial, serial_short, len)) {
                                /*
                                 * Success
index 0de004a..9d7e772 100644 (file)
@@ -4,10 +4,13 @@ udevhome_PROGRAMS = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 usb_id_SOURCES = \
        usb_id.c \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
        ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
index 92492c8..fb4c28a 100644 (file)
 #define MAX_SERIAL_LEN                 256
 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
 
-#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
 
 static char vendor_str[64];
 static char model_str[64];
@@ -219,7 +201,7 @@ static void set_scsi_type(char *to, const char *from, size_t len)
  * 6.) If the device supplies a serial number, this number
  *     is concatenated with the identification with an underscore '_'.
  */
-static int usb_id(const char *devpath)
+static int usb_id(struct udev *udev, const char *devpath)
 {
        struct sysfs_device *dev;
        struct sysfs_device *dev_interface;
@@ -231,38 +213,38 @@ static int usb_id(const char *devpath)
        dbg("devpath %s\n", devpath);
 
        /* get all usb specific information: dev_interface, if_class, dev_usb */
-       dev = sysfs_device_get(devpath);
+       dev = sysfs_device_get(udev, devpath);
        if (dev == NULL) {
-               err("unable to access '%s'\n", devpath);
+               err(udev, "unable to access '%s'\n", devpath);
                return 1;
        }
 
        /* usb interface directory */
-       dev_interface = sysfs_device_get_parent_with_subsystem(dev, "usb");
+       dev_interface = sysfs_device_get_parent_with_subsystem(udev, dev, "usb");
        if (dev_interface == NULL) {
-               info("unable to access usb_interface device of '%s'\n", devpath);
+               info(udev, "unable to access usb_interface device of '%s'\n", devpath);
                return 1;
        }
 
-       if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
+       if_class = sysfs_attr_get_value(udev, dev_interface->devpath, "bInterfaceClass");
        if (!if_class) {
-               info("%s: cannot get bInterfaceClass attribute\n", dev_interface->kernel);
+               info(udev, "%s: cannot get bInterfaceClass attribute\n", dev_interface->kernel);
                return 1;
        }
        if_class_num = strtoul(if_class, NULL, 16);
        if (if_class_num == 8) {
-               if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
+               if_subclass = sysfs_attr_get_value(udev, dev_interface->devpath, "bInterfaceSubClass");
                if (if_subclass != NULL)
                        protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
        } else
                set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
 
-       info("%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
+       info(udev, "%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
 
        /* usb device directory */
-       dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
+       dev_usb = sysfs_device_get_parent_with_subsystem(udev, dev_interface, "usb");
        if (!dev_usb) {
-               info("unable to find parent 'usb' device of '%s'\n", devpath);
+               info(udev, "unable to find parent 'usb' device of '%s'\n", devpath);
                return 1;
        }
 
@@ -273,41 +255,41 @@ static int usb_id(const char *devpath)
                int host, bus, target, lun;
 
                /* get scsi device */
-               dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
+               dev_scsi = sysfs_device_get_parent_with_subsystem(udev, dev, "scsi");
                if (dev_scsi == NULL) {
-                       info("unable to find parent 'scsi' device of '%s'\n", devpath);
+                       info(udev, "unable to find parent 'scsi' device of '%s'\n", devpath);
                        goto fallback;
                }
                if (sscanf(dev_scsi->kernel, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
-                       info("invalid scsi device '%s'\n", dev_scsi->kernel);
+                       info(udev, "invalid scsi device '%s'\n", dev_scsi->kernel);
                        goto fallback;
                }
 
                /* Generic SPC-2 device */
-               scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
+               scsi_vendor = sysfs_attr_get_value(udev, dev_scsi->devpath, "vendor");
                if (!scsi_vendor) {
-                       info("%s: cannot get SCSI vendor attribute\n", dev_scsi->kernel);
+                       info(udev, "%s: cannot get SCSI vendor attribute\n", dev_scsi->kernel);
                        goto fallback;
                }
                set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
 
-               scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
+               scsi_model = sysfs_attr_get_value(udev, dev_scsi->devpath, "model");
                if (!scsi_model) {
-                       info("%s: cannot get SCSI model attribute\n", dev_scsi->kernel);
+                       info(udev, "%s: cannot get SCSI model attribute\n", dev_scsi->kernel);
                        goto fallback;
                }
                set_str(model_str, scsi_model, sizeof(model_str)-1);
 
-               scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
+               scsi_type = sysfs_attr_get_value(udev, dev_scsi->devpath, "type");
                if (!scsi_type) {
-                       info("%s: cannot get SCSI type attribute\n", dev_scsi->kernel);
+                       info(udev, "%s: cannot get SCSI type attribute\n", dev_scsi->kernel);
                        goto fallback;
                }
                set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
 
-               scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
+               scsi_rev = sysfs_attr_get_value(udev, dev_scsi->devpath, "rev");
                if (!scsi_rev) {
-                       info("%s: cannot get SCSI revision attribute\n", dev_scsi->kernel);
+                       info(udev, "%s: cannot get SCSI revision attribute\n", dev_scsi->kernel);
                        goto fallback;
                }
                set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
@@ -325,13 +307,13 @@ fallback:
                const char *usb_vendor = NULL;
 
                if (!use_num_info)
-                       usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer");
+                       usb_vendor = sysfs_attr_get_value(udev, dev_usb->devpath, "manufacturer");
 
                if (!usb_vendor)
-                       usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor");
+                       usb_vendor = sysfs_attr_get_value(udev, dev_usb->devpath, "idVendor");
 
                if (!usb_vendor) {
-                       info("No USB vendor information available\n");
+                       info(udev, "No USB vendor information available\n");
                        return 1;
                }
                set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
@@ -341,10 +323,10 @@ fallback:
                const char *usb_model = NULL;
 
                if (!use_num_info)
-                       usb_model = sysfs_attr_get_value(dev_usb->devpath, "product");
+                       usb_model = sysfs_attr_get_value(udev, dev_usb->devpath, "product");
 
                if (!usb_model)
-                       usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct");
+                       usb_model = sysfs_attr_get_value(udev, dev_usb->devpath, "idProduct");
 
                if (!usb_model) {
                        dbg("No USB model information available\n");
@@ -356,7 +338,7 @@ fallback:
        if (revision_str[0] == '\0') {
                const char *usb_rev;
 
-               usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
+               usb_rev = sysfs_attr_get_value(udev, dev_usb->devpath, "bcdDevice");
                if (usb_rev)
                        set_str(revision_str, usb_rev, sizeof(revision_str)-1);
        }
@@ -364,7 +346,7 @@ fallback:
        if (serial_str[0] == '\0') {
                const char *usb_serial;
 
-               usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
+               usb_serial = sysfs_attr_get_value(udev, dev_usb->devpath, "serial");
                if (usb_serial)
                        set_str(serial_str, usb_serial, sizeof(serial_str)-1);
        }
@@ -373,6 +355,7 @@ fallback:
 
 int main(int argc, char **argv)
 {
+       struct udev *udev;
        int retval = 0;
        const char *env;
        char devpath[MAX_PATH_LEN];
@@ -385,7 +368,12 @@ int main(int argc, char **argv)
                {}
        };
 
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
        logging_init("usb_id");
+       udev_set_log_fn(udev, log_fn);
        sysfs_init();
 
        while (1) {
@@ -430,7 +418,7 @@ int main(int argc, char **argv)
                strlcpy(devpath, argv[optind], sizeof(devpath));
        }
 
-       retval = usb_id(devpath);
+       retval = usb_id(udev, devpath);
 
        if (retval == 0) {
                char serial[256];
@@ -463,6 +451,7 @@ int main(int argc, char **argv)
        }
 
 exit:
+       udev_unref(udev);
        sysfs_cleanup();
        logging_close();
        return retval;
index bc1b262..fe9bef7 100644 (file)
@@ -7,10 +7,14 @@ udevhome_PROGRAMS = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 vol_id_SOURCES = \
        vol_id.c \
+       ../../udev/lib/libudev.h \
+       ../../udev/lib/libudev.c \
+       ../../udev/udev_sysfs.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index b3f810f..dcfb0bb 100644 (file)
 
 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
 
-#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
 
 static void vid_log(int priority, const char *file, int line, const char *format, ...)
 {
-#ifdef USE_LOG
-       char log_str[1024];
        va_list args;
 
        va_start(args, format);
-       vsnprintf(log_str, sizeof(log_str), format, args);
-       log_str[sizeof(log_str)-1] = '\0';
-       log_message(priority, "%s:%i %s", file, line, log_str);
+       log_fn(NULL, priority, file, line, NULL, format, args);
        va_end(args);
-#endif
        return;
 }
 
@@ -126,6 +103,7 @@ static int all_probers(volume_id_probe_fn_t probe_fn,
 
 int main(int argc, char *argv[])
 {
+       struct udev *udev;
        static const struct option options[] = {
                { "label", 0, NULL, 'l' },
                { "label-raw", 0, NULL, 'L' },
@@ -161,7 +139,11 @@ int main(int argc, char *argv[])
        int retval;
        int rc = 0;
 
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
        logging_init("vol_id");
+       udev_set_log_fn(udev, log_fn);
 
        /* hook in our debug into libvolume_id */
        volume_id_log_fn = vid_log;
@@ -219,7 +201,7 @@ int main(int argc, char *argv[])
 
        node = argv[optind];
        if (!node) {
-               err("no device\n");
+               err(udev, "no device\n");
                fprintf(stderr, "no device\n");
                rc = 1;
                goto exit;
@@ -240,7 +222,7 @@ int main(int argc, char *argv[])
 
        if (ioctl(fd, BLKGETSIZE64, &size) != 0)
                size = 0;
-       dbg("BLKGETSIZE64=%llu\n", (unsigned long long)size);
+       dbg(udev, "BLKGETSIZE64=%llu\n", (unsigned long long)size);
 
        /* try to drop all privileges before reading disk content */
        if (getuid() == 0) {
@@ -251,7 +233,7 @@ int main(int argc, char *argv[])
                        if (setgroups(0, NULL) != 0 ||
                            setgid(pw->pw_gid) != 0 ||
                            setuid(pw->pw_uid) != 0)
-                               info("unable to drop privileges: %s\n\n", strerror(errno));
+                               info(udev, "unable to drop privileges: %s\n\n", strerror(errno));
                }
        }
 
@@ -325,7 +307,7 @@ int main(int argc, char *argv[])
 exit:
        if (vid != NULL)
                volume_id_close(vid);
-
+       udev_unref(udev);
        logging_close();
        return rc;
 }
index 450c948..4a30bf9 100644 (file)
@@ -10,7 +10,8 @@ noinst_PROGRAMS = \
 
 AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
-       -DUDEV_PREFIX=\""$(udev_prefix)"\"
+       -DUDEV_PREFIX=\""$(udev_prefix)"\" \
+       -D_LIBUDEV_COMPILATION
 
 common_ldadd =
 
@@ -21,7 +22,6 @@ common_files = \
        udev_rules.h \
        udev_selinux.h \
        udev_sysdeps.h \
-       udev_config.c \
        udev_db.c \
        udev_device.c \
        udev_device_event.c \
@@ -32,7 +32,11 @@ common_files = \
        udev_sysfs.c \
        udev_utils.c \
        udev_utils_file.c \
-       udev_utils_string.c
+       udev_utils_string.c \
+       lib/libudev.h \
+       lib/libudev-private.h \
+       lib/libudev.c \
+       lib/libudev-utils.c
 
 
 if USE_SELINUX
index f1f5fb7..b7dd373 100644 (file)
@@ -33,7 +33,6 @@ libudev_la_SOURCES =\
        ../udev_utils_file.c \
        ../udev_sysfs.c \
        ../udev_device.c \
-       ../udev_config.c \
        ../udev_db.c \
        ../udev_sysdeps.c
 
index 4db23b2..66275f4 100644 (file)
@@ -3,6 +3,8 @@ udev_new
 udev_ref
 udev_unref
 udev_set_log_fn
+udev_get_log_priority
+udev_set_log_priority
 udev_get_sys_path
 udev_get_dev_path
 udev_device_new_from_devpath
index 629cadb..43ed2f3 100644 (file)
@@ -58,7 +58,7 @@ struct udev_device *device_init(struct udev *udev)
        udev_device->udev = udev;
        INIT_LIST_HEAD(&udev_device->link_list);
        INIT_LIST_HEAD(&udev_device->env_list);
-       log_info(udev_device->udev, "udev_device: %p created\n", udev_device);
+       info(udev_device->udev, "udev_device: %p created\n", udev_device);
        return udev_device;
 }
 
@@ -101,7 +101,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
        if (udev_device == NULL)
                return NULL;
 
-       udevice = udev_device_init();
+       udevice = udev_device_init(udev);
        if (udevice == NULL) {
                free(udev_device);
                return NULL;
@@ -109,13 +109,13 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
 
        /* resolve possible symlink to real path */
        strlcpy(path, devpath, sizeof(path));
-       sysfs_resolve_link(path, sizeof(path));
+       sysfs_resolve_link(udev, path, sizeof(path));
        device_set_devpath(udev_device, devpath);
-       log_info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
+       info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
 
        err = udev_db_get_device(udevice, path);
        if (err >= 0)
-               log_info(udev, "device %p filled with udev database data\n", udev_device);
+               info(udev, "device %p filled with udev database data\n", udev_device);
 
        if (udevice->name[0] != '\0')
                asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
@@ -126,11 +126,11 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
                strlcpy(name, udev_get_dev_path(udev), sizeof(name));
                strlcat(name, "/", sizeof(name));
                strlcat(name, name_loop->name, sizeof(name));
-               name_list_add(&udev_device->link_list, name, 0);
+               name_list_add(udev, &udev_device->link_list, name, 0);
        }
 
        list_for_each_entry(name_loop, &udevice->env_list, node)
-               name_list_add(&udev_device->env_list, name_loop->name, 0);
+               name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
 
        udev_device_cleanup(udevice);
        return udev_device;
@@ -185,9 +185,9 @@ void udev_device_unref(struct udev_device *udev_device)
        free(udev_device->syspath);
        free(udev_device->devname);
        free(udev_device->subsystem);
-       name_list_cleanup(&udev_device->link_list);
-       name_list_cleanup(&udev_device->env_list);
-       log_info(udev_device->udev, "udev_device: %p released\n", udev_device);
+       name_list_cleanup(udev_device->udev, &udev_device->link_list);
+       name_list_cleanup(udev_device->udev, &udev_device->env_list);
+       info(udev_device->udev, "udev_device: %p released\n", udev_device);
        free(udev_device);
 }
 
@@ -359,14 +359,14 @@ int device_set_devname(struct udev_device *udev_device, const char *devname)
 
 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
 {
-       if (name_list_add(&udev_device->link_list, devlink, 0) == NULL)
+       if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
                return -ENOMEM;
        return 0;
 }
 
 int device_add_property(struct udev_device *udev_device, const char *property)
 {
-       if (name_list_add(&udev_device->env_list, property, 0) == NULL)
+       if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
                return -ENOMEM;
        return 0;
 }
index e319866..6aa04d8 100644 (file)
@@ -58,8 +58,8 @@ static int devices_scan_subsystem(struct udev *udev,
                strlcpy(devpath, &path[len], sizeof(devpath));
                strlcat(devpath, "/", sizeof(devpath));
                strlcat(devpath, dent->d_name, sizeof(devpath));
-               sysfs_resolve_link(devpath, sizeof(devpath));
-               name_list_add(device_list, devpath, 1);
+               sysfs_resolve_link(udev, devpath, sizeof(devpath));
+               name_list_add(udev, device_list, devpath, 1);
        }
        closedir(dir);
        return 0;
@@ -101,7 +101,7 @@ static int devices_delay(struct udev *udev, const char *devpath)
 
        for (i = 0; delay_device_list[i] != NULL; i++) {
                if (strstr(devpath, delay_device_list[i]) != NULL) {
-                       log_info(udev, "delaying: %s\n", devpath);
+                       info(udev, "delaying: %s\n", devpath);
                        return 1;
                }
        }
@@ -158,7 +158,7 @@ int udev_devices_enumerate(struct udev *udev, const char *subsystem,
        INIT_LIST_HEAD(&device_list);
 
        /* if we have /sys/subsystem/, forget all the old stuff */
-       strlcpy(base, sysfs_path, sizeof(base));
+       strlcpy(base, udev_get_sys_path(udev), sizeof(base));
        strlcat(base, "/subsystem", sizeof(base));
        if (stat(base, &statbuf) == 0) {
                devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
index 92617e8..0984557 100644 (file)
@@ -86,13 +86,13 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
 
        udev_monitor->socket = socket(AF_LOCAL, SOCK_DGRAM, 0);
        if (udev_monitor->socket == -1) {
-               log_err(udev, "error getting socket: %s\n", strerror(errno));
+               err(udev, "error getting socket: %s\n", strerror(errno));
                free(udev_monitor);
                return NULL;
        }
 
        if (bind(udev_monitor->socket, (struct sockaddr *) &saddr, addrlen) < 0) {
-               log_err(udev, "bind failed: %s\n", strerror(errno));
+               err(udev, "bind failed: %s\n", strerror(errno));
                close(udev_monitor->socket);
                free(udev_monitor);
                return NULL;
@@ -100,7 +100,7 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
 
        /* enable receiving of the sender credentials */
        setsockopt(udev_monitor->socket, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
-       log_info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
+       info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
 
        return udev_monitor;
 }
@@ -138,7 +138,7 @@ void udev_monitor_unref(struct udev_monitor *udev_monitor)
        if (udev_monitor->refcount > 0)
                return;
        close(udev_monitor->socket);
-       log_info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
+       info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
        free(udev_monitor);
 }
 
@@ -212,32 +212,32 @@ struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor)
 
        if (recvmsg(udev_monitor->socket, &smsg, 0) < 0) {
                if (errno != EINTR)
-                       log_info(udev_monitor->udev, "unable to receive message");
+                       info(udev_monitor->udev, "unable to receive message");
                return NULL;
        }
        cmsg = CMSG_FIRSTHDR(&smsg);
        cred = (struct ucred *)CMSG_DATA (cmsg);
 
        if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
-               log_info(udev_monitor->udev, "no sender credentials received, message ignored");
+               info(udev_monitor->udev, "no sender credentials received, message ignored");
                return NULL;
        }
 
        if (cred->uid != 0) {
-               log_info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
+               info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
                return NULL;
        }
 
        /* skip header */
        bufpos = strlen(buf) + 1;
        if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
-               log_info(udev_monitor->udev, "invalid message length");
+               info(udev_monitor->udev, "invalid message length");
                return NULL;
        }
 
        /* check message header */
        if (strstr(buf, "@/") == NULL) {
-               log_info(udev_monitor->udev, "unrecognized message header");
+               info(udev_monitor->udev, "unrecognized message header");
                return NULL;
        }
 
index ffdf571..ef26ec7 100644 (file)
 #ifndef _LIBUDEV_PRIVATE_H_
 #define _LIBUDEV_PRIVATE_H_
 
+#include "config.h"
+
+#include <syslog.h>
 #include "libudev.h"
 #include "../udev.h"
 
 #ifdef USE_LOG
-#define log_dbg(udev, arg...) \
+#ifdef USE_DEBUG
+#define dbg(udev, arg...) \
        udev_log(udev, LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, ## arg)
+#else
+#define dbg(format, arg...) do { } while (0)
+#endif /* USE_DEBUG */
 
-#define log_info(udev, arg...) \
+#define info(udev, arg...) \
        udev_log(udev, LOG_INFO, __FILE__, __LINE__, __FUNCTION__, ## arg)
 
-#define log_err(udev, arg...) \
+#define err(udev, arg...) \
        udev_log(udev, LOG_ERR, __FILE__, __LINE__, __FUNCTION__, ## arg)
+#else
+#define dbg(format, arg...) do { } while (0)
+#define info(format, arg...) do { } while (0)
+#define err(format, arg...) do { } while (0)
+#endif
 
+/* libudev */
 void udev_log(struct udev *udev,
              int priority, const char *file, int line, const char *fn,
              const char *format, ...)
              __attribute__ ((format(printf, 6, 7)));
-#else
-#define log_dbg(format, arg...) do { } while (0)
-#define log_info(format, arg...) do { } while (0)
-#define log_err(format, arg...) do { } while (0)
-#endif
-
-/* libudev */
 extern struct udev_device *device_init(struct udev *udev);
+extern const char *udev_get_rules_path(struct udev *udev);
+extern int udev_get_run(struct udev *udev);
 
 /* libudev-device */
 extern int device_set_devpath(struct udev_device *udev_device, const char *devpath);
index a8a8a5a..5a00f44 100644 (file)
@@ -26,8 +26,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
-#include <dirent.h>
-#include <sys/stat.h>
+#include <ctype.h>
 
 #include "libudev.h"
 #include "libudev-private.h"
@@ -38,6 +37,11 @@ struct udev {
        void (*log_fn)(struct udev *udev,
                       int priority, const char *file, int line, const char *fn,
                       const char *format, va_list args);
+       char *sys_path;
+       char *dev_path;
+       char *rules_path;
+       int log_priority;
+       int run:1;
 };
 
 void udev_log(struct udev *udev,
@@ -46,6 +50,9 @@ void udev_log(struct udev *udev,
 {
        va_list args;
 
+       if (priority > udev->log_priority)
+               return;
+
        va_start(args, format);
        udev->log_fn(udev, priority, file, line, fn, format, args);
        va_end(args);
@@ -55,32 +62,9 @@ static void log_stderr(struct udev *udev,
                       int priority, const char *file, int line, const char *fn,
                       const char *format, va_list args)
 {
-       static int log = -1;
-
-       if (log == -1) {
-               if (getenv("LIBUDEV_DEBUG") != NULL)
-                       log = 1;
-               else
-                       log = 0;
-       }
-
-       if (log == 1) {
-               fprintf(stderr, "libudev: %s: ", fn);
-               vfprintf(stderr, format, args);
-       }
-}
-
-/* glue to udev logging, needed until udev logging code is "fixed" */
-#ifdef USE_LOG
-void log_message(int priority, const char *format, ...)
-{
-       va_list args;
-
-       va_start(args, format);
-       log_stderr(NULL, priority, NULL, 0, "", format, args);
-       va_end(args);
+       fprintf(stderr, "libudev: %s: ", fn);
+       vfprintf(stderr, format, args);
 }
-#endif
 
 /**
  * udev_new:
@@ -95,17 +79,159 @@ void log_message(int priority, const char *format, ...)
 struct udev *udev_new(void)
 {
        struct udev *udev;
+       const char *env;
+       char *config_file;
+       FILE *f;
 
        udev = malloc(sizeof(struct udev));
        if (udev == NULL)
                return NULL;
        memset(udev, 0x00, (sizeof(struct udev)));
+
+       sysfs_init();
+
+       /* defaults */
+       config_file = NULL;
        udev->refcount = 1;
        udev->log_fn = log_stderr;
-       udev_config_init();
-       sysfs_init();
-       log_info(udev, "context %p created\n", udev);
+       udev->log_priority = LOG_ERR;
+       udev->run = 1;
+       udev->dev_path = strdup(UDEV_PREFIX "/dev");
+       udev->sys_path = strdup("/sys");
+       config_file = strdup(SYSCONFDIR "/udev/udev.conf");
+
+       if (udev->dev_path == NULL || udev->sys_path == NULL)
+               goto err;
+
+       /* settings by environment and config file */
+       env = getenv("SYSFS_PATH");
+       if (env != NULL) {
+               free(udev->sys_path);
+               udev->sys_path = strdup(env);
+               remove_trailing_chars(udev->sys_path, '/');
+       }
+
+       env = getenv("UDEV_RUN");
+       if (env != NULL && !string_is_true(env))
+               udev->run = 0;
+
+       env = getenv("UDEV_CONFIG_FILE");
+       if (env != NULL) {
+               free(config_file);
+               config_file = strdup(env);
+               remove_trailing_chars(config_file, '/');
+       }
+       if (config_file == NULL)
+               goto err;
+       f = fopen(config_file, "r");
+       if (f != NULL) {
+               char line[LINE_SIZE];
+               int line_nr = 0;
+
+               while (fgets(line, sizeof(line), f)) {
+                       size_t len;
+                       char *key;
+                       char *val;
+
+                       line_nr++;
+
+                       /* find key */
+                       key = line;
+                       while (isspace(key[0]))
+                               key++;
+
+                       /* comment or empty line */
+                       if (key[0] == '#' || key[0] == '\0')
+                               continue;
+
+                       /* split key/value */
+                       val = strchr(key, '=');
+                       if (val == NULL) {
+                               err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
+                               continue;
+                       }
+                       val[0] = '\0';
+                       val++;
+
+                       /* find value */
+                       while (isspace(val[0]))
+                               val++;
+
+                       /* terminate key */
+                       len = strlen(key);
+                       if (len == 0)
+                               continue;
+                       while (isspace(key[len-1]))
+                               len--;
+                       key[len] = '\0';
+
+                       /* terminate value */
+                       len = strlen(val);
+                       if (len == 0)
+                               continue;
+                       while (isspace(val[len-1]))
+                               len--;
+                       val[len] = '\0';
+
+                       if (len == 0)
+                               continue;
+
+                       /* unquote */
+                       if (val[0] == '"' || val[0] == '\'') {
+                               if (val[len-1] != val[0]) {
+                                       err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
+                                       continue;
+                               }
+                               val[len-1] = '\0';
+                               val++;
+                       }
+
+                       if (strcasecmp(key, "udev_log") == 0) {
+                               udev->log_priority = log_priority(val);
+                               continue;
+                       }
+                       if (strcasecmp(key, "udev_root") == 0) {
+                               free(udev->dev_path);
+                               udev->dev_path = strdup(val);
+                               remove_trailing_chars(udev->dev_path, '/');
+                               continue;
+                       }
+                       if (strcasecmp(key, "udev_rules") == 0) {
+                               free(udev->rules_path);
+                               udev->rules_path = strdup(val);
+                               remove_trailing_chars(udev->rules_path, '/');
+                               continue;
+                       }
+               }
+               fclose(f);
+       }
+       free(config_file);
+
+       env = getenv("UDEV_ROOT");
+       if (env != NULL) {
+               free(udev->dev_path);
+               udev->dev_path = strdup(env);
+               remove_trailing_chars(udev->dev_path, '/');
+       }
+
+       env = getenv("UDEV_LOG");
+       if (env != NULL)
+               udev->log_priority = log_priority(env);
+
+       if (udev->dev_path == NULL || udev->sys_path == NULL)
+               goto err;
+
+       info(udev, "context %p created\n", udev);
+       info(udev, "log_priority=%d\n", udev->log_priority);
+       info(udev, "dev_path='%s'\n", udev->dev_path);
+       if (udev->rules_path != NULL)
+               info(udev, "rules_path='%s'\n", udev->rules_path);
+
        return udev;
+err:
+       err(udev, "context creation failed\n");
+       udev_unref(udev);
+       return NULL;
 }
 
 /**
@@ -140,7 +266,9 @@ void udev_unref(struct udev *udev)
        if (udev->refcount > 0)
                return;
        sysfs_cleanup();
-       log_info(udev, "context %p released\n", udev);
+       free(udev->dev_path);
+       free(udev->sys_path);
+       info(udev, "context %p released\n", udev);
        free(udev);
 }
 
@@ -161,7 +289,27 @@ void udev_set_log_fn(struct udev *udev,
                                    const char *format, va_list args))
 {
        udev->log_fn = log_fn;
-       log_info(udev, "custom logging function %p registered\n", udev);
+       info(udev, "custom logging function %p registered\n", udev);
+}
+
+int udev_get_log_priority(struct udev *udev)
+{
+       return udev->log_priority;
+}
+
+void udev_set_log_priority(struct udev *udev, int priority)
+{
+       udev->log_priority = priority;
+}
+
+const char *udev_get_rules_path(struct udev *udev)
+{
+       return udev->rules_path;
+}
+
+int udev_get_run(struct udev *udev)
+{
+       return udev->run;
 }
 
 /**
@@ -178,7 +326,7 @@ const char *udev_get_sys_path(struct udev *udev)
 {
        if (udev == NULL)
                return NULL;
-       return sysfs_path;
+       return udev->sys_path;
 }
 
 /**
@@ -195,5 +343,5 @@ const char *udev_get_dev_path(struct udev *udev)
 {
        if (udev == NULL)
                return NULL;
-       return udev_root;
+       return udev->dev_path;
 }
index 30753a1..98652c5 100644 (file)
@@ -20,6 +20,8 @@
 #ifndef _LIBUDEV_H_
 #define _LIBUDEV_H_
 
+#include <stdarg.h>
+
 /* this will stay as long as the DeviceKit integration of udev is work in progress */
 #if !defined _LIBUDEV_COMPILATION && !defined LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
 #error "#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE is needed to use this experimental library version"
@@ -36,6 +38,8 @@ extern void udev_set_log_fn(struct udev *udev,
                            void (*log_fn)(struct udev *udev,
                                           int priority, const char *file, int line, const char *fn,
                                           const char *format, va_list args));
+extern int udev_get_log_priority(struct udev *udev);
+extern void udev_set_log_priority(struct udev *udev, int priority);
 extern const char *udev_get_sys_path(struct udev *udev);
 extern const char *udev_get_dev_path(struct udev *udev);
 
diff --git a/udev/logging.h b/udev/logging.h
deleted file mode 100644 (file)
index 6766ac3..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * simple logging functions that can be expanded into nothing
- *
- * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
- * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
- *
- *     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.
- * 
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- */
-
-#ifndef LOGGING_H
-#define LOGGING_H
-
-#include "config.h"
-
-#define err(format, arg...)            do { } while (0)
-#define info(format, arg...)           do { } while (0)
-#define dbg(format, arg...)            do { } while (0)
-#define logging_init(foo)              do { } while (0)
-#define logging_close(foo)             do { } while (0)
-
-#ifdef USE_LOG
-#include <stdarg.h>
-#include <unistd.h>
-#include <syslog.h>
-
-#undef err
-#define err(format, arg...)                                                    \
-       do {                                                                    \
-               log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg);      \
-       } while (0)
-
-#undef info
-#define info(format, arg...)                                                   \
-       do {                                                                    \
-               log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg);     \
-       } while (0)
-
-#ifdef DEBUG
-#undef dbg
-#define dbg(format, arg...)                                                    \
-       do {                                                                    \
-               log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg);    \
-       } while (0)
-#endif
-
-extern void log_message(int priority, const char *format, ...)
-       __attribute__ ((format (printf, 2, 3)));
-
-#undef logging_init
-static inline void logging_init(const char *program_name)
-{
-       openlog(program_name, LOG_PID | LOG_CONS, LOG_DAEMON);
-}
-
-#undef logging_close
-static inline void logging_close(void)
-{
-       closelog();
-}
-
-#endif /* USE_LOG */
-
-#endif
index 07628f7..4e782a0 100644 (file)
 #include "udev_rules.h"
 #include "udev_selinux.h"
 
-#ifdef USE_LOG
-void log_message(int priority, const char *format, ...)
-{
-       va_list args;
-
-       if (priority > udev_log_priority)
-               return;
-
-       va_start(args, format);
-       vsyslog(priority, format, args);
-       va_end(args);
-}
-#endif
-
 static void asmlinkage sig_handler(int signum)
 {
        switch (signum) {
@@ -62,8 +48,9 @@ static void asmlinkage sig_handler(int signum)
 
 int main(int argc, char *argv[])
 {
+       struct udev *udev;
        struct sysfs_device *dev;
-       struct udevice *udev;
+       struct udevice *udevice;
        const char *maj, *min;
        struct udev_rules rules;
        const char *action;
@@ -73,10 +60,11 @@ int main(int argc, char *argv[])
        int devnull;
        int retval = -EINVAL;
 
-       if (argc == 2 && strcmp(argv[1], "-V") == 0) {
-               printf("%s\n", VERSION);
-               exit(0);
-       }
+       udev = udev_new();
+       if (udev == NULL)
+               exit(1);
+       dbg(udev, "version %s\n", VERSION);
+       selinux_init(udev);
 
        /* set std fd's to /dev/null, /sbin/hotplug forks us, we don't have them at all */
        devnull = open("/dev/null", O_RDWR);
@@ -89,15 +77,10 @@ int main(int argc, char *argv[])
                        dup2(devnull, STDERR_FILENO);
                if (devnull > STDERR_FILENO)
                        close(devnull);
+       } else {
+               err(udev, "open /dev/null failed: %s\n", strerror(errno));
        }
 
-       logging_init("udev");
-       if (devnull < 0)
-               err("open /dev/null failed: %s\n", strerror(errno));
-       udev_config_init();
-       selinux_init();
-       dbg("version %s\n", VERSION);
-
        /* set signal handlers */
        memset(&act, 0x00, sizeof(act));
        act.sa_handler = (void (*)(int)) sig_handler;
@@ -118,61 +101,59 @@ int main(int argc, char *argv[])
                subsystem = argv[1];
 
        if (action == NULL || subsystem == NULL || devpath == NULL) {
-               err("action, subsystem or devpath missing\n");
+               err(udev, "action, subsystem or devpath missing\n");
                goto exit;
        }
 
        /* export log_priority , as called programs may want to do the same as udev */
-       if (udev_log_priority) {
+       if (udev_get_log_priority(udev) > 0) {
                char priority[32];
 
-               sprintf(priority, "%i", udev_log_priority);
+               sprintf(priority, "%i", udev_get_log_priority(udev));
                setenv("UDEV_LOG", priority, 1);
        }
 
        sysfs_init();
-       udev_rules_init(&rules, 0);
+       udev_rules_init(udev, &rules, 0);
 
-       dev = sysfs_device_get(devpath);
+       dev = sysfs_device_get(udev, devpath);
        if (dev == NULL) {
-               info("unable to open '%s'\n", devpath);
+               info(udev, "unable to open '%s'\n", devpath);
                goto fail;
        }
 
-       udev = udev_device_init();
-       if (udev == NULL)
+       udevice = udev_device_init(udev);
+       if (udevice == NULL)
                goto fail;
 
        /* override built-in sysfs device */
-       udev->dev = dev;
-       strlcpy(udev->action, action, sizeof(udev->action));
+       udevice->dev = dev;
+       strlcpy(udevice->action, action, sizeof(udevice->action));
 
        /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
        maj = getenv("MAJOR");
        min = getenv("MINOR");
        if (maj != NULL && min != NULL)
-               udev->devt = makedev(atoi(maj), atoi(min));
+               udevice->devt = makedev(atoi(maj), atoi(min));
        else
-               udev->devt = udev_device_get_devt(udev);
+               udevice->devt = udev_device_get_devt(udevice);
 
-       retval = udev_device_event(&rules, udev);
+       retval = udev_device_event(&rules, udevice);
 
        /* rules may change/disable the timeout */
-       if (udev->event_timeout >= 0)
-               alarm(udev->event_timeout);
+       if (udevice->event_timeout >= 0)
+               alarm(udevice->event_timeout);
 
-       if (retval == 0 && !udev->ignore_device && udev_run)
-               udev_rules_run(udev);
+       if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
+               udev_rules_run(udevice);
 
-       udev_device_cleanup(udev);
+       udev_device_cleanup(udevice);
 fail:
        udev_rules_cleanup(&rules);
        sysfs_cleanup();
-       selinux_exit();
+       selinux_exit(udev);
 
 exit:
-       logging_close();
-       endgrent();
        if (retval != 0)
                return 1;
        return 0;
index 4e339ff..96e4b29 100644 (file)
 #include "udevd.h"
 
 struct udev_ctrl {
+       struct udev *udev;
        int sock;
        struct sockaddr_un saddr;
        socklen_t addrlen;
 };
 
-struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path)
+struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
 {
        struct udev_ctrl *uctrl;
 
@@ -45,10 +46,11 @@ struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path)
        if (uctrl == NULL)
                return NULL;
        memset(uctrl, 0x00, sizeof(struct udev_ctrl));
+       uctrl->udev = udev;
 
        uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
        if (uctrl->sock < 0) {
-               err("error getting socket: %s\n", strerror(errno));
+               err(udev, "error getting socket: %s\n", strerror(errno));
                free(uctrl);
                return NULL;
        }
@@ -85,7 +87,7 @@ static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int
 
        err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
        if (err == -1) {
-               err("error sending message: %s\n", strerror(errno));
+               err(uctrl->udev, "error sending message: %s\n", strerror(errno));
        }
        return err;
 }
index 00f2fd8..1dd55f6 100644 (file)
 #ifndef _UDEV_H_
 #define _UDEV_H_
 
+#include "config.h"
+
 #include <sys/types.h>
 #include <sys/param.h>
 
 #include "list.h"
-#include "logging.h"
 #include "udev_sysdeps.h"
+#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE 1
+#include "lib/libudev.h"
+#include "lib/libudev-private.h"
 
 #define COMMENT_CHARACTER                      '#'
 #define LINE_SIZE                              512
@@ -59,6 +63,8 @@ struct sysfs_device {
 };
 
 struct udevice {
+       struct udev *udev;
+
        /* device event */
        struct sysfs_device *dev;               /* points to dev_local by default */
        struct sysfs_device dev_local;
@@ -92,48 +98,57 @@ struct udevice {
        int test_run;
 };
 
-/* udev_config.c */
-extern char udev_root[PATH_SIZE];
-extern char udev_config_filename[PATH_SIZE];
-extern char udev_rules_dir[PATH_SIZE];
-extern int udev_log_priority;
-extern int udev_run;
-extern void udev_config_init(void);
+static inline void logging_init(const char *program_name)
+{
+       openlog(program_name, LOG_PID | LOG_CONS, LOG_DAEMON);
+}
+
+static inline void logging_msg(struct udev *udev, int priority,
+                         const char *file, int line, const char *fn,
+                         const char *format, va_list args)
+{
+       vsyslog(priority, format, args);
+}
+
+static inline void logging_close(void)
+{
+       closelog();
+}
 
 /* udev_device.c */
-extern struct udevice *udev_device_init(void);
-extern void udev_device_cleanup(struct udevice *udev);
-extern dev_t udev_device_get_devt(struct udevice *udev);
+extern struct udevice *udev_device_init(struct udev *udev);
+extern void udev_device_cleanup(struct udevice *udevice);
+extern dev_t udev_device_get_devt(struct udevice *udevice);
 
 /* udev_device_event.c */
-extern int udev_device_event(struct udev_rules *rules, struct udevice *udev);
+extern int udev_device_event(struct udev_rules *rules, struct udevice *udevice);
 
 /* udev_sysfs.c */
-extern char sysfs_path[PATH_SIZE];
 extern int sysfs_init(void);
 extern void sysfs_cleanup(void);
-extern void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
+extern void sysfs_device_set_values(struct udev *udev,
+                                   struct sysfs_device *dev, const char *devpath,
                                    const char *subsystem, const char *driver);
-extern struct sysfs_device *sysfs_device_get(const char *devpath);
-extern struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev);
-extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem);
-extern char *sysfs_attr_get_value(const char *devpath, const char *attr_name);
-extern int sysfs_resolve_link(char *path, size_t size);
-extern int sysfs_lookup_devpath_by_subsys_id(char *devpath, size_t len, const char *subsystem, const char *id);
+extern struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath);
+extern struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev);
+extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem);
+extern char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name);
+extern int sysfs_resolve_link(struct udev *udev, char *path, size_t size);
+extern int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath, size_t len, const char *subsystem, const char *id);
 
 /* udev_node.c */
-extern int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid);
-extern void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old);
-extern int udev_node_add(struct udevice *udev);
-extern int udev_node_remove(struct udevice *udev);
+extern int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid);
+extern void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udev_old);
+extern int udev_node_add(struct udevice *udevice);
+extern int udev_node_remove(struct udevice *udevice);
 
 /* udev_db.c */
-extern int udev_db_add_device(struct udevice *dev);
-extern int udev_db_delete_device(struct udevice *dev);
-extern int udev_db_rename(const char *devpath_old, const char *devpath);
-extern int udev_db_get_device(struct udevice *udev, const char *devpath);
-extern int udev_db_get_devices_by_name(const char *name, struct list_head *name_list);
-extern int udev_db_get_all_entries(struct list_head *name_list);
+extern int udev_db_add_device(struct udevice *udevice);
+extern int udev_db_delete_device(struct udevice *udevice);
+extern int udev_db_rename(struct udev *udev, const char *devpath_old, const char *devpath);
+extern int udev_db_get_device(struct udevice *udevice, const char *devpath);
+extern int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list);
+extern int udev_db_get_all_entries(struct udev *udevconst, struct list_head *name_list);
 
 /* udev_utils.c */
 struct name_entry {
@@ -143,13 +158,13 @@ struct name_entry {
 };
 
 extern int log_priority(const char *priority);
-extern struct name_entry *name_list_add(struct list_head *name_list, const char *name, int sort);
-extern struct name_entry *name_list_key_add(struct list_head *name_list, const char *key, const char *value);
-extern int name_list_key_remove(struct list_head *name_list, const char *key);
-extern void name_list_cleanup(struct list_head *name_list);
-extern int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix);
-extern uid_t lookup_user(const char *user);
-extern gid_t lookup_group(const char *group);
+extern struct name_entry *name_list_add(struct udev *udev, struct list_head *name_list, const char *name, int sort);
+extern struct name_entry *name_list_key_add(struct udev *udev, struct list_head *name_list, const char *key, const char *value);
+extern int name_list_key_remove(struct udev *udev, struct list_head *name_list, const char *key);
+extern void name_list_cleanup(struct udev *udev, struct list_head *name_list);
+extern int add_matching_files(struct udev *udev, struct list_head *name_list, const char *dirname, const char *suffix);
+extern uid_t lookup_user(struct udev *udev, const char *user);
+extern gid_t lookup_group(struct udev *udev, const char *group);
 
 /* udev_utils_string.c */
 extern int string_is_true(const char *str);
@@ -160,24 +175,24 @@ extern int utf8_encoded_valid_unichar(const char *str);
 extern int replace_chars(char *str, const char *white);
 
 /* udev_utils_file.c */
-extern int create_path(const char *path);
-extern int delete_path(const char *path);
+extern int create_path(struct udev *udev, const char *path);
+extern int delete_path(struct udev *udev, const char *path);
+extern int unlink_secure(struct udev *udev, const char *filename);
 extern int file_map(const char *filename, char **buf, size_t *bufsize);
 extern void file_unmap(void *buf, size_t bufsize);
-extern int unlink_secure(const char *filename);
 extern size_t buf_get_line(const char *buf, size_t buflen, size_t cur);
 
 /* udevadm commands */
-extern int udevadm_monitor(int argc, char *argv[]);
-extern int udevadm_info(int argc, char *argv[]);
-extern int udevadm_control(int argc, char *argv[]);
-extern int udevadm_trigger(int argc, char *argv[]);
-extern int udevadm_settle(int argc, char *argv[]);
-extern int udevadm_test(int argc, char *argv[]);
+extern int udevadm_monitor(struct udev *udev, int argc, char *argv[]);
+extern int udevadm_info(struct udev *udev, int argc, char *argv[]);
+extern int udevadm_control(struct udev *udev, int argc, char *argv[]);
+extern int udevadm_trigger(struct udev *udev, int argc, char *argv[]);
+extern int udevadm_settle(struct udev *udev, int argc, char *argv[]);
+extern int udevadm_test(struct udev *udev, int argc, char *argv[]);
 
 /* udev_ctrl - daemon runtime setup */
 struct udev_ctrl;
-extern struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path);
+extern struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path);
 extern void udev_ctrl_unref(struct udev_ctrl *uctrl);
 extern int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority);
 extern int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl);
diff --git a/udev/udev_config.c b/udev/udev_config.c
deleted file mode 100644 (file)
index 8a1dac5..0000000
+++ /dev/null
@@ -1,200 +0,0 @@
-/*
- * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
- * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
- *
- *     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.
- * 
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- */
-
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <ctype.h>
-#include <syslog.h>
-
-#include "udev.h"
-
-/* global variables */
-char udev_root[PATH_SIZE];
-char udev_config_filename[PATH_SIZE];
-char udev_rules_dir[PATH_SIZE];
-int udev_log_priority;
-int udev_run;
-
-static int get_key(char **line, char **key, char **value)
-{
-       char *linepos;
-       char *temp;
-
-       linepos = *line;
-       if (!linepos)
-               return -1;
-
-       /* skip whitespace */
-       while (isspace(linepos[0]))
-               linepos++;
-
-       /* get the key */
-       *key = linepos;
-       while (1) {
-               linepos++;
-               if (linepos[0] == '\0')
-                       return -1;
-               if (isspace(linepos[0]))
-                       break;
-               if (linepos[0] == '=')
-                       break;
-       }
-
-       /* terminate key */
-       linepos[0] = '\0';
-       linepos++;
-
-       /* skip whitespace */
-       while (isspace(linepos[0]))
-               linepos++;
-
-       /* get the value*/
-       if (linepos[0] == '"')
-               linepos++;
-       else
-               return -1;
-       *value = linepos;
-
-       temp = strchr(linepos, '"');
-       if (!temp)
-               return -1;
-       temp[0] = '\0';
-
-       return 0;
-}
-
-static int parse_config_file(void)
-{
-       char line[LINE_SIZE];
-       char *bufline;
-       char *linepos;
-       char *variable;
-       char *value;
-       char *buf;
-       size_t bufsize;
-       size_t cur;
-       size_t count;
-       int lineno;
-       int retval = 0;
-
-       if (file_map(udev_config_filename, &buf, &bufsize) != 0) {
-               err("can't open '%s' as config file: %s\n", udev_config_filename, strerror(errno));
-               return -ENODEV;
-       }
-
-       /* loop through the whole file */
-       lineno = 0;
-       cur = 0;
-       while (cur < bufsize) {
-               count = buf_get_line(buf, bufsize, cur);
-               bufline = &buf[cur];
-               cur += count+1;
-               lineno++;
-
-               /* eat the whitespace */
-               while ((count > 0) && isspace(bufline[0])) {
-                       bufline++;
-                       count--;
-               }
-               if (count == 0)
-                       continue;
-
-               /* see if this is a comment */
-               if (bufline[0] == COMMENT_CHARACTER)
-                       continue;
-
-               if (count >= sizeof(line)) {
-                       err("line too long, conf line skipped %s, line %d\n", udev_config_filename, lineno);
-                       continue;
-               }
-
-               memcpy(line, bufline, count);
-               line[count] = '\0';
-
-               linepos = line;
-               retval = get_key(&linepos, &variable, &value);
-               if (retval != 0) {
-                       err("error parsing %s, line %d:%d\n", udev_config_filename, lineno, (int)(linepos-line));
-                       continue;
-               }
-
-               if (strcasecmp(variable, "udev_root") == 0) {
-                       strlcpy(udev_root, value, sizeof(udev_root));
-                       remove_trailing_chars(udev_root, '/');
-                       continue;
-               }
-
-               if (strcasecmp(variable, "udev_rules") == 0) {
-                       strlcpy(udev_rules_dir, value, sizeof(udev_rules_dir));
-                       remove_trailing_chars(udev_rules_dir, '/');
-                       continue;
-               }
-
-               if (strcasecmp(variable, "udev_log") == 0) {
-                       udev_log_priority = log_priority(value);
-                       continue;
-               }
-       }
-
-       file_unmap(buf, bufsize);
-       return retval;
-}
-
-void udev_config_init(void)
-{
-       const char *env;
-
-       strcpy(udev_config_filename, SYSCONFDIR "/udev/udev.conf");
-       strcpy(udev_root, UDEV_PREFIX "/dev");
-       udev_rules_dir[0] = '\0';
-       udev_log_priority = LOG_ERR;
-       udev_run = 1;
-
-       /* disable RUN key execution */
-       env = getenv("UDEV_RUN");
-       if (env && !string_is_true(env))
-               udev_run = 0;
-
-       env = getenv("UDEV_CONFIG_FILE");
-       if (env) {
-               strlcpy(udev_config_filename, env, sizeof(udev_config_filename));
-               remove_trailing_chars(udev_config_filename, '/');
-       }
-
-       parse_config_file();
-
-       env = getenv("UDEV_ROOT");
-       if (env) {
-               strlcpy(udev_root, env, sizeof(udev_root));
-               remove_trailing_chars(udev_root, '/');
-       }
-
-       env = getenv("UDEV_LOG");
-       if (env)
-               udev_log_priority = log_priority(env);
-
-       dbg("UDEV_CONFIG_FILE='%s'\n", udev_config_filename);
-       dbg("udev_root='%s'\n", udev_root);
-       dbg("udev_rules_dir='%s'\n", udev_rules_dir);
-       dbg("udev_log=%d\n", udev_log_priority);
-}
index e3f0f7f..e89f2c2 100644 (file)
 #include "udev_selinux.h"
 
 
-static size_t devpath_to_db_path(const char *devpath, char *filename, size_t len)
+static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
 {
        size_t start;
 
        /* translate to location of db file */
-       strlcpy(filename, udev_root, len);
+       strlcpy(filename, udev_get_dev_path(udev), len);
        start = strlcat(filename, "/.udev/db/", len);
        strlcat(filename, devpath, len);
        return path_encode(&filename[start], len - start);
 }
 
 /* reverse mapping from the device file name to the devpath */
-static int name_index(const char *devpath, const char *name, int add)
+static int name_index(struct udev *udev, const char *devpath, const char *name, int add)
 {
        char device[PATH_SIZE];
        char filename[PATH_SIZE * 2];
@@ -54,7 +54,7 @@ static int name_index(const char *devpath, const char *name, int add)
        int fd;
 
        /* directory with device name */
-       strlcpy(filename, udev_root, sizeof(filename));
+       strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
        start = strlcat(filename, "/.udev/names/", sizeof(filename));
        strlcat(filename, name, sizeof(filename));
        path_encode(&filename[start], sizeof(filename) - start);
@@ -65,39 +65,39 @@ static int name_index(const char *devpath, const char *name, int add)
        strlcat(filename, device, sizeof(filename));
 
        if (add) {
-               info("creating index: '%s'\n", filename);
-               create_path(filename);
+               info(udev, "creating index: '%s'\n", filename);
+               create_path(udev, filename);
                fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
                if (fd > 0)
                        close(fd);
        } else {
-               info("removing index: '%s'\n", filename);
+               info(udev, "removing index: '%s'\n", filename);
                unlink(filename);
-               delete_path(filename);
+               delete_path(udev, filename);
        }
        return 0;
 }
 
-int udev_db_get_devices_by_name(const char *name, struct list_head *name_list)
+int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list)
 {
        char dirname[PATH_MAX];
        size_t start;
        DIR *dir;
        int rc = 0;
 
-       strlcpy(dirname, udev_root, sizeof(dirname));
+       strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
        start = strlcat(dirname, "/.udev/names/", sizeof(dirname));
        strlcat(dirname, name, sizeof(dirname));
        path_encode(&dirname[start], sizeof(dirname) - start);
 
        dir = opendir(dirname);
        if (dir == NULL) {
-               info("no index directory '%s': %s\n", dirname, strerror(errno));
+               info(udev, "no index directory '%s': %s\n", dirname, strerror(errno));
                rc = -1;
                goto out;
        }
 
-       info("found index directory '%s'\n", dirname);
+       info(udev, "found index directory '%s'\n", dirname);
        while (1) {
                struct dirent *ent;
                char device[PATH_SIZE];
@@ -110,7 +110,7 @@ int udev_db_get_devices_by_name(const char *name, struct list_head *name_list)
 
                strlcpy(device, ent->d_name, sizeof(device));
                path_decode(device);
-               name_list_add(name_list, device, 0);
+               name_list_add(udev, name_list, device, 0);
                rc++;
        }
        closedir(dir);
@@ -118,40 +118,40 @@ out:
        return rc;
 }
 
-int udev_db_rename(const char *devpath_old, const char *devpath)
+int udev_db_rename(struct udev *udev, const char *devpath_old, const char *devpath)
 {
        char filename[PATH_SIZE];
        char filename_old[PATH_SIZE];
 
-       devpath_to_db_path(devpath_old, filename_old, sizeof(filename_old));
-       devpath_to_db_path(devpath, filename, sizeof(filename));
+       devpath_to_db_path(udev, devpath_old, filename_old, sizeof(filename_old));
+       devpath_to_db_path(udev, devpath, filename, sizeof(filename));
        return rename(filename_old, filename);
 }
 
-int udev_db_add_device(struct udevice *udev)
+int udev_db_add_device(struct udevice *udevice)
 {
        char filename[PATH_SIZE];
 
-       if (udev->test_run)
+       if (udevice->test_run)
                return 0;
 
-       devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
-       create_path(filename);
+       devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
+       create_path(udevice->udev, filename);
        unlink(filename);
 
        /*
         * don't waste tmpfs memory pages, if we don't have any data to store
         * create fake db-file; store the node-name in a symlink target
         */
-       if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
-           !udev->partitions && !udev->ignore_remove) {
+       if (list_empty(&udevice->symlink_list) && list_empty(&udevice->env_list) &&
+           !udevice->partitions && !udevice->ignore_remove) {
                int ret;
-               dbg("nothing interesting to store, create symlink\n");
-               selinux_setfscreatecon(filename, NULL, S_IFLNK);        
-               ret = symlink(udev->name, filename);
-               selinux_resetfscreatecon();
+               dbg(udevice->udev, "nothing interesting to store, create symlink\n");
+               selinux_setfscreatecon(udevice->udev, filename, NULL, S_IFLNK);
+               ret = symlink(udevice->name, filename);
+               selinux_resetfscreatecon(udevice->udev);
                if (ret != 0) {
-                       err("unable to create db link '%s': %s\n", filename, strerror(errno));
+                       err(udevice->udev, "unable to create db link '%s': %s\n", filename, strerror(errno));
                        return -1;
                }
        } else {
@@ -160,38 +160,38 @@ int udev_db_add_device(struct udevice *udev)
 
                f = fopen(filename, "w");
                if (f == NULL) {
-                       err("unable to create db file '%s': %s\n", filename, strerror(errno));
+                       err(udevice->udev, "unable to create db file '%s': %s\n", filename, strerror(errno));
                        return -1;
                }
-               dbg("storing data for device '%s' in '%s'\n", udev->dev->devpath, filename);
+               dbg(udevice->udev, "storing data for device '%s' in '%s'\n", udevice->dev->devpath, filename);
 
-               fprintf(f, "N:%s\n", udev->name);
-               list_for_each_entry(name_loop, &udev->symlink_list, node) {
+               fprintf(f, "N:%s\n", udevice->name);
+               list_for_each_entry(name_loop, &udevice->symlink_list, node) {
                        fprintf(f, "S:%s\n", name_loop->name);
                        /* add symlink-name to index */
-                       name_index(udev->dev->devpath, name_loop->name, 1);
+                       name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 1);
                }
-               fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
-               if (udev->link_priority != 0)
-                       fprintf(f, "L:%u\n", udev->link_priority);
-               if (udev->event_timeout >= 0)
-                       fprintf(f, "T:%u\n", udev->event_timeout);
-               if (udev->partitions != 0)
-                       fprintf(f, "A:%u\n", udev->partitions);
-               if (udev->ignore_remove)
-                       fprintf(f, "R:%u\n", udev->ignore_remove);
-               list_for_each_entry(name_loop, &udev->env_list, node)
+               fprintf(f, "M:%u:%u\n", major(udevice->devt), minor(udevice->devt));
+               if (udevice->link_priority != 0)
+                       fprintf(f, "L:%u\n", udevice->link_priority);
+               if (udevice->event_timeout >= 0)
+                       fprintf(f, "T:%u\n", udevice->event_timeout);
+               if (udevice->partitions != 0)
+                       fprintf(f, "A:%u\n", udevice->partitions);
+               if (udevice->ignore_remove)
+                       fprintf(f, "R:%u\n", udevice->ignore_remove);
+               list_for_each_entry(name_loop, &udevice->env_list, node)
                        fprintf(f, "E:%s\n", name_loop->name);
                fclose(f);
        }
 
        /* add name to index */
-       name_index(udev->dev->devpath, udev->name, 1);
+       name_index(udevice->udev, udevice->dev->devpath, udevice->name, 1);
 
        return 0;
 }
 
-int udev_db_get_device(struct udevice *udev, const char *devpath)
+int udev_db_get_device(struct udevice *udevice, const char *devpath)
 {
        struct stat stats;
        char filename[PATH_SIZE];
@@ -203,32 +203,32 @@ int udev_db_get_device(struct udevice *udev, const char *devpath)
        size_t cur;
        size_t count;
 
-       sysfs_device_set_values(udev->dev, devpath, NULL, NULL);
-       devpath_to_db_path(devpath, filename, sizeof(filename));
+       sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL);
+       devpath_to_db_path(udevice->udev, devpath, filename, sizeof(filename));
 
        if (lstat(filename, &stats) != 0) {
-               info("no db file to read %s: %s\n", filename, strerror(errno));
+               info(udevice->udev, "no db file to read %s: %s\n", filename, strerror(errno));
                return -1;
        }
        if ((stats.st_mode & S_IFMT) == S_IFLNK) {
                char target[NAME_SIZE];
                int target_len;
 
-               info("found a symlink as db file\n");
+               info(udevice->udev, "found a symlink as db file\n");
                target_len = readlink(filename, target, sizeof(target));
                if (target_len > 0)
                        target[target_len] = '\0';
                else {
-                       info("error reading db link %s: %s\n", filename, strerror(errno));
+                       info(udevice->udev, "error reading db link %s: %s\n", filename, strerror(errno));
                        return -1;
                }
-               dbg("db link points to '%s'\n", target);
-               strlcpy(udev->name, target, sizeof(udev->name));
+               dbg(udevice->udev, "db link points to '%s'\n", target);
+               strlcpy(udevice->name, target, sizeof(udevice->name));
                return 0;
        }
 
        if (file_map(filename, &buf, &bufsize) != 0) {
-               info("error reading db file %s: %s\n", filename, strerror(errno));
+               info(udevice->udev, "error reading db file %s: %s\n", filename, strerror(errno));
                return -1;
        }
 
@@ -245,68 +245,68 @@ int udev_db_get_device(struct udevice *udev, const char *devpath)
 
                switch(bufline[0]) {
                case 'N':
-                       strlcpy(udev->name, line, sizeof(udev->name));
+                       strlcpy(udevice->name, line, sizeof(udevice->name));
                        break;
                case 'M':
                        sscanf(line, "%u:%u", &maj, &min);
-                       udev->devt = makedev(maj, min);
+                       udevice->devt = makedev(maj, min);
                        break;
                case 'S':
-                       name_list_add(&udev->symlink_list, line, 0);
+                       name_list_add(udevice->udev, &udevice->symlink_list, line, 0);
                        break;
                case 'L':
-                       udev->link_priority = atoi(line);
+                       udevice->link_priority = atoi(line);
                        break;
                case 'T':
-                       udev->event_timeout = atoi(line);
+                       udevice->event_timeout = atoi(line);
                        break;
                case 'A':
-                       udev->partitions = atoi(line);
+                       udevice->partitions = atoi(line);
                        break;
                case 'R':
-                       udev->ignore_remove = atoi(line);
+                       udevice->ignore_remove = atoi(line);
                        break;
                case 'E':
-                       name_list_add(&udev->env_list, line, 0);
+                       name_list_add(udevice->udev, &udevice->env_list, line, 0);
                        break;
                }
        }
        file_unmap(buf, bufsize);
 
-       if (udev->name[0] == '\0')
+       if (udevice->name[0] == '\0')
                return -1;
 
        return 0;
 }
 
-int udev_db_delete_device(struct udevice *udev)
+int udev_db_delete_device(struct udevice *udevice)
 {
        char filename[PATH_SIZE];
        struct name_entry *name_loop;
 
-       if (udev->test_run)
+       if (udevice->test_run)
                return 0;
 
-       devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
+       devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
        unlink(filename);
 
-       name_index(udev->dev->devpath, udev->name, 0);
-       list_for_each_entry(name_loop, &udev->symlink_list, node)
-               name_index(udev->dev->devpath, name_loop->name, 0);
+       name_index(udevice->udev, udevice->dev->devpath, udevice->name, 0);
+       list_for_each_entry(name_loop, &udevice->symlink_list, node)
+               name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 0);
 
        return 0;
 }
 
-int udev_db_get_all_entries(struct list_head *name_list)
+int udev_db_get_all_entries(struct udev *udev, struct list_head *name_list)
 {
        char dbpath[PATH_MAX];
        DIR *dir;
 
-       strlcpy(dbpath, udev_root, sizeof(dbpath));
+       strlcpy(dbpath, udev_get_dev_path(udev), sizeof(dbpath));
        strlcat(dbpath, "/.udev/db", sizeof(dbpath));
        dir = opendir(dbpath);
        if (dir == NULL) {
-               info("no udev_db available '%s': %s\n", dbpath, strerror(errno));
+               info(udev, "no udev_db available '%s': %s\n", dbpath, strerror(errno));
                return -1;
        }
 
@@ -322,8 +322,8 @@ int udev_db_get_all_entries(struct list_head *name_list)
 
                strlcpy(device, ent->d_name, sizeof(device));
                path_decode(device);
-               name_list_add(name_list, device, 1);
-               dbg("added '%s'\n", device);
+               name_list_add(udev, name_list, device, 1);
+               dbg(udev, "added '%s'\n", device);
        }
 
        closedir(dir);
index 130c714..5ae6674 100644 (file)
 #include "udev_rules.h"
 
 
-struct udevice *udev_device_init(void)
+struct udevice *udev_device_init(struct udev *udev)
 {
-       struct udevice *udev;
+       struct udevice *udevice;
 
-       udev = malloc(sizeof(struct udevice));
-       if (udev == NULL)
+       udevice = malloc(sizeof(struct udevice));
+       if (udevice == NULL)
                return NULL;
-       memset(udev, 0x00, sizeof(struct udevice));
+       memset(udevice, 0x00, sizeof(struct udevice));
 
-       INIT_LIST_HEAD(&udev->symlink_list);
-       INIT_LIST_HEAD(&udev->run_list);
-       INIT_LIST_HEAD(&udev->env_list);
+       udevice->udev = udev;
+
+       INIT_LIST_HEAD(&udevice->symlink_list);
+       INIT_LIST_HEAD(&udevice->run_list);
+       INIT_LIST_HEAD(&udevice->env_list);
 
        /* set sysfs device to local storage, can be overridden if needed */
-       udev->dev = &udev->dev_local;
+       udevice->dev = &udevice->dev_local;
 
        /* default node permissions */
-       udev->mode = 0660;
-       strcpy(udev->owner, "root");
-       strcpy(udev->group, "root");
+       udevice->mode = 0660;
+       strcpy(udevice->owner, "root");
+       strcpy(udevice->group, "root");
 
-       udev->event_timeout = -1;
-       return udev;
+       udevice->event_timeout = -1;
+       return udevice;
 }
 
-void udev_device_cleanup(struct udevice *udev)
+void udev_device_cleanup(struct udevice *udevice)
 {
-       if (udev == NULL)
+       if (udevice == NULL)
                return;
-       name_list_cleanup(&udev->symlink_list);
-       name_list_cleanup(&udev->run_list);
-       name_list_cleanup(&udev->env_list);
-       free(udev);
+       name_list_cleanup(udevice->udev, &udevice->symlink_list);
+       name_list_cleanup(udevice->udev, &udevice->run_list);
+       name_list_cleanup(udevice->udev, &udevice->env_list);
+       free(udevice);
 }
 
-dev_t udev_device_get_devt(struct udevice *udev)
+dev_t udev_device_get_devt(struct udevice *udevice)
 {
        const char *attr;
        unsigned int maj, min;
 
        /* read it from sysfs  */
-       attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
+       attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "dev");
        if (attr != NULL) {
                if (sscanf(attr, "%u:%u", &maj, &min) == 2)
                        return makedev(maj, min);
index 8ad7964..6b40623 100644 (file)
@@ -54,25 +54,25 @@ static void kernel_log(struct ifreq ifr)
        fclose(f);
 }
 
-static int rename_netif(struct udevice *udev)
+static int rename_netif(struct udevice *udevice)
 {
        int sk;
        struct ifreq ifr;
        int retval;
 
-       info("changing net interface name from '%s' to '%s'\n", udev->dev->kernel, udev->name);
-       if (udev->test_run)
+       info(udevice->udev, "changing net interface name from '%s' to '%s'\n", udevice->dev->kernel, udevice->name);
+       if (udevice->test_run)
                return 0;
 
        sk = socket(PF_INET, SOCK_DGRAM, 0);
        if (sk < 0) {
-               err("error opening socket: %s\n", strerror(errno));
+               err(udevice->udev, "error opening socket: %s\n", strerror(errno));
                return -1;
        }
 
        memset(&ifr, 0x00, sizeof(struct ifreq));
-       strlcpy(ifr.ifr_name, udev->dev->kernel, IFNAMSIZ);
-       strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, udevice->dev->kernel, IFNAMSIZ);
+       strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ);
        retval = ioctl(sk, SIOCSIFNAME, &ifr);
        if (retval == 0)
                kernel_log(ifr);
@@ -81,22 +81,22 @@ static int rename_netif(struct udevice *udev)
 
                /* see if the destination interface name already exists */
                if (errno != EEXIST) {
-                       err("error changing netif name %s to %s: %s\n", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
+                       err(udevice->udev, "error changing netif name %s to %s: %s\n", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
                        goto exit;
                }
 
                /* free our own name, another process may wait for us */
-               strlcpy(ifr.ifr_newname, udev->dev->kernel, IFNAMSIZ);
+               strlcpy(ifr.ifr_newname, udevice->dev->kernel, IFNAMSIZ);
                strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ);
                retval = ioctl(sk, SIOCSIFNAME, &ifr);
                if (retval != 0) {
-                       err("error changing netif name %s to %s: %s\n", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
+                       err(udevice->udev, "error changing netif name %s to %s: %s\n", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
                        goto exit;
                }
 
                /* wait 30 seconds for our target to become available */
                strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
-               strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
+               strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ);
                loop = 30 * 20;
                while (loop--) {
                        retval = ioctl(sk, SIOCSIFNAME, &ifr);
@@ -106,11 +106,11 @@ static int rename_netif(struct udevice *udev)
                        }
 
                        if (errno != EEXIST) {
-                               err("error changing net interface name %s to %s: %s\n",
+                               err(udevice->udev, "error changing net interface name %s to %s: %s\n",
                                    ifr.ifr_name, ifr.ifr_newname, strerror(errno));
                                break;
                        }
-                       dbg("wait for netif '%s' to become free, loop=%i\n", udev->name, (30 * 20) - loop);
+                       dbg(udevice->udev, "wait for netif '%s' to become free, loop=%i\n", udevice->name, (30 * 20) - loop);
                        usleep(1000 * 1000 / 20);
                }
        }
@@ -120,139 +120,139 @@ exit:
        return retval;
 }
 
-int udev_device_event(struct udev_rules *rules, struct udevice *udev)
+int udev_device_event(struct udev_rules *rules, struct udevice *udevice)
 {
        int retval = 0;
 
-       if (udev->devpath_old != NULL)
-               if (udev_db_rename(udev->devpath_old, udev->dev->devpath) == 0)
-                       info("moved database from '%s' to '%s'\n", udev->devpath_old, udev->dev->devpath);
+       if (udevice->devpath_old != NULL)
+               if (udev_db_rename(udevice->udev, udevice->devpath_old, udevice->dev->devpath) == 0)
+                       info(udevice->udev, "moved database from '%s' to '%s'\n", udevice->devpath_old, udevice->dev->devpath);
 
        /* add device node */
-       if (major(udev->devt) != 0 &&
-           (strcmp(udev->action, "add") == 0 || strcmp(udev->action, "change") == 0)) {
-               struct udevice *udev_old;
+       if (major(udevice->devt) != 0 &&
+           (strcmp(udevice->action, "add") == 0 || strcmp(udevice->action, "change") == 0)) {
+               struct udevice *udevice_old;
 
-               dbg("device node add '%s'\n", udev->dev->devpath);
+               dbg(udevice->udev, "device node add '%s'\n", udevice->dev->devpath);
 
-               udev_rules_get_name(rules, udev);
-               if (udev->ignore_device) {
-                       info("device event will be ignored\n");
+               udev_rules_get_name(rules, udevice);
+               if (udevice->ignore_device) {
+                       info(udevice->udev, "device event will be ignored\n");
                        goto exit;
                }
-               if (udev->name[0] == '\0') {
-                       info("device node creation supressed\n");
+               if (udevice->name[0] == '\0') {
+                       info(udevice->udev, "device node creation supressed\n");
                        goto exit;
                }
 
                /* read current database entry; cleanup, if it is known device */
-               udev_old = udev_device_init();
-               if (udev_old != NULL) {
-                       udev_old->test_run = udev->test_run;
-                       if (udev_db_get_device(udev_old, udev->dev->devpath) == 0) {
-                               info("device '%s' already in database, cleanup\n", udev->dev->devpath);
-                               udev_db_delete_device(udev_old);
+               udevice_old = udev_device_init(udevice->udev);
+               if (udevice_old != NULL) {
+                       udevice_old->test_run = udevice->test_run;
+                       if (udev_db_get_device(udevice_old, udevice->dev->devpath) == 0) {
+                               info(udevice->udev, "device '%s' already in database, cleanup\n", udevice->dev->devpath);
+                               udev_db_delete_device(udevice_old);
                        } else {
-                               udev_device_cleanup(udev_old);
-                               udev_old = NULL;
+                               udev_device_cleanup(udevice_old);
+                               udevice_old = NULL;
                        }
                }
 
                /* create node */
-               retval = udev_node_add(udev);
+               retval = udev_node_add(udevice);
                if (retval != 0)
                        goto exit;
 
                /* store in database */
-               udev_db_add_device(udev);
+               udev_db_add_device(udevice);
 
                /* create, replace, delete symlinks according to priority */
-               udev_node_update_symlinks(udev, udev_old);
+               udev_node_update_symlinks(udevice, udevice_old);
 
-               if (udev_old != NULL)
-                       udev_device_cleanup(udev_old);
+               if (udevice_old != NULL)
+                       udev_device_cleanup(udevice_old);
                goto exit;
        }
 
        /* add netif */
-       if (strcmp(udev->dev->subsystem, "net") == 0 && strcmp(udev->action, "add") == 0) {
-               dbg("netif add '%s'\n", udev->dev->devpath);
-               udev_rules_get_name(rules, udev);
-               if (udev->ignore_device) {
-                       info("device event will be ignored\n");
+       if (strcmp(udevice->dev->subsystem, "net") == 0 && strcmp(udevice->action, "add") == 0) {
+               dbg(udevice->udev, "netif add '%s'\n", udevice->dev->devpath);
+               udev_rules_get_name(rules, udevice);
+               if (udevice->ignore_device) {
+                       info(udevice->udev, "device event will be ignored\n");
                        goto exit;
                }
-               if (udev->name[0] == '\0') {
-                       info("device renaming supressed\n");
+               if (udevice->name[0] == '\0') {
+                       info(udevice->udev, "device renaming supressed\n");
                        goto exit;
                }
 
                /* look if we want to change the name of the netif */
-               if (strcmp(udev->name, udev->dev->kernel) != 0) {
+               if (strcmp(udevice->name, udevice->dev->kernel) != 0) {
                        char devpath[PATH_MAX];
                        char *pos;
 
-                       retval = rename_netif(udev);
+                       retval = rename_netif(udevice);
                        if (retval != 0)
                                goto exit;
-                       info("renamed netif to '%s'\n", udev->name);
+                       info(udevice->udev, "renamed netif to '%s'\n", udevice->name);
 
                        /* export old name */
-                       setenv("INTERFACE_OLD", udev->dev->kernel, 1);
+                       setenv("INTERFACE_OLD", udevice->dev->kernel, 1);
 
                        /* now change the devpath, because the kernel device name has changed */
-                       strlcpy(devpath, udev->dev->devpath, sizeof(devpath));
+                       strlcpy(devpath, udevice->dev->devpath, sizeof(devpath));
                        pos = strrchr(devpath, '/');
                        if (pos != NULL) {
                                pos[1] = '\0';
-                               strlcat(devpath, udev->name, sizeof(devpath));
-                               sysfs_device_set_values(udev->dev, devpath, NULL, NULL);
-                               setenv("DEVPATH", udev->dev->devpath, 1);
-                               setenv("INTERFACE", udev->name, 1);
-                               info("changed devpath to '%s'\n", udev->dev->devpath);
+                               strlcat(devpath, udevice->name, sizeof(devpath));
+                               sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL);
+                               setenv("DEVPATH", udevice->dev->devpath, 1);
+                               setenv("INTERFACE", udevice->name, 1);
+                               info(udevice->udev, "changed devpath to '%s'\n", udevice->dev->devpath);
                        }
                }
                goto exit;
        }
 
        /* remove device node */
-       if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
+       if (major(udevice->devt) != 0 && strcmp(udevice->action, "remove") == 0) {
                struct name_entry *name_loop;
 
                /* import database entry, and delete it */
-               if (udev_db_get_device(udev, udev->dev->devpath) == 0) {
-                       udev_db_delete_device(udev);
+               if (udev_db_get_device(udevice, udevice->dev->devpath) == 0) {
+                       udev_db_delete_device(udevice);
                        /* restore stored persistent data */
-                       list_for_each_entry(name_loop, &udev->env_list, node)
+                       list_for_each_entry(name_loop, &udevice->env_list, node)
                                putenv(name_loop->name);
                } else {
-                       dbg("'%s' not found in database, using kernel name '%s'\n",
-                           udev->dev->devpath, udev->dev->kernel);
-                       strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
+                       dbg(udevice->udev, "'%s' not found in database, using kernel name '%s'\n",
+                           udevice->dev->devpath, udevice->dev->kernel);
+                       strlcpy(udevice->name, udevice->dev->kernel, sizeof(udevice->name));
                }
 
-               udev_rules_get_run(rules, udev);
-               if (udev->ignore_device) {
-                       info("device event will be ignored\n");
+               udev_rules_get_run(rules, udevice);
+               if (udevice->ignore_device) {
+                       info(udevice->udev, "device event will be ignored\n");
                        goto exit;
                }
 
-               if (udev->ignore_remove) {
-                       info("ignore_remove for '%s'\n", udev->name);
+               if (udevice->ignore_remove) {
+                       info(udevice->udev, "ignore_remove for '%s'\n", udevice->name);
                        goto exit;
                }
                /* remove the node */
-               retval = udev_node_remove(udev);
+               retval = udev_node_remove(udevice);
 
                /* delete or restore symlinks according to priority */
-               udev_node_update_symlinks(udev, NULL);
+               udev_node_update_symlinks(udevice, NULL);
                goto exit;
        }
 
        /* default devices */
-       udev_rules_get_run(rules, udev);
-       if (udev->ignore_device)
-               info("device event will be ignored\n");
+       udev_rules_get_run(rules, udevice);
+       if (udevice->ignore_device)
+               info(udevice->udev, "device event will be ignored\n");
 
 exit:
        return retval;
index 33183c8..462f1b6 100644 (file)
 
 #define TMP_FILE_EXT           ".udev-tmp"
 
-int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
+int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
 {
        char file_tmp[PATH_SIZE + sizeof(TMP_FILE_EXT)];
        struct stat stats;
        int preserve = 0;
        int err = 0;
 
-       if (major(devt) != 0 && strcmp(udev->dev->subsystem, "block") == 0)
+       if (major(devt) != 0 && strcmp(udevice->dev->subsystem, "block") == 0)
                mode |= S_IFBLK;
        else
                mode |= S_IFCHR;
 
        if (lstat(file, &stats) == 0) {
                if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devt)) {
-                       info("preserve file '%s', because it has correct dev_t\n", file);
+                       info(udevice->udev, "preserve file '%s', because it has correct dev_t\n", file);
                        preserve = 1;
-                       selinux_setfilecon(file, udev->dev->kernel, mode);
+                       selinux_setfilecon(udevice->udev, file, udevice->dev->kernel, mode);
                } else {
-                       info("atomically replace existing file '%s'\n", file);
+                       info(udevice->udev, "atomically replace existing file '%s'\n", file);
                        strlcpy(file_tmp, file, sizeof(file_tmp));
                        strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
                        unlink(file_tmp);
-                       selinux_setfscreatecon(file_tmp, udev->dev->kernel, mode);
+                       selinux_setfscreatecon(udevice->udev, file_tmp, udevice->dev->kernel, mode);
                        err = mknod(file_tmp, mode, devt);
-                       selinux_resetfscreatecon();
+                       selinux_resetfscreatecon(udevice->udev);
                        if (err != 0) {
-                               err("mknod(%s, %#o, %u, %u) failed: %s\n",
+                               err(udevice->udev, "mknod(%s, %#o, %u, %u) failed: %s\n",
                                    file_tmp, mode, major(devt), minor(devt), strerror(errno));
                                goto exit;
                        }
                        err = rename(file_tmp, file);
                        if (err != 0) {
-                               err("rename(%s, %s) failed: %s\n",
-                                   file_tmp, file, strerror(errno));
+                               err(udevice->udev, "rename(%s, %s) failed: %s\n", file_tmp, file, strerror(errno));
                                unlink(file_tmp);
                        }
                }
        } else {
-               info("mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
-               selinux_setfscreatecon(file, udev->dev->kernel, mode);
+               info(udevice->udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
+               selinux_setfscreatecon(udevice->udev, file, udevice->dev->kernel, mode);
                err = mknod(file, mode, devt);
-               selinux_resetfscreatecon();
+               selinux_resetfscreatecon(udevice->udev);
                if (err != 0) {
-                       err("mknod(%s, %#o, (%u,%u) failed: %s\n",
+                       err(udevice->udev, "mknod(%s, %#o, (%u,%u) failed: %s\n",
                            file, mode, major(devt), minor(devt), strerror(errno));
                        goto exit;
                }
        }
 
        if (!preserve || stats.st_mode != mode) {
-               info("chmod(%s, %#o)\n", file, mode);
+               info(udevice->udev, "chmod(%s, %#o)\n", file, mode);
                err = chmod(file, mode);
                if (err != 0) {
-                       err("chmod(%s, %#o) failed: %s\n", file, mode, strerror(errno));
+                       err(udevice->udev, "chmod(%s, %#o) failed: %s\n", file, mode, strerror(errno));
                        goto exit;
                }
        }
 
        if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
-               info("chown(%s, %u, %u)\n", file, uid, gid);
+               info(udevice->udev, "chown(%s, %u, %u)\n", file, uid, gid);
                err = chown(file, uid, gid);
                if (err != 0) {
-                       err("chown(%s, %u, %u) failed: %s\n", file, uid, gid, strerror(errno));
+                       err(udevice->udev, "chown(%s, %u, %u) failed: %s\n", file, uid, gid, strerror(errno));
                        goto exit;
                }
        }
@@ -105,7 +104,7 @@ exit:
        return err;
 }
 
-static int node_symlink(const char *node, const char *slink)
+static int node_symlink(struct udevice *udevice, const char *node, const char *slink)
 {
        struct stat stats;
        char target[PATH_SIZE] = "";
@@ -133,53 +132,53 @@ static int node_symlink(const char *node, const char *slink)
                if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
                        struct stat stats2;
 
-                       info("found existing node instead of symlink '%s'\n", slink);
+                       info(udevice->udev, "found existing node instead of symlink '%s'\n", slink);
                        if (lstat(node, &stats2) == 0) {
                                if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
                                    stats.st_rdev == stats2.st_rdev) {
-                                       info("replace device node '%s' with symlink to our node '%s'\n", slink, node);
+                                       info(udevice->udev, "replace device node '%s' with symlink to our node '%s'\n", slink, node);
                                } else {
-                                       err("device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
+                                       err(udevice->udev, "device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
                                        goto exit;
                                }
                        }
                } else if (S_ISLNK(stats.st_mode)) {
                        char buf[PATH_SIZE];
 
-                       info("found existing symlink '%s'\n", slink);
+                       info(udevice->udev, "found existing symlink '%s'\n", slink);
                        len = readlink(slink, buf, sizeof(buf));
                        if (len > 0) {
                                buf[len] = '\0';
                                if (strcmp(target, buf) == 0) {
-                                       info("preserve already existing symlink '%s' to '%s'\n", slink, target);
-                                       selinux_setfilecon(slink, NULL, S_IFLNK);
+                                       info(udevice->udev, "preserve already existing symlink '%s' to '%s'\n", slink, target);
+                                       selinux_setfilecon(udevice->udev, slink, NULL, S_IFLNK);
                                        goto exit;
                                }
                        }
                }
        } else {
-               info("creating symlink '%s' to '%s'\n", slink, target);
-               selinux_setfscreatecon(slink, NULL, S_IFLNK);
+               info(udevice->udev, "creating symlink '%s' to '%s'\n", slink, target);
+               selinux_setfscreatecon(udevice->udev, slink, NULL, S_IFLNK);
                retval = symlink(target, slink);
-               selinux_resetfscreatecon();
+               selinux_resetfscreatecon(udevice->udev);
                if (retval == 0)
                        goto exit;
        }
 
-       info("atomically replace '%s'\n", slink);
+       info(udevice->udev, "atomically replace '%s'\n", slink);
        strlcpy(slink_tmp, slink, sizeof(slink_tmp));
        strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
        unlink(slink_tmp);
-       selinux_setfscreatecon(slink, NULL, S_IFLNK);
+       selinux_setfscreatecon(udevice->udev, slink, NULL, S_IFLNK);
        retval = symlink(target, slink_tmp);
-       selinux_resetfscreatecon();
+       selinux_resetfscreatecon(udevice->udev);
        if (retval != 0) {
-               err("symlink(%s, %s) failed: %s\n", target, slink_tmp, strerror(errno));
+               err(udevice->udev, "symlink(%s, %s) failed: %s\n", target, slink_tmp, strerror(errno));
                goto exit;
        }
        retval = rename(slink_tmp, slink);
        if (retval != 0) {
-               err("rename(%s, %s) failed: %s\n", slink_tmp, slink, strerror(errno));
+               err(udevice->udev, "rename(%s, %s) failed: %s\n", slink_tmp, slink, strerror(errno));
                unlink(slink_tmp);
                goto exit;
        }
@@ -187,100 +186,100 @@ exit:
        return retval;
 }
 
-static int update_link(struct udevice *udev, const char *name)
+static int update_link(struct udevice *udevice, const char *name)
 {
        LIST_HEAD(name_list);
        char slink[PATH_SIZE];
        char node[PATH_SIZE];
-       struct udevice *udev_db;
+       struct udevice *udevice_db;
        struct name_entry *device;
        char target[PATH_MAX] = "";
        int count;
        int priority = 0;
        int rc = 0;
 
-       strlcpy(slink, udev_root, sizeof(slink));
+       strlcpy(slink, udev_get_dev_path(udevice->udev), sizeof(slink));
        strlcat(slink, "/", sizeof(slink));
        strlcat(slink, name, sizeof(slink));
 
-       count = udev_db_get_devices_by_name(name, &name_list);
-       info("found %i devices with name '%s'\n", count, name);
+       count = udev_db_get_devices_by_name(udevice->udev, name, &name_list);
+       info(udevice->udev, "found %i devices with name '%s'\n", count, name);
 
        /* if we don't have a reference, delete it */
        if (count <= 0) {
-               info("no reference left, remove '%s'\n", name);
-               if (!udev->test_run) {
+               info(udevice->udev, "no reference left, remove '%s'\n", name);
+               if (!udevice->test_run) {
                        unlink(slink);
-                       delete_path(slink);
+                       delete_path(udevice->udev, slink);
                }
                goto out;
        }
 
        /* find the device with the highest priority */
        list_for_each_entry(device, &name_list, node) {
-               info("found '%s' for '%s'\n", device->name, name);
+               info(udevice->udev, "found '%s' for '%s'\n", device->name, name);
 
                /* did we find ourself? we win, if we have the same priority */
-               if (strcmp(udev->dev->devpath, device->name) == 0) {
-                       info("compare (our own) priority of '%s' %i >= %i\n",
-                            udev->dev->devpath, udev->link_priority, priority);
-                       if (strcmp(udev->name, name) == 0) {
-                               info("'%s' is our device node, database inconsistent, skip link update\n", udev->name);
-                       } else if (target[0] == '\0' || udev->link_priority >= priority) {
-                               priority = udev->link_priority;
-                               strlcpy(target, udev->name, sizeof(target));
+               if (strcmp(udevice->dev->devpath, device->name) == 0) {
+                       info(udevice->udev, "compare (our own) priority of '%s' %i >= %i\n",
+                            udevice->dev->devpath, udevice->link_priority, priority);
+                       if (strcmp(udevice->name, name) == 0) {
+                               info(udevice->udev, "'%s' is our device node, database inconsistent, skip link update\n", udevice->name);
+                       } else if (target[0] == '\0' || udevice->link_priority >= priority) {
+                               priority = udevice->link_priority;
+                               strlcpy(target, udevice->name, sizeof(target));
                        }
                        continue;
                }
 
                /* another device, read priority from database */
-               udev_db = udev_device_init();
-               if (udev_db == NULL)
+               udevice_db = udev_device_init(udevice->udev);
+               if (udevice_db == NULL)
                        continue;
-               if (udev_db_get_device(udev_db, device->name) == 0) {
-                       if (strcmp(udev_db->name, name) == 0) {
-                               info("'%s' is a device node of '%s', skip link update\n", udev_db->name, device->name);
+               if (udev_db_get_device(udevice_db, device->name) == 0) {
+                       if (strcmp(udevice_db->name, name) == 0) {
+                               info(udevice->udev, "'%s' is a device node of '%s', skip link update\n", udevice_db->name, device->name);
                        } else {
-                               info("compare priority of '%s' %i > %i\n",
-                                    udev_db->dev->devpath, udev_db->link_priority, priority);
-                               if (target[0] == '\0' || udev_db->link_priority > priority) {
-                                       priority = udev_db->link_priority;
-                                       strlcpy(target, udev_db->name, sizeof(target));
+                               info(udevice->udev, "compare priority of '%s' %i > %i\n",
+                                    udevice_db->dev->devpath, udevice_db->link_priority, priority);
+                               if (target[0] == '\0' || udevice_db->link_priority > priority) {
+                                       priority = udevice_db->link_priority;
+                                       strlcpy(target, udevice_db->name, sizeof(target));
                                }
                        }
                }
-               udev_device_cleanup(udev_db);
+               udev_device_cleanup(udevice_db);
        }
-       name_list_cleanup(&name_list);
+       name_list_cleanup(udevice->udev, &name_list);
 
        if (target[0] == '\0') {
-               info("no current target for '%s' found\n", name);
+               info(udevice->udev, "no current target for '%s' found\n", name);
                rc = 1;
                goto out;
        }
 
        /* create symlink to the target with the highest priority */
-       strlcpy(node, udev_root, sizeof(node));
+       strlcpy(node, udev_get_dev_path(udevice->udev), sizeof(node));
        strlcat(node, "/", sizeof(node));
        strlcat(node, target, sizeof(node));
-       info("'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
-       if (!udev->test_run) {
-               create_path(slink);
-               node_symlink(node, slink);
+       info(udevice->udev, "'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
+       if (!udevice->test_run) {
+               create_path(udevice->udev, slink);
+               node_symlink(udevice, node, slink);
        }
 out:
        return rc;
 }
 
-void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old)
+void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_old)
 {
        struct name_entry *name_loop;
        char symlinks[PATH_SIZE] = "";
 
-       list_for_each_entry(name_loop, &udev->symlink_list, node) {
-               info("update symlink '%s' of '%s'\n", name_loop->name, udev->dev->devpath);
-               update_link(udev, name_loop->name);
-               strlcat(symlinks, udev_root, sizeof(symlinks));
+       list_for_each_entry(name_loop, &udevice->symlink_list, node) {
+               info(udevice->udev, "update symlink '%s' of '%s'\n", name_loop->name, udevice->dev->devpath);
+               update_link(udevice, name_loop->name);
+               strlcat(symlinks, udev_get_dev_path(udevice->udev), sizeof(symlinks));
                strlcat(symlinks, "/", sizeof(symlinks));
                strlcat(symlinks, name_loop->name, sizeof(symlinks));
                strlcat(symlinks, " ", sizeof(symlinks));
@@ -292,15 +291,15 @@ void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old)
                setenv("DEVLINKS", symlinks, 1);
 
        /* update possible left-over symlinks (device metadata changed) */
-       if (udev_old != NULL) {
+       if (udevice_old != NULL) {
                struct name_entry *link_loop;
                struct name_entry *link_old_loop;
                int found;
 
                /* remove current symlinks from old list */
-               list_for_each_entry(link_old_loop, &udev_old->symlink_list, node) {
+               list_for_each_entry(link_old_loop, &udevice_old->symlink_list, node) {
                        found = 0;
-                       list_for_each_entry(link_loop, &udev->symlink_list, node) {
+                       list_for_each_entry(link_loop, &udevice->symlink_list, node) {
                                if (strcmp(link_old_loop->name, link_loop->name) == 0) {
                                        found = 1;
                                        break;
@@ -308,9 +307,9 @@ void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old)
                        }
                        if (!found) {
                                /* link does no longer belong to this device */
-                               info("update old symlink '%s' no longer belonging to '%s'\n",
-                                    link_old_loop->name, udev->dev->devpath);
-                               update_link(udev, link_old_loop->name);
+                               info(udevice->udev, "update old symlink '%s' no longer belonging to '%s'\n",
+                                    link_old_loop->name, udevice->dev->devpath);
+                               update_link(udevice, link_old_loop->name);
                        }
                }
 
@@ -318,12 +317,12 @@ void udev_node_update_symlinks(struct udevice *udev, struct udevice *udev_old)
                 * if the node name has changed, delete the node,
                 * or possibly restore a symlink of another device
                 */
-               if (strcmp(udev->name, udev_old->name) != 0)
-                       update_link(udev, udev_old->name);
+               if (strcmp(udevice->name, udevice_old->name) != 0)
+                       update_link(udevice, udevice_old->name);
        }
 }
 
-int udev_node_add(struct udevice *udev)
+int udev_node_add(struct udevice *udevice)
 {
        char filename[PATH_SIZE];
        uid_t uid;
@@ -331,42 +330,42 @@ int udev_node_add(struct udevice *udev)
        int i;
        int retval = 0;
 
-       strlcpy(filename, udev_root, sizeof(filename));
+       strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
        strlcat(filename, "/", sizeof(filename));
-       strlcat(filename, udev->name, sizeof(filename));
-       create_path(filename);
+       strlcat(filename, udevice->name, sizeof(filename));
+       create_path(udevice->udev, filename);
 
-       if (strcmp(udev->owner, "root") == 0)
+       if (strcmp(udevice->owner, "root") == 0)
                uid = 0;
        else {
                char *endptr;
                unsigned long id;
 
-               id = strtoul(udev->owner, &endptr, 10);
+               id = strtoul(udevice->owner, &endptr, 10);
                if (endptr[0] == '\0')
                        uid = (uid_t) id;
                else
-                       uid = lookup_user(udev->owner);
+                       uid = lookup_user(udevice->udev, udevice->owner);
        }
 
-       if (strcmp(udev->group, "root") == 0)
+       if (strcmp(udevice->group, "root") == 0)
                gid = 0;
        else {
                char *endptr;
                unsigned long id;
 
-               id = strtoul(udev->group, &endptr, 10);
+               id = strtoul(udevice->group, &endptr, 10);
                if (endptr[0] == '\0')
                        gid = (gid_t) id;
                else
-                       gid = lookup_group(udev->group);
+                       gid = lookup_group(udevice->udev, udevice->group);
        }
 
-       info("creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
-            filename, major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
+       info(udevice->udev, "creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
+            filename, major(udevice->devt), minor(udevice->devt), udevice->mode, uid, gid);
 
-       if (!udev->test_run)
-               if (udev_node_mknod(udev, filename, udev->devt, udev->mode, uid, gid) != 0) {
+       if (!udevice->test_run)
+               if (udev_node_mknod(udevice, filename, udevice->devt, udevice->mode, uid, gid) != 0) {
                        retval = -1;
                        goto exit;
                }
@@ -374,27 +373,27 @@ int udev_node_add(struct udevice *udev)
        setenv("DEVNAME", filename, 1);
 
        /* create all_partitions if requested */
-       if (udev->partitions) {
+       if (udevice->partitions) {
                char partitionname[PATH_SIZE];
                char *attr;
                int range;
 
                /* take the maximum registered minor range */
-               attr = sysfs_attr_get_value(udev->dev->devpath, "range");
+               attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "range");
                if (attr != NULL) {
                        range = atoi(attr);
                        if (range > 1)
-                               udev->partitions = range-1;
+                               udevice->partitions = range-1;
                }
-               info("creating device partition nodes '%s[1-%i]'\n", filename, udev->partitions);
-               if (!udev->test_run) {
-                       for (i = 1; i <= udev->partitions; i++) {
+               info(udevice->udev, "creating device partition nodes '%s[1-%i]'\n", filename, udevice->partitions);
+               if (!udevice->test_run) {
+                       for (i = 1; i <= udevice->partitions; i++) {
                                dev_t part_devt;
 
                                snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
                                partitionname[sizeof(partitionname)-1] = '\0';
-                               part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
-                               udev_node_mknod(udev, partitionname, part_devt, udev->mode, uid, gid);
+                               part_devt = makedev(major(udevice->devt), minor(udevice->devt) + i);
+                               udev_node_mknod(udevice, partitionname, part_devt, udevice->mode, uid, gid);
                        }
                }
        }
@@ -402,7 +401,7 @@ exit:
        return retval;
 }
 
-int udev_node_remove(struct udevice *udev)
+int udev_node_remove(struct udevice *udevice)
 {
        char filename[PATH_SIZE];
        char partitionname[PATH_SIZE];
@@ -410,39 +409,39 @@ int udev_node_remove(struct udevice *udev)
        int retval = 0;
        int num;
 
-       strlcpy(filename, udev_root, sizeof(filename));
+       strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
        strlcat(filename, "/", sizeof(filename));
-       strlcat(filename, udev->name, sizeof(filename));
+       strlcat(filename, udevice->name, sizeof(filename));
        if (stat(filename, &stats) != 0) {
-               info("device node '%s' not found\n", filename);
+               info(udevice->udev, "device node '%s' not found\n", filename);
                return 0;
        }
-       if (udev->devt && stats.st_rdev != udev->devt) {
-               info("device node '%s' points to a different device, skip removal\n", filename);
+       if (udevice->devt && stats.st_rdev != udevice->devt) {
+               info(udevice->udev, "device node '%s' points to a different device, skip removal\n", filename);
                return -1;
        }
 
-       info("removing device node '%s'\n", filename);
-       if (!udev->test_run)
-               retval = unlink_secure(filename);
+       info(udevice->udev, "removing device node '%s'\n", filename);
+       if (!udevice->test_run)
+               retval = unlink_secure(udevice->udev, filename);
        if (retval)
                return retval;
 
        setenv("DEVNAME", filename, 1);
-       num = udev->partitions;
+       num = udevice->partitions;
        if (num > 0) {
                int i;
 
-               info("removing all_partitions '%s[1-%i]'\n", filename, num);
+               info(udevice->udev, "removing all_partitions '%s[1-%i]'\n", filename, num);
                if (num > 255)
                        return -1;
                for (i = 1; i <= num; i++) {
                        snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
                        partitionname[sizeof(partitionname)-1] = '\0';
-                       if (!udev->test_run)
-                               unlink_secure(partitionname);
+                       if (!udevice->test_run)
+                               unlink_secure(udevice->udev, partitionname);
                }
        }
-       delete_path(filename);
+       delete_path(udevice->udev, filename);
        return retval;
 }
index 4719cab..b864599 100644 (file)
@@ -40,7 +40,7 @@
 extern char **environ;
 
 /* extract possible {attr} and move str behind it */
-static char *get_format_attribute(char **str)
+static char *get_format_attribute(struct udev *udev, char **str)
 {
        char *pos;
        char *attr = NULL;
@@ -48,19 +48,19 @@ static char *get_format_attribute(char **str)
        if (*str[0] == '{') {
                pos = strchr(*str, '}');
                if (pos == NULL) {
-                       err("missing closing brace for format\n");
+                       err(udev, "missing closing brace for format\n");
                        return NULL;
                }
                pos[0] = '\0';
                attr = *str+1;
                *str = pos+1;
-               dbg("attribute='%s', str='%s'\n", attr, *str);
+               dbg(udev, "attribute='%s', str='%s'\n", attr, *str);
        }
        return attr;
 }
 
 /* extract possible format length and move str behind it*/
-static int get_format_len(char **str)
+static int get_format_len(struct udev *udev, char **str)
 {
        int num;
        char *tail;
@@ -69,10 +69,10 @@ static int get_format_len(char **str)
                num = (int) strtoul(*str, &tail, 10);
                if (num > 0) {
                        *str = tail;
-                       dbg("format length=%i\n", num);
+                       dbg(udev, "format length=%i\n", num);
                        return num;
                } else {
-                       err("format parsing error '%s'\n", *str);
+                       err(udev, "format parsing error '%s'\n", *str);
                }
        }
        return -1;
@@ -118,8 +118,8 @@ out:
        return 0;
 }
 
-static int run_program(const char *command, const char *subsystem,
-               char *result, size_t ressize, size_t *reslen)
+static int run_program(struct udev *udev, const char *command, const char *subsystem,
+                      char *result, size_t ressize, size_t *reslen)
 {
        int status;
        int outpipe[2] = {-1, -1};
@@ -148,7 +148,7 @@ static int run_program(const char *command, const char *subsystem,
                        } else {
                                argv[i] = strsep(&pos, " ");
                        }
-                       dbg("arg[%i] '%s'\n", i, argv[i]);
+                       dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
                        i++;
                }
                argv[i] = NULL;
@@ -156,18 +156,18 @@ static int run_program(const char *command, const char *subsystem,
                argv[0] = arg;
                argv[1] = NULL;
        }
-       info("'%s'\n", command);
+       info(udev, "'%s'\n", command);
 
        /* prepare pipes from child to parent */
-       if (result != NULL || udev_log_priority >= LOG_INFO) {
+       if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
                if (pipe(outpipe) != 0) {
-                       err("pipe failed: %s\n", strerror(errno));
+                       err(udev, "pipe failed: %s\n", strerror(errno));
                        return -1;
                }
        }
-       if (udev_log_priority >= LOG_INFO) {
+       if (udev_get_log_priority(udev) >= LOG_INFO) {
                if (pipe(errpipe) != 0) {
-                       err("pipe failed: %s\n", strerror(errno));
+                       err(udev, "pipe failed: %s\n", strerror(errno));
                        return -1;
                }
        }
@@ -198,7 +198,7 @@ static int run_program(const char *command, const char *subsystem,
                                dup2(devnull, STDERR_FILENO);
                        close(devnull);
                } else
-                       err("open /dev/null failed: %s\n", strerror(errno));
+                       err(udev, "open /dev/null failed: %s\n", strerror(errno));
                if (outpipe[WRITE_END] > 0) {
                        dup2(outpipe[WRITE_END], STDOUT_FILENO);
                        close(outpipe[WRITE_END]);
@@ -210,14 +210,14 @@ static int run_program(const char *command, const char *subsystem,
                execv(argv[0], argv);
                if (errno == ENOENT || errno == ENOTDIR) {
                        /* may be on a filesytem which is not mounted right now */
-                       info("program '%s' not found\n", argv[0]);
+                       info(udev, "program '%s' not found\n", argv[0]);
                } else {
                        /* other problems */
-                       err("exec of program '%s' failed\n", argv[0]);
+                       err(udev, "exec of program '%s' failed\n", argv[0]);
                }
                _exit(1);
        case -1:
-               err("fork of '%s' failed: %s\n", argv[0], strerror(errno));
+               err(udev, "fork of '%s' failed: %s\n", argv[0], strerror(errno));
                return -1;
        default:
                /* read from child if requested */
@@ -260,7 +260,7 @@ static int run_program(const char *command, const char *subsystem,
                                                close(outpipe[READ_END]);
                                                outpipe[READ_END] = -1;
                                                if (count < 0) {
-                                                       err("stdin read failed: %s\n", strerror(errno));
+                                                       err(udev, "stdin read failed: %s\n", strerror(errno));
                                                        retval = -1;
                                                }
                                                continue;
@@ -273,14 +273,14 @@ static int run_program(const char *command, const char *subsystem,
                                                        memcpy(&result[respos], inbuf, count);
                                                        respos += count;
                                                } else {
-                                                       err("ressize %ld too short\n", (long)ressize);
+                                                       err(udev, "ressize %ld too short\n", (long)ressize);
                                                        retval = -1;
                                                }
                                        }
                                        pos = inbuf;
                                        while ((line = strsep(&pos, "\n")))
                                                if (pos || line[0] != '\0')
-                                                       info("'%s' (stdout) '%s'\n", argv[0], line);
+                                                       info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
                                }
 
                                /* get stderr */
@@ -294,14 +294,14 @@ static int run_program(const char *command, const char *subsystem,
                                                close(errpipe[READ_END]);
                                                errpipe[READ_END] = -1;
                                                if (count < 0)
-                                                       err("stderr read failed: %s\n", strerror(errno));
+                                                       err(udev, "stderr read failed: %s\n", strerror(errno));
                                                continue;
                                        }
                                        errbuf[count] = '\0';
                                        pos = errbuf;
                                        while ((line = strsep(&pos, "\n")))
                                                if (pos || line[0] != '\0')
-                                                       info("'%s' (stderr) '%s'\n", argv[0], line);
+                                                       info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
                                }
                        }
                        if (outpipe[READ_END] > 0)
@@ -312,18 +312,18 @@ static int run_program(const char *command, const char *subsystem,
                        /* return the childs stdout string */
                        if (result) {
                                result[respos] = '\0';
-                               dbg("result='%s'\n", result);
+                               dbg(udev, "result='%s'\n", result);
                                if (reslen)
                                        *reslen = respos;
                        }
                }
                waitpid(pid, &status, 0);
                if (WIFEXITED(status)) {
-                       info("'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
+                       info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
                        if (WEXITSTATUS(status) != 0)
                                retval = -1;
                } else {
-                       err("'%s' abnormal exit\n", argv[0]);
+                       err(udev, "'%s' abnormal exit\n", argv[0]);
                        retval = -1;
                }
        }
@@ -331,7 +331,7 @@ static int run_program(const char *command, const char *subsystem,
        return retval;
 }
 
-static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
+static int import_keys_into_env(struct udevice *udevice, const char *buf, size_t bufsize)
 {
        char line[LINE_SIZE];
        const char *bufline;
@@ -364,7 +364,7 @@ static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bu
                        continue;
 
                if (count >= sizeof(line)) {
-                       err("line too long, conf line skipped %s, line %d\n", udev_config_filename, lineno);
+                       err(udevice->udev, "line too long, skipped\n");
                        continue;
                }
 
@@ -373,14 +373,14 @@ static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bu
 
                linepos = line;
                if (get_key(&linepos, &variable, &value) == 0) {
-                       dbg("import '%s=%s'\n", variable, value);
+                       dbg(udevice->udev, "import '%s=%s'\n", variable, value);
 
                        /* handle device, renamed by external tool, returning new path */
                        if (strcmp(variable, "DEVPATH") == 0) {
-                               info("updating devpath from '%s' to '%s'\n", udev->dev->devpath, value);
-                               sysfs_device_set_values(udev->dev, value, NULL, NULL);
+                               info(udevice->udev, "updating devpath from '%s' to '%s'\n", udevice->dev->devpath, value);
+                               sysfs_device_set_values(udevice->udev, udevice->dev, value, NULL, NULL);
                        } else
-                               name_list_key_add(&udev->env_list, variable, value);
+                               name_list_key_add(udevice->udev, &udevice->env_list, variable, value);
                        setenv(variable, value, 1);
                }
        }
@@ -388,48 +388,48 @@ static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bu
        return 0;
 }
 
-static int import_file_into_env(struct udevice *udev, const char *filename)
+static int import_file_into_env(struct udevice *udevice, const char *filename)
 {
        char *buf;
        size_t bufsize;
 
        if (file_map(filename, &buf, &bufsize) != 0) {
-               err("can't open '%s': %s\n", filename, strerror(errno));
+               err(udevice->udev, "can't open '%s': %s\n", filename, strerror(errno));
                return -1;
        }
-       import_keys_into_env(udev, buf, bufsize);
+       import_keys_into_env(udevice, buf, bufsize);
        file_unmap(buf, bufsize);
 
        return 0;
 }
 
-static int import_program_into_env(struct udevice *udev, const char *program)
+static int import_program_into_env(struct udevice *udevice, const char *program)
 {
        char result[2048];
        size_t reslen;
 
-       if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen) != 0)
+       if (run_program(udevice->udev, program, udevice->dev->subsystem, result, sizeof(result), &reslen) != 0)
                return -1;
-       return import_keys_into_env(udev, result, reslen);
+       return import_keys_into_env(udevice, result, reslen);
 }
 
-static int import_parent_into_env(struct udevice *udev, const char *filter)
+static int import_parent_into_env(struct udevice *udevice, const char *filter)
 {
        struct sysfs_device *dev_parent;
        int rc = -1;
 
-       dev_parent = sysfs_device_get_parent(udev->dev);
+       dev_parent = sysfs_device_get_parent(udevice->udev, udevice->dev);
        if (dev_parent != NULL) {
                struct udevice *udev_parent;
                struct name_entry *name_loop;
 
-               dbg("found parent '%s', get the node name\n", dev_parent->devpath);
-               udev_parent = udev_device_init();
+               dbg(udevice->udev, "found parent '%s', get the node name\n", dev_parent->devpath);
+               udev_parent = udev_device_init(udevice->udev);
                if (udev_parent == NULL)
                        return -1;
                /* import the udev_db of the parent */
                if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
-                       dbg("import stored parent env '%s'\n", udev_parent->name);
+                       dbg(udevice->udev, "import stored parent env '%s'\n", udev_parent->name);
                        list_for_each_entry(name_loop, &udev_parent->env_list, node) {
                                char name[NAME_SIZE];
                                char *pos;
@@ -440,23 +440,23 @@ static int import_parent_into_env(struct udevice *udev, const char *filter)
                                        pos[0] = '\0';
                                        pos++;
                                        if (fnmatch(filter, name, 0) == 0) {
-                                               dbg("import key '%s'\n", name_loop->name);
-                                               name_list_add(&udev->env_list, name_loop->name, 0);
+                                               dbg(udevice->udev, "import key '%s'\n", name_loop->name);
+                                               name_list_add(udevice->udev, &udevice->env_list, name_loop->name, 0);
                                                setenv(name, pos, 1);
                                        } else
-                                               dbg("skip key '%s'\n", name_loop->name);
+                                               dbg(udevice->udev, "skip key '%s'\n", name_loop->name);
                                }
                        }
                        rc = 0;
                } else
-                       dbg("parent not found in database\n");
+                       dbg(udevice->udev, "parent not found in database\n");
                udev_device_cleanup(udev_parent);
        }
 
        return rc;
 }
 
-static int pass_env_to_socket(const char *sockpath, const char *devpath, const char *action)
+static int pass_env_to_socket(struct udev *udev, const char *sockpath, const char *devpath, const char *action)
 {
        int sock;
        struct sockaddr_un saddr;
@@ -468,7 +468,7 @@ static int pass_env_to_socket(const char *sockpath, const char *devpath, const c
        ssize_t count;
        int retval = 0;
 
-       dbg("pass environment to socket '%s'\n", sockpath);
+       dbg(udev, "pass environment to socket '%s'\n", sockpath);
        sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
        memset(&saddr, 0x00, sizeof(struct sockaddr_un));
        saddr.sun_family = AF_LOCAL;
@@ -498,27 +498,27 @@ static int pass_env_to_socket(const char *sockpath, const char *devpath, const c
        count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
        if (count < 0)
                retval = -1;
-       info("passed %zi bytes to socket '%s', \n", count, sockpath);
+       info(udev, "passed %zi bytes to socket '%s', \n", count, sockpath);
 
        close(sock);
        return retval;
 }
 
-int udev_rules_run(struct udevice *udev)
+int udev_rules_run(struct udevice *udevice)
 {
        struct name_entry *name_loop;
        int retval = 0;
 
-       dbg("executing run list\n");
-       list_for_each_entry(name_loop, &udev->run_list, node) {
+       dbg(udevice->udev, "executing run list\n");
+       list_for_each_entry(name_loop, &udevice->run_list, node) {
                if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0) {
-                       pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
+                       pass_env_to_socket(udevice->udev, &name_loop->name[strlen("socket:")], udevice->dev->devpath, udevice->action);
                } else {
                        char program[PATH_SIZE];
 
                        strlcpy(program, name_loop->name, sizeof(program));
-                       udev_rules_apply_format(udev, program, sizeof(program));
-                       if (run_program(program, udev->dev->subsystem, NULL, 0, NULL) != 0)
+                       udev_rules_apply_format(udevice, program, sizeof(program));
+                       if (run_program(udevice->udev, program, udevice->dev->subsystem, NULL, 0, NULL) != 0)
                                if (!name_loop->ignore_error)
                                        retval = -1;
                }
@@ -528,7 +528,7 @@ int udev_rules_run(struct udevice *udev)
 }
 
 #define WAIT_LOOP_PER_SECOND           50
-static int wait_for_file(struct udevice *udev, const char *file, int timeout)
+static int wait_for_file(struct udevice *udevice, const char *file, int timeout)
 {
        char filepath[PATH_SIZE];
        char devicepath[PATH_SIZE] = "";
@@ -537,8 +537,8 @@ static int wait_for_file(struct udevice *udev, const char *file, int timeout)
 
        /* a relative path is a device attribute */
        if (file[0] != '/') {
-               strlcpy(devicepath, sysfs_path, sizeof(devicepath));
-               strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
+               strlcpy(devicepath, udev_get_sys_path(udevice->udev), sizeof(devicepath));
+               strlcat(devicepath, udevice->dev->devpath, sizeof(devicepath));
 
                strlcpy(filepath, devicepath, sizeof(filepath));
                strlcat(filepath, "/", sizeof(filepath));
@@ -546,27 +546,27 @@ static int wait_for_file(struct udevice *udev, const char *file, int timeout)
                file = filepath;
        }
 
-       dbg("will wait %i sec for '%s'\n", timeout, file);
+       dbg(udevice->udev, "will wait %i sec for '%s'\n", timeout, file);
        while (--loop) {
                /* lookup file */
                if (stat(file, &stats) == 0) {
-                       info("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
+                       info(udevice->udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
                        return 0;
                }
                /* make sure, the device did not disappear in the meantime */
                if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
-                       info("device disappeared while waiting for '%s'\n", file);
+                       info(udevice->udev, "device disappeared while waiting for '%s'\n", file);
                        return -2;
                }
-               info("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
+               info(udevice->udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
                usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
        }
-       info("waiting for '%s' failed\n", file);
+       info(udevice->udev, "waiting for '%s' failed\n", file);
        return -1;
 }
 
 /* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
-static int attr_get_by_subsys_id(const char *attrstr, char *devpath, size_t len, char **attr)
+static int attr_get_by_subsys_id(struct udev *udev, const char *attrstr, char *devpath, size_t len, char **attr)
 {
        char subsys[NAME_SIZE];
        char *pos;
@@ -592,7 +592,7 @@ static int attr_get_by_subsys_id(const char *attrstr, char *devpath, size_t len,
                goto out;
        id[0] = '\0';
        id = &id[1];
-       if (sysfs_lookup_devpath_by_subsys_id(devpath, len, subsys, id)) {
+       if (sysfs_lookup_devpath_by_subsys_id(udev, devpath, len, subsys, id)) {
                if (attr != NULL) {
                        if (attrib[0] != '\0')
                                *attr = attrib;
@@ -643,7 +643,7 @@ static int attr_subst_subdir(char *attr, size_t len)
        return found;
 }
 
-void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
+void udev_rules_apply_format(struct udevice *udevice, char *string, size_t maxsize)
 {
        char temp[PATH_SIZE];
        char temp2[PATH_SIZE];
@@ -716,12 +716,12 @@ void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
                                        if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
                                                type = subst->type;
                                                tail = head + strlen(subst->name)+1;
-                                               dbg("will substitute format name '%s'\n", subst->name);
+                                               dbg(udevice->udev, "will substitute format name '%s'\n", subst->name);
                                                goto found;
                                        }
                                }
                                head[0] = '$';
-                               err("unknown format variable '%s'\n", head);
+                               err(udevice->udev, "unknown format variable '%s'\n", head);
                        } else if (head[0] == '%') {
                                /* substitute format char */
                                if (head[1] == '\0')
@@ -734,71 +734,71 @@ void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
                                }
                                head[0] = '\0';
                                tail = head+1;
-                               len = get_format_len(&tail);
+                               len = get_format_len(udevice->udev, &tail);
                                for (subst = map; subst->name; subst++) {
                                        if (tail[0] == subst->fmt) {
                                                type = subst->type;
                                                tail++;
-                                               dbg("will substitute format char '%c'\n", subst->fmt);
+                                               dbg(udevice->udev, "will substitute format char '%c'\n", subst->fmt);
                                                goto found;
                                        }
                                }
                                head[0] = '%';
-                               err("unknown format char '%c'\n", tail[0]);
+                               err(udevice->udev, "unknown format char '%c'\n", tail[0]);
                        }
                        head++;
                }
                break;
 found:
-               attr = get_format_attribute(&tail);
+               attr = get_format_attribute(udevice->udev, &tail);
                strlcpy(temp, tail, sizeof(temp));
-               dbg("format=%i, string='%s', tail='%s'\n", type ,string, tail);
+               dbg(udevice->udev, "format=%i, string='%s', tail='%s'\n", type ,string, tail);
 
                switch (type) {
                case SUBST_DEVPATH:
-                       strlcat(string, udev->dev->devpath, maxsize);
-                       dbg("substitute devpath '%s'\n", udev->dev->devpath);
+                       strlcat(string, udevice->dev->devpath, maxsize);
+                       dbg(udevice->udev, "substitute devpath '%s'\n", udevice->dev->devpath);
                        break;
                case SUBST_KERNEL:
-                       strlcat(string, udev->dev->kernel, maxsize);
-                       dbg("substitute kernel name '%s'\n", udev->dev->kernel);
+                       strlcat(string, udevice->dev->kernel, maxsize);
+                       dbg(udevice->udev, "substitute kernel name '%s'\n", udevice->dev->kernel);
                        break;
                case SUBST_KERNEL_NUMBER:
-                       strlcat(string, udev->dev->kernel_number, maxsize);
-                       dbg("substitute kernel number '%s'\n", udev->dev->kernel_number);
+                       strlcat(string, udevice->dev->kernel_number, maxsize);
+                       dbg(udevice->udev, "substitute kernel number '%s'\n", udevice->dev->kernel_number);
                        break;
                case SUBST_ID:
-                       if (udev->dev_parent != NULL) {
-                               strlcat(string, udev->dev_parent->kernel, maxsize);
-                               dbg("substitute id '%s'\n", udev->dev_parent->kernel);
+                       if (udevice->dev_parent != NULL) {
+                               strlcat(string, udevice->dev_parent->kernel, maxsize);
+                               dbg(udevice->udev, "substitute id '%s'\n", udevice->dev_parent->kernel);
                        }
                        break;
                case SUBST_DRIVER:
-                       if (udev->dev_parent != NULL) {
-                               strlcat(string, udev->dev_parent->driver, maxsize);
-                               dbg("substitute driver '%s'\n", udev->dev_parent->driver);
+                       if (udevice->dev_parent != NULL) {
+                               strlcat(string, udevice->dev_parent->driver, maxsize);
+                               dbg(udevice->udev, "substitute driver '%s'\n", udevice->dev_parent->driver);
                        }
                        break;
                case SUBST_MAJOR:
-                       sprintf(temp2, "%d", major(udev->devt));
+                       sprintf(temp2, "%d", major(udevice->devt));
                        strlcat(string, temp2, maxsize);
-                       dbg("substitute major number '%s'\n", temp2);
+                       dbg(udevice->udev, "substitute major number '%s'\n", temp2);
                        break;
                case SUBST_MINOR:
-                       sprintf(temp2, "%d", minor(udev->devt));
+                       sprintf(temp2, "%d", minor(udevice->devt));
                        strlcat(string, temp2, maxsize);
-                       dbg("substitute minor number '%s'\n", temp2);
+                       dbg(udevice->udev, "substitute minor number '%s'\n", temp2);
                        break;
                case SUBST_RESULT:
-                       if (udev->program_result[0] == '\0')
+                       if (udevice->program_result[0] == '\0')
                                break;
                        /* get part part of the result string */
                        i = 0;
                        if (attr != NULL)
                                i = strtoul(attr, &rest, 10);
                        if (i > 0) {
-                               dbg("request part #%d of result string\n", i);
-                               cpos = udev->program_result;
+                               dbg(udevice->udev, "request part #%d of result string\n", i);
+                               cpos = udevice->program_result;
                                while (--i) {
                                        while (cpos[0] != '\0' && !isspace(cpos[0]))
                                                cpos++;
@@ -806,7 +806,7 @@ found:
                                                cpos++;
                                }
                                if (i > 0) {
-                                       err("requested part of result string not found\n");
+                                       err(udevice->udev, "requested part of result string not found\n");
                                        break;
                                }
                                strlcpy(temp2, cpos, sizeof(temp2));
@@ -817,44 +817,44 @@ found:
                                                cpos[0] = '\0';
                                }
                                strlcat(string, temp2, maxsize);
-                               dbg("substitute part of result string '%s'\n", temp2);
+                               dbg(udevice->udev, "substitute part of result string '%s'\n", temp2);
                        } else {
-                               strlcat(string, udev->program_result, maxsize);
-                               dbg("substitute result string '%s'\n", udev->program_result);
+                               strlcat(string, udevice->program_result, maxsize);
+                               dbg(udevice->udev, "substitute result string '%s'\n", udevice->program_result);
                        }
                        break;
                case SUBST_ATTR:
                        if (attr == NULL)
-                               err("missing file parameter for attr\n");
+                               err(udevice->udev, "missing file parameter for attr\n");
                        else {
                                char devpath[PATH_SIZE];
                                char *attrib;
                                const char *value = NULL;
                                size_t size;
 
-                               if (attr_get_by_subsys_id(attr, devpath, sizeof(devpath), &attrib)) {
+                               if (attr_get_by_subsys_id(udevice->udev, attr, devpath, sizeof(devpath), &attrib)) {
                                        if (attrib != NULL)
-                                               value = sysfs_attr_get_value(devpath, attrib);
+                                               value = sysfs_attr_get_value(udevice->udev, devpath, attrib);
                                        else
                                                break;
                                }
 
                                /* try the current device, other matches may have selected */
-                               if (value == NULL && udev->dev_parent != NULL && udev->dev_parent != udev->dev)
-                                       value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
+                               if (value == NULL && udevice->dev_parent != NULL && udevice->dev_parent != udevice->dev)
+                                       value = sysfs_attr_get_value(udevice->udev, udevice->dev_parent->devpath, attr);
 
                                /* look at all devices along the chain of parents */
                                if (value == NULL) {
-                                       struct sysfs_device *dev_parent = udev->dev;
+                                       struct sysfs_device *dev_parent = udevice->dev;
 
                                        do {
-                                               dbg("looking at '%s'\n", dev_parent->devpath);
-                                               value = sysfs_attr_get_value(dev_parent->devpath, attr);
+                                               dbg(udevice->udev, "looking at '%s'\n", dev_parent->devpath);
+                                               value = sysfs_attr_get_value(udevice->udev, dev_parent->devpath, attr);
                                                if (value != NULL) {
                                                        strlcpy(temp2, value, sizeof(temp2));
                                                        break;
                                                }
-                                               dev_parent = sysfs_device_get_parent(dev_parent);
+                                               dev_parent = sysfs_device_get_parent(udevice->udev, dev_parent);
                                        } while (dev_parent != NULL);
                                }
 
@@ -869,59 +869,59 @@ found:
                                        temp2[--size] = '\0';
                                count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
                                if (count > 0)
-                                       info("%i character(s) replaced\n" , count);
+                                       info(udevice->udev, "%i character(s) replaced\n" , count);
                                strlcat(string, temp2, maxsize);
-                               dbg("substitute sysfs value '%s'\n", temp2);
+                               dbg(udevice->udev, "substitute sysfs value '%s'\n", temp2);
                        }
                        break;
                case SUBST_PARENT:
                        {
                                struct sysfs_device *dev_parent;
 
-                               dev_parent = sysfs_device_get_parent(udev->dev);
+                               dev_parent = sysfs_device_get_parent(udevice->udev, udevice->dev);
                                if (dev_parent != NULL) {
                                        struct udevice *udev_parent;
 
-                                       dbg("found parent '%s', get the node name\n", dev_parent->devpath);
-                                       udev_parent = udev_device_init();
+                                       dbg(udevice->udev, "found parent '%s', get the node name\n", dev_parent->devpath);
+                                       udev_parent = udev_device_init(udevice->udev);
                                        if (udev_parent != NULL) {
                                                /* lookup the name in the udev_db with the DEVPATH of the parent */
                                                if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
                                                        strlcat(string, udev_parent->name, maxsize);
-                                                       dbg("substitute parent node name'%s'\n", udev_parent->name);
+                                                       dbg(udevice->udev, "substitute parent node name'%s'\n", udev_parent->name);
                                                } else
-                                                       dbg("parent not found in database\n");
+                                                       dbg(udevice->udev, "parent not found in database\n");
                                                udev_device_cleanup(udev_parent);
                                        }
                                }
                        }
                        break;
                case SUBST_TEMP_NODE:
-                       if (udev->tmp_node[0] == '\0' && major(udev->devt) > 0) {
-                               dbg("create temporary device node for callout\n");
-                               snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
-                                        udev_root, major(udev->devt), minor(udev->devt));
-                               udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
-                               udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
+                       if (udevice->tmp_node[0] == '\0' && major(udevice->devt) > 0) {
+                               dbg(udevice->udev, "create temporary device node for callout\n");
+                               snprintf(udevice->tmp_node, sizeof(udevice->tmp_node), "%s/.tmp-%u-%u",
+                                        udev_get_dev_path(udevice->udev), major(udevice->devt), minor(udevice->devt));
+                               udevice->tmp_node[sizeof(udevice->tmp_node)-1] = '\0';
+                               udev_node_mknod(udevice, udevice->tmp_node, udevice->devt, 0600, 0, 0);
                        }
-                       strlcat(string, udev->tmp_node, maxsize);
-                       dbg("substitute temporary device node name '%s'\n", udev->tmp_node);
+                       strlcat(string, udevice->tmp_node, maxsize);
+                       dbg(udevice->udev, "substitute temporary device node name '%s'\n", udevice->tmp_node);
                        break;
                case SUBST_NAME:
-                       if (udev->name[0] == '\0') {
-                               strlcat(string, udev->dev->kernel, maxsize);
-                               dbg("substitute udev->kernel '%s'\n", udev->name);
+                       if (udevice->name[0] == '\0') {
+                               strlcat(string, udevice->dev->kernel, maxsize);
+                               dbg(udevice->udev, "substitute udevice->kernel '%s'\n", udevice->name);
                        } else {
-                               strlcat(string, udev->name, maxsize);
-                               dbg("substitute udev->name '%s'\n", udev->name);
+                               strlcat(string, udevice->name, maxsize);
+                               dbg(udevice->udev, "substitute udevice->name '%s'\n", udevice->name);
                        }
                        break;
                case SUBST_LINKS:
-                       if (!list_empty(&udev->symlink_list)) {
+                       if (!list_empty(&udevice->symlink_list)) {
                                struct name_entry *name_loop;
                                char symlinks[PATH_SIZE] = "";
 
-                               list_for_each_entry(name_loop, &udev->symlink_list, node) {
+                               list_for_each_entry(name_loop, &udevice->symlink_list, node) {
                                        strlcat(symlinks, name_loop->name, sizeof(symlinks));
                                        strlcat(symlinks, " ", sizeof(symlinks));
                                }
@@ -930,34 +930,34 @@ found:
                        }
                        break;
                case SUBST_ROOT:
-                       strlcat(string, udev_root, maxsize);
-                       dbg("substitute udev_root '%s'\n", udev_root);
+                       strlcat(string, udev_get_dev_path(udevice->udev), maxsize);
+                       dbg(udevice->udev, "substitute udev_root '%s'\n", udev_get_dev_path(udevice->udev));
                        break;
                case SUBST_SYS:
-                       strlcat(string, sysfs_path, maxsize);
-                       dbg("substitute sysfs_path '%s'\n", sysfs_path);
+                       strlcat(string, udev_get_sys_path(udevice->udev), maxsize);
+                       dbg(udevice->udev, "substitute sys_path '%s'\n", udev_get_sys_path(udevice->udev));
                        break;
                case SUBST_ENV:
                        if (attr == NULL) {
-                               dbg("missing attribute\n");
+                               dbg(udevice->udev, "missing attribute\n");
                                break;
                        }
                        pos = getenv(attr);
                        if (pos == NULL) {
-                               dbg("env '%s' not available\n", attr);
+                               dbg(udevice->udev, "env '%s' not available\n", attr);
                                break;
                        }
-                       dbg("substitute env '%s=%s'\n", attr, pos);
+                       dbg(udevice->udev, "substitute env '%s=%s'\n", attr, pos);
                        strlcat(string, pos, maxsize);
                        break;
                default:
-                       err("unknown substitution type=%i\n", type);
+                       err(udevice->udev, "unknown substitution type=%i\n", type);
                        break;
                }
                /* possibly truncate to format-char specified length */
                if (len >= 0 && len < (int)strlen(head)) {
                        head[len] = '\0';
-                       dbg("truncate to %i chars, subtitution string becomes '%s'\n", len, head);
+                       dbg(udevice->udev, "truncate to %i chars, subtitution string becomes '%s'\n", len, head);
                }
                strlcat(string, temp, maxsize);
        }
@@ -973,7 +973,7 @@ static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
        return rule->buf + pair->key_name_off;
 }
 
-static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
+static int match_key(struct udev *udev, const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
 {
        char value[PATH_SIZE];
        char *key_value;
@@ -987,7 +987,7 @@ static int match_key(const char *key_name, struct udev_rule *rule, struct key *k
        /* look for a matching string, parts are separated by '|' */
        strlcpy(value, rule->buf + key->val_off, sizeof(value));
        key_value = value;
-       dbg("key %s value='%s'\n", key_name, key_value);
+       dbg(udev, "key %s value='%s'\n", key_name, key_value);
        while (key_value) {
                pos = strchr(key_value, '|');
                if (pos) {
@@ -995,7 +995,7 @@ static int match_key(const char *key_name, struct udev_rule *rule, struct key *k
                        pos++;
                }
 
-               dbg("match %s '%s' <-> '%s'\n", key_name, key_value, val);
+               dbg(udev, "match %s '%s' <-> '%s'\n", key_name, key_value, val);
                match = (fnmatch(key_value, val, 0) == 0);
                if (match)
                        break;
@@ -1004,38 +1004,38 @@ static int match_key(const char *key_name, struct udev_rule *rule, struct key *k
        }
 
        if (match && (key->operation == KEY_OP_MATCH)) {
-               dbg("%s is true (matching value)\n", key_name);
+               dbg(udev, "%s is true (matching value)\n", key_name);
                return 0;
        }
        if (!match && (key->operation == KEY_OP_NOMATCH)) {
-               dbg("%s is true (non-matching value)\n", key_name);
+               dbg(udev, "%s is true (non-matching value)\n", key_name);
                return 0;
        }
        return -1;
 }
 
 /* match a single rule against a given device and possibly its parent devices */
-static int match_rule(struct udevice *udev, struct udev_rule *rule)
+static int match_rule(struct udevice *udevice, struct udev_rule *rule)
 {
        int i;
 
-       if (match_key("ACTION", rule, &rule->action, udev->action))
+       if (match_key(udevice->udev, "ACTION", rule, &rule->action, udevice->action))
                goto nomatch;
 
-       if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
+       if (match_key(udevice->udev, "KERNEL", rule, &rule->kernel, udevice->dev->kernel))
                goto nomatch;
 
-       if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
+       if (match_key(udevice->udev, "SUBSYSTEM", rule, &rule->subsystem, udevice->dev->subsystem))
                goto nomatch;
 
-       if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
+       if (match_key(udevice->udev, "DEVPATH", rule, &rule->devpath, udevice->dev->devpath))
                goto nomatch;
 
-       if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
+       if (match_key(udevice->udev, "DRIVER", rule, &rule->driver, udevice->dev->driver))
                goto nomatch;
 
        /* match NAME against a value assigned by an earlier rule */
-       if (match_key("NAME", rule, &rule->name, udev->name))
+       if (match_key(udevice->udev, "NAME", rule, &rule->name, udevice->name))
                goto nomatch;
 
        /* match against current list of symlinks */
@@ -1044,8 +1044,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
                struct name_entry *name_loop;
                int match = 0;
 
-               list_for_each_entry(name_loop, &udev->symlink_list, node) {
-                       if (match_key("SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) {
+               list_for_each_entry(name_loop, &udevice->symlink_list, node) {
+                       if (match_key(udevice->udev, "SYMLINK", rule, &rule->symlink_match, name_loop->name) == 0) {
                                match = 1;
                                break;
                        }
@@ -1064,10 +1064,10 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
                        const char *value = getenv(key_name);
 
                        if (!value) {
-                               dbg("ENV{'%s'} is not set, treat as empty\n", key_name);
+                               dbg(udevice->udev, "ENV{'%s'} is not set, treat as empty\n", key_name);
                                value = "";
                        }
-                       if (match_key("ENV", rule, &pair->key, value))
+                       if (match_key(udevice->udev, "ENV", rule, &pair->key, value))
                                goto nomatch;
                }
        }
@@ -1081,10 +1081,10 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
                int match;
 
                strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
-               udev_rules_apply_format(udev, filename, sizeof(filename));
+               udev_rules_apply_format(udevice, filename, sizeof(filename));
 
-               if (attr_get_by_subsys_id(filename, devpath, sizeof(devpath), &attr)) {
-                       strlcpy(filename, sysfs_path, sizeof(filename));
+               if (attr_get_by_subsys_id(udevice->udev, filename, devpath, sizeof(devpath), &attr)) {
+                       strlcpy(filename, udev_get_sys_path(udevice->udev), sizeof(filename));
                        strlcat(filename, devpath, sizeof(filename));
                        if (attr != NULL) {
                                strlcat(filename, "/", sizeof(filename));
@@ -1093,8 +1093,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
                } else if (filename[0] != '/') {
                        char tmp[PATH_SIZE];
 
-                       strlcpy(tmp, sysfs_path, sizeof(tmp));
-                       strlcat(tmp, udev->dev->devpath, sizeof(tmp));
+                       strlcpy(tmp, udev_get_sys_path(udevice->udev), sizeof(tmp));
+                       strlcat(tmp, udevice->dev->devpath, sizeof(tmp));
                        strlcat(tmp, "/", sizeof(tmp));
                        strlcat(tmp, filename, sizeof(tmp));
                        strlcpy(filename, tmp, sizeof(filename));
@@ -1103,10 +1103,10 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
                attr_subst_subdir(filename, sizeof(filename));
 
                match = (stat(filename, &statbuf) == 0);
-               info("'%s' %s", filename, match ? "exists\n" : "does not exist\n");
+               info(udevice->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
                if (match && rule->test_mode_mask > 0) {
                        match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
-                       info("'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
+                       info(udevice->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
                             match ? "matches" : "does not match",
                             rule->test_mode_mask);
                }
@@ -1114,7 +1114,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
                        goto nomatch;
                if (!match && rule->test.operation == KEY_OP_MATCH)
                        goto nomatch;
-               dbg("TEST key is true\n");
+               dbg(udevice->udev, "TEST key is true\n");
        }
 
        if (rule->wait_for.operation != KEY_OP_UNSET) {
@@ -1122,8 +1122,8 @@ static in