# HG changeset patch # User Yuya Nishihara # Date 1537773723 -32400 # Node ID a8be2cff613f680b6b7faa7cc17991cbfebe400d # Parent 208cb7a9d0facd4a45f8fdb4b72ea2de205a044f rust-chg: add wrapper around C function diff --git a/rust/chg/src/lib.rs b/rust/chg/src/lib.rs --- a/rust/chg/src/lib.rs +++ b/rust/chg/src/lib.rs @@ -0,0 +1,8 @@ +// Copyright 2018 Yuya Nishihara +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. + +extern crate libc; + +pub mod procutil; diff --git a/rust/chg/src/procutil.rs b/rust/chg/src/procutil.rs new file mode 100644 --- /dev/null +++ b/rust/chg/src/procutil.rs @@ -0,0 +1,25 @@ +// Copyright 2018 Yuya Nishihara +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. + +//! Low-level utility for signal and process handling. + +use libc::{c_int, size_t, ssize_t}; +use std::io; +use std::os::unix::io::RawFd; + +#[link(name = "procutil", kind = "static")] +extern "C" { + // sendfds.c + fn sendfds(sockfd: c_int, fds: *const c_int, fdlen: size_t) -> ssize_t; +} + +/// Sends file descriptors via the given socket. +pub fn send_raw_fds(sock_fd: RawFd, fds: &[RawFd]) -> io::Result<()> { + let r = unsafe { sendfds(sock_fd, fds.as_ptr(), fds.len() as size_t) }; + if r < 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) +}