static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) { struct udev *udev = udev_device_get_udev(parent); struct udev_device *transportdev; struct udev_device *sessiondev = NULL; const char *target; char *connname; struct udev_device *conndev = NULL; const char *addr; const char *port; /* find iscsi session */ transportdev = parent; while (1) { transportdev = udev_device_get_parent(transportdev); if (transportdev == NULL) return NULL; if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0) break; } if (transportdev == NULL) return NULL; /* find iscsi session device */ sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev)); if (sessiondev == NULL) return NULL; target = udev_device_get_sysattr_value(sessiondev, "targetname"); if (target == NULL) { parent = NULL; goto out; } if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) { parent = NULL; goto out; } conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname); free(connname); if (conndev == NULL) { parent = NULL; goto out; } addr = udev_device_get_sysattr_value(conndev, "persistent_address"); port = udev_device_get_sysattr_value(conndev, "persistent_port"); if (addr == NULL || port == NULL) { parent = NULL; goto out; } path_prepend(path, "ip-%s:%s-iscsi-%s-lun-%s", addr, port, target, udev_device_get_sysnum(parent)); out: udev_device_unref(sessiondev); udev_device_unref(conndev); return parent; }
static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path) { struct udev *udev = udev_device_get_udev(parent); struct udev_device *targetdev; struct udev_device *fcdev = NULL; const char *port; unsigned int lun; targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target"); if (targetdev == NULL) return NULL; fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev)); if (fcdev == NULL) return NULL; port = udev_device_get_sysattr_value(fcdev, "port_name"); if (port == NULL) { parent = NULL; goto out; } lun = strtoul(udev_device_get_sysnum(parent), NULL, 10); path_prepend(path, "fc-%s:0x%04x%04x00000000", port, lun & 0xffff, (lun >> 16) & 0xffff); out: udev_device_unref(fcdev); return parent; }
void Storage::checkDevice(int fd) { PowerManagerLock* powerLock = PowerManager::getNewLock(this); powerLock->activate(); qDebug() << Q_FUNC_INFO << "FD: "<< fd; struct udev_device *dev; dev = udev_monitor_receive_device(udev_monitor); if (dev) { QString device = udev_device_get_devnode(dev); QString path = udev_device_get_devpath(dev); QString action = udev_device_get_action(dev); QString devtype = udev_device_get_devtype(dev); QString sysname("/dev/"); sysname += QString(udev_device_get_sysname(dev)); QString parent = udev_device_get_devpath( udev_device_get_parent (dev)); if (sysname.contains(QRegExp("mmcblk[0-9]\\d*")) || sysname.contains(QRegExp("mmcblk[0-9]"))) { qDebug() << "Got " << path << ":" << action; qDebug() << " Got Device"; qDebug() << " Node: " << udev_device_get_devnode(dev); qDebug() << " Subsystem: "<< udev_device_get_subsystem(dev); qDebug() << " Devtype: " << devtype; qDebug() << " Action: " << action; qDebug() << " Sysname: " << sysname; qDebug() << " sysnum: " << udev_device_get_sysnum (dev); qDebug() << " parent: " << parent; processRemovableUdevEvents(devtype, path, sysname, action); } udev_device_unref(dev); } delete powerLock; }
int Device::sysfsNumber() const { if (!d) return -1; QString value = QString::fromLatin1(udev_device_get_sysnum(d->udev)); bool success = false; int number = value.toInt(&success); if (success) return number; return -1; }
static bool is_hotplug(uint32_t drm_id, struct udev_device *device) { uint32_t id; const char *sysnum; if (!(sysnum = udev_device_get_sysnum(device)) || !chck_cstr_to_u32(sysnum, &id) || id != drm_id) return false; const char *val; if (!(val = udev_device_get_property_value(device, "HOTPLUG"))) return false; return chck_cstreq(val, "1"); }
static void print_device(struct udev_device *device) { const char *str; dev_t devnum; int count; struct udev_list_entry *list_entry; printf("*** device: %p ***\n", device); str = udev_device_get_action(device); if (str != NULL) printf("action: '%s'\n", str); str = udev_device_get_syspath(device); printf("syspath: '%s'\n", str); str = udev_device_get_sysname(device); printf("sysname: '%s'\n", str); str = udev_device_get_sysnum(device); if (str != NULL) printf("sysnum: '%s'\n", str); str = udev_device_get_devpath(device); printf("devpath: '%s'\n", str); str = udev_device_get_subsystem(device); if (str != NULL) printf("subsystem: '%s'\n", str); str = udev_device_get_devtype(device); if (str != NULL) printf("devtype: '%s'\n", str); str = udev_device_get_driver(device); if (str != NULL) printf("driver: '%s'\n", str); str = udev_device_get_devnode(device); if (str != NULL) printf("devname: '%s'\n", str); devnum = udev_device_get_devnum(device); if (major(devnum) > 0) printf("devnum: %u:%u\n", major(devnum), minor(devnum)); count = 0; udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) { printf("link: '%s'\n", udev_list_entry_get_name(list_entry)); count++; }
int Storage::enumerate_mmcblk_devices(struct udev *udev) { struct udev_enumerate *udev_enumerate; qDebug() << Q_FUNC_INFO << ": enumerate 'block' devices"; udev_enumerate = udev_enumerate_new(udev); if (udev_enumerate == NULL) return -1; udev_enumerate_add_match_subsystem(udev_enumerate,"block"); udev_enumerate_scan_devices(udev_enumerate); struct udev_list_entry *list_entry; udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) { struct udev_device *device; device = udev_device_new_from_syspath(udev_enumerate_get_udev(udev_enumerate), udev_list_entry_get_name(list_entry)); if (device != NULL) { QString sysname("/dev/"); sysname += QString(udev_device_get_sysname(device)); if (sysname.contains(QRegExp("mmcblk[1-9]\\d*")) || sysname.contains(QRegExp("mmcblk[1-9]"))) { QString path = udev_device_get_devpath(device); QString parent = udev_device_get_devpath( udev_device_get_parent (device)); QString action = "add"; QString devtype = udev_device_get_devtype(device); qDebug() << "Enumerate device " << path << ":" << action; qDebug() << " Got Device"; qDebug() << " Node: " << udev_device_get_devnode(device); qDebug() << " Subsystem: "<< udev_device_get_subsystem(device); qDebug() << " Devtype: " << devtype; qDebug() << " Action: " << action; qDebug() << " Sysname: " << sysname; qDebug() << " sysnum: " << udev_device_get_sysnum (device); qDebug() << " parent: " << parent; processRemovableUdevEvents(devtype, path, sysname, action); } udev_device_unref(device); } } udev_enumerate_unref(udev_enumerate); return 0; }
int main(int argc, char **argv) { static const struct option options[] = { { "debug", no_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, {} }; struct udev *udev; struct udev_device *dev; struct udev_device *parent; char syspath[UTIL_PATH_SIZE]; const char *devpath; char *path; char *path_suffix; int rc = 1; udev = udev_new(); if (udev == NULL) goto exit; udev_log_init("path_id"); udev_set_log_fn(udev, log_fn); while (1) { int option; option = getopt_long(argc, argv, "dh", 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': printf("Usage: path_id [--debug] [--help] <devpath>\n" " --debug print debug information\n" " --help print this help text\n\n"); default: rc = 1; goto exit; } } devpath = argv[optind]; if (devpath == NULL) { fprintf(stderr, "No device specified\n"); rc = 2; goto exit; } util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL); dev = udev_device_new_from_syspath(udev, syspath); if (dev == NULL) { fprintf(stderr, "unable to access '%s'\n", devpath); rc = 3; goto exit; } path = NULL; path_suffix = NULL; /* S390 ccw bus */ parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL); if (parent != NULL) { handle_ccw(parent, dev, &path); goto out; } /* walk up the chain of devices and compose path */ parent = dev; while (parent != NULL) { const char *subsys; subsys = udev_device_get_subsystem(parent); if (subsys == NULL) { ; } else if (strcmp(subsys, "scsi_tape") == 0) { handle_scsi_tape(parent, &path_suffix); } else if (strcmp(subsys, "scsi") == 0) { parent = handle_scsi(parent, &path); } else if (strcmp(subsys, "cciss") == 0) { handle_cciss(parent, &path); } else if (strcmp(subsys, "usb") == 0) { parent = handle_usb(parent, &path); } else if (strcmp(subsys, "serio") == 0) { path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent)); parent = skip_subsystem(parent, "serio"); } else if (strcmp(subsys, "pci") == 0) { path_prepend(&path, "pci-%s", udev_device_get_sysname(parent)); parent = skip_subsystem(parent, "pci"); } else if (strcmp(subsys, "platform") == 0) { path_prepend(&path, "platform-%s", udev_device_get_sysname(parent)); parent = skip_subsystem(parent, "platform"); } else if (strcmp(subsys, "xen") == 0) { path_prepend(&path, "xen-%s", udev_device_get_sysname(parent)); parent = skip_subsystem(parent, "xen"); } else if (strcmp(subsys, "virtio") == 0) { path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent)); parent = skip_subsystem(parent, "virtio"); } parent = udev_device_get_parent(parent); } out: if (path != NULL) { if (path_suffix != NULL) { printf("ID_PATH=%s%s\n", path, path_suffix); free(path_suffix); } else { printf("ID_PATH=%s\n", path); } free(path); rc = 0; } udev_device_unref(dev); exit: udev_unref(udev); udev_log_close(); return rc; }
void UdevSubsystem::print_info(udev_device* device) { log_debug("/---------------------------------------------"); log_debug("devpath: " << udev_device_get_devpath(device)); if (udev_device_get_action(device)) log_debug("action: " << udev_device_get_action(device)); //log_debug("init: " << udev_device_get_is_initialized(device)); if (udev_device_get_subsystem(device)) log_debug("subsystem: " << udev_device_get_subsystem(device)); if (udev_device_get_devtype(device)) log_debug("devtype: " << udev_device_get_devtype(device)); if (udev_device_get_syspath(device)) log_debug("syspath: " << udev_device_get_syspath(device)); if (udev_device_get_sysname(device)) log_debug("sysname: " << udev_device_get_sysname(device)); if (udev_device_get_sysnum(device)) log_debug("sysnum: " << udev_device_get_sysnum(device)); if (udev_device_get_devnode(device)) log_debug("devnode: " << udev_device_get_devnode(device)); if (udev_device_get_driver(device)) log_debug("driver: " << udev_device_get_driver(device)); if (udev_device_get_action(device)) log_debug("action: " << udev_device_get_action(device)); //udev_device_get_sysattr_value(device, "busnum"); //udev_device_get_sysattr_value(device, "devnum"); #if 0 // FIXME: only works with newer versions of libudev { log_debug("list: "); struct udev_list_entry* it = udev_device_get_tags_list_entry(device); while((it = udev_list_entry_get_next(it)) != 0) { log_debug(" " << udev_list_entry_get_name(it) << " = " << udev_list_entry_get_value(it) ); } } { log_debug("properties: "); struct udev_list_entry* it = udev_device_get_properties_list_entry(device); while((it = udev_list_entry_get_next(it)) != 0) { log_debug(" " << udev_list_entry_get_name(it) << " = " << udev_list_entry_get_value(it) ); } } { log_debug("devlist: "); struct udev_list_entry* it = udev_device_get_tags_list_entry(device); while((it = udev_list_entry_get_next(it)) != 0) { log_debug(" " << udev_list_entry_get_name(it) << " = " << udev_list_entry_get_value(it) ); } } #endif log_debug("\\----------------------------------------------"); }
static int get_js_number(struct udev_device *udevice) { struct udev_list_entry *devices, *dev_list_entry; struct udev_enumerate *enumerate; struct udev_device *hid_parent; const char *hidraw_node; const char *hid_id; int number = 0; hid_parent = udev_device_get_parent_with_subsystem_devtype(udevice, "hid", NULL); /* * Look for HID_UNIQ first for the correct behavior via BT, if * HID_UNIQ is not available it means the USB bus is being used and we * can rely on HID_PHYS. */ hid_id = udev_device_get_property_value(hid_parent, "HID_UNIQ"); if (!hid_id) hid_id = udev_device_get_property_value(hid_parent, "HID_PHYS"); hidraw_node = udev_device_get_devnode(udevice); if (!hid_id || !hidraw_node) return 0; enumerate = udev_enumerate_new(udev_device_get_udev(udevice)); udev_enumerate_add_match_sysname(enumerate, "js*"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); udev_list_entry_foreach(dev_list_entry, devices) { struct udev_device *input_parent; struct udev_device *js_dev; const char *input_id; const char *devname; devname = udev_list_entry_get_name(dev_list_entry); js_dev = udev_device_new_from_syspath( udev_device_get_udev(udevice), devname); input_parent = udev_device_get_parent_with_subsystem_devtype( js_dev, "input", NULL); if (!input_parent) goto next; /* check if this is the joystick relative to the hidraw device * above */ input_id = udev_device_get_sysattr_value(input_parent, "uniq"); /* * A strlen() check is needed because input device over USB * have the UNIQ attribute defined but with an empty value. */ if (!input_id || strlen(input_id) == 0) input_id = udev_device_get_sysattr_value(input_parent, "phys"); if (!input_id) goto next; if (!strcmp(input_id, hid_id)) { number = atoi(udev_device_get_sysnum(js_dev)); /* joystick numbers start from 0, leds from 1 */ number++; udev_device_unref(js_dev); break; } next: udev_device_unref(js_dev); } udev_enumerate_unref(enumerate); return number; }
static int get_js_number(struct udev_device *udevice) { struct udev_list_entry *devices, *dev_list_entry; struct udev_enumerate *enumerate; struct udev_device *hid_parent; const char *hidraw_node; const char *hid_phys; int number = 0; hid_parent = udev_device_get_parent_with_subsystem_devtype(udevice, "hid", NULL); hid_phys = udev_device_get_property_value(hid_parent, "HID_PHYS"); hidraw_node = udev_device_get_devnode(udevice); if (!hid_phys || !hidraw_node) return 0; enumerate = udev_enumerate_new(udev_device_get_udev(udevice)); udev_enumerate_add_match_sysname(enumerate, "js*"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); udev_list_entry_foreach(dev_list_entry, devices) { struct udev_device *input_parent; struct udev_device *js_dev; const char *input_phys; const char *devname; devname = udev_list_entry_get_name(dev_list_entry); js_dev = udev_device_new_from_syspath( udev_device_get_udev(udevice), devname); input_parent = udev_device_get_parent_with_subsystem_devtype( js_dev, "input", NULL); if (!input_parent) goto next; /* check if this is the joystick relative to the hidraw device * above */ input_phys = udev_device_get_sysattr_value(input_parent, "phys"); if (!input_phys) goto next; if (!strcmp(input_phys, hid_phys)) { number = atoi(udev_device_get_sysnum(js_dev)); /* joystick numbers start from 0, leds from 1 */ number++; udev_device_unref(js_dev); break; } next: udev_device_unref(js_dev); } udev_enumerate_unref(enumerate); return number; }
int main( int argc, char** argv ) { int rc = 0; char pathbuf[PATH_MAX+1]; struct udev* udev_client = NULL; struct udev_monitor* monitor = NULL; int monitor_fd = 0; struct udev_device* dev = NULL; struct pollfd pfd[1]; int num_events = INT_MAX; int num_forks = 0; // usage: $0 [num events to process [num times to fork]] if( argc > 1 ) { char* tmp = NULL; num_events = (int)strtol( argv[1], &tmp, 10 ); if( tmp == argv[1] || *tmp != '\0' ) { fprintf(stderr, "Usage: %s [number of events to process [number of times to fork]]\n", argv[0] ); exit(1); } if( argc > 2 ) { num_forks = (int)strtol( argv[2], &tmp, 10 ); if( tmp == argv[2] || *tmp != '\0' ) { fprintf(stderr, "Usage: %s [number of events to process [number of times to fork]]\n", argv[0] ); exit(1); } } } // make sure events dir exists log_trace("events directory '%s'", UDEV_FS_EVENTS_DIR); rc = mkdir( UDEV_FS_EVENTS_DIR, 0700 ); if( rc != 0 ) { rc = -errno; if( rc != -EEXIST ) { log_error("mkdir('%s') rc = %d", UDEV_FS_EVENTS_DIR, rc ); exit(1); } } udev_monitor_fs_events_path( "", pathbuf, 0 ); printf("Watching '%s'\n", pathbuf ); udev_client = udev_new(); if( udev_client == NULL ) { // OOM exit(2); } monitor = udev_monitor_new_from_netlink( udev_client, "udev" ); if( monitor == NULL ) { // OOM or error udev_unref( udev_client ); exit(2); } printf("Press Ctrl-C to quit\n"); monitor_fd = udev_monitor_get_fd( monitor ); if( monitor_fd < 0 ) { rc = -errno; log_error("udev_monitor_get_fd rc = %d\n", rc ); exit(3); } pfd[0].fd = monitor_fd; pfd[0].events = POLLIN; while( num_events > 0 ) { // wait for the next device rc = poll( pfd, 1, -1 ); if( rc < 0 ) { log_error("poll(%d) rc = %d\n", monitor_fd, rc ); break; } // get devices while( num_events > 0 ) { dev = udev_monitor_receive_device( monitor ); if( dev == NULL ) { break; } int pid = getpid(); struct udev_list_entry *list_entry = NULL; printf("[%d] [%d] ACTION: '%s'\n", pid, num_events, udev_device_get_action( dev ) ); printf("[%d] [%d] SEQNUM: %llu\n", pid, num_events, udev_device_get_seqnum( dev ) ); printf("[%d] [%d] USEC: %llu\n", pid, num_events, udev_device_get_usec_since_initialized( dev ) ); printf("[%d] [%d] DEVNODE: '%s'\n", pid, num_events, udev_device_get_devnode( dev ) ); printf("[%d] [%d] DEVPATH: '%s'\n", pid, num_events, udev_device_get_devpath( dev ) ); printf("[%d] [%d] SYSNAME: '%s'\n", pid, num_events, udev_device_get_sysname( dev ) ); printf("[%d] [%d] SYSPATH: '%s'\n", pid, num_events, udev_device_get_syspath( dev ) ); printf("[%d] [%d] SUBSYSTEM: '%s'\n", pid, num_events, udev_device_get_subsystem( dev ) ); printf("[%d] [%d] DEVTYPE: '%s'\n", pid, num_events, udev_device_get_devtype( dev ) ); printf("[%d] [%d] SYSNUM: '%s'\n", pid, num_events, udev_device_get_sysnum( dev ) ); printf("[%d] [%d] DRIVER: '%s'\n", pid, num_events, udev_device_get_driver( dev ) ); printf("[%d] [%d] DEVNUM: %d:%d\n", pid, num_events, major( udev_device_get_devnum( dev ) ), minor( udev_device_get_devnum( dev ) ) ); printf("[%d] [%d] IFINDEX: '%s'\n", pid, num_events, udev_device_get_property_value( dev, "IFINDEX" ) ); printf("[%d] [%d] DEVMODE: '%s'\n", pid, num_events, udev_device_get_property_value( dev, "DEVMODE" ) ); printf("[%d] [%d] DEVUID: '%s'\n", pid, num_events, udev_device_get_property_value( dev, "DEVUID" ) ); printf("[%d] [%d] DEVGID: '%s'\n", pid, num_events, udev_device_get_property_value( dev, "DEVGID" ) ); list_entry = udev_device_get_devlinks_list_entry( dev ); udev_list_entry_foreach( list_entry, udev_list_entry_get_next( list_entry )) { printf("[%d] [%d] devlink: '%s'\n", pid, num_events, udev_list_entry_get_name( list_entry ) ); } list_entry = udev_device_get_properties_list_entry( dev ); udev_list_entry_foreach( list_entry, udev_list_entry_get_next( list_entry )) { printf("[%d] [%d] property: '%s' = '%s'\n", pid, num_events, udev_list_entry_get_name( list_entry ), udev_list_entry_get_value( list_entry ) ); } list_entry = udev_device_get_tags_list_entry( dev ); udev_list_entry_foreach( list_entry, udev_list_entry_get_next( list_entry )) { printf("[%d] [%d] tag: '%s'\n", pid, num_events, udev_list_entry_get_name( list_entry ) ); }