diff --git a/src/main.rs b/src/main.rs index b13d5b2..d9ecaf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,13 +5,14 @@ mod system; use platform::Platform; use result::Result; +use system::dmesg; fn main() { if let Err(e) = init() { - eprintln!("Error: {e}"); + dmesg(format!("Error: {e}")); let mut opt = Some(&e as &dyn std::error::Error); while let Some(current_source) = opt { - eprintln!("Caused by: {current_source}"); + dmesg(format!("Caused by: {current_source}")); opt = current_source.source(); } } @@ -42,12 +43,12 @@ fn init_rootfs() { ]; for (src, target, fstype, flags, data) in args { match system::syscall::mount(src, target, fstype, flags, Some(data)) { - Ok(())=> eprintln!("Mounted {target}"), + Ok(())=> dmesg(format!("Mounted {target}")), Err(e)=> { - eprintln!("Error: {e}"); + dmesg(format!("Error: {e}")); let mut opt = Some(&e as &dyn std::error::Error); while let Some(current_source) = opt { - eprintln!("Caused by: {current_source}"); + dmesg(format!("Caused by: {current_source}")); opt = current_source.source(); } }, @@ -61,10 +62,10 @@ fn init() -> Result<()> { // init_rootfs(); if let Err(errors) = system::mount_default_targets() { for error in errors { - eprintln!("Error while mounting: {error}"); + dmesg(format!("Error while mounting: {error}")); let mut opt = Some(&error as &dyn std::error::Error); while let Some(current_source) = opt { - eprintln!("Caused by: {current_source}"); + dmesg(format!("Caused by: {current_source}")); opt = current_source.source(); } } diff --git a/src/platform/aws.rs b/src/platform/aws.rs index 2ecbf98..68f9960 100644 --- a/src/platform/aws.rs +++ b/src/platform/aws.rs @@ -1,4 +1,7 @@ -use crate::result::{Context, Result}; +use crate::{ + result::{Context, Result}, + system::dmesg, +}; pub struct Aws; @@ -70,8 +73,10 @@ impl super::Platform for Aws { } mod enclaveos_shim { + use super::dmesg; mod system { + use super::dmesg; use std::os::fd::AsRawFd; pub fn insmod(path: &str) { @@ -79,15 +84,11 @@ mod enclaveos_shim { let file = std::fs::File::open(path).unwrap(); let fd = file.as_raw_fd(); if unsafe { syscall(SYS_finit_module, fd, &[0u8; 1], 0) } < 0 { - eprintln!("bad insert kernel module: {path}"); + dmesg(format!("bad insert kernel module: {path}")); } } - pub fn socket_connect( - family: libc::c_int, - port: u32, - cid: u32, - ) -> libc::c_int { - use libc::{connect, socket, sockaddr, sockaddr_vm, SOCK_STREAM}; + pub fn socket_connect(family: libc::c_int, port: u32, cid: u32) -> libc::c_int { + use libc::{connect, sockaddr, sockaddr_vm, socket, SOCK_STREAM}; let fd = unsafe { socket(family, SOCK_STREAM, 0) }; if unsafe { let mut sa: sockaddr_vm = std::mem::zeroed(); @@ -99,7 +100,8 @@ mod enclaveos_shim { &sa as *const _ as *mut sockaddr, size_of::() as _, ) - } < 0 { + } < 0 + { panic!("yikes") } else { fd @@ -107,7 +109,7 @@ mod enclaveos_shim { } } - pub fn init_platform(){ + pub fn init_platform() { use system::insmod; // TODO: error handling nitro_heartbeat(); @@ -117,8 +119,8 @@ mod enclaveos_shim { } fn nitro_heartbeat() { + use libc::{close, read, write, AF_VSOCK}; use system::socket_connect; - use libc::{write, read, close, AF_VSOCK}; let mut buf: [u8; 1] = [0; 1]; buf[0] = 0xB7; // AWS Nitro heartbeat value let fd = socket_connect(AF_VSOCK, 9000, 3); @@ -127,6 +129,6 @@ mod enclaveos_shim { read(fd, buf.as_ptr() as _, 1); close(fd); } - eprintln!("Sent NSM heartbeat"); + dmesg("Sent NSM heartbeat"); } } diff --git a/src/system/mod.rs b/src/system/mod.rs index cf30e03..726fa57 100644 --- a/src/system/mod.rs +++ b/src/system/mod.rs @@ -6,7 +6,7 @@ pub mod syscall; pub fn dmesg(value: impl std::fmt::Display) { let timespec = syscall::clock_gettime(libc::CLOCK_BOOTTIME).unwrap(); - eprintln!("[{: >5}.{}]{value}", timespec.tv_sec, timespec.tv_nsec / 1000); + eprintln!("[{: >5}.{}] {value}", timespec.tv_sec, timespec.tv_nsec / 1000); } pub enum MountType {