]> git.proxmox.com Git - pve-common.git/blame - src/PVE/Systemd.pm
tools: add fchownat syscall
[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
b8d15bfd
WB
10# $code should take the parameters ($interface, $reactor, $finish_callback).
11#
12# $finish_callback can be used by dbus-signal-handlers to stop the reactor.
13#
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.
f024a872 16# NOTE: This calls the dbus main loop and must not be used when another dbus
b8d15bfd
WB
17# main loop is being used as we need to wait signals.
18sub systemd_call($;$) {
19 my ($code, $timeout) = @_;
20
21 my $bus = Net::DBus->system();
22 my $reactor = Net::DBus::Reactor->main();
23
24 my $service = $bus->get_service('org.freedesktop.systemd1');
25 my $if = $service->get_object('/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager');
26
27 my ($finished, $current_result, $timer);
28 my $finish_callback = sub {
29 my ($result) = @_;
30
31 $current_result = $result;
32
33 $finished = 1;
34
35 if (defined($timer)) {
36 $reactor->remove_timeout($timer);
37 $timer = undef;
38 }
39
40 if (defined($reactor)) {
41 $reactor->shutdown();
42 $reactor = undef;
43 }
44 };
45
46 my $result = $code->($if, $reactor, $finish_callback);
47 # Are we done immediately?
48 return $result if defined $result;
49
50 # Alterantively $finish_callback may have been called already?
51 return $current_result if $finished;
52
53 # Otherwise wait:
54 my $on_timeout = sub {
55 $finish_callback->(undef);
56 die "timeout waiting on systemd\n";
57 };
58 $timer = $reactor->add_timeout($timeout * 1000, Net::DBus::Callback->new(method => $on_timeout))
59 if defined($timeout);
60
61 $reactor->run();
62 $reactor->shutdown() if defined($reactor); # $finish_callback clears it
63
64 return $current_result;
65}
66
f024a872
WB
67# Polling the job status instead doesn't work because this doesn't give us the
68# distinction between success and failure.
69#
70# Note that the description is mandatory for security reasons.
71sub enter_systemd_scope {
72 my ($unit, $description, %extra) = @_;
73 die "missing description\n" if !defined($description);
74
75 my $timeout = delete $extra{timeout};
76
77 $unit .= '.scope';
78 my $properties = [ [PIDs => [dbus_uint32($$)]] ];
79
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',
b8d15bfd 87 dbus_uint64($extra{$key} * 10_000)];
f024a872
WB
88 } else {
89 die "Don't know how to encode $key for systemd scope\n";
90 }
91 }
92
b8d15bfd
WB
93 systemd_call(sub {
94 my ($if, $reactor, $finish_cb) = @_;
f024a872 95
b8d15bfd 96 my $job;
f024a872 97
b8d15bfd
WB
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...:
104 $finish_cb->(0);
105 die "systemd job failed\n";
106 } else {
107 $finish_cb->(1);
108 }
109 });
f024a872 110
b8d15bfd 111 $job = $if->StartTransientUnit($unit, 'fail', $properties, []);
f024a872 112
b8d15bfd
WB
113 return undef;
114 }, $timeout);
115}
f024a872 116
b8d15bfd
WB
117sub wait_for_unit_removed($;$) {
118 my ($unit, $timeout) = @_;
119
120 systemd_call(sub {
121 my ($if, $reactor, $finish_cb) = @_;
122
123 my $unit_obj = eval { $if->GetUnit($unit) };
124 return 1 if !$unit_obj;
125
126 $if->connect_to_signal('UnitRemoved', sub {
127 my ($id, $removed_unit) = @_;
128 $finish_cb->(1) if $removed_unit eq $unit_obj;
129 });
130
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) {
134 return 1;
135 }
136
137 return undef;
138 }, $timeout);
f024a872
WB
139}
140
1411;