rust-chg: add wrapper around C function
authorYuya Nishihara <yuya@tcha.org>
Mon, 24 Sep 2018 16:22:03 +0900
changeset 39970 a8be2cff613f
parent 39969 208cb7a9d0fa
child 39971 b1d8acd82d60
rust-chg: add wrapper around C function
rust/chg/src/lib.rs
rust/chg/src/procutil.rs
--- a/rust/chg/src/lib.rs	Mon Sep 24 16:14:35 2018 +0900
+++ b/rust/chg/src/lib.rs	Mon Sep 24 16:22:03 2018 +0900
@@ -0,0 +1,8 @@
+// Copyright 2018 Yuya Nishihara <yuya@tcha.org>
+//
+// 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;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/chg/src/procutil.rs	Mon Sep 24 16:22:03 2018 +0900
@@ -0,0 +1,25 @@
+// Copyright 2018 Yuya Nishihara <yuya@tcha.org>
+//
+// 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(())
+}