Skip to content

Commit

Permalink
acond: fix some issues found by
Browse files Browse the repository at this point in the history
1. remove io.rs since it is not necessary to keep it
2. some changes caused by removing io.rs
3. fix some issues reported by

Signed-off-by: Xiangquan Liu <[email protected]>
  • Loading branch information
xiangquanliu committed Jun 4, 2024
1 parent 69ba288 commit 2400553
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 288 deletions.
4 changes: 2 additions & 2 deletions acond/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl Container {
config_args: None,
exec_args: ExecArgs {
args,
envs: envs.iter().map(|var| var.clone()).collect::<Vec<_>>(),
envs: envs.to_vec(),
},
stdin: None,
stdout: None,
Expand All @@ -237,7 +237,7 @@ impl Container {

create_child(&fork_args)?;

return Ok((vec![], vec![]));
Ok((vec![], vec![]))
}

#[cfg(not(feature = "interactive"))]
Expand Down
132 changes: 0 additions & 132 deletions acond/src/io.rs

This file was deleted.

69 changes: 40 additions & 29 deletions acond/src/ipc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

use crate::{image::AttestDataValue, io as acond_io, pod::Pod, report, utils};
use crate::{image::AttestDataValue, pod::Pod, report, utils};
use anyhow::{anyhow, Result};
use std::{
fs::{self, Permissions},
Expand All @@ -12,7 +12,7 @@ use std::{
sync::Arc,
};
use tokio::{
io::AsyncReadExt,
io::{AsyncReadExt, AsyncWriteExt},
net::{UnixListener, UnixStream},
sync::{mpsc, oneshot, watch, RwLock},
};
Expand Down Expand Up @@ -146,39 +146,50 @@ fn error_to_vec(command: i32, err: &str) -> Vec<u8> {
msg_err_bytes
}

async fn async_read_struct<R, S>(reader: &mut R) -> Result<(S, Vec<u8>)>
where
R: AsyncReadExt + Unpin,
S: Copy,
{
let mut buf = vec![0u8; mem::size_of::<S>()];
reader.read_exact(&mut buf).await?;

let (_, body, _) = unsafe { buf.align_to::<S>() };
Ok((body[0], buf))
}

async fn handle_request(mut stream: UnixStream, tx: mpsc::Sender<Request>) -> Result<()> {
let recv_buf =
match acond_io::read_async_struct::<UnixStream, AconMessageHdr>(&mut stream).await {
Ok((msg_hdr, mut msg_hdr_buf)) => {
let msg_size = msg_hdr.size as usize;
if msg_size > utils::MAX_BUFF_SIZE || msg_size < mem::size_of::<AconMessageHdr>() {
let recv_buf = match async_read_struct::<UnixStream, AconMessageHdr>(&mut stream).await {
Ok((msg_hdr, mut msg_hdr_buf)) => {
let msg_size = msg_hdr.size as usize;
if msg_size > utils::MAX_BUFF_SIZE || msg_size < mem::size_of::<AconMessageHdr>() {
utils::ERR_IPC_INVALID_REQUEST.as_bytes().to_vec()
} else {
let mut msg_body_buf = vec![0; msg_size - mem::size_of::<AconMessageHdr>()];
if stream.read_exact(&mut msg_body_buf).await.is_err() {
utils::ERR_IPC_INVALID_REQUEST.as_bytes().to_vec()
} else {
let mut msg_body_buf = vec![0; msg_size - mem::size_of::<AconMessageHdr>()];
if stream.read_exact(&mut msg_body_buf).await.is_err() {
utils::ERR_IPC_INVALID_REQUEST.as_bytes().to_vec()
} else {
let (resp_tx, resp_rx) = oneshot::channel();
let mut buf = vec![];
buf.append(&mut msg_hdr_buf);
buf.append(&mut msg_body_buf);

let request = Request {
command: msg_hdr.command,
bytes: buf,
uid: stream.peer_cred()?.uid(),
resp_tx,
};

let _ = tx.send(request).await;
resp_rx.await?
}
let (resp_tx, resp_rx) = oneshot::channel();
let mut buf = vec![];
buf.append(&mut msg_hdr_buf);
buf.append(&mut msg_body_buf);

let request = Request {
command: msg_hdr.command,
bytes: buf,
uid: stream.peer_cred()?.uid(),
resp_tx,
};

let _ = tx.send(request).await;
resp_rx.await?
}
}
Err(_) => utils::ERR_IPC_INVALID_REQUEST.as_bytes().to_vec(),
};
}
Err(_) => utils::ERR_IPC_INVALID_REQUEST.as_bytes().to_vec(),
};

acond_io::write_async(&mut stream, &recv_buf, recv_buf.len()).await?;
stream.write_all(&recv_buf).await?;
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions acond/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use tokio::runtime::Builder;
mod config;
mod container;
mod image;
mod io;
mod ipc;
mod mount;
mod oidc;
Expand Down Expand Up @@ -73,7 +72,7 @@ fn start_service() -> Result<(), Box<dyn std::error::Error>> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Uncomment it to debug.
// Uncomment this line to show debug information.
// env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
mount::mount_rootfs()?;

Expand Down
Loading

0 comments on commit 2400553

Please sign in to comment.