2

I am trying to use the hardware PWM module on my pi 3 by writing to the /sys/class/pwm/pwmchip0 directory (which I think is just a symbolic link to sys/devices/platform/soc/3f20c000.pwm/pwm/pwmchip0). When I do this using bash and sudo, everything works as expected. The problem is that I need to write to this same folder from within a C++ program which I cannot run as root. I have tried creating a group 'pwm', giving ownership of pwmchip0 to 'pwm', and changing permissions with chmod -R g+rw pwmchip0, but when I try to write to the folder without sudo, I get "Permission denied". When I run ls -l on the folder, I get the following permissions, which to be honest I do not really understand:

pi@raspberry:/sys/devices/platform/soc/3f20c000.pwm/pwm $ls -l pwmchip0
total 0
lrwxrwxrwx 1 root pwm    0 May  9 20:44 device -> ../../../3f20c000.pwm
--w-rwx--- 1 root pwm 4096 May  9 20:12 export
-r--rwxr-- 1 root pwm 4096 May  9 20:41 npwm
drwxrwxr-x 2 root pwm    0 May  9 20:41 power
drwxrwxr-x 3 root pwm    0 May  9 20:41 pwm0
drwxrwxr-x 3 root pwm    0 May  9 20:41 pwm1
lrwxrwxrwx 1 root pwm    0 May  9 20:44 subsystem -> ../../../../../../class/pwm
-rw-rwxr-- 1 root pwm 4096 May  9 20:41 uevent
--w-rwx--- 1 root pwm 4096 May  9 20:41 unexport

I was hoping someone else has been able to solve this problem and has some insight for me, or perhaps someone with a better understanding of permissions and groups can enlighten me. Thanks in advance for your help!

  • You could use my pigpio daemon. It has to be run as root but may be accessed by a standard user. It might satisfy your security needs. – joan May 10 '17 at 4:02
  • @joan Thank you! That should work, I will give it a try. – m_dubya May 10 '17 at 15:19
  • 2
    Looks like the fix will be included in next kernel release. Related: github.com/raspberrypi/linux/issues/1983 – Neal Ehardt Dec 5 '17 at 22:22
1

As per github issue Neal Ehardt mentioned in the comment, you need to edit /etc/udev/rules.d/99-com.rules and add the following lines there:

SUBSYSTEM=="pwm*", PROGRAM="/bin/sh -c '\
        chown -R root:gpio /sys/class/pwm && chmod -R 770 /sys/class/pwm;\
        chown -R root:gpio /sys/devices/platform/soc/*.pwm/pwm/pwmchip* && chmod -R 770 /sys/devices/platform/soc/*.pwm/pwm/pwmchip*\
'"

After reboot the default pi user will be able to access the files in /sys/class/pwm/pwmchip0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.