Jack Jack - 24 days ago 11
Linux Question

How to set group id of a device file in Linux kernel?

My sound device looks like this:

# ls -l /dev/snd
crw-rw---- 1 root root 116, 0 Jan 1 00:00 controlC0
crw-rw---- 1 root root 116, 24 Jan 1 00:00 pcmC0D0c
crw------- 1 root root 116, 33 Jan 1 00:00 timer


But it should be like this on a normal Linux kernel (notice the group is 'audio'):

# ls -l /dev/snd
crw-rw---- 1 root audio 116, 0 Jan 1 00:00 controlC0
crw-rw---- 1 root audio 116, 24 Jan 1 00:00 pcmC0D0c
crw------- 1 root audio 116, 33 Jan 1 00:00 timer


I know there is udev that can apply rules to change group when a device is added, but since I have full control of the kernel code, I would like to modify it in the kernel and probably the device driver code.

How do I do this? How does Linux assign the group when a device is added?

PS: I'm using an embedded kernel with verion 4.1. The first is from it. The second is from my Ubuntu pc and I don't see it does it with udev rules. I've read a book says that the user/group attributes are assigned by the process ID/GID when the file is created, then why the Ubuntu pc has different group?

Answer Source

In Linux, sound devices generally work via ALSA, and from ALSA wiki:

udev is the standard way of managing /dev directories, designed to clear up some issues with previous /dev implementations, and provide a robust path forward.

and

udev rules are flexible and very powerful. Writing udev rules should solve common Alsa problems, reported by several people:

Assign several audio devices the same hwplug:x,y numbers, whenever you plug-in or plug-out a device. Identify two identical audio devices using the product ID or the USB bus ID of the device. Upload firmware to a special device.

According to above, I did some experiment by running the following command on my Ubuntu pc:

sudo udevadm control --stop-exec-queue

And I found the groups become root but not audio anymore.

So in general ALSA has used udev for handling audio groups modification to date, and there should be no group modifying code in drivers. I'll use udev to fix my problem, thanks for your help :)