Backend/Systemd/Unit.pm

filter: Statementstmt 97.2% 140 / 144 Branchbran 79.6% 43 / 54 Conditioncond 69.7% 23 / 33 MC/DCmcdc 36.3% 8 / 22 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 3 totaltotal 84.6% 232 / 274 CC 43 SCAR 21.9
CC
43
Cov
89%
__ANON__
17.4
get_unit_info
11.6
__ANON__
7.3
CRAP 8.9
« Backend/Systemd.pm ↑ Summary Backend/Systemd/Layers.pm »
1 #
2 # This file is part of Config-Model-Systemd
3 #
4 # This software is Copyright (c) 2008-2026 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::Backend::Systemd::Unit ;
11 3 $Config::Model::Backend::Systemd::Unit::VERSION = '0.261.3';
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Mouse ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 extends 'Config::Model::Backend::IniFile';
23  
24 3 with 'Config::Model::Backend::Systemd::Layers';
25  
26 3 has _has_system_file => (
27 is => 'rw',
28 isa => 'Bool',
29 default => 0,
30 );
31  
32 3 my $logger = get_logger("Backend::Systemd::Unit");
33 3 my $user_logger = get_logger("User");
34  
35 55 ▶sub get_unit_info ($self, $file_path) {
Subroutine get_unit_info: called CC=10 SCAR=24.5
Pod undocumented
36 # get info from tree when Unit is children of systemd (app is systemd)
37 55 my $unit_type = $self->node->element_name;
38 55 my $unit_name = $self->node->index_value;
39 55 my $app = $self->instance->application;
40 55 my ($trash, $app_type) = split /-/, $app;
41  
42 # get info from file name (app is systemd-* not -user)
43 55 ▶ if (my $fp = $file_path->basename) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fp = $file_path->basename) 55 0 55
44 55 my ($n,$t) = split /\./, $fp;
45 55 ▶ $unit_type ||= $t;
Condition66%2/3
$unit_type ||= $t
l!l&&r!l&&!r
41140
MC/DC0%0/2
$unit_type ||= $t
$unit_type $t
Truth table66%2/3
A ||= B
A= $unit_typeB= $t
ABresult
1X1
011
000
46 55 ▶ $unit_name ||= $n;
Condition66%2/3
$unit_name ||= $n
l!l&&r!l&&!r
41140
MC/DC0%0/2
$unit_name ||= $n
$unit_name $n
Truth table66%2/3
A ||= B
A= $unit_nameB= $n
ABresult
1X1
011
000
47 }
48  
49 # fallback to app type when file is name without unit type
50 55 ▶ $unit_type ||= $app_type if ($app_type and $app_type ne 'user');
Branch: 1 branch
BranchTrueFalseTotal
if $app_type and $app_type ne "user" 14 41 55
Condition66%4/6
$app_type and $app_type ne "user"
!ll&&!rl&&r
172414
$unit_type ||= $app_type
l!l&&r!l&&!r
1400
MC/DC100%2/2
$app_type and $app_type ne "user"
$app_type $app_type ne "user"
MC/DC0%0/2
$unit_type ||= $app_type
$unit_type $app_type
Truth table100%3/3
A and B
A= $app_typeB= $app_type ne "user"
ABresult
0X0
100
111
Truth table33%1/3
A ||= B
A= $unit_typeB= $app_type
ABresult
1X1
011
000
51  
52 55 ▶ Config::Model::Exception::User->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $unit_type 0 55 55
53 object => $self,
54 error => "Unknown unit type. Please add type to file name. e.g. "
55 . $file_path->basename.".service or socket..."
56 ) unless $unit_type;
57  
58 # safety check
59 55 ▶ if ($app !~ /^systemd(-user)?$/ and $app !~ /^systemd-$unit_type/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app =~ /^systemd(-user)?$/u or $app =~ /^systemd-$unit_type/u) 0 55 55
Condition66%2/3
$app =~ /^systemd(-user)?$/u or $app =~ /^systemd-$unit_type/u
l!l&&r!l&&!r
41140
MC/DC0%0/2
$app =~ /^systemd(-user)?$/u or $app =~ /^systemd-$unit_type/u
$app =~ /^systemd(-user)?$/u $app =~ /^systemd-$unit_type/u
Truth table66%2/3
A or B
A= $app =~ /^systemd(-user)?$/uB= $app =~ /^systemd-$unit_type/u
ABresult
1X1
011
000
60 0 Config::Model::Exception::User->throw(
61 objet => $self->node,
62 error => "Unit type $unit_type does not match app $app"
63 );
64 }
65  
66 55 return ($unit_name, $unit_type);
67 }
68  
69 40 ▶around read => sub ($orig, $self, %args) {
Subroutine __ANON__: called CC=17 SCAR=28.6
70 # enable 2 styles of comments (gh #1)
71 40 $args{comment_delimiter} = "#;";
72  
73 # args are:
74 # root => './my_test', # fake root directory, used for tests
75 # config_dir => /etc/foo', # absolute path
76 # file => 'foo.conf', # file name
77 # file_path => './my_test/etc/foo/foo.conf'
78 # check => yes|no|skip
79  
80 40 ▶ if ($self->instance->application =~ /-file$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->instance->application =~ /-file$/u) 7 33 40
81 # allow non-existent file to let user start from scratch
82 7 ▶ return 1 unless $args{file_path}->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"file_path"}->exists 2 5 7
83  
84 5 return $self->load_ini_file($orig, %args);
85 }
86  
87 33 my ($unit_name, $unit_type) = $self->get_unit_info($args{file_path});
88 33 my $app = $self->instance->application;
89  
90 33 my @default_directories;
91 33 ▶ if ($app !~ /-user$/ or not $args{file_path}->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app =~ /-user$/u and $args{"file_path"}->exists) 24 9 33
Condition100%3/3
$app =~ /-user$/u and $args{"file_path"}->exists
!ll&&!rl&&r
1779
MC/DC100%2/2
$app =~ /-user$/u and $args{"file_path"}->exists
$app =~ /-user$/u $args{"file_path"}->exists
Truth table100%3/3
A and B
A= $app =~ /-user$/uB= $args{"file_path"}->exists
ABresult
0X0
100
111
92 # this user file may overrides an existing service file
93 # so we don't read these default files
94 24 @default_directories = $self->default_directories;
95 }
96  
97 33 $self->node->instance->layered_start;
98 33 ▶ my $root = $args{root} || path('/');
Condition33%1/3
$args{'root'} || path('/')
l!l&&r!l&&!r
3300
MC/DC0%0/2
$args{'root'} || path('/')
$args{'root'} path('/')
Truth table33%1/3
A || B
A= $args{'root'}B= path('/')
ABresult
1X1
011
000
99 33 ▶ my $cwd = $args{root} || path('.');
Condition33%1/3
$args{'root'} || path('.')
l!l&&r!l&&!r
3300
MC/DC0%0/2
$args{'root'} || path('.')
$args{'root'} path('.')
Truth table33%1/3
A || B
A= $args{'root'}B= path('.')
ABresult
1X1
011
000
100  
101 # load layers for this service
102 33 my $found_unit = 0;
103 33 foreach my $layer (@default_directories) {
104 113 ▶ my $local_root = $layer =~ m!^/! ? $root : $cwd;
Branch: 1 branch
BranchTrueFalseTotal
$layer =~ m[^/]u ? : 113 0 113
105 113 my $layer_dir = $local_root->child($layer);
106 113 ▶ next unless $layer_dir->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $layer_dir->is_dir 81 32 113
107  
108 32 my $layer_file = $layer_dir->child($unit_name.'.'.$unit_type);
109 32 ▶ next unless $layer_file->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $layer_file->exists 14 18 32
110  
111 18 $user_logger->info("Reading unit '$unit_type' '$unit_name' from '$layer_file'.");
112 18 $self->load_ini_file($orig, %args, file_path => $layer_file);
113 18 $found_unit++;
114  
115 # TODO: may also need to read files in
116 # $unit_name.'.'.$unit_type.'.d' to get all default values
117 # (e.g. /lib/systemd/system/rc-local.service.d/debian.conf)
118 }
119 33 $self->node->instance->layered_stop;
120  
121 33 ▶ if ($found_unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_unit) { } 18 15 33
122 18 $self->_has_system_file(1);
123 }
124 else {
125 15 $user_logger->info("Could not find unit files for $unit_type name $unit_name");
126 }
127  
128 # now read editable file (files that can be edited with systemctl edit <unit>.<type>
129 # for systemd -> /etc/ systemd/system/unit.type.d/override.conf
130 # for user -> ~/.local/systemd/user/*.conf
131 # for local file -> $args{filexx}
132  
133 33 my $service_path;
134 33 ▶ if ($app =~ /-user$/ and $args{file_path}->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app =~ /-user$/u and $args{'file_path'}->exists) { } 9 24 33
Condition100%3/3
$app =~ /-user$/u and $args{"file_path"}->exists
!ll&&!rl&&r
1779
MC/DC100%2/2
$app =~ /-user$/u and $args{"file_path"}->exists
$app =~ /-user$/u $args{"file_path"}->exists
Truth table100%3/3
A and B
A= $app =~ /-user$/uB= $args{"file_path"}->exists
ABresult
0X0
100
111
135 # this use file may override an existing service file
136 9 $service_path = $args{file_path} ;
137 }
138 else {
139 24 $service_path = $args{file_path}->parent->child("$unit_name.$unit_type.d/override.conf");
140 }
141  
142 33 ▶ if ($service_path->exists and $service_path->realpath eq '/dev/null') {
Branch: 2 branches
BranchTrueFalseTotal
if ($service_path->exists and $service_path->realpath eq '/dev/null') { } 0 33 33
elsif ($service_path->exists) { } 26 7 33
Condition66%2/3
$service_path->exists and $service_path->realpath eq "/dev/null"
!ll&&!rl&&r
7260
MC/DC0%0/2
$service_path->exists and $service_path->realpath eq "/dev/null"
$service_path->exists $service_path->realpath eq "/dev/null"
Truth table66%2/3
A and B
A= $service_path->existsB= $service_path->realpath eq "/dev/null"
ABresult
0X0
100
111
143 0 $logger->debug("skipping unit $unit_type name $unit_name from $service_path");
144 }
145 elsif ($service_path->exists) {
146 26 $logger->debug("reading unit $unit_type name $unit_name from $service_path");
147 26 $self->load_ini_file($orig, %args, file_path => $service_path);
148 }
149 33 return 1;
150 3 };
151  
152 49 ▶sub load_ini_file ($self, $orig_read, %args) {
Subroutine load_ini_file: called CC=2 SCAR=7.0
Pod undocumented
153 49 $logger->debug("opening file '".$args{file_path}."' to read");
154  
155 49 my $res = $self->$orig_read( %args );
156 49 ▶ die "failed ". $args{file_path}." read" unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 49 49
157 49 return;
158 };
159  
160 # overrides call to node->load_data
161 49 ▶sub load_data ($self, %args) {
Subroutine load_data: called CC=1 SCAR=0.0
Pod undocumented
162 49 my $check = $args{check};
163 49 my $data = $args{data} ;
164  
165 122 ▶ my $disp_leaf = sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
166 122 my ($scanner, $data, $node,$element_name,$index, $leaf_object) = @_ ;
167 122 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 0 122 122
168 0 ▶ Config::Model::Exception::User->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $check eq "yes" 0 0 0
169 object => $leaf_object,
170 error => "Cannot store twice the same value ('"
171 .join("', '",@$data). "'). "
172 ."Is '$element_name' line duplicated in config file ? "
173 ."You can use -force option to load value '". $data->[-1]."'."
174 ) if $check eq 'yes';
175 0 $data = $data->[-1];
176 }
177 # remove this translation after Config::Model 2.146
178 122 ▶ if ($leaf_object->value_type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf_object->value_type eq "boolean") 9 113 122
179 9 ▶ $data = 'yes' if $data eq 'on';
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "on" 1 8 9
180 9 ▶ $data = 'no' if $data eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "off" 0 9 9
181 }
182 122 $leaf_object->store(value => $data, check => $check);
183 49 } ;
184  
185 151 ▶ my $unit_cb = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
186 151 my ($scanner, $data_ref,$node,@elements) = @_ ;
187  
188 # read data in the model order
189 151 foreach my $elt (@elements) {
190 13381 my $unit_data = delete $data_ref->{$elt}; # extract relevant data
191 13381 ▶ next unless defined $unit_data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unit_data 13025 356 13381
192 356 $scanner->scan_element($unit_data, $node,$elt) ;
193 }
194 # read accepted elements
195 151 foreach my $elt (sort keys %$data_ref) {
196 2 my $unit_data = $data_ref->{$elt}; # extract relevant data
197 2 $scanner->scan_element($unit_data, $node,$elt) ;
198 }
199 49 };
200  
201 # this setup is required because IniFile backend cannot push value
202 # coming from several ini files on a single list element. (even
203 # though keys can be repeated in a single ini file and stored as
204 # list in a single config element, this is not possible if the
205 # list values come from several files)
206 134 ▶ my $list_cb = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
207 134 my ($scanner, $data,$node,$element_name,@idx) = @_ ;
208 134 ▶ my $list_ref = ref($data) ? $data : [ $data ];
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 6 128 134
209 134 my $list_obj= $node->fetch_element(name => $element_name, check => $check);
210 134 foreach my $d (@$list_ref) {
211 140 $list_obj->push($d); # push also empty values
212 }
213  
214 49 };
215  
216 49 my $scan = Config::Model::ObjTreeScanner-> new (
217 node_content_cb => $unit_cb,
218 list_element_cb => $list_cb,
219 leaf_cb => $disp_leaf,
220 ) ;
221  
222 49 $scan->scan_node($data, $self->node) ;
223 49 return;
224 }
225  
226 23 ▶around 'write' => sub ($orig, $self, %args) {
Subroutine __ANON__: called CC=6 SCAR=17.9
227 # args are:
228 # root => './my_test', # fake root directory, userd for tests
229 # config_dir => /etc/foo', # absolute path
230 # file => 'foo.conf', # file name
231 # file_path => './my_test/etc/foo/foo.conf'
232 # check => yes|no|skip
233  
234 23 ▶ if ($self->node->grab_value('disable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->node->grab_value("disable")) 1 22 23
235 1 my $fp = $args{file_path};
236 1 ▶ if ($fp->realpath ne '/dev/null') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fp->realpath ne "/dev/null") 1 0 1
237 1 $user_logger->info("symlinking file $fp to /dev/null");
238 1 $fp->remove;
239 1 symlink ('/dev/null', $fp->stringify);
240 }
241 1 return 1;
242 }
243  
244 22 my ($unit_name, $unit_type) = $self->get_unit_info($args{file_path});
245  
246 22 my $app = $self->instance->application;
247 22 my $service_path;
248  
249 # check if service has files in $self->default_directories
250 # yes -> use a a file on $unit_name.$unit_type.d directry
251 # no -> create a $args{file_path} file
252 22 ▶ if ($app =~ /-(user|file)$/ and not $self->_has_system_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app =~ /-(user|file)$/u and not $self->_has_system_file) { } 10 12 22
Condition100%3/3
$app =~ /-(user|file)$/u and not $self->_has_system_file
!ll&&!rl&&r
9310
MC/DC100%2/2
$app =~ /-(user|file)$/u and not $self->_has_system_file
$app =~ /-(user|file)$/u not $self->_has_system_file
Truth table100%3/3
A and not(B)
A= $app =~ /-(user|file)$/uB= not $self->_has_system_file
ABresult
0X0
100
111
253 10 $service_path = $args{file_path};
254  
255 10 $logger->debug("writing unit to $service_path");
256 10 $self->$orig(%args, file_path => $service_path);
257 }
258 else {
259 12 my $dir = $args{file_path}->parent->child("$unit_name.$unit_type.d");
260 12 $dir->mkpath({ mode => oct(755) });
261 12 $service_path = $dir->child('override.conf');
262  
263 12 $logger->debug("writing unit to $service_path");
264 12 $self->$orig(%args, file_path => $service_path);
265  
266 12 ▶ if (scalar $dir->children == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $dir->children == 0) 3 9 12
267 # remove empty dir
268 3 $logger->info("Removing empty dir $dir");
269 3 rmdir $dir;
270 }
271 }
272 22 return 1;
273 3 };
274  
275 6389 ▶around _write_leaf => sub ($orig, $self, $args, $node, $elt) {
Subroutine __ANON__: called CC=2 SCAR=6.9
276 # must skip disable element which cannot be hidden :-(
277 6389 ▶ if ($elt eq 'disable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt eq 'disable') { } 22 6367 6389
278 22 return '';
279 } else {
280 6367 return $self->$orig($args, $node, $elt);
281 }
282 3 };
283  
284 3 ▶no Mouse ;
Subroutine BEGIN: called CC=1 SCAR=0.0
285 3 __PACKAGE__->meta->make_immutable ;
286  
287 3 1;
288  
289 # ABSTRACT: R/W backend for systemd unit files
290  
291 __END__
« Backend/Systemd.pm ↑ Summary Backend/Systemd/Layers.pm »