From c7969cdbcb1f6d39177f6cc2e28597fabe186594 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Wed, 2 Apr 2008 04:43:25 +0200 Subject: [PATCH] also accept real socket files for RUN+="socket:" --- etc/udev/rules.d/95-udev-late.rules | 2 +- udev.7 | 9 +++++---- udev.xml | 8 +++++++- udev_rules.c | 27 +++++++++++++++++++-------- udevadm.8 | 11 +++++++++-- udevadm.xml | 12 +++++++++++- udevcontrol.c | 2 +- udevd.8 | 2 +- udevd.c | 2 +- udevmonitor.c | 2 +- udevtrigger.c | 18 +++++++++++++++--- 11 files changed, 71 insertions(+), 24 deletions(-) diff --git a/etc/udev/rules.d/95-udev-late.rules b/etc/udev/rules.d/95-udev-late.rules index 4a015b9e..7207081d 100644 --- a/etc/udev/rules.d/95-udev-late.rules +++ b/etc/udev/rules.d/95-udev-late.rules @@ -4,5 +4,5 @@ ACTION=="remove", ENV{REMOVE_CMD}!="", RUN+="$env{REMOVE_CMD}" # event to be catched by udevmonitor -RUN+="socket:/org/kernel/udev/monitor" +RUN+="socket:@/org/kernel/udev/monitor" diff --git a/udev.7 b/udev.7 index e237ba7d..3bc04b7c 100644 --- a/udev.7 +++ b/udev.7 @@ -1,6 +1,6 @@ .\" Title: udev .\" Author: -.\" Generator: DocBook XSL Stylesheets v1.73.1 +.\" Generator: DocBook XSL Stylesheets v1.73.2 .\" Date: August 2005 .\" Manual: udev .\" Source: udev @@ -213,6 +213,9 @@ Export a variable to the environment\. Depending on the type of operator, this k \fBRUN\fR .RS 4 Add a program to the list of programs to be executed for a specific device\. This can only be used for very short running tasks\. Running an event process for a long period of time may block all further events for this or a dependent device\. Long running tasks need to be immediately detached from the event process itself\. +.sp +If the specifiefd string starts with +\fBsocket:\fR\fB\fIpath\fR\fR, all current event values will be passed to the specified socket, as a message in the same format the kernel sends an uevent\. If the first character of the specified path is an @ character, an abstract namespace socket is used, instead of an existing socket file\. .RE .PP \fBLABEL\fR @@ -409,9 +412,7 @@ The count of characters to be substituted may be limited by specifying the forma Written by Greg Kroah\-Hartman and Kay Sievers -\. With much help from Dan Stekloff - -and many others\. +\. With much help from Dan Stekloff and many others\. .SH "SEE ALSO" .PP \fBudevd\fR(8), diff --git a/udev.xml b/udev.xml index 71832a2f..e4329653 100644 --- a/udev.xml +++ b/udev.xml @@ -333,6 +333,12 @@ event process for a long period of time may block all further events for this or a dependent device. Long running tasks need to be immediately detached from the event process itself. + If the specifiefd string starts with + , all current event + values will be passed to the specified socket, as a message in the same + format the kernel sends an uevent. If the first character of the specified path + is an @ character, an abstract namespace socket is used, instead of an existing + socket file. @@ -600,7 +606,7 @@ AUTHOR Written by Greg Kroah-Hartman greg@kroah.com and Kay Sievers kay.sievers@vrfy.org. With much help from - Dan Stekloff dsteklof@us.ibm.com and many others. + Dan Stekloff and many others. diff --git a/udev_rules.c b/udev_rules.c index 1521db08..eccea3b5 100644 --- a/udev_rules.c +++ b/udev_rules.c @@ -455,24 +455,35 @@ static int import_parent_into_env(struct udevice *udev, const char *filter) return rc; } -static int pass_env_to_socket(const char *sockname, const char *devpath, const char *action) +static int pass_env_to_socket(const char *sockpath, const char *devpath, const char *action) { int sock; struct sockaddr_un saddr; - socklen_t addrlen; + socklen_t saddrlen; + struct stat stats; char buf[2048]; size_t bufpos = 0; int i; ssize_t count; int retval = 0; - dbg("pass environment to socket '%s'", sockname); + dbg("pass environment to socket '%s'", sockpath); sock = socket(AF_LOCAL, SOCK_DGRAM, 0); memset(&saddr, 0x00, sizeof(struct sockaddr_un)); saddr.sun_family = AF_LOCAL; - /* abstract namespace only */ - strcpy(&saddr.sun_path[1], sockname); - addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; + if (sockpath[0] == '@') { + /* abstract namespace socket requested */ + strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1); + saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); + } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) { + /* existing socket file */ + strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path)); + saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path); + } else { + /* no socket file, assume abstract namespace socket */ + strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1); + saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); + } bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath); bufpos++; @@ -483,10 +494,10 @@ static int pass_env_to_socket(const char *sockname, const char *devpath, const c if (bufpos > sizeof(buf)) bufpos = sizeof(buf); - count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen); + count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen); if (count < 0) retval = -1; - info("passed %zi bytes to socket '%s', ", count, sockname); + info("passed %zi bytes to socket '%s', ", count, sockpath); close(sock); return retval; diff --git a/udevadm.8 b/udevadm.8 index acad283b..0db30126 100644 --- a/udevadm.8 +++ b/udevadm.8 @@ -1,6 +1,6 @@ .\" Title: udevadm .\" Author: -.\" Generator: DocBook XSL Stylesheets v1.73.1 +.\" Generator: DocBook XSL Stylesheets v1.73.2 .\" Date: November 2007 .\" Manual: udevadm .\" Source: udev @@ -97,7 +97,7 @@ Print help text\. .RE .SS "udevadm trigger [options]" .PP -Request kernel device uevents, usually used to replay events at system coldplug\. +Request device uevents, usually used to replay events at system coldplug\. .PP \fB\-\-verbose\fR .RS 4 @@ -138,6 +138,13 @@ Trigger events for devices with a matching sysfs attribute\. If a value is speci .RS 4 Do not trigger events for devices with a matching sysfs attribute\. If a value is specified along with the attribute name, the content of the attribute is matched against the given value using shell style pattern matching\. If no value is specified, the existence of the sysfs attribute is checked\. This option can be specified multiple times\. .RE +.PP +\fB\-\-socket\fR\fB\fIpath\fR\fR +.RS 4 +Pass the synthesized events to the specified socket, instead of triggering a global kernel event\. All available event values will be send in the same format the kernel sends an uevent, or +\fBRUN+="socket:\fR\fB\fIpath\fR\fR\fB"\fR +sends a message\. If the first character of the specified path is an @ character, an abstract namespace socket is used, instead of an existing socket file\. +.RE .SS "udevadm settle [options]" .PP Watches the udev event queue, and exits if all current events are handled\. diff --git a/udevadm.xml b/udevadm.xml index ccbeeca4..121c1401 100644 --- a/udevadm.xml +++ b/udevadm.xml @@ -130,7 +130,7 @@ udevadm trigger <optional>options</optional> - Request kernel device uevents, usually used to replay events at system coldplug. + Request device uevents, usually used to replay events at system coldplug. @@ -188,6 +188,16 @@ of the sysfs attribute is checked. This option can be specified multiple times. + + + + Pass the synthesized events to the specified socket, instead of triggering + a global kernel event. All available event values will be send in the same format + the kernel sends an uevent, or + sends a message. If the first character of the specified path is an @ character, + an abstract namespace socket is used, instead of an existing socket file. + + diff --git a/udevcontrol.c b/udevcontrol.c index 2442a3e0..f6b5dd90 100644 --- a/udevcontrol.c +++ b/udevcontrol.c @@ -144,7 +144,7 @@ int udevcontrol(int argc, char *argv[], char *envp[]) saddr.sun_family = AF_LOCAL; /* use abstract namespace for socket path */ strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH); - addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; + addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen); if (retval == -1) { diff --git a/udevd.8 b/udevd.8 index 0f0dd1ac..1323313a 100644 --- a/udevd.8 +++ b/udevd.8 @@ -1,6 +1,6 @@ .\" Title: udevd .\" Author: -.\" Generator: DocBook XSL Stylesheets v1.73.1 +.\" Generator: DocBook XSL Stylesheets v1.73.2 .\" Date: August 2005 .\" Manual: udevd .\" Source: udev diff --git a/udevd.c b/udevd.c index 82ebd22d..493bb545 100644 --- a/udevd.c +++ b/udevd.c @@ -862,7 +862,7 @@ static int init_udevd_socket(void) saddr.sun_family = AF_LOCAL; /* use abstract namespace for socket path */ strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH); - addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; + addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0); if (udevd_sock == -1) { diff --git a/udevmonitor.c b/udevmonitor.c index 75e39481..2430dd39 100644 --- a/udevmonitor.c +++ b/udevmonitor.c @@ -49,7 +49,7 @@ static int init_udev_monitor_socket(void) saddr.sun_family = AF_LOCAL; /* use abstract namespace for socket path */ strcpy(&saddr.sun_path[1], "/org/kernel/udev/monitor"); - addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; + addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); udev_monitor_sock = socket(AF_LOCAL, SOCK_DGRAM, 0); if (udev_monitor_sock == -1) { diff --git a/udevtrigger.c b/udevtrigger.c index 4e3a8fab..e50fd4ee 100644 --- a/udevtrigger.c +++ b/udevtrigger.c @@ -631,12 +631,24 @@ int udevtrigger(int argc, char *argv[], char *envp[]) } if (sockpath != NULL) { + struct stat stats; + sock = socket(AF_LOCAL, SOCK_DGRAM, 0); memset(&saddr, 0x00, sizeof(struct sockaddr_un)); saddr.sun_family = AF_LOCAL; - /* abstract namespace only */ - strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1); - saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; + if (sockpath[0] == '@') { + /* abstract namespace socket requested */ + strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1); + saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); + } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) { + /* existing socket file */ + strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path)); + saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path); + } else { + /* no socket file, assume abstract namespace socket */ + strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1); + saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]); + } } if (failed) { -- 2.39.5