Skip to content

Commit 54bd66e

Browse files
teohhanhuislp
authored andcommitted
Use uuidv7 for stdout / stderr files
It's too easy to hit the same millisecond... Signed-off-by: Teoh Han Hui <[email protected]>
1 parent 3614978 commit 54bd66e

9 files changed

Lines changed: 44 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ tempfile = { version = "3.10.1", default-features = false }
2727
tokio = { version = "1.38.0", default-features = false }
2828
tokio-stream = { version = "0.1.15", default-features = false }
2929
utils = { path = "crates/utils", default-features = false }
30+
uuid = { version = "1.10.0", default-features = false }

crates/krun-guest/src/mount.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ pub fn mount_filesystems() -> Result<()> {
5454

5555
// Expose the host filesystem (without any overlaid mounts) as /run/krun-host
5656
let host_path = Path::new("/run/krun-host");
57-
std::fs::create_dir_all(&host_path)?;
57+
std::fs::create_dir_all(host_path)?;
5858
mount_bind("/", host_path).context("Failed to bind-mount / on /run/krun-host")?;
5959

6060
if Path::new("/tmp/.X11-unix").exists() {
61-
// Mount a tmpfs for X11 sockets, so the guest doesn't clobber host X server sockets
61+
// Mount a tmpfs for X11 sockets, so the guest doesn't clobber host X server
62+
// sockets
6263
mount2(
6364
Some("tmpfs"),
6465
"/tmp/.X11-unix",

crates/krun-guest/src/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ where
2525
socket_path
2626
.as_ref()
2727
.to_str()
28-
.expect("pulse_path should not contain invalid UTF-8")
28+
.expect("socket_path should not contain invalid UTF-8")
2929
))
3030
.arg(format!("VSOCK-CONNECT:2:{}", port))
3131
.stdin(Stdio::null())

crates/krun-guest/src/x11.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use crate::socket::setup_socket_proxy;
2-
3-
use anyhow::{anyhow, Context, Result};
4-
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
51
use std::env;
62
use std::fs::File;
73
use std::io::{Read, Write};
84
use std::path::Path;
95

6+
use anyhow::{anyhow, Context, Result};
7+
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
8+
9+
use crate::socket::setup_socket_proxy;
10+
1011
pub fn setup_x11_forwarding<P>(run_path: P) -> Result<()>
1112
where
1213
P: AsRef<Path>,
@@ -70,13 +71,13 @@ where
7071

7172
wtr.write_u16::<BigEndian>(family)?;
7273
wtr.write_u16::<BigEndian>(addr.len().try_into()?)?;
73-
wtr.write(&addr)?;
74+
wtr.write_all(&addr)?;
7475
wtr.write_u16::<BigEndian>(display.len().try_into()?)?;
75-
wtr.write(display)?;
76+
wtr.write_all(display)?;
7677
wtr.write_u16::<BigEndian>(name.len().try_into()?)?;
77-
wtr.write(&name)?;
78+
wtr.write_all(&name)?;
7879
wtr.write_u16::<BigEndian>(data.len().try_into()?)?;
79-
wtr.write(&data)?;
80+
wtr.write_all(&data)?;
8081

8182
break;
8283
}

crates/krun/src/bin/krun.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ fn main() -> Result<()> {
198198

199199
// Forward the native X11 display into the guest as a socket
200200
if let Ok(x11_display) = env::var("DISPLAY") {
201-
if x11_display.starts_with(":") {
202-
let socket_path = Path::new("/tmp/.X11-unix/").join(format!("X{}", &x11_display[1..]));
201+
if let Some(x11_display) = x11_display.strip_prefix(":") {
202+
let socket_path = Path::new("/tmp/.X11-unix/").join(format!("X{}", x11_display));
203203
if socket_path.exists() {
204204
let socket_path = CString::new(
205205
socket_path

crates/krun/src/net.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ pub fn start_passt(server_port: u32) -> Result<UnixStream> {
2525

2626
// SAFETY: The parent process should not keep the file descriptor of
2727
// `child_socket` open. It is a `UnixStream` so the file descriptor will be
28-
// closed on drop. See https://doc.rust-lang.org/std/io/index.html#io-safety
28+
// closed on drop.
29+
// See https://doc.rust-lang.org/std/io/index.html#io-safety
2930
//
3031
// The `dup` call clears the `FD_CLOEXEC` flag on the new `child_fd`, which
3132
// should be inherited by the child process.

crates/utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ publish = false
1010
[dependencies]
1111
anyhow = { workspace = true, features = ["std"] }
1212
serde = { workspace = true, features = ["derive"] }
13+
uuid = { workspace = true, features = ["std", "v7"] }
1314

1415
[features]
1516
default = []

crates/utils/src/stdio.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::collections::HashMap;
22
use std::fs::File;
33
use std::path::Path;
44
use std::process::Stdio;
5-
use std::time::{SystemTime, UNIX_EPOCH};
65

76
use anyhow::{Context, Result};
7+
use uuid::Uuid;
88

99
pub fn make_stdout_stderr<P>(command: P, envs: &HashMap<String, String>) -> Result<(Stdio, Stdio)>
1010
where
@@ -22,9 +22,9 @@ where
2222
} else {
2323
Path::new("/tmp")
2424
};
25-
let ts = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis();
26-
let path_stdout = base.join(format!("krun-{filename}-{ts}.stdout"));
27-
let path_stderr = base.join(format!("krun-{filename}-{ts}.stderr"));
25+
let uuid = Uuid::now_v7();
26+
let path_stdout = base.join(format!("krun-{filename}-{uuid}.stdout"));
27+
let path_stderr = base.join(format!("krun-{filename}-{uuid}.stderr"));
2828
Ok((
2929
File::create_new(path_stdout)?.into(),
3030
File::create_new(path_stderr)?.into(),

0 commit comments

Comments
 (0)