Debian Codegraph @

src/unix.rs

raw symbols SCIP
1 use std::env;
2 use std::io;
3 use std::mem;
4 use std::net::{TcpListener, UdpSocket};
5 use std::os::unix::io::{FromRawFd, RawFd};
6 use std::os::unix::net::{UnixDatagram, UnixListener};
7
8 pub type FdType = RawFd;
9
10 fn is_sock(fd: FdType) -> bool {
11 unsafe {
12 let mut stat: libc::stat = mem::zeroed();
13 libc::fstat(fd as libc::c_int, &mut stat);
14 (stat.st_mode & libc::S_IFMT) == libc::S_IFSOCK
15 }
16 }
17
18 fn validate_socket(
19 fd: FdType,
20 sock_fam: libc::c_int,
21 sock_type: libc::c_int,
22 hint: &str,
23 ) -> io::Result<FdType> {
24 if !is_sock(fd) {
25 return Err(io::Error::new(
26 io::ErrorKind::InvalidInput,
27 format!("fd {} is not a socket", fd),
28 ));
29 }
30
31 let is_valid = unsafe {
32 let mut ty: libc::c_int = mem::zeroed();
33 let mut ty_len = mem::size_of_val(&ty) as libc::c_uint;
34 let mut sockaddr: libc::sockaddr = mem::zeroed();
35 let mut sockaddr_len = mem::size_of_val(&sockaddr) as libc::c_uint;
36 libc::getsockname(fd, &mut sockaddr, &mut sockaddr_len) == 0
37 && libc::getsockopt(
38 fd,
39 libc::SOL_SOCKET,
40 libc::SO_TYPE,
41 &mut ty as *mut i32 as *mut libc::c_void,
42 &mut ty_len,
43 ) == 0
44 && ty == sock_type
45 && (sockaddr.sa_family as libc::c_int == sock_fam
46 || (sockaddr.sa_family as libc::c_int == libc::AF_INET6
47 && sock_fam == libc::AF_INET))
48 };
49
50 if !is_valid {
51 return Err(io::Error::new(
52 io::ErrorKind::InvalidInput,
53 format!("fd {} is not a valid {}", fd, hint),
54 ));
55 }
56
57 Ok(fd)
58 }
59
60 fn mark_cloexec(fd: i32) -> io::Result<i32> {
61 let rv = unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) };
62 if rv < 0 {
63 return Err(io::Error::new(
64 io::ErrorKind::Other,
65 format!(
66 "fd {} cannot be marked as FD_CLOEXEC ({})",
67 fd,
68 io::Error::last_os_error()
69 ),
70 ));
71 }
72 Ok(fd)
73 }
74
75 pub fn make_tcp_listener(fd: FdType) -> io::Result<TcpListener> {
76 validate_socket(fd, libc::AF_INET, libc::SOCK_STREAM, "tcp socket")
77 .and_then(mark_cloexec)
78 .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) })
79 }
80
81 pub fn make_unix_listener(fd: FdType) -> io::Result<UnixListener> {
82 validate_socket(fd, libc::AF_UNIX, libc::SOCK_STREAM, "unix stream socket")
83 .and_then(mark_cloexec)
84 .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) })
85 }
86
87 pub fn make_udp_socket(fd: FdType) -> io::Result<UdpSocket> {
88 validate_socket(fd, libc::AF_INET, libc::SOCK_DGRAM, "udp socket")
89 .and_then(mark_cloexec)
90 .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) })
91 }
92
93 pub fn make_unix_datagram(fd: FdType) -> io::Result<UnixDatagram> {
94 validate_socket(fd, libc::AF_UNIX, libc::SOCK_DGRAM, "unix datagram socket")
95 .and_then(mark_cloexec)
96 .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) })
97 }
98
99 pub fn make_custom<T: FromRawFd>(
100 fd: FdType,
101 sock_fam: libc::c_int,
102 sock_type: libc::c_int,
103 hint: &str,
104 ) -> io::Result<T> {
105 validate_socket(fd, sock_fam, sock_type, hint)
106 .and_then(mark_cloexec)
107 .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) })
108 }
109
110 pub fn get_fds() -> Option<Vec<FdType>> {
111 // modified systemd protocol
112 if let Some(count) = env::var("LISTEN_FDS").ok().and_then(|x| x.parse().ok()) {
113 let ok = match env::var("LISTEN_PID").as_ref().map(|x| x.as_str()) {
114 Err(env::VarError::NotPresent) | Ok("") => true,
115 Ok(val) if val.parse().ok() == Some(unsafe { libc::getpid() }) => true,
116 _ => false,
117 };
118
119 let first_fd = env::var("LISTEN_FDS_FIRST_FD")
120 .ok()
121 .and_then(|x| x.parse().ok())
122 .unwrap_or(3);
123
124 env::remove_var("LISTEN_PID");
125 env::remove_var("LISTEN_FDS");
126 if ok {
127 return Some(
128 (0..count)
129 .map(|offset| first_fd + offset as FdType)
130 .collect(),
131 );
132 }
133 }
134
135 None
136 }