summaryrefslogtreecommitdiffstats
path: root/nixos/modules
diff options
context:
space:
mode:
authorJanne Heß <janne@hess.ooo>2022-03-06 22:43:47 +0100
committerJanne Heß <janne@hess.ooo>2022-03-11 14:05:19 +0100
commitbc58430068d0bd0ffd3ef561a92a05f5970d149c (patch)
treedee35f2bf657e7056bdae49e67746089664334a1 /nixos/modules
parent3052d3aa50674f2cfeee7c7ddf42c36d84013e48 (diff)
nixos/switch-to-configuration: Fix reloading of stopped services
Diffstat (limited to 'nixos/modules')
-rw-r--r--nixos/modules/system/activation/switch-to-configuration.pl31
1 files changed, 31 insertions, 0 deletions
diff --git a/nixos/modules/system/activation/switch-to-configuration.pl b/nixos/modules/system/activation/switch-to-configuration.pl
index a67a9b057787..d83198bc346e 100644
--- a/nixos/modules/system/activation/switch-to-configuration.pl
+++ b/nixos/modules/system/activation/switch-to-configuration.pl
@@ -104,6 +104,19 @@ sub getActiveUnits {
return $res;
}
+# Returns whether a systemd unit is active
+sub unit_is_active {
+ my ($unit_name) = @_;
+
+ my $mgr = Net::DBus->system->get_service('org.freedesktop.systemd1')->get_object('/org/freedesktop/systemd1');
+ my $units = $mgr->ListUnitsByNames([$unit_name]);
+ if (@{$units} == 0) {
+ return 0;
+ }
+ my $active_state = $units->[0]->[3]; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
+ return $active_state eq 'active' || $active_state eq 'activating';
+}
+
sub parseFstab {
my ($filename) = @_;
my ($fss, $swaps);
@@ -744,6 +757,24 @@ close $listActiveUsers;
print STDERR "setting up tmpfiles\n";
system("@systemd@/bin/systemd-tmpfiles", "--create", "--remove", "--exclude-prefix=/dev") == 0 or $res = 3;
+# Before reloading we need to ensure that the units are still active. They may have been
+# deactivated because one of their requirements got stopped. If they are inactive
+# but should have been reloaded, the user probably expects them to be started.
+if (scalar(keys %unitsToReload) > 0) {
+ for my $unit (keys %unitsToReload) {
+ if (!unit_is_active($unit)) {
+ # Figure out if we need to start the unit
+ my %unit_info = parse_unit("$out/etc/systemd/system/$unit");
+ if (!(parseSystemdBool(\%unit_info, 'Unit', 'RefuseManualStart', 0) || parseSystemdBool(\%unit_info, 'Unit', 'X-OnlyManualStart', 0))) {
+ $unitsToStart{$unit} = 1;
+ recordUnit($startListFile, $unit);
+ }
+ # Don't reload the unit, reloading would fail
+ delete %unitsToReload{$unit};
+ unrecord_unit($reloadListFile, $unit);
+ }
+ }
+}
# Reload units that need it. This includes remounting changed mount
# units.
if (scalar(keys %unitsToReload) > 0) {