简体   繁体   中英

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM