Example #1
0
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! */
}
Example #3
0
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;
}
Example #4
0
void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
{
        struct udev *udev = udev_device_get_udev(dev);
        struct udev_list_entry *list_entry;

        /* update possible left-over symlinks */
        udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
                const char *name = udev_list_entry_get_name(list_entry);
                struct udev_list_entry *list_entry_current;
                int found;

                /* check if old link name still belongs to this device */
                found = 0;
                udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
                        const char *name_current = udev_list_entry_get_name(list_entry_current);

                        if (strcmp(name, name_current) == 0) {
                                found = 1;
                                break;
                        }
                }
                if (found)
                        continue;

                info(udev, "update old name, '%s' no longer belonging to '%s'\n",
                     name, udev_device_get_devpath(dev));
                link_update(dev, name, 0);
        }
}
Example #5
0
static void print_record(struct udev_device *device) {
        const char *str;
        int i;
        struct udev_list_entry *list_entry;

        printf("P: %s\n", udev_device_get_devpath(device));

        str = udev_device_get_devnode(device);
        if (str != NULL)
                printf("N: %s\n", str + STRLEN("/dev/"));

        i = udev_device_get_devlink_priority(device);
        if (i != 0)
                printf("L: %i\n", i);

        udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device))
                printf("S: %s\n",
                       udev_list_entry_get_name(list_entry) + STRLEN("/dev/"));

        udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
                printf("E: %s=%s\n",
                       udev_list_entry_get_name(list_entry),
                       udev_list_entry_get_value(list_entry));
        printf("\n");
}
static void monitor_cb(
        pa_mainloop_api*a,
        pa_io_event* e,
        int fd,
        pa_io_event_flags_t events,
        void *userdata) {

    struct userdata *u = userdata;
    struct udev_device *dev;

    pa_assert(a);

    if (!(dev = udev_monitor_receive_device(u->monitor))) {
        pa_log("Failed to get udev device object from monitor.");
        goto fail;
    }

    if (!path_get_card_id(udev_device_get_devpath(dev))) {
        udev_device_unref(dev);
        return;
    }

    process_device(u, dev);
    udev_device_unref(dev);
    return;

fail:
    a->io_free(u->udev_io);
    u->udev_io = NULL;
}
Example #7
0
int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid, int test)
{
	struct udev *udev = udev_device_get_udev(dev);
	int i;
	int num;
	struct udev_list_entry *list_entry;
	int err = 0;

	info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
	     udev_device_get_devnode(dev),
	     major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
	     mode, uid, gid);

	util_create_path(udev, udev_device_get_devnode(dev));
	if (!test)
		if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
			err = -1;
			goto exit;
		}

	/* create all_partitions if requested */
	num = udev_device_get_num_fake_partitions(dev);
	if (num > 0) {
		info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
		if (!test) {
			for (i = 1; i <= num; i++) {
				char partitionname[UTIL_PATH_SIZE];
				dev_t part_devnum;

				snprintf(partitionname, sizeof(partitionname), "%s%d",
					 udev_device_get_devnode(dev), i);
				partitionname[sizeof(partitionname)-1] = '\0';
				part_devnum = makedev(major(udev_device_get_devnum(dev)),
						    minor(udev_device_get_devnum(dev)) + i);
				udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
			}
		}
	}

	/* add node to name index */
	name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 1, test);

	/* create/update symlinks, add symlinks to name index */
	udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
		name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 1, test);
		update_link(dev, udev_list_entry_get_name(list_entry), test);
	}
Example #8
0
void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test)
{
	struct udev *udev = udev_device_get_udev(dev);
	struct udev_list_entry *list_entry;
	const char *devnode_old;

	/* update possible left-over symlinks */
	udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
		const char *name = udev_list_entry_get_name(list_entry);
		struct udev_list_entry *list_entry_current;
		int found;

		/* check if old link name is now our node name */
		if (strcmp(name, udev_device_get_devnode(dev)) == 0)
			continue;

		/* check if old link name still belongs to this device */
		found = 0;
		udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
			const char *name_current = udev_list_entry_get_name(list_entry_current);

			if (strcmp(name, name_current) == 0) {
				found = 1;
				break;
			}
		}
		if (found)
			continue;

		info(udev, "update old symlink '%s' no longer belonging to '%s'\n", name, udev_device_get_devpath(dev));
		name_index(udev, udev_device_get_devpath(dev), name, 0, test);
		update_link(dev, name, test);
	}

	/*
	 * if the node name has changed, delete the node,
	 * and possibly restore a symlink of another device
	 */
	devnode_old = udev_device_get_devnode(dev_old);
	if (devnode_old != NULL) {
		const char *devnode = udev_device_get_devnode(dev);

		if (devnode != NULL && strcmp(devnode_old, devnode) != 0)
			update_link(dev, devnode_old, test);
	}
}
Example #9
0
static int print_device_chain(struct udev_device *device)
{
        struct udev_device *device_parent;
        const char *str;

        printf("\n"
               "Udevadm info starts with the device specified by the devpath and then\n"
               "walks up the chain of parent devices. It prints for every device\n"
               "found, all possible attributes in the udev rules key format.\n"
               "A rule to match, can be composed by the attributes of the device\n"
               "and the attributes from one single parent device.\n"
               "\n");

        printf("  looking at device '%s':\n", udev_device_get_devpath(device));
        printf("    KERNEL==\"%s\"\n", udev_device_get_sysname(device));
        str = udev_device_get_subsystem(device);
        if (str == NULL)
                str = "";
        printf("    SUBSYSTEM==\"%s\"\n", str);
        str = udev_device_get_driver(device);
        if (str == NULL)
                str = "";
        printf("    DRIVER==\"%s\"\n", str);
        print_all_attributes(device, "ATTR");

        device_parent = device;
        do {
                device_parent = udev_device_get_parent(device_parent);
                if (device_parent == NULL)
                        break;
                printf("  looking at parent device '%s':\n", udev_device_get_devpath(device_parent));
                printf("    KERNELS==\"%s\"\n", udev_device_get_sysname(device_parent));
                str = udev_device_get_subsystem(device_parent);
                if (str == NULL)
                        str = "";
                printf("    SUBSYSTEMS==\"%s\"\n", str);
                str = udev_device_get_driver(device_parent);
                if (str == NULL)
                        str = "";
                printf("    DRIVERS==\"%s\"\n", str);
                print_all_attributes(device_parent, "ATTRS");
        } while (device_parent != NULL);

        return 0;
}
Example #10
0
extern int udev_node_remove(struct udev_device *dev, int test)
{
	struct udev *udev = udev_device_get_udev(dev);
	struct udev_list_entry *list_entry;
	const char *devnode;
	char partitionname[UTIL_PATH_SIZE];
	struct stat stats;
	int err = 0;
	int num;

	/* remove node from name index */
	name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 0, test);

	/* remove,update symlinks, remove symlinks from name index */
	udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
		name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 0, test);
		update_link(dev, udev_list_entry_get_name(list_entry), test);
	}
static void card_changed(struct userdata *u, struct udev_device *dev) {
    struct device *d;
    const char *path;
    const char *t;
    char *n;

    pa_assert(u);
    pa_assert(dev);

    /* Maybe /dev/snd is now available? */
    setup_inotify(u);

    path = udev_device_get_devpath(dev);

    if ((d = pa_hashmap_get(u->devices, path))) {
        verify_access(u, d);
        return;
    }

    d = pa_xnew0(struct device, 1);
    d->path = pa_xstrdup(path);
    d->module = PA_INVALID_INDEX;
    PA_INIT_RATELIMIT(d->ratelimit, 10*PA_USEC_PER_SEC, 5);

    if (!(t = udev_device_get_property_value(dev, "PULSE_NAME")))
        if (!(t = udev_device_get_property_value(dev, "ID_ID")))
            if (!(t = udev_device_get_property_value(dev, "ID_PATH")))
                t = path_get_card_id(path);

    n = pa_namereg_make_valid_name(t);
    d->card_name = pa_sprintf_malloc("alsa_card.%s", n);
    d->args = pa_sprintf_malloc("device_id=\"%s\" "
                                "name=\"%s\" "
                                "card_name=\"%s\" "
                                "namereg_fail=false "
                                "tsched=%s "
                                "fixed_latency_range=%s "
                                "ignore_dB=%s "
                                "deferred_volume=%s "
                                "use_ucm=1 "
                                "card_properties=\"module-udev-detect.discovered=1\"",
                                path_get_card_id(path),
                                n,
                                d->card_name,
                                pa_yes_no(u->use_tsched),
                                pa_yes_no(u->fixed_latency_range),
                                pa_yes_no(u->ignore_dB),
                                pa_yes_no(u->deferred_volume));
    pa_xfree(n);

    pa_hashmap_put(u->devices, d->path, d);

    verify_access(u, d);
}
Example #12
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 void remove_card(struct userdata *u, struct udev_device *dev) {
    struct device *d;

    pa_assert(u);
    pa_assert(dev);

    if (!(d = pa_hashmap_remove(u->devices, udev_device_get_devpath(dev))))
        return;

    pa_log_info("Card %s removed.", d->path);

    if (d->module != PA_INVALID_INDEX)
        pa_module_unload_request_by_index(u->core, d->module, true);

    device_free(d);
}
/* manage "stack of names" with possibly specified device priorities */
static void link_update(struct udev_device *dev, const char *slink, bool add)
{
	struct udev *udev = udev_device_get_udev(dev);
	char name_enc[UTIL_PATH_SIZE];
	char filename[UTIL_PATH_SIZE * 2];
	char dirname[UTIL_PATH_SIZE];
	const char *target;
	char buf[UTIL_PATH_SIZE];

	dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));

	util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc));
	snprintf(dirname, sizeof(dirname), "%s/.udev/links/%s", udev_get_dev_path(udev), name_enc);
	snprintf(filename, sizeof(filename), "%s/%c%u:%u", dirname,
		 strcmp(udev_device_get_subsystem(dev), "block") == 0 ? 'b' : 'c',
		 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));

	if (!add) {
		dbg(udev, "removing index: '%s'\n", filename);
		unlink(filename);
		util_delete_path(udev, filename);
	}

	target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
	if (target == NULL) {
		info(udev, "no reference left, remove '%s'\n", slink);
		unlink(slink);
		util_delete_path(udev, slink);
	} else {
		info(udev, "creating link '%s' to '%s'\n", slink, target);
		node_symlink(udev, target, slink);
	}

	if (add) {
		int err;

		dbg(udev, "creating index: '%s'\n", filename);
		do {
			err = util_create_path(udev, filename);
			if (err != 0 && err != -ENOENT)
				break;
			err = symlink(udev_device_get_devpath(dev), filename);
			if (err != 0)
				err = -errno;
		} while (err == -ENOENT);
	}
}
Example #15
0
static void print_device(struct udev_device *device, const char *source, int prop) {
        struct timespec ts;

        assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
        printf("%-6s[%"PRI_TIME".%06"PRI_NSEC"] %-8s %s (%s)\n",
               source,
               ts.tv_sec, (nsec_t)ts.tv_nsec/1000,
               udev_device_get_action(device),
               udev_device_get_devpath(device),
               udev_device_get_subsystem(device));
        if (prop) {
                struct udev_list_entry *list_entry;

                udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
                        printf("%s=%s\n",
                               udev_list_entry_get_name(list_entry),
                               udev_list_entry_get_value(list_entry));
                printf("\n");
        }
}
static void print_device(struct udev_device *device, const char *source, int prop)
{
        struct timespec ts;

        clock_gettime(CLOCK_MONOTONIC, &ts);
        printf("%-6s[%llu.%06u] %-8s %s (%s)\n",
               source,
               (unsigned long long) ts.tv_sec, (unsigned int) ts.tv_nsec/1000,
               udev_device_get_action(device),
               udev_device_get_devpath(device),
               udev_device_get_subsystem(device));
        if (prop) {
                struct udev_list_entry *list_entry;

                udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
                        printf("%s=%s\n",
                               udev_list_entry_get_name(list_entry),
                               udev_list_entry_get_value(list_entry));
                printf("\n");
        }
}
Example #17
0
/*
 * Add 'legacy' device
 *
 * The term legacy is a bit misleading, but this adds devices according
 * to the original ofono model.
 *
 * - We cannot assume that these are USB devices
 * - The modem consists of only a single interface
 * - The device must have an OFONO_DRIVER property from udev
 */
static void add_serial_device(struct udev_device *dev)
{
	const char *syspath, *devpath, *devname, *devnode;
	struct modem_info *modem;
	struct serial_device_info *info;
	const char *subsystem;
	struct udev_device* mdev;
	const char* driver;

	mdev = get_serial_modem_device(dev);
	if (!mdev) {
		DBG("Device is missing required OFONO_DRIVER property");
		return;
	}

	driver = udev_device_get_property_value(mdev, "OFONO_DRIVER");

	syspath = udev_device_get_syspath(mdev);
	devname = udev_device_get_devnode(mdev);
	devpath = udev_device_get_devpath(mdev);

	devnode = udev_device_get_devnode(dev);

	if (!syspath || !devname || !devpath || !devnode)
		return;

	modem = g_hash_table_lookup(modem_list, syspath);
	if (modem == NULL) {
		modem = g_try_new0(struct modem_info, 1);
		if (modem == NULL)
			return;

		modem->type = MODEM_TYPE_SERIAL;
		modem->syspath = g_strdup(syspath);
		modem->devname = g_strdup(devname);
		modem->driver = g_strdup("legacy");

		g_hash_table_replace(modem_list, modem->syspath, modem);
	}
Example #18
0
void udev_watch_begin(struct udev *udev, struct udev_device *dev)
{
    char filename[UTIL_PATH_SIZE];
    int wd;

    if (inotify_fd < 0)
        return;

    info(udev, "adding watch on '%s'\n", udev_device_get_devnode(dev));
    wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
    if (wd < 0) {
        err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
            inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
    }

    snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);
    util_create_path(udev, filename);
    unlink(filename);
    symlink(udev_device_get_devpath(dev), filename);

    udev_device_set_watch_handle(dev, wd);
}
Example #19
0
void UdevNotifier::eventReady(int socket)
{
    struct udev_device * dev;
    while (nullptr != (dev = udev_monitor_receive_device(d->monitor)))
    {
        QString const action = udev_device_get_action(dev);
        QString const device = udev_device_get_devpath(dev);

        if (QStringLiteral("add") == action)
            emit deviceAdded(std::move(device));
        else if (QStringLiteral("remove") == action)
            emit deviceRemoved(std::move(device));
        else if (QStringLiteral("change") == action)
            emit deviceChanged(std::move(device));
        else if (QStringLiteral("online") == action)
            emit deviceOnline(std::move(device));
        else if (QStringLiteral("offline") == action)
            emit deviceOffline(std::move(device));

        udev_device_unref(dev);
    }
}
Example #20
0
static int print_device(struct udev_device *device, const char *f_action1, const char *f_action2, const char *f_devsuffix)
{
	struct timeval  tv;

    const  char *action  = udev_device_get_action(device);
    const  char *devpath = udev_device_get_devpath(device);
    size_t f_len, d_len;

	gettimeofday(&tv, NULL);
	printf("%lu.%06u %s %s\n",
	       (unsigned long) tv.tv_sec, (unsigned int) tv.tv_usec,
	       action, devpath);

    if (!strcmp(f_action1, action) || !strcmp(f_action2, action)) {
        f_len = strlen(f_devsuffix);
        d_len = strlen(devpath);

        if (d_len >= f_len && !strcmp(devpath+(d_len-f_len), f_devsuffix))
            return 1;
    }

    return 0;
}
void PowerExcessHandlerPrivate::handleUdevNotification() {
    udev_device *dev = udev_monitor_receive_device(udevMonitor);
    if (!dev) {
        qDebug() << "udev_monitor_receive_device() failed";
        return;
    }

    static const QString KEVENT_DEVPATH("/devices/virtual/misc/kevent");
    if (udev_device_get_devpath(dev) == KEVENT_DEVPATH) {
        const char *event = udev_device_get_property_value(dev, "EVENT");

        static const QString EVENT_WAKELOCK_DUMP("WakelockDump");
        static const QString EVENT_SUBSYSTEM("Subsystem");
        if (event == EVENT_WAKELOCK_DUMP || event == EVENT_SUBSYSTEM) {
            qDebug() << "Power excess detected, requesting dump of "
                    "system logs.";
            if (!CReporterUtils::invokeLogCollection(CReporter::PowerExcessPrefix)) {
                qDebug() << "Problem invoking rich-core-dumper.";
            }
        }
    }

    udev_device_unref(dev);
}
Example #22
0
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();
  }
}
/*
 * get the current latency timer value for ftdi devices.
 * return value read on success or -1 if any error occurs.
 */
jint get_latency_timer_value(JNIEnv *env, jstring comPortName) {

	struct udev *udev_ctx;
	struct udev_enumerate *enumerator;
	struct udev_list_entry *devices, *dev_list_entry;
	const char *device_path;
	const char *device_node;
	const char *path;
	struct udev_device *udev_device;
	const char *com_port_to_match = NULL;
	char buffer[512];
	int fd = 0;
	int ret = 0;
	char *endptr;
	jint timer_value = 0;

	udev_ctx = udev_new();
	enumerator = udev_enumerate_new(udev_ctx);
	udev_enumerate_add_match_subsystem(enumerator, "tty");
	udev_enumerate_scan_devices(enumerator);
	devices = udev_enumerate_get_list_entry(enumerator);

	com_port_to_match = (*env)->GetStringUTFChars(env, comPortName, NULL);
	if((com_port_to_match == NULL) || ((*env)->ExceptionOccurred(env) != NULL)) {
		throw_serialcom_exception(env, 3, 0, E_GETSTRUTFCHARSTR);
		return -1;
	}

	udev_list_entry_foreach(dev_list_entry, devices) {
		path = udev_list_entry_get_name(dev_list_entry);
		udev_device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerator), path);
		if(udev_device == NULL) {
			continue;
		}

		/* match the device node, if matched get device path, and create absolute path to
		 * latency file and read its value */
		device_node = udev_device_get_devnode(udev_device);
		if(device_node != NULL) {
			if(strcmp(device_node, com_port_to_match) == 0) {

				device_path = udev_device_get_devpath(udev_device);
				if(device_path != NULL) {
					memset(buffer, '\0', sizeof(buffer));
					snprintf(buffer, 512, "/sys%s/device/latency_timer", device_path);

					errno = 0;
					fd = open(buffer, O_RDONLY);
					if(fd < 0) {
						(*env)->ReleaseStringUTFChars(env, comPortName, com_port_to_match);
						throw_serialcom_exception(env, 1, errno, NULL);
						return -1;
					}

					memset(buffer, '\0', sizeof(buffer));
					errno = 0;
					ret = read(fd, buffer, 512);
					if(ret < 0) {
						(*env)->ReleaseStringUTFChars(env, comPortName, com_port_to_match);
						throw_serialcom_exception(env, 1, errno, NULL);
						return -1;
					}
					close(fd);

					timer_value = (jint) strtol(buffer, &endptr, 10);
					(*env)->ReleaseStringUTFChars(env, comPortName, com_port_to_match);
					udev_device_unref(udev_device);
					udev_enumerate_unref(enumerator);
					udev_unref(udev_ctx);
					return timer_value;
				}
			}
		}

		udev_device_unref(udev_device);
	}
Example #24
0
File: udev.c Project: etix/vlc
        "video4linux", N_("Video capture"),
        v4l_get_mrl, v4l_get_name, ITEM_TYPE_CARD,
    };

    return Open (obj, &subsys);
}


#ifdef HAVE_ALSA
/*** Advanced Linux Sound Architecture support ***/
#include <alsa/asoundlib.h>

static int alsa_get_device (struct udev_device *dev, unsigned *restrict pcard,
                            unsigned *restrict pdevice)
{
    const char *node = udev_device_get_devpath (dev);
    char type;

    node = strrchr (node, '/');
    if (node == NULL)
        return -1;
    if (sscanf (node, "/pcmC%uD%u%c", pcard, pdevice, &type) < 3)
        return -1;
    if (type != 'c')
        return -1;
    return 0;
}


static char *alsa_get_mrl (struct udev_device *dev)
{
Example #25
0
static void
udev_monitor_watcher (struct udev_monitor *udev_monitor,
		      NihIoWatch *         watch,
		      NihIoEvents          events)
{
	struct udev_device *    udev_device;
	nih_local char *        subsystem = NULL;
	nih_local char *        action = NULL;
	nih_local char *        kernel = NULL;
	nih_local char *        devpath = NULL;
	nih_local char *        devname = NULL;
	nih_local char *        name = NULL;
	nih_local char **       env = NULL;
	const char *            value = NULL;
	size_t                  env_len = 0;
	DBusPendingCall *       pending_call;
	char                 *(*copy_string)(const void *, const char *) = NULL;


	udev_device = udev_monitor_receive_device (udev_monitor);
	if (! udev_device)
		return;

	copy_string = no_strip_udev_data ? nih_strdup : make_safe_string;

	value = udev_device_get_subsystem (udev_device);
	subsystem = value ? copy_string (NULL, value) : NULL;

	value = udev_device_get_action (udev_device);
	action = value ? copy_string (NULL, value) : NULL;

	value = udev_device_get_sysname (udev_device);
	kernel = value ? copy_string (NULL, value) : NULL;

	value = udev_device_get_devpath (udev_device);
	devpath = value ? copy_string (NULL, value) : NULL;

	value = udev_device_get_devnode (udev_device);
	devname = value ? copy_string (NULL, value) : NULL;

	/* Protect against the "impossible" */
	if (! action)
		goto out;

	if (! strcmp (action, "add")) {
		name = NIH_MUST (nih_sprintf (NULL, "%s-device-added",
					      subsystem));
	} else if (! strcmp (action, "change")) {
		name = NIH_MUST (nih_sprintf (NULL, "%s-device-changed",
					      subsystem));
	} else if (! strcmp (action, "remove")) {
		name = NIH_MUST (nih_sprintf (NULL, "%s-device-removed",
					      subsystem));
	} else {
		name = NIH_MUST (nih_sprintf (NULL, "%s-device-%s",
					      subsystem, action));
	}

	env = NIH_MUST (nih_str_array_new (NULL));

	if (kernel) {
		nih_local char *var = NULL;

		var = NIH_MUST (nih_sprintf (NULL, "KERNEL=%s", kernel));
		NIH_MUST (nih_str_array_addp (&env, NULL, &env_len, var));
	}

	if (devpath) {
		nih_local char *var = NULL;

		var = NIH_MUST (nih_sprintf (NULL, "DEVPATH=%s", devpath));
		NIH_MUST (nih_str_array_addp (&env, NULL, &env_len, var));
	}

	if (devname) {
		nih_local char *var = NULL;

		var = NIH_MUST (nih_sprintf (NULL, "DEVNAME=%s", devname));
		NIH_MUST (nih_str_array_addp (&env, NULL, &env_len, var));
	}

	if (subsystem) {
		nih_local char *var = NULL;

		var = NIH_MUST (nih_sprintf (NULL, "SUBSYSTEM=%s", subsystem));
		NIH_MUST (nih_str_array_addp (&env, NULL, &env_len, var));
	}

	if (action) {
		nih_local char *var = NULL;

		var = NIH_MUST (nih_sprintf (NULL, "ACTION=%s", action));
		NIH_MUST (nih_str_array_addp (&env, NULL, &env_len, var));
	}

	for (struct udev_list_entry *list_entry = udev_device_get_properties_list_entry (udev_device);
	     list_entry != NULL;
	     list_entry = udev_list_entry_get_next (list_entry)) {
		nih_local char *udev_name = NULL;
		nih_local char *udev_value = NULL;
		nih_local char *var = NULL;

		udev_name = copy_string (NULL, udev_list_entry_get_name (list_entry));

		if (! strcmp (udev_name, "DEVPATH"))
			continue;
		if (! strcmp (udev_name, "DEVNAME"))
			continue;
		if (! strcmp (udev_name, "SUBSYSTEM"))
			continue;
		if (! strcmp (udev_name, "ACTION"))
			continue;

		udev_value = copy_string (NULL, udev_list_entry_get_value (list_entry));

		var = NIH_MUST (nih_sprintf (NULL, "%s=%s", udev_name, udev_value));
		NIH_MUST (nih_str_array_addp (&env, NULL, &env_len, var));
	}

	nih_debug ("%s %s", name, devname ? devname : "");

	pending_call = upstart_emit_event (upstart,
			name, env, FALSE,
			NULL, emit_event_error, NULL,
			NIH_DBUS_TIMEOUT_NEVER);

	if (! pending_call) {
		NihError *err;
		int saved = errno;

		err = nih_error_get ();
		nih_warn ("%s", err->message);

		if (saved != ENOMEM && subsystem)
			nih_warn ("Likely that udev '%s' event contains binary garbage", subsystem);

		nih_free (err);
	}

	dbus_pending_call_unref (pending_call);

out:
	udev_device_unref (udev_device);
}
Example #26
0
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("\\----------------------------------------------");
}
Example #27
0
static int update_link(struct udev_device *dev, const char *slink, int test)
{
	struct udev *udev = udev_device_get_udev(dev);
	struct udev_list_node dev_list;
	struct udev_list_entry *dev_entry;
	char target[UTIL_PATH_SIZE];
	int count;
	int priority = 0;
	int rc = 0;

	dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));

	udev_list_init(&dev_list);
	count = name_index_get_devices(udev, slink, &dev_list);
	if (count > 1)
		info(udev, "found %i devices with name '%s'\n", count, slink);

	/* if we don't have a reference, delete it */
	if (count <= 0) {
		info(udev, "no reference left, remove '%s'\n", slink);
		if (!test) {
			unlink(slink);
			util_delete_path(udev, slink);
		}
		goto out;
	}

	/* find the device with the highest priority */
	target[0] = '\0';
	udev_list_entry_foreach(dev_entry, udev_list_get_entry(&dev_list)) {
		const char *syspath;
		struct udev_device *dev_db;
		const char *devnode;

		syspath = udev_list_entry_get_name(dev_entry);
		dbg(udev, "found '%s' for '%s'\n", syspath, slink);

		/* did we find ourself? we win, if we have the same priority */
		if (strcmp(udev_device_get_syspath(dev), syspath) == 0) {
			dbg(udev, "compare (our own) priority of '%s' %i >= %i\n",
			    udev_device_get_devpath(dev), udev_device_get_devlink_priority(dev), priority);
			if (strcmp(udev_device_get_devnode(dev), slink) == 0) {
				info(udev, "'%s' is our device node, database inconsistent, skip link update\n",
				     udev_device_get_devnode(dev));
			} else if (target[0] == '\0' || udev_device_get_devlink_priority(dev) >= priority) {
				priority = udev_device_get_devlink_priority(dev);
				util_strlcpy(target, udev_device_get_devnode(dev), sizeof(target));
			}
			continue;
		}

		/* another device, read priority from database */
		dev_db = udev_device_new_from_syspath(udev, syspath);
		if (dev_db == NULL)
			continue;
		devnode = udev_device_get_devnode(dev_db);
		if (devnode != NULL) {
			if (strcmp(devnode, slink) == 0) {
				info(udev, "'%s' is a device node of '%s', skip link update\n",
				     devnode, syspath);
			} else {
				dbg(udev, "compare priority of '%s' %i > %i\n",
				    udev_device_get_devpath(dev_db),
				    udev_device_get_devlink_priority(dev_db),
				    priority);
				if (target[0] == '\0' || udev_device_get_devlink_priority(dev_db) > priority) {
					priority = udev_device_get_devlink_priority(dev_db);
					util_strlcpy(target, devnode, sizeof(target));
				}
			}
		}
		udev_device_unref(dev_db);
	}
	udev_list_cleanup_entries(udev, &dev_list);

	if (target[0] == '\0') {
		info(udev, "no current target for '%s' found\n", slink);
		rc = 1;
		goto out;
	}

	/* create symlink to the target with the highest priority */
	info(udev, "'%s' with target '%s' has the highest priority %i, create it\n", slink, target, priority);
	if (!test) {
		util_create_path(udev, slink);
		node_symlink(udev, target, slink);
	}
out:
	return rc;
}
Example #28
0
static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test)
{
        struct udev *udev = udev_device_get_udev(dev);
        static const char *searchpath[] = { FIRMWARE_PATH };
        char fwencpath[UTIL_PATH_SIZE];
        char misspath[UTIL_PATH_SIZE];
        char loadpath[UTIL_PATH_SIZE];
        char datapath[UTIL_PATH_SIZE];
        char fwpath[UTIL_PATH_SIZE];
        const char *firmware;
        FILE *fwfile = NULL;
        struct utsname kernel;
        struct stat statbuf;
        unsigned int i;
        int rc = EXIT_SUCCESS;

        firmware = udev_device_get_property_value(dev, "FIRMWARE");
        if (firmware == NULL) {
                log_error("firmware parameter missing\n\n");
                rc = EXIT_FAILURE;
                goto exit;
        }

        /* lookup firmware file */
        uname(&kernel);
        for (i = 0; i < ELEMENTSOF(searchpath); i++) {
                util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
                fwfile = fopen(fwpath, "re");
                if (fwfile != NULL)
                        break;

                util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
                fwfile = fopen(fwpath, "re");
                if (fwfile != NULL)
                        break;
        }

        util_path_encode(firmware, fwencpath, sizeof(fwencpath));
        util_strscpyl(misspath, sizeof(misspath), "/run/udev/firmware-missing/", fwencpath, NULL);
        util_strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);

        if (fwfile == NULL) {
                int err;

                /* This link indicates the missing firmware file and the associated device */
                log_debug("did not find firmware file '%s'\n", firmware);
                do {
                        err = mkdir_parents(misspath, 0755);
                        if (err != 0 && err != -ENOENT)
                                break;
                        err = symlink(udev_device_get_devpath(dev), misspath);
                        if (err != 0)
                                err = -errno;
                } while (err == -ENOENT);
                rc = EXIT_FAILURE;
                /*
                 * Do not cancel the request in the initrd, the real root might have
                 * the firmware file and the 'coldplug' run in the real root will find
                 * this pending request and fulfill or cancel it.
                 * */
                if (!in_initrd())
                        set_loading(udev, loadpath, "-1");
                goto exit;
        }

        if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
                if (!in_initrd())
                        set_loading(udev, loadpath, "-1");
                rc = EXIT_FAILURE;
                goto exit;
        }

        if (unlink(misspath) == 0)
                util_delete_path(udev, misspath);

        if (!set_loading(udev, loadpath, "1"))
                goto exit;

        util_strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL);
        if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
                log_error("error sending firmware '%s' to device\n", firmware);
                set_loading(udev, loadpath, "-1");
                rc = EXIT_FAILURE;
                goto exit;
        };

        set_loading(udev, loadpath, "0");
exit:
        if (fwfile)
                fclose(fwfile);
        return rc;
}
Example #29
0
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 ) );
         }