-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating RGBMatrix with Python bindings while running as root hijacks/disables file system IO #1170
Comments
print(os.getcwd())
print(os.path.abspath("./fonts/7x13.bdf")) ...and this confirms that the same path/file is being requested regardless of whether I'm running with as a user, with When I change the LoadFont line to font.LoadFont('/home/pi/poolmonitor/display_driver/fonts/7x13.bdf') ...running as In what way is this issue off-topic? |
The text you have quoted does not relate to this issue; it is displayed only when I run not as root, which I am doing only to demonstrate that the issue is not related to any basic configuration -- the program works correctly when not run as root (despite the message about lower performance that you have quoted from, which I assume is true, which is why I want to run as root, which is why this issue is a problem). Perhaps I am not being clear; let me try to be more succinct: When I attempt to run my Python program as root as suggested (
I would like to investigate whether this possible difference between root and non-root is the cause of the SystemError. What do you suggest I do to check this beyond confirming that the SystemError still occurs when I use an absolute path to the font as I described in my most recent comment?
Sounds like an avenue that can be explored. What flags might cause a SystemError in this repo's code as root, but work properly as not-root?
I would also be interested in exploring this possibility. What environment variables might cause a SystemError in this repo's code as root, but work properly as not-root? Here are my environment variables both as root and not as root:
The things I am running are the Python bindings defined in this repository, and the documentation suggests that I should run as root (which is the condition under which the error occurs). So I am encountering an error when running the code from this repository in the manner suggested by this repository. Where do you suggest is an appropriate forum for me to address this issue? |
I've distilled this issue to a smaller set of repro steps. Here is from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
matrix = RGBMatrix(options=options)
font = graphics.Font()
font.LoadFont('/home/pi/poolmonitor/display_driver/fonts/7x13.bdf')
print('Exited successfully') That script produces the following results:
Summary: But, now I comment out just one line: from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
#matrix = RGBMatrix(options=options)
font = graphics.Font()
font.LoadFont('/home/pi/poolmonitor/display_driver/fonts/7x13.bdf')
print('Exited successfully') Now, everything works fine:
So, it seems like there must be some kind of static initialization when an from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
font = graphics.Font()
font.LoadFont('/home/pi/poolmonitor/display_driver/fonts/7x13.bdf')
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
matrix = RGBMatrix(options=options)
print('Exited successfully') And indeed, this works properly:
So, I'm retitling this bug to better reflect the unexpected behavior: it seems that fonts cannot be created after creating an RGBMatrix, but this deficiency is only present when running as root. Very weird. |
It looks like the original behavior observed is actually a special case of a general problem: this library seems to completely hijack/disable all IO once an RGBMatrix is created if run as root. Consider hijack.py: from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
print('Writing test1')
with open('test1.txt', 'w') as f:
f.write('ok')
print('Wrote test1')
matrix = RGBMatrix(options=options)
print('Writing test2')
with open('test2.txt', 'w') as f:
f.write('ok')
print('Wrote test2') The result of running this file as a normal user and as root is:
Creating an RGBMatrix while running as root appears to disable all ability to interact with the file system. WTF? |
By default, the matrix initialization drops root privileges to So if you don't want to have the privileges dropped or to the priv dropping yourself to another user, set the |
Thanks, that makes sense and I see it is intended and reasonable, though I wouldn't say that a demotion in privileges as a result of constructing an object (but only when root) is the behavior most users would expect. For anyone else who finds this issue in the future, run import os
import pwd
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
print('User before RGBMatrix: ' + pwd.getpwuid(os.getuid()).pw_name)
matrix = RGBMatrix(options=options)
print('User after RGBMatrix: ' + pwd.getpwuid(os.getuid()).pw_name) Result:
The drop_privileges option is defined here and exposed to Python here (note that the preceding links may point to an older code version). To change behavior, modify the example above to: import os
import pwd
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
options.drop_privileges = False
print('User before RGBMatrix: ' + pwd.getpwuid(os.getuid()).pw_name)
matrix = RGBMatrix(options=options)
print('User after RGBMatrix: ' + pwd.getpwuid(os.getuid()).pw_name) Result:
|
This is primarily in response to #1170. Also, this PR fixes a few minor grammar issues in the documentation.
I have a basic Python program modeled off of the
runtext.py
binding example, and I've copied one of the fonts (7x13.bdf
) into afonts
subfolder relative to my script (display_driver.py
). It works when the font is owned bypi
:(the RGB matrix is displaying text properly until I hit ctrl-c)
However, my problem is that LoadFont has an error when I run as root:
I thought this was likely due to the font file being owned by
pi
, so I tried changing ownership toroot
:...however, as you can see above, changing ownership to root breaks even what was working previously. How can I fix this LoadFont error?
Here is the content of
display_driver.py
:The text was updated successfully, but these errors were encountered: