-
Notifications
You must be signed in to change notification settings - Fork 9
/
activation_socket.rs
59 lines (54 loc) · 1.73 KB
/
activation_socket.rs
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
#[cfg(target_os = "macos")]
use libc::size_t;
use std::io::Result as IoResult;
use std::net::UdpSocket;
#[cfg(target_os = "macos")]
use std::os::raw::{c_char, c_int, c_void};
#[cfg(target_os = "macos")]
extern "C" {
fn launch_activate_socket(name: *const c_char, fds: *mut *mut c_int, cnt: *mut size_t)
-> c_int;
}
#[cfg(target_os = "macos")]
pub(super) fn get_activation_socket() -> IoResult<UdpSocket> {
use libc::free;
use std::ffi::CString;
use std::io;
use std::io::ErrorKind::Other;
use std::os::unix::io::FromRawFd;
use std::ptr::null_mut;
unsafe {
let mut fds: *mut c_int = null_mut();
let mut cnt: size_t = 0;
let name = CString::new("Listeners").expect("CString::new failed");
if launch_activate_socket(name.as_ptr(), &mut fds, &mut cnt) == 0 {
if cnt == 1 {
let socket = UdpSocket::from_raw_fd(*fds.offset(0));
free(fds as *mut c_void);
Ok(socket)
} else {
Err(io::Error::new(Other, "Could not get fd: cnt != 1"))
}
} else {
Err(io::Error::new(
Other,
"Could not get fd: launch_activate_socket != 0",
))
}
}
}
#[allow(clippy::unnecessary_wraps)]
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
pub(super) fn get_activation_socket() -> IoResult<UdpSocket> {
use std::os::unix::io::FromRawFd;
unsafe { Ok(UdpSocket::from_raw_fd(3)) }
}
#[cfg(target_family = "windows")]
pub(super) fn get_activation_socket() -> IoResult<UdpSocket> {
use std::io;
use std::io::ErrorKind::Other;
Err(io::Error::new(
Other,
"This is not supported in windows platforms",
))
}