Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit 6f416c5

Browse files
committed
Initial commit
0 parents  commit 6f416c5

File tree

8 files changed

+293
-0
lines changed

8 files changed

+293
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
auto-inhibit.1

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2018 James Reed
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
VERSION := $(shell git describe --dirty --tags --always || cat VERSION)
2+
3+
PREFIX ?= /usr/local
4+
BINPREFIX ?= $(PREFIX)/bin
5+
MANPREFIX ?= $(PREFIX)/share/man
6+
7+
MANPAGE = auto-inhibit.1
8+
9+
all: $(MANPAGE)
10+
11+
$(MANPAGE): man/$(MANPAGE).pod
12+
pod2man -n=auto-inhibit -c=auto-inhibit -r=$(VERSION) $< $(MANPAGE)
13+
14+
install:
15+
mkdir -p $(DESTDIR)$(BINPREFIX)
16+
cp -p auto-inhibit $(DESTDIR)$(BINPREFIX)
17+
mkdir -p $(DESTDIR)/etc
18+
cp -p auto-inhibit.conf $(DESTDIR)/etc
19+
mkdir -p $(DESTDIR)$(MANPREFIX)/man1
20+
cp -p $(MANPAGE) $(DESTDIR)$(MANPREFIX)/man1
21+
22+
uninstall:
23+
rm -f $(DESTDIR)$(BINPREFIX)/auto-inhibit
24+
rm -f $(DESTDIR)/etc/auto-inhibit.conf
25+
rm -f $(DESTDIR)$(MANPREFIX)/man1/iniq.1
26+
27+
clean:
28+
rm -f $(MANPAGE)
29+
30+
test:
31+
$(MAKE) -C test
32+
33+
.PHONY: all install uninstall clean test

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# auto-inhibit
2+
3+
**auto-inhibit** manages symbolic links to itself named after programs to be run
4+
with an inhibition lock via **systemd-inhibit**.
5+
6+
## Usage
7+
8+
```
9+
usage: auto-inhibit [options] [command]
10+
11+
options:
12+
-h Show help message
13+
-d DIR Operate on DIR
14+
15+
commands:
16+
list List symlinks
17+
generate Create symlinks
18+
check Check symlinks
19+
remove Remove symlinks
20+
status List active inhibitors
21+
```
22+
23+
## Configuration
24+
25+
Options in the configuration file _/etc/auto-inhibit.conf_ are passed directly
26+
as flags to **systemd-inhibit**.
27+
28+
Given _example.conf_:
29+
30+
```
31+
[aria2c]
32+
why=Download in progress
33+
```
34+
35+
Running `aria2c` via the symlink will result in:
36+
37+
`systemd-inhibit --why='Download in progress' aria2c`
38+
39+
## License
40+
41+
This project is licensed under the MIT License (see [LICENSE](LICENSE)).

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.1.0

auto-inhibit

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
readonly CONFIG="${AUTO_INHIBIT_CONF:-/etc/auto-inhibit.conf}"
6+
readonly COMMAND="${AUTO_INHIBIT_CMD:-/usr/bin/systemd-inhibit}"
7+
readonly INHIBITOR="${AUTO_INHIBITOR:-/usr/bin/auto-inhibit}"
8+
9+
is_inhibitor_link() {
10+
[[ -L "$1" && "$(/usr/bin/readlink -f "$1")" == "$INHIBITOR" ]]
11+
}
12+
13+
name="$(/usr/bin/basename "$0")"
14+
15+
if [[ "$name" != 'auto-inhibit' ]]; then
16+
bltin=false
17+
path=''
18+
19+
for p in $(type -a "$name" 2> /dev/null | /usr/bin/awk '{print $NF}'); do
20+
if [[ "$p" == 'builtin' ]]; then
21+
bltin=true
22+
continue
23+
fi
24+
if ! is_inhibitor_link "$p"; then
25+
path="$p"
26+
break
27+
fi
28+
done
29+
30+
if [[ -z "$path" ]]; then
31+
$bltin && msg='is a builtin' || msg='not found'
32+
echo "$name $msg" >&2
33+
exit 1
34+
fi
35+
36+
mapfile -t flags < <(/usr/bin/iniq -p "$name" -f '--%k=%v' "$CONFIG")
37+
38+
exec "$COMMAND" "${flags[@]}" "$path" "$@"
39+
fi
40+
41+
symdir='/usr/local/bin'
42+
43+
declare -a links=()
44+
45+
usage() {
46+
echo 'usage: auto-inhibit [options] [command]
47+
48+
options:
49+
-h Show help message
50+
-d DIR Operate on DIR
51+
52+
commands:
53+
list List symlinks
54+
generate Create symlinks
55+
check Check symlinks
56+
remove Remove symlinks
57+
status List active inhibitors'
58+
}
59+
60+
get_links() {
61+
for f in "$symdir"/*; do
62+
is_inhibitor_link "$f" && links+=("$f")
63+
done
64+
}
65+
66+
status() {
67+
/usr/bin/systemd-inhibit --list
68+
exit $?
69+
}
70+
71+
while getopts ':hd:' opt; do
72+
case "$opt" in
73+
h) usage; exit ;;
74+
d) symdir="$OPTARG" ;;
75+
*) usage >&2; exit 2
76+
esac
77+
done
78+
79+
shift $((OPTIND - 1))
80+
81+
[[ $# -eq 0 ]] && status
82+
83+
case "$1" in
84+
list)
85+
get_links
86+
for f in "${links[@]}"; do
87+
echo "${f##*/}"
88+
done
89+
;;
90+
generate)
91+
for f in $(/usr/bin/iniq -d "$CONFIG"); do
92+
/usr/bin/ln -vs "$INHIBITOR" "$symdir/$f"
93+
done
94+
;;
95+
check)
96+
ret=0
97+
for f in $(/usr/bin/iniq -d "$CONFIG"); do
98+
err=''
99+
if [[ ! -e "$symdir/$f" ]]; then
100+
err="$f does not exist"
101+
elif ! is_inhibitor_link "$symdir/$f"; then
102+
err="$f is not a symlink to auto-inhibit"
103+
fi
104+
if [[ -n "$err" ]]; then
105+
echo "$err" >&2
106+
ret=$((ret+1))
107+
fi
108+
done
109+
exit $ret
110+
;;
111+
remove)
112+
get_links
113+
for f in "${links[@]}"; do
114+
/usr/bin/rm -v "$f"
115+
done
116+
;;
117+
status) status ;;
118+
*) usage >&2; exit 2
119+
esac

auto-inhibit.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [DEFAULT]
2+
# what=sleep
3+
4+
# [aria2c]
5+
# why=Download in progress
6+
7+
# [youtube-dl]
8+
# why=Download in progress

man/auto-inhibit.1.pod

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
=head1 NAME
2+
3+
auto-inhibit - automate use of systemd-inhibit
4+
5+
=head1 SYNOPSIS
6+
7+
B<auto-inhibit> [options] [command]
8+
9+
=head1 DESCRIPTION
10+
11+
auto-inhibit manages symbolic links to itself named after programs to be run
12+
with an inhibition lock via B<systemd-inhibit>.
13+
14+
=head1 OPTIONS
15+
16+
=over
17+
18+
=item B<-h>
19+
20+
Show help message.
21+
22+
=item B<-d> I<DIRECTORY>
23+
24+
Operate on I<DIRECTORY>.
25+
26+
=back
27+
28+
=head1 COMMANDS
29+
30+
=over
31+
32+
=item B<list>
33+
34+
List existing symbolic links.
35+
36+
=item B<generate>
37+
38+
Create symbolic links based on configuration.
39+
40+
=item B<check>
41+
42+
Check that symlinks reflect the configuration.
43+
44+
=item B<remove>
45+
46+
Remove generated symbolic links.
47+
48+
=item B<status>
49+
50+
List active inhibitors.
51+
52+
=back
53+
54+
=head1 CONFIGURATION
55+
56+
Options in the configuration file F</etc/auto-inhibit.conf> are passed directly
57+
as flags to B<systemd-inhibit>.
58+
59+
Given F<example.conf>:
60+
61+
[aria2c]
62+
why=Download in progress
63+
64+
Running I<aria2c> via the symlink will result in:
65+
66+
systemd-inhibit --why='Download in progress' aria2c
67+
68+
=head1 AUTHOR
69+
70+
James Reed E<lt>[email protected]<gt>
71+
72+
=head1 REPORTING BUGS
73+
74+
Bugs and issues can be reported here:
75+
L<https://github.com/jcrd/auto-inhibit/issues>
76+
77+
=head1 COPYRIGHT
78+
79+
Copyright 2018 James Reed. auto-inhibit is licensed under the MIT License.
80+
81+
=head1 SEE ALSO
82+
83+
B<systemd-inhibit>(1)

0 commit comments

Comments
 (0)