From: Kay Sievers Date: Sat, 6 Sep 2008 13:45:31 +0000 (+0200) Subject: use libudev code, unify logging, pass udev context around everywhere X-Git-Tag: 174~1588 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d563a17f3967890331daf08d43f2f005418139b;p=systemd use libudev code, unify logging, pass udev context around everywhere --- diff --git a/extras/ata_id/Makefile.am b/extras/ata_id/Makefile.am index 81332dd3..7e7fd2b7 100644 --- a/extras/ata_id/Makefile.am +++ b/extras/ata_id/Makefile.am @@ -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 diff --git a/extras/ata_id/ata_id.c b/extras/ata_id/ata_id.c index 125c3f3d..43d9516a 100644 --- a/extras/ata_id/ata_id.c +++ b/extras/ata_id/ata_id.c @@ -1,7 +1,7 @@ /* * ata_id - reads product/serial number from ATA drives * - * Copyright (C) 2005 Kay Sievers + * Copyright (C) 2005-2008 Kay Sievers * * 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 @@ -28,30 +28,12 @@ #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; } diff --git a/extras/cdrom_id/Makefile.am b/extras/cdrom_id/Makefile.am index 88de1379..dfc98850 100644 --- a/extras/cdrom_id/Makefile.am +++ b/extras/cdrom_id/Makefile.am @@ -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 diff --git a/extras/cdrom_id/cdrom_id.c b/extras/cdrom_id/cdrom_id.c index 9e274718..dbe80ea7 100644 --- a/extras/cdrom_id/cdrom_id.c +++ b/extras/cdrom_id/cdrom_id.c @@ -46,37 +46,17 @@ 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; } diff --git a/extras/edd_id/Makefile.am b/extras/edd_id/Makefile.am index fa4f18c9..cc693967 100644 --- a/extras/edd_id/Makefile.am +++ b/extras/edd_id/Makefile.am @@ -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 diff --git a/extras/edd_id/edd_id.c b/extras/edd_id/edd_id.c index 9d745c50..b53700e7 100644 --- a/extras/edd_id/edd_id.c +++ b/extras/edd_id/edd_id.c @@ -25,33 +25,16 @@ #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; } diff --git a/extras/floppy/Makefile.am b/extras/floppy/Makefile.am index 4854d524..7879e7c2 100644 --- a/extras/floppy/Makefile.am +++ b/extras/floppy/Makefile.am @@ -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 \ diff --git a/extras/floppy/create_floppy_devices.c b/extras/floppy/create_floppy_devices.c index 3e73aa83..2c7d44b1 100644 --- a/extras/floppy/create_floppy_devices.c +++ b/extras/floppy/create_floppy_devices.c @@ -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; } diff --git a/extras/fstab_import/79-fstab_import.rules b/extras/fstab_import/79-fstab_import.rules index ccd718c5..44accde4 100644 --- a/extras/fstab_import/79-fstab_import.rules +++ b/extras/fstab_import/79-fstab_import.rules @@ -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}" diff --git a/extras/fstab_import/Makefile.am b/extras/fstab_import/Makefile.am index 0ee7efb9..902ab1e2 100644 --- a/extras/fstab_import/Makefile.am +++ b/extras/fstab_import/Makefile.am @@ -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 diff --git a/extras/fstab_import/fstab_import.c b/extras/fstab_import/fstab_import.c index 2d24cfb3..0f4685ae 100644 --- a/extras/fstab_import/fstab_import.c +++ b/extras/fstab_import/fstab_import.c @@ -13,6 +13,8 @@ #define _GNU_SOURCE 1 #endif +#include "config.h" + #include #include #include @@ -27,54 +29,32 @@ #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; } diff --git a/extras/scsi_id/Makefile.am b/extras/scsi_id/Makefile.am index 7f890b04..fc5c4778 100644 --- a/extras/scsi_id/Makefile.am +++ b/extras/scsi_id/Makefile.am @@ -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 diff --git a/extras/scsi_id/scsi_id.c b/extras/scsi_id/scsi_id.c index 23bcb946..d692a867 100644 --- a/extras/scsi_id/scsi_id.c +++ b/extras/scsi_id/scsi_id.c @@ -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; } diff --git a/extras/scsi_id/scsi_id.h b/extras/scsi_id/scsi_id.h index 680c39d9..48e4a582 100644 --- a/extras/scsi_id/scsi_id.h +++ b/extras/scsi_id/scsi_id.h @@ -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); /* diff --git a/extras/scsi_id/scsi_serial.c b/extras/scsi_id/scsi_serial.c index 104db3d9..757f41d1 100644 --- a/extras/scsi_id/scsi_serial.c +++ b/extras/scsi_id/scsi_serial.c @@ -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 diff --git a/extras/usb_id/Makefile.am b/extras/usb_id/Makefile.am index 0de004a2..9d7e7724 100644 --- a/extras/usb_id/Makefile.am +++ b/extras/usb_id/Makefile.am @@ -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 \ diff --git a/extras/usb_id/usb_id.c b/extras/usb_id/usb_id.c index 92492c86..fb4c28ac 100644 --- a/extras/usb_id/usb_id.c +++ b/extras/usb_id/usb_id.c @@ -28,30 +28,12 @@ #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; diff --git a/extras/volume_id/Makefile.am b/extras/volume_id/Makefile.am index bc1b262f..fe9bef7b 100644 --- a/extras/volume_id/Makefile.am +++ b/extras/volume_id/Makefile.am @@ -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 diff --git a/extras/volume_id/vol_id.c b/extras/volume_id/vol_id.c index b3f810fc..dcfb0bb9 100644 --- a/extras/volume_id/vol_id.c +++ b/extras/volume_id/vol_id.c @@ -39,43 +39,20 @@ #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; } diff --git a/udev/Makefile.am b/udev/Makefile.am index 450c9489..4a30bf9e 100644 --- a/udev/Makefile.am +++ b/udev/Makefile.am @@ -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 diff --git a/udev/lib/Makefile.am b/udev/lib/Makefile.am index f1f5fb74..b7dd373d 100644 --- a/udev/lib/Makefile.am +++ b/udev/lib/Makefile.am @@ -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 diff --git a/udev/lib/exported_symbols b/udev/lib/exported_symbols index 4db23b22..66275f4f 100644 --- a/udev/lib/exported_symbols +++ b/udev/lib/exported_symbols @@ -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 diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index 629cadbc..43ed2f3d 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -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; } diff --git a/udev/lib/libudev-enumerate.c b/udev/lib/libudev-enumerate.c index e3198665..6aa04d8f 100644 --- a/udev/lib/libudev-enumerate.c +++ b/udev/lib/libudev-enumerate.c @@ -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); diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c index 92617e88..09845576 100644 --- a/udev/lib/libudev-monitor.c +++ b/udev/lib/libudev-monitor.c @@ -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; } diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h index ffdf5715..ef26ec7e 100644 --- a/udev/lib/libudev-private.h +++ b/udev/lib/libudev-private.h @@ -20,31 +20,39 @@ #ifndef _LIBUDEV_PRIVATE_H_ #define _LIBUDEV_PRIVATE_H_ +#include "config.h" + +#include #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); diff --git a/udev/lib/libudev.c b/udev/lib/libudev.c index a8a8a5ac..5a00f44d 100644 --- a/udev/lib/libudev.c +++ b/udev/lib/libudev.c @@ -26,8 +26,7 @@ #include #include #include -#include -#include +#include #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 = 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; } diff --git a/udev/lib/libudev.h b/udev/lib/libudev.h index 30753a19..98652c59 100644 --- a/udev/lib/libudev.h +++ b/udev/lib/libudev.h @@ -20,6 +20,8 @@ #ifndef _LIBUDEV_H_ #define _LIBUDEV_H_ +#include + /* 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 index 6766ac3e..00000000 --- a/udev/logging.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * simple logging functions that can be expanded into nothing - * - * Copyright (C) 2003-2004 Greg Kroah-Hartman - * Copyright (C) 2004-2006 Kay Sievers - * - * 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 -#include -#include - -#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 diff --git a/udev/test-udev.c b/udev/test-udev.c index 07628f74..4e782a0d 100644 --- a/udev/test-udev.c +++ b/udev/test-udev.c @@ -35,20 +35,6 @@ #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; diff --git a/udev/udev-control.c b/udev/udev-control.c index 4e339ff4..96e4b29b 100644 --- a/udev/udev-control.c +++ b/udev/udev-control.c @@ -32,12 +32,13 @@ #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; } diff --git a/udev/udev.h b/udev/udev.h index 00f2fd82..1dd55f6a 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -20,12 +20,16 @@ #ifndef _UDEV_H_ #define _UDEV_H_ +#include "config.h" + #include #include #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 index 8a1dac51..00000000 --- a/udev/udev_config.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (C) 2003-2004 Greg Kroah-Hartman - * Copyright (C) 2004-2005 Kay Sievers - * - * 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 -#include -#include -#include -#include -#include -#include -#include - -#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); -} diff --git a/udev/udev_db.c b/udev/udev_db.c index e3f0f7fd..e89f2c2a 100644 --- a/udev/udev_db.c +++ b/udev/udev_db.c @@ -34,19 +34,19 @@ #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); diff --git a/udev/udev_device.c b/udev/udev_device.c index 130c7143..5ae66743 100644 --- a/udev/udev_device.c +++ b/udev/udev_device.c @@ -34,48 +34,50 @@ #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); diff --git a/udev/udev_device_event.c b/udev/udev_device_event.c index 8ad79644..6b406233 100644 --- a/udev/udev_device_event.c +++ b/udev/udev_device_event.c @@ -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; diff --git a/udev/udev_node.c b/udev/udev_node.c index 33183c8d..462f1b63 100644 --- a/udev/udev_node.c +++ b/udev/udev_node.c @@ -35,69 +35,68 @@ #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; } diff --git a/udev/udev_rules.c b/udev/udev_rules.c index 4719cab5..b864599a 100644 --- a/udev/udev_rules.c +++ b/udev/udev_rules.c @@ -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]" 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 int match_rule(struct udevice *udev, struct udev_rule *rule) int found; strlcpy(filename, key_val(rule, &rule->wait_for), sizeof(filename)); - udev_rules_apply_format(udev, filename, sizeof(filename)); - found = (wait_for_file(udev, filename, 10) == 0); + udev_rules_apply_format(udevice, filename, sizeof(filename)); + found = (wait_for_file(udevice, filename, 10) == 0); if (!found && (rule->wait_for.operation != KEY_OP_NOMATCH)) goto nomatch; } @@ -1142,14 +1142,14 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule) char val[VALUE_SIZE]; size_t len; - if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) { + if (attr_get_by_subsys_id(udevice->udev, key_name, devpath, sizeof(devpath), &attrib)) { if (attrib != NULL) - value = sysfs_attr_get_value(devpath, attrib); + value = sysfs_attr_get_value(udevice->udev, devpath, attrib); else goto nomatch; } if (value == NULL) - value = sysfs_attr_get_value(udev->dev->devpath, key_name); + value = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, key_name); if (value == NULL) goto nomatch; strlcpy(val, value, sizeof(val)); @@ -1160,27 +1160,27 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule) len = strlen(val); while (len > 0 && isspace(val[len-1])) val[--len] = '\0'; - dbg("removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val); + dbg(udevice->udev, "removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val); } - if (match_key("ATTR", rule, &pair->key, val)) + if (match_key(udevice->udev, "ATTR", rule, &pair->key, val)) goto nomatch; } } /* walk up the chain of parent devices and find a match */ - udev->dev_parent = udev->dev; + udevice->dev_parent = udevice->dev; while (1) { /* check for matching kernel device name */ - if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel)) + if (match_key(udevice->udev, "KERNELS", rule, &rule->kernels, udevice->dev_parent->kernel)) goto try_parent; /* check for matching subsystem value */ - if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem)) + if (match_key(udevice->udev, "SUBSYSTEMS", rule, &rule->subsystems, udevice->dev_parent->subsystem)) goto try_parent; /* check for matching driver */ - if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver)) + if (match_key(udevice->udev, "DRIVERS", rule, &rule->drivers, udevice->dev_parent->driver)) goto try_parent; /* check for matching sysfs attribute pairs */ @@ -1195,9 +1195,9 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule) char val[VALUE_SIZE]; size_t len; - value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name); + value = sysfs_attr_get_value(udevice->udev, udevice->dev_parent->devpath, key_name); if (value == NULL) - value = sysfs_attr_get_value(udev->dev->devpath, key_name); + value = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, key_name); if (value == NULL) goto try_parent; strlcpy(val, value, sizeof(val)); @@ -1208,10 +1208,10 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule) len = strlen(val); while (len > 0 && isspace(val[len-1])) val[--len] = '\0'; - dbg("removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val); + dbg(udevice->udev, "removed %zi trailing whitespace chars from '%s'\n", strlen(val)-len, val); } - if (match_key("ATTRS", rule, &pair->key, val)) + if (match_key(udevice->udev, "ATTRS", rule, &pair->key, val)) goto try_parent; } } @@ -1220,12 +1220,12 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule) break; try_parent: /* move to parent device */ - dbg("try parent sysfs device\n"); - udev->dev_parent = sysfs_device_get_parent(udev->dev_parent); - if (udev->dev_parent == NULL) + dbg(udevice->udev, "try parent sysfs device\n"); + udevice->dev_parent = sysfs_device_get_parent(udevice->udev, udevice->dev_parent); + if (udevice->dev_parent == NULL) goto nomatch; - dbg("looking at dev_parent->devpath='%s'\n", udev->dev_parent->devpath); - dbg("looking at dev_parent->kernel='%s'\n", udev->dev_parent->kernel); + dbg(udevice->udev, "looking at dev_parent->devpath='%s'\n", udevice->dev_parent->devpath); + dbg(udevice->udev, "looking at dev_parent->kernel='%s'\n", udevice->dev_parent->kernel); } /* execute external program */ @@ -1234,34 +1234,34 @@ try_parent: char result[PATH_SIZE]; strlcpy(program, key_val(rule, &rule->program), sizeof(program)); - udev_rules_apply_format(udev, program, sizeof(program)); - if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL) != 0) { - dbg("PROGRAM is false\n"); - udev->program_result[0] = '\0'; + udev_rules_apply_format(udevice, program, sizeof(program)); + if (run_program(udevice->udev, program, udevice->dev->subsystem, result, sizeof(result), NULL) != 0) { + dbg(udevice->udev, "PROGRAM is false\n"); + udevice->program_result[0] = '\0'; if (rule->program.operation != KEY_OP_NOMATCH) goto nomatch; } else { int count; - dbg("PROGRAM matches\n"); + dbg(udevice->udev, "PROGRAM matches\n"); remove_trailing_chars(result, '\n'); if (rule->string_escape == ESCAPE_UNSET || rule->string_escape == ESCAPE_REPLACE) { count = replace_chars(result, ALLOWED_CHARS_INPUT); if (count > 0) - info("%i character(s) replaced\n" , count); + info(udevice->udev, "%i character(s) replaced\n" , count); } - dbg("result is '%s'\n", result); - strlcpy(udev->program_result, result, sizeof(udev->program_result)); - dbg("PROGRAM returned successful\n"); + dbg(udevice->udev, "result is '%s'\n", result); + strlcpy(udevice->program_result, result, sizeof(udevice->program_result)); + dbg(udevice->udev, "PROGRAM returned successful\n"); if (rule->program.operation == KEY_OP_NOMATCH) goto nomatch; } - dbg("PROGRAM key is true\n"); + dbg(udevice->udev, "PROGRAM key is true\n"); } /* check for matching result of external program */ - if (match_key("RESULT", rule, &rule->result, udev->program_result)) + if (match_key(udevice->udev, "RESULT", rule, &rule->result, udevice->program_result)) goto nomatch; /* import variables returned from program or or file into environment */ @@ -1270,24 +1270,24 @@ try_parent: int rc = -1; strlcpy(import, key_val(rule, &rule->import), sizeof(import)); - udev_rules_apply_format(udev, import, sizeof(import)); - dbg("check for IMPORT import='%s'\n", import); + udev_rules_apply_format(udevice, import, sizeof(import)); + dbg(udevice->udev, "check for IMPORT import='%s'\n", import); if (rule->import_type == IMPORT_PROGRAM) { - rc = import_program_into_env(udev, import); + rc = import_program_into_env(udevice, import); } else if (rule->import_type == IMPORT_FILE) { - dbg("import file import='%s'\n", import); - rc = import_file_into_env(udev, import); + dbg(udevice->udev, "import file import='%s'\n", import); + rc = import_file_into_env(udevice, import); } else if (rule->import_type == IMPORT_PARENT) { - dbg("import parent import='%s'\n", import); - rc = import_parent_into_env(udev, import); + dbg(udevice->udev, "import parent import='%s'\n", import); + rc = import_parent_into_env(udevice, import); } if (rc != 0) { - dbg("IMPORT failed\n"); + dbg(udevice->udev, "IMPORT failed\n"); if (rule->import.operation != KEY_OP_NOMATCH) goto nomatch; } else - dbg("IMPORT '%s' imported\n", key_val(rule, &rule->import)); - dbg("IMPORT key is true\n"); + dbg(udevice->udev, "IMPORT '%s' imported\n", key_val(rule, &rule->import)); + dbg(udevice->udev, "IMPORT key is true\n"); } /* rule matches, if we have ENV assignments export it */ @@ -1301,20 +1301,20 @@ try_parent: /* make sure we don't write to the same string we possibly read from */ strlcpy(temp_value, value, sizeof(temp_value)); - udev_rules_apply_format(udev, temp_value, NAME_SIZE); + udev_rules_apply_format(udevice, temp_value, NAME_SIZE); if (temp_value[0] == '\0') { - name_list_key_remove(&udev->env_list, key_name); + name_list_key_remove(udevice->udev, &udevice->env_list, key_name); unsetenv(key_name); - info("unset ENV '%s'\n", key_name); + info(udevice->udev, "unset ENV '%s'\n", key_name); } else { struct name_entry *entry; - entry = name_list_key_add(&udev->env_list, key_name, temp_value); + entry = name_list_key_add(udevice->udev, &udevice->env_list, key_name, temp_value); if (entry == NULL) break; putenv(entry->name); - info("set ENV '%s'\n", entry->name); + info(udevice->udev, "set ENV '%s'\n", entry->name); } } } @@ -1331,9 +1331,9 @@ try_parent: char value[NAME_SIZE]; FILE *f; - if (attr_get_by_subsys_id(key_name, devpath, sizeof(devpath), &attrib)) { + if (attr_get_by_subsys_id(udevice->udev, key_name, devpath, sizeof(devpath), &attrib)) { if (attrib != NULL) { - strlcpy(attr, sysfs_path, sizeof(attr)); + strlcpy(attr, udev_get_sys_path(udevice->udev), sizeof(attr)); strlcat(attr, devpath, sizeof(attr)); strlcat(attr, "/", sizeof(attr)); strlcat(attr, attrib, sizeof(attr)); @@ -1341,8 +1341,8 @@ try_parent: } if (attr[0] == '\0') { - strlcpy(attr, sysfs_path, sizeof(attr)); - strlcat(attr, udev->dev->devpath, sizeof(attr)); + strlcpy(attr, udev_get_sys_path(udevice->udev), sizeof(attr)); + strlcat(attr, udevice->dev->devpath, sizeof(attr)); strlcat(attr, "/", sizeof(attr)); strlcat(attr, key_name, sizeof(attr)); } @@ -1350,16 +1350,16 @@ try_parent: attr_subst_subdir(attr, sizeof(attr)); strlcpy(value, key_val(rule, &pair->key), sizeof(value)); - udev_rules_apply_format(udev, value, sizeof(value)); - info("writing '%s' to sysfs file '%s'\n", value, attr); + udev_rules_apply_format(udevice, value, sizeof(value)); + info(udevice->udev, "writing '%s' to sysfs file '%s'\n", value, attr); f = fopen(attr, "w"); if (f != NULL) { - if (!udev->test_run) + if (!udevice->test_run) if (fprintf(f, "%s", value) <= 0) - err("error writing ATTR{%s}: %s\n", attr, strerror(errno)); + err(udevice->udev, "error writing ATTR{%s}: %s\n", attr, strerror(errno)); fclose(f); } else - err("error opening ATTR{%s} for writing: %s\n", attr, strerror(errno)); + err(udevice->udev, "error opening ATTR{%s} for writing: %s\n", attr, strerror(errno)); } } return 0; @@ -1368,13 +1368,13 @@ nomatch: return -1; } -int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev) +int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice) { struct udev_rule *rule; int name_set = 0; - dbg("udev->dev->devpath='%s'\n", udev->dev->devpath); - dbg("udev->dev->kernel='%s'\n", udev->dev->kernel); + dbg(udevice->udev, "udevice->dev->devpath='%s'\n", udevice->dev->devpath); + dbg(udevice->udev, "udevice->dev->kernel='%s'\n", udevice->dev->kernel); /* look for a matching rule to apply */ udev_rules_iter_init(rules); @@ -1387,64 +1387,64 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev) (rule->name.operation == KEY_OP_ASSIGN || rule->name.operation == KEY_OP_ASSIGN_FINAL || rule->name.operation == KEY_OP_ADD)) { - dbg("node name already set, rule ignored\n"); + dbg(udevice->udev, "node name already set, rule ignored\n"); continue; } - dbg("process rule\n"); - if (match_rule(udev, rule) == 0) { + dbg(udevice->udev, "process rule\n"); + if (match_rule(udevice, rule) == 0) { /* apply options */ if (rule->ignore_device) { - info("rule applied, '%s' is ignored\n", udev->dev->kernel); - udev->ignore_device = 1; + info(udevice->udev, "rule applied, '%s' is ignored\n", udevice->dev->kernel); + udevice->ignore_device = 1; return 0; } if (rule->ignore_remove) { - udev->ignore_remove = 1; - dbg("remove event should be ignored\n"); + udevice->ignore_remove = 1; + dbg(udevice->udev, "remove event should be ignored\n"); } if (rule->link_priority != 0) { - udev->link_priority = rule->link_priority; - info("link_priority=%i\n", udev->link_priority); + udevice->link_priority = rule->link_priority; + info(udevice->udev, "link_priority=%i\n", udevice->link_priority); } if (rule->event_timeout >= 0) { - udev->event_timeout = rule->event_timeout; - info("event_timeout=%i\n", udev->event_timeout); + udevice->event_timeout = rule->event_timeout; + info(udevice->udev, "event_timeout=%i\n", udevice->event_timeout); } /* apply all_partitions option only at a main block device */ if (rule->partitions && - strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') { - udev->partitions = rule->partitions; - dbg("creation of partition nodes requested\n"); + strcmp(udevice->dev->subsystem, "block") == 0 && udevice->dev->kernel_number[0] == '\0') { + udevice->partitions = rule->partitions; + dbg(udevice->udev, "creation of partition nodes requested\n"); } /* apply permissions */ - if (!udev->mode_final && rule->mode.operation != KEY_OP_UNSET) { + if (!udevice->mode_final && rule->mode.operation != KEY_OP_UNSET) { if (rule->mode.operation == KEY_OP_ASSIGN_FINAL) - udev->mode_final = 1; + udevice->mode_final = 1; char buf[20]; strlcpy(buf, key_val(rule, &rule->mode), sizeof(buf)); - udev_rules_apply_format(udev, buf, sizeof(buf)); - udev->mode = strtol(buf, NULL, 8); - dbg("applied mode=%#o to '%s'\n", udev->mode, udev->dev->kernel); + udev_rules_apply_format(udevice, buf, sizeof(buf)); + udevice->mode = strtol(buf, NULL, 8); + dbg(udevice->udev, "applied mode=%#o to '%s'\n", udevice->mode, udevice->dev->kernel); } - if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) { + if (!udevice->owner_final && rule->owner.operation != KEY_OP_UNSET) { if (rule->owner.operation == KEY_OP_ASSIGN_FINAL) - udev->owner_final = 1; - strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner)); - udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner)); - dbg("applied owner='%s' to '%s'\n", udev->owner, udev->dev->kernel); + udevice->owner_final = 1; + strlcpy(udevice->owner, key_val(rule, &rule->owner), sizeof(udevice->owner)); + udev_rules_apply_format(udevice, udevice->owner, sizeof(udevice->owner)); + dbg(udevice->udev, "applied owner='%s' to '%s'\n", udevice->owner, udevice->dev->kernel); } - if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) { + if (!udevice->group_final && rule->group.operation != KEY_OP_UNSET) { if (rule->group.operation == KEY_OP_ASSIGN_FINAL) - udev->group_final = 1; - strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group)); - udev_rules_apply_format(udev, udev->group, sizeof(udev->group)); - dbg("applied group='%s' to '%s'\n", udev->group, udev->dev->kernel); + udevice->group_final = 1; + strlcpy(udevice->group, key_val(rule, &rule->group), sizeof(udevice->group)); + udev_rules_apply_format(udevice, udevice->group, sizeof(udevice->group)); + dbg(udevice->udev, "applied group='%s' to '%s'\n", udevice->group, udevice->dev->kernel); } /* collect symlinks */ - if (!udev->symlink_final && + if (!udevice->symlink_final && (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL || rule->symlink.operation == KEY_OP_ADD)) { @@ -1453,38 +1453,38 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev) int count; if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL) - udev->symlink_final = 1; + udevice->symlink_final = 1; if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) { - info("reset symlink list\n"); - name_list_cleanup(&udev->symlink_list); + info(udevice->udev, "reset symlink list\n"); + name_list_cleanup(udevice->udev, &udevice->symlink_list); } /* allow multiple symlinks separated by spaces */ strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp)); - udev_rules_apply_format(udev, temp, sizeof(temp)); + udev_rules_apply_format(udevice, temp, sizeof(temp)); if (rule->string_escape == ESCAPE_UNSET || rule->string_escape == ESCAPE_REPLACE) { count = replace_chars(temp, ALLOWED_CHARS_FILE " "); if (count > 0) - info("%i character(s) replaced\n" , count); + info(udevice->udev, "%i character(s) replaced\n" , count); } - dbg("rule applied, added symlink(s) '%s'\n", temp); + dbg(udevice->udev, "rule applied, added symlink(s) '%s'\n", temp); pos = temp; while (isspace(pos[0])) pos++; next = strchr(pos, ' '); while (next) { next[0] = '\0'; - info("add symlink '%s'\n", pos); - name_list_add(&udev->symlink_list, pos, 0); + info(udevice->udev, "add symlink '%s'\n", pos); + name_list_add(udevice->udev, &udevice->symlink_list, pos, 0); while (isspace(next[1])) next++; pos = &next[1]; next = strchr(pos, ' '); } if (pos[0] != '\0') { - info("add symlink '%s'\n", pos); - name_list_add(&udev->symlink_list, pos, 0); + info(udevice->udev, "add symlink '%s'\n", pos); + name_list_add(udevice->udev, &udevice->symlink_list, pos, 0); } } @@ -1495,67 +1495,67 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev) int count; name_set = 1; - strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name)); - udev_rules_apply_format(udev, udev->name, sizeof(udev->name)); + strlcpy(udevice->name, key_val(rule, &rule->name), sizeof(udevice->name)); + udev_rules_apply_format(udevice, udevice->name, sizeof(udevice->name)); if (rule->string_escape == ESCAPE_UNSET || rule->string_escape == ESCAPE_REPLACE) { - count = replace_chars(udev->name, ALLOWED_CHARS_FILE); + count = replace_chars(udevice->name, ALLOWED_CHARS_FILE); if (count > 0) - info("%i character(s) replaced\n", count); + info(udevice->udev, "%i character(s) replaced\n", count); } - info("rule applied, '%s' becomes '%s'\n", udev->dev->kernel, udev->name); - if (strcmp(udev->dev->subsystem, "net") != 0) - dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i\n", - udev->name, udev->owner, udev->group, udev->mode, udev->partitions); + info(udevice->udev, "rule applied, '%s' becomes '%s'\n", udevice->dev->kernel, udevice->name); + if (strcmp(udevice->dev->subsystem, "net") != 0) + dbg(udevice->udev, "name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i\n", + udevice->name, udevice->owner, udevice->group, udevice->mode, udevice->partitions); } - if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) { + if (!udevice->run_final && rule->run.operation != KEY_OP_UNSET) { struct name_entry *entry; if (rule->run.operation == KEY_OP_ASSIGN_FINAL) - udev->run_final = 1; + udevice->run_final = 1; if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) { - info("reset run list\n"); - name_list_cleanup(&udev->run_list); + info(udevice->udev, "reset run list\n"); + name_list_cleanup(udevice->udev, &udevice->run_list); } - dbg("add run '%s'\n", key_val(rule, &rule->run)); - entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0); + dbg(udevice->udev, "add run '%s'\n", key_val(rule, &rule->run)); + entry = name_list_add(udevice->udev, &udevice->run_list, key_val(rule, &rule->run), 0); if (rule->run_ignore_error) entry->ignore_error = 1; } if (rule->last_rule) { - dbg("last rule to be applied\n"); + dbg(udevice->udev, "last rule to be applied\n"); break; } if (rule->goto_label.operation != KEY_OP_UNSET) { - dbg("moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); + dbg(udevice->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); udev_rules_iter_label(rules, key_val(rule, &rule->goto_label)); } } } if (!name_set) { - info("no node name set, will use kernel name '%s'\n", udev->dev->kernel); - strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name)); + info(udevice->udev, "no node name set, will use kernel name '%s'\n", udevice->dev->kernel); + strlcpy(udevice->name, udevice->dev->kernel, sizeof(udevice->name)); } - if (udev->tmp_node[0] != '\0') { - dbg("removing temporary device node\n"); - unlink_secure(udev->tmp_node); - udev->tmp_node[0] = '\0'; + if (udevice->tmp_node[0] != '\0') { + dbg(udevice->udev, "removing temporary device node\n"); + unlink_secure(udevice->udev, udevice->tmp_node); + udevice->tmp_node[0] = '\0'; } return 0; } -int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev) +int udev_rules_get_run(struct udev_rules *rules, struct udevice *udevice) { struct udev_rule *rule; - dbg("udev->kernel='%s'\n", udev->dev->kernel); + dbg(udevice->udev, "udevice->kernel='%s'\n", udevice->dev->kernel); /* look for a matching rule to apply */ udev_rules_iter_init(rules); @@ -1564,7 +1564,7 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev) if (rule == NULL) break; - dbg("process rule\n"); + dbg(udevice->udev, "process rule\n"); if (rule->name.operation == KEY_OP_ASSIGN || rule->name.operation == KEY_OP_ASSIGN_FINAL || rule->name.operation == KEY_OP_ADD || @@ -1573,31 +1573,31 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev) rule->symlink.operation == KEY_OP_ADD || rule->mode.operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) { - dbg("skip rule that names a device\n"); + dbg(udevice->udev, "skip rule that names a device\n"); continue; } - if (match_rule(udev, rule) == 0) { + if (match_rule(udevice, rule) == 0) { if (rule->ignore_device) { - info("rule applied, '%s' is ignored\n", udev->dev->kernel); - udev->ignore_device = 1; + info(udevice->udev, "rule applied, '%s' is ignored\n", udevice->dev->kernel); + udevice->ignore_device = 1; return 0; } if (rule->ignore_remove) { - udev->ignore_remove = 1; - dbg("remove event should be ignored\n"); + udevice->ignore_remove = 1; + dbg(udevice->udev, "remove event should be ignored\n"); } - if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) { + if (!udevice->run_final && rule->run.operation != KEY_OP_UNSET) { struct name_entry *entry; if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) { - info("reset run list\n"); - name_list_cleanup(&udev->run_list); + info(udevice->udev, "reset run list\n"); + name_list_cleanup(udevice->udev, &udevice->run_list); } - dbg("add run '%s'\n", key_val(rule, &rule->run)); - entry = name_list_add(&udev->run_list, key_val(rule, &rule->run), 0); + dbg(udevice->udev, "add run '%s'\n", key_val(rule, &rule->run)); + entry = name_list_add(udevice->udev, &udevice->run_list, key_val(rule, &rule->run), 0); if (rule->run_ignore_error) entry->ignore_error = 1; if (rule->run.operation == KEY_OP_ASSIGN_FINAL) @@ -1605,12 +1605,12 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev) } if (rule->last_rule) { - dbg("last rule to be applied\n"); + dbg(udevice->udev, "last rule to be applied\n"); break; } if (rule->goto_label.operation != KEY_OP_UNSET) { - dbg("moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); + dbg(udevice->udev, "moving forward to label '%s'\n", key_val(rule, &rule->goto_label)); udev_rules_iter_label(rules, key_val(rule, &rule->goto_label)); } } diff --git a/udev/udev_rules.h b/udev/udev_rules.h index 9a41ccbd..4f95121f 100644 --- a/udev/udev_rules.h +++ b/udev/udev_rules.h @@ -108,13 +108,14 @@ struct udev_rule { }; struct udev_rules { + struct udev *udev; char *buf; size_t bufsize; size_t current; int resolve_names; }; -extern int udev_rules_init(struct udev_rules *rules, int resolve_names); +extern int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_names); extern void udev_rules_cleanup(struct udev_rules *rules); extern void udev_rules_iter_init(struct udev_rules *rules); diff --git a/udev/udev_rules_parse.c b/udev/udev_rules_parse.c index 90b139be..df7b57ba 100644 --- a/udev/udev_rules_parse.c +++ b/udev/udev_rules_parse.c @@ -35,7 +35,7 @@ void udev_rules_iter_init(struct udev_rules *rules) { - dbg("bufsize=%zi\n", rules->bufsize); + dbg(rules->udev, "bufsize=%zi\n", rules->bufsize); rules->current = 0; } @@ -46,9 +46,9 @@ struct udev_rule *udev_rules_iter_next(struct udev_rules *rules) if (!rules) return NULL; - dbg("current=%zi\n", rules->current); + dbg(rules->udev, "current=%zi\n", rules->current); if (rules->current >= rules->bufsize) { - dbg("no more rules\n"); + dbg(rules->udev, "no more rules\n"); return NULL; } @@ -65,25 +65,25 @@ struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *la size_t start = rules->current; next: - dbg("current=%zi\n", rules->current); + dbg(rules->udev, "current=%zi\n", rules->current); if (rules->current >= rules->bufsize) { - err("LABEL='%s' not found, GOTO will be ignored\n", label); + err(rules->udev, "LABEL='%s' not found, GOTO will be ignored\n", label); rules->current = start; return NULL; } rule = (struct udev_rule *) (rules->buf + rules->current); if (strcmp(&rule->buf[rule->label.val_off], label) != 0) { - dbg("moving forward, looking for label '%s'\n", label); + dbg(rules->udev, "moving forward, looking for label '%s'\n", label); rules->current += sizeof(struct udev_rule) + rule->bufsize; goto next; } - dbg("found label '%s'\n", label); + dbg(rules->udev, "found label '%s'\n", label); return rule; } -static int get_key(char **line, char **key, enum key_operation *operation, char **value) +static int get_key(struct udev_rules *rules, char **line, char **key, enum key_operation *operation, char **value) { char *linepos; char *temp; @@ -127,29 +127,29 @@ static int get_key(char **line, char **key, enum key_operation *operation, char if (linepos[0] == '=' && linepos[1] == '=') { *operation = KEY_OP_MATCH; linepos += 2; - dbg("operator=match\n"); + dbg(rules->udev, "operator=match\n"); } else if (linepos[0] == '!' && linepos[1] == '=') { *operation = KEY_OP_NOMATCH; linepos += 2; - dbg("operator=nomatch\n"); + dbg(rules->udev, "operator=nomatch\n"); } else if (linepos[0] == '+' && linepos[1] == '=') { *operation = KEY_OP_ADD; linepos += 2; - dbg("operator=add\n"); + dbg(rules->udev, "operator=add\n"); } else if (linepos[0] == '=') { *operation = KEY_OP_ASSIGN; linepos++; - dbg("operator=assign\n"); + dbg(rules->udev, "operator=assign\n"); } else if (linepos[0] == ':' && linepos[1] == '=') { *operation = KEY_OP_ASSIGN_FINAL; linepos += 2; - dbg("operator=assign_final\n"); + dbg(rules->udev, "operator=assign_final\n"); } else return -1; /* terminate key */ temp[0] = '\0'; - dbg("key='%s'\n", *key); + dbg(rules->udev, "key='%s'\n", *key); /* skip whitespace after operator */ while (isspace(linepos[0])) @@ -169,7 +169,7 @@ static int get_key(char **line, char **key, enum key_operation *operation, char return -1; temp[0] = '\0'; temp++; - dbg("value='%s'\n", *value); + dbg(rules->udev, "value='%s'\n", *value); /* move line to next key */ *line = temp; @@ -178,7 +178,7 @@ static int get_key(char **line, char **key, enum key_operation *operation, char } /* extract possible KEY{attr} */ -static char *get_key_attribute(char *str) +static char *get_key_attribute(struct udev_rules *rules, char *str) { char *pos; char *attr; @@ -188,11 +188,11 @@ static char *get_key_attribute(char *str) attr++; pos = strchr(attr, '}'); if (pos == NULL) { - err("missing closing brace for format\n"); + err(rules->udev, "missing closing brace for format\n"); return NULL; } pos[0] = '\0'; - dbg("attribute='%s'\n", attr); + dbg(rules->udev, "attribute='%s'\n", attr); return attr; } @@ -213,13 +213,13 @@ static int add_rule_key(struct udev_rule *rule, struct key *key, return 0; } -static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs, +static int add_rule_key_pair(struct udev_rules *rules, struct udev_rule *rule, struct key_pairs *pairs, enum key_operation operation, const char *key, const char *value) { size_t key_len = strnlen(key, PATH_SIZE); if (pairs->count >= PAIRS_MAX) { - err("skip, too many keys of the same type in a single rule\n"); + err(rules->udev, "skip, too many keys of the same type in a single rule\n"); return -1; } @@ -259,14 +259,14 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena char *value; enum key_operation operation = KEY_OP_UNSET; - retval = get_key(&linepos, &key, &operation, &value); + retval = get_key(rules, &linepos, &key, &operation, &value); if (retval) break; if (strcasecmp(key, "ACTION") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid ACTION operation\n"); + err(rules->udev, "invalid ACTION operation\n"); goto invalid; } add_rule_key(rule, &rule->action, operation, value); @@ -277,7 +277,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena if (strcasecmp(key, "DEVPATH") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid DEVPATH operation\n"); + err(rules->udev, "invalid DEVPATH operation\n"); goto invalid; } add_rule_key(rule, &rule->devpath, operation, value); @@ -288,7 +288,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena if (strcasecmp(key, "KERNEL") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid KERNEL operation\n"); + err(rules->udev, "invalid KERNEL operation\n"); goto invalid; } add_rule_key(rule, &rule->kernel, operation, value); @@ -299,7 +299,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena if (strcasecmp(key, "SUBSYSTEM") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid SUBSYSTEM operation\n"); + err(rules->udev, "invalid SUBSYSTEM operation\n"); goto invalid; } /* bus, class, subsystem events should all be the same */ @@ -307,7 +307,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena strcmp(value, "bus") == 0 || strcmp(value, "class") == 0) { if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0) - err("'%s' must be specified as 'subsystem' \n" + err(rules->udev, "'%s' must be specified as 'subsystem' \n" "please fix it in %s:%u", value, filename, lineno); add_rule_key(rule, &rule->subsystem, operation, "subsystem|class|bus"); } else @@ -319,7 +319,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena if (strcasecmp(key, "DRIVER") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid DRIVER operation\n"); + err(rules->udev, "invalid DRIVER operation\n"); goto invalid; } add_rule_key(rule, &rule->driver, operation, value); @@ -328,12 +328,12 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena } if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) { - attr = get_key_attribute(key + sizeof("ATTR")-1); + attr = get_key_attribute(rules, key + sizeof("ATTR")-1); if (attr == NULL) { - err("error parsing ATTR attribute\n"); + err(rules->udev, "error parsing ATTR attribute\n"); goto invalid; } - if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0) + if (add_rule_key_pair(rules, rule, &rule->attr, operation, attr, value) != 0) goto invalid; valid = 1; continue; @@ -343,7 +343,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena strcasecmp(key, "ID") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid KERNELS operation\n"); + err(rules->udev, "invalid KERNELS operation\n"); goto invalid; } add_rule_key(rule, &rule->kernels, operation, value); @@ -355,7 +355,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena strcasecmp(key, "BUS") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid SUBSYSTEMS operation\n"); + err(rules->udev, "invalid SUBSYSTEMS operation\n"); goto invalid; } add_rule_key(rule, &rule->subsystems, operation, value); @@ -366,7 +366,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena if (strcasecmp(key, "DRIVERS") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid DRIVERS operation\n"); + err(rules->udev, "invalid DRIVERS operation\n"); goto invalid; } add_rule_key(rule, &rule->drivers, operation, value); @@ -378,35 +378,35 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid ATTRS operation\n"); + err(rules->udev, "invalid ATTRS operation\n"); goto invalid; } - attr = get_key_attribute(key + sizeof("ATTRS")-1); + attr = get_key_attribute(rules, key + sizeof("ATTRS")-1); if (attr == NULL) { - err("error parsing ATTRS attribute\n"); + err(rules->udev, "error parsing ATTRS attribute\n"); goto invalid; } if (strncmp(attr, "device/", 7) == 0) - err("the 'device' link is deprecated and will be removed from a future kernel, \n" + err(rules->udev, "the 'device' link is deprecated and will be removed from a future kernel, \n" "please fix it in %s:%u", filename, lineno); else if (strstr(attr, "../") != NULL) - err("do not reference parent sysfs directories directly, that may break with a future kernel, \n" + err(rules->udev, "do not reference parent sysfs directories directly, that may break with a future kernel, \n" "please fix it in %s:%u", filename, lineno); - if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0) + if (add_rule_key_pair(rules, rule, &rule->attrs, operation, attr, value) != 0) goto invalid; valid = 1; continue; } if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) { - attr = get_key_attribute(key + sizeof("ENV")-1); + attr = get_key_attribute(rules, key + sizeof("ENV")-1); if (attr == NULL) { - err("error parsing ENV attribute\n"); + err(rules->udev, "error parsing ENV attribute\n"); goto invalid; } if (strncmp(attr, "PHYSDEV", 7) == 0) physdev = 1; - if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0) + if (add_rule_key_pair(rules, rule, &rule->env, operation, attr, value) != 0) goto invalid; valid = 1; continue; @@ -421,7 +421,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena if (strcasecmp(key, "RESULT") == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid RESULT operation\n"); + err(rules->udev, "invalid RESULT operation\n"); goto invalid; } add_rule_key(rule, &rule->result, operation, value); @@ -430,15 +430,15 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena } if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) { - attr = get_key_attribute(key + sizeof("IMPORT")-1); + attr = get_key_attribute(rules, key + sizeof("IMPORT")-1); if (attr != NULL && strstr(attr, "program")) { - dbg("IMPORT will be executed\n"); + dbg(rules->udev, "IMPORT will be executed\n"); rule->import_type = IMPORT_PROGRAM; } else if (attr != NULL && strstr(attr, "file")) { - dbg("IMPORT will be included as file\n"); + dbg(rules->udev, "IMPORT will be included as file\n"); rule->import_type = IMPORT_FILE; } else if (attr != NULL && strstr(attr, "parent")) { - dbg("IMPORT will include the parent values\n"); + dbg(rules->udev, "IMPORT will include the parent values\n"); rule->import_type = IMPORT_PARENT; } else { /* figure it out if it is executable */ @@ -460,12 +460,12 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena pos[0] = '\0'; } - dbg("IMPORT auto mode for '%s'\n", file); + dbg(rules->udev, "IMPORT auto mode for '%s'\n", file); if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) { - dbg("IMPORT is executable, will be executed (autotype)\n"); + dbg(rules->udev, "IMPORT is executable, will be executed (autotype)\n"); rule->import_type = IMPORT_PROGRAM; } else { - dbg("IMPORT is not executable, will be included as file (autotype)\n"); + dbg(rules->udev, "IMPORT is not executable, will be included as file (autotype)\n"); rule->import_type = IMPORT_FILE; } } @@ -477,10 +477,10 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) { if (operation != KEY_OP_MATCH && operation != KEY_OP_NOMATCH) { - err("invalid TEST operation\n"); + err(rules->udev, "invalid TEST operation\n"); goto invalid; } - attr = get_key_attribute(key + sizeof("TEST")-1); + attr = get_key_attribute(rules, key + sizeof("TEST")-1); if (attr != NULL) rule->test_mode_mask = strtol(attr, NULL, 8); add_rule_key(rule, &rule->test, operation, value); @@ -489,7 +489,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena } if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) { - attr = get_key_attribute(key + sizeof("RUN")-1); + attr = get_key_attribute(rules, key + sizeof("RUN")-1); if (attr != NULL) { if (strstr(attr, "ignore_error")) rule->run_ignore_error = 1; @@ -518,19 +518,19 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena } if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) { - attr = get_key_attribute(key + sizeof("NAME")-1); + attr = get_key_attribute(rules, key + sizeof("NAME")-1); if (attr != NULL) { if (strstr(attr, "all_partitions") != NULL) { - dbg("creation of partition nodes requested\n"); + dbg(rules->udev, "creation of partition nodes requested\n"); rule->partitions = DEFAULT_PARTITIONS_COUNT; } if (strstr(attr, "ignore_remove") != NULL) { - dbg("remove event should be ignored\n"); + dbg(rules->udev, "remove event should be ignored\n"); rule->ignore_remove = 1; } } if (value[0] == '\0') - dbg("name empty, node creation supressed\n"); + dbg(rules->udev, "name empty, node creation supressed\n"); add_rule_key(rule, &rule->name, operation, value); continue; } @@ -552,8 +552,8 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena strtoul(value, &endptr, 10); if (endptr[0] != '\0') { char owner[32]; - uid_t uid = lookup_user(value); - dbg("replacing username='%s' by id=%i\n", value, uid); + uid_t uid = lookup_user(rules->udev, value); + dbg(rules->udev, "replacing username='%s' by id=%i\n", value, uid); sprintf(owner, "%u", (unsigned int) uid); add_rule_key(rule, &rule->owner, operation, owner); continue; @@ -571,8 +571,8 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena strtoul(value, &endptr, 10); if (endptr[0] != '\0') { char group[32]; - gid_t gid = lookup_group(value); - dbg("replacing groupname='%s' by id=%i\n", value, gid); + gid_t gid = lookup_group(rules->udev, value); + dbg(rules->udev, "replacing groupname='%s' by id=%i\n", value, gid); sprintf(group, "%u", (unsigned int) gid); add_rule_key(rule, &rule->group, operation, group); continue; @@ -593,26 +593,26 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena const char *pos; if (strstr(value, "last_rule") != NULL) { - dbg("last rule to be applied\n"); + dbg(rules->udev, "last rule to be applied\n"); rule->last_rule = 1; } if (strstr(value, "ignore_device") != NULL) { - dbg("device should be ignored\n"); + dbg(rules->udev, "device should be ignored\n"); rule->ignore_device = 1; } if (strstr(value, "ignore_remove") != NULL) { - dbg("remove event should be ignored\n"); + dbg(rules->udev, "remove event should be ignored\n"); rule->ignore_remove = 1; } pos = strstr(value, "link_priority="); if (pos != NULL) { rule->link_priority = atoi(&pos[strlen("link_priority=")]); - dbg("link priority=%i\n", rule->link_priority); + dbg(rules->udev, "link priority=%i\n", rule->link_priority); } pos = strstr(value, "event_timeout="); if (pos != NULL) { rule->event_timeout = atoi(&pos[strlen("event_timeout=")]); - dbg("event timout=%i\n", rule->event_timeout); + dbg(rules->udev, "event timout=%i\n", rule->event_timeout); } pos = strstr(value, "string_escape="); if (pos != NULL) { @@ -623,18 +623,18 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena rule->string_escape = ESCAPE_REPLACE; } if (strstr(value, "all_partitions") != NULL) { - dbg("creation of partition nodes requested\n"); + dbg(rules->udev, "creation of partition nodes requested\n"); rule->partitions = DEFAULT_PARTITIONS_COUNT; } valid = 1; continue; } - err("unknown key '%s' in %s:%u\n", key, filename, lineno); + err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno); } if (physdev && rule->wait_for.operation == KEY_OP_UNSET) - err("PHYSDEV* values are deprecated and will be removed from a future kernel, \n" + err(rules->udev, "PHYSDEV* values are deprecated and will be removed from a future kernel, \n" "please fix it in %s:%u", filename, lineno); /* skip line if not any valid key was found */ @@ -644,23 +644,23 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena /* grow buffer and add rule */ rule_size = sizeof(struct udev_rule) + rule->bufsize; padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t); - dbg("add %zi padding bytes\n", padding); + dbg(rules->udev, "add %zi padding bytes\n", padding); rule_size += padding; rule->bufsize += padding; rules->buf = realloc(rules->buf, rules->bufsize + rule_size); if (!rules->buf) { - err("realloc failed\n"); + err(rules->udev, "realloc failed\n"); goto exit; } - dbg("adding rule to offset %zi\n", rules->bufsize); + dbg(rules->udev, "adding rule to offset %zi\n", rules->bufsize); memcpy(rules->buf + rules->bufsize, rule, rule_size); rules->bufsize += rule_size; exit: return 0; invalid: - err("invalid rule '%s:%u'\n", filename, lineno); + err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno); return -1; } @@ -676,10 +676,10 @@ static int parse_file(struct udev_rules *rules, const char *filename) int retval = 0; if (file_map(filename, &buf, &bufsize) != 0) { - err("can't open '%s' as rules file: %s\n", filename, strerror(errno)); + err(rules->udev, "can't open '%s' as rules file: %s\n", filename, strerror(errno)); return -1; } - info("reading '%s' as rules file\n", filename); + info(rules->udev, "reading '%s' as rules file\n", filename); /* loop through the whole file */ cur = 0; @@ -705,7 +705,7 @@ static int parse_file(struct udev_rules *rules, const char *filename) continue; if (count >= sizeof(line)) { - err("line too long, rule skipped '%s:%u'\n", filename, lineno); + err(rules->udev, "line too long, rule skipped '%s:%u'\n", filename, lineno); continue; } @@ -718,7 +718,7 @@ static int parse_file(struct udev_rules *rules, const char *filename) } line[j] = '\0'; - dbg("read '%s'\n", line); + dbg(rules->udev, "read '%s'\n", line); add_to_rules(rules, line, filename, lineno); } @@ -726,7 +726,7 @@ static int parse_file(struct udev_rules *rules, const char *filename) return retval; } -int udev_rules_init(struct udev_rules *rules, int resolve_names) +int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_names) { struct stat statbuf; char filename[PATH_MAX]; @@ -737,28 +737,29 @@ int udev_rules_init(struct udev_rules *rules, int resolve_names) int retval = 0; memset(rules, 0x00, sizeof(struct udev_rules)); + rules->udev = udev; rules->resolve_names = resolve_names; - if (udev_rules_dir[0] != '\0') { + if (udev_get_rules_path(udev) != NULL) { /* custom rules location for testing */ - add_matching_files(&name_list, udev_rules_dir, ".rules"); + add_matching_files(udev, &name_list, udev_get_rules_path(udev), ".rules"); } else { /* read user/custom rules */ - add_matching_files(&name_list, SYSCONFDIR "/udev/rules.d", ".rules"); + add_matching_files(udev, &name_list, SYSCONFDIR "/udev/rules.d", ".rules"); /* read dynamic/temporary rules */ - strlcpy(filename, udev_root, sizeof(filename)); + strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); strlcat(filename, "/.udev/rules.d", sizeof(filename)); if (stat(filename, &statbuf) != 0) { - create_path(filename); - selinux_setfscreatecon(filename, NULL, S_IFDIR|0755); + create_path(udev, filename); + selinux_setfscreatecon(udev, filename, NULL, S_IFDIR|0755); mkdir(filename, 0755); - selinux_resetfscreatecon(); + selinux_resetfscreatecon(udev); } - add_matching_files(&sort_list, filename, ".rules"); + add_matching_files(udev, &sort_list, filename, ".rules"); /* read default rules */ - add_matching_files(&sort_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules"); + add_matching_files(udev, &sort_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules"); /* sort all rules files by basename into list of files */ list_for_each_entry_safe(sort_loop, sort_tmp, &sort_list, node) { @@ -774,7 +775,7 @@ int udev_rules_init(struct udev_rules *rules, int resolve_names) continue; if (strcmp(name_base, sort_base) == 0) { - info("rule file '%s' already added, ignoring '%s'\n", + info(udev, "rule file '%s' already added, ignoring '%s'\n", name_loop->name, sort_loop->name); list_del(&sort_loop->node); free(sort_loop); @@ -796,9 +797,9 @@ int udev_rules_init(struct udev_rules *rules, int resolve_names) if (statbuf.st_size) parse_file(rules, name_loop->name); else - dbg("empty rules file '%s'\n", name_loop->name); + dbg(udev, "empty rules file '%s'\n", name_loop->name); } else - err("could not read '%s': %s\n", name_loop->name, strerror(errno)); + err(udev, "could not read '%s': %s\n", name_loop->name, strerror(errno)); list_del(&name_loop->node); free(name_loop); } diff --git a/udev/udev_selinux.c b/udev/udev_selinux.c index 66742e1f..2e76a743 100644 --- a/udev/udev_selinux.c +++ b/udev/udev_selinux.c @@ -35,18 +35,18 @@ static security_context_t prev_scontext = NULL; -static int is_selinux_running(void) +static int is_selinux_running(struct udev *udev) { static int selinux_enabled = -1; if (selinux_enabled == -1) selinux_enabled = (is_selinux_enabled() > 0); - dbg("selinux=%i\n", selinux_enabled); + dbg(udev, "selinux=%i\n", selinux_enabled); return selinux_enabled; } -static char *get_media(const char *devname, int mode) +static char *get_media(struct udev *udev, const char *devname, int mode) { FILE *fp; char procfile[PATH_MAX]; @@ -77,7 +77,7 @@ static char *get_media(const char *devname, int mode) } media = strdup(mediabuf); - info("selinux_get_media(%s)='%s'\n", devname, media); + info(udev, "selinux_get_media(%s)='%s'\n", devname, media); close_out: fclose(fp); @@ -85,15 +85,15 @@ out: return media; } -void selinux_setfilecon(const char *file, const char *devname, unsigned int mode) +void selinux_setfilecon(struct udev *udev, const char *file, const char *devname, unsigned int mode) { - if (is_selinux_running()) { + if (is_selinux_running(udev)) { security_context_t scontext = NULL; char *media; int ret = -1; if (devname) { - media = get_media(devname, mode); + media = get_media(udev, devname, mode); if (media) { ret = matchmediacon(media, &scontext); free(media); @@ -102,26 +102,26 @@ void selinux_setfilecon(const char *file, const char *devname, unsigned int mode if (ret < 0) if (matchpathcon(file, mode, &scontext) < 0) { - err("matchpathcon(%s) failed\n", file); + err(udev, "matchpathcon(%s) failed\n", file); return; } if (lsetfilecon(file, scontext) < 0) - err("setfilecon %s failed: %s\n", file, strerror(errno)); + err(udev, "setfilecon %s failed: %s\n", file, strerror(errno)); freecon(scontext); } } -void selinux_setfscreatecon(const char *file, const char *devname, unsigned int mode) +void selinux_setfscreatecon(struct udev *udev, const char *file, const char *devname, unsigned int mode) { - if (is_selinux_running()) { + if (is_selinux_running(udev)) { security_context_t scontext = NULL; char *media; int ret = -1; if (devname) { - media = get_media(devname, mode); + media = get_media(udev, devname, mode); if (media) { ret = matchmediacon(media, &scontext); free(media); @@ -130,45 +130,45 @@ void selinux_setfscreatecon(const char *file, const char *devname, unsigned int if (ret < 0) if (matchpathcon(file, mode, &scontext) < 0) { - err("matchpathcon(%s) failed\n", file); + err(udev, "matchpathcon(%s) failed\n", file); return; } if (setfscreatecon(scontext) < 0) - err("setfscreatecon %s failed: %s\n", file, strerror(errno)); + err(udev, "setfscreatecon %s failed: %s\n", file, strerror(errno)); freecon(scontext); } } -void selinux_resetfscreatecon(void) +void selinux_resetfscreatecon(struct udev *udev) { - if (is_selinux_running()) { + if (is_selinux_running(udev)) { if (setfscreatecon(prev_scontext) < 0) - err("setfscreatecon failed: %s\n", strerror(errno)); + err(udev, "setfscreatecon failed: %s\n", strerror(errno)); } } -void selinux_init(void) +void selinux_init(struct udev *udev) { /* * record the present security context, for file-creation * restoration creation purposes. */ - if (is_selinux_running()) { - if (!udev_root[0]) - err("selinux_init: udev_root not set\n"); - matchpathcon_init_prefix(NULL, udev_root); + if (is_selinux_running(udev)) { + if (!udev_get_dev_path(udev)[0]) + err(udev, "selinux_init: udev_root not set\n"); + matchpathcon_init_prefix(NULL, udev_get_dev_path(udev)); if (getfscreatecon(&prev_scontext) < 0) { - err("getfscreatecon failed\n"); + err(udev, "getfscreatecon failed\n"); prev_scontext = NULL; } } } -void selinux_exit(void) +void selinux_exit(struct udev *udev) { - if (is_selinux_running() && prev_scontext) { + if (is_selinux_running(udev) && prev_scontext) { freecon(prev_scontext); prev_scontext = NULL; } diff --git a/udev/udev_selinux.h b/udev/udev_selinux.h index fcdd9dbe..55bc9122 100644 --- a/udev/udev_selinux.h +++ b/udev/udev_selinux.h @@ -19,17 +19,17 @@ #define _UDEV_SELINUX_H #ifdef USE_SELINUX -extern void selinux_setfilecon(const char *file, const char *devname, unsigned int mode); -extern void selinux_setfscreatecon(const char *file, const char *devname, unsigned int mode); -extern void selinux_resetfscreatecon(void); -extern void selinux_init(void); -extern void selinux_exit(void); +extern void selinux_setfilecon(struct udev *udev, const char *file, const char *devname, unsigned int mode); +extern void selinux_setfscreatecon(struct udev *udev, const char *file, const char *devname, unsigned int mode); +extern void selinux_resetfscreatecon(struct udev *udev); +extern void selinux_init(struct udev *udev); +extern void selinux_exit(struct udev *udev); #else -static inline void selinux_setfilecon(const char *file, const char *devname, unsigned int mode) {} -static inline void selinux_setfscreatecon(const char *file, const char *devname, unsigned int mode) {} -static inline void selinux_resetfscreatecon(void) {} -static inline void selinux_init(void) {} -static inline void selinux_exit(void) {} +static inline void selinux_setfilecon(struct udev *udev, const char *file, const char *devname, unsigned int mode) {} +static inline void selinux_setfscreatecon(struct udev *udev, const char *file, const char *devname, unsigned int mode) {} +static inline void selinux_resetfscreatecon(struct udev *udev) {} +static inline void selinux_init(struct udev *udev) {} +static inline void selinux_exit(struct udev *udev) {} #endif #endif diff --git a/udev/udev_sysfs.c b/udev/udev_sysfs.c index 91f5997d..f0dfd7f4 100644 --- a/udev/udev_sysfs.c +++ b/udev/udev_sysfs.c @@ -29,13 +29,12 @@ #include "udev.h" -char sysfs_path[PATH_SIZE]; - /* device cache */ static LIST_HEAD(dev_list); /* attribute value cache */ static LIST_HEAD(attr_list); + struct sysfs_attr { struct list_head node; char path[PATH_SIZE]; @@ -45,16 +44,6 @@ struct sysfs_attr { int sysfs_init(void) { - const char *env; - - env = getenv("SYSFS_PATH"); - if (env) { - strlcpy(sysfs_path, env, sizeof(sysfs_path)); - remove_trailing_chars(sysfs_path, '/'); - } else - strlcpy(sysfs_path, "/sys", sizeof(sysfs_path)); - dbg("sysfs_path='%s'\n", sysfs_path); - INIT_LIST_HEAD(&dev_list); INIT_LIST_HEAD(&attr_list); return 0; @@ -78,7 +67,8 @@ void sysfs_cleanup(void) } } -void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath, +void sysfs_device_set_values(struct udev *udev, + struct sysfs_device *dev, const char *devpath, const char *subsystem, const char *driver) { char *pos; @@ -94,7 +84,7 @@ void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath, if (pos == NULL) return; strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel)); - dbg("kernel='%s'\n", dev->kernel); + dbg(udev, "kernel='%s'\n", dev->kernel); /* some devices have '!' in their name, change that to '/' */ pos = dev->kernel; @@ -109,10 +99,10 @@ void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath, while (isdigit(pos[-1])) pos--; strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number)); - dbg("kernel_number='%s'\n", dev->kernel_number); + dbg(udev, "kernel_number='%s'\n", dev->kernel_number); } -int sysfs_resolve_link(char *devpath, size_t size) +int sysfs_resolve_link(struct udev *udev, char *devpath, size_t size) { char link_path[PATH_SIZE]; char link_target[PATH_SIZE]; @@ -120,17 +110,17 @@ int sysfs_resolve_link(char *devpath, size_t size) int i; int back; - strlcpy(link_path, sysfs_path, sizeof(link_path)); + strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path)); strlcat(link_path, devpath, sizeof(link_path)); len = readlink(link_path, link_target, sizeof(link_target)); if (len <= 0) return -1; link_target[len] = '\0'; - dbg("path link '%s' points to '%s'\n", devpath, link_target); + dbg(udev, "path link '%s' points to '%s'\n", devpath, link_target); for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++) ; - dbg("base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back); + dbg(udev, "base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back); for (i = 0; i <= back; i++) { char *pos = strrchr(devpath, '/'); @@ -138,13 +128,13 @@ int sysfs_resolve_link(char *devpath, size_t size) return -1; pos[0] = '\0'; } - dbg("after moving back '%s'\n", devpath); + dbg(udev, "after moving back '%s'\n", devpath); strlcat(devpath, "/", size); strlcat(devpath, &link_target[back * 3], size); return 0; } -struct sysfs_device *sysfs_device_get(const char *devpath) +struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath) { char path[PATH_SIZE]; char devpath_real[PATH_SIZE]; @@ -166,7 +156,7 @@ struct sysfs_device *sysfs_device_get(const char *devpath) strncmp(devpath, "/block/", 7) != 0) return NULL; - dbg("open '%s'\n", devpath); + dbg(udev, "open '%s'\n", devpath); strlcpy(devpath_real, devpath, sizeof(devpath_real)); remove_trailing_chars(devpath_real, '/'); if (devpath[0] == '\0' ) @@ -175,49 +165,49 @@ struct sysfs_device *sysfs_device_get(const char *devpath) /* look for device already in cache (we never put an untranslated path in the cache) */ list_for_each_entry(dev_loop, &dev_list, node) { if (strcmp(dev_loop->devpath, devpath_real) == 0) { - dbg("found in cache '%s'\n", dev_loop->devpath); + dbg(udev, "found in cache '%s'\n", dev_loop->devpath); return dev_loop; } } /* if we got a link, resolve it to the real device */ - strlcpy(path, sysfs_path, sizeof(path)); + strlcpy(path, udev_get_sys_path(udev), sizeof(path)); strlcat(path, devpath_real, sizeof(path)); if (lstat(path, &statbuf) != 0) { - dbg("stat '%s' failed: %s\n", path, strerror(errno)); + dbg(udev, "stat '%s' failed: %s\n", path, strerror(errno)); return NULL; } if (S_ISLNK(statbuf.st_mode)) { - if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0) + if (sysfs_resolve_link(udev, devpath_real, sizeof(devpath_real)) != 0) return NULL; /* now look for device in cache after path translation */ list_for_each_entry(dev_loop, &dev_list, node) { if (strcmp(dev_loop->devpath, devpath_real) == 0) { - dbg("found in cache '%s'\n", dev_loop->devpath); + dbg(udev, "found in cache '%s'\n", dev_loop->devpath); return dev_loop; } } } /* it is a new device */ - dbg("new uncached device '%s'\n", devpath_real); + dbg(udev, "new uncached device '%s'\n", devpath_real); dev = malloc(sizeof(struct sysfs_device)); if (dev == NULL) return NULL; memset(dev, 0x00, sizeof(struct sysfs_device)); - sysfs_device_set_values(dev, devpath_real, NULL, NULL); + sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL); /* get subsystem name */ - strlcpy(link_path, sysfs_path, sizeof(link_path)); + strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path)); strlcat(link_path, dev->devpath, sizeof(link_path)); strlcat(link_path, "/subsystem", sizeof(link_path)); len = readlink(link_path, link_target, sizeof(link_target)); if (len > 0) { /* get subsystem from "subsystem" link */ link_target[len] = '\0'; - dbg("subsystem link '%s' points to '%s'\n", link_path, link_target); + dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target); pos = strrchr(link_target, '/'); if (pos != NULL) strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem)); @@ -240,37 +230,37 @@ struct sysfs_device *sysfs_device_get(const char *devpath) } /* get driver name */ - strlcpy(link_path, sysfs_path, sizeof(link_path)); + strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path)); strlcat(link_path, dev->devpath, sizeof(link_path)); strlcat(link_path, "/driver", sizeof(link_path)); len = readlink(link_path, link_target, sizeof(link_target)); if (len > 0) { link_target[len] = '\0'; - dbg("driver link '%s' points to '%s'\n", link_path, link_target); + dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target); pos = strrchr(link_target, '/'); if (pos != NULL) strlcpy(dev->driver, &pos[1], sizeof(dev->driver)); } - dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver); + dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver); list_add(&dev->node, &dev_list); return dev; } -struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev) +struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev) { char parent_devpath[PATH_SIZE]; char *pos; - dbg("open '%s'\n", dev->devpath); + dbg(udev, "open '%s'\n", dev->devpath); /* look if we already know the parent */ if (dev->parent != NULL) return dev->parent; strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath)); - dbg("'%s'\n", parent_devpath); + dbg(udev, "'%s'\n", parent_devpath); /* strip last element */ pos = strrchr(parent_devpath, '/'); @@ -281,12 +271,12 @@ struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev) if (strncmp(parent_devpath, "/class", 6) == 0) { pos = strrchr(parent_devpath, '/'); if (pos == &parent_devpath[6] || pos == parent_devpath) { - dbg("/class top level, look for device link\n"); + dbg(udev, "/class top level, look for device link\n"); goto device_link; } } if (strcmp(parent_devpath, "/block") == 0) { - dbg("/block top level, look for device link\n"); + dbg(udev, "/block top level, look for device link\n"); goto device_link; } @@ -296,34 +286,34 @@ struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev) return NULL; /* get parent and remember it */ - dev->parent = sysfs_device_get(parent_devpath); + dev->parent = sysfs_device_get(udev, parent_devpath); return dev->parent; device_link: strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath)); strlcat(parent_devpath, "/device", sizeof(parent_devpath)); - if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0) + if (sysfs_resolve_link(udev, parent_devpath, sizeof(parent_devpath)) != 0) return NULL; /* get parent and remember it */ - dev->parent = sysfs_device_get(parent_devpath); + dev->parent = sysfs_device_get(udev, parent_devpath); return dev->parent; } -struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem) +struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem) { struct sysfs_device *dev_parent; - dev_parent = sysfs_device_get_parent(dev); + dev_parent = sysfs_device_get_parent(udev, dev); while (dev_parent != NULL) { if (strcmp(dev_parent->subsystem, subsystem) == 0) return dev_parent; - dev_parent = sysfs_device_get_parent(dev_parent); + dev_parent = sysfs_device_get_parent(udev, dev_parent); } return NULL; } -char *sysfs_attr_get_value(const char *devpath, const char *attr_name) +char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name) { char path_full[PATH_SIZE]; const char *path; @@ -335,8 +325,8 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name) ssize_t size; size_t sysfs_len; - dbg("open '%s'/'%s'\n", devpath, attr_name); - sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full)); + dbg(udev, "open '%s'/'%s'\n", devpath, attr_name); + sysfs_len = strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full)); if(sysfs_len >= sizeof(path_full)) sysfs_len = sizeof(path_full) - 1; path = &path_full[sysfs_len]; @@ -347,23 +337,23 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name) /* look for attribute in cache */ list_for_each_entry(attr_loop, &attr_list, node) { if (strcmp(attr_loop->path, path) == 0) { - dbg("found in cache '%s'\n", attr_loop->path); + dbg(udev, "found in cache '%s'\n", attr_loop->path); return attr_loop->value; } } /* store attribute in cache (also negatives are kept in cache) */ - dbg("new uncached attribute '%s'\n", path_full); + dbg(udev, "new uncached attribute '%s'\n", path_full); attr = malloc(sizeof(struct sysfs_attr)); if (attr == NULL) return NULL; memset(attr, 0x00, sizeof(struct sysfs_attr)); strlcpy(attr->path, path, sizeof(attr->path)); - dbg("add to cache '%s'\n", path_full); + dbg(udev, "add to cache '%s'\n", path_full); list_add(&attr->node, &attr_list); if (lstat(path_full, &statbuf) != 0) { - dbg("stat '%s' failed: %s\n", path_full, strerror(errno)); + dbg(udev, "stat '%s' failed: %s\n", path_full, strerror(errno)); goto out; } @@ -378,7 +368,7 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name) link_target[len] = '\0'; pos = strrchr(link_target, '/'); if (pos != NULL) { - dbg("cache '%s' with link value '%s'\n", path_full, value); + dbg(udev, "cache '%s' with link value '%s'\n", path_full, value); strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local)); attr->value = attr->value_local; } @@ -397,7 +387,7 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name) /* read attribute value */ fd = open(path_full, O_RDONLY); if (fd < 0) { - dbg("attribute '%s' can not be opened\n", path_full); + dbg(udev, "attribute '%s' can not be opened\n", path_full); goto out; } size = read(fd, value, sizeof(value)); @@ -410,7 +400,7 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name) /* got a valid value, store and return it */ value[size] = '\0'; remove_trailing_chars(value, '\n'); - dbg("cache '%s' with attribute value '%s'\n", path_full, value); + dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value); strlcpy(attr->value_local, value, sizeof(attr->value_local)); attr->value = attr->value_local; @@ -418,14 +408,14 @@ out: return attr->value; } -int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id) +int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id) { size_t sysfs_len; char path_full[PATH_SIZE]; char *path; struct stat statbuf; - sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full)); + sysfs_len = strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full)); path = &path_full[sysfs_len]; if (strcmp(subsystem, "subsystem") == 0) { @@ -504,7 +494,7 @@ out: return 0; found: if (S_ISLNK(statbuf.st_mode)) - sysfs_resolve_link(path, sizeof(path_full) - sysfs_len); + sysfs_resolve_link(udev, path, sizeof(path_full) - sysfs_len); strlcpy(devpath_full, path, len); return 1; } diff --git a/udev/udev_utils.c b/udev/udev_utils.c index f63a9fa1..1649b8cb 100644 --- a/udev/udev_utils.c +++ b/udev/udev_utils.c @@ -55,7 +55,7 @@ int log_priority(const char *priority) return 0; } -struct name_entry *name_list_add(struct list_head *name_list, const char *name, int sort) +struct name_entry *name_list_add(struct udev *udev, struct list_head *name_list, const char *name, int sort) { struct name_entry *name_loop; struct name_entry *name_new; @@ -63,7 +63,7 @@ struct name_entry *name_list_add(struct list_head *name_list, const char *name, /* avoid duplicate entries */ list_for_each_entry(name_loop, name_list, node) { if (strcmp(name_loop->name, name) == 0) { - dbg("'%s' is already in the list\n", name); + dbg(udev, "'%s' is already in the list\n", name); return name_loop; } } @@ -79,20 +79,20 @@ struct name_entry *name_list_add(struct list_head *name_list, const char *name, return NULL; memset(name_new, 0x00, sizeof(struct name_entry)); strlcpy(name_new->name, name, sizeof(name_new->name)); - dbg("adding '%s'\n", name_new->name); + dbg(udev, "adding '%s'\n", name_new->name); list_add_tail(&name_new->node, &name_loop->node); return name_new; } -struct name_entry *name_list_key_add(struct list_head *name_list, const char *key, const char *value) +struct name_entry *name_list_key_add(struct udev *udev, struct list_head *name_list, const char *key, const char *value) { struct name_entry *name_loop; struct name_entry *name_new; list_for_each_entry(name_loop, name_list, node) { if (strncmp(name_loop->name, key, strlen(key)) == 0) { - dbg("key already present '%s', replace it\n", name_loop->name); + dbg(udev, "key already present '%s', replace it\n", name_loop->name); snprintf(name_loop->name, sizeof(name_loop->name), "%s=%s", key, value); name_loop->name[sizeof(name_loop->name)-1] = '\0'; return name_loop; @@ -105,13 +105,13 @@ struct name_entry *name_list_key_add(struct list_head *name_list, const char *ke memset(name_new, 0x00, sizeof(struct name_entry)); snprintf(name_new->name, sizeof(name_new->name), "%s=%s", key, value); name_new->name[sizeof(name_new->name)-1] = '\0'; - dbg("adding '%s'\n", name_new->name); + dbg(udev, "adding '%s'\n", name_new->name); list_add_tail(&name_new->node, &name_loop->node); return name_new; } -int name_list_key_remove(struct list_head *name_list, const char *key) +int name_list_key_remove(struct udev *udev, struct list_head *name_list, const char *key) { struct name_entry *name_loop; struct name_entry *name_tmp; @@ -131,7 +131,7 @@ int name_list_key_remove(struct list_head *name_list, const char *key) return retval; } -void name_list_cleanup(struct list_head *name_list) +void name_list_cleanup(struct udev *udev, struct list_head *name_list) { struct name_entry *name_loop; struct name_entry *name_tmp; @@ -143,16 +143,16 @@ void name_list_cleanup(struct list_head *name_list) } /* calls function for every file found in specified directory */ -int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix) +int add_matching_files(struct udev *udev, struct list_head *name_list, const char *dirname, const char *suffix) { struct dirent *ent; DIR *dir; char filename[PATH_SIZE]; - dbg("open directory '%s'\n", dirname); + dbg(udev, "open directory '%s'\n", dirname); dir = opendir(dirname); if (dir == NULL) { - err("unable to open '%s': %s\n", dirname, strerror(errno)); + err(udev, "unable to open '%s': %s\n", dirname, strerror(errno)); return -1; } @@ -174,18 +174,18 @@ int add_matching_files(struct list_head *name_list, const char *dirname, const c if (strcmp(ext, suffix) != 0) continue; } - dbg("put file '%s/%s' into list\n", dirname, ent->d_name); + dbg(udev, "put file '%s/%s' into list\n", dirname, ent->d_name); snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name); filename[sizeof(filename)-1] = '\0'; - name_list_add(name_list, filename, 1); + name_list_add(udev, name_list, filename, 1); } closedir(dir); return 0; } -uid_t lookup_user(const char *user) +uid_t lookup_user(struct udev *udev, const char *user) { struct passwd *pw; uid_t uid = 0; @@ -194,16 +194,16 @@ uid_t lookup_user(const char *user) pw = getpwnam(user); if (pw == NULL) { if (errno == 0 || errno == ENOENT || errno == ESRCH) - err("specified user '%s' unknown\n", user); + err(udev, "specified user '%s' unknown\n", user); else - err("error resolving user '%s': %s\n", user, strerror(errno)); + err(udev, "error resolving user '%s': %s\n", user, strerror(errno)); } else uid = pw->pw_uid; return uid; } -extern gid_t lookup_group(const char *group) +extern gid_t lookup_group(struct udev *udev, const char *group) { struct group *gr; gid_t gid = 0; @@ -212,9 +212,9 @@ extern gid_t lookup_group(const char *group) gr = getgrnam(group); if (gr == NULL) { if (errno == 0 || errno == ENOENT || errno == ESRCH) - err("specified group '%s' unknown\n", group); + err(udev, "specified group '%s' unknown\n", group); else - err("error resolving group '%s': %s\n", group, strerror(errno)); + err(udev, "error resolving group '%s': %s\n", group, strerror(errno)); } else gid = gr->gr_gid; diff --git a/udev/udev_utils_file.c b/udev/udev_utils_file.c index 3e73c876..7ccb5b40 100644 --- a/udev/udev_utils_file.c +++ b/udev/udev_utils_file.c @@ -32,7 +32,7 @@ #include "udev.h" #include "udev_selinux.h" -int create_path(const char *path) +int create_path(struct udev *udev, const char *path) { char p[PATH_SIZE]; char *pos; @@ -48,17 +48,17 @@ int create_path(const char *path) pos--; pos[0] = '\0'; - dbg("stat '%s'\n", p); + dbg(udev, "stat '%s'\n", p); if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) return 0; - if (create_path(p) != 0) + if (create_path(udev, p) != 0) return -1; - dbg("mkdir '%s'\n", p); - selinux_setfscreatecon(p, NULL, S_IFDIR|0755); + dbg(udev, "mkdir '%s'\n", p); + selinux_setfscreatecon(udev, p, NULL, S_IFDIR|0755); ret = mkdir(p, 0755); - selinux_resetfscreatecon(); + selinux_resetfscreatecon(udev); if (ret == 0) return 0; @@ -68,7 +68,7 @@ int create_path(const char *path) return -1; } -int delete_path(const char *path) +int delete_path(struct udev *udev, const char *path) { char p[PATH_SIZE]; char *pos; @@ -94,10 +94,10 @@ int delete_path(const char *path) if (retval) { if (errno == ENOTEMPTY) return 0; - err("rmdir(%s) failed: %s\n", p, strerror(errno)); + err(udev, "rmdir(%s) failed: %s\n", p, strerror(errno)); break; } - dbg("removed '%s'\n", p); + dbg(udev, "removed '%s'\n", p); } return 0; } @@ -105,24 +105,24 @@ int delete_path(const char *path) /* Reset permissions on the device node, before unlinking it to make sure, * that permisions of possible hard links will be removed too. */ -int unlink_secure(const char *filename) +int unlink_secure(struct udev *udev, const char *filename) { int retval; retval = chown(filename, 0, 0); if (retval) - err("chown(%s, 0, 0) failed: %s\n", filename, strerror(errno)); + err(udev, "chown(%s, 0, 0) failed: %s\n", filename, strerror(errno)); retval = chmod(filename, 0000); if (retval) - err("chmod(%s, 0000) failed: %s\n", filename, strerror(errno)); + err(udev, "chmod(%s, 0000) failed: %s\n", filename, strerror(errno)); retval = unlink(filename); if (errno == ENOENT) retval = 0; if (retval) - err("unlink(%s) failed: %s\n", filename, strerror(errno)); + err(udev, "unlink(%s) failed: %s\n", filename, strerror(errno)); return retval; } diff --git a/udev/udev_utils_string.c b/udev/udev_utils_string.c index 78bfca80..3bfe22c8 100644 --- a/udev/udev_utils_string.c +++ b/udev/udev_utils_string.c @@ -46,6 +46,8 @@ void remove_trailing_chars(char *path, char c) { size_t len; + if (path == NULL) + return; len = strlen(path); while (len > 0 && path[len-1] == c) path[--len] = '\0'; diff --git a/udev/udevadm-control.c b/udev/udevadm-control.c index de4b658d..6e10316f 100644 --- a/udev/udevadm-control.c +++ b/udev/udevadm-control.c @@ -34,12 +34,9 @@ #include "udev.h" #include "udevd.h" -static int udev_log = 0; - -int udevadm_control(int argc, char *argv[]) +int udevadm_control(struct udev *udev, int argc, char *argv[]) { struct udev_ctrl *uctrl; - const char *env; int rc = 1; /* compat values with '_' will be removed in a future release */ @@ -61,19 +58,12 @@ int udevadm_control(int argc, char *argv[]) {} }; - env = getenv("UDEV_LOG"); - if (env) - udev_log = log_priority(env); - - logging_init("udevcontrol"); - dbg("version %s\n", VERSION); - if (getuid() != 0) { fprintf(stderr, "root privileges required\n"); goto exit; } - uctrl = udev_ctrl_new_from_socket(UDEVD_CTRL_SOCK_PATH); + uctrl = udev_ctrl_new_from_socket(udev, UDEVD_CTRL_SOCK_PATH); if (uctrl == NULL) goto exit; @@ -87,7 +77,7 @@ int udevadm_control(int argc, char *argv[]) break; if (option > 255) { - info("udevadm control expects commands without underscore, " + info(udev, "udevadm control expects commands without underscore, " "this will stop working in a future release\n"); fprintf(stderr, "udevadm control expects commands without underscore, " "this will stop working in a future release\n"); @@ -140,7 +130,6 @@ int udevadm_control(int argc, char *argv[]) } udev_ctrl_set_max_childs_running(uctrl, i); break; - break; case 'h': printf("Usage: udevadm control COMMAND\n" " --log-priority= set the udev log level for the daemon\n" @@ -163,7 +152,7 @@ int udevadm_control(int argc, char *argv[]) fprintf(stderr, "udevadm control commands requires the -- format, " "this will stop working in a future release\n"); - err("udevadm control commands requires the -- format, " + err(udev, "udevadm control commands requires the -- format, " "this will stop working in a future release\n"); if (!strncmp(arg, "log_priority=", strlen("log_priority="))) { @@ -182,11 +171,10 @@ int udevadm_control(int argc, char *argv[]) udev_ctrl_set_env(uctrl, &arg[strlen("env=")]); } else { fprintf(stderr, "unrecognized command '%s'\n", arg); - err("unrecognized command '%s'\n", arg); + err(udev, "unrecognized command '%s'\n", arg); } } exit: udev_ctrl_unref(uctrl); - logging_close(); return rc; } diff --git a/udev/udevadm-info.c b/udev/udevadm-info.c index fdf53d2a..b54b61e0 100644 --- a/udev/udevadm-info.c +++ b/udev/udevadm-info.c @@ -33,13 +33,13 @@ #include "udev.h" -static void print_all_attributes(const char *devpath, const char *key) +static void print_all_attributes(struct udev *udev, const char *devpath, const char *key) { char path[PATH_SIZE]; DIR *dir; struct dirent *dent; - strlcpy(path, sysfs_path, sizeof(path)); + strlcpy(path, udev_get_sys_path(udev), sizeof(path)); strlcat(path, devpath, sizeof(path)); dir = opendir(path); @@ -67,13 +67,13 @@ static void print_all_attributes(const char *devpath, const char *key) if (S_ISLNK(statbuf.st_mode)) continue; - attr_value = sysfs_attr_get_value(devpath, dent->d_name); + attr_value = sysfs_attr_get_value(udev, devpath, dent->d_name); if (attr_value == NULL) continue; len = strlcpy(value, attr_value, sizeof(value)); if(len >= sizeof(value)) len = sizeof(value) - 1; - dbg("attr '%s'='%s'(%zi)\n", dent->d_name, value, len); + dbg(udev, "attr '%s'='%s'(%zi)\n", dent->d_name, value, len); /* remove trailing newlines */ while (len && value[len-1] == '\n') @@ -83,7 +83,7 @@ static void print_all_attributes(const char *devpath, const char *key) while (len && isprint(value[len-1])) len--; if (len) { - dbg("attribute value of '%s' non-printable, skip\n", dent->d_name); + dbg(udev, "attribute value of '%s' non-printable, skip\n", dent->d_name); continue; } @@ -93,11 +93,11 @@ static void print_all_attributes(const char *devpath, const char *key) printf("\n"); } -static int print_device_chain(const char *devpath) +static int print_device_chain(struct udev *udev, const char *devpath) { struct sysfs_device *dev; - dev = sysfs_device_get(devpath); + dev = sysfs_device_get(udev, devpath); if (dev == NULL) return -1; @@ -113,11 +113,11 @@ static int print_device_chain(const char *devpath) printf(" KERNEL==\"%s\"\n", dev->kernel); printf(" SUBSYSTEM==\"%s\"\n", dev->subsystem); printf(" DRIVER==\"%s\"\n", dev->driver); - print_all_attributes(dev->devpath, "ATTR"); + print_all_attributes(udev, dev->devpath, "ATTR"); /* walk up the chain of devices */ while (1) { - dev = sysfs_device_get_parent(dev); + dev = sysfs_device_get_parent(udev, dev); if (dev == NULL) break; printf(" looking at parent device '%s':\n", dev->devpath); @@ -125,93 +125,94 @@ static int print_device_chain(const char *devpath) printf(" SUBSYSTEMS==\"%s\"\n", dev->subsystem); printf(" DRIVERS==\"%s\"\n", dev->driver); - print_all_attributes(dev->devpath, "ATTRS"); + print_all_attributes(udev, dev->devpath, "ATTRS"); } return 0; } -static void print_record(struct udevice *udev) +static void print_record(struct udevice *udevice) { struct name_entry *name_loop; - printf("P: %s\n", udev->dev->devpath); - printf("N: %s\n", udev->name); - list_for_each_entry(name_loop, &udev->symlink_list, node) + printf("P: %s\n", udevice->dev->devpath); + printf("N: %s\n", udevice->name); + list_for_each_entry(name_loop, &udevice->symlink_list, node) printf("S: %s\n", name_loop->name); - if (udev->link_priority != 0) - printf("L: %i\n", udev->link_priority); - if (udev->partitions != 0) - printf("A:%u\n", udev->partitions); - if (udev->ignore_remove) - printf("R:%u\n", udev->ignore_remove); - list_for_each_entry(name_loop, &udev->env_list, node) + if (udevice->link_priority != 0) + printf("L: %i\n", udevice->link_priority); + if (udevice->partitions != 0) + printf("A:%u\n", udevice->partitions); + if (udevice->ignore_remove) + printf("R:%u\n", udevice->ignore_remove); + list_for_each_entry(name_loop, &udevice->env_list, node) printf("E: %s\n", name_loop->name); } -static void export_db(void) { +static void export_db(struct udev *udev) +{ LIST_HEAD(name_list); struct name_entry *name_loop; - udev_db_get_all_entries(&name_list); + udev_db_get_all_entries(udev, &name_list); list_for_each_entry(name_loop, &name_list, node) { - struct udevice *udev_db; + struct udevice *udevice_db; - udev_db = udev_device_init(); - if (udev_db == NULL) + udevice_db = udev_device_init(udev); + if (udevice_db == NULL) continue; - if (udev_db_get_device(udev_db, name_loop->name) == 0) - print_record(udev_db); + if (udev_db_get_device(udevice_db, name_loop->name) == 0) + print_record(udevice_db); printf("\n"); - udev_device_cleanup(udev_db); + udev_device_cleanup(udevice_db); } - name_list_cleanup(&name_list); + name_list_cleanup(udev, &name_list); } -static int lookup_device_by_name(struct udevice **udev, const char *name) +static int lookup_device_by_name(struct udev *udev, struct udevice **udevice, const char *name) { LIST_HEAD(name_list); int count; struct name_entry *device; int rc = -1; - count = udev_db_get_devices_by_name(name, &name_list); + count = udev_db_get_devices_by_name(udev, name, &name_list); if (count <= 0) goto out; - info("found %i devices for '%s'\n", count, name); + info(udev, "found %i devices for '%s'\n", count, name); /* select the device that seems to match */ list_for_each_entry(device, &name_list, node) { - struct udevice *udev_loop; + struct udevice *udevice_loop; char filename[PATH_SIZE]; struct stat statbuf; - udev_loop = udev_device_init(); - if (udev_loop == NULL) + udevice_loop = udev_device_init(udev); + if (udevice_loop == NULL) break; - if (udev_db_get_device(udev_loop, device->name) != 0) + if (udev_db_get_device(udevice_loop, device->name) != 0) goto next; - info("found db entry '%s'\n", device->name); + info(udev, "found db entry '%s'\n", device->name); /* make sure, we don't get a link of a different device */ - strlcpy(filename, udev_root, sizeof(filename)); + strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); strlcat(filename, "/", sizeof(filename)); strlcat(filename, name, sizeof(filename)); if (stat(filename, &statbuf) != 0) goto next; - if (major(udev_loop->devt) > 0 && udev_loop->devt != statbuf.st_rdev) { - info("skip '%s', dev_t doesn't match\n", udev_loop->name); + if (major(udevice_loop->devt) > 0 && udevice_loop->devt != statbuf.st_rdev) { + info(udev, "skip '%s', dev_t doesn't match\n", udevice_loop->name); goto next; } rc = 0; - *udev = udev_loop; + *udevice = udevice_loop; break; next: - udev_device_cleanup(udev_loop); + udev_device_cleanup(udevice_loop); } out: - name_list_cleanup(&name_list); + name_list_cleanup(udev, &name_list); return rc; } @@ -234,10 +235,9 @@ static int stat_device(const char *name, int export, const char *prefix) return 0; } -int udevadm_info(int argc, char *argv[]) +int udevadm_info(struct udev *udev, int argc, char *argv[]) { - int option; - struct udevice *udev = NULL; + struct udevice *udevice = NULL; int root = 0; int export = 0; const char *export_prefix = NULL; @@ -280,31 +280,33 @@ int udevadm_info(int argc, char *argv[]) int rc = 0; while (1) { + int option; + option = getopt_long(argc, argv, "aed:n:p:q:rxPVh", options, NULL); if (option == -1) break; - dbg("option '%c'\n", option); + dbg(udev, "option '%c'\n", option); switch (option) { case 'n': /* remove /dev if given */ - if (strncmp(optarg, udev_root, strlen(udev_root)) == 0) - strlcpy(name, &optarg[strlen(udev_root)+1], sizeof(name)); + if (strncmp(optarg, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) == 0) + strlcpy(name, &optarg[strlen(udev_get_dev_path(udev))+1], sizeof(name)); else strlcpy(name, optarg, sizeof(name)); remove_trailing_chars(name, '/'); - dbg("name: %s\n", name); + dbg(udev, "name: %s\n", name); break; case 'p': /* remove /sys if given */ - if (strncmp(optarg, sysfs_path, strlen(sysfs_path)) == 0) - strlcpy(path, &optarg[strlen(sysfs_path)], sizeof(path)); + if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0) + strlcpy(path, &optarg[strlen(udev_get_sys_path(udev))], sizeof(path)); else strlcpy(path, optarg, sizeof(path)); remove_trailing_chars(path, '/'); /* possibly resolve to real devpath */ - if (sysfs_resolve_link(path, sizeof(path)) != 0) { + if (sysfs_resolve_link(udev, path, sizeof(path)) != 0) { char temp[PATH_SIZE]; char *pos; @@ -316,13 +318,13 @@ int udevadm_info(int argc, char *argv[]) strlcpy(tail, pos, sizeof(tail)); pos[0] = '\0'; - if (sysfs_resolve_link(temp, sizeof(temp)) == 0) { + if (sysfs_resolve_link(udev, temp, sizeof(temp)) == 0) { strlcpy(path, temp, sizeof(path)); strlcat(path, tail, sizeof(path)); } } } - dbg("path: %s\n", path); + dbg(udev, "path: %s\n", path); break; case 'q': action = ACTION_QUERY; @@ -362,7 +364,7 @@ int udevadm_info(int argc, char *argv[]) action = ACTION_ATTRIBUTE_WALK; break; case 'e': - export_db(); + export_db(udev); goto exit; case 'x': export = 1; @@ -404,18 +406,18 @@ int udevadm_info(int argc, char *argv[]) case ACTION_QUERY: /* needs devpath or node/symlink name for query */ if (path[0] != '\0') { - udev = udev_device_init(); - if (udev == NULL) { + udevice = udev_device_init(udev); + if (udevice == NULL) { rc = 1; goto exit; } - if (udev_db_get_device(udev, path) != 0) { + if (udev_db_get_device(udevice, path) != 0) { fprintf(stderr, "no record for '%s' in database\n", path); rc = 3; goto exit; } } else if (name[0] != '\0') { - if (lookup_device_by_name(&udev, name) != 0) { + if (lookup_device_by_name(udev, &udevice, name) != 0) { fprintf(stderr, "node name not found\n"); rc = 4; goto exit; @@ -429,29 +431,29 @@ int udevadm_info(int argc, char *argv[]) switch(query) { case QUERY_NAME: if (root) - printf("%s/%s\n", udev_root, udev->name); + printf("%s/%s\n", udev_get_dev_path(udev), udevice->name); else - printf("%s\n", udev->name); + printf("%s\n", udevice->name); break; case QUERY_SYMLINK: - list_for_each_entry(name_loop, &udev->symlink_list, node) { - char c = name_loop->node.next != &udev->symlink_list ? ' ' : '\n'; + list_for_each_entry(name_loop, &udevice->symlink_list, node) { + char c = name_loop->node.next != &udevice->symlink_list ? ' ' : '\n'; if (root) - printf("%s/%s%c", udev_root, name_loop->name, c); + printf("%s/%s%c", udev_get_dev_path(udev), name_loop->name, c); else printf("%s%c", name_loop->name, c); } break; case QUERY_PATH: - printf("%s\n", udev->dev->devpath); + printf("%s\n", udevice->dev->devpath); goto exit; case QUERY_ENV: - list_for_each_entry(name_loop, &udev->env_list, node) + list_for_each_entry(name_loop, &udevice->env_list, node) printf("%s\n", name_loop->name); break; case QUERY_ALL: - print_record(udev); + print_record(udevice); break; default: fprintf(stderr, "unknown query type\n"); @@ -460,18 +462,18 @@ int udevadm_info(int argc, char *argv[]) break; case ACTION_ATTRIBUTE_WALK: if (path[0] != '\0') { - if (print_device_chain(path) != 0) { + if (print_device_chain(udev, path) != 0) { fprintf(stderr, "no valid sysfs device found\n"); rc = 4; goto exit; } } else if (name[0] != '\0') { - if (lookup_device_by_name(&udev, name) != 0) { + if (lookup_device_by_name(udev, &udevice, name) != 0) { fprintf(stderr, "node name not found\n"); rc = 4; goto exit; } - if (print_device_chain(udev->dev->devpath) != 0) { + if (print_device_chain(udev, udevice->dev->devpath) != 0) { fprintf(stderr, "no valid sysfs device found\n"); rc = 4; goto exit; @@ -487,7 +489,7 @@ int udevadm_info(int argc, char *argv[]) rc = 6; break; case ACTION_ROOT: - printf("%s\n", udev_root); + printf("%s\n", udev_get_dev_path(udev)); break; default: fprintf(stderr, "missing option\n"); @@ -496,6 +498,6 @@ int udevadm_info(int argc, char *argv[]) } exit: - udev_device_cleanup(udev); + udev_device_cleanup(udevice); return rc; } diff --git a/udev/udevadm-monitor.c b/udev/udevadm-monitor.c index a45f165c..0b9b8118 100644 --- a/udev/udevadm-monitor.c +++ b/udev/udevadm-monitor.c @@ -123,13 +123,13 @@ static const char *search_key(const char *searchkey, const char *buf, size_t buf return NULL; } -int udevadm_monitor(int argc, char *argv[]) +int udevadm_monitor(struct udev *udev, int argc, char *argv[]) { struct sigaction act; int option; int env = 0; - int kernel = 0; - int udev = 0; + int print_kernel = 0; + int print_udev = 0; fd_set readfds; int retval = 0; @@ -151,10 +151,10 @@ int udevadm_monitor(int argc, char *argv[]) env = 1; break; case 'k': - kernel = 1; + print_kernel = 1; break; case 'u': - udev = 1; + print_udev = 1; break; case 'h': printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n" @@ -167,12 +167,12 @@ int udevadm_monitor(int argc, char *argv[]) } } - if (!kernel && !udev) { - kernel = 1; - udev =1; + if (!print_kernel && !print_udev) { + print_kernel = 1; + print_udev =1; } - if (getuid() != 0 && kernel) { + if (getuid() != 0 && print_kernel) { fprintf(stderr, "root privileges needed to subscribe to kernel events\n"); goto out; } @@ -185,14 +185,14 @@ int udevadm_monitor(int argc, char *argv[]) sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); - printf("udevmonitor will print the received events for:\n"); - if (udev) { + printf("monitor will print the received events for:\n"); + if (print_udev) { retval = init_udev_monitor_socket(); if (retval) goto out; printf("UDEV the event which udev sends out after rule processing\n"); } - if (kernel) { + if (print_kernel) { retval = init_uevent_netlink_sock(); if (retval) goto out; diff --git a/udev/udevadm-settle.c b/udev/udevadm-settle.c index 3a886469..c519f91c 100644 --- a/udev/udevadm-settle.c +++ b/udev/udevadm-settle.c @@ -35,12 +35,12 @@ #define DEFAULT_TIMEOUT 180 #define LOOP_PER_SECOND 20 -static void print_queue(const char *dir) +static void print_queue(struct udev *udev, const char *dir) { LIST_HEAD(files); struct name_entry *item; - if (add_matching_files(&files, dir, NULL) < 0) + if (add_matching_files(udev, &files, dir, NULL) < 0) return; printf("\n\nAfter the udevadm settle timeout, the events queue contains:\n\n"); @@ -67,7 +67,7 @@ static void print_queue(const char *dir) printf("\n\n"); } -int udevadm_settle(int argc, char *argv[]) +int udevadm_settle(struct udev *udev, int argc, char *argv[]) { char queuename[PATH_SIZE]; char filename[PATH_SIZE]; @@ -87,7 +87,7 @@ int udevadm_settle(int argc, char *argv[]) int rc = 1; int seconds; - dbg("version %s\n", VERSION); + dbg(udev, "version %s\n", VERSION); while (1) { option = getopt_long(argc, argv, "t:h", options, NULL); @@ -101,7 +101,7 @@ int udevadm_settle(int argc, char *argv[]) timeout = seconds; else fprintf(stderr, "invalid timeout value\n"); - dbg("timeout=%i\n", timeout); + dbg(udev, "timeout=%i\n", timeout); break; case 'h': printf("Usage: udevadm settle [--help] [--timeout=]\n\n"); @@ -109,7 +109,7 @@ int udevadm_settle(int argc, char *argv[]) } } - strlcpy(queuename, udev_root, sizeof(queuename)); + strlcpy(queuename, udev_get_dev_path(udev), sizeof(queuename)); strlcat(queuename, "/.udev/queue", sizeof(queuename)); loop = timeout * LOOP_PER_SECOND; @@ -119,19 +119,19 @@ int udevadm_settle(int argc, char *argv[]) struct stat statbuf; if (stat(queuename, &statbuf) < 0) { - info("queue is empty\n"); + info(udev, "queue is empty\n"); break; } usleep(1000 * 1000 / LOOP_PER_SECOND); } if (loop <= 0) { - info("timeout waiting for queue\n"); - print_queue(queuename); + info(udev, "timeout waiting for queue\n"); + print_queue(udev, queuename); goto exit; } /* read current udev seqnum */ - strlcpy(filename, udev_root, sizeof(filename)); + strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename)); fd = open(filename, O_RDONLY); if (fd < 0) @@ -142,10 +142,10 @@ int udevadm_settle(int argc, char *argv[]) goto exit; seqnum[len] = '\0'; seq_udev = strtoull(seqnum, NULL, 10); - info("udev seqnum = %llu\n", seq_udev); + info(udev, "udev seqnum = %llu\n", seq_udev); /* read current kernel seqnum */ - strlcpy(filename, sysfs_path, sizeof(filename)); + strlcpy(filename, udev_get_sys_path(udev), sizeof(filename)); strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename)); fd = open(filename, O_RDONLY); if (fd < 0) @@ -156,16 +156,16 @@ int udevadm_settle(int argc, char *argv[]) goto exit; seqnum[len] = '\0'; seq_kernel = strtoull(seqnum, NULL, 10); - info("kernel seqnum = %llu\n", seq_kernel); + info(udev, "kernel seqnum = %llu\n", seq_kernel); /* make sure all kernel events have arrived in the queue */ if (seq_udev >= seq_kernel) { - info("queue is empty and no pending events left\n"); + info(udev, "queue is empty and no pending events left\n"); rc = 0; goto exit; } usleep(1000 * 1000 / LOOP_PER_SECOND); - info("queue is empty, but events still pending\n"); + info(udev, "queue is empty, but events still pending\n"); } exit: diff --git a/udev/udevadm-test.c b/udev/udevadm-test.c index d06249c6..0f7d5dbe 100644 --- a/udev/udevadm-test.c +++ b/udev/udevadm-test.c @@ -32,7 +32,7 @@ #include "udev.h" #include "udev_rules.h" -static int import_uevent_var(const char *devpath) +static int import_uevent_var(struct udev *udev, const char *devpath) { char path[PATH_SIZE]; static char value[4096]; /* must stay, used with putenv */ @@ -43,7 +43,7 @@ static int import_uevent_var(const char *devpath) int rc = -1; /* read uevent file */ - strlcpy(path, sysfs_path, sizeof(path)); + strlcpy(path, udev_get_sys_path(udev), sizeof(path)); strlcat(path, devpath, sizeof(path)); strlcat(path, "/uevent", sizeof(path)); fd = open(path, O_RDONLY); @@ -62,7 +62,7 @@ static int import_uevent_var(const char *devpath) if (next == NULL) goto out; next[0] = '\0'; - info("import into environment: '%s'\n", key); + info(udev, "import into environment: '%s'\n", key); putenv(key); key = &next[1]; } @@ -71,13 +71,13 @@ out: return rc; } -int udevadm_test(int argc, char *argv[]) +int udevadm_test(struct udev *udev, int argc, char *argv[]) { int force = 0; const char *action = "add"; const char *subsystem = NULL; const char *devpath = NULL; - struct udevice *udev; + struct udevice *udevice; struct sysfs_device *dev; struct udev_rules rules = {}; int retval; @@ -91,12 +91,13 @@ int udevadm_test(int argc, char *argv[]) {} }; - info("version %s\n", VERSION); - if (udev_log_priority < LOG_INFO) { + info(udev, "version %s\n", VERSION); + + /* export log priority to executed programs */ + if (udev_get_log_priority(udev) > 0) { char priority[32]; - udev_log_priority = LOG_INFO; - sprintf(priority, "%i", udev_log_priority); + sprintf(priority, "%i", udev_get_log_priority(udev)); setenv("UDEV_LOG", priority, 1); } @@ -107,7 +108,7 @@ int udevadm_test(int argc, char *argv[]) if (option == -1) break; - dbg("option '%c'\n", option); + dbg(udev, "option '%c'\n", option); switch (option) { case 'a': action = optarg; @@ -142,21 +143,21 @@ int udevadm_test(int argc, char *argv[]) "some values may be different, or not available at a simulation run.\n" "\n"); - udev_rules_init(&rules, 0); + udev_rules_init(udev, &rules, 0); /* remove /sys if given */ - if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0) - devpath = &devpath[strlen(sysfs_path)]; + if (strncmp(devpath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0) + devpath = &devpath[strlen(udev_get_sys_path(udev))]; - dev = sysfs_device_get(devpath); + dev = sysfs_device_get(udev, devpath); if (dev == NULL) { fprintf(stderr, "unable to open device '%s'\n", devpath); rc = 2; goto exit; } - udev = udev_device_init(); - if (udev == NULL) { + udevice = udev_device_init(udev); + if (udevice == NULL) { fprintf(stderr, "error initializing device\n"); rc = 3; goto exit; @@ -166,37 +167,37 @@ int udevadm_test(int argc, char *argv[]) strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem)); /* override built-in sysfs device */ - udev->dev = dev; - strlcpy(udev->action, action, sizeof(udev->action)); - udev->devt = udev_device_get_devt(udev); + udevice->dev = dev; + strlcpy(udevice->action, action, sizeof(udevice->action)); + udevice->devt = udev_device_get_devt(udevice); /* simulate node creation with test flag */ if (!force) - udev->test_run = 1; + udevice->test_run = 1; - setenv("DEVPATH", udev->dev->devpath, 1); - setenv("SUBSYSTEM", udev->dev->subsystem, 1); - setenv("ACTION", udev->action, 1); - import_uevent_var(udev->dev->devpath); + setenv("DEVPATH", udevice->dev->devpath, 1); + setenv("SUBSYSTEM", udevice->dev->subsystem, 1); + setenv("ACTION", udevice->action, 1); + import_uevent_var(udev, udevice->dev->devpath); - info("looking at device '%s' from subsystem '%s'\n", udev->dev->devpath, udev->dev->subsystem); - retval = udev_device_event(&rules, udev); + info(udev, "looking at device '%s' from subsystem '%s'\n", udevice->dev->devpath, udevice->dev->subsystem); + retval = udev_device_event(&rules, udevice); - if (udev->event_timeout >= 0) - info("custom event timeout: %i\n", udev->event_timeout); + if (udevice->event_timeout >= 0) + info(udev, "custom event timeout: %i\n", udevice->event_timeout); - if (retval == 0 && !udev->ignore_device && udev_run) { + if (retval == 0 && !udevice->ignore_device && udev_get_run(udev)) { struct name_entry *name_loop; - list_for_each_entry(name_loop, &udev->run_list, node) { + list_for_each_entry(name_loop, &udevice->run_list, node) { char program[PATH_SIZE]; strlcpy(program, name_loop->name, sizeof(program)); - udev_rules_apply_format(udev, program, sizeof(program)); - info("run: '%s'\n", program); + udev_rules_apply_format(udevice, program, sizeof(program)); + info(udev, "run: '%s'\n", program); } } - udev_device_cleanup(udev); + udev_device_cleanup(udevice); exit: udev_rules_cleanup(&rules); diff --git a/udev/udevadm-trigger.c b/udev/udevadm-trigger.c index 04d8f79b..1c57e0a2 100644 --- a/udev/udevadm-trigger.c +++ b/udev/udevadm-trigger.c @@ -64,13 +64,13 @@ static int delay_device(const char *devpath) return 0; } -static int device_list_insert(const char *path) +static int device_list_insert(struct udev *udev, const char *path) { char filename[PATH_SIZE]; char devpath[PATH_SIZE]; struct stat statbuf; - dbg("add '%s'\n" , path); + dbg(udev, "add '%s'\n" , path); /* we only have a device, if we have an uevent file */ strlcpy(filename, path, sizeof(filename)); @@ -80,25 +80,25 @@ static int device_list_insert(const char *path) if (!(statbuf.st_mode & S_IWUSR)) return -1; - strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath)); + strlcpy(devpath, &path[strlen(udev_get_sys_path(udev))], sizeof(devpath)); /* resolve possible link to real target */ if (lstat(path, &statbuf) < 0) return -1; if (S_ISLNK(statbuf.st_mode)) - if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0) + if (sysfs_resolve_link(udev, devpath, sizeof(devpath)) != 0) return -1; - name_list_add(&device_list, devpath, 1); + name_list_add(udev, &device_list, devpath, 1); return 0; } -static void trigger_uevent(const char *devpath, const char *action) +static void trigger_uevent(struct udev *udev, const char *devpath, const char *action) { char filename[PATH_SIZE]; int fd; - strlcpy(filename, sysfs_path, sizeof(filename)); + strlcpy(filename, udev_get_sys_path(udev), sizeof(filename)); strlcat(filename, devpath, sizeof(filename)); strlcat(filename, "/uevent", sizeof(filename)); @@ -110,19 +110,19 @@ static void trigger_uevent(const char *devpath, const char *action) fd = open(filename, O_WRONLY); if (fd < 0) { - dbg("error on opening %s: %s\n", filename, strerror(errno)); + dbg(udev, "error on opening %s: %s\n", filename, strerror(errno)); return; } if (write(fd, action, strlen(action)) < 0) - info("error writing '%s' to '%s': %s\n", action, filename, strerror(errno)); + info(udev, "error writing '%s' to '%s': %s\n", action, filename, strerror(errno)); close(fd); } -static int pass_to_socket(const char *devpath, const char *action, const char *env) +static int pass_to_socket(struct udev *udev, const char *devpath, const char *action, const char *env) { - struct udevice *udev; + struct udevice *udevice; struct name_entry *name_loop; char buf[4096]; size_t bufpos = 0; @@ -136,10 +136,10 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e if (verbose) printf("%s\n", devpath); - udev = udev_device_init(); - if (udev == NULL) + udevice = udev_device_init(udev); + if (udevice == NULL) return -1; - udev_db_get_device(udev, devpath); + udev_db_get_device(udevice, devpath); /* add header */ bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath); @@ -158,7 +158,7 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e bufpos++; /* add subsystem */ - strlcpy(path, sysfs_path, sizeof(path)); + strlcpy(path, udev_get_sys_path(udev), sizeof(path)); strlcat(path, devpath, sizeof(path)); strlcat(path, "/subsystem", sizeof(path)); len = readlink(path, link_target, sizeof(link_target)); @@ -175,8 +175,8 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e /* add symlinks and node name */ path[0] = '\0'; - list_for_each_entry(name_loop, &udev->symlink_list, node) { - strlcat(path, udev_root, sizeof(path)); + list_for_each_entry(name_loop, &udevice->symlink_list, node) { + strlcat(path, udev_get_dev_path(udev), sizeof(path)); strlcat(path, "/", sizeof(path)); strlcat(path, name_loop->name, sizeof(path)); strlcat(path, " ", sizeof(path)); @@ -186,16 +186,16 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path); bufpos++; } - if (udev->name[0] != '\0') { - strlcpy(path, udev_root, sizeof(path)); + if (udevice->name[0] != '\0') { + strlcpy(path, udev_get_dev_path(udev), sizeof(path)); strlcat(path, "/", sizeof(path)); - strlcat(path, udev->name, sizeof(path)); + strlcat(path, udevice->name, sizeof(path)); bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path); bufpos++; } /* add keys from device "uevent" file */ - strlcpy(path, sysfs_path, sizeof(path)); + strlcpy(path, udev_get_sys_path(udev), sizeof(path)); strlcat(path, devpath, sizeof(path)); strlcat(path, "/uevent", sizeof(path)); fd = open(path, O_RDONLY); @@ -224,7 +224,7 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e } /* add keys from database */ - list_for_each_entry(name_loop, &udev->env_list, node) { + list_for_each_entry(name_loop, &udevice->env_list, node) { bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1); bufpos++; } @@ -238,7 +238,7 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e return err; } -static void exec_list(const char *action, const char *env) +static void exec_list(struct udev *udev, const char *action, const char *env) { struct name_entry *loop_device; struct name_entry *tmp_device; @@ -247,9 +247,9 @@ static void exec_list(const char *action, const char *env) if (delay_device(loop_device->name)) continue; if (sock >= 0) - pass_to_socket(loop_device->name, action, env); + pass_to_socket(udev, loop_device->name, action, env); else - trigger_uevent(loop_device->name, action); + trigger_uevent(udev, loop_device->name, action); list_del(&loop_device->node); free(loop_device); } @@ -257,9 +257,9 @@ static void exec_list(const char *action, const char *env) /* trigger remaining delayed devices */ list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) { if (sock >= 0) - pass_to_socket(loop_device->name, action, env); + pass_to_socket(udev, loop_device->name, action, env); else - trigger_uevent(loop_device->name, action); + trigger_uevent(udev, loop_device->name, action); list_del(&loop_device->node); free(loop_device); } @@ -357,7 +357,7 @@ enum scan_type { SCAN_SUBSYSTEM, }; -static void scan_subsystem(const char *subsys, enum scan_type scan) +static void scan_subsystem(struct udev *udev, const char *subsys, enum scan_type scan) { char base[PATH_SIZE]; DIR *dir; @@ -371,7 +371,7 @@ static void scan_subsystem(const char *subsys, enum scan_type scan) else return; - strlcpy(base, sysfs_path, sizeof(base)); + strlcpy(base, udev_get_sys_path(udev), sizeof(base)); strlcat(base, "/", sizeof(base)); strlcat(base, subsys, sizeof(base)); @@ -397,7 +397,7 @@ static void scan_subsystem(const char *subsys, enum scan_type scan) if (attr_filtered(dirname)) continue; if (!subsystem_filtered("subsystem")) - device_list_insert(dirname); + device_list_insert(udev, dirname); if (subsystem_filtered("drivers")) continue; } @@ -418,7 +418,7 @@ static void scan_subsystem(const char *subsys, enum scan_type scan) strlcat(dirname2, dent2->d_name, sizeof(dirname2)); if (attr_filtered(dirname2)) continue; - device_list_insert(dirname2); + device_list_insert(udev, dirname2); } closedir(dir2); } @@ -427,7 +427,7 @@ static void scan_subsystem(const char *subsys, enum scan_type scan) } } -static void scan_block(void) +static void scan_block(struct udev *udev) { char base[PATH_SIZE]; DIR *dir; @@ -436,7 +436,7 @@ static void scan_block(void) if (subsystem_filtered("block")) return; - strlcpy(base, sysfs_path, sizeof(base)); + strlcpy(base, udev_get_sys_path(udev), sizeof(base)); strlcat(base, "/block", sizeof(base)); dir = opendir(base); @@ -454,7 +454,7 @@ static void scan_block(void) strlcat(dirname, dent->d_name, sizeof(dirname)); if (attr_filtered(dirname)) continue; - if (device_list_insert(dirname) != 0) + if (device_list_insert(udev, dirname) != 0) continue; /* look for partitions */ @@ -474,7 +474,7 @@ static void scan_block(void) strlcat(dirname2, dent2->d_name, sizeof(dirname2)); if (attr_filtered(dirname2)) continue; - device_list_insert(dirname2); + device_list_insert(udev, dirname2); } closedir(dir2); } @@ -483,13 +483,13 @@ static void scan_block(void) } } -static void scan_class(void) +static void scan_class(struct udev *udev) { char base[PATH_SIZE]; DIR *dir; struct dirent *dent; - strlcpy(base, sysfs_path, sizeof(base)); + strlcpy(base, udev_get_sys_path(udev), sizeof(base)); strlcat(base, "/class", sizeof(base)); dir = opendir(base); @@ -524,7 +524,7 @@ static void scan_class(void) strlcat(dirname2, dent2->d_name, sizeof(dirname2)); if (attr_filtered(dirname2)) continue; - device_list_insert(dirname2); + device_list_insert(udev, dirname2); } closedir(dir2); } @@ -533,13 +533,13 @@ static void scan_class(void) } } -static void scan_failed(void) +static void scan_failed(struct udev *udev) { char base[PATH_SIZE]; DIR *dir; struct dirent *dent; - strlcpy(base, udev_root, sizeof(base)); + strlcpy(base, udev_get_dev_path(udev), sizeof(base)); strlcat(base, "/.udev/failed", sizeof(base)); dir = opendir(base); @@ -551,18 +551,18 @@ static void scan_failed(void) if (dent->d_name[0] == '.') continue; - start = strlcpy(device, sysfs_path, sizeof(device)); + start = strlcpy(device, udev_get_sys_path(udev), sizeof(device)); if(start >= sizeof(device)) start = sizeof(device) - 1; strlcat(device, dent->d_name, sizeof(device)); path_decode(&device[start]); - device_list_insert(device); + device_list_insert(udev, device); } closedir(dir); } } -int udevadm_trigger(int argc, char *argv[]) +int udevadm_trigger(struct udev *udev, int argc, char *argv[]) { int failed = 0; const char *sockpath = NULL; @@ -584,7 +584,7 @@ int udevadm_trigger(int argc, char *argv[]) {} }; - dbg("version %s\n", VERSION); + dbg(udev, "version %s\n", VERSION); while (1) { option = getopt_long(argc, argv, "vnFo:hce::s:S:a:A:", options, NULL); @@ -612,16 +612,16 @@ int udevadm_trigger(int argc, char *argv[]) env = optarg; break; case 's': - name_list_add(&filter_subsystem_match_list, optarg, 0); + name_list_add(udev, &filter_subsystem_match_list, optarg, 0); break; case 'S': - name_list_add(&filter_subsystem_nomatch_list, optarg, 0); + name_list_add(udev, &filter_subsystem_nomatch_list, optarg, 0); break; case 'a': - name_list_add(&filter_attr_match_list, optarg, 0); + name_list_add(udev, &filter_attr_match_list, optarg, 0); break; case 'A': - name_list_add(&filter_attr_nomatch_list, optarg, 0); + name_list_add(udev, &filter_attr_nomatch_list, optarg, 0); break; case 'h': printf("Usage: udevadm trigger OPTIONS\n" @@ -670,40 +670,40 @@ int udevadm_trigger(int argc, char *argv[]) } if (failed) { - scan_failed(); - exec_list(action, env); + scan_failed(udev); + exec_list(udev, action, env); } else { char base[PATH_SIZE]; struct stat statbuf; /* 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) { - scan_subsystem("subsystem", SCAN_SUBSYSTEM); - exec_list(action, env); - scan_subsystem("subsystem", SCAN_DEVICES); - exec_list(action, env); + scan_subsystem(udev, "subsystem", SCAN_SUBSYSTEM); + exec_list(udev, action, env); + scan_subsystem(udev, "subsystem", SCAN_DEVICES); + exec_list(udev, action, env); } else { - scan_subsystem("bus", SCAN_SUBSYSTEM); - exec_list(action, env); - scan_subsystem("bus", SCAN_DEVICES); - scan_class(); + scan_subsystem(udev, "bus", SCAN_SUBSYSTEM); + exec_list(udev, action, env); + scan_subsystem(udev, "bus", SCAN_DEVICES); + scan_class(udev); /* scan "block" if it isn't a "class" */ - strlcpy(base, sysfs_path, sizeof(base)); + strlcpy(base, udev_get_sys_path(udev), sizeof(base)); strlcat(base, "/class/block", sizeof(base)); if (stat(base, &statbuf) != 0) - scan_block(); - exec_list(action, env); + scan_block(udev); + exec_list(udev, action, env); } } exit: - name_list_cleanup(&filter_subsystem_match_list); - name_list_cleanup(&filter_subsystem_nomatch_list); - name_list_cleanup(&filter_attr_match_list); - name_list_cleanup(&filter_attr_nomatch_list); + name_list_cleanup(udev, &filter_subsystem_match_list); + name_list_cleanup(udev, &filter_subsystem_nomatch_list); + name_list_cleanup(udev, &filter_attr_match_list); + name_list_cleanup(udev, &filter_attr_nomatch_list); if (sock >= 0) close(sock); diff --git a/udev/udevadm.c b/udev/udevadm.c index 047365d9..e4f24205 100644 --- a/udev/udevadm.c +++ b/udev/udevadm.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007 Kay Sievers + * Copyright (C) 2007-2008 Kay Sievers * * 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 @@ -30,43 +30,38 @@ 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; - - if (priority > udev_log_priority) - return; - - va_start(args, format); - if (debug) - vprintf(format, args); - else + if (debug) { + fprintf(stderr, "%s: ", fn); + vfprintf(stderr, format, args); + } else { vsyslog(priority, format, args); - va_end(args); + } } -#endif struct command { const char *name; - int (*cmd)(int argc, char *argv[]); + int (*cmd)(struct udev *udev, int argc, char *argv[]); const char *help; int debug; }; static const struct command cmds[]; -static int version(int argc, char *argv[]) +static int version(struct udev *udev, int argc, char *argv[]) { printf("%s\n", VERSION); return 0; } -static int help(int argc, char *argv[]) +static int help(struct udev *udev, int argc, char *argv[]) { const struct command *cmd; - printf("Usage: udevadm COMMAND [OPTIONS]\n"); + printf("Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n"); for (cmd = cmds; cmd->name != NULL; cmd++) printf(" %-12s %s\n", cmd->name, cmd->help); printf("\n"); @@ -118,28 +113,40 @@ static const struct command cmds[] = { {} }; +static int run_command(struct udev *udev, const struct command *cmd, int argc, char *argv[]) +{ + if (cmd->debug) { + debug = 1; + if (udev_get_log_priority(udev) < LOG_INFO) + udev_set_log_priority(udev, LOG_INFO); + } + info(udev, "calling: %s\n", cmd->name); + return cmd->cmd(udev, argc, argv); +} + int main(int argc, char *argv[]) { - const char *command = argv[1]; + struct udev *udev; + static const struct option options[] = { + { "debug", 0, NULL, 'd' }, + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + {} + }; + const char *command; int i; const char *pos; - int rc; + int rc = 1; + + udev = udev_new(); + if (udev == NULL) + goto out; logging_init("udevadm"); - udev_config_init(); + udev_set_log_fn(udev, log_fn); sysfs_init(); - /* find command */ - if (command != NULL) - for (i = 0; cmds[i].cmd != NULL; i++) { - if (strcmp(cmds[i].name, command) == 0) { - debug = cmds[i].debug; - rc = cmds[i].cmd(argc-1, &argv[1]); - goto out; - } - } - - /* try to find compat link, will be removed in a future release */ + /* see if we are a compat link, this will be removed in a future release */ command = argv[0]; pos = strrchr(command, '/'); if (pos != NULL) @@ -161,19 +168,53 @@ int main(int argc, char *argv[]) prog[len] = '\0'; fprintf(stderr, "the program '%s' called '%s', it should use 'udevadm %s ', " "this will stop working in a future release\n", prog, argv[0], command); - info("the program '%s' called '%s', it should use 'udevadm %s ', " + info(udev, "the program '%s' called '%s', it should use 'udevadm %s ', " "this will stop working in a future release\n", prog, argv[0], command); } - debug = cmds[i].debug; - rc = cmds[i].cmd(argc, argv); + rc = run_command(udev, &cmds[i], argc, argv); + goto out; + } + } + + while (1) { + int option; + + option = getopt_long(argc, argv, "+dhV", options, NULL); + if (option == -1) + break; + + switch (option) { + case 'd': + debug = 1; + if (udev_get_log_priority(udev) < LOG_INFO) + udev_set_log_priority(udev, LOG_INFO); + break; + case 'h': + rc = help(udev, argc, argv); + goto out; + case 'V': + rc = version(udev, argc, argv); + goto out; + default: goto out; } } + command = argv[optind]; + + if (command != NULL) + for (i = 0; cmds[i].cmd != NULL; i++) { + if (strcmp(cmds[i].name, command) == 0) { + optind++; + rc = run_command(udev, &cmds[i], argc, argv); + goto out; + } + } - fprintf(stderr, "unknown command, try help\n\n"); + fprintf(stderr, "unknown command, try udevadm help\n\n"); rc = 2; out: sysfs_cleanup(); + udev_unref(udev); logging_close(); return rc; } diff --git a/udev/udevadm.xml b/udev/udevadm.xml index de0e026e..2e02c305 100644 --- a/udev/udevadm.xml +++ b/udev/udevadm.xml @@ -33,7 +33,7 @@ udevadm settle options - udevadm control options instruction + udevadm control command udevadm monitor options diff --git a/udev/udevd.c b/udev/udevd.c index 68cf2643..c9ee21a2 100644 --- a/udev/udevd.c +++ b/udev/udevd.c @@ -50,9 +50,40 @@ #include "udevd.h" #include "udev_selinux.h" -static int debug_trace; static int debug; +static void log_fn(struct udev *udev, int priority, + const char *file, int line, const char *fn, + const char *format, va_list args) +{ + if (debug) { + fprintf(stderr, "[%d] %s: ", (int) getpid(), fn); + vfprintf(stderr, format, args); + } else { + vsyslog(priority, format, args); + } +} + +struct udevd_uevent_msg { + struct udev *udev; + struct list_head node; + pid_t pid; + int exitstatus; + time_t queue_time; + char *action; + char *devpath; + char *subsystem; + char *driver; + dev_t devt; + unsigned long long seqnum; + char *devpath_old; + char *physdevpath; + unsigned int timeout; + char *envp[UEVENT_NUM_ENVP+1]; + char envbuf[]; +}; + +static int debug_trace; static struct udev_rules rules; static int udevd_sock = -1; static int uevent_netlink_sock = -1; @@ -67,31 +98,11 @@ static int run_exec_q; static int stop_exec_q; static int max_childs; static int max_childs_running; -static char udev_log[32]; +static char udev_log_env[32]; static LIST_HEAD(exec_list); static LIST_HEAD(running_list); - -#ifdef USE_LOG -void log_message(int priority, const char *format, ...) -{ - va_list args; - - if (priority > udev_log_priority) - return; - - va_start(args, format); - if (debug) { - printf("[%d] ", (int) getpid()); - vprintf(format, args); - } else - vsyslog(priority, format, args); - va_end(args); -} - -#endif - static void asmlinkage udev_event_sig_handler(int signum) { if (signum == SIGALRM) @@ -101,7 +112,7 @@ static void asmlinkage udev_event_sig_handler(int signum) static int udev_event_process(struct udevd_uevent_msg *msg) { struct sigaction act; - struct udevice *udev; + struct udevice *udevice; int i; int retval; @@ -126,25 +137,25 @@ static int udev_event_process(struct udevd_uevent_msg *msg) for (i = 0; msg->envp[i]; i++) putenv(msg->envp[i]); - udev = udev_device_init(); - if (udev == NULL) + udevice = udev_device_init(msg->udev); + if (udevice == NULL) return -1; - strlcpy(udev->action, msg->action, sizeof(udev->action)); - sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem, msg->driver); - udev->devpath_old = msg->devpath_old; - udev->devt = msg->devt; + strlcpy(udevice->action, msg->action, sizeof(udevice->action)); + sysfs_device_set_values(udevice->udev, udevice->dev, msg->devpath, msg->subsystem, msg->driver); + udevice->devpath_old = msg->devpath_old; + udevice->devt = msg->devt; - 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); /* run programs collected by RUN-key*/ - if (retval == 0 && !udev->ignore_device && udev_run) - retval = udev_rules_run(udev); + if (retval == 0 && !udevice->ignore_device && udev_get_run(msg->udev)) + retval = udev_rules_run(udevice); - udev_device_cleanup(udev); + udev_device_cleanup(udevice); return retval; } @@ -161,10 +172,10 @@ static void export_event_state(struct udevd_uevent_msg *msg, enum event_state st size_t start; /* location of queue file */ - snprintf(filename, sizeof(filename), "%s/.udev/queue/%llu", udev_root, msg->seqnum); + snprintf(filename, sizeof(filename), "%s/.udev/queue/%llu", udev_get_dev_path(msg->udev), msg->seqnum); /* location of failed file */ - strlcpy(filename_failed, udev_root, sizeof(filename_failed)); + strlcpy(filename_failed, udev_get_dev_path(msg->udev), sizeof(filename_failed)); strlcat(filename_failed, "/", sizeof(filename_failed)); start = strlcat(filename_failed, ".udev/failed/", sizeof(filename_failed)); strlcat(filename_failed, msg->devpath, sizeof(filename_failed)); @@ -173,42 +184,41 @@ static void export_event_state(struct udevd_uevent_msg *msg, enum event_state st switch (state) { case EVENT_QUEUED: unlink(filename_failed); - delete_path(filename_failed); - - create_path(filename); - selinux_setfscreatecon(filename, NULL, S_IFLNK); + delete_path(msg->udev, filename_failed); + create_path(msg->udev, filename); + selinux_setfscreatecon(msg->udev, filename, NULL, S_IFLNK); symlink(msg->devpath, filename); - selinux_resetfscreatecon(); + selinux_resetfscreatecon(msg->udev); break; case EVENT_FINISHED: if (msg->devpath_old != NULL) { /* "move" event - rename failed file to current name, do not delete failed */ char filename_failed_old[PATH_SIZE]; - strlcpy(filename_failed_old, udev_root, sizeof(filename_failed_old)); + strlcpy(filename_failed_old, udev_get_dev_path(msg->udev), sizeof(filename_failed_old)); strlcat(filename_failed_old, "/", sizeof(filename_failed_old)); start = strlcat(filename_failed_old, ".udev/failed/", sizeof(filename_failed_old)); strlcat(filename_failed_old, msg->devpath_old, sizeof(filename_failed_old)); path_encode(&filename_failed_old[start], sizeof(filename) - start); if (rename(filename_failed_old, filename_failed) == 0) - info("renamed devpath, moved failed state of '%s' to %s'\n", + info(msg->udev, "renamed devpath, moved failed state of '%s' to %s'\n", msg->devpath_old, msg->devpath); } else { unlink(filename_failed); - delete_path(filename_failed); + delete_path(msg->udev, filename_failed); } unlink(filename); - delete_path(filename); + delete_path(msg->udev, filename); break; case EVENT_FAILED: /* move failed event to the failed directory */ - create_path(filename_failed); + create_path(msg->udev, filename_failed); rename(filename, filename_failed); /* clean up possibly empty queue directory */ - delete_path(filename); + delete_path(msg->udev, filename); break; } @@ -244,24 +254,23 @@ static void udev_event_run(struct udevd_uevent_msg *msg) close(signal_pipe[READ_END]); close(signal_pipe[WRITE_END]); logging_close(); - logging_init("udevd-event"); setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY); retval = udev_event_process(msg); - info("seq %llu finished with %i\n", msg->seqnum, retval); + info(msg->udev, "seq %llu finished with %i\n", msg->seqnum, retval); logging_close(); if (retval) exit(1); exit(0); case -1: - err("fork of child failed: %s\n", strerror(errno)); + err(msg->udev, "fork of child failed: %s\n", strerror(errno)); msg_queue_delete(msg); break; default: /* get SIGCHLD in main loop */ - info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n", + info(msg->udev, "seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n", msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time); msg->pid = pid; } @@ -275,9 +284,9 @@ static void msg_queue_insert(struct udevd_uevent_msg *msg) msg->queue_time = time(NULL); export_event_state(msg, EVENT_QUEUED); - info("seq %llu queued, '%s' '%s'\n", msg->seqnum, msg->action, msg->subsystem); + info(msg->udev, "seq %llu queued, '%s' '%s'\n", msg->seqnum, msg->action, msg->subsystem); - strlcpy(filename, udev_root, sizeof(filename)); + strlcpy(filename, udev_get_dev_path(msg->udev), sizeof(filename)); strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename)); fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); if (fd >= 0) { @@ -484,7 +493,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) /* check identical, parent, or child device event */ if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) { - dbg("%llu, device event still pending %llu (%s)\n", + dbg(msg->udev, "%llu, device event still pending %llu (%s)\n", msg->seqnum, loop_msg->seqnum, loop_msg->devpath); return 3; } @@ -492,7 +501,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) /* check for our major:minor number */ if (msg->devt && loop_msg->devt == msg->devt && strcmp(msg->subsystem, loop_msg->subsystem) == 0) { - dbg("%llu, device event still pending %llu (%d:%d)\n", msg->seqnum, + dbg(msg->udev, "%llu, device event still pending %llu (%d:%d)\n", msg->seqnum, loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt)); return 4; } @@ -500,7 +509,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) /* check physical device event (special case of parent) */ if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0) if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) { - dbg("%llu, physical device event still pending %llu (%s)\n", + dbg(msg->udev, "%llu, physical device event still pending %llu (%s)\n", msg->seqnum, loop_msg->seqnum, loop_msg->devpath); return 5; } @@ -509,7 +518,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) /* check run queue for still running events */ list_for_each_entry(loop_msg, &running_list, node) { if (limit && childs_count++ > limit) { - dbg("%llu, maximum number (%i) of childs reached\n", msg->seqnum, childs_count); + dbg(msg->udev, "%llu, maximum number (%i) of childs reached\n", msg->seqnum, childs_count); return 1; } @@ -520,7 +529,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) /* check identical, parent, or child device event */ if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) { - dbg("%llu, device event still running %llu (%s)\n", + dbg(msg->udev, "%llu, device event still running %llu (%s)\n", msg->seqnum, loop_msg->seqnum, loop_msg->devpath); return 3; } @@ -528,7 +537,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) /* check for our major:minor number */ if (msg->devt && loop_msg->devt == msg->devt && strcmp(msg->subsystem, loop_msg->subsystem) == 0) { - dbg("%llu, device event still running %llu (%d:%d)\n", msg->seqnum, + dbg(msg->udev, "%llu, device event still running %llu (%d:%d)\n", msg->seqnum, loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt)); return 4; } @@ -536,7 +545,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) /* check physical device event (special case of parent) */ if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0) if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) { - dbg("%llu, physical device event still running %llu (%s)\n", + dbg(msg->udev, "%llu, physical device event still running %llu (%s)\n", msg->seqnum, loop_msg->seqnum, loop_msg->devpath); return 5; } @@ -545,7 +554,7 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit) } /* serializes events for the identical and parent and child devices */ -static void msg_queue_manager(void) +static void msg_queue_manager(struct udev *udev) { struct udevd_uevent_msg *loop_msg; struct udevd_uevent_msg *tmp_msg; @@ -555,7 +564,7 @@ static void msg_queue_manager(void) return; running = running_processes(); - dbg("%d processes runnning on system\n", running); + dbg(udev, "%d processes runnning on system\n", running); if (running < 0) running = max_childs_running; @@ -563,16 +572,16 @@ static void msg_queue_manager(void) /* check running processes in our session and possibly throttle */ if (running >= max_childs_running) { running = running_processes_in_session(sid, max_childs_running+10); - dbg("at least %d processes running in session\n", running); + dbg(udev, "at least %d processes running in session\n", running); if (running >= max_childs_running) { - dbg("delay seq %llu, too many processes already running\n", loop_msg->seqnum); + dbg(udev, "delay seq %llu, too many processes already running\n", loop_msg->seqnum); return; } } /* serialize and wait for parent or child events */ if (devpath_busy(loop_msg, max_childs) != 0) { - dbg("delay seq %llu (%s)\n", loop_msg->seqnum, loop_msg->devpath); + dbg(udev, "delay seq %llu (%s)\n", loop_msg->seqnum, loop_msg->devpath); continue; } @@ -580,11 +589,11 @@ static void msg_queue_manager(void) list_move_tail(&loop_msg->node, &running_list); udev_event_run(loop_msg); running++; - dbg("moved seq %llu to running list\n", loop_msg->seqnum); + dbg(udev, "moved seq %llu to running list\n", loop_msg->seqnum); } } -static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size) +static struct udevd_uevent_msg *get_msg_from_envbuf(struct udev *udev, const char *buf, int buf_size) { int bufpos; int i; @@ -597,6 +606,7 @@ static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_siz if (msg == NULL) return NULL; memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size); + msg->udev = udev; /* copy environment buffer and reconstruct envp */ memcpy(msg->envbuf, buf, buf_size); @@ -609,7 +619,7 @@ static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_siz keylen = strlen(key); msg->envp[i] = key; bufpos += keylen + 1; - dbg("add '%s' to msg.envp[%i]\n", msg->envp[i], i); + dbg(udev, "add '%s' to msg.envp[%i]\n", msg->envp[i], i); /* remember some keys for further processing */ if (strncmp(key, "ACTION=", 7) == 0) @@ -647,7 +657,7 @@ static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_siz msg->envp[i] = NULL; if (msg->devpath == NULL || msg->action == NULL) { - info("DEVPATH or ACTION missing, ignore message\n"); + info(udev, "DEVPATH or ACTION missing, ignore message\n"); free(msg); return NULL; } @@ -655,7 +665,7 @@ static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_siz } /* receive the udevd message from userspace */ -static void get_ctrl_msg(void) +static void get_ctrl_msg(struct udev *udev) { struct udevd_ctrl_msg ctrl_msg; ssize_t size; @@ -679,24 +689,24 @@ static void get_ctrl_msg(void) size = recvmsg(udevd_sock, &smsg, 0); if (size < 0) { if (errno != EINTR) - err("unable to receive user udevd message: %s\n", strerror(errno)); + err(udev, "unable to receive user udevd message: %s\n", strerror(errno)); return; } cmsg = CMSG_FIRSTHDR(&smsg); cred = (struct ucred *) CMSG_DATA(cmsg); if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { - err("no sender credentials received, message ignored\n"); + err(udev, "no sender credentials received, message ignored\n"); return; } if (cred->uid != 0) { - err("sender uid=%i, message ignored\n", cred->uid); + err(udev, "sender uid=%i, message ignored\n", cred->uid); return; } if (strncmp(ctrl_msg.magic, UDEVD_CTRL_MAGIC, sizeof(UDEVD_CTRL_MAGIC)) != 0 ) { - err("message magic '%s' doesn't match, ignore it\n", ctrl_msg.magic); + err(udev, "message magic '%s' doesn't match, ignore it\n", ctrl_msg.magic); return; } @@ -704,52 +714,52 @@ static void get_ctrl_msg(void) case UDEVD_CTRL_ENV: pos = strchr(ctrl_msg.buf, '='); if (pos == NULL) { - err("wrong key format '%s'\n", ctrl_msg.buf); + err(udev, "wrong key format '%s'\n", ctrl_msg.buf); break; } pos[0] = '\0'; if (pos[1] == '\0') { - info("udevd message (ENV) received, unset '%s'\n", ctrl_msg.buf); + info(udev, "udevd message (ENV) received, unset '%s'\n", ctrl_msg.buf); unsetenv(ctrl_msg.buf); } else { - info("udevd message (ENV) received, set '%s=%s'\n", ctrl_msg.buf, &pos[1]); + info(udev, "udevd message (ENV) received, set '%s=%s'\n", ctrl_msg.buf, &pos[1]); setenv(ctrl_msg.buf, &pos[1], 1); } break; case UDEVD_CTRL_STOP_EXEC_QUEUE: - info("udevd message (STOP_EXEC_QUEUE) received\n"); + info(udev, "udevd message (STOP_EXEC_QUEUE) received\n"); stop_exec_q = 1; break; case UDEVD_CTRL_START_EXEC_QUEUE: - info("udevd message (START_EXEC_QUEUE) received\n"); + info(udev, "udevd message (START_EXEC_QUEUE) received\n"); stop_exec_q = 0; - msg_queue_manager(); + msg_queue_manager(udev); break; case UDEVD_CTRL_SET_LOG_LEVEL: - info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i\n", ctrl_msg.intval); - udev_log_priority = ctrl_msg.intval; - sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority); - putenv(udev_log); + info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", ctrl_msg.intval); + udev_set_log_priority(udev, ctrl_msg.intval); + sprintf(udev_log_env, "UDEV_LOG=%i", udev_get_log_priority(udev)); + putenv(udev_log_env); break; case UDEVD_CTRL_SET_MAX_CHILDS: - info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i\n", ctrl_msg.intval); + info(udev, "udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i\n", ctrl_msg.intval); max_childs = ctrl_msg.intval; break; case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING: - info("udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs_running=%i\n", ctrl_msg.intval); + info(udev, "udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs_running=%i\n", ctrl_msg.intval); max_childs_running = ctrl_msg.intval; break; case UDEVD_CTRL_RELOAD_RULES: - info("udevd message (RELOAD_RULES) received\n"); + info(udev, "udevd message (RELOAD_RULES) received\n"); reload_config = 1; break; default: - err("unknown control message type\n"); + err(udev, "unknown control message type\n"); } } /* receive the kernel user event message and do some sanity checks */ -static struct udevd_uevent_msg *get_netlink_msg(void) +static struct udevd_uevent_msg *get_netlink_msg(struct udev *udev) { struct udevd_uevent_msg *msg; int bufpos; @@ -760,38 +770,38 @@ static struct udevd_uevent_msg *get_netlink_msg(void) size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0); if (size < 0) { if (errno != EINTR) - err("unable to receive kernel netlink message: %s\n", strerror(errno)); + err(udev, "unable to receive kernel netlink message: %s\n", strerror(errno)); return NULL; } if ((size_t)size > sizeof(buffer)-1) size = sizeof(buffer)-1; buffer[size] = '\0'; - dbg("uevent_size=%zi\n", size); + dbg(udev, "uevent_size=%zi\n", size); /* start of event payload */ bufpos = strlen(buffer)+1; - msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos); + msg = get_msg_from_envbuf(udev, &buffer[bufpos], size-bufpos); if (msg == NULL) return NULL; /* validate message */ pos = strchr(buffer, '@'); if (pos == NULL) { - err("invalid uevent '%s'\n", buffer); + err(udev, "invalid uevent '%s'\n", buffer); free(msg); return NULL; } pos[0] = '\0'; if (msg->action == NULL) { - info("no ACTION in payload found, skip event '%s'\n", buffer); + info(udev, "no ACTION in payload found, skip event '%s'\n", buffer); free(msg); return NULL; } if (strcmp(msg->action, buffer) != 0) { - err("ACTION in payload does not match uevent, skip event '%s'\n", buffer); + err(udev, "ACTION in payload does not match uevent, skip event '%s'\n", buffer); free(msg); return NULL; } @@ -826,7 +836,7 @@ static void udev_done(int pid, int exitstatus) list_for_each_entry(msg, &running_list, node) { if (msg->pid == pid) { - info("seq %llu, pid [%d] exit with %i, %ld seconds old\n", msg->seqnum, msg->pid, + info(msg->udev, "seq %llu, pid [%d] exit with %i, %ld seconds old\n", msg->seqnum, msg->pid, exitstatus, time(NULL) - msg->queue_time); msg->exitstatus = exitstatus; msg_queue_delete(msg); @@ -857,7 +867,7 @@ static void reap_sigchilds(void) } } -static int init_udevd_socket(void) +static int init_udevd_socket(struct udev *udev) { struct sockaddr_un saddr; socklen_t addrlen; @@ -874,14 +884,14 @@ static int init_udevd_socket(void) udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0); if (udevd_sock == -1) { - err("error getting socket: %s\n", strerror(errno)); + err(udev, "error getting socket: %s\n", strerror(errno)); return -1; } /* the bind takes care of ensuring only one copy running */ retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen); if (retval < 0) { - err("bind failed: %s\n", strerror(errno)); + err(udev, "bind failed: %s\n", strerror(errno)); close(udevd_sock); udevd_sock = -1; return -1; @@ -893,7 +903,7 @@ static int init_udevd_socket(void) return 0; } -static int init_uevent_netlink_sock(void) +static int init_uevent_netlink_sock(struct udev *udev) { struct sockaddr_nl snl; const int buffersize = 16 * 1024 * 1024; @@ -906,7 +916,7 @@ static int init_uevent_netlink_sock(void) uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); if (uevent_netlink_sock == -1) { - err("error getting socket: %s\n", strerror(errno)); + err(udev, "error getting socket: %s\n", strerror(errno)); return -1; } @@ -915,7 +925,7 @@ static int init_uevent_netlink_sock(void) retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl)); if (retval < 0) { - err("bind failed: %s\n", strerror(errno)); + err(udev, "bind failed: %s\n", strerror(errno)); close(uevent_netlink_sock); uevent_netlink_sock = -1; return -1; @@ -923,14 +933,14 @@ static int init_uevent_netlink_sock(void) return 0; } -static void export_initial_seqnum(void) +static void export_initial_seqnum(struct udev *udev) { char filename[PATH_SIZE]; int fd; char seqnum[32]; ssize_t len = 0; - strlcpy(filename, sysfs_path, sizeof(filename)); + strlcpy(filename, udev_get_sys_path(udev), sizeof(filename)); strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename)); fd = open(filename, O_RDONLY); if (fd >= 0) { @@ -941,9 +951,9 @@ static void export_initial_seqnum(void) strcpy(seqnum, "0\n"); len = 3; } - strlcpy(filename, udev_root, sizeof(filename)); + strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename)); - create_path(filename); + create_path(udev, filename); fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); if (fd >= 0) { write(fd, seqnum, len); @@ -953,13 +963,13 @@ static void export_initial_seqnum(void) int main(int argc, char *argv[]) { + struct udev *udev; int retval; int fd; struct sigaction act; fd_set readfds; const char *value; int daemonize = 0; - int option; static const struct option options[] = { { "daemon", 0, NULL, 'd' }, { "debug-trace", 0, NULL, 't' }, @@ -971,12 +981,19 @@ int main(int argc, char *argv[]) int rc = 1; int maxfd; + udev = udev_new(); + if (udev == NULL) + goto exit; + logging_init("udevd"); - udev_config_init(); - selinux_init(); - dbg("version %s\n", VERSION); + udev_set_log_fn(udev, log_fn); + + selinux_init(udev); + dbg(udev, "version %s\n", VERSION); while (1) { + int option; + option = getopt_long(argc, argv, "dDthV", options, NULL); if (option == -1) break; @@ -990,8 +1007,8 @@ int main(int argc, char *argv[]) break; case 'D': debug = 1; - if (udev_log_priority < LOG_INFO) - udev_log_priority = LOG_INFO; + if (udev_get_log_priority(udev) < LOG_INFO) + udev_set_log_priority(udev, LOG_INFO); break; case 'h': printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--debug] [--version]\n"); @@ -1006,7 +1023,7 @@ int main(int argc, char *argv[]) if (getuid() != 0) { fprintf(stderr, "root privileges required\n"); - err("root privileges required\n"); + err(udev, "root privileges required\n"); goto exit; } @@ -1014,7 +1031,7 @@ int main(int argc, char *argv[]) fd = open("/dev/null", O_RDWR); if (fd < 0) { fprintf(stderr, "cannot open /dev/null\n"); - err("cannot open /dev/null\n"); + err(udev, "cannot open /dev/null\n"); } if (fd > STDIN_FILENO) dup2(fd, STDIN_FILENO); @@ -1024,22 +1041,22 @@ int main(int argc, char *argv[]) dup2(fd, STDERR_FILENO); /* init sockets to receive events */ - if (init_udevd_socket() < 0) { + if (init_udevd_socket(udev) < 0) { if (errno == EADDRINUSE) { fprintf(stderr, "another udev daemon already running\n"); - err("another udev daemon already running\n"); + err(udev, "another udev daemon already running\n"); rc = 1; } else { fprintf(stderr, "error initializing udevd socket\n"); - err("error initializing udevd socket\n"); + err(udev, "error initializing udevd socket\n"); rc = 2; } goto exit; } - if (init_uevent_netlink_sock() < 0) { + if (init_uevent_netlink_sock(udev) < 0) { fprintf(stderr, "error initializing netlink socket\n"); - err("error initializing netlink socket\n"); + err(udev, "error initializing netlink socket\n"); rc = 3; goto exit; } @@ -1047,37 +1064,37 @@ int main(int argc, char *argv[]) /* setup signal handler pipe */ retval = pipe(signal_pipe); if (retval < 0) { - err("error getting pipes: %s\n", strerror(errno)); + err(udev, "error getting pipes: %s\n", strerror(errno)); goto exit; } retval = fcntl(signal_pipe[READ_END], F_GETFL, 0); if (retval < 0) { - err("error fcntl on read pipe: %s\n", strerror(errno)); + err(udev, "error fcntl on read pipe: %s\n", strerror(errno)); goto exit; } retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK); if (retval < 0) { - err("error fcntl on read pipe: %s\n", strerror(errno)); + err(udev, "error fcntl on read pipe: %s\n", strerror(errno)); goto exit; } retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0); if (retval < 0) { - err("error fcntl on write pipe: %s\n", strerror(errno)); + err(udev, "error fcntl on write pipe: %s\n", strerror(errno)); goto exit; } retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK); if (retval < 0) { - err("error fcntl on write pipe: %s\n", strerror(errno)); + err(udev, "error fcntl on write pipe: %s\n", strerror(errno)); goto exit; } /* parse the rules and keep them in memory */ sysfs_init(); - udev_rules_init(&rules, 1); + udev_rules_init(udev, &rules, 1); - export_initial_seqnum(); + export_initial_seqnum(udev); if (daemonize) { pid_t pid; @@ -1085,14 +1102,14 @@ int main(int argc, char *argv[]) pid = fork(); switch (pid) { case 0: - dbg("daemonized fork running\n"); + dbg(udev, "daemonized fork running\n"); break; case -1: - err("fork of daemon failed: %s\n", strerror(errno)); + err(udev, "fork of daemon failed: %s\n", strerror(errno)); rc = 4; goto exit; default: - dbg("child [%u] running, parent exits\n", pid); + dbg(udev, "child [%u] running, parent exits\n", pid); rc = 0; goto exit; } @@ -1113,12 +1130,12 @@ int main(int argc, char *argv[]) /* become session leader */ sid = setsid(); - dbg("our session is %d\n", sid); + dbg(udev, "our session is %d\n", sid); /* OOM_DISABLE == -17 */ fd = open("/proc/self/oom_adj", O_RDWR); if (fd < 0) - err("error disabling OOM: %s\n", strerror(errno)); + err(udev, "error disabling OOM: %s\n", strerror(errno)); else { write(fd, "-17", 3); close(fd); @@ -1145,8 +1162,8 @@ int main(int argc, char *argv[]) /* watch rules directory */ inotify_fd = inotify_init(); if (inotify_fd >= 0) { - if (udev_rules_dir[0] != '\0') { - inotify_add_watch(inotify_fd, udev_rules_dir, + if (udev_get_rules_path(udev) != NULL) { + inotify_add_watch(inotify_fd, udev_get_rules_path(udev), IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE); } else { char filename[PATH_MAX]; @@ -1157,15 +1174,15 @@ int main(int argc, char *argv[]) IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE); /* watch dynamic rules directory */ - strlcpy(filename, udev_root, sizeof(filename)); + strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); strlcat(filename, "/.udev/rules.d", sizeof(filename)); inotify_add_watch(inotify_fd, filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE); } } else if (errno == ENOSYS) - err("the kernel does not support inotify, udevd can't monitor rules file changes\n"); + err(udev, "the kernel does not support inotify, udevd can't monitor rules file changes\n"); else - err("inotify_init failed: %s\n", strerror(errno)); + err(udev, "inotify_init failed: %s\n", strerror(errno)); /* maximum limit of forked childs */ value = getenv("UDEVD_MAX_CHILDS"); @@ -1178,7 +1195,7 @@ int main(int argc, char *argv[]) else max_childs = UDEVD_MAX_CHILDS; } - info("initialize max_childs to %u\n", max_childs); + info(udev, "initialize max_childs to %u\n", max_childs); /* start to throttle forking if maximum number of _running_ childs is reached */ value = getenv("UDEVD_MAX_CHILDS_RUNNING"); @@ -1191,14 +1208,14 @@ int main(int argc, char *argv[]) else max_childs_running = UDEVD_MAX_CHILDS_RUNNING; } - info("initialize max_childs_running to %u\n", max_childs_running); + info(udev, "initialize max_childs_running to %u\n", max_childs_running); /* clear environment for forked event processes */ clearenv(); /* export log_priority , as called programs may want to follow that setting */ - sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority); - putenv(udev_log); + sprintf(udev_log_env, "UDEV_LOG=%i", udev_get_log_priority(udev)); + putenv(udev_log_env); if (debug_trace) putenv("DEBUG=1"); @@ -1221,17 +1238,17 @@ int main(int argc, char *argv[]) fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL); if (fdcount < 0) { if (errno != EINTR) - err("error in select: %s\n", strerror(errno)); + err(udev, "error in select: %s\n", strerror(errno)); continue; } /* get control message */ if (FD_ISSET(udevd_sock, &readfds)) - get_ctrl_msg(); + get_ctrl_msg(udev); /* get netlink message */ if (FD_ISSET(uevent_netlink_sock, &readfds)) { - msg = get_netlink_msg(); + msg = get_netlink_msg(udev); if (msg) msg_queue_insert(msg); } @@ -1254,7 +1271,7 @@ int main(int argc, char *argv[]) reload_config = 1; buf = malloc(nbytes); if (buf == NULL) { - err("error getting buffer for inotify, disable watching\n"); + err(udev, "error getting buffer for inotify, disable watching\n"); close(inotify_fd); inotify_fd = -1; } @@ -1267,7 +1284,7 @@ int main(int argc, char *argv[]) if (reload_config) { reload_config = 0; udev_rules_cleanup(&rules); - udev_rules_init(&rules, 1); + udev_rules_init(udev, &rules, 1); } /* forked child has returned */ @@ -1279,7 +1296,7 @@ int main(int argc, char *argv[]) if (run_exec_q) { run_exec_q = 0; if (!stop_exec_q) - msg_queue_manager(); + msg_queue_manager(udev); } } rc = 0; @@ -1287,7 +1304,7 @@ int main(int argc, char *argv[]) exit: udev_rules_cleanup(&rules); sysfs_cleanup(); - selinux_exit(); + selinux_exit(udev); if (signal_pipe[READ_END] >= 0) close(signal_pipe[READ_END]); @@ -1302,6 +1319,5 @@ exit: close(uevent_netlink_sock); logging_close(); - return rc; } diff --git a/udev/udevd.h b/udev/udevd.h index a22bc96e..63c99516 100644 --- a/udev/udevd.h +++ b/udev/udevd.h @@ -54,20 +54,3 @@ struct udevd_ctrl_msg { }; }; -struct udevd_uevent_msg { - struct list_head node; - pid_t pid; - int exitstatus; - time_t queue_time; - char *action; - char *devpath; - char *subsystem; - char *driver; - dev_t devt; - unsigned long long seqnum; - char *devpath_old; - char *physdevpath; - unsigned int timeout; - char *envp[UEVENT_NUM_ENVP+1]; - char envbuf[]; -};