ggarg118
Posts: 15
Joined: Thu May 31, 2018 8:04 am

Problem while accessing /dev/mem

Mon Aug 20, 2018 1:43 pm

folks... i got a source code for accessing GPIO of rpi through library "mmapGpio". Somewhere in this c++ file, it is trying to open "/dev/mem".
The problem is i can't access it. Please find the snapshot.
I am sure the problem is related to access permission.. What should i do?
Attachments
can't access _dev_mem.png
can't access _dev_mem.png (145.26 KiB) Viewed 1236 times

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Problem while accessing /dev/mem

Mon Aug 20, 2018 2:00 pm

In general, you will need to be running as root in order to access /dev/mem.

0 crw-r----- 1 root kmem 1, 1 Jun 16 21:44 /dev/mem

(Or be in group kmem; pi is not in that group [by default])

So, you have a few choices:

1) Run the program via "sudo" (this will be the most common advice given on this forum)
2) Make the program setuid root - or setgid kmem
3) Make pi be in the kmem group (and reboot to make it take effect)
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

ggarg118
Posts: 15
Joined: Thu May 31, 2018 8:04 am

Re: Problem while accessing /dev/mem

Mon Aug 20, 2018 2:19 pm

n67 wrote:
Mon Aug 20, 2018 2:00 pm
In general, you will need to be running as root in order to access /dev/mem.

0 crw-r----- 1 root kmem 1, 1 Jun 16 21:44 /dev/mem

(Or be in group kmem; pi is not in that group [by default])

So, you have a few choices:

1) Run the program via "sudo" (this will be the most common advice given on this forum)
2) Make the program setuid root - or setgid kmem
3) Make pi be in the kmem group (and reboot to make it take effect)
I am pretty new to the linux environment. Could u help more? How to do that?? any link suggesting the same?

User avatar
PeterO
Posts: 5436
Joined: Sun Jul 22, 2012 4:14 pm

Re: Problem while accessing /dev/mem

Mon Aug 20, 2018 4:29 pm

Or use /dev/gpiomem which doesn't need root permissions.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Problem while accessing /dev/mem

Mon Aug 20, 2018 5:43 pm

ggarg118 wrote:
Mon Aug 20, 2018 2:19 pm
...
3) Make pi be in the kmem group (and reboot to make it take effect)
I am pretty new to the linux environment. Could u help more? How to do that?? any link suggesting the same?
Probably the simplest (if it works) is to follow the advice of another thread responder and use /dev/gpiomem instead.

But if you want to follow item 3 from the above list, here's how:

0) run "groups" - see that kmem is not in the list.
1) Become root (sudo bash)
2) Edit /etc/group with your favorite editor, say the "vigr" program.

Find this text:

Code: Select all

proxy:x:13:
kmem:x:15:
dialout:x:20:pi
fax:x:21:
See how the "dialout" line has "pi" at the end. This makes pi be a member of the dialout group.
If you put "pi" at the end of the "kmem" line, then you will be in the kmem group as well.

3) Save and exit
4) reboot
5) Log in as pi and run "groups" - see that kmem is now in the list.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Return to “C/C++”