Question: Has Anyone Been Able to Compile BCM2835 SPI Code such that Application Does not Requie to be Run as Root

162 views
Skip to first unread message

John L. Males

unread,
Nov 11, 2020, 10:53:46 AM11/11/20
to bcm...@googlegroups.com
Hello,

Has anyone been able to create an application using the BCM2835
libraries such that the application does not need to be run as
root? If so how did you accomplish this?

I have no problem with the application compiling. It will
run as root. I have yet to figure out how to do so such
that the application can run as normal user.


John L. Males
Toronto, Ontario
Canada
11 November 2020 10:53 -0500 EST


================================================================

2020-11-11 15:47:51+0000-UTC Time: 1605109671 PC/System time

11 Nov 15:47:51 ntpdate[52299]: ntpdate 4.2.8p12-a (1)

11 Nov 15:48:05 ntpdate[52734]: step time server 163.237.218.19
offset +0.002039 sec

FreeBSD 11.4-RELEASE-p3 FreeBSD 11.4-RELEASE-p3 #0: Tue Sep 1
08:22:33 UTC 2020
ro...@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC

(Work in progress alternative to Linux Kernel of its own right,
Debian, and
other Linux based Kernel distributions determined.)

Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz
Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz (1396.86-MHz K8-class
CPU) Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz (1396.86-MHz
K8-class CPU) Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz
(1396.86-MHz K8-class CPU) Intel(R) Core(TM) i3-2367M CPU @
1.40GHz (1396.86-MHz K8-class CPU) Intel(R) Core(TM) i3-2367M
CPU @ 1.40GHz (1396.86-MHz K8-class CPU)

dev.cpu.0.temperature: 74.0C
dev.cpu.1.temperature: 74.0C
dev.cpu.2.temperature: 73.0C
dev.cpu.3.temperature: 73.0C
hw.acpi.thermal.tz0.temperature: 74.1C

vmstat -s:

3385669379 cpu context switches
89365788 device interrupts
13007437 software interrupts
403759850 traps
129921771 system calls
27 kernel threads created
44279 fork() calls
3264 vfork() calls
0 rfork() calls
70492 swap pager pageins
289437 swap pager pages paged in
127742 swap pager pageouts
782438 swap pager pages paged out
50609 vnode pager pageins
392687 vnode pager pages paged in
350 vnode pager pageouts
374 vnode pager pages paged out
963 page daemon wakeups
667957848 pages examined by the page daemon
0 clean page reclamation shortfalls
59630132 pages reactivated by the page daemon
2357744 copy-on-write faults
6675 copy-on-write optimized faults
325231740 zero fill pages zeroed
7885 zero fill pages prezeroed
71859 intransit blocking page faults
424331607 total VM faults taken
123747 page faults requiring I/O
0 pages affected by kernel thread creation
1861153 pages affected by fork()
114507 pages affected by vfork()
0 pages affected by rfork()
408997115 pages freed
55783745 pages freed by daemon
208959760 pages freed by exiting processes
1020458 pages active
1833704 pages inactive
451936 pages in the laundry queue
687810 pages wired down
58857 pages free
4096 bytes per page
143084256 total name lookups
cache hits (82% pos + 3% neg) system 0% per-directory
deletions 0%, falsehits 0%, toolong 0%

Boot time : 1604644041

procs memory page disks
faults cpu0 cpu1 cpu2 cpu3 r b w avm
fre flt re pi po fr sr ad0 pa0 in sy cs us sy
id us sy id us sy id us sy id 0 0 0 100491876 235352 911
128 0 0 878 1434 0 0 192 279 7271 13 4 82 14 4
83 14 4 82 14 4 82

memory info:

real memory = 17179869184 (16384 MB)
avail memory = 16495013888 (15730 MB)

last pid: 54410; load averages: 0.42, 0.76, 1.15 up
5+09:20:45 15:48:06 71 processes: 1 running, 69 sleeping, 1
zombie

Mem: 3987M Active, 7163M Inact, 1765M Laundry, 2687M Wired,
1554M Buf, 229M Free Swap: 48G Total, 2744M Used, 45G Free, 5%
Inuse

hw.physmem: 17053859840
hw.usermem: 14236389376
hw.realmem: 17179869184

total used free shared
buffers cached Mem: 16210872 8640792
7570080 0 0 0 Swap: 50331644
2810268 47521376

swapinfo:

Device 1K-blocks Used Avail Capacity
/dev/ada0s1b 50331644 2810268 47521376 6%

vmstat:

procs memory page disks
faults cpu r b w avm fre flt re pi po
fr sr ad0 pa0 in sy cs us sy id 2 0 0 100491896
234880 911 128 0 0 878 1434 0 0 192 279 7271
14 4 82

Arjan van Vught

unread,
Nov 11, 2020, 11:41:30 AM11/11/20
to bcm...@googlegroups.com

Op 11 nov. 2020, om 16:53 heeft John L. Males <jlm...@gmail.com> het volgende geschreven:

Hello,

Has anyone been able to create an application using the BCM2835
libraries such that the application does not need to be run as
root?  If so how did you accomplish this?


"If the library runs with any other effective UID (ie not root), .. In particular, bcm2835_spi_begin()and bcm2835_i2c_begin() will return false and all other non-gpio operations may fail silently or crash.

As there is low-level access to the SPI/I2C devices , there is a prerequisite for root level authorization. 

- Arjan

John L. Males

unread,
Nov 11, 2020, 12:52:11 PM11/11/20
to Arjan van Vught
Arjan,

I had read that as you noted as of few months ago and few more
times since. The BCM2835 library does document a process to
enable non root access with the BCM2835 library, but does not
work at most basic level of changing the define the BCM2835
library documents. So I cannot even try what is documented
on the BCM2835 site to enable non-root access to SPI.

I2C can work without need for root access. Why is it SPI cannot
do likewise? It seems SPI can be accessed via Python without
root access. For the specific SPI device I am coding for
Python is not an option as the the overhead of Python will
serious limit the data rate the SPI device is capable of and is
needed to be.


John L. Males
Toronto, Ontario
Canada
11 November 2020 12:52 -0500 EST


================================================================

2020-11-11 17:43:49+0000-UTC Time: 1605116629 PC/System time

11 Nov 17:43:49 ntpdate[74368]: ntpdate 4.2.8p12-a (1)

11 Nov 17:44:03 ntpdate[74758]: step time server 75.159.85.245
offset -0.002387 sec

FreeBSD 11.4-RELEASE-p3 FreeBSD 11.4-RELEASE-p3 #0: Tue Sep 1
08:22:33 UTC 2020
ro...@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC

(Work in progress alternative to Linux Kernel of its own right,
Debian, and
other Linux based Kernel distributions determined.)

Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz
Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz (1396.86-MHz K8-class
CPU) Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz (1396.86-MHz
K8-class CPU) Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz
(1396.86-MHz K8-class CPU) Intel(R) Core(TM) i3-2367M CPU @
1.40GHz (1396.86-MHz K8-class CPU) Intel(R) Core(TM) i3-2367M
CPU @ 1.40GHz (1396.86-MHz K8-class CPU)

dev.cpu.0.temperature: 75.0C
dev.cpu.1.temperature: 76.0C
dev.cpu.2.temperature: 74.0C
dev.cpu.3.temperature: 75.0C
hw.acpi.thermal.tz0.temperature: 74.1C

vmstat -s:

3425522888 cpu context switches
91424895 device interrupts
13249471 software interrupts
409300151 traps
244790902 system calls
27 kernel threads created
44514 fork() calls
3276 vfork() calls
0 rfork() calls
74393 swap pager pageins
302025 swap pager pages paged in
127950 swap pager pageouts
784710 swap pager pages paged out
51076 vnode pager pageins
395682 vnode pager pages paged in
353 vnode pager pageouts
377 vnode pager pages paged out
984 page daemon wakeups
688347009 pages examined by the page daemon
0 clean page reclamation shortfalls
63708740 pages reactivated by the page daemon
2374099 copy-on-write faults
6691 copy-on-write optimized faults
329918895 zero fill pages zeroed
7938 zero fill pages prezeroed
75781 intransit blocking page faults
430178412 total VM faults taken
128105 page faults requiring I/O
0 pages affected by kernel thread creation
1875841 pages affected by fork()
114927 pages affected by vfork()
0 pages affected by rfork()
414382296 pages freed
57004624 pages freed by daemon
212333817 pages freed by exiting processes
1311590 pages active
1502169 pages inactive
454635 pages in the laundry queue
673219 pages wired down
111152 pages free
4096 bytes per page
144063849 total name lookups
cache hits (83% pos + 3% neg) system 0% per-directory
deletions 0%, falsehits 0%, toolong 0%

Boot time : 1604644041

procs memory page disks
faults cpu0 cpu1 cpu2 cpu3 r b w avm
fre flt re pi po fr sr ad0 pa0 in sy cs us sy
id us sy id us sy id us sy id 2 0 0 100918636 444544 910
135 0 0 877 1457 0 0 193 518 7248 13 4 82 14 4
83 14 4 82 14 4 82

memory info:

real memory = 17179869184 (16384 MB)
avail memory = 16495013888 (15730 MB)

last pid: 77966; load averages: 1.28, 0.95, 0.78 up
5+11:16:43 17:44:04 71 processes: 3 running, 67 sleeping, 1
zombie

Mem: 5124M Active, 5868M Inact, 1776M Laundry, 2630M Wired,
1554M Buf, 433M Free Swap: 48G Total, 2745M Used, 45G Free, 5%
Inuse

hw.physmem: 17053859840
hw.usermem: 14296178688
hw.realmem: 17179869184

total used free shared
buffers cached Mem: 16210872 9755996
6454876 0 0 0 Swap: 50331644
2810728 47520916

swapinfo:

Device 1K-blocks Used Avail Capacity
/dev/ada0s1b 50331644 2810728 47520916 6%

vmstat:

procs memory page disks
faults cpu r b w avm fre flt re pi po
fr sr ad0 pa0 in sy cs us sy id 3 0 0 100916360
446164 910 135 0 0 877 1457 0 0 193 518 7248
14 4 82


Message replied to:

Date: Wed, 11 Nov 2020 17:41:25 +0100
From: Arjan van Vught <arjan.v...@gmail.com>
To: bcm...@googlegroups.com
Subject: Re: [bcm2835] Question: Has Anyone Been Able to
Compile BCM2835 SPI Code such that Application Does not Requie
to be Run as Root


>
>
> > Op 11 nov. 2020, om 16:53 heeft John L. Males
> > <jlm...@gmail.com> het volgende geschreven:
> >
> > Hello,
> >
> > Has anyone been able to create an application using the
> > BCM2835 libraries such that the application does not need
> > to be run as root? If so how did you accomplish this?
>
> With reference to https://www.airspayce.com/mikem/bcm2835/
> <https://www.airspayce.com/mikem/bcm2835/>
>
> "If the library runs with any other effective UID (ie not
> root), .. In particular, bcm2835_spi_begin()
> <https://www.airspayce.com/mikem/bcm2835/group__spi.html#ga6f0330a183f3c5765a36f1839e029a44>and
> bcm2835_i2c_begin()
> <https://www.airspayce.com/mikem/bcm2835/group__i2c.html#gaae82c3f4b7dfb4dcb51358d1ae878029>
> will return false and all other non-gpio operations may fail
> silently or crash.?
>
> As there is low-level access to the SPI/I2C devices , there
> is a prerequisite for root level authorization.
>
> - Arjan
> https://github.com/vanvught/rpidmx512
>
> --
> You received this message because you are subscribed to the
> Google Groups "bcm2835" group. To unsubscribe from this group
> and stop receiving emails from it, send an email to
> bcm2835+u...@googlegroups.com. To view this discussion
> on the web visit
> https://groups.google.com/d/msgid/bcm2835/E5B2B7B1-02CB-4C28-AC29-2758FFD77C1A%40gmail.com.

Arjan van Vught

unread,
Nov 11, 2020, 1:13:34 PM11/11/20
to bcm...@googlegroups.com
Hi John,

 The BCM2835 library does document a process to
enable non root access with the BCM2835 library, but does not
work at most basic level of changing the define the BCM2835
library documents.
Ok. This 
1.65, 1.66 2020-04-16 Added support for use of capability cap_sys_rawio to determine if access to /dev/mem is available for non-root users. Contributed by Doug McFadyen.
should enable non-root users accessing /dev/mem. And requires modifications to the source files. 

I2C can work without need for root access.
Not really. I’ve tested that scenario. 


Add in your C/C++ program:

#include <sys/types.h>
#include <unistd.h>

int main(void) {
setuid(0);

Then make sure that the executable is owned by root:root
And give it a: chmod 4755

Example for I2C, the same would apply for SPI:

pi@raspberrypi-3:/development/workspace/scratch-raspbian-c++ $ ls -al scratch-raspbian-c++
-rwxr-xr-x 1 pi pi 37724 Nov 11 17:22 scratch-raspbian-c++

pi@raspberrypi-3:/development/workspace/scratch-raspbian-c++ $ ./scratch-raspbian-c++ 
gpio
Segmentation fault

Copied as root to /tmp with chmod 4755

pi@raspberrypi-3:/tmp $ ls -al scratch-raspbian-c++
-rwsr-xr-x 1 root root 37724 Nov 11 17:25 scratch-raspbian-c++


pi@raspberrypi-3:/tmp $ ./scratch-raspbian-c++ 
gpio

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: — — — — — -- — --                         

Done!


John L. Males

unread,
Nov 11, 2020, 1:45:24 PM11/11/20
to Arjan van Vught
Arjan,

You are correct in the reference you made related to BCM2835
1.65, 1.66. In BCM2835 1.68 I changed "... o enable this
ability, uncomment the #define BCM2835_HAVE_LIBCAP in
bcm2835.h" to be able to "If your program needs acccess
to /dev/mem but not as root, ..." Sadly the result was errors
at compile time netting out to not being able to build BCM2835
to allow non-root access to /dev/mem.

I will try your setuid code addition related example. This
will likley be in a few days. I have to work on important
application support code related for the SPI device that does
not involve any SPI functionality of the SPI device, only the
application elements related to the SPI device.

I have lots of Raspberry Pi code accessing I2C devices that has
never needed root nor setuid(0) access and I never had to do
any special coding nor library activity to allow the Raspberry
Pi to run as non-root. The code has been running for few years
now. I am fairly certain if I needed to run I2C as root I would
have remembered. If your reference to I2C needs root access is
when using the BCM2835 library, then I do not know. I have
ever used the BCM2835 library for I2C device access.


John L. Males
Toronto, Ontario
Canada
11 November 2020 13:45 -0500 EST


================================================================

2020-11-11 18:17:55+0000-UTC Time: 1605118675 PC/System time

11 Nov 18:17:55 ntpdate[83563]: ntpdate 4.2.8p12-a (1)

11 Nov 18:18:09 ntpdate[84168]: step time server 206.108.0.131
offset -0.001364 sec

FreeBSD 11.4-RELEASE-p3 FreeBSD 11.4-RELEASE-p3 #0: Tue Sep 1
08:22:33 UTC 2020
ro...@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC

(Work in progress alternative to Linux Kernel of its own right,
Debian, and
other Linux based Kernel distributions determined.)

Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz
Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz (1396.86-MHz K8-class
CPU) Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz (1396.86-MHz
K8-class CPU) Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz
(1396.86-MHz K8-class CPU) Intel(R) Core(TM) i3-2367M CPU @
1.40GHz (1396.86-MHz K8-class CPU) Intel(R) Core(TM) i3-2367M
CPU @ 1.40GHz (1396.86-MHz K8-class CPU)

dev.cpu.0.temperature: 74.0C
dev.cpu.1.temperature: 75.0C
dev.cpu.2.temperature: 73.0C
dev.cpu.3.temperature: 74.0C
hw.acpi.thermal.tz0.temperature: 74.1C

vmstat -s:

3437813664 cpu context switches
92031529 device interrupts
13315510 software interrupts
411811522 traps
279719330 system calls
27 kernel threads created
44592 fork() calls
3287 vfork() calls
0 rfork() calls
75581 swap pager pageins
305856 swap pager pages paged in
127950 swap pager pageouts
784710 swap pager pages paged out
51551 vnode pager pageins
398956 vnode pager pages paged in
357 vnode pager pageouts
381 vnode pager pages paged out
986 page daemon wakeups
692439118 pages examined by the page daemon
0 clean page reclamation shortfalls
63741852 pages reactivated by the page daemon
2382590 copy-on-write faults
6699 copy-on-write optimized faults
331753388 zero fill pages zeroed
7939 zero fill pages prezeroed
76984 intransit blocking page faults
432781459 total VM faults taken
129754 page faults requiring I/O
0 pages affected by kernel thread creation
1887153 pages affected by fork()
115312 pages affected by vfork()
0 pages affected by rfork()
416508661 pages freed
57113106 pages freed by daemon
213952713 pages freed by exiting processes
1020028 pages active
1819347 pages inactive
448731 pages in the laundry queue
684485 pages wired down
80174 pages free
4096 bytes per page
144384092 total name lookups
cache hits (83% pos + 3% neg) system 0% per-directory
deletions 0%, falsehits 0%, toolong 0%

Boot time : 1604644041

procs memory page disks
faults cpu0 cpu1 cpu2 cpu3 r b w avm
fre flt re pi po fr sr ad0 pa0 in sy cs us sy
id us sy id us sy id us sy id 0 0 0 99803696 320636 912
134 0 0 878 1459 0 0 194 589 7243 13 4 82 14 4
83 14 4 82 14 4 82

memory info:

real memory = 17179869184 (16384 MB)
avail memory = 16495013888 (15730 MB)

last pid: 86447; load averages: 0.76, 1.14, 1.04 up
5+11:50:49 18:18:10 71 processes: 2 running, 68 sleeping, 1
zombie

Mem: 3985M Active, 7107M Inact, 1753M Laundry, 2674M Wired,
1554M Buf, 312M Free Swap: 48G Total, 2735M Used, 45G Free, 5%
Inuse

hw.physmem: 17053859840
hw.usermem: 14250041344
hw.realmem: 17179869184

total used free shared
buffers cached Mem: 16210872 8612900
7597972 0 0 0 Swap: 50331644
2800924 47530720

swapinfo:

Device 1K-blocks Used Avail Capacity
/dev/ada0s1b 50331644 2800924 47530720 6%

vmstat:

procs memory page disks
faults cpu r b w avm fre flt re pi po
fr sr ad0 pa0 in sy cs us sy id 0 0 0 99803696
320616 912 134 0 0 878 1459 0 0 194 589 7243
14 4 82


Message replied to:

Date: Wed, 11 Nov 2020 19:13:28 +0100
From: Arjan van Vught <arjan.v...@gmail.com>
To: bcm...@googlegroups.com
Subject: Re: [bcm2835] Question: Has Anyone Been Able to
Compile BCM2835 SPI Code such that Application Does not Requie
to be Run as Root


> Hi John,
>
> > The BCM2835 library does document a process to
> > enable non root access with the BCM2835 library, but does
> > not work at most basic level of changing the define the
> > BCM2835 library documents.
>
> Ok. This
> 1.65, 1.66 2020-04-16 Added support for use of capability
> cap_sys_rawio to determine if access to /dev/mem is available
> for non-root users. Contributed by Doug McFadyen. should
> enable non-root users accessing /dev/mem. And requires
> modifications to the source files.
>
> > I2C can work without need for root access.
> Not really. I?ve tested that scenario.
> 70: ? ? ? ? ? -- ? --
>
> Done!
>
>
> - Arjan
> https://github.com/vanvught/rpidmx512
>
> --
> You received this message because you are subscribed to the
> Google Groups "bcm2835" group. To unsubscribe from this group
> and stop receiving emails from it, send an email to
> bcm2835+u...@googlegroups.com. To view this discussion
> on the web visit
> https://groups.google.com/d/msgid/bcm2835/BE93163C-06DB-4398-9974-B632A1E79F8F%40gmail.com.

Arjan van Vught

unread,
Nov 11, 2020, 1:50:43 PM11/11/20
to bcm...@googlegroups.com
John,


  If your reference to I2C needs root access is
when using the BCM2835 library, then I do not know. 

Yes I am referring to the BCM2835 library, as this mailing list is specific for the BCM2835 C library.

- Arjan
Reply all
Reply to author
Forward
0 new messages