]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/Systemd.pm
6 use Net
::DBus
qw(dbus_uint32 dbus_uint64);
7 use Net
::DBus
::Callback
;
8 use Net
::DBus
::Reactor
;
10 # $code should take the parameters ($interface, $reactor, $finish_callback).
12 # $finish_callback can be used by dbus-signal-handlers to stop the reactor.
14 # In order to even start waiting on the reactor, $code needs to return undef, if it returns a
15 # defined value instead, it is assumed that this is the result already and we can stop.
16 # NOTE: This calls the dbus main loop and must not be used when another dbus
17 # main loop is being used as we need to wait signals.
18 sub systemd_call
($;$) {
19 my ($code, $timeout) = @_;
21 my $bus = Net
::DBus-
>system();
22 my $reactor = Net
::DBus
::Reactor-
>main();
24 my $service = $bus->get_service('org.freedesktop.systemd1');
25 my $if = $service->get_object('/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager');
27 my ($finished, $current_result, $timer);
28 my $finish_callback = sub {
31 $current_result = $result;
35 if (defined($timer)) {
36 $reactor->remove_timeout($timer);
40 if (defined($reactor)) {
46 my $result = $code->($if, $reactor, $finish_callback);
47 # Are we done immediately?
48 return $result if defined $result;
50 # Alterantively $finish_callback may have been called already?
51 return $current_result if $finished;
54 my $on_timeout = sub {
55 $finish_callback->(undef);
56 die "timeout waiting on systemd\n";
58 $timer = $reactor->add_timeout($timeout * 1000, Net
::DBus
::Callback-
>new(method => $on_timeout))
62 $reactor->shutdown() if defined($reactor); # $finish_callback clears it
64 return $current_result;
67 # Polling the job status instead doesn't work because this doesn't give us the
68 # distinction between success and failure.
70 # Note that the description is mandatory for security reasons.
71 sub enter_systemd_scope
{
72 my ($unit, $description, %extra) = @_;
73 die "missing description\n" if !defined($description);
75 my $timeout = delete $extra{timeout
};
78 my $properties = [ [PIDs
=> [dbus_uint32
($$)]] ];
80 foreach my $key (keys %extra) {
81 if ($key eq 'Slice' || $key eq 'KillMode') {
82 push @{$properties}, [$key, $extra{$key}];
83 } elsif ($key eq 'CPUShares') {
84 push @{$properties}, [$key, dbus_uint64
($extra{$key})];
85 } elsif ($key eq 'CPUQuota') {
86 push @{$properties}, ['CPUQuotaPerSecUSec',
87 dbus_uint64
($extra{$key} * 10_000)];
89 die "Don't know how to encode $key for systemd scope\n";
94 my ($if, $reactor, $finish_cb) = @_;
98 $if->connect_to_signal('JobRemoved', sub {
99 my ($id, $removed_job, $signaled_unit, $result) = @_;
100 return if $signaled_unit ne $unit || $removed_job ne $job;
101 if ($result ne 'done') {
102 # I seem to remember $reactor->run() catching die() at some point?
103 # so better call finish to be sure...:
105 die "systemd job failed\n";
111 $job = $if->StartTransientUnit($unit, 'fail', $properties, []);
117 sub wait_for_unit_removed
($;$) {
118 my ($unit, $timeout) = @_;
121 my ($if, $reactor, $finish_cb) = @_;
123 my $unit_obj = eval { $if->GetUnit($unit) };
124 return 1 if !$unit_obj;
126 $if->connect_to_signal('UnitRemoved', sub {
127 my ($id, $removed_unit) = @_;
128 $finish_cb->(1) if $removed_unit eq $unit_obj;
131 # Deal with what we lost between GetUnit() and connecting to UnitRemoved:
132 my $unit_obj_new = eval { $if->GetUnit($unit) };
133 if (!$unit_obj_new) {