Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Need for root access to use SPI #4

Closed
davidoccam opened this Issue May 31, 2013 · 8 comments

Comments

Projects
None yet
2 participants
Great work thank you!

I have incorporated this excellent library in an Erlang/OTP Virtual Machine on Pi.
The application, which is for educational purposes, provides a rebar generated environment that incorporates rcouch web accessible nosql database with wpi driven access to GPIO and SPI (and the others available in your wrapper).
All is working very well thanks to you and Gordon for contributing this excellent work.

My only problem is that in order to use the SPI interface I have to run the whole system as root!. I have no problem with the GPIO but as soon as I try to use SPI as normal user I get the following;-

Must be root to call wiringPiSetup().
(Did you forget sudo?)

The code fragment in question is;-
io:format("load spi driver and change user to pi (I hope!!!)ipn",
[os:cmd("gpio load spi")]),

     wpi:spi_setup(0, 200*1024),
     wpi:spi_get_fd(0),

This all works perfectly as root.
I am trying to get to the bottom of this as it is regarded as insecure having the web accessible database run as root. I realise that this is an issue with the wiringpi library itself but I thought I would query here before troubling Gordon with something perhaps regarded as off the main topic and confused by NIF's and wrappers etc.
yours sincerely David Martin

Owner

klajo commented Jun 6, 2013

Hello David,

Sorry for the delay in replying to your post.

Nice to hear that you're putting wpi to good use.

One limitation with the current implementation of the wpi is, as you've discovered, that Erlang has to be run as root (listed in the Caveats section of the readme). The WiringPi library has multiple "setup" functions, wiringPiSetup() for root and wiringPiSetupSys() for non-root, but wpi only supports wiringPiSetup() at the moment.

The way it works today is that as soon as the wpi module is loaded, the NIF is loaded and wiringPiSetup() (which requires root) is called by the NIF. Hence, you'll get the "Must be root to call wiringPiSetup" error message (which is printed to the console by WiringPi).

Perhaps there's another solution, but the one I know is to add support for wiringPiSetupSys(). Then the user of wpi would have to choose which setup to call. Two solutions spring to mind:

  1. Let the user call wpi:setup() or wpi:setup_sys() from their module. This might be a backwards-incompatible change unless wpi defaults to something.

  2. Let the user choose by using some module attribute, like so:

    -wpi_setup_mode(sys).

I'll have to think about that.

Do you have any other ideas?

Thanks and Regards,
Klas

@ghost ghost assigned klajo Jun 6, 2013

On 06/06/13 22:40, Klas Johansson wrote:

Hello David,

Sorry for the delay in replying to your post.

Nice to hear that you're putting wpi to good use.

One limitation with the current implementation of the wpi is, as you've discovered, that Erlang has to be run as root (listed in the Caveats section of the readme). The WiringPi library has multiple "setup" functions, wiringPiSetup() for root and wiringPiSetupSys() for non-root, but wpi only supports wiringPiSetup() at the moment.

The way it works today is that as soon as the wpi module is loaded, the NIF is loaded and wiringPiSetup() (which requires root) is called by the NIF. Hence, you'll get the "Must be root to call wiringPiSetup" error message (which is printed to the console by WiringPi).

Perhaps there's another solution, but the one I know is to add support for wiringPiSetupSys(). Then the user of wpi would have to choose which setup to call. Two solutions spring to mind:

Let the user call wpi:setup() or wpi:setup_sys() from their module. This might be a backwards-incompatible change unless wpi defaults to something.
Let the user choose by using some module attribute, like so:

-wpi_setup_mode(sys).

I'll have to think about that.

Do you have any other ideas?

Thanks and Regards,
Klas


Reply to this email directly or view it on GitHub #4 (comment).

Dear Klas,
no problem, I am very happy that you could reply at all!
Wpi is really great! It saved me having to learn to write the NIF as I only really like write in Erlang.
Everything is really spot on! thanks again!

I bore you with tedious erlang output now,
With SUDO
pi@raspis ~/rcouch/rel/rcouch/bin $ sudo ./rcouch console
Exec: /home/pi/rcouch/rel/rcouch/erts-5.9.3.1/bin/erlexec -boot
/home/pi/rcouch/rel/rcouch/releases/rcouch-0.6.1-4-g08c4c81/rcouch -embedded -config /home/pi/rcouch/rel/rcouch/etc/app.config -args_file /home/pi/rcouch/rel/rcouch/etc/vm.args -- console
Root: /home/pi/rcouch/rel/rcouch
heart_beat_kill_pid = 18157
Erlang R15B03 (erts-5.9.3.1) [source] [async-threads:16] [kernel-poll:true]

04:57:38.281 [info] Application lager started on node 'rcouch@raspis.mol.gas.bog'
04:57:38.584 [notice] Deprecated lager_file_backend config detected, please consider updating it
couch core 1.3.0 is starting
rcouch rcouch-0.6.1-4-g08c4c81 (LogLevel=info) is starting.
04:57:39.198 [info] needed 215.645 ms to open new _users
04:57:39.439 [info] Application couch started on node 'rcouch@raspis.mol.gas.bog'
04:57:39.948 [info] HTTP API started on "http://0.0.0.0:5984"
04:57:39.976 [info] Application couch_httpd started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.062 [info] Application couch_index started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.148 [info] Application couch_mrview started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.150 [info] Application couch_changes started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.290 [info] needed 7.807 ms to open new _replicator
04:57:40.303 [info] Application couch_replicator started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.308 [info] Application vtree started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.312 [info] Application refuge_spatial started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.317 [info] Application couch_randomdoc started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.322 [info] Application couch_dbupdates started on node 'rcouch@raspis.mol.gas.bog'
04:57:40.326 [info] Application wpi started on node 'rcouch@raspis.mol.gas.bog'
gpio export pin 0(native 17) output[]
gpio export pin 1(native 18) output[]
gpio export pin 2(native 21) output[]
gpio export pin 3(native 22) output[]
gpio export pin 4(native 23) output[]
gpio export pin 5(native 24) output[]
gpio export pin 6(native 25) output[]
gpio export pin 7(native 4) output[]
load spi cmd issued[<<>>]
[<<"+----------+------+--------+------+-------+">>,
<<"| wiringPi | GPIO | Name | Mode | Value |">>,
<<"+----------+------+--------+------+-------+">>,
<<"| 0 | 17 | GPIO 0 | OUT | Low |">>,
<<"| 1 | 18 | GPIO 1 | OUT | Low |">>,
<<"| 2 | 21 | GPIO 2 | OUT | Low |">>,
<<"| 3 | 22 | GPIO 3 | OUT | Low |">>,
<<"| 4 | 23 | GPIO 4 | OUT | Low |">>,
<<"| 5 | 24 | GPIO 5 | OUT | Low |">>,
<<"| 6 | 25 | GPIO 6 | OUT | Low |">>,
<<"| 7 | 4 | GPIO 7 | OUT | Low |">>,
<<"| 8 | 0 | SDA | IN | High |">>,
<<"| 9 | 1 | SCL | IN | High |">>,
<<"| 10 | 8 | CE0 | ALT0 | High |">>,
<<"| 11 | 7 | CE1 | ALT0 | High |">>,
<<"| 12 | 10 | MOSI | ALT0 | High |">>,
<<"| 13 | 9 | MISO | ALT0 | High |">>,
<<"| 14 | 11 | SCLK | ALT0 | Low |">>,
<<"| 15 | 14 | TxD | ALT0 | High |">>,
<<"| 16 | 15 | RxD | ALT0 | High |">>,
<<"+----------+------+--------+------+-------+">>,<<>>]
[<<"Module Size Used by">>,
<<"spi_bcm2708 4816 0 ">>,
<<"spidev 5224 0 ">>,
<<"snd_bcm2835 16304 0 ">>,
<<"snd_pcm 77560 1 snd_bcm2835">>,
<<"snd_seq 53329 0 ">>,
<<"snd_timer 19998 2 snd_pcm,snd_seq">>,
<<"snd_seq_device 6438 1 snd_seq">>,
<<"snd 58447 5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device">>,
<<"snd_page_alloc 5145 1 snd_pcm">>,
<<"8192cu 490353 0 ">>,
<<"leds_gpio 2235 0 ">>,
<<"led_class 3562 1 leds_gpio">>,<<>>]
init gpio7_server V1.0
init gpio6_server V1.0
init gpio5_server V1.0
init gpio4_server V1.0
init gpio3_server V1.0
init gpio2_server V1.0
init gpio1_server V1.0
init gpio0_server V1.0
04:57:41.968 [info] Application gpio started on node 'rcouch@raspis.mol.gas.bog'
init spi0_server V1.01
Create I/O buffer Tab 57398
load spi driver and change user to p I hope!!!i[]
init spi1_server V1.0
04:57:43.136 [info] Application spi started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.189 [info] Application lcd started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.244 [info] Application shift_bits started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.300 [info] Application soft_pcm started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.359 [info] Application serial_io started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.483 [info] Application couchbeam started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.532 [info] Starting Walk_app application with '/media/15MiBGP/project_walk/rel/walk_app/bin/walk_app console'
in counter_server init,
created ets table 61496
04:57:43.622 [info] Application walk_app started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.625 [info] Application eper started on node 'rcouch@raspis.mol.gas.bog'
04:57:43.687 [info] Application hotload started on node 'rcouch@raspis.mol.gas.bog'

without SUDO
pi@raspis ~/rcouch/rel/rcouch/bin $ ./rcouch console
Exec: /home/pi/rcouch/rel/rcouch/erts-5.9.3.1/bin/erlexec -boot
/home/pi/rcouch/rel/rcouch/releases/rcouch-0.6.1-4-g08c4c81/rcouch -embedded -config /home/pi/rcouch/rel/rcouch/etc/app.config -args_file /home/pi/rcouch/rel/rcouch/etc/vm.args -- console
Root: /home/pi/rcouch/rel/rcouch
heart_beat_kill_pid = 18643
Erlang R15B03 (erts-5.9.3.1) [source] [async-threads:16] [kernel-poll:true]

08:40:18.732 [info] Application lager started on node 'rcouch@raspis.mol.gas.bog'
08:40:18.991 [notice] Deprecated lager_file_backend config detected, please consider updating it
couch core 1.3.0 is starting
rcouch rcouch-0.6.1-4-g08c4c81 (LogLevel=info) is starting.
08:40:19.764 [info] needed 252.193 ms to open new _users
08:40:20.011 [info] Application couch started on node 'rcouch@raspis.mol.gas.bog'
08:40:20.562 [info] HTTP API started on "http://0.0.0.0:5984"
08:40:21.327 [info] Application couch_httpd started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.429 [info] Application couch_index started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.532 [info] Application couch_mrview started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.535 [info] Application couch_changes started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.696 [info] needed 10.193 ms to open new _replicator
08:40:21.709 [info] Application couch_replicator started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.714 [info] Application vtree started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.718 [info] Application refuge_spatial started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.723 [info] Application couch_randomdoc started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.728 [info] Application couch_dbupdates started on node 'rcouch@raspis.mol.gas.bog'
08:40:21.732 [info] Application wpi started on node 'rcouch@raspis.mol.gas.bog'
gpio export pin 0(native 17) output[]
gpio export pin 1(native 18) output[]
gpio export pin 2(native 21) output[]
gpio export pin 3(native 22) output[]
gpio export pin 4(native 23) output[]
gpio export pin 5(native 24) output[]
gpio export pin 6(native 25) output[]
gpio export pin 7(native 4) output[]
load spi cmd issued[<<>>]
[<<"+----------+------+--------+------+-------+">>,
<<"| wiringPi | GPIO | Name | Mode | Value |">>,
<<"+----------+------+--------+------+-------+">>,
<<"| 0 | 17 | GPIO 0 | OUT | Low |">>,
<<"| 1 | 18 | GPIO 1 | OUT | Low |">>,
<<"| 2 | 21 | GPIO 2 | OUT | Low |">>,
<<"| 3 | 22 | GPIO 3 | OUT | Low |">>,
<<"| 4 | 23 | GPIO 4 | OUT | Low |">>,
<<"| 5 | 24 | GPIO 5 | OUT | Low |">>,
<<"| 6 | 25 | GPIO 6 | OUT | Low |">>,
<<"| 7 | 4 | GPIO 7 | OUT | Low |">>,
<<"| 8 | 0 | SDA | IN | High |">>,
<<"| 9 | 1 | SCL | IN | High |">>,
<<"| 10 | 8 | CE0 | ALT0 | High |">>,
<<"| 11 | 7 | CE1 | ALT0 | High |">>,
<<"| 12 | 10 | MOSI | ALT0 | High |">>,
<<"| 13 | 9 | MISO | ALT0 | High |">>,
<<"| 14 | 11 | SCLK | ALT0 | Low |">>,
<<"| 15 | 14 | TxD | ALT0 | High |">>,
<<"| 16 | 15 | RxD | ALT0 | High |">>,
<<"+----------+------+--------+------+-------+">>,<<>>]
[<<"Module Size Used by">>,
<<"spi_bcm2708 4816 0 ">>,
<<"spidev 5224 0 ">>,
<<"snd_bcm2835 16304 0 ">>,
<<"snd_pcm 77560 1 snd_bcm2835">>,
<<"snd_seq 53329 0 ">>,
<<"snd_timer 19998 2 snd_pcm,snd_seq">>,
<<"snd_seq_device 6438 1 snd_seq">>,
<<"snd 58447 5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device">>,
<<"snd_page_alloc 5145 1 snd_pcm">>,
<<"8192cu 490353 0 ">>,
<<"leds_gpio 2235 0 ">>,
<<"led_class 3562 1 leds_gpio">>,<<>>]
init gpio7_server V1.0
init gpio6_server V1.0
init gpio5_server V1.0
init gpio4_server V1.0
init gpio3_server V1.0
init gpio2_server V1.0
init gpio1_server V1.0
init gpio0_server V1.0
08:40:23.431 [info] Application gpio started on node 'rcouch@raspis.mol.gas.bog'
init spi0_server V1.01
Create I/O buffer Tab 57398
load spi driver and change user to p I hope!!!i[]
wiringPi:
Must be root to call wiringPiSetup().
(Did you forget sudo?)
heart: Fri Jun 7 08:40:24 2013: Erlang has closed.
heart: Fri Jun 7 08:40:24 2013: Would reboot. Terminating.

the GPIO works ok without root after exporting pins but SPI no good. Yes I did see the caveat, thank you for that!

It would be presumptuous for me to have any Ideas in the face of your knowledge on the subject, I would be happy for either (any) of your solutions. Then you will be able to boast the perfect combination with Gordon's great work.
In robotics (amongst others) I think the combination of OTP/Erlang and wpi makes a compelling case when you may have thousands of gpio/spi/iic processes being coordinated yay!
and hot code load!

keep up the good work, be glad to cooperate with you,

yours sincerely David Martin
(alias davidoccam, big_integer)

David Martin

klajo added a commit that referenced this issue Jun 19, 2013

Expose the setup, setup_gpio and setup_sys
This is a backwards-incompatible change while preparing for wiringPi
version 2 support.  This commit still supports wiringPi version 1.

Previously the setup function - wiringPiSetup() - was called when the
NIF was loaded.  The downside with that approach was that wpi had to
be run as root, although the nice thing was that a user wouldn't have
to worry about calling setup before accessing pins.

After some consideration I've (despite breaking backwards
compatibility) decided to remove the setup from the NIF loading and
let the user choose the method.

Hopefully fixes issue #4.
Owner

klajo commented Jun 19, 2013

Hi David,

I've made some changes that will hopefully improve the situation for you. See commit d29a866.

Gone is the implicit setup done by the load function in the NIF; instead you'll have to call one of the wpi:setup/0, wpi:setup_gpio/0 or (like in your case) wpi:setup_sys/0.

I haven't tested any SPI devices, only blinking a led on a pin so far. Please let me know how it works.

Kind Regards,
Klas

On 19/06/13 22:37, Klas Johansson wrote:

Hi David,

I've made some changes that will hopefully improve the situation for you. See commit d29a866 d29a866.

Gone is the implicit setup done by the load function in the NIF; instead you'll have to call one of the wpi:setup/0, wpi:setup_gpio/0 or (like in your case) wpi:setup_sys/0.

I haven't tested any SPI devices, only blinking a led on a pin so far. Please let me know how it works.

Kind Regards,
Klas


Reply to this email directly or view it on GitHub #4 (comment).

Thanks for your rapid response, I will test this ASAP and let you know results
David

David Martin

Owner

klajo commented Aug 4, 2013

Hi David,

Just out of curiosity, have you had the chance to try this one out yet?

Kind Regards,
Klas

Not yet Klas, I am looking at cubieboard2 with 96 gpio in a variety of forms.
I note also wiringPi new version and I am hesitant about proceeding with old version.
Living with sudo at present.I will test as soon as possible!
more thanks,
David Martin

On 04/08/13 22:29, Klas Johansson wrote:

Hi David,

Just out of curiosity, have you had the chance to try this one out yet?

Kind Regards,
Klas


Reply to this email directly or view it on GitHub #4 (comment).

David Martin

Owner

klajo commented Aug 5, 2013

Hi,

Ok, no worries, just curious. There are many boards in the same segment as raspberry pi to play with. :-)

Commit d29a866 supports WiringPi version 1 (but with exposed setup functions) and commit 33d0356 (the latest) supports WiringPi version 2. So depending on which one you choose you can try the new setup functions either on version 1 or version 2.

Regards,
Klas

Owner

klajo commented Sep 23, 2013

Let me know if you need any further assistance in this area.

Regards,
Klas

@klajo klajo closed this Sep 23, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment