ceviche 0.7.0

Rust daemon/service wrapper
Documentation
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use std::{
    env,
    ffi::c_void,
    fmt,
    fs::{self, File},
    io::Write,
    path::{Path, PathBuf},
    process::Command,
    ptr,
    sync::{
        atomic::{AtomicBool, Ordering},
        mpsc, Arc, Mutex,
    },
    thread,
};

use chrono;
use ctrlc;
use log::info;
use timer;

use core_foundation::{
    array::{kCFTypeArrayCallBacks, CFArray, CFArrayCreate, CFArrayRef},
    base::{CFAllocatorRef, CFRelease, CFType, CFTypeRef, TCFType, ToVoid},
    runloop::{
        kCFRunLoopDefaultMode, CFRunLoopAddSource, CFRunLoopGetCurrent, CFRunLoopRef, CFRunLoopRun,
        CFRunLoopStop,
    },
    string::{CFString, CFStringRef},
};

use system_configuration_sys::{
    dynamic_store::{
        SCDynamicStoreContext, SCDynamicStoreCreate, SCDynamicStoreCreateRunLoopSource,
        SCDynamicStoreRef, SCDynamicStoreSetNotificationKeys,
    },
    dynamic_store_copy_specific::{uid_t, SCDynamicStoreCopyConsoleUser},
};

use crate::controller::{ControllerInterface, ServiceMainFn};
use crate::session;
use crate::Error;
use crate::ServiceEvent;

type MacosServiceMainWrapperFn = extern "system" fn(args: Vec<String>);
pub type Session = session::Session_<u32>;

pub enum LaunchAgentTargetSesssion {
    GUI,
    NonGUI,
    PerUser,
    PreLogin,
}

impl fmt::Display for LaunchAgentTargetSesssion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            LaunchAgentTargetSesssion::GUI => write!(f, "Aqua"),
            LaunchAgentTargetSesssion::NonGUI => write!(f, "StandardIO"),
            LaunchAgentTargetSesssion::PerUser => write!(f, "Background"),
            LaunchAgentTargetSesssion::PreLogin => write!(f, "LoginWindow"),
        }
    }
}

fn launchctl_load_daemon(plist_path: &Path) -> Result<(), Error> {
    let output = Command::new("launchctl")
        .arg("load")
        .arg(&plist_path.to_str().unwrap())
        .output()
        .map_err(|e| {
            Error::new(&format!(
                "Failed to load plist {}: {}",
                plist_path.display(),
                e
            ))
        })?;
    if output.stdout.len() > 0 {
        info!("{}", String::from_utf8_lossy(&output.stdout));
    }
    Ok(())
}

fn launchctl_unload_daemon(plist_path: &Path) -> Result<(), Error> {
    let output = Command::new("launchctl")
        .arg("unload")
        .arg(&plist_path.to_str().unwrap())
        .output()
        .map_err(|e| {
            Error::new(&format!(
                "Failed to unload plist {}: {}",
                plist_path.display(),
                e
            ))
        })?;
    if output.stdout.len() > 0 {
        info!("{}", String::from_utf8_lossy(&output.stdout));
    }
    Ok(())
}

fn launchctl_start_daemon(name: &str) -> Result<(), Error> {
    let output = Command::new("launchctl")
        .arg("start")
        .arg(name)
        .output()
        .map_err(|e| Error::new(&format!("Failed to start {}: {}", name, e)))?;
    if output.stdout.len() > 0 {
        info!("{}", String::from_utf8_lossy(&output.stdout));
    }
    Ok(())
}

fn launchctl_stop_daemon(name: &str) -> Result<(), Error> {
    let output = Command::new("launchctl")
        .arg("stop")
        .arg(name)
        .output()
        .map_err(|e| Error::new(&format!("Failed to stop {}: {}", name, e)))?;
    if output.stdout.len() > 0 {
        info!("{}", String::from_utf8_lossy(&output.stdout));
    }
    Ok(())
}

pub struct MacosController {
    /// Manages the service on the system.
    pub service_name: String,
    pub display_name: String,
    pub description: String,
    pub is_agent: bool,
    pub session_types: Option<Vec<LaunchAgentTargetSesssion>>,
    pub keep_alive: bool,
}

impl MacosController {
    pub fn new(service_name: &str, display_name: &str, description: &str) -> MacosController {
        MacosController {
            service_name: service_name.to_string(),
            display_name: display_name.to_string(),
            description: description.to_string(),
            is_agent: false,
            session_types: None,
            keep_alive: true,
        }
    }

    /// Register the `service_main_wrapper` function, this function is generated by the `Service!` macro.
    pub fn register(
        &mut self,
        service_main_wrapper: MacosServiceMainWrapperFn,
    ) -> Result<(), Error> {
        service_main_wrapper(env::args().collect());
        Ok(())
    }

    fn get_plist_content(&self) -> Result<String, Error> {
        let mut current_exe = env::current_exe()
            .map_err(|e| Error::new(&format!("env::current_exe() failed: {}", e)))?;
        let current_exe_str = current_exe
            .to_str()
            .expect("current_exe path to be unicode")
            .to_string();

        current_exe.pop();
        let working_dir_str = current_exe
            .to_str()
            .expect("working_dir path to be unicode");

        let mut plist = String::new();
        plist.push_str(r#"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>"#);

        plist.push_str(&format!(
            r#"
<key>Disabled</key>
<false/>
<key>Label</key>
<string>{}</string>
<key>ProgramArguments</key>
<array>
<string>{}</string>
</array>
<key>WorkingDirectory</key>
<string>{}</string>
<key>RunAtLoad</key>
<true/>"#,
            self.service_name, current_exe_str, working_dir_str,
        ));

        if self.is_agent {
            if let Some(session_types) = self.session_types.as_ref() {
                plist.push_str(
                    r#"
<key>LimitLoadToSessionType</key>
<array>"#,
                );

                for session_type in session_types {
                    plist.push_str(&format!(
                        r#"
<string>{}</string>"#,
                        session_type
                    ));
                }

                plist.push_str(
                    r#"
</array>"#,
                );
            }
        }

        if self.keep_alive {
            plist.push_str(
                r#"
<key>KeepAlive</key>
<true/>"#,
            );
        }

        plist.push_str(
            r#"
</dict>
</plist>"#,
        );

        Ok(plist)
    }

    fn write_plist(&self, path: &Path) -> Result<(), Error> {
        info!("Writing plist file {}", path.display());
        let content = self.get_plist_content()?;
        File::create(path)
            .and_then(|mut file| file.write_all(content.as_bytes()))
            .map_err(|e| Error::new(&format!("Failed to write {}: {}", path.display(), e)))
    }

    fn plist_path(&mut self) -> PathBuf {
        Path::new("/Library/")
            .join(if self.is_agent {
                "LaunchAgents/"
            } else {
                "LaunchDaemons/"
            })
            .join(format!("{}.plist", &self.service_name))
    }
}

impl ControllerInterface for MacosController {
    /// Creates the service on the system.
    fn create(&mut self) -> Result<(), Error> {
        let plist_path = self.plist_path();

        self.write_plist(&plist_path)?;
        if !self.is_agent {
            return launchctl_load_daemon(&plist_path);
        }
        Ok(())
    }
    /// Deletes the service.
    fn delete(&mut self) -> Result<(), Error> {
        let plist_path = self.plist_path();
        if !self.is_agent {
            launchctl_unload_daemon(&plist_path)?;
        }
        fs::remove_file(&plist_path)
            .map_err(|e| Error::new(&format!("Failed to delete {}: {}", plist_path.display(), e)))
    }
    /// Starts the service.
    fn start(&mut self) -> Result<(), Error> {
        launchctl_start_daemon(&self.service_name)
    }
    /// Stops the service.
    fn stop(&mut self) -> Result<(), Error> {
        launchctl_stop_daemon(&self.service_name)
    }
    // Loads the agent service.
    fn load(&mut self) -> Result<(), Error> {
        launchctl_load_daemon(&self.plist_path())
    }
    // Loads the agent service.
    fn unload(&mut self) -> Result<(), Error> {
        launchctl_unload_daemon(&self.plist_path())
    }
}

/// Generates a `service_main_wrapper` that wraps the provided service main function.
#[macro_export]
macro_rules! Service {
    ($name:expr, $function:ident) => {
        extern "system" fn service_main_wrapper(args: Vec<String>) {
            dispatch($function, args);
        }
    };
}

fn active_session_uid(store_ref: Option<SCDynamicStoreRef>) -> u32 {
    let mut uid: uid_t = 0;
    let store = store_ref.unwrap_or(ptr::null());

    let user = unsafe { SCDynamicStoreCopyConsoleUser(store, &mut uid, ptr::null_mut()) };
    if user.is_null() {
        return 0;
    }
    let _cf_user: CFString = unsafe { TCFType::wrap_under_create_rule(user) };
    return uid;
}

unsafe extern "C" fn on_sc_console_user_change<F>(
    store: SCDynamicStoreRef,
    _keys: CFArrayRef,
    info: *mut c_void,
) where
    F: FnMut(u32, EventType) + Send + 'static,
{
    let uid = active_session_uid(Some(store));
    let ctx_box = Box::from_raw(info as *mut Arc<SyncSessionContext<F>>);
    let ctx_ptr = Box::leak(ctx_box);
    let mut ctx = ctx_ptr.lock().unwrap();

    let old_uid = ctx.uid;
    if uid != ctx.uid {
        ctx.uid = uid;
        if old_uid != 0 || ctx.last_was_logout.load(Ordering::SeqCst) {
            ctx.last_was_logout.store(false, Ordering::SeqCst);
            (ctx.callback)(old_uid, EventType::Disconnect);
        }

        if uid != 0 {
            ctx.pending_connect.store(false, Ordering::SeqCst);
            (ctx.callback)(uid, EventType::Connect);
        } else {
            ctx.pending_connect.store(true, Ordering::SeqCst);
            let ctx_weak = Arc::downgrade(ctx_ptr);
            thread::spawn(move || {
                let timer = timer::Timer::new();
                let (tx, rx) = mpsc::channel();

                let _guard = timer.schedule_with_delay(chrono::Duration::seconds(3), move || {
                    let _ignored = tx.send(());
                });
                rx.recv().unwrap();
                match ctx_weak.upgrade() {
                    Some(ctx_ptr) => {
                        let mut ctx = ctx_ptr.lock().unwrap();
                        if ctx.pending_connect.load(Ordering::SeqCst) {
                            ctx.last_was_logout.store(true, Ordering::SeqCst);
                            (ctx.callback)(uid, EventType::Connect);
                        }
                    }
                    None => return,
                };
            });
        }
    }
}

#[link(name = "SystemConfiguration", kind = "framework")]
extern "C" {
    pub fn SCDynamicStoreKeyCreateConsoleUser(allocator: CFAllocatorRef) -> CFStringRef;
}

pub enum EventType {
    Connect,
    Disconnect,
}

pub struct SessionContext<F: FnMut(u32, EventType)> {
    uid: u32,
    callback: F,
    pending_connect: AtomicBool,
    last_was_logout: AtomicBool,
}

impl<F: FnMut(u32, EventType)> SessionContext<F> {
    pub fn new(cb: F) -> Self
    where
        F: FnMut(u32, EventType) + Send + 'static,
    {
        Self {
            uid: active_session_uid(None),
            callback: cb,
            pending_connect: AtomicBool::new(false),
            last_was_logout: AtomicBool::new(true),
        }
    }
}

pub type SyncSessionContext<T> = Mutex<SessionContext<T>>;

pub struct Monitor<F: FnMut(u32, EventType)> {
    context: *mut Arc<SyncSessionContext<F>>,
}

impl<F: FnMut(u32, EventType)> Drop for Monitor<F> {
    fn drop(&mut self) {
        let _ = unsafe { Box::from_raw(self.context as *mut Arc<SyncSessionContext<F>>) };
    }
}

impl<F: FnMut(u32, EventType)> Monitor<F> {
    pub fn new(cb: F) -> Result<Self, std::io::Error>
    where
        F: FnMut(u32, EventType) + Send + 'static,
    {
        let session_ctx = Box::new(Arc::new(Mutex::new(SessionContext::new(cb))));
        let session_ctx_ptr = Box::into_raw(session_ctx);

        let mut ctx = SCDynamicStoreContext {
            version: 0,
            info: session_ctx_ptr as *mut c_void,
            retain: None,
            release: None,
            copyDescription: None,
        };

        unsafe {
            let name = CFString::from_static_string("kCGSSessionUserNameKey");
            let store_ref = SCDynamicStoreCreate(
                ptr::null_mut(),
                name.to_void() as CFStringRef,
                Some(on_sc_console_user_change::<F>),
                &mut ctx,
            );
            let key = SCDynamicStoreKeyCreateConsoleUser(ptr::null());
            let keys = CFArrayCreate(ptr::null(), &key.to_void(), 1, &kCFTypeArrayCallBacks);
            let _ = SCDynamicStoreSetNotificationKeys(store_ref, keys, ptr::null_mut());
            // releases array
            let _: CFArray<CFType> = TCFType::wrap_under_create_rule(keys);

            let rls = SCDynamicStoreCreateRunLoopSource(ptr::null_mut(), store_ref, 0);
            CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);

            CFRelease(rls as CFTypeRef);
            CFRelease(store_ref as CFTypeRef);

            Ok(Monitor {
                context: session_ctx_ptr,
            })
        }
    }
}

pub struct MonitorLoopRef {
    loop_ref: CFRunLoopRef,
}
unsafe impl Send for MonitorLoopRef {}

impl MonitorLoopRef {
    pub fn stop(&mut self) {
        unsafe { CFRunLoopStop(self.loop_ref) };
    }
}

pub fn run_monitor<T: Send + 'static>(
    tx: mpsc::Sender<ServiceEvent<T>>,
) -> Result<MonitorLoopRef, std::io::Error> {
    let (_tx, rx) = mpsc::channel();
    thread::spawn(move || {
        let mon = Monitor::new(move |uid: u32, event: EventType| {
            match event {
                EventType::Connect => {
                    let _ = tx.send(ServiceEvent::SessionConnect(Session::new(uid)));
                }
                EventType::Disconnect => {
                    let _ = tx.send(ServiceEvent::SessionDisconnect(Session::new(uid)));
                }
            };
        });

        let loop_ref = unsafe { CFRunLoopGetCurrent() };
        let mon_loop_ref = MonitorLoopRef { loop_ref: loop_ref };
        _tx.send(mon_loop_ref).unwrap();
        unsafe { CFRunLoopRun() };
        drop(mon);
    });

    let received = rx.recv().unwrap();

    return Ok(received);
}

#[doc(hidden)]
pub fn dispatch<T: Send + 'static>(service_main: ServiceMainFn<T>, args: Vec<String>) {
    let (tx, rx) = mpsc::channel();

    let mut session_monitor = run_monitor(tx.clone()).expect("Failed to run session monitor");
    let _tx = tx.clone();

    ctrlc::set_handler(move || {
        let _ = tx.send(ServiceEvent::Stop);
    })
    .expect("Failed to register Ctrl-C handler");
    service_main(rx, _tx, args, false);

    session_monitor.stop();
}