--- doc/install/linux/install.pl 2024/08/05 13:36:59 1.95 +++ doc/install/linux/install.pl 2024/11/26 22:08:36 1.96 @@ -78,7 +78,7 @@ if (!open(LOG,">>loncapa_install.log")) &mt('Stopping execution.')."\n"; exit; } else { - print LOG '$Id: install.pl,v 1.95 2024/08/05 13:36:59 raeburn Exp $'."\n"; + print LOG '$Id: install.pl,v 1.96 2024/11/26 22:08:36 raeburn Exp $'."\n"; } # @@ -2979,46 +2979,87 @@ sub check_systemd_update { if (-d '/etc/systemd/system/'.$service.'.d') { if (-e '/etc/systemd/system/'.$service.'.d/override.conf') { if (open(my $fh,'<','/etc/systemd/system/'.$service.'.d/override.conf')) { - my ($inservice,$addservice,$protectoff,$linenum,$change,@lines); + my ($category,$addservice,$protectoff,$linenum,$change, + %lines,@move,@nocat,@ordered); + $linenum = 0; while (my $entry = <$fh>) { $linenum ++; chomp($entry); - if ($entry eq '[Service]') { - if (!$protectoff) { - $inservice = $linenum; - push(@lines,$entry); + if ($entry =~ /^\s*\[([^\]]+)\]\s*$/) { + $category = $1; + if ($category =~ /^Service$/i) { + push(@ordered,'Service'); } else { - $addservice = 1; - next; + push(@ordered,$category); } - } - if ($entry =~ /^ProtectHome\s*=\s*([\w-]+)\s*$/) { + } elsif ($entry =~ /^ProtectHome\s*=\s*([\w-]+)\s*$/) { my $value = $1; - if ($protectoff) { - next; - if (lc($value) eq 'no') { - $protectoff = $linenum; - push(@lines,$entry); + next if ($protectoff); + if (lc($value) eq 'no') { + if ($category =~ /^Service$/i) { + push(@{$lines{'Service'}},$entry); } else { - if ($protectoff) { - next; - } else { - push(@lines,'ProtectHome=no'); - $protectoff = $linenum; - $change = $linenum; - } + push(@move,$entry); + $change = $linenum; + } + } else { + if ($category =~ /^Service$/i) { + push(@{$lines{'Service'}},'ProtectHome=no'); + } else { + push(@move,'ProtectHome=no'); } + $change = $linenum; + } + $protectoff = $linenum; + } else { + next if ($entry =~ /^\s*$/); + if ($category =~ /^Service$/i) { + push(@{$lines{'Service'}},$entry); + } elsif ($category ne '') { + push(@{$lines{$category}},$entry); + } else { + push(@nocat,$entry); } } } close($fh); + unless (grep(/^Service$/,@ordered)) { + $addservice = 1; + unshift(@ordered,'Service'); + } + if (!$protectoff) { + push(@{$lines{'Service'}},'ProtectHome=no'); + } if ($addservice || $change || !$protectoff) { if (open(my $fh,'>','/etc/systemd/system/'.$service.'.d/override.conf')) { - if ($addservice) { - print $fh "[Service]\n"; + if (@ordered) { + foreach my $category (@ordered) { + print $fh "[$category]\n"; + if (ref($lines{$category}) eq 'ARRAY') { + foreach my $item (@{$lines{$category}}) { + print $fh "$item\n"; + } + } + if ($category eq 'Service') { + if (@move) { + foreach my $item (@move) { + if ($item =~ /^ProtectHome\s*=\s*no\s*$/i) { + unless (grep/^ProtectHome\s*=\s*no\s*$/i,@{$lines{$category}}) { + print $fh "$item\n"; + } + } else { + print $fh "$item\n"; + } + } + } + } + print $fh "\n"; + } } - foreach my $entry (@lines) { - print $fh "$entry\n"; + if (@nocat) { + foreach my $item (@nocat) { + print $fh "$item\n"; + } } close($fh); print_and_log('Updated /etc/systemd/system/'.$service.'.d/override.conf');