X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=libudev%2Flibudev-monitor.c;h=97e52c42db430019ed26871da3a9e70b4040d944;hp=5c4e8ca04ab6159710e70119c8c86490be1779bd;hb=0ec5b5e1429307fa7573aaa9b8f25fbd9b1d71d5;hpb=a5710160377a85e7c78601e2c45049b392891436 diff --git a/libudev/libudev-monitor.c b/libudev/libudev-monitor.c index 5c4e8ca04..97e52c42d 100644 --- a/libudev/libudev-monitor.c +++ b/libudev/libudev-monitor.c @@ -27,6 +27,18 @@ #include "libudev.h" #include "libudev-private.h" +/** + * SECTION:libudev-monitor + * @short_description: device event source + * + * Connects to a device event source. + */ + +/** + * udev_monitor: + * + * Opaque object handling one event source. + */ struct udev_monitor { struct udev *udev; int refcount; @@ -127,13 +139,12 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path); udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1; } - udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0); + udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0); if (udev_monitor->sock == -1) { err(udev, "error getting socket: %m\n"); free(udev_monitor); return NULL; } - util_set_fd_cloexec(udev_monitor->sock); dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path); return udev_monitor; @@ -185,13 +196,12 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char if (udev_monitor == NULL) return NULL; - udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); + udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT); if (udev_monitor->sock == -1) { err(udev, "error getting socket: %m\n"); free(udev_monitor); return NULL; } - util_set_fd_cloexec(udev_monitor->sock); udev_monitor->snl.nl_family = AF_NETLINK; udev_monitor->snl.nl_groups = group; @@ -227,6 +237,14 @@ static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i, (*i)++; } +/** + * udev_monitor_filter_update: + * @udev_monitor: monitor + * + * Update the installed filter. This might only be needed, if the filter was removed or changed. + * + * Returns: 0 on success, otherwise a negative error value. + */ int udev_monitor_filter_update(struct udev_monitor *udev_monitor) { static struct sock_filter ins[256]; @@ -290,7 +308,14 @@ int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid; return 0; } - +/** + * udev_monitor_enable_receiving: + * @udev_monitor: the monitor which should receive events + * + * Binds the @udev_monitor socket to the event source. + * + * Returns: 0 on success, otherwise a negative error value. + */ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) { int err; @@ -309,7 +334,7 @@ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) /* * get the address the kernel has assigned us - * it is usually, but not neccessarily the pid + * it is usually, but not necessarily the pid */ addrlen = sizeof(struct sockaddr_nl); err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen); @@ -472,12 +497,6 @@ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monito ssize_t buflen; ssize_t bufpos; struct udev_monitor_netlink_header *nlh; - int devpath_set = 0; - int subsystem_set = 0; - int action_set = 0; - int maj = 0; - int min = 0; - int is_kernel = 0; retry: if (udev_monitor == NULL) @@ -518,10 +537,10 @@ retry: } } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) { if (snl.nl_pid > 0) { - info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid); + info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", + snl.nl_pid); return NULL; } - is_kernel = 1; } } @@ -563,9 +582,9 @@ retry: } udev_device = udev_device_new(udev_monitor->udev); - if (udev_device == NULL) { + if (udev_device == NULL) return NULL; - } + udev_device_set_info_loaded(udev_device); while (bufpos < buflen) { char *key; @@ -576,60 +595,11 @@ retry: if (keylen == 0) break; bufpos += keylen + 1; - - if (strncmp(key, "DEVPATH=", 8) == 0) { - char path[UTIL_PATH_SIZE]; - - util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_monitor->udev), &key[8], NULL); - udev_device_set_syspath(udev_device, path); - devpath_set = 1; - } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) { - udev_device_set_subsystem(udev_device, &key[10]); - subsystem_set = 1; - } else if (strncmp(key, "DEVTYPE=", 8) == 0) { - udev_device_set_devtype(udev_device, &key[8]); - } else if (strncmp(key, "DEVNAME=", 8) == 0) { - if (is_kernel) - udev_device_set_knodename(udev_device, &key[8]); - else - udev_device_set_devnode(udev_device, &key[8]); - } else if (strncmp(key, "DEVLINKS=", 9) == 0) { - char devlinks[UTIL_PATH_SIZE]; - char *slink; - char *next; - - util_strscpy(devlinks, sizeof(devlinks), &key[9]); - slink = devlinks; - next = strchr(slink, ' '); - while (next != NULL) { - next[0] = '\0'; - udev_device_add_devlink(udev_device, slink); - slink = &next[1]; - next = strchr(slink, ' '); - } - if (slink[0] != '\0') - udev_device_add_devlink(udev_device, slink); - } else if (strncmp(key, "DRIVER=", 7) == 0) { - udev_device_set_driver(udev_device, &key[7]); - } else if (strncmp(key, "ACTION=", 7) == 0) { - udev_device_set_action(udev_device, &key[7]); - action_set = 1; - } else if (strncmp(key, "MAJOR=", 6) == 0) { - maj = strtoull(&key[6], NULL, 10); - } else if (strncmp(key, "MINOR=", 6) == 0) { - min = strtoull(&key[6], NULL, 10); - } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) { - udev_device_set_devpath_old(udev_device, &key[12]); - } else if (strncmp(key, "SEQNUM=", 7) == 0) { - udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10)); - } else if (strncmp(key, "TIMEOUT=", 8) == 0) { - udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10)); - } else { - udev_device_add_property_from_string(udev_device, key); - } + udev_device_add_property_from_string_parse(udev_device, key); } - if (!devpath_set || !subsystem_set || !action_set) { - info(udev_monitor->udev, "missing values, skip\n"); + + if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) { + info(udev_monitor->udev, "missing values, invalid device\n"); udev_device_unref(udev_device); return NULL; } @@ -650,9 +620,6 @@ retry: return NULL; } - if (maj > 0) - udev_device_set_devnum(udev_device, makedev(maj, min)); - udev_device_set_info_loaded(udev_device); return udev_device; } @@ -722,7 +689,7 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, /* * Use custom address for target, or the default one. * - * If we send to a muticast group, we will get + * If we send to a multicast group, we will get * ECONNREFUSED, which is expected. */ if (destination != NULL) @@ -739,6 +706,16 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, return count; } +/** + * udev_monitor_filter_add_match_subsystem_devtype: + * @udev_monitor: the monitor + * @subsystem: the subsystem value to match the incoming devices against + * @devtype: the devtype value to match the incoming devices against + * + * The filter must be installed before the monitor is switched to listening mode. + * + * Returns: 0 on success, otherwise a negative error value. + */ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype) { if (udev_monitor == NULL) @@ -751,6 +728,14 @@ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_mo return 0; } +/** + * udev_monitor_filter_remove: + * @udev_monitor: monitor + * + * Remove all filters from monitor. + * + * Returns: 0 on success, otherwise a negative error value. + */ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor) { static struct sock_fprog filter = { 0, NULL };