Skip to content

Commit

Permalink
build: major rework of make targets
Browse files Browse the repository at this point in the history
Signed-off-by: Cameron Nemo <[email protected]>
  • Loading branch information
CameronNemo committed May 4, 2019
1 parent 0ff7fd9 commit 5cb1c3c
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 97 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*~
*.o
brillo
brillo.1
contrib/*brillo.policy
/build
96 changes: 56 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
PROG := brillo
DESC := Control the brightness of backlight and keyboard LED devices
VENDOR := com.gitlab.CameronNemo
VERSION := 1.4.7

GOMD2MAN ?= go-md2man
GROUP ?= video

SYSCONFDIR ?= /etc
AADIR ?= $(SYSCONFDIR)/apparmor.d
PREFIX ?= /usr
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man/man1
PKEDIR ?= $(PREFIX)/share/polkit-1/actions
UDEVRULESDIR ?= $(PREFIX)/lib/udev/rules.d

CFLAGS += -std=c99 -D_XOPEN_SOURCE=700 -pedantic -Wall -Werror -Wextra
LDLIBS += -lm
CFLAGS := $(CFLAGS) \
-std=c99 -D_XOPEN_SOURCE=700 -pedantic \
-Wall -Werror -Wextra \
-DPROG='"$(PROG)"' -DVERSION='"$(VERSION)"'

BINDIR=$(DESTDIR)$(PREFIX)/bin
MANDIR=$(DESTDIR)$(PREFIX)/share/man/man1
PKEDIR=$(DESTDIR)$(PREFIX)/share/polkit-1/actions
UDEVDIR=$(DESTDIR)$(PREFIX)/lib/udev/rules.d
AADIR=$(DESTDIR)/etc/apparmor.d
LDLIBS := $(LDLIBS) -lm

SRC = \
src/vlog.c \
Expand All @@ -28,52 +36,60 @@ SRC = \

OBJ = $(SRC:.c=.o)

$(PROG): $(OBJ)
build/$(PROG): $(OBJ)
mkdir -p build
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

install: $(PROG)
install -dZ -m 755 $(BINDIR)
install -DZ -m 755 ./$(PROG) -t $(BINDIR)
install.bin: build/$(PROG)
install -Dm 0755 -t $(DESTDIR)$(BINDIR) $^

build/$(VENDOR).$(PROG): contrib/apparmor.in
sed -e 's|@vendor@|$(VENDOR)|g' -e 's|@prog@|$(PROG)|g' $^ > $@

install.apparmor: build/$(VENDOR).$(PROG)
install -d $(DESTDIR)$(AADIR)
install -m 0640 -t $(DESTDIR)$(AADIR) $^

build/$(PROG).1: doc/man/brillo.1.md
$(GOMD2MAN) -in $^ -out $@

install.man: build/$(PROG).1
install -d $(DESTDIR)$(MANDIR)
install -m 0644 -t $(DESTDIR)$(MANDIR) $^

GOMD2MAN := $(shell which go-md2man 2>/dev/null)
build/92-$(VENDOR).$(PROG).rules: contrib/udev.in
sed -e 's|@group@|$(GROUP)|g' $^ > $@

$(PROG).1:
ifndef GOMD2MAN
$(error "go-md2man not available")
else
$(GOMD2MAN) -in doc/man/brillo.1.md -out $@
endif
install.udev: build/92-$(VENDOR).$(PROG).rules
install -d $(DESTDIR)$(UDEVRULESDIR)
install -m 00644 -t $(DESTDIR)$(UDEVRULESDIR) $^

man: $(PROG).1
install.common: install.apparmor install.man install.udev

contrib/$(VENDOR).$(PROG).policy: contrib/polkit.in
sed -e 's|@bindir@|$(PREFIX)/bin|g' \
install: install.bin install.common

install.bin.setgid: build/$(PROG)
install -d $(DESTDIR)$(BINDIR)
install -m 2755 -g $(GROUP) -t $(DESTDIR)$(BINDIR) $^

install.setgid: install.bin.setgid install.common

build/$(VENDOR).$(PROG).policy: contrib/polkit.in
sed -e 's|@bindir@|$(BINDIR)|g' \
-e 's|@prog@|$(PROG)|g' \
-e 's|@vendor@|$(VENDOR)|g' \
-e 's|@desc@|$(DESC)|g' \
$^ > $@

polkit: contrib/$(VENDOR).$(PROG).policy

dist: $(PROG) man polkit

install-dist: install dist
install -dZ -m 755 $(MANDIR) $(PKEDIR) $(UDEVDIR) $(AADIR) $(AADIR)/local
install -DZ -m 644 $(PROG).1 -t $(MANDIR)
install -DZ -m 644 contrib/$(VENDOR).$(PROG).policy -t $(PKEDIR)
install -DZ -m 644 contrib/90-brillo.rules -t $(UDEVDIR)
install -DZ -m 644 contrib/bin.brillo -t $(AADIR)
install -DZ -m 644 contrib/local/bin.brillo -t $(AADIR)/local
install.polkit: build/$(VENDOR).$(PROG).policy
install -d $(DESTDIR)$(PKEDIR)
install -m 0640 -t $(DESTDIR)$(PKEDIR) $^

uninstall:
rm -f $(BINDIR)/$(PROG)
dist:

uninstall-dist:
rm -f $(MANDIR)/$(PROG).1
rm -f $(PKEDIR)/$(VENDOR).$(PROG).policy
rm -f $(UDEVDIR)/90-brillo.rules
install-dist: install.bin install.common install.polkit

clean:
rm -vfr *~ $(PROG) $(PROG).1 $(OBJ) contrib/$(VENDOR).$(PROG).policy
rm -rfv -- *~ $(OBJ) build

.PHONY: install uninstall polkit dist install-dist uninstall-dist man clean
.PHONY: install.bin install.apparmor install.man install.udev install.common install install.setgid install.polkit dist install-dist clean
59 changes: 44 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,46 @@ For detailed usage, please refer to the [man page](doc/man/brillo.1.md).
Installation
------------

To build the binary `brillo` and install it:
Building the manpage requires `go-md2man`.

make
sudo make install
To build and install the binary, manpage, and udev rule:

A full install, including the man page, polkit action, udev rule, and apparmor
profile in addition to the binary:
```
$ make
# make install
```

make dist
sudo make install-dist
Users in the configured group (`video` by default) will be able to adjust
the brightness.
An alternative group can be configured using the `GROUP` variable:

> Note: the Makefile uses the `PREFIX` and `DESTDIR` variables to compose the
> installation path. If set, they should be consistent for each make target.
```
# make install GROUP=_brillo
```

Unpriveleged Access
If you would prefer to allow any user, regardless of group, to change the
brightness, you may install the binary as `setgid`:

```
# make install.setgid GROUP=_brillo
```

To additionally install the apparmor profile:

```
# make install.apparmor
```

To additionally install the polkit rule:

```
# make install.polkit
```

> Note: the `install*` targets use the `PREFIX` and `DESTDIR` variables to
> compose the installation path and generate configuration files.
Unprivileged Access
-------------------

### polkit
Expand All @@ -40,17 +65,21 @@ Active sessions can invoke `brillo` via `pkexec` to escalate priveleges.

Examples:

pkexec /usr/bin/brillo -O
pkexec /usr/bin/brillo -A 5
```
$ pkexec /usr/bin/brillo -O
$ pkexec /usr/bin/brillo -A 5
```

> Note: this requires polkitd and (e)logind or ConsoleKit.
### udev

`brillo`'s udev rule grants necessary permissions to the `video` group for
backlight devices and to the `input` group for keyboard LED devices.
backlight and keyboard LED devices.

Any user in these groups can modify values directly.
Users in these groups can modify values directly.
Alternately, the binary may be installed as `setgid` so that any user may
change the brightness.

> Note: in this mode, stored brightness and minimum cap files will be in
> the user's cache home (typically "~/.cache").
Expand All @@ -70,7 +99,7 @@ correction for external monitors.
Copyright
---------

Copyright (C) 2018 Cameron Nemo, 2014 Fredrik Haikarainen
Copyright (C) 2018-2019 Cameron Nemo, 2014 Fredrik Haikarainen

This is free software, see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
4 changes: 0 additions & 4 deletions contrib/90-brillo.rules

This file was deleted.

25 changes: 25 additions & 0 deletions contrib/apparmor.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# vim:syntax=apparmor

#include <tunables/global>

profile @vendor@.@prog@ /{,usr/}{,local/}bin/@prog@ {
#include <abstractions/base>

capability dac_override,
capability dac_read_search,

/{,usr/}{,local/}bin/@prog@ mixr,
/var/cache/@prog@ rw,
/var/cache/@prog@/** rwk,

# unpriveleged mode
owner @{HOME}/.cache/@prog@ rw,
owner @{HOME}/.cache/@prog@/** rwk,

/sys/class/{backlight,leds}/ r,
/sys/devices/**/brightness rwk,
/sys/devices/**/max_brightness r,

# Site-specific additions and overrides. See local/README for details.
#include if exists <local/@vendor@.@prog@>
}
28 changes: 0 additions & 28 deletions contrib/bin.brillo

This file was deleted.

4 changes: 4 additions & 0 deletions contrib/udev.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chgrp @group@ /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="leds", RUN+="/bin/chgrp @group@ /sys/class/leds/%k/brightness"
ACTION=="add", SUBSYSTEM=="leds", RUN+="/bin/chmod g+w /sys/class/leds/%k/brightness"
4 changes: 2 additions & 2 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool info_print(LIGHT_OP_MODE op, char *prefix, bool exec)
**/
void info_print_version()
{
printf("%s %s\n", LIGHT_PROG, LIGHT_VER);
printf("%s %s\n", PROG, VERSION);
}

/**
Expand All @@ -84,6 +84,6 @@ void info_print_version()
**/
void *info_print_help()
{
printf("Usage: %s [options] [operation [value]]\n", LIGHT_PROG);
printf("Usage: %s [options] [operation [value]]\n", PROG);
return NULL;
}
4 changes: 2 additions & 2 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ static char *init_cache(const char * const tgt)

if ((geteuid() == 0 && (env = "/var/cache")) ||
(env = getenv("XDG_CACHE_HOME")))
dirfmt = "%s/" LIGHT_PROG;
dirfmt = "%s/" PROG;
else if ((env = getenv("HOME")))
dirfmt = "%s/.cache/" LIGHT_PROG;
dirfmt = "%s/.cache/" PROG;

if (!env) {
vlog_err("XDG/HOME env vars not set, failed to init cache");
Expand Down
3 changes: 0 additions & 3 deletions src/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include <stdlib.h>
#include <stdint.h>

#define LIGHT_PROG "brillo"
#define LIGHT_VER "1.4.6"

typedef enum LIGHT_FIELD {
LIGHT_FIELD_UNSET = 0,
LIGHT_BRIGHTNESS,
Expand Down

0 comments on commit 5cb1c3c

Please sign in to comment.