author | Yuya Nishihara <yuya@tcha.org> |
Sat, 29 Sep 2018 21:59:07 +0900 | |
changeset 39974 | ba447b83cd56 |
parent 39971 | a8be2cff613f |
child 39977 | 44840bcc411a |
permissions | -rw-r--r-- |
39971
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
1 |
// Copyright 2018 Yuya Nishihara <yuya@tcha.org> |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
2 |
// |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
3 |
// This software may be used and distributed according to the terms of the |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
4 |
// GNU General Public License version 2 or any later version. |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
5 |
|
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
6 |
//! Low-level utility for signal and process handling. |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
7 |
|
39974
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
8 |
use libc::{self, c_int, size_t, ssize_t}; |
39971
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
9 |
use std::io; |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
10 |
use std::os::unix::io::RawFd; |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
11 |
|
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
12 |
#[link(name = "procutil", kind = "static")] |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
13 |
extern "C" { |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
14 |
// sendfds.c |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
15 |
fn sendfds(sockfd: c_int, fds: *const c_int, fdlen: size_t) -> ssize_t; |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
16 |
} |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
17 |
|
39974
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
18 |
/// Changes the given fd to blocking mode. |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
19 |
pub fn set_blocking_fd(fd: RawFd) -> io::Result<()> { |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
20 |
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
21 |
if flags < 0 { |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
22 |
return Err(io::Error::last_os_error()); |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
23 |
} |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
24 |
let r = unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) }; |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
25 |
if r < 0 { |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
26 |
return Err(io::Error::last_os_error()) |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
27 |
} |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
28 |
Ok(()) |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
29 |
} |
ba447b83cd56
rust-chg: add low-level function to set pager fd blocking
Yuya Nishihara <yuya@tcha.org>
parents:
39971
diff
changeset
|
30 |
|
39971
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 |
/// Sends file descriptors via the given socket. |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
32 |
pub fn send_raw_fds(sock_fd: RawFd, fds: &[RawFd]) -> io::Result<()> { |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
33 |
let r = unsafe { sendfds(sock_fd, fds.as_ptr(), fds.len() as size_t) }; |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
34 |
if r < 0 { |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
35 |
return Err(io::Error::last_os_error()); |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
36 |
} |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
37 |
Ok(()) |
a8be2cff613f
rust-chg: add wrapper around C function
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
38 |
} |