Skip to content

Commit

Permalink
switch-to-configuration: Stop units in one call to systemctl
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Aug 3, 2012
1 parent b6bd0a4 commit bd3c9fe
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions modules/system/activation/switch-to-configuration.pl
Expand Up @@ -65,27 +65,30 @@ sub getActiveUnits {
# 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;
Expand All @@ -107,7 +110,7 @@ sub getActiveUnits {
# 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);
Expand Down

0 comments on commit bd3c9fe

Please sign in to comment.