I have been experimenting with different apps that need access to /dev/mem, but I wanted to run these apps without being sudo all the time.
TL;DR
1. install libcap2
2. add user to kmem group
3. create udev rule to allow read/write access to /dev/mem by kmem group members
4. compile application then set its capability to cap_sys_rawio
I have attached a patch I made to bcm2835 that allows it to work without being root, as long as the above steps are performed once. This current patch does NOT change the library's behavior in that it will not try to access /dev/mem unless you are root or the rawio capability is set. See below for another way to handle this.
A slight downside to my current approach is that any app that brings in the bcm2835 library needs to link with -lcap and also needs libcap-dev installed.
A perhaps "cleaner" solution would be for the bcm2835 library to NOT fail/exit if it can't open /dev/mem - it could treat this situation as the same as not being root. Therefore, /dev/mem can be opened by either running the app as root OR setting its capability to cap_sys_rawio. If this change was made then the bcm2835 code doesn't need to test the capability flag and then wouldn't need to be linked with -lcap and libcap-dev would not be required.
To set an app to have the rawio capability, this needs to be run once, after the app has been compiled:
sudo setcap cap_sys_rawio+ep myappname
Once this is done, you can run the app without being sudo and bcm2835 will be able to access /dev/mem.