aboutsummaryrefslogtreecommitdiff
path: root/rust/qemu-api/src/errno.rs
blob: 18d101448b93652df20f8f3f9c9b678c38a2df1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// SPDX-License-Identifier: GPL-2.0-or-later

//! Utility functions to convert `errno` to and from
//! [`io::Error`]/[`io::Result`]
//!
//! QEMU C functions often have a "positive success/negative `errno`" calling
//! convention.  This module provides functions to portably convert an integer
//! into an [`io::Result`] and back.

use std::{convert::TryFrom, io, io::ErrorKind};

/// An `errno` value that can be converted into an [`io::Error`]
pub struct Errno(pub u16);

// On Unix, from_raw_os_error takes an errno value and OS errors
// are printed using strerror.  On Windows however it takes a
// GetLastError() value; therefore we need to convert errno values
// into io::Error by hand.  This is the same mapping that the
// standard library uses to retrieve the kind of OS errors
// (`std::sys::pal::unix::decode_error_kind`).
impl From<Errno> for ErrorKind {
    fn from(value: Errno) -> ErrorKind {
        use ErrorKind::*;
        let Errno(errno) = value;
        match i32::from(errno) {
            libc::EPERM | libc::EACCES => PermissionDenied,
            libc::ENOENT => NotFound,
            libc::EINTR => Interrupted,
            x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => WouldBlock,
            libc::ENOMEM => OutOfMemory,
            libc::EEXIST => AlreadyExists,
            libc::EINVAL => InvalidInput,
            libc::EPIPE => BrokenPipe,
            libc::EADDRINUSE => AddrInUse,
            libc::EADDRNOTAVAIL => AddrNotAvailable,
            libc::ECONNABORTED => ConnectionAborted,
            libc::ECONNREFUSED => ConnectionRefused,
            libc::ECONNRESET => ConnectionReset,
            libc::ENOTCONN => NotConnected,
            libc::ENOTSUP => Unsupported,
            libc::ETIMEDOUT => TimedOut,
            _ => Other,
        }
    }
}

// This is used on Windows for all io::Errors, but also on Unix if the
// io::Error does not have a raw OS error.  This is the reversed
// mapping of the above; EIO is returned for unknown ErrorKinds.
impl From<io::ErrorKind> for Errno {
    fn from(value: io::ErrorKind) -> Errno {
        use ErrorKind::*;
        let errno = match value {
            // can be both EPERM or EACCES :( pick one
            PermissionDenied => libc::EPERM,
            NotFound => libc::ENOENT,
            Interrupted => libc::EINTR,
            WouldBlock => libc::EAGAIN,
            OutOfMemory => libc::ENOMEM,
            AlreadyExists => libc::EEXIST,
            InvalidInput => libc::EINVAL,
            BrokenPipe => libc::EPIPE,
            AddrInUse => libc::EADDRINUSE,
            AddrNotAvailable => libc::EADDRNOTAVAIL,
            ConnectionAborted => libc::ECONNABORTED,
            ConnectionRefused => libc::ECONNREFUSED,
            ConnectionReset => libc::ECONNRESET,
            NotConnected => libc::ENOTCONN,
            Unsupported => libc::ENOTSUP,
            TimedOut => libc::ETIMEDOUT,
            _ => libc::EIO,
        };
        Errno(errno as u16)
    }
}

impl From<Errno> for io::Error {
    #[cfg(unix)]
    fn from(value: Errno) -> io::Error {
        let Errno(errno) = value;
        io::Error::from_raw_os_error(errno.into())
    }

    #[cfg(windows)]
    fn from(value: Errno) -> io::Error {
        let error_kind: ErrorKind = value.into();
        error_kind.into()
    }
}

impl From<io::Error> for Errno {
    fn from(value: io::Error) -> Errno {
        if cfg!(unix) {
            if let Some(errno) = value.raw_os_error() {
                return Errno(u16::try_from(errno).unwrap());
            }
        }
        value.kind().into()
    }
}

/// Internal traits; used to enable [`into_io_result`] and [`into_neg_errno`]
/// for the "right" set of types.
mod traits {
    use super::Errno;

    /// A signed type that can be converted into an
    /// [`io::Result`](std::io::Result)
    pub trait GetErrno {
        /// Unsigned variant of `Self`, used as the type for the `Ok` case.
        type Out;

        /// Return `Ok(self)` if positive, `Err(Errno(-self))` if negative
        fn into_errno_result(self) -> Result<Self::Out, Errno>;
    }

    /// A type that can be taken out of an [`io::Result`](std::io::Result) and
    /// converted into "positive success/negative `errno`" convention.
    pub trait MergeErrno {
        /// Signed variant of `Self`, used as the return type of
        /// [`into_neg_errno`](super::into_neg_errno).
        type Out: From<u16> + std::ops::Neg<Output = Self::Out>;

        /// Return `self`, asserting that it is in range
        fn map_ok(self) -> Self::Out;
    }

    macro_rules! get_errno {
        ($t:ty, $out:ty) => {
            impl GetErrno for $t {
                type Out = $out;
                fn into_errno_result(self) -> Result<Self::Out, Errno> {
                    match self {
                        0.. => Ok(self as $out),
                        -65535..=-1 => Err(Errno(-self as u16)),
                        _ => panic!("{self} is not a negative errno"),
                    }
                }
            }
        };
    }

    get_errno!(i32, u32);
    get_errno!(i64, u64);
    get_errno!(isize, usize);

    macro_rules! merge_errno {
        ($t:ty, $out:ty) => {
            impl MergeErrno for $t {
                type Out = $out;
                fn map_ok(self) -> Self::Out {
                    self.try_into().unwrap()
                }
            }
        };
    }

    merge_errno!(u8, i32);
    merge_errno!(u16, i32);
    merge_errno!(u32, i32);
    merge_errno!(u64, i64);

    impl MergeErrno for () {
        type Out = i32;
        fn map_ok(self) -> i32 {
            0
        }
    }
}

use traits::{GetErrno, MergeErrno};

/// Convert an integer value into a [`io::Result`].
///
/// Positive values are turned into an `Ok` result; negative values
/// are interpreted as negated `errno` and turned into an `Err`.
///
/// ```
/// # use qemu_api::errno::into_io_result;
/// # use std::io::ErrorKind;
/// let ok = into_io_result(1i32).unwrap();
/// assert_eq!(ok, 1u32);
///
/// let err = into_io_result(-1i32).unwrap_err(); // -EPERM
/// assert_eq!(err.kind(), ErrorKind::PermissionDenied);
/// ```
///
/// # Panics
///
/// Since the result is an unsigned integer, negative values must
/// be close to 0; values that are too far away are considered
/// likely overflows and will panic:
///
/// ```should_panic
/// # use qemu_api::errno::into_io_result;
/// # #[allow(dead_code)]
/// let err = into_io_result(-0x1234_5678i32); // panic
/// ```
pub fn into_io_result<T: GetErrno>(value: T) -> io::Result<T::Out> {
    value.into_errno_result().map_err(Into::into)
}

/// Convert a [`Result`] into an integer value, using negative `errno`
/// values to report errors.
///
/// ```
/// # use qemu_api::errno::into_neg_errno;
/// # use std::io::{self, ErrorKind};
/// let ok: io::Result<()> = Ok(());
/// assert_eq!(into_neg_errno(ok), 0);
///
/// let err: io::Result<()> = Err(ErrorKind::InvalidInput.into());
/// assert_eq!(into_neg_errno(err), -22); // -EINVAL
/// ```
///
/// Since this module also provides the ability to convert [`io::Error`]
/// to an `errno` value, [`io::Result`] is the most commonly used type
/// for the argument of this function:
///
/// # Panics
///
/// Since the result is a signed integer, integer `Ok` values must remain
/// positive:
///
/// ```should_panic
/// # use qemu_api::errno::into_neg_errno;
/// # use std::io;
/// let err: io::Result<u32> = Ok(0x8899_AABB);
/// into_neg_errno(err) // panic
/// # ;
/// ```
pub fn into_neg_errno<T: MergeErrno, E: Into<Errno>>(value: Result<T, E>) -> T::Out {
    match value {
        Ok(x) => x.map_ok(),
        Err(err) => -T::Out::from(err.into().0),
    }
}

#[cfg(test)]
mod tests {
    use std::io::ErrorKind;

    use super::*;
    use crate::assert_match;

    #[test]
    pub fn test_from_u8() {
        let ok: io::Result<_> = Ok(42u8);
        assert_eq!(into_neg_errno(ok), 42);

        let err: io::Result<u8> = Err(io::ErrorKind::PermissionDenied.into());
        assert_eq!(into_neg_errno(err), -1);

        if cfg!(unix) {
            let os_err: io::Result<u8> = Err(io::Error::from_raw_os_error(10));
            assert_eq!(into_neg_errno(os_err), -10);
        }
    }

    #[test]
    pub fn test_from_u16() {
        let ok: io::Result<_> = Ok(1234u16);
        assert_eq!(into_neg_errno(ok), 1234);

        let err: io::Result<u16> = Err(io::ErrorKind::PermissionDenied.into());
        assert_eq!(into_neg_errno(err), -1);

        if cfg!(unix) {
            let os_err: io::Result<u16> = Err(io::Error::from_raw_os_error(10));
            assert_eq!(into_neg_errno(os_err), -10);
        }
    }

    #[test]
    pub fn test_i32() {
        assert_match!(into_io_result(1234i32), Ok(1234));

        let err = into_io_result(-1i32).unwrap_err();
        #[cfg(unix)]
        assert_match!(err.raw_os_error(), Some(1));
        assert_match!(err.kind(), ErrorKind::PermissionDenied);
    }

    #[test]
    pub fn test_from_u32() {
        let ok: io::Result<_> = Ok(1234u32);
        assert_eq!(into_neg_errno(ok), 1234);

        let err: io::Result<u32> = Err(io::ErrorKind::PermissionDenied.into());
        assert_eq!(into_neg_errno(err), -1);

        if cfg!(unix) {
            let os_err: io::Result<u32> = Err(io::Error::from_raw_os_error(10));
            assert_eq!(into_neg_errno(os_err), -10);
        }
    }

    #[test]
    pub fn test_i64() {
        assert_match!(into_io_result(1234i64), Ok(1234));

        let err = into_io_result(-22i64).unwrap_err();
        #[cfg(unix)]
        assert_match!(err.raw_os_error(), Some(22));
        assert_match!(err.kind(), ErrorKind::InvalidInput);
    }

    #[test]
    pub fn test_from_u64() {
        let ok: io::Result<_> = Ok(1234u64);
        assert_eq!(into_neg_errno(ok), 1234);

        let err: io::Result<u64> = Err(io::ErrorKind::InvalidInput.into());
        assert_eq!(into_neg_errno(err), -22);

        if cfg!(unix) {
            let os_err: io::Result<u64> = Err(io::Error::from_raw_os_error(6));
            assert_eq!(into_neg_errno(os_err), -6);
        }
    }

    #[test]
    pub fn test_isize() {
        assert_match!(into_io_result(1234isize), Ok(1234));

        let err = into_io_result(-4isize).unwrap_err();
        #[cfg(unix)]
        assert_match!(err.raw_os_error(), Some(4));
        assert_match!(err.kind(), ErrorKind::Interrupted);
    }

    #[test]
    pub fn test_from_unit() {
        let ok: io::Result<_> = Ok(());
        assert_eq!(into_neg_errno(ok), 0);

        let err: io::Result<()> = Err(io::ErrorKind::OutOfMemory.into());
        assert_eq!(into_neg_errno(err), -12);

        if cfg!(unix) {
            let os_err: io::Result<()> = Err(io::Error::from_raw_os_error(2));
            assert_eq!(into_neg_errno(os_err), -2);
        }
    }
}