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::{Credentials, CredentialsType, ffi};
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 /// As the signedness of `uid_t` is not specified by POSIX, it is recommended to
63 /// check @error for failure rather than trying to check the return value,
64 /// particularly in language bindings.
65 ///
66 /// # Returns
67 ///
68 /// The UNIX user identifier or `(uid_t) -1` if @error is set.
69 #[cfg(unix)]
70 #[cfg_attr(docsrs, doc(cfg(unix)))]
71 #[doc(alias = "g_credentials_get_unix_user")]
72 #[doc(alias = "get_unix_user")]
73 pub fn unix_user(&self) -> Result<libc::uid_t, glib::Error> {
74 unsafe {
75 let mut error = std::ptr::null_mut();
76 let ret = ffi::g_credentials_get_unix_user(self.to_glib_none().0, &mut error);
77 if error.is_null() {
78 Ok(ret)
79 } else {
80 Err(from_glib_full(error))
81 }
82 }
83 }
84
85 /// Copies the native credentials of type @native_type from @native
86 /// into @self.
87 ///
88 /// It is a programming error (which will cause a warning to be
89 /// logged) to use this method if there is no #GCredentials support for
90 /// the OS or if @native_type isn't supported by the OS.
91 /// ## `native_type`
92 /// The type of native credentials to set.
93 /// ## `native`
94 /// A pointer to native credentials.
95 #[doc(alias = "g_credentials_set_native")]
96 pub unsafe fn set_native(&self, native_type: CredentialsType, native: glib::ffi::gpointer) {
97 unsafe {
98 ffi::g_credentials_set_native(self.to_glib_none().0, native_type.into_glib(), native)
99 }
100 }
101
102 /// Tries to set the UNIX user identifier on @self. This method
103 /// is only available on UNIX platforms.
104 ///
105 /// This operation can fail if #GCredentials is not supported on the
106 /// OS or if the native credentials type does not contain information
107 /// about the UNIX user. It can also fail if the OS does not allow the
108 /// use of "spoofed" credentials.
109 /// ## `uid`
110 /// The UNIX user identifier to set.
111 ///
112 /// # Returns
113 ///
114 /// [`true`] if @uid was set, [`false`] if error is set.
115 #[cfg(unix)]
116 #[cfg_attr(docsrs, doc(cfg(unix)))]
117 #[doc(alias = "g_credentials_set_unix_user")]
118 pub fn set_unix_user(&self, uid: libc::uid_t) -> Result<(), glib::Error> {
119 unsafe {
120 let mut error = std::ptr::null_mut();
121 let is_ok = ffi::g_credentials_set_unix_user(self.to_glib_none().0, uid, &mut error);
122 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
123 if error.is_null() {
124 Ok(())
125 } else {
126 Err(from_glib_full(error))
127 }
128 }
129 }
130}