How to access a device from linux daemon
I'm writing a linux daemon that should control a hardware device presented on a filesystem as /dev/mydevice. The owner of the file is root and group is set to mydaemon. Chmod 660.
The daemon is started as root, but then drops the user and changes it to mydaemon Code:
if (getuid() == 0 || geteuid() == 0) { Code:
open("/dev/mydevice", "O_RDWR"); Running the same code as mydaemon user without daemonizing works fine - so mydaemon really have access to device files. What I'm doing wrong? I have posted the same question on stackoverflow (http://stackoverflow.com/q/12476288/693039), but unfortunately there is no answer, yet. best regards Jan |
Pick one:
1. perform open before setuid 2. change chmod from 0640 to 0660 |
Hi,
Quote:
Code:
# ls /dev/mydevice |
Pick one:
1. perform open(2) before setuid(2) 2. use setgid(2) also |
Unfortunately I have still not solved my problem. Better description is now in stackoverflow question http://stackoverflow.com/questions/12476288.
In short: I have a file owned by a root:video. Chmod is 440. User mydaemon is also member of a video group. When I run my application as root and change user to mydaemon using setuid and setgid to mydaemon:mydaemon I cannot access the files belonging to video group. |
Because you should have setgid(2) to 'video' group. Or, better, call getgroups(2), add 'video' to the list, then call setgroups(2).
PS: Ever tried to open the file before setuid(2)? |
Thank you for pointing to setgroups()
Quote:
|
It looks like you are setting the primary group only, but not the additional group list. You can add this to the settings:
Code:
if (pw) { |
Quote:
|
All times are GMT -5. The time now is 09:07 AM. |