Add cat_systemd sub to parse systemd unit config

This commit is contained in:
Ilia Ross
2025-01-15 02:27:50 +02:00
parent dedfc90e71
commit 468a570adf

View File

@@ -2550,6 +2550,44 @@ if ($init_mode eq "systemd") {
return wantarray ? (-1, undef) : 0;
}
=head2 cat_systemd(unit)
List given systemd unit file contents
=cut
sub cat_systemd
{
my ($unit) = @_;
my @config;
my $current_section;
my $current_file;
# Execute and parse the system command
&open_execute_command(*CAT, "systemctl cat ".quotemeta($unit), 1, 1);
while (<CAT>) {
s/\r|\n//g;
next if /^$/;
if (/^#\s+(\/.*)$/) {
# File name line, e.g., # /usr/lib/systemd/system/ssh.socket
$current_file = $1;
push @config, { file => $current_file, sections => {} };
}
elsif (/^\[(.+?)\]$/) {
# Section header, e.g., [Unit]
$current_section = $1;
$config[-1]{'sections'}{$current_section} ||= {};
}
elsif (/^([^=]+)=(.*)$/ && $current_section) {
# Key-value pair, e.g., ListenStream=0.0.0.0:22
my ($key, $value) = ($1, $2);
push @{ $config[-1]{'sections'}{$current_section}{$key} }, $value;
}
}
close(CAT);
return \@config;
}
=head2 reboot_system
Immediately reboots the system.