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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
extern crate dbus;
extern crate futures;
extern crate futures_cpupool;
extern crate tokio_timer;

use std::str::FromStr;
use std::time::Duration;
use self::dbus::{BusType, Connection, ConnectionItem, Interface, Member, Message, Path, Props};
use self::dbus::arg::{Dict, Iter, Variant};
use self::futures::Future;
use self::futures_cpupool::CpuPool;
use self::tokio_timer::Timer;

pub const SD_SERVICE_MANAGER: &str = "org.freedesktop.systemd1";
pub const SD_SERVICE_PATH: &str = "/org/freedesktop/systemd1";
pub const SD_MANAGER_INTERFACE: &str = "org.freedesktop.systemd1.Manager";
pub const SD_UNIT_INTERFACE: &str = "org.freedesktop.systemd1.Unit";

pub fn start_service(timeout: u64) -> Result<ServiceState, Error> {
    let state = get_service_state()?;
    match state {
        ServiceState::Active => Ok(state),
        ServiceState::Activating => handler(timeout, ServiceState::Active),
        ServiceState::Failed => Err(Error::Failed),
        _ => {
            let message = Message::new_method_call(
                SD_SERVICE_MANAGER,
                SD_SERVICE_PATH,
                SD_MANAGER_INTERFACE,
                "StartUnit",
            ).map_err(Error::Message)?
                .append2("NetworkManager.service", "fail");

            let connection = Connection::get_private(BusType::System).map_err(Error::Connection)?;

            connection
                .send_with_reply_and_block(message, 2000)
                .map_err(Error::Connection)?;

            handler(timeout, ServiceState::Active)
        },
    }
}

pub fn stop_service(timeout: u64) -> Result<ServiceState, Error> {
    let state = get_service_state()?;
    match state {
        ServiceState::Inactive => Ok(state),
        ServiceState::Deactivating => handler(timeout, ServiceState::Inactive),
        ServiceState::Failed => Err(Error::Failed),
        _ => {
            let message = Message::new_method_call(
                SD_SERVICE_MANAGER,
                SD_SERVICE_PATH,
                SD_MANAGER_INTERFACE,
                "StopUnit",
            ).map_err(Error::Message)?
                .append2("NetworkManager.service", "fail");

            let connection = Connection::get_private(BusType::System).map_err(Error::Connection)?;

            connection
                .send_with_reply_and_block(message, 2000)
                .map_err(Error::Connection)?;

            handler(timeout, ServiceState::Inactive)
        },
    }
}

pub fn get_service_state() -> Result<ServiceState, Error> {
    let message = Message::new_method_call(
        SD_SERVICE_MANAGER,
        SD_SERVICE_PATH,
        SD_MANAGER_INTERFACE,
        "GetUnit",
    ).map_err(Error::Message)?
        .append1("NetworkManager.service");

    let connection = Connection::get_private(BusType::System).map_err(Error::Connection)?;

    let response = connection
        .send_with_reply_and_block(message, 2000)
        .map_err(Error::Connection)?;

    let path = response.get1::<Path>().ok_or(Error::NotFound)?;

    let response = Props::new(
        &connection,
        SD_SERVICE_MANAGER,
        path,
        SD_UNIT_INTERFACE,
        2000,
    ).get("ActiveState")
        .map_err(Error::Props)?;

    response
        .inner::<&str>()
        .ok()
        .ok_or(Error::NotFound)?
        .parse()
}

fn handler(timeout: u64, target_state: ServiceState) -> Result<ServiceState, Error> {
    if timeout == 0 {
        return get_service_state();
    }

    let timer = Timer::default()
        .sleep(Duration::from_secs(timeout))
        .then(|_| Err(Error::TimedOut));

    let process = CpuPool::new_num_cpus().spawn_fn(|| {
        let connection = Connection::get_private(BusType::System).map_err(Error::Connection)?;
        connection
            .add_match(
                "type='signal', sender='org.freedesktop.systemd1', \
                 interface='org.freedesktop.DBus.Properties', \
                 member='PropertiesChanged', \
                 path='/org/freedesktop/systemd1/unit/NetworkManager_2eservice'",
            )
            .map_err(Error::Connection)?;

        if get_service_state()? == target_state {
            return Ok(target_state);
        }

        for item in connection.iter(0) {
            let response = if let ConnectionItem::Signal(ref signal) = item {
                signal
            } else {
                continue;
            };

            if response.interface().ok_or(Error::NotFound)?
                != Interface::from("org.freedesktop.DBus.Properties")
                || response.member().ok_or(Error::NotFound)? != Member::from("PropertiesChanged")
                || response.path().ok_or(Error::NotFound)?
                    != Path::from("/org/freedesktop/systemd1/unit/NetworkManager_2eservice")
            {
                continue;
            }

            let (interface, dictionary) = response.get2::<&str, Dict<&str, Variant<Iter>, _>>();

            if interface.ok_or(Error::NotFound)? != "org.freedesktop.systemd1.Unit" {
                continue;
            }

            for (k, mut v) in dictionary.ok_or(Error::NotFound)? {
                if k == "ActiveState" {
                    let response = v.0.get::<&str>().ok_or(Error::NotFound)?;
                    let state: ServiceState = response.parse()?;
                    if state == target_state {
                        return Ok(target_state);
                    }
                }
            }
        }
        Err(Error::NotFound)
    });

    match timer.select(process).map(|(result, _)| result).wait() {
        Ok(val) => Ok(val),
        Err(val) => Err(val.0),
    }
}

#[derive(Debug, Eq, PartialEq)]
pub enum ServiceState {
    Active,
    Reloading,
    Inactive,
    Failed,
    Activating,
    Deactivating,
}

#[derive(Debug)]
pub enum Error {
    Message(String),
    Connection(dbus::Error),
    Props(dbus::Error),
    TimedOut,
    Failed,
    NotFound,
}

impl FromStr for ServiceState {
    type Err = Error;
    fn from_str(s: &str) -> Result<ServiceState, Error> {
        match s {
            "active" => Ok(ServiceState::Active),
            "reloading" => Ok(ServiceState::Reloading),
            "inactive" => Ok(ServiceState::Inactive),
            "failed" => Ok(ServiceState::Failed),
            "activating" => Ok(ServiceState::Activating),
            "deactivating" => Ok(ServiceState::Deactivating),
            _ => Err(Error::NotFound),
        }
    }
}