libc-rs 0.2.0

Pure-Rust no_std native runtime and capability call substrate for unikernel environments
Documentation
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
//! Filesystem metadata, path resolution, and directory iteration.

use core::mem::{MaybeUninit, offset_of, size_of};

use crate::errno::{EINVAL, ENAMETOOLONG, Errno, SysResult};
use crate::fd::{AT_FDCWD, AT_REMOVEDIR, AT_SYMLINK_NOFOLLOW, RawFd};
use crate::raw::{nr, syscall1, syscall2, syscall3, syscall4, syscall5};

pub const PATH_MAX: usize = 4096;

pub const O_RDONLY: i32 = 0;
pub const O_WRONLY: i32 = 1;
pub const O_RDWR: i32 = 2;
pub const O_ACCMODE: i32 = 3;

pub const O_CREAT: i32 = 0x40;
pub const O_EXCL: i32 = 0x80;
pub const O_NOCTTY: i32 = 0x100;
pub const O_TRUNC: i32 = 0x200;
pub const O_APPEND: i32 = 0x400;
pub const O_NONBLOCK: i32 = 0x800;
pub const O_DSYNC: i32 = 0x1000;
pub const O_DIRECT: i32 = 0x4000;
pub const O_LARGEFILE: i32 = 0x8000;
pub const O_DIRECTORY: i32 = 0x10000;
pub const O_NOFOLLOW: i32 = 0x20000;
pub const O_NOATIME: i32 = 0x40000;
pub const O_CLOEXEC: i32 = 0x80000;
pub const O_PATH: i32 = 0x200000;

pub const SEEK_SET: i32 = 0;
pub const SEEK_CUR: i32 = 1;
pub const SEEK_END: i32 = 2;

pub const F_OK: i32 = 0;
pub const X_OK: i32 = 1;
pub const W_OK: i32 = 2;
pub const R_OK: i32 = 4;

pub const S_IFMT: u32 = 0o17_0000;
pub const S_IFSOCK: u32 = 0o14_0000;
pub const S_IFLNK: u32 = 0o12_0000;
pub const S_IFREG: u32 = 0o10_0000;
pub const S_IFBLK: u32 = 0o06_0000;
pub const S_IFDIR: u32 = 0o04_0000;
pub const S_IFCHR: u32 = 0o02_0000;
pub const S_IFIFO: u32 = 0o01_0000;

pub const LOCK_SH: i32 = 1;
pub const LOCK_EX: i32 = 2;
pub const LOCK_NB: i32 = 4;
pub const LOCK_UN: i32 = 8;

/// 128-byte AArch64 file metadata structure (`struct stat`).
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Stat {
    pub st_dev: u64,
    pub st_ino: u64,
    pub st_mode: u32,
    pub st_nlink: u32,
    pub st_uid: u32,
    pub st_gid: u32,
    pub st_rdev: u64,
    pub pad1: u64,
    pub st_size: i64,
    pub st_blksize: i32,
    pub pad2: i32,
    pub st_blocks: i64,
    pub st_atime: i64,
    pub st_atime_nsec: i64,
    pub st_mtime: i64,
    pub st_mtime_nsec: i64,
    pub st_ctime: i64,
    pub st_ctime_nsec: i64,
    pub unused: [u32; 2],
}

const _: () = {
    assert!(size_of::<Stat>() == 128);
    assert!(offset_of!(Stat, st_size) == 48);
    assert!(offset_of!(Stat, st_atime) == 72);
};

/// Invokes `f` with a pointer to a NUL-terminated path buffer.
///
/// - Rejects empty paths or paths with interior NUL bytes with `EINVAL`.
/// - If `path` already ends with a single trailing NUL (`b'\0'`) and has length `<= PATH_MAX`,
///   passes `path.as_ptr()` directly without copying.
/// - Otherwise copies `path` into a stack `[0u8; PATH_MAX]` buffer and appends `0`.
pub fn with_c_path<R>(path: &[u8], f: impl FnOnce(*const u8) -> SysResult<R>) -> SysResult<R> {
    if path.is_empty() {
        return Err(Errno(EINVAL));
    }
    if let Some((&last, prefix)) = path.split_last() {
        if last == 0 {
            if prefix.is_empty() || prefix.contains(&0) {
                return Err(Errno(EINVAL));
            }
            if path.len() > PATH_MAX {
                return Err(Errno(ENAMETOOLONG));
            }
            return f(path.as_ptr());
        }
    }
    if path.contains(&0) {
        return Err(Errno(EINVAL));
    }
    if path.len() >= PATH_MAX {
        return Err(Errno(ENAMETOOLONG));
    }
    if path.len() < 256 {
        let mut buf = [0u8; 256];
        buf[..path.len()].copy_from_slice(path);
        return f(buf.as_ptr());
    }
    with_c_path_long(path, f)
}

#[cold]
fn with_c_path_long<R>(path: &[u8], f: impl FnOnce(*const u8) -> SysResult<R>) -> SysResult<R> {
    let mut buf = [0u8; PATH_MAX];
    buf[..path.len()].copy_from_slice(path);
    f(buf.as_ptr())
}

/// Converts a NUL-terminated C string pointer bounded by [`PATH_MAX`] into a byte slice.
///
/// # Safety
///
/// `ptr` must be non-null and point to a valid NUL-terminated byte sequence within `PATH_MAX` bytes.
pub unsafe fn cstr_slice<'a>(ptr: *const u8) -> SysResult<&'a [u8]> {
    if ptr.is_null() {
        return Err(Errno(EINVAL));
    }
    let mut len = 0usize;
    while len < PATH_MAX {
        // SAFETY: Caller guarantees `ptr` is readable up to the terminating NUL within `PATH_MAX`.
        let b = unsafe { *ptr.add(len) };
        if b == 0 {
            // SAFETY: `[ptr, ptr + len)` was verified to be initialized bytes before NUL.
            return Ok(unsafe { core::slice::from_raw_parts(ptr, len) });
        }
        len += 1;
    }
    Err(Errno(ENAMETOOLONG))
}

/// Opens a file relative to `dirfd`.
pub fn openat(dirfd: RawFd, path: &[u8], flags: i32, mode: u32) -> SysResult<RawFd> {
    with_c_path(path, |c_path| {
        // SAFETY: `c_path` points to a valid NUL-terminated buffer.
        let raw = unsafe {
            syscall4(
                nr::OPENAT,
                dirfd as usize,
                c_path as usize,
                flags as u32 as usize,
                mode as usize,
            )?
        };
        Ok(raw as RawFd)
    })
}

/// Opens a file relative to the current working directory (`AT_FDCWD`).
pub fn open(path: &[u8], flags: i32, mode: u32) -> SysResult<RawFd> {
    openat(AT_FDCWD, path, flags, mode)
}

/// Queries file metadata relative to `dirfd`.
pub fn statat(dirfd: RawFd, path: &[u8], flags: i32) -> SysResult<Stat> {
    with_c_path(path, |c_path| {
        let mut st = MaybeUninit::<Stat>::zeroed();
        // SAFETY: `c_path` is a valid C string and `st` is a writable 128-byte `Stat` buffer.
        unsafe {
            syscall4(
                nr::NEWFSTATAT,
                dirfd as usize,
                c_path as usize,
                st.as_mut_ptr() as usize,
                flags as u32 as usize,
            )?;
            Ok(st.assume_init())
        }
    })
}

/// Queries file metadata (following symbolic links).
pub fn stat(path: &[u8]) -> SysResult<Stat> {
    statat(AT_FDCWD, path, 0)
}

/// Queries file metadata without following symbolic links (`AT_SYMLINK_NOFOLLOW`).
pub fn lstat(path: &[u8]) -> SysResult<Stat> {
    statat(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW)
}

/// Queries file metadata for an open file descriptor `fd`.
pub fn fstat(fd: RawFd) -> SysResult<Stat> {
    let mut st = MaybeUninit::<Stat>::zeroed();
    // SAFETY: `st` is a writable 128-byte `Stat` buffer.
    unsafe {
        syscall2(nr::FSTAT, fd as usize, st.as_mut_ptr() as usize)?;
        Ok(st.assume_init())
    }
}

/// Repositions the read/write file offset of `fd`.
pub fn lseek(fd: RawFd, offset: i64, whence: i32) -> SysResult<i64> {
    // SAFETY: `LSEEK` takes integer arguments only.
    let raw = unsafe {
        syscall3(
            nr::LSEEK,
            fd as usize,
            offset as usize,
            whence as u32 as usize,
        )?
    };
    Ok(raw as i64)
}

/// Truncates or extends a file referred to by `fd` to `length` bytes.
pub fn ftruncate(fd: RawFd, length: i64) -> SysResult<()> {
    // SAFETY: `FTRUNCATE` takes a file descriptor and byte length.
    unsafe {
        syscall2(nr::FTRUNCATE, fd as usize, length as usize)?;
    }
    Ok(())
}

/// Synchronizes all modified in-core data and metadata of `fd` to storage.
pub fn fsync(fd: RawFd) -> SysResult<()> {
    // SAFETY: `FSYNC` takes a single file descriptor.
    unsafe {
        syscall1(nr::FSYNC, fd as usize)?;
    }
    Ok(())
}

/// Changes permissions of an open file descriptor `fd`.
pub fn fchmod(fd: RawFd, mode: u32) -> SysResult<()> {
    // SAFETY: `FCHMOD` takes a file descriptor and mode bits.
    unsafe {
        syscall2(nr::FCHMOD, fd as usize, mode as usize)?;
    }
    Ok(())
}

/// Creates a directory relative to `dirfd`.
pub fn mkdirat(dirfd: RawFd, path: &[u8], mode: u32) -> SysResult<()> {
    with_c_path(path, |c_path| {
        // SAFETY: `c_path` is a valid NUL-terminated path.
        unsafe {
            syscall3(nr::MKDIRAT, dirfd as usize, c_path as usize, mode as usize)?;
        }
        Ok(())
    })
}

/// Creates a directory relative to the current working directory.
pub fn mkdir(path: &[u8], mode: u32) -> SysResult<()> {
    mkdirat(AT_FDCWD, path, mode)
}

/// Unlinks a file or directory relative to `dirfd`.
pub fn unlinkat(dirfd: RawFd, path: &[u8], flags: i32) -> SysResult<()> {
    with_c_path(path, |c_path| {
        // SAFETY: `c_path` is a valid NUL-terminated path.
        unsafe {
            syscall3(
                nr::UNLINKAT,
                dirfd as usize,
                c_path as usize,
                flags as u32 as usize,
            )?;
        }
        Ok(())
    })
}

/// Removes a directory entry relative to the current working directory.
pub fn unlink(path: &[u8]) -> SysResult<()> {
    unlinkat(AT_FDCWD, path, 0)
}

/// Removes an empty directory relative to the current working directory.
pub fn rmdir(path: &[u8]) -> SysResult<()> {
    unlinkat(AT_FDCWD, path, AT_REMOVEDIR)
}

/// Renames a filesystem entry between `olddir` and `newdir`.
pub fn renameat(olddir: RawFd, oldpath: &[u8], newdir: RawFd, newpath: &[u8]) -> SysResult<()> {
    with_c_path(oldpath, |c_old| {
        with_c_path(newpath, |c_new| {
            // SAFETY: Both `c_old` and `c_new` are valid NUL-terminated paths.
            unsafe {
                syscall4(
                    nr::RENAMEAT,
                    olddir as usize,
                    c_old as usize,
                    newdir as usize,
                    c_new as usize,
                )?;
            }
            Ok(())
        })
    })
}

/// Renames a filesystem entry relative to the current working directory.
pub fn rename(oldpath: &[u8], newpath: &[u8]) -> SysResult<()> {
    renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath)
}

/// Creates a hard link between `olddir`/`oldpath` and `newdir`/`newpath`.
pub fn linkat(
    olddir: RawFd,
    oldpath: &[u8],
    newdir: RawFd,
    newpath: &[u8],
    flags: i32,
) -> SysResult<()> {
    with_c_path(oldpath, |c_old| {
        with_c_path(newpath, |c_new| {
            // SAFETY: Both `c_old` and `c_new` are valid NUL-terminated paths.
            unsafe {
                syscall5(
                    nr::LINKAT,
                    olddir as usize,
                    c_old as usize,
                    newdir as usize,
                    c_new as usize,
                    flags as u32 as usize,
                )?;
            }
            Ok(())
        })
    })
}

/// Creates a hard link relative to the current working directory.
pub fn link(oldpath: &[u8], newpath: &[u8]) -> SysResult<()> {
    linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0)
}

/// Reads the target of a symbolic link relative to `dirfd` into `out`.
pub fn readlinkat<'a>(dirfd: RawFd, path: &[u8], out: &'a mut [u8]) -> SysResult<&'a [u8]> {
    if out.is_empty() {
        return Err(Errno(EINVAL));
    }
    let n = with_c_path(path, |c_path| {
        // SAFETY: `c_path` is a valid C string and `out` is a writable byte buffer.
        unsafe {
            syscall4(
                nr::READLINKAT,
                dirfd as usize,
                c_path as usize,
                out.as_mut_ptr() as usize,
                out.len(),
            )
        }
    })?;
    let len = n.min(out.len());
    Ok(&out[..len])
}

/// Reads the target of a symbolic link relative to the current working directory into `out`.
pub fn readlink<'a>(path: &[u8], out: &'a mut [u8]) -> SysResult<&'a [u8]> {
    readlinkat(AT_FDCWD, path, out)
}

/// Checks accessibility of `path` relative to `dirfd`.
pub fn faccessat(dirfd: RawFd, path: &[u8], mode: i32, flags: i32) -> SysResult<()> {
    with_c_path(path, |c_path| {
        // SAFETY: `c_path` is a valid NUL-terminated path.
        unsafe {
            syscall4(
                nr::FACCESSAT,
                dirfd as usize,
                c_path as usize,
                mode as u32 as usize,
                flags as u32 as usize,
            )?;
        }
        Ok(())
    })
}

/// Checks accessibility of `path` relative to the current working directory.
pub fn access(path: &[u8], mode: i32) -> SysResult<()> {
    faccessat(AT_FDCWD, path, mode, 0)
}

/// Applies or removes an advisory lock on open file `fd`.
pub fn flock(fd: RawFd, operation: i32) -> SysResult<()> {
    // SAFETY: `FLOCK` takes a file descriptor and integer operation flags.
    unsafe {
        syscall2(nr::FLOCK, fd as usize, operation as u32 as usize)?;
    }
    Ok(())
}

/// Queries the current working directory into `out`, returning the slice without the trailing NUL.
pub fn getcwd(out: &mut [u8]) -> SysResult<&[u8]> {
    if out.is_empty() {
        return Err(Errno(EINVAL));
    }
    // SAFETY: `out` is a writable slice of `out.len()` bytes.
    let n = unsafe { syscall2(nr::GETCWD, out.as_mut_ptr() as usize, out.len())? };
    let used = n.min(out.len());
    let slice = &out[..used];
    if let Some(pos) = slice.iter().position(|&b| b == 0) {
        Ok(&slice[..pos])
    } else {
        Ok(slice)
    }
}

/// Changes the current working directory to `path`.
pub fn chdir(path: &[u8]) -> SysResult<()> {
    with_c_path(path, |c_path| {
        // SAFETY: `c_path` is a valid NUL-terminated path.
        unsafe {
            syscall1(nr::CHDIR, c_path as usize)?;
        }
        Ok(())
    })
}

/// Reads raw `linux_dirent64` entries from directory `fd` into `buf`.
pub fn getdents64(fd: RawFd, buf: &mut [u8]) -> SysResult<usize> {
    // SAFETY: `buf` is a valid writable byte slice.
    unsafe {
        syscall3(
            nr::GETDENTS64,
            fd as usize,
            buf.as_mut_ptr() as usize,
            buf.len(),
        )
    }
}

/// Borrowed view of a single `linux_dirent64` directory entry.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DirEntryRef<'a> {
    pub ino: u64,
    pub off: i64,
    pub file_type: u8,
    pub name: &'a [u8],
}

/// Iterator over packed `linux_dirent64` records in a byte slice.
#[derive(Clone, Debug)]
pub struct DirentIter<'a> {
    buf: &'a [u8],
    offset: usize,
}

/// Parses packed `linux_dirent64` records from the `filled` prefix returned by [`getdents64`].
#[must_use]
pub fn parse_dirents(filled: &[u8]) -> DirentIter<'_> {
    DirentIter {
        buf: filled,
        offset: 0,
    }
}

impl<'a> Iterator for DirentIter<'a> {
    type Item = DirEntryRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let rem = &self.buf[self.offset..];
        if rem.len() < 19 {
            self.offset = self.buf.len();
            return None;
        }
        let ino = u64::from_ne_bytes([
            rem[0], rem[1], rem[2], rem[3], rem[4], rem[5], rem[6], rem[7],
        ]);
        let off = i64::from_ne_bytes([
            rem[8], rem[9], rem[10], rem[11], rem[12], rem[13], rem[14], rem[15],
        ]);
        let reclen = u16::from_ne_bytes([rem[16], rem[17]]) as usize;
        if reclen < 19 || reclen > rem.len() {
            self.offset = self.buf.len();
            return None;
        }
        let file_type = rem[18];
        let name_region = &rem[19..reclen];
        let name_len = name_region
            .iter()
            .position(|&b| b == 0)
            .unwrap_or(name_region.len());
        self.offset += reclen;
        Some(DirEntryRef {
            ino,
            off,
            file_type,
            name: &name_region[..name_len],
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::raw::with_mock_syscall;

    #[test]
    fn stat_layout_matches_aarch64_abi() {
        assert_eq!(size_of::<Stat>(), 128);
        assert_eq!(offset_of!(Stat, st_dev), 0);
        assert_eq!(offset_of!(Stat, st_ino), 8);
        assert_eq!(offset_of!(Stat, st_mode), 16);
        assert_eq!(offset_of!(Stat, st_nlink), 20);
        assert_eq!(offset_of!(Stat, st_uid), 24);
        assert_eq!(offset_of!(Stat, st_gid), 28);
        assert_eq!(offset_of!(Stat, st_rdev), 32);
        assert_eq!(offset_of!(Stat, st_size), 48);
        assert_eq!(offset_of!(Stat, st_blksize), 56);
        assert_eq!(offset_of!(Stat, st_blocks), 64);
        assert_eq!(offset_of!(Stat, st_atime), 72);
        assert_eq!(offset_of!(Stat, st_mtime), 88);
        assert_eq!(offset_of!(Stat, st_ctime), 104);
    }

    #[test]
    fn with_c_path_validates_and_avoids_copy_when_nul_terminated() {
        assert_eq!(with_c_path(b"", |_| Ok(())), Err(Errno(EINVAL)));
        assert_eq!(with_c_path(b"\0", |_| Ok(())), Err(Errno(EINVAL)));
        assert_eq!(with_c_path(b"a\0b", |_| Ok(())), Err(Errno(EINVAL)));
        assert_eq!(with_c_path(b"a\0b\0", |_| Ok(())), Err(Errno(EINVAL)));

        let pre_nul = b"/etc/hosts\0";
        let ptr_seen = with_c_path(pre_nul, Ok).unwrap();
        assert_eq!(ptr_seen, pre_nul.as_ptr());

        let slice_no_nul = b"/etc/hosts";
        with_c_path(slice_no_nul, |p| {
            assert_ne!(p, slice_no_nul.as_ptr());
            let decoded = unsafe { cstr_slice(p)? };
            assert_eq!(decoded, slice_no_nul);
            Ok(())
        })
        .unwrap();

        let too_long = [b'x'; PATH_MAX];
        assert_eq!(with_c_path(&too_long, |_| Ok(())), Err(Errno(ENAMETOOLONG)));
    }

    #[test]
    fn parse_dirents_decodes_records() {
        let mut buf = [0u8; 48];
        // Record 1: ino=10, off=24, reclen=24, type=4 (DT_DIR), name="."
        buf[0..8].copy_from_slice(&10u64.to_ne_bytes());
        buf[8..16].copy_from_slice(&24i64.to_ne_bytes());
        buf[16..18].copy_from_slice(&24u16.to_ne_bytes());
        buf[18] = 4;
        buf[19] = b'.';
        buf[20] = 0;

        // Record 2: ino=42, off=48, reclen=24, type=8 (DT_REG), name="abc"
        buf[24..32].copy_from_slice(&42u64.to_ne_bytes());
        buf[32..40].copy_from_slice(&48i64.to_ne_bytes());
        buf[40..42].copy_from_slice(&24u16.to_ne_bytes());
        buf[42] = 8;
        buf[43..46].copy_from_slice(b"abc");
        buf[46] = 0;

        let mut iter = parse_dirents(&buf);
        let first = iter.next().unwrap();
        assert_eq!(
            first,
            DirEntryRef {
                ino: 10,
                off: 24,
                file_type: 4,
                name: b".",
            }
        );
        let second = iter.next().unwrap();
        assert_eq!(
            second,
            DirEntryRef {
                ino: 42,
                off: 48,
                file_type: 8,
                name: b"abc",
            }
        );
        assert!(iter.next().is_none());
    }

    #[test]
    fn getcwd_strips_trailing_nul() {
        with_mock_syscall(
            |nr, args| {
                assert_eq!(nr, nr::GETCWD);
                let out_ptr = args[0] as *mut u8;
                let data = b"/work\0";
                unsafe {
                    core::ptr::copy_nonoverlapping(data.as_ptr(), out_ptr, data.len());
                }
                Ok(data.len())
            },
            || {
                let mut buf = [0u8; 32];
                let path = getcwd(&mut buf).unwrap();
                assert_eq!(path, b"/work");
            },
        );
    }
}