gio/
unix_fd_message.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(unix)]
4use std::os::unix::io::{AsRawFd, RawFd};
5use std::{mem, ptr};
6
7use glib::{prelude::*, translate::*};
8#[cfg(all(not(unix), docsrs))]
9use socket::{AsRawFd, RawFd};
10
11use crate::{ffi, UnixFDMessage};
12
13pub trait UnixFDMessageExtManual: IsA<UnixFDMessage> + Sized {
14    /// Adds a file descriptor to @self.
15    ///
16    /// The file descriptor is duplicated using dup(). You keep your copy
17    /// of the descriptor and the copy contained in @self will be closed
18    /// when @self is finalized.
19    ///
20    /// A possible cause of failure is exceeding the per-process or
21    /// system-wide file descriptor limit.
22    /// ## `fd`
23    /// a valid open file descriptor
24    ///
25    /// # Returns
26    ///
27    /// [`true`] in case of success, else [`false`] (and @error is set)
28    #[doc(alias = "g_unix_fd_message_append_fd")]
29    fn append_fd<T: AsRawFd>(&self, fd: T) -> Result<(), glib::Error> {
30        unsafe {
31            let mut error = ptr::null_mut();
32            ffi::g_unix_fd_message_append_fd(
33                self.as_ref().to_glib_none().0,
34                fd.as_raw_fd(),
35                &mut error,
36            );
37            if error.is_null() {
38                Ok(())
39            } else {
40                Err(from_glib_full(error))
41            }
42        }
43    }
44    /// Returns the array of file descriptors that is contained in this
45    /// object.
46    ///
47    /// After this call, the descriptors are no longer contained in
48    /// @self. Further calls will return an empty list (unless more
49    /// descriptors have been added).
50    ///
51    /// The return result of this function must be freed with g_free().
52    /// The caller is also responsible for closing all of the file
53    /// descriptors.
54    ///
55    /// If @length is non-[`None`] then it is set to the number of file
56    /// descriptors in the returned array. The returned array is also
57    /// terminated with -1.
58    ///
59    /// This function never returns [`None`]. In case there are no file
60    /// descriptors contained in @self, an empty array is returned.
61    ///
62    /// # Returns
63    ///
64    /// an array of file
65    ///     descriptors
66    #[doc(alias = "g_unix_fd_message_steal_fds")]
67    fn steal_fds(&self) -> Vec<RawFd> {
68        unsafe {
69            let mut length = mem::MaybeUninit::uninit();
70            let ret = FromGlibContainer::from_glib_full_num(
71                ffi::g_unix_fd_message_steal_fds(
72                    self.as_ref().to_glib_none().0,
73                    length.as_mut_ptr(),
74                ),
75                length.assume_init() as usize,
76            );
77            ret
78        }
79    }
80}
81
82impl<O: IsA<UnixFDMessage>> UnixFDMessageExtManual for O {}