diff --git a/crates/kiri-init/src/linux/container.rs b/crates/kiri-init/src/linux/container.rs index 8800dc00..7f448290 100644 --- a/crates/kiri-init/src/linux/container.rs +++ b/crates/kiri-init/src/linux/container.rs @@ -808,6 +808,19 @@ pub(super) fn valid_id(id: &str) -> io::Result<()> { Ok(()) } +/// Creates `cwd` and its parents, 0755, when it is missing. Errors are left +/// to the chdir after the user switch, which reports them. +fn make_workdir(cwd: &std::ffi::CStr) { + // SAFETY: a valid NUL-terminated path. + if unsafe { libc::access(cwd.as_ptr(), libc::F_OK) } == 0 { + return; + } + use std::os::unix::fs::DirBuilderExt; + // SAFETY: the bytes came from a &str, so they are valid UTF-8. + let dir = unsafe { std::ffi::OsStr::from_encoded_bytes_unchecked(cwd.to_bytes()) }; + let _ = fs::DirBuilder::new().recursive(true).mode(0o755).create(dir); +} + fn prepare(argv: StrList<'_>, env: StrList<'_>, cwd: &str, user: &str, hostname: Option<&str>, flags: u32, caps: u64) -> io::Result { let c = |s: &str| CString::new(s).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "argument contains NUL")); let argv = argv.iter().map(c).collect::>>()?; @@ -1229,6 +1242,10 @@ fn container_child(p: &Prepared, rootfs: &CStr, io: &ChildStdio) -> ! { libc::umount2(c".".as_ptr(), libc::MNT_DETACH); libc::chdir(c"/".as_ptr()); } + // Docker creates a missing working directory (mkdir -p) as root, 0755, + // before it switches user and before the root goes read-only, so both + // `-u 1000 -w /missing` and `--read-only -w /missing` start (#370). + make_workdir(&p.cwd); if p.readonly_rootfs { // Only the root itself: volumes, tmpfs and /dev stay as they are. sys::mount(c"none", c"/", c"none", libc::MS_REMOUNT | libc::MS_BIND | libc::MS_RDONLY, None).unwrap_or_else(|e| fail("read-only root", e)); diff --git a/crates/kiri/src/daemon/api.rs b/crates/kiri/src/daemon/api.rs index c23e0cfd..58c1fe9f 100644 --- a/crates/kiri/src/daemon/api.rs +++ b/crates/kiri/src/daemon/api.rs @@ -473,7 +473,9 @@ pub(super) fn reference_matches(pattern: &str, name: &str) -> bool { fn images(req: &Request, engine: &Engine) -> Result { let f = filters(req, &["reference", "dangling", "label"])?; - let list = engine.store.images()?; + let mut list = engine.store.images()?; + // Newest first, as Docker lists them (#364). + list.sort_by_key(|i| std::cmp::Reverse(time::parse_unix(&i.created))); // Every stored image is tagged, so there are no dangling ones. if f.bool("dangling") == Some(true) { return Ok(json!([])); diff --git a/tests/e2e.sh b/tests/e2e.sh index 92605573..71bb8b5c 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -95,6 +95,8 @@ t pull-alpine "alpine:latest" $D pull alpine:latest t pull-busybox "busybox" $D pull busybox t pull-tag "3.21" $D pull alpine:3.21 t images-list "alpine" $D images +# Newest first, as Docker lists them (#364). +t images-newest-first "sorted" bash -c "$D images --format '{{.CreatedAt}}' | sort -rc && echo sorted" t image-inspect-arch "$($D version -f '{{.Server.Arch}}')" $D image inspect -f '{{.Architecture}}' alpine t image-inspect-cmd "/bin/sh" $D image inspect -f '{{.Config.Cmd}}' alpine code pull-missing 1 $D pull kiri-does-not-exist/nope:none @@ -130,6 +132,12 @@ t env "bar" $D run --rm -e FOO=bar alpine sh -c 'echo $FOO' t env-image "PATH=/usr/local/sbin" $D run --rm alpine env t workdir "/tmp" $D run --rm -w /tmp alpine pwd t workdir-created "/made/here" $D run --rm -w /made/here alpine pwd +# A non-root user gets the missing directory too, made by root as 0755 (#370). +t workdir-created-nonroot "/made/for/1000" $D run --rm -u 1000:1000 -w /made/for/1000 alpine pwd +t workdir-created-nonroot-mode "755 root" $D run --rm -u 1000:1000 -w /made/x alpine stat -c '%a %U' /made/x +# ... and with a read-only root, made before the root goes read-only, as in Docker. +t workdir-created-readonly "/made/ro" $D run --rm --read-only -w /made/ro alpine pwd +t workdir-created-readonly-nonroot "/made/ro/1000" $D run --rm --read-only -u 1000:1000 -w /made/ro/1000 alpine pwd t user-numeric "uid=1000" $D run --rm -u 1000 alpine id t user-name "uid=70(postgres)" $D run --rm -u postgres postgres:17-alpine id t user-group "gid=0(root)" $D run --rm -u 1000:0 alpine id