@@ -10,6 +10,8 @@ use Net::DBus;
1010use Sys::Syslog qw(:standard :macros);
1111use Cwd 'abs_path';
1212
13+## no critic(CodeLayout::ProhibitParensWithBuiltins)
14+
1315my $out = "@out@";
1416
1517my $curSystemd = abs_path("/run/current-system/sw/bin");
@@ -36,13 +38,13 @@ my $dryReloadByActivationFile = "/run/nixos/dry-activation-reload-list";
3638
3739make_path("/run/nixos", { mode => oct(755) });
3840
39−my $action = shift @ARGV;
41+my $action = shift(@ARGV);
4042
4143if ("@localeArchive@" ne "") {
4244 $ENV{LOCALE_ARCHIVE} = "@localeArchive@";
4345}
4446
45−if (!defined $action || ($action ne "switch" && $action ne "boot" && $action ne "test" && $action ne "dry-activate")) {
47+if (!defined($action) || ($action ne "switch" && $action ne "boot" && $action ne "test" && $action ne "dry-activate")) {
4648 print STDERR <<EOF;
4749Usage: $0 [switch|boot|test]
4850
@@ -51,27 +53,27 @@ boot: make the configuration the boot default
5153test: activate the configuration, but don\'t make it the boot default
5254dry-activate: show what would be done if this configuration were activated
5355EOF
54− exit 1;
56+ exit(1);
5557}
5658
5759$ENV{NIXOS_ACTION} = $action;
5860
5961# This is a NixOS installation if it has /etc/NIXOS or a proper
6062# /etc/os-release.
61−die "This is not a NixOS installation!\n" unless
63+die("This is not a NixOS installation!\n") unless
6264 -f "/etc/NIXOS" || (read_file("/etc/os-release", err_mode => 'quiet') // "") =~ /ID="?nixos"?/s;
6365
6466openlog("nixos", "", LOG_USER);
6567
6668# Install or update the bootloader.
6769if ($action eq "switch" || $action eq "boot") {
68− system("@installBootLoader@ $out") == 0 or exit 1;
70+ system('@installBootLoader@', $out) == 0 or exit 1;
6971}
7072
7173# Just in case the new configuration hangs the system, do a sync now.
7274system("@coreutils@/bin/sync", "-f", "/nix/store") unless ($ENV{"NIXOS_NO_SYNC"} // "") eq "1";
7375
74−exit 0 if $action eq "boot";
76+exit(0) if $action eq "boot";
7577
7678# Check if we can activate the new configuration.
7779my $oldVersion = read_file("/run/current-system/init-interface-version", err_mode => 'quiet') // "";
@@ -83,7 +85,7 @@ Warning: the new NixOS configuration has an ‘init’ that is
8385incompatible with the current configuration. The new configuration
8486won\'t take effect until you reboot the system.
8587EOF
86− exit 100;
88+ exit(100);
8789}
8890
8991# Ignore SIGHUP so that we're not killed if we're running on (say)
@@ -104,14 +106,27 @@ sub getActiveUnits {
104106 return $res;
105107}
106108
109+# Returns whether a systemd unit is active
110+sub unit_is_active {
111+ my ($unit_name) = @_;
112+
113+ my $mgr = Net::DBus->system->get_service('org.freedesktop.systemd1')->get_object('/org/freedesktop/systemd1');
114+ my $units = $mgr->ListUnitsByNames([$unit_name]);
115+ if (scalar(@{$units}) == 0) {
116+ return 0;
117+ }
118+ my $active_state = $units->[0]->[3]; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
119+ return $active_state eq 'active' || $active_state eq 'activating';
120+}
121+
107122sub parseFstab {
108123 my ($filename) = @_;
109124 my ($fss, $swaps);
110125 foreach my $line (read_file($filename, err_mode => 'quiet')) {
111− chomp $line;
126+ chomp($line);
112127 $line =~ s/^\s*#.*//;
113128 next if $line =~ /^\s*$/;
114− my @xs = split / /, $line;
129+ my @xs = split(/ /, $line);
115130 if ($xs[2] eq "swap") {
116131 $swaps->{$xs[0]} = { options => $xs[3] // "" };
117132 } else {
@@ -133,17 +148,16 @@ sub parseFstab {
133148sub parseSystemdIni {
134149 my ($unitContents, $path) = @_;
135150 # Tie the ini file to a hash for easier access
136− my %fileContents;
137− tie %fileContents, "Config::IniFiles", (-file => $path, -allowempty => 1, -allowcontinue => 1);
151+ tie(my %fileContents, 'Config::IniFiles', (-file => $path, -allowempty => 1, -allowcontinue => 1)); ## no critic(Miscellanea::ProhibitTies)
138152
139153 # Copy over all sections
140− foreach my $sectionName (keys %fileContents) {
154+ foreach my $sectionName (keys(%fileContents)) {
141155 if ($sectionName eq "Install") {
142156 # Skip the [Install] section because it has no relevant keys for us
143157 next;
144158 }
145159 # Copy over all keys
146− foreach my $iniKey (keys %{$fileContents{$sectionName}}) {
160+ foreach my $iniKey (keys(%{$fileContents{$sectionName}})) {
147161 # Ensure the value is an array so it's easier to work with
148162 my $iniValue = $fileContents{$sectionName}{$iniKey};
149163 my @iniValues;
@@ -181,7 +195,7 @@ sub parse_unit {
181195 # Replace \ with \\ so glob() still works with units that have a \ in them
182196 # Valid characters in unit names are ASCII letters, digits, ":", "-", "_", ".", and "\"
183197 $unit_path =~ s/\\/\\\\/gmsx;
184− foreach (glob "${unit_path}{,.d/*.conf}") {
198+ foreach (glob("${unit_path}{,.d/*.conf}")) {
185199 parseSystemdIni(\%unit_data, "$_")
186200 }
187201 return %unit_data;
@@ -194,7 +208,7 @@ sub parseSystemdBool {
194208
195209 my @values = @{$unitConfig->{$sectionName}{$boolName} // []};
196210 # Return default if value is not set
197− if (scalar @values lt 1 || not defined $values[-1]) {
211+ if (scalar(@values) lt 1 || not defined($values[-1])) {
198212 return $default;
199213 }
200214 # If value is defined multiple times, use the last definition
@@ -211,7 +225,7 @@ sub recordUnit {
211225# The opposite of recordUnit, removes a unit name from a file
212226sub unrecord_unit {
213227 my ($fn, $unit) = @_;
214− edit_file { s/^$unit\n//msx } $fn if $action ne "dry-activate";
228+ edit_file(sub { s/^$unit\n//msx }, $fn) if $action ne "dry-activate";
215229}
216230
217231# Compare the contents of two unit files and return whether the unit
@@ -226,6 +240,16 @@ sub unrecord_unit {
226240sub compare_units {
227241 my ($old_unit, $new_unit) = @_;
228242 my $ret = 0;
243+ # Keys to ignore in the [Unit] section
244+ my %unit_section_ignores = map { $_ => 1 } qw(
245+ X-Reload-Triggers
246+ Description Documentation
247+ OnFailure OnSuccess OnFailureJobMode
248+ IgnoreOnIsolate StopWhenUnneeded
249+ RefuseManualStart RefuseManualStop
250+ AllowIsolate CollectMode
251+ SourcePath
252+ );
229253
230254 my $comp_array = sub {
231255 my ($a, $b) = @_;
@@ -233,11 +257,23 @@ sub compare_units {
233257 };
234258
235259 # Comparison hash for the sections
236− my %section_cmp = map { $_ => 1 } keys %{$new_unit};
260+ my %section_cmp = map { $_ => 1 } keys(%{$new_unit});
237261 # Iterate over the sections
238− foreach my $section_name (keys %{$old_unit}) {
262+ foreach my $section_name (keys(%{$old_unit})) {
239263 # Missing section in the new unit?
240− if (not exists $section_cmp{$section_name}) {
264+ if (not exists($section_cmp{$section_name})) {
265+ # If the [Unit] section was removed, make sure that only keys
266+ # were in it that are ignored
267+ if ($section_name eq 'Unit') {
268+ foreach my $ini_key (keys(%{$old_unit->{'Unit'}})) {
269+ if (not defined($unit_section_ignores{$ini_key})) {
270+ return 1;
271+ }
272+ }
273+ next; # check the next section
274+ } else {
275+ return 1;
276+ }
241277 if ($section_name eq 'Unit' and %{$old_unit->{'Unit'}} == 1 and defined(%{$old_unit->{'Unit'}}{'X-Reload-Triggers'})) {
242278 # If a new [Unit] section was removed that only contained X-Reload-Triggers,
243279 # do nothing.
@@ -248,15 +284,15 @@ sub compare_units {
248284 }
249285 delete $section_cmp{$section_name};
250286 # Comparison hash for the section contents
251− my %ini_cmp = map { $_ => 1 } keys %{$new_unit->{$section_name}};
287+ my %ini_cmp = map { $_ => 1 } keys(%{$new_unit->{$section_name}});
252288 # Iterate over the keys of the section
253− foreach my $ini_key (keys %{$old_unit->{$section_name}}) {
289+ foreach my $ini_key (keys(%{$old_unit->{$section_name}})) {
254290 delete $ini_cmp{$ini_key};
255291 my @old_value = @{$old_unit->{$section_name}{$ini_key}};
256292 # If the key is missing in the new unit, they are different...
257293 if (not $new_unit->{$section_name}{$ini_key}) {
258− # ... unless the key that is now missing was the reload trigger
259− if ($section_name eq 'Unit' and $ini_key eq 'X-Reload-Triggers') {
294+ # ... unless the key that is now missing is one of the ignored keys
295+ if ($section_name eq 'Unit' and defined($unit_section_ignores{$ini_key})) {
260296 next;
261297 }
262298 return 1;
@@ -264,19 +300,30 @@ sub compare_units {
264300 my @new_value = @{$new_unit->{$section_name}{$ini_key}};
265301 # If the contents are different, the units are different
266302 if (not $comp_array->(\@old_value, \@new_value)) {
267− # Check if only the reload triggers changed
268− if ($section_name eq 'Unit' and $ini_key eq 'X-Reload-Triggers') {
269− $ret = 2;
270− } else {
271− return 1;
303+ # Check if only the reload triggers changed or one of the ignored keys
304+ if ($section_name eq 'Unit') {
305+ if ($ini_key eq 'X-Reload-Triggers') {
306+ $ret = 2;
307+ next;
308+ } elsif (defined($unit_section_ignores{$ini_key})) {
309+ next;
310+ }
272311 }
312+ return 1;
273313 }
274314 }
275315 # A key was introduced that was missing in the old unit
276316 if (%ini_cmp) {
277− if ($section_name eq 'Unit' and %ini_cmp == 1 and defined($ini_cmp{'X-Reload-Triggers'})) {
278− # If the newly introduced key was the reload triggers, reload the unit
279− $ret = 2;
317+ if ($section_name eq 'Unit') {
318+ foreach my $ini_key (keys(%ini_cmp)) {
319+ if ($ini_key eq 'X-Reload-Triggers') {
320+ $ret = 2;
321+ } elsif (defined($unit_section_ignores{$ini_key})) {
322+ next;
323+ } else {
324+ return 1;
325+ }
326+ }
280327 } else {
281328 return 1;
282329 }
@@ -284,10 +331,14 @@ sub compare_units {
284331 }
285332 # A section was introduced that was missing in the old unit
286333 if (%section_cmp) {
287− if (%section_cmp == 1 and defined($section_cmp{'Unit'}) and %{$new_unit->{'Unit'}} == 1 and defined(%{$new_unit->{'Unit'}}{'X-Reload-Triggers'})) {
288− # If a new [Unit] section was introduced that only contains X-Reload-Triggers,
289− # reload instead of restarting
290− $ret = 2;
334+ if (%section_cmp == 1 and defined($section_cmp{'Unit'})) {
335+ foreach my $ini_key (keys(%{$new_unit->{'Unit'}})) {
336+ if (not defined($unit_section_ignores{$ini_key})) {
337+ return 1;
338+ } elsif ($ini_key eq 'X-Reload-Triggers') {
339+ $ret = 2;
340+ }
341+ }
291342 } else {
292343 return 1;
293344 }
@@ -343,11 +394,11 @@ sub handleModifiedUnit {
343394 my $socket_activated = 0;
344395 if ($unit =~ /\.service$/) {
345396 my @sockets = split(/ /, join(" ", @{$unitInfo{Service}{Sockets} // []}));
346− if (scalar @sockets == 0) {
397+ if (scalar(@sockets) == 0) {
347398 @sockets = ("$baseName.socket");
348399 }
349400 foreach my $socket (@sockets) {
350− if (defined $activePrev->{$socket}) {
401+ if (defined($activePrev->{$socket})) {
351402 # We can now be sure this is a socket-activate unit
352403
353404 $unitsToStop->{$socket} = 1;
@@ -355,7 +406,11 @@ sub handleModifiedUnit {
355406 # exist in new configuration:
356407 if (-e "$out/etc/systemd/system/$socket") {
357408 $unitsToStart->{$socket} = 1;
358− recordUnit($startListFile, $socket);
409+ if ($unitsToStart eq $unitsToRestart) {
410+ recordUnit($restartListFile, $socket);
411+ } else {
412+ recordUnit($startListFile, $socket);
413+ }
359414 $socket_activated = 1;
360415 }
361416 # Remove from units to reload so we don't restart and reload
@@ -373,7 +428,11 @@ sub handleModifiedUnit {
373428 # service gets restarted if we're interrupted.
374429 if (!$socket_activated) {
375430 $unitsToStart->{$unit} = 1;
376− recordUnit($startListFile, $unit);
431+ if ($unitsToStart eq $unitsToRestart) {
432+ recordUnit($restartListFile, $unit);
433+ } else {
434+ recordUnit($startListFile, $unit);
435+ }
377436 }
378437
379438 $unitsToStop->{$unit} = 1;
@@ -401,8 +460,8 @@ $unitsToRestart{$_} = 1 foreach
401460$unitsToReload{$_} = 1 foreach
402461 split('\n', read_file($reloadListFile, err_mode => 'quiet') // "");
403462
404−my $activePrev = getActiveUnits;
405−while (my ($unit, $state) = each %{$activePrev}) {
463+my $activePrev = getActiveUnits();
464+while (my ($unit, $state) = each(%{$activePrev})) {
406465 my $baseUnit = $unit;
407466
408467 my $prevUnitFile = "/etc/systemd/system/$baseUnit";
@@ -462,9 +521,9 @@ while (my ($unit, $state) = each %{$activePrev}) {
462521 my %old_unit_info = parse_unit($prevUnitFile);
463522 my %new_unit_info = parse_unit($newUnitFile);
464523 my $diff = compare_units(\%old_unit_info, \%new_unit_info);
465− if ($diff eq 1) {
524+ if ($diff == 1) {
466525 handleModifiedUnit($unit, $baseName, $newUnitFile, \%new_unit_info, $activePrev, \%unitsToStop, \%unitsToStart, \%unitsToReload, \%unitsToRestart, \%unitsToSkip);
467− } elsif ($diff eq 2 and not $unitsToRestart{$unit}) {
526+ } elsif ($diff == 2 and not $unitsToRestart{$unit}) {
468527 $unitsToReload{$unit} = 1;
469528 recordUnit($reloadListFile, $unit);
470529 }
@@ -475,11 +534,11 @@ while (my ($unit, $state) = each %{$activePrev}) {
475534sub pathToUnitName {
476535 my ($path) = @_;
477536 # Use current version of systemctl binary before daemon is reexeced.
478− open my $cmd, "-|", "$curSystemd/systemd-escape", "--suffix=mount", "-p", $path
537+ open(my $cmd, "-|", "$curSystemd/systemd-escape", "--suffix=mount", "-p", $path)
479538 or die "Unable to escape $path!\n";
480− my $escaped = join "", <$cmd>;
481− chomp $escaped;
482− close $cmd or die;
539+ my $escaped = join("", <$cmd>);
540+ chomp($escaped);
541+ close($cmd) or die('Unable to close systemd-escape pipe');
483542 return $escaped;
484543}
485544
@@ -488,13 +547,13 @@ sub pathToUnitName {
488547# automatically by starting local-fs.target. FIXME: might be nicer if
489548# we generated units for all mounts; then we could unify this with the
490549# unit checking code above.
491−my ($prevFss, $prevSwaps) = parseFstab "/etc/fstab";
492−my ($newFss, $newSwaps) = parseFstab "$out/etc/fstab";
493−foreach my $mountPoint (keys %$prevFss) {
550+my ($prevFss, $prevSwaps) = parseFstab("/etc/fstab");
551+my ($newFss, $newSwaps) = parseFstab("$out/etc/fstab");
552+foreach my $mountPoint (keys(%$prevFss)) {
494553 my $prev = $prevFss->{$mountPoint};
495554 my $new = $newFss->{$mountPoint};
496555 my $unit = pathToUnitName($mountPoint);
497− if (!defined $new) {
556+ if (!defined($new)) {
498557 # Filesystem entry disappeared, so unmount it.
499558 $unitsToStop{$unit} = 1;
500559 } elsif ($prev->{fsType} ne $new->{fsType} || $prev->{device} ne $new->{device}) {
@@ -510,10 +569,10 @@ foreach my $mountPoint (keys %$prevFss) {
510569}
511570
512571# Also handles swap devices.
513−foreach my $device (keys %$prevSwaps) {
572+foreach my $device (keys(%$prevSwaps)) {
514573 my $prev = $prevSwaps->{$device};
515574 my $new = $newSwaps->{$device};
516− if (!defined $new) {
575+ if (!defined($new)) {
517576 # Swap entry disappeared, so turn it off. Can't use
518577 # "systemctl stop" here because systemd has lots of alias
519578 # units that prevent a stop from actually calling
@@ -544,8 +603,8 @@ if ($prevSystemdSystemConfig ne $newSystemdSystemConfig) {
544603sub filterUnits {
545604 my ($units) = @_;
546605 my @res;
547− foreach my $unit (sort(keys %{$units})) {
548− push @res, $unit if !defined $unitsToFilter{$unit};
606+ foreach my $unit (sort(keys(%{$units}))) {
607+ push(@res, $unit) if !defined($unitsToFilter{$unit});
549608 }
550609 return @res;
551610}
@@ -556,9 +615,9 @@ my @unitsToStopFiltered = filterUnits(\%unitsToStop);
556615# Show dry-run actions.
557616if ($action eq "dry-activate") {
558617 print STDERR "would stop the following units: ", join(", ", @unitsToStopFiltered), "\n"
559− if scalar @unitsToStopFiltered > 0;
560− print STDERR "would NOT stop the following changed units: ", join(", ", sort(keys %unitsToSkip)), "\n"
561− if scalar(keys %unitsToSkip) > 0;
618+ if scalar(@unitsToStopFiltered) > 0;
619+ print STDERR "would NOT stop the following changed units: ", join(", ", sort(keys(%unitsToSkip))), "\n"
620+ if scalar(keys(%unitsToSkip)) > 0;
562621
563622 print STDERR "would activate the configuration...\n";
564623 system("$out/dry-activate", "$out");
@@ -579,7 +638,7 @@ if ($action eq "dry-activate") {
579638 $baseName =~ s/\.[a-z]*$//;
580639
581640 # Start units if they were not active previously
582− if (not defined $activePrev->{$unit}) {
641+ if (not defined($activePrev->{$unit})) {
583642 $unitsToStart{$unit} = 1;
584643 next;
585644 }
@@ -599,28 +658,28 @@ if ($action eq "dry-activate") {
599658 unlink($dryReloadByActivationFile);
600659
601660 print STDERR "would restart systemd\n" if $restartSystemd;
602− print STDERR "would reload the following units: ", join(", ", sort(keys %unitsToReload)), "\n"
603− if scalar(keys %unitsToReload) > 0;
604− print STDERR "would restart the following units: ", join(", ", sort(keys %unitsToRestart)), "\n"
605− if scalar(keys %unitsToRestart) > 0;
661+ print STDERR "would reload the following units: ", join(", ", sort(keys(%unitsToReload))), "\n"
662+ if scalar(keys(%unitsToReload)) > 0;
663+ print STDERR "would restart the following units: ", join(", ", sort(keys(%unitsToRestart))), "\n"
664+ if scalar(keys(%unitsToRestart)) > 0;
606665 my @unitsToStartFiltered = filterUnits(\%unitsToStart);
607666 print STDERR "would start the following units: ", join(", ", @unitsToStartFiltered), "\n"
608− if scalar @unitsToStartFiltered;
667+ if scalar(@unitsToStartFiltered);
609668 exit 0;
610669}
611670
612671
613672syslog(LOG_NOTICE, "switching to system configuration $out");
614673
615−if (scalar (keys %unitsToStop) > 0) {
674+if (scalar(keys(%unitsToStop)) > 0) {
616675 print STDERR "stopping the following units: ", join(", ", @unitsToStopFiltered), "\n"
617− if scalar @unitsToStopFiltered;
676+ if scalar(@unitsToStopFiltered);
618677 # Use current version of systemctl binary before daemon is reexeced.
619− system("$curSystemd/systemctl", "stop", "--", sort(keys %unitsToStop));
678+ system("$curSystemd/systemctl", "stop", "--", sort(keys(%unitsToStop)));
620679}
621680
622−print STDERR "NOT restarting the following changed units: ", join(", ", sort(keys %unitsToSkip)), "\n"
623− if scalar(keys %unitsToSkip) > 0;
681+print STDERR "NOT restarting the following changed units: ", join(", ", sort(keys(%unitsToSkip))), "\n"
682+ if scalar(keys(%unitsToSkip)) > 0;
624683
625684# Activate the new configuration (i.e., update /etc, make accounts,
626685# and so on).
@@ -644,7 +703,7 @@ foreach (split('\n', read_file($restartByActivationFile, err_mode => 'quiet') //
644703 $baseName =~ s/\.[a-z]*$//;
645704
646705 # Start units if they were not active previously
647− if (not defined $activePrev->{$unit}) {
706+ if (not defined($activePrev->{$unit})) {
648707 $unitsToStart{$unit} = 1;
649708 recordUnit($startListFile, $unit);
650709 next;
@@ -681,7 +740,7 @@ system("@systemd@/bin/systemctl", "reset-failed");
681740system("@systemd@/bin/systemctl", "daemon-reload") == 0 or $res = 3;
682741
683742# Reload user units
684−open my $listActiveUsers, '-|', '@systemd@/bin/loginctl', 'list-users', '--no-legend';
743+open(my $listActiveUsers, '-|', '@systemd@/bin/loginctl', 'list-users', '--no-legend');
685744while (my $f = <$listActiveUsers>) {
686745 next unless $f =~ /^\s*(?<uid>\d+)\s+(?<user>\S+)/;
687746 my ($uid, $name) = ($+{uid}, $+{user});
@@ -693,25 +752,43 @@ while (my $f = <$listActiveUsers>) {
693752 "@systemd@/bin/systemctl --user start nixos-activation.service");
694753}
695754
696−close $listActiveUsers;
755+close($listActiveUsers);
697756
698757# Set the new tmpfiles
699758print STDERR "setting up tmpfiles\n";
700759system("@systemd@/bin/systemd-tmpfiles", "--create", "--remove", "--exclude-prefix=/dev") == 0 or $res = 3;
701760
761+# Before reloading we need to ensure that the units are still active. They may have been
762+# deactivated because one of their requirements got stopped. If they are inactive
763+# but should have been reloaded, the user probably expects them to be started.
764+if (scalar(keys(%unitsToReload)) > 0) {
765+ for my $unit (keys(%unitsToReload)) {
766+ if (!unit_is_active($unit)) {
767+ # Figure out if we need to start the unit
768+ my %unit_info = parse_unit("$out/etc/systemd/system/$unit");
769+ if (!(parseSystemdBool(\%unit_info, 'Unit', 'RefuseManualStart', 0) || parseSystemdBool(\%unit_info, 'Unit', 'X-OnlyManualStart', 0))) {
770+ $unitsToStart{$unit} = 1;
771+ recordUnit($startListFile, $unit);
772+ }
773+ # Don't reload the unit, reloading would fail
774+ delete %unitsToReload{$unit};
775+ unrecord_unit($reloadListFile, $unit);
776+ }
777+ }
778+}
702779# Reload units that need it. This includes remounting changed mount
703780# units.
704−if (scalar(keys %unitsToReload) > 0) {
705− print STDERR "reloading the following units: ", join(", ", sort(keys %unitsToReload)), "\n";
706− system("@systemd@/bin/systemctl", "reload", "--", sort(keys %unitsToReload)) == 0 or $res = 4;
781+if (scalar(keys(%unitsToReload)) > 0) {
782+ print STDERR "reloading the following units: ", join(", ", sort(keys(%unitsToReload))), "\n";
783+ system("@systemd@/bin/systemctl", "reload", "--", sort(keys(%unitsToReload))) == 0 or $res = 4;
707784 unlink($reloadListFile);
708785}
709786
710787# Restart changed services (those that have to be restarted rather
711788# than stopped and started).
712−if (scalar(keys %unitsToRestart) > 0) {
713− print STDERR "restarting the following units: ", join(", ", sort(keys %unitsToRestart)), "\n";
714− system("@systemd@/bin/systemctl", "restart", "--", sort(keys %unitsToRestart)) == 0 or $res = 4;
789+if (scalar(keys(%unitsToRestart)) > 0) {
790+ print STDERR "restarting the following units: ", join(", ", sort(keys(%unitsToRestart))), "\n";
791+ system("@systemd@/bin/systemctl", "restart", "--", sort(keys(%unitsToRestart))) == 0 or $res = 4;
715792 unlink($restartListFile);
716793}
717794
@@ -723,17 +800,17 @@ if (scalar(keys %unitsToRestart) > 0) {
723800# systemd.
724801my @unitsToStartFiltered = filterUnits(\%unitsToStart);
725802print STDERR "starting the following units: ", join(", ", @unitsToStartFiltered), "\n"
726− if scalar @unitsToStartFiltered;
727−system("@systemd@/bin/systemctl", "start", "--", sort(keys %unitsToStart)) == 0 or $res = 4;
803+ if scalar(@unitsToStartFiltered);
804+system("@systemd@/bin/systemctl", "start", "--", sort(keys(%unitsToStart))) == 0 or $res = 4;
728805unlink($startListFile);
729806
730807
731808# Print failed and new units.
732809my (@failed, @new);
733−my $activeNew = getActiveUnits;
734−while (my ($unit, $state) = each %{$activeNew}) {
810+my $activeNew = getActiveUnits();
811+while (my ($unit, $state) = each(%{$activeNew})) {
735812 if ($state->{state} eq "failed") {
736− push @failed, $unit;
813+ push(@failed, $unit);
737814 next;
738815 }
739816
@@ -743,7 +820,7 @@ while (my ($unit, $state) = each %{$activeNew}) {
743820 chomp($main_status);
744821
745822 if ($main_status ne "0") {
746− push @failed, $unit;
823+ push(@failed, $unit);
747824 next;
748825 }
749826 }
@@ -751,19 +828,19 @@ while (my ($unit, $state) = each %{$activeNew}) {
751828 # Ignore scopes since they are not managed by this script but rather
752829 # created and managed by third-party services via the systemd dbus API.
753830 # This only lists units that are not failed (including ones that are in auto-restart but have not failed previously)
754− if ($state->{state} ne "failed" && !defined $activePrev->{$unit} && $unit !~ /\.scope$/msx) {
755− push @new, $unit;
831+ if ($state->{state} ne "failed" && !defined($activePrev->{$unit}) && $unit !~ /\.scope$/msx) {
832+ push(@new, $unit);
756833 }
757834}
758835
759−if (scalar @new > 0) {
836+if (scalar(@new) > 0) {
760837 print STDERR "the following new units were started: ", join(", ", sort(@new)), "\n"
761838}
762839
763−if (scalar @failed > 0) {
764− my @failed_sorted = sort @failed;
840+if (scalar(@failed) > 0) {
841+ my @failed_sorted = sort(@failed);
765842 print STDERR "warning: the following units failed: ", join(", ", @failed_sorted), "\n\n";
766− system "@systemd@/bin/systemctl status --no-pager --full '" . join("' '", @failed_sorted) . "' >&2";
843+ system("@systemd@/bin/systemctl status --no-pager --full '" . join("' '", @failed_sorted) . "' >&2");
767844 $res = 4;
768845}
769846
@@ -773,4 +850,4 @@ if ($res == 0) {
773850 syslog(LOG_ERR, "switching to system configuration $out failed (status $res)");
774851}
775852
776−exit $res;
853+exit($res);