| 1 |
#!/usr/bin/perl |
| 2 |
# vim:ts=4:sw=4:expandtab |
| 3 |
# © 2013-2014 Michael Stapelberg <[email protected]> |
| 4 |
# |
| 5 |
# All rights reserved. |
| 6 |
# |
| 7 |
# Redistribution and use in source and binary forms, with or without |
| 8 |
# modification, are permitted provided that the following conditions are met: |
| 9 |
# |
| 10 |
# * Redistributions of source code must retain the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer. |
| 12 |
# |
| 13 |
# * Redistributions in binary form must reproduce the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer in the |
| 15 |
# documentation and/or other materials provided with the distribution. |
| 16 |
# |
| 17 |
# * Neither the name of Michael Stapelberg nor the |
| 18 |
# names of contributors may be used to endorse or promote products |
| 19 |
# derived from this software without specific prior written permission. |
| 20 |
# . |
| 21 |
# THIS SOFTWARE IS PROVIDED BY Michael Stapelberg ''AS IS'' AND ANY |
| 22 |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 |
# DISCLAIMED. IN NO EVENT SHALL Michael Stapelberg BE LIABLE FOR ANY |
| 25 |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
|
| 32 |
=head1 NAME |
| 33 |
|
| 34 |
deb-systemd-helper - subset of systemctl for machines not running systemd |
| 35 |
|
| 36 |
=head1 SYNOPSIS |
| 37 |
|
| 38 |
B<deb-systemd-helper> enable | disable | purge | mask | unmask | is-enabled | was-enabled | debian-installed | update-state | reenable S<I<unit file> ...> |
| 39 |
|
| 40 |
=head1 DESCRIPTION |
| 41 |
|
| 42 |
B<deb-systemd-helper> is a Debian-specific helper script which re-implements |
| 43 |
the enable, disable, is-enabled and reenable commands from systemctl. |
| 44 |
|
| 45 |
The "enable" action will only be performed once (when first installing the |
| 46 |
package). On the first "enable", a state file is created which will be deleted |
| 47 |
upon "purge". |
| 48 |
|
| 49 |
The "mask" action will keep state on whether the service was enabled/disabled |
| 50 |
before and will properly return to that state on "unmask". |
| 51 |
|
| 52 |
The "was-enabled" action is not present in systemctl, but is required in Debian |
| 53 |
so that we can figure out whether a service was enabled before we installed an |
| 54 |
updated service file. See http://bugs.debian.org/717603 for details. |
| 55 |
|
| 56 |
The "debian-installed" action is also not present in systemctl. It returns 0 if |
| 57 |
the state file of at least one of the given units is present. |
| 58 |
|
| 59 |
The "update-state" action is also not present in systemctl. It updates |
| 60 |
B<deb-systemd-helper>'s state file, removing obsolete entries (e.g. service |
| 61 |
files that are no longer shipped by the package) and adding new entries (e.g. |
| 62 |
new service files shipped by the package) without enabling them. |
| 63 |
|
| 64 |
B<deb-systemd-helper> is intended to be used from maintscripts to enable |
| 65 |
systemd unit files. It is specifically NOT intended to be used interactively by |
| 66 |
users. Instead, users should run systemd and use systemctl, or not bother about |
| 67 |
the systemd enabled state in case they are not running systemd. |
| 68 |
|
| 69 |
=head1 ENVIRONMENT |
| 70 |
|
| 71 |
=over 4 |
| 72 |
|
| 73 |
=item B<_DEB_SYSTEMD_HELPER_DEBUG> |
| 74 |
|
| 75 |
If you export _DEB_SYSTEMD_HELPER_DEBUG=1, deb-systemd-helper will print debug |
| 76 |
messages to stderr (thus visible in dpkg runs). Please include these when |
| 77 |
filing a bugreport. |
| 78 |
|
| 79 |
=item B<DPKG_ROOT> |
| 80 |
|
| 81 |
Instead of working on the filesystem root /, perform all operations on a chroot |
| 82 |
system in the directory given by DPKG_ROOT. |
| 83 |
|
| 84 |
=back |
| 85 |
|
| 86 |
=cut |
| 87 |
|
| 88 |
use strict; |
| 89 |
use warnings; |
| 90 |
use File::Path qw(make_path); # in core since Perl 5.001 |
| 91 |
use File::Basename; # in core since Perl 5 |
| 92 |
use File::Temp qw(tempfile); # in core since Perl 5.6.1 |
| 93 |
use Getopt::Long; # in core since Perl 5 |
| 94 |
# Make Data::Dumper::Dumper available if present (not present on systems that |
| 95 |
# only have perl-base, not perl). |
| 96 |
eval { require Data::Dumper; } or *Data::Dumper::Dumper = sub { "no Data::Dumper" }; |
| 97 |
|
| 98 |
my $dpkg_root = $ENV{DPKG_ROOT} // ''; |
| 99 |
|
| 100 |
use constant { |
| 101 |
SYSTEM_INSTANCE_ENABLED_STATE_DIR => '/var/lib/systemd/deb-systemd-helper-enabled', |
| 102 |
USER_INSTANCE_ENABLED_STATE_DIR => '/var/lib/systemd/deb-systemd-user-helper-enabled', |
| 103 |
SYSTEM_INSTANCE_MASKED_STATE_DIR => '/var/lib/systemd/deb-systemd-helper-masked', |
| 104 |
USER_INSTANCE_MASKED_STATE_DIR => '/var/lib/systemd/deb-systemd-user-helper-masked', |
| 105 |
}; |
| 106 |
|
| 107 |
my $quiet = 0; |
| 108 |
my $instance = 'system'; |
| 109 |
my $enabled_state_dir = $dpkg_root . SYSTEM_INSTANCE_ENABLED_STATE_DIR; |
| 110 |
my $masked_state_dir = $dpkg_root . SYSTEM_INSTANCE_MASKED_STATE_DIR; |
| 111 |
|
| 112 |
# Globals are bad, but in this specific case, it really makes things much |
| 113 |
# easier to write and understand. |
| 114 |
my $changed_sth; |
| 115 |
my $has_systemctl = -x "$dpkg_root/bin/systemctl" || -x "$dpkg_root/usr/bin/systemctl"; |
| 116 |
|
| 117 |
sub assertdpkgroot { |
| 118 |
my ($path, $msg) = @_; |
| 119 |
if (length $ENV{DPKG_ROOT}) { |
| 120 |
if ($path !~ /^\Q$dpkg_root\E/) { |
| 121 |
error("doesn't start with dpkg_root: $path $msg"); |
| 122 |
} |
| 123 |
if ($path =~ /^\Q$dpkg_root$dpkg_root\E/) { |
| 124 |
error("double dpkg_root: $path $msg"); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
sub assertnotdpkgroot { |
| 130 |
my ($path, $msg) = @_; |
| 131 |
if (length $ENV{DPKG_ROOT}) { |
| 132 |
if ($path =~ /^\Q$dpkg_root\E/) { |
| 133 |
error("starts with dpkg_root: $path $msg"); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
sub error { |
| 139 |
print STDERR "$0: error: @_\n"; |
| 140 |
exit (1); |
| 141 |
} |
| 142 |
|
| 143 |
sub debug { |
| 144 |
my ($msg) = @_; |
| 145 |
return if !defined($ENV{_DEB_SYSTEMD_HELPER_DEBUG}) || $ENV{_DEB_SYSTEMD_HELPER_DEBUG} != 1; |
| 146 |
print STDERR "(deb-systemd-helper DEBUG) $msg\n"; |
| 147 |
} |
| 148 |
|
| 149 |
sub is_purge { |
| 150 |
return (defined($ENV{_DEB_SYSTEMD_HELPER_PURGE}) && $ENV{_DEB_SYSTEMD_HELPER_PURGE} == 1) |
| 151 |
} |
| 152 |
|
| 153 |
sub find_unit { |
| 154 |
my ($scriptname) = @_; |
| 155 |
|
| 156 |
my $service_path = $scriptname; |
| 157 |
|
| 158 |
if (-f "$dpkg_root/etc/systemd/$instance/$scriptname") { |
| 159 |
$service_path = "/etc/systemd/$instance/$scriptname"; |
| 160 |
} elsif (-f "$dpkg_root/lib/systemd/$instance/$scriptname") { |
| 161 |
$service_path = "/lib/systemd/$instance/$scriptname"; |
| 162 |
} elsif (-f "$dpkg_root/usr/lib/systemd/$instance/$scriptname") { |
| 163 |
$service_path = "/usr/lib/systemd/$instance/$scriptname"; |
| 164 |
} |
| 165 |
|
| 166 |
return $service_path; |
| 167 |
} |
| 168 |
|
| 169 |
sub dsh_state_path { |
| 170 |
my ($scriptname) = @_; |
| 171 |
return $enabled_state_dir . '/' . basename($scriptname) . '.dsh-also'; |
| 172 |
} |
| 173 |
|
| 174 |
sub state_file_entries { |
| 175 |
my ($dsh_state) = @_; |
| 176 |
debug "Reading state file $dsh_state"; |
| 177 |
my @entries; |
| 178 |
if (open(my $fh, '<', $dsh_state)) { |
| 179 |
@entries = map { chomp; "$dpkg_root$_" } <$fh>; |
| 180 |
close($fh); |
| 181 |
} |
| 182 |
return @entries; |
| 183 |
} |
| 184 |
|
| 185 |
# Writes $service_link into $dsh_state unless it’s already in there. |
| 186 |
sub record_in_statefile { |
| 187 |
my ($dsh_state, $service_link) = @_; |
| 188 |
|
| 189 |
assertdpkgroot($dsh_state, "record_in_statefile"); |
| 190 |
assertnotdpkgroot($service_link, "record_in_statefile"); |
| 191 |
|
| 192 |
# Appending a newline makes the following code simpler; we can skip |
| 193 |
# chomp()ing and appending newlines in every print. |
| 194 |
$service_link .= "\n"; |
| 195 |
|
| 196 |
make_path(dirname($dsh_state)); |
| 197 |
my $line_exists; |
| 198 |
my ($outfh, $tmpname) = tempfile('.stateXXXXX', |
| 199 |
DIR => dirname($dsh_state), |
| 200 |
SUFFIX => '.tmp', |
| 201 |
UNLINK => 0); |
| 202 |
chmod(0644, $tmpname); |
| 203 |
if (-e $dsh_state) { |
| 204 |
open(my $infh, '<', $dsh_state) or error("unable to read from $dsh_state"); |
| 205 |
while (<$infh>) { |
| 206 |
$line_exists = 1 if $_ eq $service_link; |
| 207 |
print $outfh $_; |
| 208 |
} |
| 209 |
close($infh); |
| 210 |
} |
| 211 |
print $outfh $service_link unless $line_exists; |
| 212 |
close($outfh) or error("unable to close $tmpname"); |
| 213 |
|
| 214 |
debug "Renaming temp file $tmpname to state file $dsh_state"; |
| 215 |
rename($tmpname, $dsh_state) or |
| 216 |
error("Unable to move $tmpname to $dsh_state"); |
| 217 |
} |
| 218 |
|
| 219 |
# Gets the transitive closure of links, i.e. all links that need to be created |
| 220 |
# when enabling this service file. Not straight-forward because service files |
| 221 |
# can refer to other service files using Also=. |
| 222 |
sub get_link_closure { |
| 223 |
my ($scriptname, $service_path, @visited) = @_; |
| 224 |
assertnotdpkgroot($service_path, "get_link_closure"); |
| 225 |
|
| 226 |
my @links; |
| 227 |
my @wants_dirs; |
| 228 |
|
| 229 |
my $unit_name = basename($service_path); |
| 230 |
my $wanted_target = $unit_name; |
| 231 |
|
| 232 |
# The keys parsed from the unit file below can only have unit names |
| 233 |
# as values. Since unit names can't have whitespace in systemd, |
| 234 |
# simply use split and strip any leading/trailing quotes. See |
| 235 |
# systemd-escape(1) for examples of valid unit names. |
| 236 |
open my $fh, '<', "$dpkg_root$service_path" or error("unable to read $dpkg_root$service_path"); |
| 237 |
while (my $line = <$fh>) { |
| 238 |
chomp($line); |
| 239 |
my $service_link; |
| 240 |
|
| 241 |
if ($line =~ /^\s*(WantedBy|RequiredBy)=(.+)$/i) { |
| 242 |
for my $value (split(/\s+/, $2)) { |
| 243 |
$value =~ s/^(["'])(.*)\g1$/$2/; |
| 244 |
my $wants_dir = "/etc/systemd/$instance/$value"; |
| 245 |
$wants_dir .= '.wants' if $1 eq 'WantedBy'; |
| 246 |
$wants_dir .= '.requires' if $1 eq 'RequiredBy'; |
| 247 |
push @wants_dirs, "$wants_dir/"; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if ($line =~ /^\s*Also=(.+)$/i) { |
| 252 |
for my $value (split(/\s+/, $1)) { |
| 253 |
$value =~ s/^(["'])(.*)\g1$/$2/; |
| 254 |
if ($value ne $unit_name and not grep $_ eq $value, @visited) { |
| 255 |
# We can end up in an infinite recursion, so remember what units we |
| 256 |
# already processed to break it |
| 257 |
push @visited, $value; |
| 258 |
push @links, get_link_closure($value, find_unit($value), @visited); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
if ($line =~ /^\s*Alias=(.+)$/i) { |
| 264 |
for my $value (split(/\s+/, $1)) { |
| 265 |
$value =~ s/^(["'])(.*)\g1$/$2/; |
| 266 |
if ($value ne $unit_name) { |
| 267 |
push @links, { dest => $service_path, src => "/etc/systemd/$instance/$1" }; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if ($line =~ /^\s*DefaultInstance=\s*(["']?+)(.+)\g1\s*$/i) { |
| 273 |
$wanted_target = $2; |
| 274 |
$wanted_target = $unit_name =~ s/^(.*\@)(\.\w+)$/$1$wanted_target$2/r; |
| 275 |
} |
| 276 |
} |
| 277 |
close($fh); |
| 278 |
|
| 279 |
for my $wants_dir (@wants_dirs) { |
| 280 |
push @links, { dest => $service_path, src => $wants_dir . $wanted_target }; |
| 281 |
} |
| 282 |
|
| 283 |
return @links; |
| 284 |
} |
| 285 |
|
| 286 |
sub all_links_installed { |
| 287 |
my ($scriptname, $service_path) = @_; |
| 288 |
|
| 289 |
my @links = get_link_closure($scriptname, $service_path); |
| 290 |
foreach my $link (@links) { |
| 291 |
assertnotdpkgroot($link->{src}, "all_links_installed"); |
| 292 |
} |
| 293 |
my @missing_links = grep { ! -l "$dpkg_root$_->{src}" } @links; |
| 294 |
|
| 295 |
return (@missing_links == 0); |
| 296 |
} |
| 297 |
|
| 298 |
sub no_link_installed { |
| 299 |
my ($scriptname, $service_path) = @_; |
| 300 |
|
| 301 |
my @links = get_link_closure($scriptname, $service_path); |
| 302 |
foreach my $link (@links) { |
| 303 |
assertnotdpkgroot($link->{src}, "all_links_installed"); |
| 304 |
} |
| 305 |
my @existing_links = grep { -l "$dpkg_root$_->{src}" } @links; |
| 306 |
|
| 307 |
return (@existing_links == 0); |
| 308 |
} |
| 309 |
|
| 310 |
sub enable { |
| 311 |
my ($scriptname, $service_path) = @_; |
| 312 |
if ($has_systemctl) { |
| 313 |
# We use 'systemctl preset' on the initial installation only. |
| 314 |
# On upgrade, we manually add the missing symlinks only if the |
| 315 |
# service already has some links installed. Using 'systemctl |
| 316 |
# preset' allows administrators and downstreams to alter the |
| 317 |
# enable policy using systemd-native tools. |
| 318 |
my $create_links = 0; |
| 319 |
if (debian_installed($scriptname)) { |
| 320 |
$create_links = 1 unless no_link_installed($scriptname, $service_path); |
| 321 |
} else { |
| 322 |
debug "Using systemctl preset to enable $scriptname"; |
| 323 |
my $systemd_root = '/'; |
| 324 |
if ($dpkg_root ne '') { |
| 325 |
$systemd_root = $dpkg_root; |
| 326 |
} |
| 327 |
system("systemctl", |
| 328 |
"--root=$systemd_root", |
| 329 |
$instance eq "user" ? "--global" : "--system", |
| 330 |
"--preset-mode=enable-only", |
| 331 |
"preset", $scriptname) == 0 |
| 332 |
or error("systemctl preset failed on $scriptname: $!"); |
| 333 |
} |
| 334 |
make_systemd_links($scriptname, $service_path, create_links => $create_links); |
| 335 |
} else { |
| 336 |
# We create all the symlinks ourselves |
| 337 |
make_systemd_links($scriptname, $service_path); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
sub make_systemd_links { |
| 342 |
my ($scriptname, $service_path, %opts) = @_; |
| 343 |
$opts{'create_links'} //= 1; |
| 344 |
|
| 345 |
my $dsh_state = dsh_state_path($scriptname); |
| 346 |
|
| 347 |
my @links = get_link_closure($scriptname, $service_path); |
| 348 |
for my $link (@links) { |
| 349 |
my $service_path = $link->{dest}; |
| 350 |
my $service_link = $link->{src}; |
| 351 |
|
| 352 |
record_in_statefile($dsh_state, $service_link); |
| 353 |
|
| 354 |
my $statefile = $service_link; |
| 355 |
$statefile =~ s,^/etc/systemd/$instance/,$enabled_state_dir/,; |
| 356 |
$service_link = "$dpkg_root$service_link"; |
| 357 |
assertdpkgroot($statefile, "make_systemd_links"); |
| 358 |
assertdpkgroot($service_link, "make_systemd_links"); |
| 359 |
assertnotdpkgroot($service_path, "make_systemd_links"); |
| 360 |
next if -e $statefile; |
| 361 |
|
| 362 |
if ($opts{'create_links'} && ! -l $service_link) { |
| 363 |
make_path(dirname($service_link)); |
| 364 |
symlink($service_path, $service_link) or |
| 365 |
error("unable to link $service_link to $service_path: $!"); |
| 366 |
$changed_sth = 1; |
| 367 |
} |
| 368 |
|
| 369 |
# Store the fact that we ran enable for this service_path, |
| 370 |
# so that we can skip enable the next time. |
| 371 |
# This allows us to call deb-systemd-helper unconditionally |
| 372 |
# and still only enable unit files on the initial installation |
| 373 |
# of a package. |
| 374 |
make_path(dirname($statefile)); |
| 375 |
open(my $fh, '>>', $statefile) or error("Failed to create/touch $statefile"); |
| 376 |
close($fh) or error("Failed to create/touch $statefile"); |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
# In contrary to make_systemd_links(), which only modifies the state file in an |
| 382 |
# append-only fashion, update_state() can also remove entries from the state |
| 383 |
# file. |
| 384 |
# |
| 385 |
# The distinction is important because update_state() should only be called |
| 386 |
# when the unit file(s) are guaranteed to be on-disk, e.g. on package updates, |
| 387 |
# but not on package removals. |
| 388 |
sub update_state { |
| 389 |
my ($scriptname, $service_path) = @_; |
| 390 |
|
| 391 |
my $dsh_state = dsh_state_path($scriptname); |
| 392 |
my @links = get_link_closure($scriptname, $service_path); |
| 393 |
assertdpkgroot($dsh_state, "update_state"); |
| 394 |
|
| 395 |
debug "Old state file contents: " . |
| 396 |
Data::Dumper::Dumper([ state_file_entries($dsh_state) ]); |
| 397 |
|
| 398 |
make_path(dirname($dsh_state)); |
| 399 |
my ($outfh, $tmpname) = tempfile('.stateXXXXX', |
| 400 |
DIR => dirname($dsh_state), |
| 401 |
SUFFIX => '.tmp', |
| 402 |
UNLINK => 0); |
| 403 |
chmod(0644, $tmpname); |
| 404 |
for my $link (@links) { |
| 405 |
assertnotdpkgroot($link->{src}, "update_state"); |
| 406 |
print $outfh $link->{src} . "\n"; |
| 407 |
} |
| 408 |
close($outfh) or error("Failed to close $tmpname"); |
| 409 |
|
| 410 |
debug "Renaming temp file $tmpname to state file $dsh_state"; |
| 411 |
rename($tmpname, $dsh_state) or |
| 412 |
error("Unable to move $tmpname to $dsh_state"); |
| 413 |
|
| 414 |
debug "New state file contents: " . |
| 415 |
Data::Dumper::Dumper([ state_file_entries($dsh_state) ]); |
| 416 |
} |
| 417 |
|
| 418 |
sub was_enabled { |
| 419 |
my ($scriptname) = @_; |
| 420 |
|
| 421 |
my @entries = state_file_entries(dsh_state_path($scriptname)); |
| 422 |
debug "Contents: " . Data::Dumper::Dumper(\@entries); |
| 423 |
|
| 424 |
for my $link (@entries) { |
| 425 |
assertdpkgroot($link, "was_enabled"); |
| 426 |
if (! -l $link) { |
| 427 |
debug "Link $link is missing, considering $scriptname was-disabled."; |
| 428 |
return 0; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
debug "All links present, considering $scriptname was-enabled."; |
| 433 |
return 1; |
| 434 |
} |
| 435 |
|
| 436 |
sub debian_installed { |
| 437 |
my ($scriptname) = @_; |
| 438 |
return -f dsh_state_path($scriptname); |
| 439 |
} |
| 440 |
|
| 441 |
sub remove_links { |
| 442 |
my ($service_path) = @_; |
| 443 |
|
| 444 |
my $dsh_state = dsh_state_path($service_path); |
| 445 |
my @entries = state_file_entries($dsh_state); |
| 446 |
debug "Contents: " . Data::Dumper::Dumper(\@entries); |
| 447 |
assertdpkgroot($dsh_state, "remove_links"); |
| 448 |
assertnotdpkgroot($service_path, "remove_links"); |
| 449 |
|
| 450 |
if (is_purge()) { |
| 451 |
unlink($dsh_state) if -e $dsh_state; |
| 452 |
} |
| 453 |
|
| 454 |
# Also disable all the units which were enabled when this one was enabled. |
| 455 |
for my $link (@entries) { |
| 456 |
# Delete the corresponding state file: |
| 457 |
# • Always when purging |
| 458 |
# • If the user did not disable (= link still exists) the service. |
| 459 |
# If we don’t do this, the link will be deleted a few lines down, |
| 460 |
# but not re-created when re-installing the package. |
| 461 |
assertdpkgroot($link, "remove_links"); |
| 462 |
if (is_purge() || -l $link) { |
| 463 |
my $link_state = $link; |
| 464 |
$link_state =~ s,^\Q$dpkg_root\E/etc/systemd/$instance/,$enabled_state_dir/,; |
| 465 |
unlink($link_state); |
| 466 |
} |
| 467 |
|
| 468 |
next unless -l $link; |
| 469 |
unlink($link) or |
| 470 |
print STDERR "$0: unable to remove '$link': $!\n"; |
| 471 |
|
| 472 |
$changed_sth = 1; |
| 473 |
} |
| 474 |
|
| 475 |
# Read $service_path, recurse for all Also= units. |
| 476 |
# This might not work when $service_path was already deleted, |
| 477 |
# i.e. after apt-get remove. In this case we just return |
| 478 |
# silently in order to not confuse the user about whether |
| 479 |
# disabling actually worked or not — the case is handled by |
| 480 |
# dh_installsystemd generating an appropriate disable |
| 481 |
# command by parsing the service file at debhelper-time. |
| 482 |
open(my $fh, '<', "$dpkg_root$service_path") or return; |
| 483 |
while (my $line = <$fh>) { |
| 484 |
chomp($line); |
| 485 |
my $service_link; |
| 486 |
|
| 487 |
if ($line =~ /^\s*Also=(.+)$/i) { |
| 488 |
remove_links(find_unit($1)); |
| 489 |
} |
| 490 |
} |
| 491 |
close($fh); |
| 492 |
} |
| 493 |
|
| 494 |
# Recursively deletes a directory structure, if all (!) components are empty, |
| 495 |
# e.g. to clean up after purging. |
| 496 |
sub rmdir_if_empty { |
| 497 |
my ($dir) = @_; |
| 498 |
|
| 499 |
debug "rmdir_if_empty $dir"; |
| 500 |
|
| 501 |
rmdir_if_empty($_) for (grep { -d } <$dir/*>); |
| 502 |
|
| 503 |
if (!rmdir($dir)) { |
| 504 |
debug "rmdir($dir) failed ($!)"; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
sub mask_service { |
| 509 |
my ($scriptname, $service_path) = @_; |
| 510 |
|
| 511 |
my $mask_link = "$dpkg_root/etc/systemd/$instance/" . basename($service_path); |
| 512 |
|
| 513 |
if (-e $mask_link) { |
| 514 |
# If the link already exists, don’t do anything. |
| 515 |
return if -l $mask_link && readlink($mask_link) eq '/dev/null'; |
| 516 |
|
| 517 |
# If the file already exists, the user most likely copied the .service |
| 518 |
# file to /etc/ to change it in some way. In this case we don’t need to |
| 519 |
# mask the .service in the first place, since it will not be removed by |
| 520 |
# dpkg. |
| 521 |
debug "$mask_link already exists, not masking."; |
| 522 |
return; |
| 523 |
} |
| 524 |
|
| 525 |
make_path(dirname($mask_link)); |
| 526 |
# clean up after possible leftovers from Alias= to self (LP#1439793) |
| 527 |
unlink($mask_link); |
| 528 |
symlink('/dev/null', $mask_link) or |
| 529 |
error("unable to link $mask_link to /dev/null: $!"); |
| 530 |
$changed_sth = 1; |
| 531 |
|
| 532 |
my $statefile = $mask_link; |
| 533 |
$statefile =~ s,^\Q$dpkg_root\E/etc/systemd/$instance/,$masked_state_dir/,; |
| 534 |
|
| 535 |
# Store the fact that we masked this service, so that we can unmask it on |
| 536 |
# installation time. We cannot unconditionally unmask because that would |
| 537 |
# interfere with the user’s decision to mask a service. |
| 538 |
make_path(dirname($statefile)); |
| 539 |
open(my $fh, '>>', $statefile) or error("Failed to create/touch $statefile"); |
| 540 |
close($fh) or error("Failed to create/touch $statefile"); |
| 541 |
} |
| 542 |
|
| 543 |
sub unmask_service { |
| 544 |
my ($scriptname, $service_path) = @_; |
| 545 |
|
| 546 |
my $mask_link = "$dpkg_root/etc/systemd/$instance/" . basename($service_path); |
| 547 |
|
| 548 |
# Not masked? Nothing to do. |
| 549 |
return unless -e $mask_link; |
| 550 |
|
| 551 |
if (! -l $mask_link || readlink($mask_link) ne '/dev/null') { |
| 552 |
debug "Not unmasking $mask_link because it is not a link to /dev/null"; |
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
my $statefile = $mask_link; |
| 557 |
$statefile =~ s,^\Q$dpkg_root\E/etc/systemd/$instance/,$masked_state_dir/,; |
| 558 |
|
| 559 |
if (! -e $statefile) { |
| 560 |
debug "Not unmasking $mask_link because the state file $statefile does not exist"; |
| 561 |
return; |
| 562 |
} |
| 563 |
|
| 564 |
unlink($mask_link) or |
| 565 |
error("unable to remove $mask_link: $!"); |
| 566 |
$changed_sth = 1; |
| 567 |
unlink($statefile); |
| 568 |
} |
| 569 |
|
| 570 |
my $result = GetOptions( |
| 571 |
"quiet" => \$quiet, |
| 572 |
"user" => sub { $instance = 'user'; }, |
| 573 |
"system" => sub { $instance = 'system'; }, # default |
| 574 |
); |
| 575 |
|
| 576 |
if ($instance eq 'user') { |
| 577 |
debug "is user unit = yes"; |
| 578 |
$enabled_state_dir = $dpkg_root . USER_INSTANCE_ENABLED_STATE_DIR; |
| 579 |
$masked_state_dir = $dpkg_root . USER_INSTANCE_MASKED_STATE_DIR; |
| 580 |
} |
| 581 |
|
| 582 |
my $action = shift; |
| 583 |
if (!defined($action)) { |
| 584 |
# Called without arguments. Explain that this script should not be run interactively. |
| 585 |
print "$0 is a program which should be called by dpkg maintscripts only.\n"; |
| 586 |
print "Please do not run it interactively, ever. Also see the manpage deb-systemd-helper(1).\n"; |
| 587 |
exit 0; |
| 588 |
} |
| 589 |
|
| 590 |
if (!$ENV{DPKG_MAINTSCRIPT_PACKAGE}) { |
| 591 |
print STDERR "$0 was not called from dpkg. Exiting.\n"; |
| 592 |
exit 1; |
| 593 |
} |
| 594 |
|
| 595 |
if ($action eq 'purge') { |
| 596 |
$ENV{_DEB_SYSTEMD_HELPER_PURGE} = 1; |
| 597 |
$action = 'disable'; |
| 598 |
} |
| 599 |
|
| 600 |
debug "is purge = " . (is_purge() ? "yes" : "no"); |
| 601 |
|
| 602 |
my $rc = 0; |
| 603 |
if ($action eq 'is-enabled' || |
| 604 |
$action eq 'was-enabled' || |
| 605 |
$action eq 'debian-installed') { |
| 606 |
$rc = 1; |
| 607 |
} |
| 608 |
for my $scriptname (@ARGV) { |
| 609 |
my $service_path = find_unit($scriptname); |
| 610 |
|
| 611 |
debug "action = $action, scriptname = $scriptname, service_path = $service_path"; |
| 612 |
|
| 613 |
if ($action eq 'is-enabled') { |
| 614 |
my $enabled = all_links_installed($scriptname, $service_path); |
| 615 |
print STDERR ($enabled ? "enabled\n" : "disabled\n") unless $quiet; |
| 616 |
$rc = 0 if $enabled; |
| 617 |
} |
| 618 |
|
| 619 |
# was-enabled is the same as is-enabled, but only considers links recorded |
| 620 |
# in the state file. This is useful after package upgrades, to determine |
| 621 |
# whether the unit file was enabled before upgrading, even if the unit file |
| 622 |
# has changed and is not entirely enabled currently (due to a new Alias= |
| 623 |
# line for example). |
| 624 |
# |
| 625 |
# If all machines were running systemd, this issue would not be present |
| 626 |
# because is-enabled would query systemd, which would not have picked up |
| 627 |
# the new unit file yet. |
| 628 |
if ($action eq 'was-enabled') { |
| 629 |
my $enabled = was_enabled($scriptname); |
| 630 |
print STDERR ($enabled ? "enabled\n" : "disabled\n") unless $quiet; |
| 631 |
$rc = 0 if $enabled; |
| 632 |
} |
| 633 |
|
| 634 |
if ($action eq 'update-state') { |
| 635 |
update_state($scriptname, $service_path); |
| 636 |
} |
| 637 |
|
| 638 |
if ($action eq 'debian-installed') { |
| 639 |
$rc = 0 if debian_installed($scriptname); |
| 640 |
} |
| 641 |
|
| 642 |
if ($action eq 'reenable') { |
| 643 |
remove_links($service_path); |
| 644 |
make_systemd_links($scriptname, $service_path); |
| 645 |
} |
| 646 |
|
| 647 |
if ($action eq 'disable') { |
| 648 |
remove_links($service_path); |
| 649 |
# Clean up the state dir if it’s empty, or at least clean up all empty |
| 650 |
# subdirectories. Necessary to cleanly pass a piuparts run. |
| 651 |
rmdir_if_empty($dpkg_root . SYSTEM_INSTANCE_ENABLED_STATE_DIR); |
| 652 |
rmdir_if_empty($dpkg_root . USER_INSTANCE_ENABLED_STATE_DIR); |
| 653 |
|
| 654 |
# Same with directories below /etc/systemd, where we create symlinks. |
| 655 |
# If systemd is not installed (and no other package shipping service |
| 656 |
# files), this would make piuparts fail, too. |
| 657 |
rmdir_if_empty($_) for (grep { -d } <$dpkg_root/etc/systemd/system/*>); |
| 658 |
rmdir_if_empty($_) for (grep { -d } <$dpkg_root/etc/systemd/user/*>); |
| 659 |
} |
| 660 |
|
| 661 |
if ($action eq 'enable') { |
| 662 |
enable($scriptname, $service_path); |
| 663 |
} |
| 664 |
|
| 665 |
if ($action eq 'mask') { |
| 666 |
mask_service($scriptname, $service_path); |
| 667 |
} |
| 668 |
|
| 669 |
if ($action eq 'unmask') { |
| 670 |
unmask_service($scriptname, $service_path); |
| 671 |
# Clean up the state dir if it’s empty, or at least clean up all empty |
| 672 |
# subdirectories. Necessary to cleanly pass a piuparts run. |
| 673 |
rmdir_if_empty($dpkg_root . SYSTEM_INSTANCE_MASKED_STATE_DIR); |
| 674 |
rmdir_if_empty($dpkg_root . USER_INSTANCE_MASKED_STATE_DIR); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
# If we changed anything and this machine is running systemd, tell |
| 679 |
# systemd to reload so that it will immediately pick up our |
| 680 |
# changes. |
| 681 |
if (!length $ENV{DPKG_ROOT} && $changed_sth && $instance eq 'system' && -d "/run/systemd/system") { |
| 682 |
system("systemctl", "daemon-reload"); |
| 683 |
} |
| 684 |
|
| 685 |
exit $rc; |
| 686 |
|
| 687 |
=head1 AUTHOR |
| 688 |
|
| 689 |
Michael Stapelberg <[email protected]> |
| 690 |
|
| 691 |
=cut |