]> git.proxmox.com Git - pve-common.git/commitdiff
add PVE::Systemd::is_unit_active
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 15 Feb 2024 12:30:18 +0000 (13:30 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 6 Mar 2024 10:57:22 +0000 (11:57 +0100)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/PVE/Systemd.pm

index 2517d313de09efdd690af1d9faae07f7fbc3a8db..07c912e35a4a60c6387f45ddacba73c88fc8791f 100644 (file)
@@ -167,6 +167,23 @@ sub wait_for_unit_removed($;$) {
     }, $timeout);
 }
 
+sub is_unit_active($;$) {
+    my ($unit) = @_;
+
+    my $bus = Net::DBus->system();
+    my $reactor = Net::DBus::Reactor->main();
+
+    my $service = $bus->get_service('org.freedesktop.systemd1');
+    my $if = $service->get_object('/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager');
+
+    my $unit_path = eval { $if->GetUnit($unit) }
+       or return 0;
+    $if = $service->get_object($unit_path, 'org.freedesktop.systemd1.Unit')
+       or return 0;
+    my $state = $if->ActiveState;
+    return defined($state) && $state eq 'active';
+}
+
 sub read_ini {
     my ($filename) = @_;