Skip to contentSkip to conversation

Short experiment with libudev to support media controller concept

DISCUSSION Andy Walls 2 participants 4 messages Jul 4, 2009 – Jul 11, 2009 Media (V4L/DVB)

lore
Share & tools
lore

Andy Walls

author
#0 lore raw

Hans,

The inline source file at the end of this post is a small program I used to play with libudev to see if it would complement the media controller concept (as you suspected it would).

Documentation on the libudev calls is here:

http://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/

The test program I wrote takes a (type,major,minor) tuple and lists the device node and device symlinks as fetched by libudev.

My test setup was a little strange since libudev is no longer maintained separately but is bundled in with udev. On my Fedora 9 system I have udev v124 (Fedora 9 stock) and libudev v143 (custom built from the udev 143 source).

Here's some output:

$ ./finddev -c -M 1 -m 3
Requested device: type 'c', major 1, minor 3
Device directory path: '/dev'
Device node: '/dev/null'
Device link: '/dev/XOR'
$ ls -al /dev/ | grep '[ /]null'
crw-rw-rw-   1 root root     1,   3 2009-07-04 08:34 null
lrwxrwxrwx   1 root root          4 2009-07-04 08:34 X0R -> null
lrwxrwxrwx   1 root root          4 2009-07-04 08:34 XOR -> null

(Hmmm, not perfect for /dev/null)

$ ./finddev -b -M 11 -m 0
Requested device: type 'b', major 11, minor 0
Device directory path: '/dev'
Device node: '/dev/sr0'
Device link: '/dev/scd0'
Device link: '/dev/disk/by-path/pci-0000:00:14.1-scsi-1:0:0:0'
Device link: '/dev/cdrom'
Device link: '/dev/cdrw'
$ ls -alR /dev/* | grep '[ /]sr0'
lrwxrwxrwx  1 root root          3 2009-07-04 08:34 /dev/cdrom -> sr0
lrwxrwxrwx  1 root root          3 2009-07-04 08:34 /dev/cdrw -> sr0
lrwxrwxrwx  1 root root          3 2009-07-04 08:34 /dev/scd0 -> sr0
brw-rw----+ 1 root disk    11,   0 2009-07-04 08:34 /dev/sr0
lrwxrwxrwx 1 root root   9 2009-07-04 08:34 pci-0000:00:14.1-scsi-1:0:0:0 -> ../../sr0

(OK for the CDROM drive.)

$ ./finddev -c -M 81 -m 9
Requested device: type 'c', major 81, minor 9
Device directory path: '/dev'
Device node: '/dev/video0'
Device link: '/dev/video'
$ ls -alR /dev/* | grep '[ /]video0'
lrwxrwxrwx  1 root root          6 2009-07-04 08:34 /dev/video -> video0
crw-rw----+ 1 root root    81,   9 2009-07-04 08:34 /dev/video0

(OK for video nodes)

$ ./finddev -c -M 116 -m 6
Requested device: type 'c', major 116, minor 6
Device directory path: '/dev'
Device node: '/dev/snd/pcmC0D0p'
$ ls -alR /dev/* | grep '[ /]pcmC0D0p'
crw-rw----+  1 root root 116, 6 2009-07-04 13:43 pcmC0D0p

(OK for ALSA PCM stream nodes).

Do you have any other particular questions about libudev's capabilities?

Regards,
Andy

/*
* finddev.c - Find a device node given (type,major,minor) using libudev
* Copyright (C) 2009 Andy Walls <[email protected]>
*

 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*
* Compilation:
* gcc -o finddev finddev.c -Wall -ludev
*
* Example invocation (/dev/null on my system):
* ./finddev -c -M 1 -m 3
*/

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libudev.h>

struct parsed_args {
	int major;
	int minor;
	char type;
};

int parse_args(int argc, char *argv[], struct parsed_args *args)

{
	int c, ret;

	ret = 0;
	args->major = args->minor = -1;
	args->type = 'c';

	while ((c = getopt(argc, argv, "bcM:m:")) != -1) {
		switch (c) {
		case 'b': args->type = 'b';           break;
		case 'c': args->type = 'c';           break;
		case 'M': args->major = atoi(optarg); break;
		case 'm': args->minor = atoi(optarg); break;
		default:  ret = -1;                   break;
		}
	}
	if (ret || args->major == -1 || args->minor == -1) {
		fprintf(stderr, "Usage: %s [-b|-c] -M major -m minor\n",
			argv[0]);
		ret = -1;
	}
	return ret;
}

int main(int argc, char *argv[])

{
	struct parsed_args args;
	dev_t devnum;
	struct udev *udev;
	struct udev_device *udev_device;
	const char *s;
	struct udev_list_entry *udev_list_entry;

	if (parse_args(argc,argv, &args))
		exit(1);

	printf("Requested device: type '%c', major %d, minor %d\n",
	       args.type, args.major, args.minor);

	devnum = makedev(args.major, args.minor);

	udev = udev_new();
	if (udev == NULL) {
		fprintf(stderr, "udev_new() failed\n");
		exit(2);
	}

	printf("Device directory path: '%s'\n", udev_get_dev_path(udev));

	udev_device = udev_device_new_from_devnum(udev, args.type, devnum);

	if (udev_device == NULL) {
		fprintf(stderr, "udev_device_new_from_devnum() failed\n");
		udev_unref(udev);
		exit(3);
	}

	s = udev_device_get_devnode(udev_device);
	if (s) {
		printf("Device node: '%s'\n", s);
	} else {
		fprintf(stderr, "udev_device_get_devnode() failed\n");
		udev_device_unref(udev_device);
		udev_unref(udev);
		exit(4);
	}

	udev_list_entry_foreach(udev_list_entry,
			     udev_device_get_devlinks_list_entry(udev_device)) {
		s = udev_list_entry_get_name(udev_list_entry);
		printf("Device link: '%s'\n", s);
	}

	udev_device_unref(udev_device);
	udev_unref(udev);
	exit(0);
}
#1 lore raw
Quoted 71 lines · On Saturday 04 July 2009 19:52:15 Andy Walls wrote:

Hans,

The inline source file at the end of this post is a small program I used to play with libudev to see if it would complement the media controller concept (as you suspected it would).

Documentation on the libudev calls is here:

http://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/

The test program I wrote takes a (type,major,minor) tuple and lists the device node and device symlinks as fetched by libudev.

My test setup was a little strange since libudev is no longer maintained separately but is bundled in with udev. On my Fedora 9 system I have udev v124 (Fedora 9 stock) and libudev v143 (custom built from the udev 143 source).

Here's some output:

$ ./finddev -c -M 1 -m 3
Requested device: type 'c', major 1, minor 3
Device directory path: '/dev'
Device node: '/dev/null'
Device link: '/dev/XOR'
$ ls -al /dev/ | grep '[ /]null'
crw-rw-rw-   1 root root     1,   3 2009-07-04 08:34 null
lrwxrwxrwx   1 root root          4 2009-07-04 08:34 X0R -> null
lrwxrwxrwx   1 root root          4 2009-07-04 08:34 XOR -> null

(Hmmm, not perfect for /dev/null)

$ ./finddev -b -M 11 -m 0
Requested device: type 'b', major 11, minor 0
Device directory path: '/dev'
Device node: '/dev/sr0'
Device link: '/dev/scd0'
Device link: '/dev/disk/by-path/pci-0000:00:14.1-scsi-1:0:0:0'
Device link: '/dev/cdrom'
Device link: '/dev/cdrw'
$ ls -alR /dev/* | grep '[ /]sr0'
lrwxrwxrwx  1 root root          3 2009-07-04 08:34 /dev/cdrom -> sr0
lrwxrwxrwx  1 root root          3 2009-07-04 08:34 /dev/cdrw -> sr0
lrwxrwxrwx  1 root root          3 2009-07-04 08:34 /dev/scd0 -> sr0
brw-rw----+ 1 root disk    11,   0 2009-07-04 08:34 /dev/sr0
lrwxrwxrwx 1 root root   9 2009-07-04 08:34 pci-0000:00:14.1-scsi-1:0:0:0 -> ../../sr0

(OK for the CDROM drive.)

$ ./finddev -c -M 81 -m 9
Requested device: type 'c', major 81, minor 9
Device directory path: '/dev'
Device node: '/dev/video0'
Device link: '/dev/video'
$ ls -alR /dev/* | grep '[ /]video0'
lrwxrwxrwx  1 root root          6 2009-07-04 08:34 /dev/video -> video0
crw-rw----+ 1 root root    81,   9 2009-07-04 08:34 /dev/video0

(OK for video nodes)

$ ./finddev -c -M 116 -m 6
Requested device: type 'c', major 116, minor 6
Device directory path: '/dev'
Device node: '/dev/snd/pcmC0D0p'
Quoted 7 lines
$ ls -alR /dev/* | grep '[ /]pcmC0D0p'
crw-rw----+  1 root root 116, 6 2009-07-04 13:43 pcmC0D0p

(OK for ALSA PCM stream nodes).

Do you have any other particular questions about libudev's capabilities?

Hi Andy,

This looks very promising. Can you try a few things like adding new symlinks for video devices in the udev config file, or renaming the video0 node to, say, mpeg0? If finddev still returns the right nodes, then all that a media controller needs to do is to export the major and minor numbers for each node. That's exactly what I'm hoping for.

Note that I will not have a lot of time to work in v4l-dvb for the next 10 to 14 days as I have visitors this week and will be traveling abroad next week.

Thanks!

Hans

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by TANDBERG Telecom
author
#2 lore raw
In reply to Andy Walls
Quoted 39 lines · Andy Walls

Hans,

The inline source file at the end of this post is a small program I used to play with libudev to see if it would complement the media controller concept (as you suspected it would).

Documentation on the libudev calls is here:

http://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/

The test program I wrote takes a (type,major,minor) tuple and lists the device node and device symlinks as fetched by libudev.

My test setup was a little strange since libudev is no longer maintained separately but is bundled in with udev. On my Fedora 9 system I have udev v124 (Fedora 9 stock) and libudev v143 (custom built from the udev 143 source).

Here's some output:

$ ./finddev -c -M 81 -m 9
Requested device: type 'c', major 81, minor 9
Device directory path: '/dev'
Device node: '/dev/video0'
Device link: '/dev/video'
$ ls -alR /dev/* | grep '[ /]video0'
lrwxrwxrwx  1 root root          6 2009-07-04 08:34 /dev/video -> video0
crw-rw----+ 1 root root    81,   9 2009-07-04 08:34 /dev/video0

(OK for video nodes)

$ ./finddev -c -M 116 -m 6
Requested device: type 'c', major 116, minor 6
Device directory path: '/dev'
Device node: '/dev/snd/pcmC0D0p'
Quoted 7 lines
$ ls -alR /dev/* | grep '[ /]pcmC0D0p'
crw-rw----+  1 root root 116, 6 2009-07-04 13:43 pcmC0D0p

(OK for ALSA PCM stream nodes).

Do you have any other particular questions about libudev's capabilities?

OK, so more tests. Two Major groups: with udev still running and with udev not running (killed after I log in).

Case 1:

- udev running:
- Adding a manual symlink

   # cd /dev
   # ln -s video0 mpeg0

Result: finddev using libudev doesn't find the manual symlink

Case 2:

- udev running
- A manual mknod

   # cd /dev
   # mknod mpeg0 c 81 9

Result: finddev doesn't find the manually created device node

Case 3:
- same as case 2, but manually delete /dev/video0

Result: finddev reports /dev/video0 ! :(

Case 4:
- same as case 3
- add to 50-udev-default.rules:

	KERNEL=="video[0-9]*", SYMLINK+="mpeg%n"
- Reload udev rules
	udevadm control --reload_rules
	udevadm trigger --subsystem-match=video4linux

Result: findddev reports the new '/dev/mpeg0' symlink for /dev/video0 as well as the the '/dev/video' synmlink and the '/dev/video0' device node. :)

Case 5:
- same as case 3
- add to 50-udev-default.rules:

	KERNEL=="video[0-9]*", NAME="mpeg%n"
- Reload udev rules
	udevadm control --reload_rules
	udevadm trigger --subsystem-match=video4linux

Result: SELinux gripes at me because HAL is allowed to acces the attributes of /dev/mpeg*. :)
finddev reports the new /dev/mpeg0 and the /dev/video symlink to it and they both exist in the filesystem. :)

Case 6:
- after case 5
- kill udevd. :)

Result: finddev finds /dev/mpeg0 and the /dev/video symlink

Case 7:
- after case 6:
- manually remove /dev/mpeg0 and /dev/video

Result: finddev reports /dev/mpeg0 and the /dev/video symlink. :?

Apparently libudev uses what's in the udev database and /sys but doesn't look in the /dev directory for manual actions, even when udevd is dead.

Regards,
Andy

Quoted 2 lines

Regards,
Andy

#3 lore raw
Quoted 69 lines · On Saturday 11 July 2009 21:19:46 Andy Walls wrote:
Quoted 39 lines · Andy Walls

Hans,

The inline source file at the end of this post is a small program I used to play with libudev to see if it would complement the media controller concept (as you suspected it would).

Documentation on the libudev calls is here:

http://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/

The test program I wrote takes a (type,major,minor) tuple and lists the device node and device symlinks as fetched by libudev.

My test setup was a little strange since libudev is no longer maintained separately but is bundled in with udev. On my Fedora 9 system I have udev v124 (Fedora 9 stock) and libudev v143 (custom built from the udev 143 source).

Here's some output:

$ ./finddev -c -M 81 -m 9
Requested device: type 'c', major 81, minor 9
Device directory path: '/dev'
Device node: '/dev/video0'
Device link: '/dev/video'
$ ls -alR /dev/* | grep '[ /]video0'
lrwxrwxrwx  1 root root          6 2009-07-04 08:34 /dev/video ->
video0 crw-rw----+ 1 root root    81,   9 2009-07-04 08:34 /dev/video0

(OK for video nodes)

$ ./finddev -c -M 116 -m 6
Requested device: type 'c', major 116, minor 6
Device directory path: '/dev'
Device node: '/dev/snd/pcmC0D0p'
Quoted 8 lines
$ ls -alR /dev/* | grep '[ /]pcmC0D0p'
crw-rw----+  1 root root 116, 6 2009-07-04 13:43 pcmC0D0p

(OK for ALSA PCM stream nodes).

Do you have any other particular questions about libudev's capabilities?

OK, so more tests. Two Major groups: with udev still running and with udev not running (killed after I log in).

Case 1:

- udev running:
- Adding a manual symlink

   # cd /dev
   # ln -s video0 mpeg0

Result: finddev using libudev doesn't find the manual symlink

Case 2:

- udev running
- A manual mknod

   # cd /dev
   # mknod mpeg0 c 81 9
Quoted 7 lines

Result: finddev doesn't find the manually created device node

Case 3:
- same as case 2, but manually delete /dev/video0

Result: finddev reports /dev/video0 ! :(

That's not surprising as cases 1-3 by-pass udev, so libudev knows nothing about those changes.

Quoted 5 lines

Case 4:
- same as case 3
- add to 50-udev-default.rules:

Quoted 8 lines
	KERNEL=="video[0-9]*", SYMLINK+="mpeg%n"
- Reload udev rules
	udevadm control --reload_rules
	udevadm trigger --subsystem-match=video4linux

Result: findddev reports the new '/dev/mpeg0' symlink for /dev/video0 as well as the the '/dev/video' synmlink and the '/dev/video0' device node. :)

Nice!

Quoted 9 lines

Case 5:
- same as case 3
- add to 50-udev-default.rules:

	KERNEL=="video[0-9]*", NAME="mpeg%n"
- Reload udev rules
	udevadm control --reload_rules
	udevadm trigger --subsystem-match=video4linux
Quoted 4 lines

Result: SELinux gripes at me because HAL is allowed to acces the attributes of /dev/mpeg*. :)
finddev reports the new /dev/mpeg0 and the /dev/video symlink to it and they both exist in the filesystem. :)

Nice again!

Quoted 6 lines

Case 6:
- after case 5
- kill udevd. :)

Result: finddev finds /dev/mpeg0 and the /dev/video symlink

Nice test :-) and nice result.

Quoted 5 lines

Case 7:
- after case 6:
- manually remove /dev/mpeg0 and /dev/video

Result: finddev reports /dev/mpeg0 and the /dev/video symlink. :?

Similar to cases 1-3: you bypass udev so libudev won't know about it.

Quoted 4 lines

Apparently libudev uses what's in the udev database and /sys but doesn't look in the /dev directory for manual actions, even when udevd is dead.

That makes sense as udev is basically in charge of the /dev/ contents and making manual /dev/ changes while udev is running defeats the purpose of udev.

I'm really happy finddev finds the symlinks as well. This means that the major and minor numbers are all that is needed in order to reliably find the right device nodes in /dev.

Thanks!

Hans

Quoted 11 lines

Regards,
Andy

Quoted 2 lines

Regards,
Andy

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by TANDBERG