gio/
credentials.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use crate::{ffi, Credentials, CredentialsType};
6
7impl Credentials {
8    /// Gets a pointer to native credentials of type @native_type from
9    /// @self.
10    ///
11    /// It is a programming error (which will cause a warning to be
12    /// logged) to use this method if there is no #GCredentials support for
13    /// the OS or if @native_type isn't supported by the OS.
14    /// ## `native_type`
15    /// The type of native credentials to get.
16    ///
17    /// # Returns
18    ///
19    /// The pointer to native credentials or
20    ///     [`None`] if there is no #GCredentials support for the OS or if @native_type
21    ///     isn't supported by the OS. Do not free the returned data, it is owned
22    ///     by @self.
23    #[doc(alias = "g_credentials_get_native")]
24    #[doc(alias = "get_native")]
25    pub fn native(&self, native_type: CredentialsType) -> glib::ffi::gpointer {
26        unsafe { ffi::g_credentials_get_native(self.to_glib_none().0, native_type.into_glib()) }
27    }
28
29    /// Tries to get the UNIX process identifier from @self. This
30    /// method is only available on UNIX platforms.
31    ///
32    /// This operation can fail if #GCredentials is not supported on the
33    /// OS or if the native credentials type does not contain information
34    /// about the UNIX process ID.
35    ///
36    /// # Returns
37    ///
38    /// The UNIX process ID, or `-1` if @error is set.
39    #[cfg(unix)]
40    #[cfg_attr(docsrs, doc(cfg(unix)))]
41    #[doc(alias = "g_credentials_get_unix_pid")]
42    #[doc(alias = "get_unix_pid")]
43    pub fn unix_pid(&self) -> Result<libc::pid_t, glib::Error> {
44        unsafe {
45            let mut error = std::ptr::null_mut();
46            let ret = ffi::g_credentials_get_unix_pid(self.to_glib_none().0, &mut error);
47            if error.is_null() {
48                Ok(ret)
49            } else {
50                Err(from_glib_full(error))
51            }
52        }
53    }
54
55    /// Tries to get the UNIX user identifier from @self. This
56    /// method is only available on UNIX platforms.
57    ///
58    /// This operation can fail if #GCredentials is not supported on the
59    /// OS or if the native credentials type does not contain information
60    /// about the UNIX user.
61    ///
62    /// # Returns
63    ///
64    /// The UNIX user identifier or `-1` if @error is set.
65    #[cfg(unix)]
66    #[cfg_attr(docsrs, doc(cfg(unix)))]
67    #[doc(alias = "g_credentials_get_unix_user")]
68    #[doc(alias = "get_unix_user")]
69    pub fn unix_user(&self) -> Result<libc::uid_t, glib::Error> {
70        unsafe {
71            let mut error = std::ptr::null_mut();
72            let ret = ffi::g_credentials_get_unix_user(self.to_glib_none().0, &mut error);
73            if error.is_null() {
74                Ok(ret)
75            } else {
76                Err(from_glib_full(error))
77            }
78        }
79    }
80
81    /// Copies the native credentials of type @native_type from @native
82    /// into @self.
83    ///
84    /// It is a programming error (which will cause a warning to be
85    /// logged) to use this method if there is no #GCredentials support for
86    /// the OS or if @native_type isn't supported by the OS.
87    /// ## `native_type`
88    /// The type of native credentials to set.
89    /// ## `native`
90    /// A pointer to native credentials.
91    #[doc(alias = "g_credentials_set_native")]
92    pub unsafe fn set_native(&self, native_type: CredentialsType, native: glib::ffi::gpointer) {
93        unsafe {
94            ffi::g_credentials_set_native(self.to_glib_none().0, native_type.into_glib(), native)
95        }
96    }
97
98    /// Tries to set the UNIX user identifier on @self. This method
99    /// is only available on UNIX platforms.
100    ///
101    /// This operation can fail if #GCredentials is not supported on the
102    /// OS or if the native credentials type does not contain information
103    /// about the UNIX user. It can also fail if the OS does not allow the
104    /// use of "spoofed" credentials.
105    /// ## `uid`
106    /// The UNIX user identifier to set.
107    ///
108    /// # Returns
109    ///
110    /// [`true`] if @uid was set, [`false`] if error is set.
111    #[cfg(unix)]
112    #[cfg_attr(docsrs, doc(cfg(unix)))]
113    #[doc(alias = "g_credentials_set_unix_user")]
114    pub fn set_unix_user(&self, uid: libc::uid_t) -> Result<(), glib::Error> {
115        unsafe {
116            let mut error = std::ptr::null_mut();
117            let is_ok = ffi::g_credentials_set_unix_user(self.to_glib_none().0, uid, &mut error);
118            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
119            if error.is_null() {
120                Ok(())
121            } else {
122                Err(from_glib_full(error))
123            }
124        }
125    }
126}