1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::cell::RefCell;
use std::rc::Rc;

use super::*;
use super::systemd_dbus::*;

type ListUnitTuple <'a> = (
	& 'a str,
	& 'a str,
	& 'a str,
	& 'a str,
	& 'a str,
	& 'a str,
	DbusPath <'a>,
	u32,
	& 'a str,
	DbusPath <'a>,
);

pub struct SystemdManager {
	connection: Rc <RefCell <SystemdConnection>>,
}

impl SystemdManager {

	pub fn new (
		connection: Rc <RefCell <SystemdConnection>>,
	) -> SystemdManager {

		SystemdManager {
			connection: connection,
		}

	}

	pub fn list_units (
		& self,
	) -> Result <Vec <SystemdUnit>, String> {

		let connection =
			self.connection.borrow ();

		let dbus_request =
			DbusMessage::new_method_call (
				"org.freedesktop.systemd1",
				"/org/freedesktop/systemd1",
				"org.freedesktop.systemd1.Manager",
				"ListUnits",
			).unwrap ();

		let dbus_response =
			connection.dbus_connection ().send_with_reply_and_block (
				dbus_request,
				2000,
			).unwrap ();

		let systemd_units: Vec <ListUnitTuple> =
			dbus_response.get1 ().unwrap ();

		Ok (systemd_units.into_iter ().map (|systemd_unit| {

			let (
				unit_name,
				unit_description,
				unit_load_state,
				unit_active_state,
				unit_sub_state,
				unit_following_unit,
				unit_object_path,
				unit_job_id,
				unit_job_type,
				unit_job_object_path,
			) = systemd_unit;

			SystemdUnit::new (
				self.connection.clone (),
				unit_name.to_string (),
				unit_description.to_string (),
				SystemdLoadState::from (unit_load_state),
				SystemdActiveState::from (unit_active_state),
				SystemdSubState::from (unit_sub_state),
				if unit_following_unit != "" {
					Some (unit_following_unit.to_owned ())
				} else { None },
				unit_object_path.to_string (),
				unit_job_id,
				SystemdJobType::from (unit_job_type),
				unit_job_object_path.to_string (),
			)

		}).collect ())

	}

}

// ex: noet ts=4 filetype=rust