reclog 0.1.6

Command-line tool to capture command output to a file.
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#![allow(clippy::unnecessary_cast)]

use libc::{self, FD_ISSET, FD_SET, FD_ZERO};
use rustix::io::Errno;
use rustix::process::{Pid, Signal};
use std::cmp::max;
use std::ffi::CStr;
use std::io::Error;
use std::mem::{self, MaybeUninit};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::ptr::null_mut;
use std::sync::Mutex;
use std::time::Duration;

/// Get errno from last libc call.
fn last_errno() -> Errno {
    Errno::from_io_error(&Error::last_os_error()).unwrap()
}

pub struct SelectFd<'fd> {
    pub fd: BorrowedFd<'fd>,
    pub mask: u32,
}

impl SelectFd<'_> {
    pub const READABLE: u32 = 0x1;
    pub const WRITEABLE: u32 = 0x2;
    pub const EXCEPTION: u32 = 0x4;
}

/// Safe shim for libc::select().
/// Handles EINTR.
pub fn select(select_fds: &mut [&mut SelectFd], timeout: Option<Duration>) -> Result<(), Errno> {
    let mut tv_timeout = timeout.map(|d| libc::timeval {
        tv_sec: d.as_secs() as libc::time_t,
        tv_usec: d.subsec_micros() as libc::suseconds_t,
    });

    let max_fd = select_fds
        .iter()
        .fold(0, |max_fd, sel_fd| max(max_fd, sel_fd.fd.as_raw_fd()));

    // SAFETY: We're holding an BorrowedFd (via SelectFd) for every descriptor
    // during the call, so they're guaranteed to be valid.
    //
    // NOTE: We use libc::select() instead of rustix::event::select() or
    // rustix::event::poll() because:
    //  - rustix::event::select() is not available on all platforms
    //  - rustix::event::poll() does not work with TTYs on macOS
    unsafe {
        let mut rd_fds = MaybeUninit::<libc::fd_set>::uninit();
        let mut wr_fds = MaybeUninit::<libc::fd_set>::uninit();
        let mut ex_fds = MaybeUninit::<libc::fd_set>::uninit();

        FD_ZERO(rd_fds.as_mut_ptr());
        FD_ZERO(wr_fds.as_mut_ptr());
        FD_ZERO(ex_fds.as_mut_ptr());

        rd_fds.assume_init();
        wr_fds.assume_init();
        ex_fds.assume_init();

        for sel_fd in select_fds.iter() {
            if sel_fd.mask & SelectFd::READABLE != 0 {
                FD_SET(sel_fd.fd.as_raw_fd(), rd_fds.as_mut_ptr());
            }
            if sel_fd.mask & SelectFd::WRITEABLE != 0 {
                FD_SET(sel_fd.fd.as_raw_fd(), wr_fds.as_mut_ptr());
            }
            if sel_fd.mask & SelectFd::EXCEPTION != 0 {
                FD_SET(sel_fd.fd.as_raw_fd(), ex_fds.as_mut_ptr());
            }
        }

        let mut nfds;

        loop {
            nfds = libc::select(
                max_fd + 1,
                rd_fds.as_mut_ptr(),
                wr_fds.as_mut_ptr(),
                ex_fds.as_mut_ptr(),
                if tv_timeout.is_some() {
                    tv_timeout.as_mut().unwrap() as *mut libc::timeval
                } else {
                    null_mut()
                },
            );
            if nfds < 0 {
                if last_errno() == Errno::INTR {
                    continue;
                }
                return Err(last_errno());
            }
            break;
        }

        for sel_fd in select_fds.iter_mut() {
            sel_fd.mask = 0;
            if FD_ISSET(sel_fd.fd.as_raw_fd(), rd_fds.as_mut_ptr()) {
                sel_fd.mask |= SelectFd::READABLE;
            }
            if FD_ISSET(sel_fd.fd.as_raw_fd(), wr_fds.as_mut_ptr()) {
                sel_fd.mask |= SelectFd::WRITEABLE;
            }
            if FD_ISSET(sel_fd.fd.as_raw_fd(), ex_fds.as_mut_ptr()) {
                sel_fd.mask |= SelectFd::EXCEPTION;
            }
        }
    };

    Ok(())
}

/// Safe (in context of this program) shim for libc::ptsname().
pub fn ptsname<Fd: AsFd>(fd: Fd) -> Result<String, Errno> {
    // SAFETY: ptsname() returns a pointer to static storage and hence is not
    // thread-safe. However, `reclog` is a program, not a library, and we know
    // in advance that this is the only place that calls it. The guard below
    // guarantees that the shim is not running concurrently.
    //
    // NOTE: We use libc::ptsname() instead of rustix::pty::ptsname() because
    // the latter is not available on all platforms. Rustix version works
    // only on platforms where non-standard thread-safe versions are available
    // (such as ptsname_r), but we don't want to limit supported platforms.
    static MUTEX: Mutex<()> = Mutex::new(());
    let _guard = MUTEX.lock();

    let s_ref = unsafe {
        let s_ptr = libc::ptsname(fd.as_fd().as_raw_fd());
        if s_ptr.is_null() {
            return Err(last_errno());
        }

        CStr::from_ptr(s_ptr).to_str().unwrap()
    };

    Ok(s_ref.to_string())
}

pub enum Fork {
    Parent(Pid),
    Child,
}

/// Convenience shim for libc::fork().
/// In Rust, fork() is not safe in general case, only its specific usages can be proven so.
/// Hence we mark shim as unsafe, and leave the safe usage as responsibility of the caller.
pub unsafe fn fork() -> Result<Fork, Errno> {
    match unsafe { libc::fork() } {
        pid if pid > 0 => Ok(Fork::Parent(Pid::from_raw(pid).unwrap())),
        0 => Ok(Fork::Child),
        _ => Err(last_errno()),
    }
}

/// Shim for libc::_exit().
/// It's like process::exit(), but it doesn't run atexit handlers or any other destructors,
/// just kills the process immediately.
/// While it's not really unsafe, we still mark it so, to make its usage bolder in code
/// when implementing safe use of fork().
pub unsafe fn fast_exit(code: i32) {
    unsafe { libc::_exit(code) }
}

/// Safe shim for libc::read().
/// Handles EINTR.
/// Unlike rustix version, doesn't consume the buffer.
pub fn read<Fd: AsFd>(fd: Fd, buf: &mut [u8]) -> Result<usize, Errno> {
    loop {
        let ret = unsafe {
            libc::read(
                fd.as_fd().as_raw_fd(),
                buf.as_mut_ptr() as *mut libc::c_void,
                buf.len(),
            )
        };
        if ret < 0 {
            if last_errno() == Errno::INTR {
                continue;
            }
            return Err(last_errno());
        }
        return Ok(ret as usize);
    }
}

/// Safe shim for libc::write().
/// Handles EINTR.
pub fn write<Fd: AsFd>(fd: Fd, buf: &[u8]) -> Result<usize, Errno> {
    loop {
        let ret = unsafe {
            libc::write(
                fd.as_fd().as_raw_fd(),
                buf.as_ptr() as *mut libc::c_void,
                buf.len(),
            )
        };
        if ret < 0 {
            if last_errno() == Errno::INTR {
                continue;
            }
            return Err(last_errno());
        }
        return Ok(ret as usize);
    }
}

/// Safe shim for libc::write().
/// Handles EINTR, EAGAIN, and partial writes.
pub fn write_all<Fd: AsFd>(fd: Fd, buf: &[u8]) -> Result<usize, Errno> {
    let mut pos = 0;
    while pos < buf.len() {
        let ret = unsafe {
            libc::write(
                fd.as_fd().as_raw_fd(),
                buf[pos..].as_ptr() as *mut libc::c_void,
                buf.len() - pos,
            )
        };
        if ret == 0 {
            continue;
        }
        if ret < 0 {
            if last_errno() == Errno::AGAIN || last_errno() == Errno::INTR {
                continue;
            }
            return Err(last_errno());
        }
        pos += ret as usize;
    }
    Ok(pos)
}

/// Shim for libc::close().
/// It violates OwnedFd/BorrowedFd contract by making it possible to close underlying
/// fd while it's still owned, hence marked unsafe.
/// Handles EINTR.
pub unsafe fn close_raw(fd: RawFd) {
    loop {
        if unsafe { libc::close(fd) } == 0 || last_errno() != Errno::INTR {
            break;
        }
    }
}

/// Safe shim for fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK).
/// Handles EINTR.
pub fn fcntl_nonblock<Fd: AsFd>(fd: Fd, non_block: bool) -> Result<(), Errno> {
    loop {
        let mut flags = unsafe { libc::fcntl(fd.as_fd().as_raw_fd(), libc::F_GETFL) };
        if flags < 0 {
            if last_errno() == Errno::INTR {
                continue;
            }
            return Err(last_errno());
        }

        if non_block {
            flags |= libc::O_NONBLOCK;
        } else {
            flags &= !libc::O_NONBLOCK;
        }

        let ret =
            unsafe { libc::fcntl(fd.as_fd().as_raw_fd(), libc::F_SETFL, flags as libc::c_uint) };
        if ret < 0 {
            if last_errno() == Errno::INTR {
                continue;
            }
            return Err(last_errno());
        }

        return Ok(());
    }
}

pub enum SigAction {
    Default,
    Ignore,
    Noop,
}

/// Safe shim for sigaction().
pub fn sigaction(sig: Signal, action: SigAction) -> Result<(), Errno> {
    extern "C" fn noop(_sig: libc::c_int) {}

    let handler = match action {
        SigAction::Default => libc::SIG_DFL,
        SigAction::Ignore => libc::SIG_IGN,
        SigAction::Noop => noop as libc::sighandler_t,
    };

    let ret = unsafe {
        let mut sa: libc::sigaction = mem::zeroed();
        sa.sa_sigaction = handler;
        sa.sa_flags = libc::SA_RESTART;
        libc::sigemptyset(&mut sa.sa_mask as *mut libc::sigset_t);

        libc::sigaction(sig.as_raw(), &sa, null_mut())
    };
    if ret < 0 {
        return Err(last_errno());
    }

    Ok(())
}

pub enum SigMask {
    Block,
    Unblock,
}

/// Safe shim for pthread_sigmask().
pub fn sigmask(sig_list: &[Signal], action: SigMask) -> Result<(), Errno> {
    let how = match action {
        SigMask::Block => libc::SIG_BLOCK,
        SigMask::Unblock => libc::SIG_UNBLOCK,
    };

    let ret = unsafe {
        let mut sig_set: libc::sigset_t = mem::zeroed();
        libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
        for sig in sig_list {
            libc::sigaddset(
                &mut sig_set as *mut libc::sigset_t,
                sig.as_raw() as libc::c_int,
            );
        }

        #[cfg(has_pthread_sigmask)]
        {
            libc::pthread_sigmask(how, &mut sig_set as *mut libc::sigset_t, null_mut())
        }

        // If pthread_sigmask() isn't available, we fallback to sigprocmask().
        // POSIX does not specify how sigprocmask behaves in case of multiple threads.
        // If we're lucky, sigprocmask() works per-thread on this system (i.e. same as
        // pthread_sigmask), which is true at least on some platforms.
        #[cfg(not(has_pthread_sigmask))]
        {
            libc::sigprocmask(how, &mut sig_set as *mut libc::sigset_t, null_mut())
        }
    };
    if ret < 0 {
        return Err(last_errno());
    }

    Ok(())
}

/// Safe shim for sigwait() with optional timeout.
/// Uses sigtimedwait() or sigwaitinfo().
#[cfg(has_sigtimedwait)]
pub fn sigwait(sig_list: &[Signal], timeout: Option<Duration>) -> Result<Option<Signal>, Errno> {
    let mut ts_timeout = timeout.map(|d| libc::timespec {
        tv_sec: d.as_secs() as libc::time_t,
        tv_nsec: d.subsec_nanos() as _,
    });

    let mut ret;
    loop {
        unsafe {
            let mut sig_set: libc::sigset_t = mem::zeroed();
            libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
            for sig in sig_list {
                libc::sigaddset(
                    &mut sig_set as *mut libc::sigset_t,
                    sig.as_raw() as libc::c_int,
                );
            }

            let mut sig_info: libc::siginfo_t = mem::zeroed();
            if ts_timeout.is_some() {
                ret = libc::sigtimedwait(
                    &mut sig_set as *mut libc::sigset_t,
                    &mut sig_info as *mut libc::siginfo_t,
                    ts_timeout.as_mut().unwrap() as *mut libc::timespec,
                );
            } else {
                ret = libc::sigwaitinfo(
                    &mut sig_set as *mut libc::sigset_t,
                    &mut sig_info as *mut libc::siginfo_t,
                )
            }
        };
        if ret < 0 {
            if last_errno() == Errno::AGAIN {
                // Timeout expired.
                return Ok(None);
            }
            if last_errno() == Errno::INTR {
                continue;
            }
            return Err(last_errno());
        }
        break;
    }

    let sig_no = ret as i32;
    match Signal::from_named_raw(sig_no) {
        Some(sig) => Ok(Some(sig)),
        None => Err(Errno::INVAL),
    }
}

/// Safe shim for sigwait() with optional timeout.
/// Uses sigwait() and timer_create().
#[cfg(all(not(has_sigtimedwait), has_timer_create))]
pub fn sigwait(sig_list: &[Signal], timeout: Option<Duration>) -> Result<Option<Signal>, Errno> {
    // We use SIGALRM, which makes this function not usable from concurrent threads.
    static MUTEX: Mutex<()> = Mutex::new(());
    let _guard = MUTEX.lock();

    let ts_timeout = timeout.map(|d| libc::timespec {
        tv_sec: d.as_secs() as libc::time_t,
        tv_nsec: d.subsec_nanos() as i64,
    });

    let mut sig_no: libc::c_int = 0;
    loop {
        unsafe {
            // If zero timeout is given, call sigwait() only if signal is already pending.
            if timeout.is_some() && timeout.unwrap().is_zero() {
                let mut sig_set: libc::sigset_t = mem::zeroed();
                libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
                libc::sigpending(&mut sig_set as *mut libc::sigset_t);

                if !sig_list.iter().any(|sig| {
                    libc::sigismember(
                        &mut sig_set as *mut libc::sigset_t,
                        sig.as_raw() as libc::c_int,
                    ) == 1
                }) {
                    return Ok(None);
                }
            }

            // If positive timeout is given, set timer for SIGALRM.
            let mut timer: libc::timer_t = mem::zeroed();

            if timeout.is_some() && !timeout.unwrap().is_zero() {
                let mut timer_sig: libc::sigevent = mem::zeroed();
                timer_sig.sigev_notify = libc::SIGEV_SIGNAL;
                timer_sig.sigev_signo = libc::SIGALRM;

                if libc::timer_create(
                    libc::CLOCK_MONOTONIC,
                    &mut timer_sig as *mut libc::sigevent,
                    &mut timer as *mut libc::timer_t,
                ) < 0
                {
                    if last_errno() == Errno::AGAIN || last_errno() == Errno::INTR {
                        continue;
                    }
                    return Err(last_errno());
                }

                let mut timer_spec: libc::itimerspec = mem::zeroed();
                timer_spec.it_value = ts_timeout.unwrap();

                if libc::timer_settime(
                    timer,
                    0,
                    &mut timer_spec as *mut libc::itimerspec,
                    null_mut(),
                ) < 0
                {
                    if last_errno() == Errno::AGAIN || last_errno() == Errno::INTR {
                        continue;
                    }
                    return Err(last_errno());
                }
            }

            // Wait for signal.
            let mut sig_set: libc::sigset_t = mem::zeroed();
            libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
            for sig in sig_list {
                libc::sigaddset(
                    &mut sig_set as *mut libc::sigset_t,
                    sig.as_raw() as libc::c_int,
                );
            }
            if timeout.is_some() {
                libc::sigaddset(&mut sig_set as *mut libc::sigset_t, libc::SIGALRM);
            }

            let err = libc::sigwait(
                &mut sig_set as *mut libc::sigset_t,
                &mut sig_no as *mut libc::c_int,
            );

            if timeout.is_some() {
                // Delete timer.
                libc::timer_delete(timer);

                // Clear pending SIGALRM.
                let mut sig_set: libc::sigset_t = mem::zeroed();
                libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
                libc::sigpending(&mut sig_set as *mut libc::sigset_t);

                if libc::sigismember(&mut sig_set as *mut libc::sigset_t, libc::SIGALRM) == 1 {
                    libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
                    libc::sigaddset(&mut sig_set as *mut libc::sigset_t, libc::SIGALRM);

                    let mut ignore_sig = 0;
                    libc::sigwait(
                        &mut sig_set as *mut libc::sigset_t,
                        &mut ignore_sig as *mut libc::c_int,
                    );
                }
            }

            if err != 0 {
                if last_errno() == Errno::AGAIN || last_errno() == Errno::INTR {
                    continue;
                }
                return Err(Errno::from_raw_os_error(err));
            }
            if sig_no == libc::SIGALRM {
                return Ok(None); // timeout expired
            }
            break;
        }
    }

    match Signal::from_named_raw(sig_no) {
        Some(sig) => Ok(Some(sig)),
        None => Err(Errno::INVAL),
    }
}

/// Safe shim for sigwait() with optional timeout.
/// Uses sigwait() and setitimer().
#[cfg(all(not(has_sigtimedwait), not(has_timer_create)))]
pub fn sigwait(sig_list: &[Signal], timeout: Option<Duration>) -> Result<Option<Signal>, Errno> {
    // We use SIGALRM, which makes this function not usable from concurrent threads.
    static MUTEX: Mutex<()> = Mutex::new(());
    let _guard = MUTEX.lock();

    let tv_timeout = timeout.map(|d| libc::timeval {
        tv_sec: d.as_secs() as libc::time_t,
        tv_usec: d.subsec_micros() as libc::suseconds_t,
    });

    let mut sig_no: libc::c_int = 0;
    loop {
        unsafe {
            // If zero timeout is given, call sigwait() only if signal is already pending.
            if timeout.is_some() && timeout.unwrap().is_zero() {
                let mut sig_set: libc::sigset_t = mem::zeroed();
                libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
                libc::sigpending(&mut sig_set as *mut libc::sigset_t);

                if !sig_list.iter().any(|sig| {
                    libc::sigismember(
                        &mut sig_set as *mut libc::sigset_t,
                        sig.as_raw() as libc::c_int,
                    ) == 1
                }) {
                    return Ok(None);
                }
            }

            // If positive timeout is given, set timer for SIGALRM.
            if timeout.is_some() && !timeout.unwrap().is_zero() {
                let mut timer_val: libc::itimerval = mem::zeroed();
                timer_val.it_value = tv_timeout.unwrap();

                if libc::setitimer(
                    libc::ITIMER_REAL,
                    &mut timer_val as *mut libc::itimerval,
                    null_mut(),
                ) < 0
                {
                    if last_errno() == Errno::AGAIN || last_errno() == Errno::INTR {
                        continue;
                    }
                    return Err(last_errno());
                }
            }

            // Wait for signal.
            let mut sig_set: libc::sigset_t = mem::zeroed();
            libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
            for sig in sig_list {
                libc::sigaddset(
                    &mut sig_set as *mut libc::sigset_t,
                    sig.as_raw() as libc::c_int,
                );
            }
            if timeout.is_some() {
                libc::sigaddset(&mut sig_set as *mut libc::sigset_t, libc::SIGALRM);
            }

            let err = libc::sigwait(
                &mut sig_set as *mut libc::sigset_t,
                &mut sig_no as *mut libc::c_int,
            );

            if timeout.is_some() {
                // Clear timer.
                let mut timer_val: libc::itimerval = mem::zeroed();

                if libc::setitimer(
                    libc::ITIMER_REAL,
                    &mut timer_val as *mut libc::itimerval,
                    null_mut(),
                ) < 0
                {
                    if last_errno() == Errno::AGAIN || last_errno() == Errno::INTR {
                        continue;
                    }
                    return Err(last_errno());
                }

                // Clear pending SIGALRM.
                let mut sig_set: libc::sigset_t = mem::zeroed();
                libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
                libc::sigpending(&mut sig_set as *mut libc::sigset_t);

                if libc::sigismember(&mut sig_set as *mut libc::sigset_t, libc::SIGALRM) == 1 {
                    libc::sigemptyset(&mut sig_set as *mut libc::sigset_t);
                    libc::sigaddset(&mut sig_set as *mut libc::sigset_t, libc::SIGALRM);

                    let mut ignore_sig = 0;
                    libc::sigwait(
                        &mut sig_set as *mut libc::sigset_t,
                        &mut ignore_sig as *mut libc::c_int,
                    );
                }
            }

            if err != 0 {
                if last_errno() == Errno::AGAIN || last_errno() == Errno::INTR {
                    continue;
                }
                return Err(Errno::from_raw_os_error(err));
            }
            if sig_no == libc::SIGALRM {
                return Ok(None); // timeout expired
            }
            break;
        }
    }

    match Signal::from_named_raw(sig_no) {
        Some(sig) => Ok(Some(sig)),
        None => Err(Errno::INVAL),
    }
}