From bd3c9febc9672e6f6ff8705b6437e1d8fca1bebb Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 3 Aug 2012 13:29:56 -0400 Subject: [PATCH] switch-to-configuration: Stop units in one call to systemctl --- .../activation/switch-to-configuration.pl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/modules/system/activation/switch-to-configuration.pl b/modules/system/activation/switch-to-configuration.pl index fd515341d134c8..e017098a411183 100644 --- a/modules/system/activation/switch-to-configuration.pl +++ b/modules/system/activation/switch-to-configuration.pl @@ -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; @@ -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);