1use crate::backend;
2#[cfg(feature = "sessiond")]
3use crate::sessiond_state;
4use std::io;
5#[cfg(feature = "consolekit")]
6use zbus::DBusError;
7#[cfg(feature = "consolekit")]
8use zbus::fdo;
9
10#[cfg(feature = "sessiond")]
11impl From<sessiond_state::Error> for backend::Error {
12 fn from(err: sessiond_state::Error) -> Self {
13 match err {
14 sessiond_state::Error::Io(err) => Self::Io(err),
15 err @ sessiond_state::Error::Decode(_) => {
16 Self::Io(io::Error::new(io::ErrorKind::InvalidData, err))
17 }
18 err => Self::Io(io::Error::other(err)),
19 }
20 }
21}
22
23impl From<&backend::Error> for crate::LibsessionStatus {
24 fn from(error: &backend::Error) -> Self {
25 match error {
26 backend::Error::Io(error) => error.into(),
27 backend::Error::ParseUid(_) => Self::InvalidData,
28 #[cfg(feature = "consolekit")]
29 backend::Error::Dbus(error) => error.into(),
30 }
31 }
32}
33
34impl From<&io::Error> for crate::LibsessionStatus {
35 fn from(value: &io::Error) -> Self {
36 if matches!(value.raw_os_error(), Some(libc::ESRCH | libc::ENODATA)) {
37 return Self::NotFound;
38 }
39
40 match value.kind() {
41 io::ErrorKind::NotFound => Self::NotFound,
42 io::ErrorKind::PermissionDenied => Self::PermissionDenied,
43 io::ErrorKind::InvalidInput => Self::InvalidArgument,
44 io::ErrorKind::InvalidData => Self::InvalidData,
45 io::ErrorKind::ConnectionRefused
46 | io::ErrorKind::ConnectionReset
47 | io::ErrorKind::ConnectionAborted
48 | io::ErrorKind::NotConnected
49 | io::ErrorKind::BrokenPipe
50 | io::ErrorKind::TimedOut => Self::Unavailable,
51 _ => Self::IoError,
52 }
53 }
54}
55
56#[cfg(feature = "consolekit")]
57impl From<&zbus::Error> for crate::LibsessionStatus {
58 fn from(value: &zbus::Error) -> Self {
59 let name = match value {
60 zbus::Error::InputOutput(error) => {
61 return if error.kind() == io::ErrorKind::NotFound {
62 Self::Unavailable
63 } else {
64 error.as_ref().into()
65 };
66 }
67 zbus::Error::MethodError(name, _, _) => name.as_str(),
68 zbus::Error::FDO(error) => {
69 if let fdo::Error::ZBus(error) = error.as_ref() {
70 return error.into();
71 }
72 return dbus_name_status(error.name().as_str());
73 }
74 _ => return Self::DbusError,
75 };
76 dbus_name_status(name)
77 }
78}
79
80#[cfg(feature = "consolekit")]
81fn dbus_name_status(name: &str) -> crate::LibsessionStatus {
82 match name {
83 "org.freedesktop.DBus.Error.AccessDenied" | "org.freedesktop.DBus.Error.AuthFailed" => {
84 crate::LibsessionStatus::PermissionDenied
85 }
86 "org.freedesktop.DBus.Error.InvalidArgs" => crate::LibsessionStatus::InvalidArgument,
87 "org.freedesktop.DBus.Error.UnknownObject" => crate::LibsessionStatus::NotFound,
88 "org.freedesktop.DBus.Error.ServiceUnknown"
89 | "org.freedesktop.DBus.Error.NameHasNoOwner"
90 | "org.freedesktop.DBus.Error.NoServer"
91 | "org.freedesktop.DBus.Error.NoNetwork"
92 | "org.freedesktop.DBus.Error.Disconnected"
93 | "org.freedesktop.DBus.Error.NoReply"
94 | "org.freedesktop.DBus.Error.Timeout"
95 | "org.freedesktop.DBus.Error.TimedOut" => crate::LibsessionStatus::Unavailable,
96 _ => crate::LibsessionStatus::DbusError,
97 }
98}