]>
Commit | Line | Data |
---|---|---|
eae43687 | 1 | package PVE::Systemd; |
f024a872 WB |
2 | |
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Net::DBus qw(dbus_uint32 dbus_uint64); | |
7 | use Net::DBus::Callback; | |
8 | use Net::DBus::Reactor; | |
9 | ||
b4f88e88 TL |
10 | sub escape_unit { |
11 | my ($val, $is_path) = @_; | |
12 | ||
13 | # NOTE: this is not complete, but enough for our needs. normally all | |
14 | # characters which are not alpha-numerical, '.' or '_' would need escaping | |
15 | $val =~ s/\-/\\x2d/g; | |
16 | ||
17 | if ($is_path) { | |
18 | $val =~ s/^\///g; | |
19 | $val =~ s/\/$//g; | |
20 | } | |
21 | $val =~ s/\//-/g; | |
22 | ||
23 | return $val; | |
24 | } | |
25 | ||
26 | sub unescape_unit { | |
27 | my ($val) = @_; | |
28 | ||
29 | $val =~ s/-/\//g; | |
30 | $val =~ s/\\x([a-fA-F0-9]{2})/chr(hex($1))/eg; | |
31 | ||
32 | return $val; | |
33 | } | |
34 | ||
b8d15bfd WB |
35 | # $code should take the parameters ($interface, $reactor, $finish_callback). |
36 | # | |
37 | # $finish_callback can be used by dbus-signal-handlers to stop the reactor. | |
38 | # | |
39 | # In order to even start waiting on the reactor, $code needs to return undef, if it returns a | |
40 | # defined value instead, it is assumed that this is the result already and we can stop. | |
f024a872 | 41 | # NOTE: This calls the dbus main loop and must not be used when another dbus |
b8d15bfd WB |
42 | # main loop is being used as we need to wait signals. |
43 | sub systemd_call($;$) { | |
44 | my ($code, $timeout) = @_; | |
45 | ||
46 | my $bus = Net::DBus->system(); | |
47 | my $reactor = Net::DBus::Reactor->main(); | |
48 | ||
49 | my $service = $bus->get_service('org.freedesktop.systemd1'); | |
50 | my $if = $service->get_object('/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager'); | |
51 | ||
52 | my ($finished, $current_result, $timer); | |
53 | my $finish_callback = sub { | |
54 | my ($result) = @_; | |
55 | ||
56 | $current_result = $result; | |
57 | ||
58 | $finished = 1; | |
59 | ||
60 | if (defined($timer)) { | |
61 | $reactor->remove_timeout($timer); | |
62 | $timer = undef; | |
63 | } | |
64 | ||
65 | if (defined($reactor)) { | |
66 | $reactor->shutdown(); | |
67 | $reactor = undef; | |
68 | } | |
69 | }; | |
70 | ||
71 | my $result = $code->($if, $reactor, $finish_callback); | |
72 | # Are we done immediately? | |
73 | return $result if defined $result; | |
74 | ||
75 | # Alterantively $finish_callback may have been called already? | |
76 | return $current_result if $finished; | |
77 | ||
78 | # Otherwise wait: | |
79 | my $on_timeout = sub { | |
80 | $finish_callback->(undef); | |
81 | die "timeout waiting on systemd\n"; | |
82 | }; | |
83 | $timer = $reactor->add_timeout($timeout * 1000, Net::DBus::Callback->new(method => $on_timeout)) | |
84 | if defined($timeout); | |
85 | ||
86 | $reactor->run(); | |
87 | $reactor->shutdown() if defined($reactor); # $finish_callback clears it | |
88 | ||
89 | return $current_result; | |
90 | } | |
91 | ||
f024a872 WB |
92 | # Polling the job status instead doesn't work because this doesn't give us the |
93 | # distinction between success and failure. | |
94 | # | |
95 | # Note that the description is mandatory for security reasons. | |
96 | sub enter_systemd_scope { | |
97 | my ($unit, $description, %extra) = @_; | |
98 | die "missing description\n" if !defined($description); | |
99 | ||
100 | my $timeout = delete $extra{timeout}; | |
101 | ||
102 | $unit .= '.scope'; | |
103 | my $properties = [ [PIDs => [dbus_uint32($$)]] ]; | |
104 | ||
105 | foreach my $key (keys %extra) { | |
106 | if ($key eq 'Slice' || $key eq 'KillMode') { | |
107 | push @{$properties}, [$key, $extra{$key}]; | |
108 | } elsif ($key eq 'CPUShares') { | |
109 | push @{$properties}, [$key, dbus_uint64($extra{$key})]; | |
110 | } elsif ($key eq 'CPUQuota') { | |
111 | push @{$properties}, ['CPUQuotaPerSecUSec', | |
b8d15bfd | 112 | dbus_uint64($extra{$key} * 10_000)]; |
f024a872 WB |
113 | } else { |
114 | die "Don't know how to encode $key for systemd scope\n"; | |
115 | } | |
116 | } | |
117 | ||
b8d15bfd WB |
118 | systemd_call(sub { |
119 | my ($if, $reactor, $finish_cb) = @_; | |
f024a872 | 120 | |
b8d15bfd | 121 | my $job; |
f024a872 | 122 | |
b8d15bfd WB |
123 | $if->connect_to_signal('JobRemoved', sub { |
124 | my ($id, $removed_job, $signaled_unit, $result) = @_; | |
125 | return if $signaled_unit ne $unit || $removed_job ne $job; | |
126 | if ($result ne 'done') { | |
127 | # I seem to remember $reactor->run() catching die() at some point? | |
128 | # so better call finish to be sure...: | |
129 | $finish_cb->(0); | |
130 | die "systemd job failed\n"; | |
131 | } else { | |
132 | $finish_cb->(1); | |
133 | } | |
134 | }); | |
f024a872 | 135 | |
b8d15bfd | 136 | $job = $if->StartTransientUnit($unit, 'fail', $properties, []); |
f024a872 | 137 | |
b8d15bfd WB |
138 | return undef; |
139 | }, $timeout); | |
140 | } | |
f024a872 | 141 | |
b8d15bfd WB |
142 | sub wait_for_unit_removed($;$) { |
143 | my ($unit, $timeout) = @_; | |
144 | ||
145 | systemd_call(sub { | |
146 | my ($if, $reactor, $finish_cb) = @_; | |
147 | ||
148 | my $unit_obj = eval { $if->GetUnit($unit) }; | |
149 | return 1 if !$unit_obj; | |
150 | ||
151 | $if->connect_to_signal('UnitRemoved', sub { | |
152 | my ($id, $removed_unit) = @_; | |
153 | $finish_cb->(1) if $removed_unit eq $unit_obj; | |
154 | }); | |
155 | ||
156 | # Deal with what we lost between GetUnit() and connecting to UnitRemoved: | |
157 | my $unit_obj_new = eval { $if->GetUnit($unit) }; | |
158 | if (!$unit_obj_new) { | |
159 | return 1; | |
160 | } | |
161 | ||
162 | return undef; | |
163 | }, $timeout); | |
f024a872 WB |
164 | } |
165 | ||
166 | 1; |