Run part of Qt program as root? (setting ACPI_video0)
All, I wrote a small Qt app to adjust the display backlight on my laptop: https://paste.opensuse.org/pastes/a67b89db433f It will be a systray app like kmix. As part of the program I write to /sys/class/backlight/ACPI_video0/brightness which is owned by root with 0644 permissions. I have a bash script that does it, but simply using sudo is enough, but how to do the same thing in Qt? I guess I could wrap the write in a system() call using sudo -- but is there a better way to do this? I'd rather not make the executable setuid-root, or something wonky like that. Is there a way to configure the /sys/class/backlight/... file to be owned by the video or wheel group and make users a member of that group? Maybe a new backlight group? If I run it as root, all is well, but if I run as me, it fails, e.g. root: # ./nvidia-bl-qt3 setting backlight: 5 setting backlight: 4 setting backlight: 3 setting backlight: 2 setting backlight: 1 setting backlight: 2 setting backlight: 3 setting backlight: 4 setting backlight: 5 setting backlight: 6 setting backlight: 7 setting backlight: 8 setting backlight: 9 setting backlight: 10 me: $ ./nvidia-bl-qt3 setting backlight: 8 error: file open failed '/sys/class/backlight/acpi_video0/brightness' setting backlight: 9 error: file open failed '/sys/class/backlight/acpi_video0/brightness' setting backlight: 10 error: file open failed '/sys/class/backlight/acpi_video0/brightness' setting backlight: 11 error: file open failed '/sys/class/backlight/acpi_video0/brightness' setting backlight: 12 error: file open failed '/sys/class/backlight/acpi_video0/brightness' setting backlight: 11 error: file open failed '/sys/class/backlight/acpi_video0/brightness' setting backlight: 10 error: file open failed '/sys/class/backlight/acpi_video0/brightness' This is Qt3 (as you could guess from the name), but if you have a way in the current Qt(5/6), I'll dig backwards and find out what the prior equivalent was. Or I could just rewrite in Qt5. === I can't susepaste from TW with my nick and key from 15.4? === Also, why can't I susepaste from TW with my key anymore? I can do it anonymous, but if I pass my nick (-n) and key (-k) copied from 15.4 it always fails? -- David C. Rankin, J.D.,P.E.
On Tue, Jul 23, 2024 at 9:13 AM David C. Rankin <drankinatty@gmail.com> wrote:
All,
I wrote a small Qt app to adjust the display backlight on my laptop:
https://paste.opensuse.org/pastes/a67b89db433f
It will be a systray app like kmix. As part of the program I write to /sys/class/backlight/ACPI_video0/brightness which is owned by root with 0644 permissions.
I have a bash script that does it, but simply using sudo is enough, but how to do the same thing in Qt?
Just use systemd-logind D-Bus API, like ``` busctl call org.freedesktop.login1 /org/freedesktop/login1/session/auto org.freedesktop.login1.Session SetBrightness "ssu" "backlight" "amdgpu_bl0" 4711 ``` or appropriate bindings for the language you are using. See https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.logi... for details.
On 7/23/24 1:46 AM, Andrei Borzenkov wrote:
Just use systemd-logind D-Bus API, like
``` busctl call org.freedesktop.login1 /org/freedesktop/login1/session/auto org.freedesktop.login1.Session SetBrightness "ssu" "backlight" "amdgpu_bl0" 4711 ```
or appropriate bindings for the language you are using.
See
https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.logi...
for details.
Thank you Andrei, Very nice, and I was about to go messing with kdesu (the -f file option looked nice, and the use in the .desktop file was attractive). Time to learn systemd-logind. The other option I was considering was trying to use Linux capabilities and perhaps the CAP_SYS_RAWIO capability. I'll have to learn something new either way. This is just plain old C++ and Qt Designer -- that I end up fighting with to not overwrite my damn files (I know that's what it's supposed to do, but after I tweak the header and source file generated from the ui by the moc -- I just want it to leave the files alone ... :) Designer is too limited to know about other classes I want to integrate with the dialog to use the slider/spinbox value change signal connected to the custom set_backlight slot of another widget. No doubt I need to refactor to eliminate that problem and just inherit from the Designer class. More gui practice needed. -- David C. Rankin, J.D.,P.E.
On 7/23/24 1:46 AM, Andrei Borzenkov wrote:
Just use systemd-logind D-Bus API, like
``` busctl call org.freedesktop.login1 /org/freedesktop/login1/session/auto org.freedesktop.login1.Session SetBrightness "ssu" "backlight" "amdgpu_bl0" 4711 ```
or appropriate bindings for the language you are using.
See
https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.logi...
for details.
Yes! That was so damn simple and eliminates the sudo/su user issue completely, e.g. int displayBacklight::set_backlight ( int b ) { char buf[256] = ""; qWarning ("setting backlight: %d", b); sprintf (buf, "busctl call org.freedesktop.login1 " "/org/freedesktop/login1/session/auto " "org.freedesktop.login1.Session SetBrightness " "\"ssu\" \"backlight\" \"acpi_video0\" %d", b); return system (buf); } Works like a champ for the user. spinbox ValueChanged signal calls the set_backlight slot and a simple mousewheel scroll to set the nvidia backlight. Now to make systray the parent main window and have it display on mouseover and close on focus-out. Thank you for pointing me to this. I wouldn't have found it in a month of Sundays otherwise. -- David C. Rankin, J.D.,P.E.
On 7/23/24 1:46 AM, Andrei Borzenkov wrote:
busctl call org.freedesktop.login1 /org/freedesktop/login1/session/auto org.freedesktop.login1.Session SetBrightness "ssu" "backlight" "amdgpu_bl0" 4711 ```
or appropriate bindings for the language you are using.
See
https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.logi...
for details.
Andrei, Replacing the system ("busctl ...") with an implementation through dbus/dbus.h, after I get the DBusMessage pointer for the SetBrightness method, how do I then form the message with the data parameters, e.g. "ssu backlight acpi_video0 10" (or whatever the backlight value is) Is this a separate dbus call? The dbus_message_new_method_call() method only takes the method name without additional parameter information. If I attempt to include the parameter info, it throws the error _dbus_check_is_valid_member (method)? I can form the message with: dbus_msg = ::dbus_message_new_method_call ("org.freedesktop.login1", "/org/freedesktop/login1/session/auto", "org.freedesktop.login1.Session", "SetBrightness"); but what do I do with the dbus_message_new_empty_header() returned by that call? https://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga98ddc824... How do I finish sending the "ssu backlight acpi_video0 10" argument info? The doxygen documentation is a bit light on explanation. -- David C. Rankin, J.D.,P.E.
On 7/23/24 1:46 AM, Andrei Borzenkov wrote:
Just use systemd-logind D-Bus API, like
Disregard appending arguments question - got it. https://paste.opensuse.org/7678f5d3b5c5 Thanks! -- David C. Rankin, J.D.,P.E.
participants (2)
-
Andrei Borzenkov
-
David C. Rankin