forked from Debian/debhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dh_installsystemduser
executable file
·268 lines (202 loc) · 7.42 KB
/
dh_installsystemduser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/perl -w
=head1 NAME
dh_installsystemduser - install systemd unit files
=cut
use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;
our $VERSION = DH_BUILTIN_VERSION;
=head1 SYNOPSIS
B<dh_installsystemduser> [S<I<debhelper options>>] [B<--no-enable>] [B<--name=>I<name>] [S<I<unit file> ...>]
=head1 DESCRIPTION
B<dh_installsystemduser> finds the systemd user instance service files
installed by a package and generates F<postinst>, and F<prerm> code
blocks for enabling and disabling the corresponding systemd user
instance services, when the package is installed, updated, or
removed. These snippets are added to the maintainer scripts by
L<dh_installdeb(1)>.
L<deb-systemd-helper(1)> is used to enable and disable the systemd
units, thus it is not necessary that the machine actually runs systemd
during package installation time, enabling happens on all machines.
B<dh_installsystemduser> operates on all user instance unit files
installed by a package. For only generating blocks for specific unit
files, pass them as arguments. Specific unit files can be excluded
from processing using the B<-X> common L<debhelper(1)> option.
=head1 FILES
=over 4
=item debian/I<package>.user.path,
debian/I<package>@.user.path,
debian/I<package>.user.service,
debian/I<package>@.user.service,
debian/I<package>.user.socket,
debian/I<package>@.user.socket,
debian/I<package>.user.target,
debian/I<package>@.user.target,
debian/I<package>.user.timer,
debian/I<package>@.user.timer
If any of those files exists, they are installed into
F<usr/lib/systemd/user/> in the package build directory removing the
F<.user> file name part.
=back
=head1 OPTIONS
=over 4
=item B<--name=>I<name>
Install the service file as I<name.service> instead of the default
filename I<package.service>. When this parameter is used,
B<dh_installsystemd> looks for and installs files named
F<debian/package.name.user.service> instead of the usual
F<debian/package.user.service>. Moreover, maintainer scripts are only
generated for units that match the given I<name>.
=item B<--no-enable>
Disable the service(s) on purge, but do not enable them on install.
=back
=head1 NOTES
This command is not idempotent. L<dh_prep(1)> should be called between
invocations of this command (with the same arguments). Otherwise, it
may cause multiple instances of the same text to be added to
maintainer scripts.
=cut
# PROMISE: DH NOOP WITHOUT internal(bug#950723) tmp(usr/lib/systemd/user) user.service
init(options => {
"no-enable" => \$dh{NO_ENABLE},
});
sub quote {
# Add single quotes around the argument.
return '\'' . $_[0] . '\'';
}
sub uniq {
my %seen;
return grep { !$seen{$_}++ } @_;
}
sub contains_install_section {
my ($unit_path) = @_;
open(my $fh, '<', $unit_path) or error("Cannot open($unit_path) to check for [Install]: $!");
while (my $line = <$fh>) {
chomp($line);
return 1 if $line =~ /^\s*\[Install\]$/i;
}
close($fh);
return 0;
}
sub install_user_unit {
my ($package, $name, $suffix, $path) = @_;
my $unit = pkgfile($package, "user.$suffix");
return if $unit eq '';
install_dir($path);
install_file($unit, "$path/$name.$suffix");
}
# Extracts the directive values from a unit file. Handles repeated
# directives in the same unit file. Assumes values can only be
# composed of lists of unit names. This is good enough for the 'Also='
# and 'Alias=' directives handled here.
sub extract_key {
my ($unit_path, $key) = @_;
my @values;
open(my $fh, '<', $unit_path) or error("Cannot open($unit_path): $!");
while (my $line = <$fh>) {
chomp($line);
# Since unit names can't have whitespace in systemd, simply
# use split and strip any leading/trailing quotes. See
# systemd-escape(1) for examples of valid unit names.
if ($line =~ /^\s*$key=(.+)$/i) {
for my $value (split(/\s+/, $1)) {
$value =~ s/^(["'])(.*)\g1$/$2/;
push @values, $value;
}
}
}
close($fh);
return @values;
}
sub list_installed_user_units {
my ($tmpdir, $aliases) = @_;
my $lib_systemd_user = "$tmpdir/usr/lib/systemd/user";
my @installed;
return unless -d $lib_systemd_user;
opendir(my $dh, $lib_systemd_user) or error("Cannot opendir($lib_systemd_user): $!");
foreach my $name (readdir($dh)) {
my $path = "$lib_systemd_user/$name";
next unless -f $path;
if (-l $path) {
my $dest = basename(readlink($path));
$aliases->{$dest} //= [ ];
push @{$aliases->{$dest}}, $name;
} else {
push @installed, $name;
}
}
closedir($dh);
return @installed;
}
# Install package maintainer provided unit files.
foreach my $package (@{$dh{DOPACKAGES}}) {
my $tmpdir = tmpdir($package);
# unit file name
my $name = $dh{NAME} // $package;
my $path = "$tmpdir/usr/lib/systemd/user";
for my $type (qw(service target socket path timer)) {
install_user_unit($package, $name, $type, $path);
install_user_unit("${package}@", "${name}@", $type, $path);
}
}
# Generate postinst and prerm code blocks to enable and disable units
foreach my $package (@{$dh{DOPACKAGES}}) {
my (@args, @enable_units, %aliases);
my $tmpdir = tmpdir($package);
my @units = list_installed_user_units($tmpdir, \%aliases);
# Handle either only the unit files which were passed as arguments
# or all unit files that are installed in this package.
if (@ARGV) {
@args = @ARGV;
}
elsif ($dh{NAME}) {
# Treat --name flag as if the corresponding units were passed
# in the command line.
@args = grep /(^|\/)$dh{NAME}\.(service|target|socket|path|timer)$/, @units;
}
else {
@args = @units;
}
# Support excluding units via the -X debhelper common option.
foreach my $x (@{$dh{EXCLUDE}}) {
@args = grep !/(^|\/)$x$/, @args;
}
# This hash prevents us from looping forever in the following
# while loop. An actual real-world example of such a loop is
# systemd's systemd-readahead-drop.service, which contains
# Also=systemd-readahead-collect.service, and that file in turn
# contains Also=systemd-readahead-drop.service, thus forming an
# endless loop.
my %seen;
# Must use while and shift because the loop alters the list.
while (@args) {
my $name = shift @args;
my $path = "${tmpdir}/usr/lib/systemd/user/${name}";
error("User unit file \"$name\" not found in package \"$package\".") if ! -f $path;
# Skip template service files. Enabling or disabling those
# services without specifying the instance is not useful.
next if ($name =~ /\@/);
# Handle all unit files specified via Also= explicitly. This
# is not necessary for enabling, but for disabling, as we
# cannot read the unit file when disabling as it has already
# been deleted.
push @args, $_ for grep { !$seen{$_}++ } extract_key($path, 'Also');
push @enable_units, $name if contains_install_section($path);
}
@enable_units = map { quote($_) } sort(uniq(@enable_units));
if (@enable_units) {
# The generated maintainer script code blocks use the --user
# option that was added to deb-systemd-helper in version 1.52.
addsubstvar($package, 'misc:Depends', 'init-system-helpers', ">= 1.52");
my $postinst = $dh{NO_ENABLE} ? 'postinst-systemd-user-dont-enable' : 'postinst-systemd-user-enable';
foreach my $unit (@enable_units) {
autoscript($package, 'postinst', $postinst, { 'UNITFILE' => $unit });
}
autoscript($package, 'postrm', 'postrm-systemd-user', { 'UNITFILES' => join(' ', @enable_units) });
}
}
=head1 SEE ALSO
L<debhelper(7)>, L<dh_installsystemd(1)>, L<deb-systemd-helper(1)>
=head1 AUTHORS
pkg-systemd-maintainers@lists.alioth.debian.org
=cut