Debian Codegraph @

rust/chg/src/procutil.rs

raw
1 // Copyright 2018 Yuya Nishihara <[email protected]>
2 //
3 // This software may be used and distributed according to the terms of the
4 // GNU General Public License version 2 or any later version.
5
6 //! Low-level utility for signal and process handling.
7
8 use std::io;
9 use std::os::unix::io::RawFd;
10 use std::sync;
11
12 use libc::c_int;
13 use libc::pid_t;
14 use libc::size_t;
15 use libc::ssize_t;
16 use libc::{self};
17
18 #[link(name = "procutil", kind = "static")]
19 extern "C" {
20 // sendfds.c
21 fn sendfds(sockfd: c_int, fds: *const c_int, fdlen: size_t) -> ssize_t;
22
23 // sighandlers.c
24 fn setupsignalhandler(pid: pid_t, pgid: pid_t) -> c_int;
25 fn restoresignalhandler() -> c_int;
26 }
27
28 /// Returns the effective uid of the current process.
29 pub fn get_effective_uid() -> u32 {
30 unsafe { libc::geteuid() }
31 }
32
33 /// Returns the umask of the current process.
34 ///
35 /// # Safety
36 ///
37 /// This is unsafe because the umask value is temporarily changed, and
38 /// the change can be observed from the other threads. Don't call this in
39 /// multi-threaded context.
40 pub unsafe fn get_umask() -> u32 {
41 let mask = libc::umask(0);
42 libc::umask(mask);
43 mask
44 }
45
46 /// Changes the given fd to blocking mode.
47 pub fn set_blocking_fd(fd: RawFd) -> io::Result<()> {
48 let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
49 if flags < 0 {
50 return Err(io::Error::last_os_error());
51 }
52 let r =
53 unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) };
54 if r < 0 {
55 return Err(io::Error::last_os_error());
56 }
57 Ok(())
58 }
59
60 /// Sends file descriptors via the given socket.
61 pub fn send_raw_fds(sock_fd: RawFd, fds: &[RawFd]) -> io::Result<()> {
62 let r = unsafe { sendfds(sock_fd, fds.as_ptr(), fds.len() as size_t) };
63 if r < 0 {
64 return Err(io::Error::last_os_error());
65 }
66 Ok(())
67 }
68
69 static SETUP_SIGNAL_HANDLER: sync::Once = sync::Once::new();
70 static RESTORE_SIGNAL_HANDLER: sync::Once = sync::Once::new();
71
72 /// Installs signal handlers to forward signals to the server.
73 ///
74 /// # Safety
75 ///
76 /// This touches global states, and thus synchronized as a one-time
77 /// initialization function.
78 pub fn setup_signal_handler_once(
79 pid: u32,
80 pgid: Option<u32>,
81 ) -> io::Result<()> {
82 let pid_signed = pid as i32;
83 let pgid_signed = pgid.map(|n| n as i32).unwrap_or(0);
84 let mut r = 0;
85 SETUP_SIGNAL_HANDLER.call_once(|| {
86 r = unsafe { setupsignalhandler(pid_signed, pgid_signed) };
87 });
88 if r < 0 {
89 return Err(io::Error::last_os_error());
90 }
91 Ok(())
92 }
93
94 /// Restores the original signal handlers.
95 ///
96 /// # Safety
97 ///
98 /// This touches global states, and thus synchronized as a one-time
99 /// initialization function.
100 pub fn restore_signal_handler_once() -> io::Result<()> {
101 let mut r = 0;
102 RESTORE_SIGNAL_HANDLER.call_once(|| {
103 r = unsafe { restoresignalhandler() };
104 });
105 if r < 0 {
106 return Err(io::Error::last_os_error());
107 }
108 Ok(())
109 }