3 // This software may be used and distributed according to the terms of the |
3 // This software may be used and distributed according to the terms of the |
4 // GNU General Public License version 2 or any later version. |
4 // GNU General Public License version 2 or any later version. |
5 |
5 |
6 //! Low-level utility for signal and process handling. |
6 //! Low-level utility for signal and process handling. |
7 |
7 |
8 use libc::{c_int, size_t, ssize_t}; |
8 use libc::{self, c_int, size_t, ssize_t}; |
9 use std::io; |
9 use std::io; |
10 use std::os::unix::io::RawFd; |
10 use std::os::unix::io::RawFd; |
11 |
11 |
12 #[link(name = "procutil", kind = "static")] |
12 #[link(name = "procutil", kind = "static")] |
13 extern "C" { |
13 extern "C" { |
14 // sendfds.c |
14 // sendfds.c |
15 fn sendfds(sockfd: c_int, fds: *const c_int, fdlen: size_t) -> ssize_t; |
15 fn sendfds(sockfd: c_int, fds: *const c_int, fdlen: size_t) -> ssize_t; |
|
16 } |
|
17 |
|
18 /// Changes the given fd to blocking mode. |
|
19 pub fn set_blocking_fd(fd: RawFd) -> io::Result<()> { |
|
20 let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; |
|
21 if flags < 0 { |
|
22 return Err(io::Error::last_os_error()); |
|
23 } |
|
24 let r = unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) }; |
|
25 if r < 0 { |
|
26 return Err(io::Error::last_os_error()) |
|
27 } |
|
28 Ok(()) |
16 } |
29 } |
17 |
30 |
18 /// Sends file descriptors via the given socket. |
31 /// Sends file descriptors via the given socket. |
19 pub fn send_raw_fds(sock_fd: RawFd, fds: &[RawFd]) -> io::Result<()> { |
32 pub fn send_raw_fds(sock_fd: RawFd, fds: &[RawFd]) -> io::Result<()> { |
20 let r = unsafe { sendfds(sock_fd, fds.as_ptr(), fds.len() as size_t) }; |
33 let r = unsafe { sendfds(sock_fd, fds.as_ptr(), fds.len() as size_t) }; |