From: Kay Sievers Date: Wed, 15 Dec 2010 07:57:46 +0000 (+0100) Subject: libudev: record and export "age" of device record X-Git-Tag: 174~279 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c6a11b1c6c68dabfd28b72d361da0e1930c7321;p=systemd libudev: record and export "age" of device record --- diff --git a/configure.ac b/configure.ac index e82b9b8e..7fda8bba 100644 --- a/configure.ac +++ b/configure.ac @@ -12,6 +12,8 @@ GTK_DOC_CHECK(1.10) AC_PREFIX_DEFAULT([/usr]) AC_PATH_PROG([XSLTPROC], [xsltproc]) +AC_SEARCH_LIBS([clock_gettime], [rt], [], [AC_MSG_ERROR([POSIX RT library not found])]) + AC_ARG_WITH([rootlibdir], AS_HELP_STRING([--with-rootlibdir=DIR], [rootfs directory to install shared libraries]), [], [with_rootlibdir=$libdir]) diff --git a/libudev/docs/libudev-sections.txt b/libudev/docs/libudev-sections.txt index 05647768..7db9c1bb 100644 --- a/libudev/docs/libudev-sections.txt +++ b/libudev/docs/libudev-sections.txt @@ -55,6 +55,7 @@ udev_device_get_devnum udev_device_get_action udev_device_get_sysattr_value udev_device_get_seqnum +udev_device_get_usec_since_initialized
diff --git a/libudev/exported_symbols b/libudev/exported_symbols index 9e77fb1b..2e6a9b7d 100644 --- a/libudev/exported_symbols +++ b/libudev/exported_symbols @@ -37,6 +37,7 @@ udev_device_get_action udev_device_get_driver udev_device_get_devnum udev_device_get_seqnum +udev_device_get_usec_since_initialized udev_device_get_sysattr_value udev_enumerate_new udev_enumerate_ref diff --git a/libudev/libudev-device-private.c b/libudev/libudev-device-private.c index 36824a74..3afa82e0 100644 --- a/libudev/libudev-device-private.c +++ b/libudev/libudev-device-private.c @@ -147,6 +147,8 @@ int udev_device_update_db(struct udev_device *udev_device) fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device)); if (udev_device_get_watch_handle(udev_device) >= 0) fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device)); + if (udev_device_get_usec_initialized(udev_device) > 0) + fprintf(f, "I:%llu\n", udev_device_get_usec_initialized(udev_device)); udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) { if (!udev_list_entry_get_flags(list_entry)) continue; diff --git a/libudev/libudev-device.c b/libudev/libudev-device.c index 66f80631..16bee19d 100644 --- a/libudev/libudev-device.c +++ b/libudev/libudev-device.c @@ -63,6 +63,7 @@ struct udev_device { struct udev_list_node sysattr_list; struct udev_list_node tags_list; unsigned long long int seqnum; + unsigned long long int usec_initialized; int event_timeout; int timeout; int devlink_priority; @@ -284,6 +285,9 @@ int udev_device_read_db(struct udev_device *udev_device) case 'W': udev_device_set_watch_handle(udev_device, atoi(val)); break; + case 'I': + udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10)); + break; } } fclose(f); @@ -1094,6 +1098,44 @@ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device) return udev_device->seqnum; } +/** + * udev_device_get_usec_since_initialized: + * @udev_device: udev device + * + * Return the number of microseconds passed since udev set up the + * device for the first time. + * + * This is only implemented for devices with need to store properties + * in the udev database. All other devices return 0 here. + * + * Returns: the number of microseconds since the device was first seen. + **/ +unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device) +{ + unsigned long long now; + + if (udev_device == NULL) + return 0; + if (!udev_device->info_loaded) + udev_device_read_db(udev_device); + if (udev_device->usec_initialized == 0) + return 0; + now = usec_monotonic(); + if (now == 0) + return 0; + return now - udev_device->usec_initialized; +} + +unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device) +{ + return udev_device->usec_initialized; +} + +void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized) +{ + udev_device->usec_initialized = usec_initialized; +} + /** * udev_device_get_sysattr_value: * @udev_device: udev device @@ -1318,7 +1360,7 @@ const char *udev_device_get_id_filename(struct udev_device *udev_device) * device node permissions and context, or has renamed a network * device. * - * For now, this is only implemented for devices with a device node + * This is only implemented for devices with a device node * or network interfaces. All other devices return 1 here. * * Returns: 1 if the device is set up. 0 otherwise. diff --git a/libudev/libudev-private.h b/libudev/libudev-private.h index f7b4f905..f95be53d 100644 --- a/libudev/libudev-private.h +++ b/libudev/libudev-private.h @@ -98,6 +98,8 @@ int udev_device_get_event_timeout(struct udev_device *udev_device); int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout); int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum); int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum); +unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device); +void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized); int udev_device_get_devlink_priority(struct udev_device *udev_device); int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio); int udev_device_get_watch_handle(struct udev_device *udev_device); @@ -224,6 +226,7 @@ int util_run_program(struct udev *udev, const char *command, char **envp, const sigset_t *sigmask, bool reset_prio); int util_resolve_subsys_kernel(struct udev *udev, const char *string, char *result, size_t maxsize, int read_value); +unsigned long long usec_monotonic(void); /* libudev-selinux-private.c */ #ifndef WITH_SELINUX diff --git a/libudev/libudev-util.c b/libudev/libudev-util.c index 6c309afd..51dd0174 100644 --- a/libudev/libudev-util.c +++ b/libudev/libudev-util.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "libudev.h" @@ -553,3 +554,15 @@ uint64_t util_string_bloom64(const char *str) bits |= 1LLU << ((hash >> 18) & 63); return bits; } + +#define USEC_PER_SEC 1000000ULL +#define NSEC_PER_USEC 1000ULL +unsigned long long usec_monotonic(void) +{ + struct timespec ts; + + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) + return 0; + return (unsigned long long) ts.tv_sec * USEC_PER_SEC + + (unsigned long long) ts.tv_nsec / NSEC_PER_USEC; +} diff --git a/libudev/libudev.h b/libudev/libudev.h index 087991ca..0abd7c8b 100644 --- a/libudev/libudev.h +++ b/libudev/libudev.h @@ -97,6 +97,7 @@ const char *udev_device_get_driver(struct udev_device *udev_device); dev_t udev_device_get_devnum(struct udev_device *udev_device); const char *udev_device_get_action(struct udev_device *udev_device); unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device); +unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device); const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr); /* diff --git a/libudev/libudev.pc.in b/libudev/libudev.pc.in index 677d53a9..f75794ce 100644 --- a/libudev/libudev.pc.in +++ b/libudev/libudev.pc.in @@ -6,6 +6,6 @@ includedir=@prefix@/include Name: libudev Description: Library to access udev device information Version: @VERSION@ -Libs: -L${libdir} -ludev +Libs: -L${libdir} -ludev -lrt Libs.private: Cflags: -I${includedir} diff --git a/udev/udev-event.c b/udev/udev-event.c index 4d38c5b8..100a79c1 100644 --- a/udev/udev-event.c +++ b/udev/udev-event.c @@ -643,6 +643,13 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules) err = udev_node_add(dev, event->mode, event->uid, event->gid); } + /* preserve old, or get new initialization timestamp */ + if (event->dev_db != NULL && udev_device_get_usec_initialized(event->dev_db) > 0) + udev_device_set_usec_initialized(event->dev, udev_device_get_usec_initialized(event->dev_db)); + else + udev_device_set_usec_initialized(event->dev, usec_monotonic()); + + /* (re)write database file */ udev_device_update_db(dev); udev_device_tag_index(dev, event->dev_db, true); udev_device_set_is_initialized(dev);