]> git.proxmox.com Git - pve-common.git/blame - src/PVE/Systemd.pm
systemd: add CPUWeight encoding
[pve-common.git] / src / PVE / Systemd.pm
CommitLineData
eae43687 1package PVE::Systemd;
f024a872
WB
2
3use strict;
4use warnings;
5
6use Net::DBus qw(dbus_uint32 dbus_uint64);
7use Net::DBus::Callback;
8use Net::DBus::Reactor;
9
e65624a3
SI
10use PVE::Tools qw(file_set_contents file_get_contents trim);
11
b4f88e88
TL
12sub escape_unit {
13 my ($val, $is_path) = @_;
14
15 # NOTE: this is not complete, but enough for our needs. normally all
16 # characters which are not alpha-numerical, '.' or '_' would need escaping
17 $val =~ s/\-/\\x2d/g;
18
19 if ($is_path) {
20 $val =~ s/^\///g;
21 $val =~ s/\/$//g;
22 }
23 $val =~ s/\//-/g;
24
25 return $val;
26}
27
28sub unescape_unit {
29 my ($val) = @_;
30
31 $val =~ s/-/\//g;
32 $val =~ s/\\x([a-fA-F0-9]{2})/chr(hex($1))/eg;
33
34 return $val;
35}
36
b8d15bfd
WB
37# $code should take the parameters ($interface, $reactor, $finish_callback).
38#
39# $finish_callback can be used by dbus-signal-handlers to stop the reactor.
40#
41# In order to even start waiting on the reactor, $code needs to return undef, if it returns a
42# defined value instead, it is assumed that this is the result already and we can stop.
f024a872 43# NOTE: This calls the dbus main loop and must not be used when another dbus
b8d15bfd
WB
44# main loop is being used as we need to wait signals.
45sub systemd_call($;$) {
46 my ($code, $timeout) = @_;
47
48 my $bus = Net::DBus->system();
49 my $reactor = Net::DBus::Reactor->main();
50
51 my $service = $bus->get_service('org.freedesktop.systemd1');
52 my $if = $service->get_object('/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager');
53
54 my ($finished, $current_result, $timer);
55 my $finish_callback = sub {
56 my ($result) = @_;
57
58 $current_result = $result;
59
60 $finished = 1;
61
62 if (defined($timer)) {
63 $reactor->remove_timeout($timer);
64 $timer = undef;
65 }
66
67 if (defined($reactor)) {
68 $reactor->shutdown();
69 $reactor = undef;
70 }
71 };
72
73 my $result = $code->($if, $reactor, $finish_callback);
74 # Are we done immediately?
75 return $result if defined $result;
76
77 # Alterantively $finish_callback may have been called already?
78 return $current_result if $finished;
79
80 # Otherwise wait:
81 my $on_timeout = sub {
82 $finish_callback->(undef);
83 die "timeout waiting on systemd\n";
84 };
85 $timer = $reactor->add_timeout($timeout * 1000, Net::DBus::Callback->new(method => $on_timeout))
86 if defined($timeout);
87
88 $reactor->run();
89 $reactor->shutdown() if defined($reactor); # $finish_callback clears it
90
91 return $current_result;
92}
93
f024a872
WB
94# Polling the job status instead doesn't work because this doesn't give us the
95# distinction between success and failure.
96#
97# Note that the description is mandatory for security reasons.
98sub enter_systemd_scope {
99 my ($unit, $description, %extra) = @_;
100 die "missing description\n" if !defined($description);
101
102 my $timeout = delete $extra{timeout};
103
104 $unit .= '.scope';
105 my $properties = [ [PIDs => [dbus_uint32($$)]] ];
106
107 foreach my $key (keys %extra) {
108 if ($key eq 'Slice' || $key eq 'KillMode') {
109 push @{$properties}, [$key, $extra{$key}];
39bf4956 110 } elsif ($key eq 'CPUShares' || $key eq 'CPUWeight') {
f024a872
WB
111 push @{$properties}, [$key, dbus_uint64($extra{$key})];
112 } elsif ($key eq 'CPUQuota') {
113 push @{$properties}, ['CPUQuotaPerSecUSec',
b8d15bfd 114 dbus_uint64($extra{$key} * 10_000)];
f024a872
WB
115 } else {
116 die "Don't know how to encode $key for systemd scope\n";
117 }
118 }
119
b8d15bfd
WB
120 systemd_call(sub {
121 my ($if, $reactor, $finish_cb) = @_;
f024a872 122
b8d15bfd 123 my $job;
f024a872 124
b8d15bfd
WB
125 $if->connect_to_signal('JobRemoved', sub {
126 my ($id, $removed_job, $signaled_unit, $result) = @_;
127 return if $signaled_unit ne $unit || $removed_job ne $job;
128 if ($result ne 'done') {
129 # I seem to remember $reactor->run() catching die() at some point?
130 # so better call finish to be sure...:
131 $finish_cb->(0);
132 die "systemd job failed\n";
133 } else {
134 $finish_cb->(1);
135 }
136 });
f024a872 137
b8d15bfd 138 $job = $if->StartTransientUnit($unit, 'fail', $properties, []);
f024a872 139
b8d15bfd
WB
140 return undef;
141 }, $timeout);
142}
f024a872 143
b8d15bfd
WB
144sub wait_for_unit_removed($;$) {
145 my ($unit, $timeout) = @_;
146
147 systemd_call(sub {
148 my ($if, $reactor, $finish_cb) = @_;
149
150 my $unit_obj = eval { $if->GetUnit($unit) };
151 return 1 if !$unit_obj;
152
153 $if->connect_to_signal('UnitRemoved', sub {
154 my ($id, $removed_unit) = @_;
155 $finish_cb->(1) if $removed_unit eq $unit_obj;
156 });
157
158 # Deal with what we lost between GetUnit() and connecting to UnitRemoved:
159 my $unit_obj_new = eval { $if->GetUnit($unit) };
160 if (!$unit_obj_new) {
161 return 1;
162 }
163
164 return undef;
165 }, $timeout);
f024a872
WB
166}
167
e65624a3
SI
168sub read_ini {
169 my ($filename) = @_;
170
171 my $content = file_get_contents($filename);
172 my @lines = split /\n/, $content;
173
174 my $result = {};
175 my $section;
176
177 foreach my $line (@lines) {
178 $line = trim($line);
179 if ($line =~ m/^\[([^\]]+)\]/) {
180 $section = $1;
181 if (!defined($result->{$section})) {
182 $result->{$section} = {};
183 }
184 } elsif ($line =~ m/^(.*?)=(.*)$/) {
185 my ($key, $val) = ($1, $2);
186 if (!$section) {
187 warn "key value pair found without section, skipping\n";
188 next;
189 }
190
191 if ($result->{$section}->{$key}) {
192 # make duplicate properties to arrays to keep the order
193 my $prop = $result->{$section}->{$key};
194 if (ref($prop) eq 'ARRAY') {
195 push @$prop, $val;
196 } else {
197 $result->{$section}->{$key} = [$prop, $val];
198 }
199 } else {
200 $result->{$section}->{$key} = $val;
201 }
202 }
203 # ignore everything else
204 }
205
206 return $result;
207};
208
209sub write_ini {
210 my ($ini, $filename) = @_;
211
212 my $content = "";
213
214 foreach my $sname (sort keys %$ini) {
215 my $section = $ini->{$sname};
216
217 $content .= "[$sname]\n";
218
219 foreach my $pname (sort keys %$section) {
220 my $prop = $section->{$pname};
221
222 if (!ref($prop)) {
223 $content .= "$pname=$prop\n";
224 } elsif (ref($prop) eq 'ARRAY') {
225 foreach my $val (@$prop) {
226 $content .= "$pname=$val\n";
227 }
228 } else {
229 die "invalid property '$pname'\n";
230 }
231 }
232 $content .= "\n";
233 }
234
235 file_set_contents($filename, $content);
236};
237
f024a872 2381;