static int manager_process_link(Manager *m, struct udev_device *device) { Link *link; int r; if (streq_ptr(udev_device_get_action(device), "remove")) { uint64_t ifindex; log_debug("Link removed: %s", udev_device_get_sysname(device)); ifindex = udev_device_get_ifindex(device); link = hashmap_get(m->links, &ifindex); if (!link) return 0; link_free(link); } else { log_debug("New link: %s", udev_device_get_sysname(device)); r = link_add(m, device); if (r < 0) { log_error("Could not handle link %s: %s", udev_device_get_sysname(device), strerror(-r)); } } return 0; }
void LinuxDeviceManager::udevReceiveDevice() { struct udev_device *dev = udev_monitor_receive_device(_umon); if (!dev) { return; } const char *action_cstr = udev_device_get_action(dev); if (!action_cstr) { log->warn("BUG? Device event witout action value."); udev_device_unref(dev); return; } if (strcmp(action_cstr, "add") == 0) { processDeviceInsertion(dev); } else if (strcmp(action_cstr, "remove") == 0) { processDeviceRemoval(dev); } else { log->warn("BUG? Unknown device action value \"{}\"", action_cstr); } udev_device_unref(dev); return; }
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; }
static void process_device(struct userdata *u, struct udev_device *dev) { const char *action, *ff; pa_assert(u); pa_assert(dev); if (udev_device_get_property_value(dev, "PULSE_IGNORE")) { pa_log_debug("Ignoring %s, because marked so.", udev_device_get_devpath(dev)); return; } if ((ff = udev_device_get_property_value(dev, "SOUND_CLASS")) && pa_streq(ff, "modem")) { pa_log_debug("Ignoring %s, because it is a modem.", udev_device_get_devpath(dev)); return; } action = udev_device_get_action(dev); if (action && pa_streq(action, "remove")) remove_card(u, dev); else if ((!action || pa_streq(action, "change")) && udev_device_get_property_value(dev, "SOUND_INITIALIZED")) card_changed(u, dev); /* For an explanation why we don't look for 'add' events here * have a look into /lib/udev/rules.d/78-sound-card.rules! */ }
static void *Run (void *data) { services_discovery_t *sd = data; services_discovery_sys_t *p_sys = sd->p_sys; struct udev_monitor *mon = p_sys->monitor; int fd = udev_monitor_get_fd (mon); struct pollfd ufd = { .fd = fd, .events = POLLIN, }; for (;;) { while (poll (&ufd, 1, -1) == -1) if (errno != EINTR) break; int canc = vlc_savecancel (); struct udev_device *dev = udev_monitor_receive_device (mon); if (dev == NULL) continue; const char *action = udev_device_get_action (dev); if (!strcmp (action, "add")) AddDevice (sd, dev); else if (!strcmp (action, "remove")) RemoveDevice (sd, dev); else if (!strcmp (action, "change")) { RemoveDevice (sd, dev); AddDevice (sd, dev); } udev_device_unref (dev); vlc_restorecancel (canc); } return NULL; }
static int udev_handle_device(void *ctx) { struct udev_device *device; const char *subsystem, *ifname, *action; device = udev_monitor_receive_device(monitor); if (device == NULL) { syslog(LOG_ERR, "libudev: received NULL device"); return -1; } subsystem = udev_device_get_subsystem(device); ifname = udev_device_get_sysname(device); action = udev_device_get_action(device); /* udev filter documentation says "usually" so double check */ if (strcmp(subsystem, "net") == 0) { syslog(LOG_DEBUG, "%s: libudev: %s", ifname, action); if (strcmp(action, "add") == 0 || strcmp(action, "move") == 0) dhcpcd.handle_interface(ctx, 1, ifname); else if (strcmp(action, "remove") == 0) dhcpcd.handle_interface(ctx, -1, ifname); } udev_device_unref(device); return 1; }
void DeviceManager::processHotplugEvents() { udev_device * device; device = udev_monitor_receive_device(hotplugMonitor); if (device) { if (string(udev_device_get_action(device)) == "remove") { map <string, Device *>::iterator it; for (it = devices.begin(); it != devices.end(); it++) { if (it->second->getNode() == udev_device_get_devnode(device)) { Device * dev = it->second; remove(dev); delete dev; break; } } } else { add(new DeviceHard(udev_device_get_devnode(device))); } udev_device_unref(device); } }
static int manager_process_link(Manager *m, struct udev_device *device) { Link *link; int r; if (streq_ptr(udev_device_get_action(device), "remove")) { uint64_t ifindex; log_debug("%s: link removed", udev_device_get_sysname(device)); ifindex = udev_device_get_ifindex(device); link = hashmap_get(m->links, &ifindex); if (!link) return 0; link_free(link); } else { r = link_add(m, device, &link); if (r < 0) { if (r == -EEXIST) log_debug("%s: link already exists, ignoring", link->ifname); else log_error("%s: could not handle link: %s", udev_device_get_sysname(device), strerror(-r)); } else log_debug("%s: link (with ifindex %" PRIu64") added", link->ifname, link->ifindex); } return 0; }
//Here we can only handle udev events related to plugging and removing entire //drives void pup_cdrom_udev_event(PupVMMonitor *monitor, struct udev_device *dev) { //An optical drive is a block device if (g_strcmp0(udev_device_get_subsystem(dev), "block") != 0) return; g_debug("processing event for %s...\n", udev_device_get_sysname(dev)); if (pup_drive_test_optical(dev)) { //We are handling it... pup_vm_monitor_stop_processing_uevent(monitor); const gchar *action = udev_device_get_action(dev); g_return_if_fail(action); if (strcmp(action, "remove") == 0) { g_debug("Removing %s", udev_device_get_sysname(dev)); PupDrive *drv = pup_vm_monitor_lookup_drive (monitor, udev_device_get_sysname(dev), FALSE); if (drv) pup_vm_monitor_remove_device(monitor, PUP_DEVICE(drv)); PupVolume *vol = pup_vm_monitor_lookup_volume (monitor, udev_device_get_sysname(dev), FALSE); if (vol) pup_vm_monitor_remove_device(monitor, PUP_DEVICE(vol)); } else { g_debug("Optical drive, now probing...\n"); pup_cd_drive_new_from_udev_device(monitor, dev); } } else g_debug("Not an optical drive\n"); }
static int manager_process_link(Manager *m, struct udev_device *device) { Link *link = NULL; int r; assert(m); assert(device); link_get(m, udev_device_get_ifindex(device), &link); if (streq_ptr(udev_device_get_action(device), "remove")) { log_debug("%s: link removed", udev_device_get_sysname(device)); if (link) link_free(link); } else { if (link) { log_debug("%s: link already exists, ignoring", link->ifname); return 0; } r = link_add(m, device, &link); if (r < 0) { log_error("%s: could not handle link: %s", udev_device_get_sysname(device), strerror(-r)); } else log_debug("%s: link (with ifindex %" PRIu64") added", link->ifname, link->ifindex); } return 0; }
static void udev_hotplug_event(struct udev_device* udev_dev) { const char* udev_action; const char* sys_name = NULL; uint8_t busnum = 0, devaddr = 0; int detached; int r; do { udev_action = udev_device_get_action(udev_dev); if (!udev_action) { break; } detached = !strncmp(udev_action, "remove", 6); r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name); if (LIBUSB_SUCCESS != r) { break; } usbi_dbg("udev hotplug event. action: %s.", udev_action); if (strncmp(udev_action, "add", 3) == 0) { linux_hotplug_enumerate(busnum, devaddr, sys_name); } else if (detached) { linux_hotplug_disconnected(busnum, devaddr, sys_name); } else { usbi_err(NULL, "ignoring udev action %s", udev_action); } } while (0); udev_device_unref(udev_dev); }
void GamePadManager::deviceEventProc( struct udev_device & _device ) { const auto PATH = udev_device_get_devnode( &_device ); if( PATH == nullptr ) { return; } if( isGamePadPath( PATH ) == false ) { return; } const auto ACTION = udev_device_get_action( &_device ); auto removed = ACTION != nullptr && std::strcmp( ACTION , "remove" ) == 0; if( removed ) { this->removeGamePad( PATH ); } else { this->addGamePad( PATH ); } }
static monome_t *monitor_attach(detector_state_t *state) { struct udev_device *ud; struct pollfd fds[1]; fds[0].fd = udev_monitor_get_fd(state->um); fds[0].events = POLLIN; do { if( poll(fds, 1, -1) < 0 ) switch( errno ) { case EINVAL: perror("error in poll()"); exit(1); case EINTR: case EAGAIN: continue; } ud = udev_monitor_receive_device(state->um); /* check if this was an add event. "add"[0] == 'a' */ if( *(udev_device_get_action(ud)) == 'a' ) spawn_server(state->exec_path, udev_device_get_devnode(ud)); udev_device_unref(ud); } while( 1 ); }
void CALSADeviceMonitor::FDEventCallback(int id, int fd, short revents, void *data) { struct udev_monitor *udevMonitor = (struct udev_monitor *)data; bool audioDevicesChanged = false; struct udev_device *device; while ((device = udev_monitor_receive_device(udevMonitor)) != NULL) { const char* action = udev_device_get_action(device); const char* soundInitialized = udev_device_get_property_value(device, "SOUND_INITIALIZED"); if (!action || !soundInitialized) continue; /* cardX devices emit a "change" event when ready (i.e. all subdevices added) */ if (strcmp(action, "change") == 0) { CLog::Log(LOGDEBUG, "CALSADeviceMonitor - ALSA card added (\"%s\", \"%s\")", udev_device_get_syspath(device), udev_device_get_devpath(device)); audioDevicesChanged = true; } else if (strcmp(action, "remove") == 0) { CLog::Log(LOGDEBUG, "CALSADeviceMonitor - ALSA card removed"); audioDevicesChanged = true; } udev_device_unref(device); } if (audioDevicesChanged) { CServiceBroker::GetActiveAE()->DeviceChange(); } }
static void make_device_msg(DBusMessageIter *dict, struct udev_device *device) { const char *key, *str; struct udev_list_entry *list_entry; mylog_trace("start %s", __func__); mylog_trace("%s add action", __func__); str = udev_device_get_action(device); if (str != NULL) mydbus_dict_append_entry(dict, "action", DBUS_TYPE_STRING, &str); mylog_trace("%s add subsystem", __func__); str = udev_device_get_subsystem(device); if (str != NULL) mydbus_dict_append_entry(dict, "subsystem", DBUS_TYPE_STRING, &str); mylog_trace("%s add properties", __func__); udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) { key = udev_list_entry_get_name(list_entry); str = udev_list_entry_get_value(list_entry); mydbus_dict_append_entry(dict, key, DBUS_TYPE_STRING, &str); }
static void udev_input_handle_hotplug(udev_input_t *udev) { bool is_keyboard, is_mouse, is_touchpad; struct udev_device *dev = udev_monitor_receive_device(udev->monitor); device_handle_cb cb = NULL; const char *devtype = NULL; const char *val_keyboard = NULL; const char *val_mouse = NULL; const char *val_touchpad = NULL; const char *action = NULL; const char *devnode = NULL; if (!dev) return; val_keyboard = udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"); val_mouse = udev_device_get_property_value(dev, "ID_INPUT_MOUSE"); val_touchpad = udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"); action = udev_device_get_action(dev); devnode = udev_device_get_devnode(dev); is_keyboard = val_keyboard && !strcmp(val_keyboard, "1") && devnode; is_mouse = val_mouse && !strcmp(val_mouse, "1") && devnode; is_touchpad = val_touchpad && !strcmp(val_touchpad, "1") && devnode; if (!is_keyboard && !is_mouse && !is_touchpad) goto end; if (is_keyboard) { cb = udev_handle_keyboard; devtype = "keyboard"; } else if (is_touchpad) { cb = udev_handle_touchpad; devtype = "touchpad"; } else if (is_mouse) { cb = udev_handle_mouse; devtype = "mouse"; } if (!strcmp(action, "add")) { RARCH_LOG("[udev]: Hotplug add %s: %s.\n", devtype, devnode); add_device(udev, devnode, cb); } else if (!strcmp(action, "remove")) { RARCH_LOG("[udev]: Hotplug remove %s: %s.\n", devtype, devnode); udev_input_remove_device(udev, devnode); } end: udev_device_unref(dev); }
static void udev_input_handle_hotplug(udev_input_t *udev) { const char *devtype = NULL; const char *val_keyboard = NULL; const char *val_mouse = NULL; const char *val_touchpad = NULL; const char *action = NULL; const char *devnode = NULL; struct udev_device *dev = udev_monitor_receive_device(udev->monitor); if (!dev) return; val_keyboard = udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"); val_mouse = udev_device_get_property_value(dev, "ID_INPUT_MOUSE"); val_touchpad = udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"); action = udev_device_get_action(dev); devnode = udev_device_get_devnode(dev); if (val_keyboard && string_is_equal_fast(val_keyboard, "1", 1) && devnode) devtype = "keyboard"; if (val_mouse && string_is_equal_fast(val_mouse, "1", 1) && devnode) devtype = "mouse"; if (val_touchpad && string_is_equal_fast(val_touchpad, "1", 1) && devnode) devtype = "touchpad"; if (!devtype) goto end; if (string_is_equal_fast(action, "add", 3)) { device_handle_cb cb = NULL; if (string_is_equal_fast(devtype, "keyboard", 8)) cb = udev_handle_keyboard; else if (string_is_equal_fast(devtype, "touchpad", 8)) cb = udev_handle_touchpad; else if (string_is_equal_fast(devtype, "mouse", 5)) cb = udev_handle_mouse; RARCH_LOG("[udev]: Hotplug add %s: %s.\n", devtype, devnode); udev_input_add_device(udev, devnode, cb); } else if (string_is_equal_fast(action, "remove", 6)) { RARCH_LOG("[udev]: Hotplug remove %s: %s.\n", devtype, devnode); udev_input_remove_device(udev, devnode); } end: udev_device_unref(dev); }
void joystick_linux::monitor_joysticks(udev *p_udev) { udev_device *dev = NULL; udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev"); udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL); udev_monitor_enable_receiving(mon); int fd = udev_monitor_get_fd(mon); while (!exit_udev) { fd_set fds; struct timeval tv; int ret; FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = 0; tv.tv_usec = 0; ret = select(fd+1, &fds, NULL, NULL, &tv); /* Check if our file descriptor has received data. */ if (ret > 0 && FD_ISSET(fd, &fds)) { /* Make the call to receive the device. select() ensured that this will not block. */ dev = udev_monitor_receive_device(mon); if (dev && udev_device_get_devnode(dev) != 0) { joy_mutex->lock(); String action = udev_device_get_action(dev); const char* devnode = udev_device_get_devnode(dev); if (devnode) { String devnode_str = devnode; if (devnode_str.find(ignore_str) == -1) { if (action == "add") open_joystick(devnode); else if (String(action) == "remove") close_joystick(get_joy_from_path(devnode)); } } udev_device_unref(dev); joy_mutex->unlock(); } } usleep(50000); } //printf("exit udev\n"); udev_monitor_unref(mon); }
int main(int argc, char *argv[]) { struct udev *udev; // struct udev_enumerate *enumerate; // struct udev_list_entry *devices, *device; udev_monitor = NULL; struct udev_device *udev_device; const char *action; udev = udev_new(); if (!udev) { printf("udev_new failed\n"); return 0; } udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); if (!udev_monitor) { printf("udev_monitor_new_from_netlink failed\n"); return 0; } udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input", NULL); if (udev_monitor_enable_receiving(udev_monitor)) { printf("config/udev: failed to bind the udev monitor\n"); return 0; } int udev_fd = udev_monitor_get_fd(udev_monitor); select_for_read(udev_fd); udev_device = udev_monitor_receive_device(udev_monitor); if (!udev_device) { printf("udev_monitor_receive_device failed\n"); return 0; } action = udev_device_get_action(udev_device); printf("udev action %s\n", action); if (action) { if (!strcmp(action, "add") || !strcmp(action, "change")) { printf("device_removed(udev_device); device_added(udev_device);\n"); } else if (!strcmp(action, "remove")) { printf("device_removed(udev_device);\n"); } } udev_device_unref(udev_device); return 0; }
void GamepadUpdate(void) { if (MON != NULL) { fd_set r; struct timeval tv; int fd = udev_monitor_get_fd(MON); /* set up a poll on the udev device */ FD_ZERO(&r); FD_SET(fd, &r); tv.tv_sec = 0; tv.tv_usec = 0; select(fd + 1, &r, 0, 0, &tv); /* test if we have a device change */ if (FD_ISSET(fd, &r)) { struct udev_device* dev = udev_monitor_receive_device(MON); if (dev) { const char* devNode = udev_device_get_devnode(dev); const char* sysPath = udev_device_get_syspath(dev); const char* action = udev_device_get_action(dev); sysPath = udev_device_get_syspath(dev); action = udev_device_get_action(dev); if (strstr(sysPath, "/js") != 0) { if (strcmp(action, "remove") == 0) { GamepadRemoveDevice(devNode); } else if (strcmp(action, "add") == 0) { GamepadAddDevice(devNode); } } udev_device_unref(dev); } } } GamepadUpdateCommon(); }
bool UdevSubsystem::on_udev_data(GIOChannel* channel, GIOCondition condition) { if (condition == G_IO_OUT) { log_error("data can be written"); } else if (condition == G_IO_PRI) { log_error("data can be read"); } else if (condition == G_IO_ERR) { log_error("data error"); } else if (condition != G_IO_IN) { log_error("unknown condition: " << condition); } else { log_info("trying to read data from udev"); log_info("trying to read data from udev monitor"); struct udev_device* device = udev_monitor_receive_device(m_monitor); log_info("got data from udev monitor"); if (!device) { // seem to be normal, do we get this when the given device is filtered out? log_debug("udev device couldn't be read: " << device); } else { const char* action = udev_device_get_action(device); if (g_logger.get_log_level() >= Logger::kDebug) { print_info(device); } if (action && strcmp(action, "add") == 0) { m_process_match_cb(device); } udev_device_unref(device); } } return true; }
int usbmain(){ // Load the uinput module (if it's not loaded already) if(system("modprobe uinput") != 0) ckb_warn("Failed to load uinput module\n"); // Create the udev object if(!(udev = udev_new())){ ckb_fatal("Failed to initialize udev\n"); return -1; } // Enumerate all currently connected devices udev_enum(); // Done scanning. Enter a loop to poll for device updates struct udev_monitor* monitor = udev_monitor_new_from_netlink(udev, "udev"); udev_monitor_filter_add_match_subsystem_devtype(monitor, "usb", 0); udev_monitor_enable_receiving(monitor); // Get an fd for the monitor int fd = udev_monitor_get_fd(monitor); fd_set fds; while(udev){ FD_ZERO(&fds); FD_SET(fd, &fds); // Block until an event is read if(select(fd + 1, &fds, 0, 0, 0) > 0 && FD_ISSET(fd, &fds)){ struct udev_device* dev = udev_monitor_receive_device(monitor); if(!dev) continue; const char* action = udev_device_get_action(dev); if(!action){ udev_device_unref(dev); continue; } // Add/remove device if(!strcmp(action, "add")){ int res = usb_add_device(dev); if(res == 0) continue; // If the device matched but the handle wasn't opened correctly, re-enumerate (this sometimes solves the problem) if(res == -1) udev_enum(); } else if(!strcmp(action, "remove")) usb_rm_device(dev); udev_device_unref(dev); } } udev_monitor_unref(monitor); return 0; }
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++; }
static int manager_udev_fn(sd_event_source *source, int fd, uint32_t mask, void *data) { _cleanup_udev_device_ struct udev_device *d = NULL; struct manager *m = data; const char *action, *ifname; unsigned int ifindex; struct link *l; d = udev_monitor_receive_device(m->udev_mon); if (!d) return 0; ifindex = ifindex_from_udev_device(d); if (!ifindex) return 0; l = manager_find_link(m, ifindex); action = udev_device_get_action(d); if (action && !strcmp(action, "remove")) { if (l) link_free(l); } else if (l) { ifname = udev_device_get_property_value(d, "INTERFACE"); if (action && !strcmp(action, "move")) { if (ifname) link_renamed(l, ifname); } #ifdef RELY_UDEV if (udev_device_has_tag(d, "miracle") && !lazy_managed) link_set_managed(l, true); else link_set_managed(l, false); #else if ((!interface_name || !strcmp(interface_name, ifname)) && !lazy_managed) { link_set_managed(l, true); } else { log_debug("ignored device: %s", ifname); } #endif } else { manager_add_udev_link(m, d); } return 0; }
void QDeviceDiscovery::handleUDevNotification() { if (!m_udevMonitor) return; struct udev_device *dev; QString devNode; dev = udev_monitor_receive_device(m_udevMonitor); if (!dev) goto cleanup; const char *action; action = udev_device_get_action(dev); if (!action) goto cleanup; const char *str; str = udev_device_get_devnode(dev); if (!str) goto cleanup; const char *subsystem; devNode = QString::fromUtf8(str); if (devNode.startsWith(QLatin1String(QT_EVDEV_DEVICE))) subsystem = "input"; else if (devNode.startsWith(QLatin1String(QT_DRM_DEVICE))) subsystem = "drm"; else goto cleanup; // if we cannot determine a type, walk up the device tree if (!checkDeviceType(dev)) { // does not increase the refcount dev = udev_device_get_parent_with_subsystem_devtype(dev, subsystem, 0); if (!dev) goto cleanup; if (!checkDeviceType(dev)) goto cleanup; } if (qstrcmp(action, "add") == 0) emit deviceDetected(devNode); if (qstrcmp(action, "remove") == 0) emit deviceRemoved(devNode); cleanup: udev_device_unref(dev); }
int main () { udev = udev_new(); if(!udev) { fprintf(stderr, "Can't create udev.\n"); exit(EXIT_FAILURE); } mon = udev_monitor_new_from_netlink(udev, "udev"); udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL); udev_monitor_enable_receiving(mon); while(1) { if (mon != NULL) FD_SET(udev_monitor_get_fd(mon), &readfds); select(udev_monitor_get_fd(mon) + 1, &readfds, NULL, NULL, NULL); if ((mon != NULL) && FD_ISSET(udev_monitor_get_fd(mon), &readfds)) { dev = udev_monitor_receive_device(mon); if(dev) { device = (char *) udev_device_get_sysname(dev); } } devnum = udev_device_get_devnum(dev); major = major(devnum); minor = minor(devnum); action = udev_device_get_action(dev); printf("Processing device %s %d:%d\n", action, major, minor); struct udev_list_entry * props = udev_device_get_properties_list_entry(dev); while(props != NULL) { printf("%s = %s\n", udev_list_entry_get_name(props), udev_list_entry_get_value(props)); props = udev_list_entry_get_next (props); } props = udev_device_get_sysattr_list_entry(dev); while(props != NULL) { printf("%s = %s\n", udev_list_entry_get_name(props), udev_device_get_sysattr_value(dev, udev_list_entry_get_name(props))); props = udev_list_entry_get_next (props); } } return 0; }
bool CUDevProvider::PumpDriveChangeEvents(IStorageEventsCallback *callback) { bool changed = false; fd_set readfds; FD_ZERO(&readfds); FD_SET(udev_monitor_get_fd(m_udevMon), &readfds); // non-blocking, check the file descriptor for received data struct timeval tv = {0}; int count = select(udev_monitor_get_fd(m_udevMon) + 1, &readfds, NULL, NULL, &tv); if (count < 0) return false; if (FD_ISSET(udev_monitor_get_fd(m_udevMon), &readfds)) { struct udev_device *dev = udev_monitor_receive_device(m_udevMon); if (!dev) return false; const char *action = udev_device_get_action(dev); const char *devtype = udev_device_get_devtype(dev); if (action) { const char *label = udev_device_get_property_value(dev, "ID_FS_LABEL"); const char *mountpoint = get_mountpoint(udev_device_get_devnode(dev)); if (!label) label = URIUtils::GetFileName(mountpoint); if (!strcmp(action, "add") && !strcmp(devtype, "partition")) { CLog::Log(LOGNOTICE, "UDev: Added %s", mountpoint); if (callback) callback->OnStorageAdded(label, mountpoint); changed = true; } if (!strcmp(action, "remove") && !strcmp(devtype, "partition")) { CLog::Log(LOGNOTICE, "UDev: Removed %s", mountpoint); if (callback) callback->OnStorageSafelyRemoved(label); changed = true; } } udev_device_unref(dev); } return changed; }
static int udev_handle(int fd) { struct udev_device *dev = udev_monitor_receive_device(udev_mon); const char *action = udev_device_get_action(dev); if (action != NULL) { if (autoadd && strcmp("add", action) == 0) { const char *devnode = udev_device_get_devnode(dev); int id; if (devnode != NULL && sscanf(devnode, "/dev/input/event%d", &id) == 1) { evdev_create(devnode, defaultMapfile); } } udev_device_unref(dev); } return LOOP_OK; }
static int node_fixup(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid) { struct udev *udev = udev_device_get_udev(dev); const char *devnode = udev_device_get_devnode(dev); dev_t devnum = udev_device_get_devnum(dev); struct stat stats; int err = 0; if (strcmp(udev_device_get_subsystem(dev), "block") == 0) mode |= S_IFBLK; else mode |= S_IFCHR; if (lstat(devnode, &stats) != 0) { err = -errno; info(udev, "can not stat() node '%s' (%m)\n", devnode); goto out; } if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) { err = -EEXIST; info(udev, "found node '%s' with non-matching devnum %s, skip handling\n", udev_device_get_devnode(dev), udev_device_get_id_filename(dev)); goto out; } if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) { info(udev, "set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid); chmod(devnode, mode); chown(devnode, uid, gid); } else { info(udev, "preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid); } /* * Set initial selinux file context only on add events. * We set the proper context on bootup (triger) or for newly * added devices, but we don't change it later, in case * something else has set a custom context in the meantime. */ if (strcmp(udev_device_get_action(dev), "add") == 0) udev_selinux_lsetfilecon(udev, devnode, mode); /* always update timestamp when we re-use the node, like on media change events */ utimensat(AT_FDCWD, devnode, NULL, 0); out: return err; }
static gboolean monitor_watch(GIOChannel *source, GIOCondition condition, gpointer data) { struct udev_device *udevice; udevice = udev_monitor_receive_device(monitor); if (!udevice) return TRUE; if (!g_strcmp0(udev_device_get_action(udevice), "add")) device_added(udevice); udev_device_unref(udevice); return TRUE; }