1
0
forked from pool/brightnessctl

Accepting request 769065 from utilities

Add support for using logind mode for recent distributions (forwarded request 768528 from dancermak)

OBS-URL: https://build.opensuse.org/request/show/769065
OBS-URL: https://build.opensuse.org/package/show/openSUSE:Factory/brightnessctl?expand=0&rev=3
This commit is contained in:
Dominique Leuenberger 2020-01-31 22:58:05 +00:00 committed by Git OBS Bridge
commit 4c47c35102
6 changed files with 389 additions and 5 deletions

View File

@ -0,0 +1,26 @@
From 5bee979cd84347369027b45b5df5d917b3231ab6 Mon Sep 17 00:00:00 2001
From: Hummer12007 <hilobakho@gmail.com>
Date: Fri, 2 Aug 2019 15:23:38 +0300
Subject: [PATCH 1/4] Fixed inconsistency in README (thanks @AJGQ)
Closes #28
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 92b65b0..0042bb4 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,7 @@ Modifying brightness requires write permissions for device files. `brightnessctl
2) installing `brightnessctl` as a suid binary.
-The behavior is controlled by the `INSTALL_UDEV_RULES` flag (setting it to `1` installs the udev rules, `0` is the default value).
+The behavior is controlled by the `INSTALL_UDEV_RULES` flag (setting it to `1` installs the udev rules, it is the default value).
## Usage
```
--
2.25.0

View File

@ -0,0 +1,155 @@
From d8dfd8ed49762922e9ff8ebd19b2607fc6cd60aa Mon Sep 17 00:00:00 2001
From: Antoine Damhet <antoine.damhet@lse.epita.fr>
Date: Tue, 10 Sep 2019 00:24:05 +0200
Subject: [PATCH 2/4] Support the new `SetBrightness` logind API
This API from `org.freedesktop.login1.Session` allows an unprivileged
user with an active session to change its own backlight.
Systemd >= v243 is needed.
Signed-off-by: Antoine Damhet <antoine.damhet@lse.epita.fr>
---
Makefile | 6 ++++++
README.md | 6 +++++-
brightnessctl.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
index f324efd..22ff475 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,12 @@ INSTALL_UDEV_RULES = 1
INSTALL_UDEV_1 = install_udev_rules
UDEVDIR ?= /lib/udev/rules.d
+ifdef ENABLE_SYSTEMD
+ CFLAGS += ${shell pkg-config --cflags libsystemd}
+ LDLIBS += ${shell pkg-config --libs libsystemd}
+ CPPFLAGS += -DENABLE_SYSTEMD
+endif
+
MODE_0 = 4711
MODE_1 = 0755
MODE = ${MODE_${INSTALL_UDEV_RULES}}
diff --git a/README.md b/README.md
index 0042bb4..b57d3df 100644
--- a/README.md
+++ b/README.md
@@ -17,14 +17,18 @@ One can build and install the program using `make install`. Consult the Makefile
## Permissions
-Modifying brightness requires write permissions for device files. `brightnessctl` accomplishes this (without using `sudo`/`su`/etc.) by either of the following means:
+Modifying brightness requires write permissions for device files or systemd support. `brightnessctl` accomplishes this (without using `sudo`/`su`/etc.) by either of the following means:
1) installing relevant udev rules to add permissions to backlight class devices for users in `video` and leds for users in `input`. (done by default)
2) installing `brightnessctl` as a suid binary.
+3) using the `systemd-logind` API.
+
The behavior is controlled by the `INSTALL_UDEV_RULES` flag (setting it to `1` installs the udev rules, it is the default value).
+The systemd support (since v243) is controlled by the `ENABLE_SYSTEMD` flag (in that case it is recommended to set `INSTALL_UDEV_RULES` to 0).
+
## Usage
```
Usage: brightnessctl [options] [operation] [value]
diff --git a/brightnessctl.c b/brightnessctl.c
index eb3ce3c..3fec6ec 100644
--- a/brightnessctl.c
+++ b/brightnessctl.c
@@ -14,6 +14,10 @@
#include <string.h>
#include <math.h>
+#ifdef ENABLE_SYSTEMD
+# include <systemd/sd-bus.h>
+#endif
+
static char *path = "/sys/class";
static char *classes[] = { "backlight", "leds", NULL };
@@ -101,7 +105,12 @@ int main(int argc, char **argv) {
struct device *devs[255];
struct device *dev;
struct utsname name;
- char *dev_name, *file_path, *sys_run_dir;
+ char *dev_name, *sys_run_dir;
+
+#ifndef ENABLE_SYSTEMD
+ char *file_path;
+#endif
+
int n, c, phelp = 0;
if (uname(&name))
fail("Unable to determine current OS. Exiting!\n");
@@ -194,6 +203,8 @@ int main(int argc, char **argv) {
fail("Invalid value given");
if (!(dev = find_device(devs, dev_name)))
fail("Device '%s' not found.\n", dev_name);
+
+#ifndef ENABLE_SYSTEMD
if ((p.operation == SET || p.restore) && !p.pretend && geteuid()) {
errno = 0;
file_path = cat_with('/', path, dev->class, dev->id, "brightness");
@@ -204,6 +215,8 @@ int main(int argc, char **argv) {
}
free(file_path);
}
+#endif
+
if ((sys_run_dir = getenv("XDG_RUNTIME_DIR")))
run_dir = dir_child(sys_run_dir, "brightnessctl");
if (p.save)
@@ -325,6 +338,37 @@ apply:
d->curr_brightness = new;
}
+#ifdef ENABLE_SYSTEMD
+
+bool write_device(struct device *d) {
+ sd_bus *bus = NULL;
+ int r = sd_bus_default_system(&bus);
+ if (r < 0) {
+ fprintf(stderr, "Can't connect to system bus: %s\n", strerror(-r));
+ return false;
+ }
+
+ r = sd_bus_call_method(bus,
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1/session/auto",
+ "org.freedesktop.login1.Session",
+ "SetBrightness",
+ NULL,
+ NULL,
+ "ssu",
+ d->class,
+ d->id,
+ d->curr_brightness);
+ if (r < 0)
+ fprintf(stderr, "Failed to set brightness: %s\n", strerror(-r));
+
+ sd_bus_unref(bus);
+
+ return r >= 0;
+}
+
+#else
+
bool write_device(struct device *d) {
FILE *f;
char c[16];
@@ -348,6 +392,8 @@ fail:
return !errno;
}
+#endif
+
bool read_device(struct device *d, char *class, char *id) {
DIR *dirp;
FILE *f;
--
2.25.0

View File

@ -0,0 +1,115 @@
From 658d5d058cb03a94269f8eec5b7ac4f1e8c43880 Mon Sep 17 00:00:00 2001
From: Antoine Damhet <antoine.damhet@lse.epita.fr>
Date: Tue, 10 Sep 2019 08:52:59 +0200
Subject: [PATCH 3/4] Make the use of `SetBrightness` dynamic
It will only be used if the user do not have the permission to directly
write to the backlight device.
Signed-off-by: Antoine Damhet <antoine.damhet@lse.epita.fr>
---
brightnessctl.c | 31 +++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)
diff --git a/brightnessctl.c b/brightnessctl.c
index 3fec6ec..576e591 100644
--- a/brightnessctl.c
+++ b/brightnessctl.c
@@ -38,7 +38,7 @@ static char *class_path(char *);
static void apply_value(struct device *, struct value *);
static int apply_operation(struct device *, enum operation, struct value *);
static bool parse_value(struct value *, char *);
-static bool write_device(struct device *);
+static bool do_write_device(struct device *);
static bool read_device(struct device *, char *, char *);
static int read_class(struct device **, char *);
static int read_devices(struct device **);
@@ -50,6 +50,10 @@ static bool restore_device_data(struct device *);
static bool ensure_dir(char *);
#define ensure_run_dir() ensure_dir(run_dir)
+#ifdef ENABLE_SYSTEMD
+static bool logind_set_brightness(struct device *);
+#endif
+
struct device {
char *class;
char *id;
@@ -101,16 +105,13 @@ static const struct option options[] = {
{NULL,}
};
+static bool (*write_device)(struct device *) = do_write_device;
+
int main(int argc, char **argv) {
struct device *devs[255];
struct device *dev;
struct utsname name;
- char *dev_name, *sys_run_dir;
-
-#ifndef ENABLE_SYSTEMD
- char *file_path;
-#endif
-
+ char *dev_name, *file_path, *sys_run_dir;
int n, c, phelp = 0;
if (uname(&name))
fail("Unable to determine current OS. Exiting!\n");
@@ -203,20 +204,20 @@ int main(int argc, char **argv) {
fail("Invalid value given");
if (!(dev = find_device(devs, dev_name)))
fail("Device '%s' not found.\n", dev_name);
-
-#ifndef ENABLE_SYSTEMD
if ((p.operation == SET || p.restore) && !p.pretend && geteuid()) {
errno = 0;
file_path = cat_with('/', path, dev->class, dev->id, "brightness");
if (access(file_path, W_OK)) {
+#ifdef ENABLE_SYSTEMD
+ write_device = logind_set_brightness;
+#else
perror("Can't modify brightness");
fail("\nYou should run this program with root privileges.\n"
"Alternatively, get write permissions for device files.\n");
+#endif
}
free(file_path);
}
-#endif
-
if ((sys_run_dir = getenv("XDG_RUNTIME_DIR")))
run_dir = dir_child(sys_run_dir, "brightnessctl");
if (p.save)
@@ -340,7 +341,7 @@ apply:
#ifdef ENABLE_SYSTEMD
-bool write_device(struct device *d) {
+bool logind_set_brightness(struct device *d) {
sd_bus *bus = NULL;
int r = sd_bus_default_system(&bus);
if (r < 0) {
@@ -367,9 +368,9 @@ bool write_device(struct device *d) {
return r >= 0;
}
-#else
+#endif
-bool write_device(struct device *d) {
+bool do_write_device(struct device *d) {
FILE *f;
char c[16];
size_t s = sprintf(c, "%u", d->curr_brightness);
@@ -392,8 +393,6 @@ fail:
return !errno;
}
-#endif
-
bool read_device(struct device *d, char *class, char *id) {
DIR *dirp;
FILE *f;
--
2.25.0

View File

@ -0,0 +1,41 @@
From 4e815152fb366972ca24ce17d3d25f337a3b01b9 Mon Sep 17 00:00:00 2001
From: Christian Kellner <christian@kellner.me>
Date: Sun, 26 Jan 2020 13:15:40 +0100
Subject: [PATCH 4/4] Use non-suid permissions when logind is used
When systemd-logind and its D-Bus API is used to actual change the
brightness of devices the binary does not have to be suid, which is
desired for security reasons.
---
Makefile | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
index 22ff475..fcd746a 100644
--- a/Makefile
+++ b/Makefile
@@ -11,16 +11,17 @@ INSTALL_UDEV_RULES = 1
INSTALL_UDEV_1 = install_udev_rules
UDEVDIR ?= /lib/udev/rules.d
+MODE_0 = 4711
+MODE_1 = 0755
+MODE = ${MODE_${INSTALL_UDEV_RULES}}
+
ifdef ENABLE_SYSTEMD
CFLAGS += ${shell pkg-config --cflags libsystemd}
LDLIBS += ${shell pkg-config --libs libsystemd}
CPPFLAGS += -DENABLE_SYSTEMD
+ MODE = 0755
endif
-MODE_0 = 4711
-MODE_1 = 0755
-MODE = ${MODE_${INSTALL_UDEV_RULES}}
-
all: brightnessctl brightnessctl.1
install: all ${INSTALL_UDEV_${INSTALL_UDEV_RULES}}
--
2.25.0

View File

@ -1,3 +1,18 @@
-------------------------------------------------------------------
Thu Jan 30 08:52:07 UTC 2020 - Dan Čermák <dcermak@suse.com>
- Add support for using logind mode for recent distributions
cherry pick patches from upstream:
* 0001-Fixed-inconsistency-in-README-thanks-AJGQ.patch
* 0002-Support-the-new-SetBrightness-logind-API.patch
* 0003-Make-the-use-of-SetBrightness-dynamic.patch
* 0004-Use-non-suid-permissions-when-logind-is-used.patch
-------------------------------------------------------------------
Thu Oct 17 14:57:05 UTC 2019 - Richard Brown <rbrown@suse.com>
- Remove obsolete Groups tag (fate#326485)
-------------------------------------------------------------------
Wed May 1 13:17:04 UTC 2019 - Jan Engelhardt <jengelh@inai.de>

View File

@ -1,7 +1,7 @@
#
# spec file for package brightnessctl
#
# Copyright (c) 2019 SUSE LINUX GmbH, Nuernberg, Germany.
# Copyright (c) 2020 SUSE LLC
# Copyright (c) 2019 R Tyler Croy <rtyler@brokenco.de>
# Copyright (c) 2016 Fabio Alessandro Locati <fale@fedoraproject.org>
#
@ -18,37 +18,69 @@
#
# use systemd-logind instead of udev for systemd >= 243
%if 0%{?sle_version} >= 150200 || 0%{?suse_version} > 1500
%global use_logind 1
%else
%global use_logind 0
%endif
Name: brightnessctl
Version: 0.4
Release: 0
Summary: Tool to read and control device brightness
License: MIT
Group: System/Management
URL: https://github.com/Hummer12007/%{name}
Source: https://github.com/Hummer12007/brightnessctl/archive/%{version}.tar.gz
Source: %{URL}/archive/%{version}.tar.gz
# required to resolve cherry pick conflicts
# https://github.com/Hummer12007/brightnessctl/commit/aa6a71cd8206992a64269239f038dbf5f516e54a
Patch0: 0001-Fixed-inconsistency-in-README-thanks-AJGQ.patch
# https://github.com/Hummer12007/brightnessctl/pull/33
Patch1: 0002-Support-the-new-SetBrightness-logind-API.patch
Patch2: 0003-Make-the-use-of-SetBrightness-dynamic.patch
# https://github.com/Hummer12007/brightnessctl/pull/41
Patch3: 0004-Use-non-suid-permissions-when-logind-is-used.patch
BuildRequires: gcc
BuildRequires: make
%if %{use_logind}
BuildRequires: pkgconfig(libsystemd)
Requires: systemd >= 243
%else
BuildRequires: systemd-rpm-macros
%endif
%description
A utility to read and control the display brightness.
%prep
%autosetup
%autosetup -N
%if %{use_logind}
%autopatch -p1
%endif
%build
export CFLAGS="%{optflags}"
%if %{use_logind}
export ENABLE_SYSTEMD=1
%{set_build_flags}
%else
export MODE="4755"
export CFLAGS="%{optflags}"
%endif
%make_build
%install
%if %{use_logind}
%make_install INSTALL_UDEV_RULES=0 ENABLE_SYSTEMD=1 PREFIX=%{_prefix}
%else
%make_install UDEVDIR=%{_udevrulesdir}
%endif
%files
%doc README.md
%license LICENSE
%{_bindir}/%{name}
%if ! %{use_logind}
%{_udevrulesdir}/90-brightnessctl.rules
%endif
%{_mandir}/man1/brightnessctl.1%{?ext_man}
%changelog