changeset 39970:a8be2cff613f

rust-chg: add wrapper around C function
author Yuya Nishihara <yuya@tcha.org>
date Mon, 24 Sep 2018 16:22:03 +0900
parents 208cb7a9d0fa
children b1d8acd82d60
files rust/chg/src/lib.rs rust/chg/src/procutil.rs
diffstat 2 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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(())
+}