Patchwork [04,of,13] rust-chg: add wrapper around C function

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 2, 2018, 2:25 p.m.
Message ID <9332bf9f7e3b6bfc0f46.1538490343@mimosa>
Download mbox | patch
Permalink /patch/35310/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 2, 2018, 2:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1537773723 -32400
#      Mon Sep 24 16:22:03 2018 +0900
# Node ID 9332bf9f7e3b6bfc0f46a006d4fa6f5b2c04b592
# Parent  6e54b2234a94cca1fd3d8adc050345ab94a1904c
rust-chg: add wrapper around C function

Patch

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 <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;
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 <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(())
+}