Exemplo n.º 1
0
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;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
UdevMonitor::UdevMonitor(udev_monitor* sMonitor)
	:monitor(sMonitor),fd(-1),listening(false)
	{
	/* Get the monitor's file descriptor: */
	if(monitor!=0)
		fd=udev_monitor_get_fd(monitor);
	}
Exemplo n.º 4
0
GamepadController::GamepadController()
    : m_gamepadDevices(Nix::Gamepads::itemsLengthCap)
{
    m_udev = udev_new();
    m_gamepadsMonitor = udev_monitor_new_from_netlink(m_udev, "udev");

    udev_monitor_enable_receiving(m_gamepadsMonitor);
    udev_monitor_filter_add_match_subsystem_devtype(m_gamepadsMonitor, "input", 0);

    GIOChannel *channel = g_io_channel_unix_new(udev_monitor_get_fd(m_gamepadsMonitor));
    g_io_add_watch(channel, GIOCondition(G_IO_IN), static_cast<GIOFunc>(&GamepadController::onGamepadChange), this);
    g_io_channel_unref(channel);

    struct udev_enumerate* enumerate = udev_enumerate_new(m_udev);
    udev_enumerate_add_match_subsystem(enumerate, "input");
    udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
    udev_enumerate_scan_devices(enumerate);
    struct udev_list_entry* cur;
    struct udev_list_entry* devs = udev_enumerate_get_list_entry(enumerate);
    udev_list_entry_foreach(cur, devs)
    {
        const char* devname = udev_list_entry_get_name(cur);
        struct udev_device* device = udev_device_new_from_syspath(m_udev, devname);
        if (isGamepadDevice(device))
            registerDevice(udev_device_get_devnode(device));
        udev_device_unref(device);
    }
    udev_enumerate_unref(enumerate);
}
Exemplo n.º 5
0
Arquivo: udev.c Projeto: etix/vlc
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;
}
Exemplo n.º 6
0
int linux_udev_start_event_monitor(void)
{
	int r;

	if (NULL == udev_ctx) {
		udev_ctx = udev_new();
		if (!udev_ctx) {
			return LIBUSB_ERROR_OTHER;
		}
	}

        udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
        if (!udev_monitor) {
                usbi_err(NULL, "could not initialize udev monitor");
                return LIBUSB_ERROR_OTHER;
        }

        r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
        if (r) {
                usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
                return LIBUSB_ERROR_OTHER;
        }

        if (udev_monitor_enable_receiving(udev_monitor)) {
                usbi_err(NULL, "failed to enable the udev monitor");
                return LIBUSB_ERROR_OTHER;
        }

        udev_monitor_fd = udev_monitor_get_fd(udev_monitor);

        pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);

        return LIBUSB_SUCCESS;
}
Exemplo n.º 7
0
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 );
}
Exemplo n.º 8
0
UdevNotifier::UdevNotifier(QString const & subsystem, QObject * parent/* = nullptr*/)
    : QObject(parent)
    , d(new Impl)
{
    d->udev = udev_new();
    d->monitor = udev_monitor_new_from_netlink(d->udev, "udev");
    if (nullptr == d->monitor)
    {
        qWarning() << QStringLiteral("UdevNotifier: unable to initialize udev_monitor, monitoring will be disabled");
        return;
    }

    int ret = udev_monitor_filter_add_match_subsystem_devtype(d->monitor, subsystem.toUtf8().constData(), nullptr);
    if (0 != ret)
        qWarning() << QStringLiteral("UdevNotifier: unable to add match subsystem, monitor will receive all devices");

    ret = udev_monitor_enable_receiving(d->monitor);
    if (0 != ret)
    {
        qWarning() << QStringLiteral("UdevNotifier: unable to enable receiving(%1), monitoring will be disabled").arg(ret);
        return;
    }

    d->notifier.reset(new QSocketNotifier(udev_monitor_get_fd(d->monitor), QSocketNotifier::Read));
    connect(d->notifier.data(), &QSocketNotifier::activated, this, &UdevNotifier::eventReady);
    d->notifier->setEnabled(true);
}
void QNetworkInfoPrivate::connectNotify(const QMetaMethod &signal)
{
#if !defined(QT_NO_OFONO)
    if (QOfonoWrapper::isOfonoAvailable()) {
        if (!ofonoWrapper)
            ofonoWrapper = new QOfonoWrapper(this);
        QMetaMethod sourceSignal = proxyToSourceSignal(signal, ofonoWrapper);
        connect(ofonoWrapper, sourceSignal, this, signal, Qt::UniqueConnection);
    }
#endif

    static const QMetaMethod currentNetworkModeChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::currentNetworkModeChanged);
    static const QMetaMethod networkNameChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkNameChanged);
    static const QMetaMethod networkSignalStrengthChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkSignalStrengthChanged);
    static const QMetaMethod networkStatusChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkStatusChanged);

    //    we always monitor "networkInterfaceCount" , as long as users connect any signals,
    //    with update to date network interface counts, we can compute network mode, strength,
    //    status, name properties in an efficient way
    if (!watchNetworkInterfaceCount) {
        QList<QNetworkInfo::NetworkMode> modes;
        modes << QNetworkInfo::WlanMode << QNetworkInfo::EthernetMode << QNetworkInfo::BluetoothMode;
        networkInterfaceCounts.clear();
        foreach (QNetworkInfo::NetworkMode mode, modes)
            networkInterfaceCounts[mode] = getNetworkInterfaceCount(mode);
#if !defined(QT_NO_UDEV)
        if (!udevHandle) {
            udevHandle = udev_new();
            udevMonitor = udev_monitor_new_from_netlink(udevHandle, "udev");
            udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, "net", NULL);
            udev_monitor_enable_receiving(udevMonitor);
            udevNotifier = new QSocketNotifier(udev_monitor_get_fd(udevMonitor), QSocketNotifier::Read, this);
            connect(udevNotifier, SIGNAL(activated(int)), this, SLOT(onUdevChanged()));
        }
Exemplo n.º 10
0
QDeviceDiscovery::QDeviceDiscovery(QDeviceTypes types, struct udev *udev, QObject *parent) :
    QObject(parent),
    m_types(types), m_udev(udev), m_udevMonitor(0), m_udevMonitorFileDescriptor(-1), m_udevSocketNotifier(0)
{
#ifdef QT_QPA_DEVICE_DISCOVERY_DEBUG
    qWarning() << "New UDeviceHelper created for type" << types;
#endif

    if (!m_udev)
        return;

    m_udevMonitor = udev_monitor_new_from_netlink(m_udev, "udev");
    if (!m_udevMonitor) {
#ifdef QT_QPA_DEVICE_DISCOVERY_DEBUG
        qWarning("Unable to create an Udev monitor. No devices can be detected.");
#endif
        return;
    }

    udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitor, "input", 0);
    udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitor, "drm", 0);
    udev_monitor_enable_receiving(m_udevMonitor);
    m_udevMonitorFileDescriptor = udev_monitor_get_fd(m_udevMonitor);

    m_udevSocketNotifier = new QSocketNotifier(m_udevMonitorFileDescriptor, QSocketNotifier::Read, this);
    connect(m_udevSocketNotifier, SIGNAL(activated(int)), this, SLOT(handleUDevNotification()));
}
Exemplo n.º 11
0
static int sixaxis_init(void)
{
	GIOChannel *channel;

	DBG("");

	ctx = udev_new();
	if (!ctx)
		return -EIO;

	monitor = udev_monitor_new_from_netlink(ctx, "udev");
	if (!monitor) {
		udev_unref(ctx);
		ctx = NULL;

		return -EIO;
	}

	/* Listen for newly connected hidraw interfaces */
	udev_monitor_filter_add_match_subsystem_devtype(monitor, "hidraw",
									NULL);
	udev_monitor_enable_receiving(monitor);

	channel = g_io_channel_unix_new(udev_monitor_get_fd(monitor));
	watch_id = g_io_add_watch(channel, G_IO_IN, monitor_watch, NULL);
	g_io_channel_unref(channel);

	return 0;
}
Exemplo n.º 12
0
int linux_udev_start_event_monitor(void)
{
	int r;

	assert(udev_ctx == NULL);
	udev_ctx = udev_new();
	if (!udev_ctx) {
		usbi_err(NULL, "could not create udev context");
		return LIBUSB_ERROR_OTHER;
	}

	udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
	if (!udev_monitor) {
		usbi_err(NULL, "could not initialize udev monitor");
		goto err_free_ctx;
	}

	r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
	if (r) {
		usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
		goto err_free_monitor;
	}

	if (udev_monitor_enable_receiving(udev_monitor)) {
		usbi_err(NULL, "failed to enable the udev monitor");
		goto err_free_monitor;
	}

	udev_monitor_fd = udev_monitor_get_fd(udev_monitor);

	/* Some older versions of udev are not non-blocking by default,
	 * so make sure this is set */
	r = fcntl(udev_monitor_fd, F_GETFL);
	if (r == -1) {
		usbi_err(NULL, "getting udev monitor fd flags (%d)", errno);
		goto err_free_monitor;
	}
	r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
	if (r) {
		usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
		goto err_free_monitor;
	}

	r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
	if (r) {
		usbi_err(NULL, "creating hotplug event thread (%d)", r);
		goto err_free_monitor;
	}

	return LIBUSB_SUCCESS;

err_free_monitor:
	udev_monitor_unref(udev_monitor);
	udev_monitor = NULL;
	udev_monitor_fd = -1;
err_free_ctx:
	udev_unref(udev_ctx);
	udev_ctx = NULL;
	return LIBUSB_ERROR_OTHER;
}
Exemplo n.º 13
0
rw_status_t
rw_udev_register_cb(rw_udev_handle_t *handle,
                    void *userdata, rw_udev_cb_t cb)
{
  struct udev_enumerate *enumerate = NULL;
  struct udev_list_entry *devices, *dev_list_entry;
  int ret = -1;
  rw_status_t status = RW_STATUS_SUCCESS;
  rw_pci_address_t pci_addr;
  rw_pci_device_t *dev;
  struct udev_device *udevice;
  
  handle->cb = cb;
  handle->userdata = userdata;
  
  //walk throight he list and call the callback..
  enumerate = udev_enumerate_new(handle->udev);
  if (!enumerate){
    goto free_and_ret;
  }
  udev_enumerate_add_match_subsystem(enumerate, "net");
  udev_enumerate_scan_devices(enumerate);
  devices = udev_enumerate_get_list_entry(enumerate);
  udev_list_entry_foreach(dev_list_entry, devices) {
    const char *path;
    path = udev_list_entry_get_name(dev_list_entry);
    ret = rw_sys_populate_pci_from_path(path, &pci_addr);
    if (!ret){
      
    }else{
      udevice = udev_device_new_from_syspath(handle->udev, path);
      dev = rw_udev_insert_device(handle, &pci_addr);
      rw_udev_update_device(udevice, dev);
      if (dev){
        handle->cb(handle->userdata, dev, RW_UDEV_DEVICE_ADD);
      }
      udev_device_unref(udevice);
    }
  }
  
  handle->mon = udev_monitor_new_from_netlink(handle->udev, "udev");
  udev_monitor_filter_add_match_subsystem_devtype(handle->mon, "net", NULL);
  udev_monitor_enable_receiving(handle->mon);
  /* Get the file descriptor (fd) for the monitor.
     This fd will get passed to select() */
  handle->fd = udev_monitor_get_fd(handle->mon);

  
ret:
  return status;
  
free_and_ret:
  if (enumerate){
    udev_enumerate_unref(enumerate);
    enumerate = NULL;
  }
  status = RW_STATUS_FAILURE;
  goto ret;
}
Exemplo n.º 14
0
int udev_handle_get_fd(struct udev_handle *uh)
{
	int fd = -1;
	if (!uh || !uh->udev_ctx || !uh->udev_monitor)
		return -1;
	fd = udev_monitor_get_fd(uh->udev_monitor);
	return fd;
}
Exemplo n.º 15
0
void input_init(char* mapfile) {
  #ifdef HAVE_LIBCEC
  init_cec();
  #endif

  udev = udev_new();
  if (!udev) {
    fprintf(stderr, "Can't create udev\n");
    exit(1);
  }

  autoadd = (numDevices == 0);
  if (autoadd) {
    struct udev_enumerate *enumerate = udev_enumerate_new(udev);
    udev_enumerate_add_match_subsystem(enumerate, "input");
    udev_enumerate_scan_devices(enumerate);
    struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);

    struct udev_list_entry *dev_list_entry;
    udev_list_entry_foreach(dev_list_entry, devices) {
      const char *path = udev_list_entry_get_name(dev_list_entry);
      struct udev_device *dev = udev_device_new_from_syspath(udev, path);
      const char *devnode = udev_device_get_devnode(dev);
      int id;
      if (devnode != NULL && sscanf(devnode, "/dev/input/event%d", &id) == 1) {
        input_create(devnode, mapfile);
      }
      udev_device_unref(dev);
    }

    udev_enumerate_unref(enumerate);
  }

  udev_mon = udev_monitor_new_from_netlink(udev, "udev");
  udev_monitor_filter_add_match_subsystem_devtype(udev_mon, "input", NULL);
  udev_monitor_enable_receiving(udev_mon);

  udev_fdindex = numFds++;
  sig_fdindex = numFds++;

  if (fds == NULL)
    fds = malloc(sizeof(struct pollfd)*numFds);
  else
    fds = realloc(fds, sizeof(struct pollfd)*numFds);

  if (fds == NULL) {
    fprintf(stderr, "Not enough memory\n");
    exit(EXIT_FAILURE);
  }

  defaultMapfile = mapfile;
  fds[udev_fdindex].fd = udev_monitor_get_fd(udev_mon);
  fds[udev_fdindex].events = POLLIN;

  main_thread_id = pthread_self();
}
Exemplo n.º 16
0
bool udev_hotplug_available(void *dev)
{
   struct pollfd fds;

   fds.fd      = udev_monitor_get_fd((struct udev_monitor*)dev);
   fds.events  = POLLIN;
   fds.revents = 0;

   return (poll(&fds, 1, 0) == 1) && (fds.revents & POLLIN);
}
Exemplo n.º 17
0
static bool udev_input_hotplug_available(udev_input_t *udev)
{
   struct pollfd fds = {0};

   if (!udev || !udev->monitor)
      return false;

   fds.fd     = udev_monitor_get_fd(udev->monitor);
   fds.events = POLLIN;

   return (poll(&fds, 1, 0) == 1) && (fds.revents & POLLIN);
}
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);
}
Exemplo n.º 19
0
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;
}
gboolean testdisplay_setup_hotplug(void)
{
	int ret;

	udev = udev_new();
	if (!udev) {
		igt_warn("failed to create udev object\n");
		goto out;
	}

	uevent_monitor = udev_monitor_new_from_netlink(udev, "udev");
	if (!uevent_monitor) {
		igt_warn("failed to create udev event monitor\n");
		goto out;
	}

	ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor,
							      "drm",
							      "drm_minor");
	if (ret < 0) {
		igt_warn("failed to filter for drm events\n");
		goto out;
	}

	ret = udev_monitor_enable_receiving(uevent_monitor);
	if (ret < 0) {
		igt_warn("failed to enable udev event reception\n");
		goto out;
	}

	udevchannel =
		g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor));
	if (!udevchannel) {
		igt_warn("failed to create udev GIO channel\n");
		goto out;
	}

	ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event,
			     udev);
	if (ret < 0) {
		igt_warn("failed to add watch on udev GIO channel\n");
		goto out;
	}

	return TRUE;

out:
	testdisplay_cleanup_hotplug();
	return FALSE;
}
Exemplo n.º 21
0
Arquivo: udev.c Projeto: sylware/lwl
void lwl_udev_init(i ep_fd)
{
  lwl_udev=udev_new();
  if(!lwl_udev){
    PERRC("fatal:unable to init udev context\n");
    exit(LWL_ERR);
  }

  //get events *after* udev rule engine processing
  monitor=udev_monitor_new_from_netlink(lwl_udev,"udev");
  if(!monitor){
    PERRC("fatal:unable to create an udev monitor\n");
    exit(LWL_ERR);
  }

  l r=(l)udev_monitor_filter_add_match_subsystem_devtype(monitor,INPUT_STR,0);
  if(r<0){
    PERRC("fatal:unable to add " INPUT_STR 
                                          " subsystem match to udev monitor\n");
    exit(LWL_ERR);
  }

  r=(l)udev_monitor_filter_add_match_subsystem_devtype(monitor,DCE6_DISPLAY_STR,
                                                                             0);
  if(r<0){
    PERRC("fatal:unable to add " DCE6_DISPLAY_STR
                                          " subsystem match to udev monitor\n");
    exit(LWL_ERR);
  }

  r=(l)udev_monitor_enable_receiving(monitor);
  if(r<0){
    PERRC("fatal:unable to enable receiving of udev monitor events\n");
    exit(LWL_ERR);
  }

  i monitor_fd=udev_monitor_get_fd(monitor);

  struct epoll_event ep_evt;
  memset(&ep_evt,0,sizeof(ep_evt));
  ep_evt.events=EPOLLIN|EPOLLHUP;//*level* triggered
  ep_evt.data._32=EPOLL_UDEV;

  r=epoll_ctl(ep_fd,EPOLL_CTL_ADD,monitor_fd,&ep_evt);
  if(ISERR(r)){
    PERR("fatal(%ld):unable to register udev monitor socket(%d) to epoll\n",r,
                                                                    monitor_fd);
    exit(LWL_ERR);
  }
}
Exemplo n.º 22
0
Monitor::Monitor(Root const &udev, char const *subsystem, char const *devtype)
    : fd_(-1)
    , h_([&udev, subsystem, devtype, this]() {
            auto h = udev.mk_monitor("udev");
            if (!h)
                throw cor::Error("Can't create udev monitor");
            udev_monitor_filter_add_match_subsystem_devtype
                (h.get(), subsystem, devtype);
            udev_monitor_enable_receiving(h_.get());
            fd_ = udev_monitor_get_fd(h_.get());
            return h;
        }())
{
}
Exemplo n.º 23
0
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;
}
Exemplo n.º 24
0
static int
udev_open(struct sol_flow_node *node, void *data, const struct sol_flow_node_options *options)
{
    struct udev_data *mdata = data;
    struct udev_device *device;
    bool value;
    const struct sol_flow_node_type_udev_boolean_options *opts =
        (const struct sol_flow_node_type_udev_boolean_options *)options;

    SOL_FLOW_NODE_OPTIONS_SUB_API_CHECK(options,
        SOL_FLOW_NODE_TYPE_UDEV_BOOLEAN_OPTIONS_API_VERSION, -EINVAL);

    mdata->udev = udev_new();
    SOL_NULL_CHECK(mdata->udev, -EINVAL);

    mdata->monitor = udev_monitor_new_from_netlink(mdata->udev, "udev");
    if (!mdata->monitor) {
        SOL_WRN("Fail on create the udev monitor");
        goto monitor_error;
    }

    if (udev_monitor_enable_receiving(mdata->monitor) < 0) {
        SOL_WRN("error: unable to subscribe to udev events");
        goto receive_error;
    }

    mdata->addr = strdup(opts->address);

    mdata->node = node;
    mdata->watch = sol_fd_add(udev_monitor_get_fd(mdata->monitor),
        SOL_FD_FLAGS_IN | SOL_FD_FLAGS_ERR | SOL_FD_FLAGS_HUP,
        _on_event, mdata);

    device = udev_device_new_from_syspath(mdata->udev, mdata->addr);
    if (device) {
        value = true;
        udev_device_unref(device);
    } else {
        value = false;
    }

    return sol_flow_send_boolean_packet(node,
        SOL_FLOW_NODE_TYPE_UDEV_BOOLEAN__OUT__OUT, value);

receive_error:
    mdata->monitor = udev_monitor_unref(mdata->monitor);
monitor_error:
    mdata->udev = udev_unref(mdata->udev);
    return -EINVAL;
}
Exemplo n.º 25
0
void CALSADeviceMonitor::Start()
{
  int err;

  if (!m_udev)
  {
    m_udev = udev_new();
    if (!m_udev)
    {
      CLog::Log(LOGWARNING, "CALSADeviceMonitor::Start - Unable to open udev handle");
      return;
    }

    m_udevMonitor = udev_monitor_new_from_netlink(m_udev, "udev");
    if (!m_udevMonitor)
    {
      CLog::Log(LOGERROR, "CALSADeviceMonitor::Start - udev_monitor_new_from_netlink() failed");
      goto err_unref_udev;
    }

    err = udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitor, "sound", NULL);
    if (err)
    {
      CLog::Log(LOGERROR, "CALSADeviceMonitor::Start - udev_monitor_filter_add_match_subsystem_devtype() failed");
      goto err_unref_monitor;
    }

    err = udev_monitor_enable_receiving(m_udevMonitor);
    if (err)
    {
      CLog::Log(LOGERROR, "CALSADeviceMonitor::Start - udev_monitor_enable_receiving() failed");
      goto err_unref_monitor;
    }

    g_fdEventMonitor.AddFD(
        CFDEventMonitor::MonitoredFD(udev_monitor_get_fd(m_udevMonitor),
                                     POLLIN, FDEventCallback, m_udevMonitor),
        m_fdMonitorId);
  }

  return;

err_unref_monitor:
  udev_monitor_unref(m_udevMonitor);
  m_udevMonitor = NULL;
err_unref_udev:
  udev_unref(m_udev);
  m_udev = NULL;
}
Exemplo n.º 26
0
PowerExcessHandler::PowerExcessHandler(QObject *parent):
  QObject(parent), d_ptr(new PowerExcessHandlerPrivate) {
    Q_D(PowerExcessHandler);

    d->udevHandle = udev_new();
    d->udevMonitor = udev_monitor_new_from_netlink(d->udevHandle, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(d->udevMonitor, "misc", 0);
    udev_monitor_enable_receiving(d->udevMonitor);

    d->udevSocketNotifier =
            new QSocketNotifier(udev_monitor_get_fd(d->udevMonitor),
                    QSocketNotifier::Read, this);
    connect(d->udevSocketNotifier, SIGNAL(activated(int)),
            this, SLOT(handleUdevNotification()));
}
Exemplo n.º 27
0
/*
 * Initiate the device list (with udev monitoring)
 * args:
 *   none
 * 
 * asserts:
 *   none
 * 
 * returns: none
 */ 
void v4l2core_init_device_list()
{
	/* Create a udev object */
	my_device_list.udev = udev_new();
	/*start udev device monitoring*/
	/* Set up a monitor to monitor v4l2 devices */
	if(my_device_list.udev)
	{
		my_device_list.udev_mon = udev_monitor_new_from_netlink(my_device_list.udev, "udev");
		udev_monitor_filter_add_match_subsystem_devtype(my_device_list.udev_mon, "video4linux", NULL);
		udev_monitor_enable_receiving(my_device_list.udev_mon);
		/* Get the file descriptor (fd) for the monitor */
		my_device_list.udev_fd = udev_monitor_get_fd(my_device_list.udev_mon);

		enum_v4l2_devices();
	}
} 
Exemplo n.º 28
0
Storage::Storage()
    : privatePartition(NULL)
    , publicPartition(NULL)
    , removablePartition(NULL)
    , dataPartition(NULL)
    , usb_active(false)
{
        qDebug() << Q_FUNC_INFO;
        _partitions = QMap<QString, StoragePartition*>();
        _devices = QMap<QString, StorageDevice*>();

        udev = udev_new();
        if (!udev) {
                qDebug() << Q_FUNC_INFO << "Cannot create udev";
                return;
        }

        enumerate_mmcblk_devices(udev);

        udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
        if (!udev_monitor) {
                qDebug() << Q_FUNC_INFO << "Cannot create mon";
                return;
        }

        if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) != 0) {
                qDebug() << Q_FUNC_INFO << "Error with udev_monitor_filter_add_match_subsystem_devtype";
                return;
        }

        udev_monitor_set_receive_buffer_size(udev_monitor, UDEV_MONITOR_BUFFER_SIZE_STORAGE);

        if (udev_monitor_enable_receiving(udev_monitor) < 0) {
                qDebug() << Q_FUNC_INFO << "Error, cannot receive from kernel";
                return;
        }
        int fd = udev_monitor_get_fd(udev_monitor);
        if (fd <= 0) {
                qDebug() << Q_FUNC_INFO << "Error at udev_monitor_get_fd";
                return;
        }

        notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
        notifier->setEnabled(true);
        connect(notifier, SIGNAL(activated(int)), this, SLOT(checkDevice(int)));
}
Exemplo n.º 29
0
static int
udev_start(void)
{
	int fd;

	if (udev) {
		syslog(LOG_ERR, "udev: already started");
		return -1;
	}

	syslog(LOG_DEBUG, "udev: starting");
	udev = udev_new();
	if (udev == NULL) {
		syslog(LOG_ERR, "udev_new: %m");
		return -1;
	}
	monitor = udev_monitor_new_from_netlink(udev, "udev");
	if (monitor == NULL) {
		syslog(LOG_ERR, "udev_monitor_new_from_netlink: %m");
		goto bad;
	}
#ifndef LIBUDEV_NOFILTER
	if (udev_monitor_filter_add_match_subsystem_devtype(monitor,
	    "net", NULL) != 0)
	{
		syslog(LOG_ERR,
		    "udev_monitor_filter_add_match_subsystem_devtype: %m");
		goto bad;
	}
#endif
	if (udev_monitor_enable_receiving(monitor) != 0) {
		syslog(LOG_ERR, "udev_monitor_enable_receiving: %m");
		goto bad;
	}
	fd = udev_monitor_get_fd(monitor);
	if (fd == -1) {
		syslog(LOG_ERR, "udev_monitor_get_fd: %m");
		goto bad;
	}
	return fd;

bad:
	udev_stop();
	return -1;
}
Exemplo n.º 30
0
Status UdevEventPublisher::run() {
  int fd = 0;

  {
    WriteLock lock(mutex_);
    if (monitor_ == nullptr) {
      return Status(1);
    }
    fd = udev_monitor_get_fd(monitor_);
  }

  struct pollfd fds[1];
  fds[0].fd = fd;
  fds[0].events = POLLIN;

  int selector = ::poll(fds, 1, 1000);
  if (selector == -1 && errno != EINTR && errno != EAGAIN) {
    LOG(ERROR) << "Could not read udev monitor";
    return Status(1, "udev monitor failed.");
  }

  if (selector == 0 || !(fds[0].revents & POLLIN)) {
    // Read timeout.
    return Status(0, "Finished");
  }

  {
    WriteLock lock(mutex_);
    struct udev_device* device = udev_monitor_receive_device(monitor_);
    if (device == nullptr) {
      LOG(ERROR) << "udev monitor returned invalid device";
      return Status(1, "udev monitor failed.");
    }

    auto ec = createEventContextFrom(device);
    fire(ec);

    udev_device_unref(device);
  }

  pauseMilli(kUdevMLatency);
  return Status(0, "OK");
}