switch-to-configuration: Stop units in one call to systemctl

This commit is contained in:
Eelco Dolstra 2012-08-03 13:29:56 -04:00
parent b6bd0a4f84
commit bd3c9febc9

@ -65,27 +65,30 @@ system("@systemd@/bin/systemctl", "reset-failed");
# Stop all services that no longer exist or have changed in the new
# configuration.
# FIXME: handle template units (e.g. getty@.service).
my @unitsToStop;
my $active = getActiveUnits;
foreach my $unitFile (glob "/etc/systemd/system/*") {
next unless -f "$unitFile";
my $unit = basename $unitFile;
while (my ($unit, $state) = each %{$active}) {
my $state = $active->{$unit};
if (defined $state && ($state->{state} eq "active" || $state->{state} eq "activating")) {
my $curUnitFile = "/etc/systemd/system/$unit";
if (-e $curUnitFile && ($state->{state} eq "active" || $state->{state} eq "activating")) {
my $newUnitFile = "@out@/etc/systemd/system/$unit";
if (! -e $newUnitFile) {
print STDERR "stopping obsolete unit $unit...\n";
system("@systemd@/bin/systemctl", "stop", $unit); # FIXME: ignore errors?
} elsif (abs_path($unitFile) ne abs_path($newUnitFile)) {
push @unitsToStop, $unit;
} elsif (abs_path($curUnitFile) ne abs_path($newUnitFile)) {
print STDERR "stopping changed unit $unit...\n";
# Record that this unit needs to be started below. We
# write this to a file to ensure that the service gets
# restarted if we're interrupted.
write_file($restartListFile, { append => 1 }, "$unit\n");
system("@systemd@/bin/systemctl", "stop", $unit); # FIXME: ignore errors?
push @unitsToStop, $unit;
}
}
}
system("@systemd@/bin/systemctl", "stop", @unitsToStop)
if scalar @unitsToStop > 0; # FIXME: ignore errors?
# Activate the new configuration (i.e., update /etc, make accounts,
# and so on).
my $res = 0;
@ -107,7 +110,7 @@ system("@systemd@/bin/systemctl", "start", "default.target") == 0 or $res = 4;
# manually started).
my @stopped = split '\n', read_file($restartListFile, err_mode => 'quiet') // "";
if (scalar @stopped > 0) {
print STDERR "restarting unit(s) ", join(" ", @stopped), "...\n";
print STDERR "restarting the following units: ", join(" ", @stopped), "\n";
my %unique = map { $_, 1 } @stopped;
system("@systemd@/bin/systemctl", "start", keys(%unique)) == 0 or $res = 4;
unlink($restartListFile);