Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draf: Handle SIGWINCH signal add resize logic #1811

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lengrongfu
Copy link
Collaborator

issue: #1672

@codecov-commenter
Copy link

codecov-commenter commented Apr 13, 2023

Codecov Report

Merging #1811 (6a7afb0) into main (307e941) will decrease coverage by 0.14%.
The diff coverage is 51.61%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1811      +/-   ##
==========================================
- Coverage   68.64%   68.50%   -0.14%     
==========================================
  Files         121      121              
  Lines       13325    13378      +53     
==========================================
+ Hits         9147     9165      +18     
- Misses       4178     4213      +35     

.write(true)
.custom_flags(libc::O_RDWR | libc::O_NOCTTY | libc::O_CLOEXEC)
.open(path)?;
let ret = unsafe { libc::ioctl(socket_file.as_raw_fd(), libc::TIOCSWINSZ, &ws) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried using the nix crate?
https://docs.rs/nix/latest/nix/sys/ioctl/index.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw a saying that because nix is slow to update, some projects now directly use libc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, libc::TIOCGWINSZ and libc::TIOCSWINSZ cannot be found in nix.

Copy link
Collaborator

@yihuaf yihuaf Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw a saying that because nix is slow to update, some projects now directly use libc.

Can you post a link here if you can still find it? I would love to see it. I will also read up on ioctl from nix, but from a quick scanning, it looks to be quite messy so for this simple case, it may be worth to just go with the libc version.

Signed-off-by: lengrongfu <[email protected]>
Copy link
Collaborator

@yihuaf yihuaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks OK. Left some nits and I'd love to see more comments explaining what is happening. Please also clean up the comments in the test case since it is copy pasted from other test case.

// We mask all signals here and forward most of the signals to the container
// init process.
resize(socket_path.as_ref())?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move resize() to above the comment.

log::debug!("winsize is {:?},socket_path is {:?}", ws, socket_path);

// get socket_file file fd to set socket_file termios winsize
// let path = socket_path.unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deadcode?

@@ -105,12 +110,50 @@ fn handle_foreground(init_pid: Pid) -> Result<i32> {
}
}

fn resize(socket_path: Option<&PathBuf>) -> Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write comments explaining the resizing process? Including the reference to the ioctl used? The comment should allow someone with no prior background information to quickly understand what is going on.

.write(true)
.custom_flags(libc::O_RDWR | libc::O_NOCTTY | libc::O_CLOEXEC)
.open(path)?;
let ret = unsafe { libc::ioctl(socket_file.as_raw_fd(), libc::TIOCSWINSZ, &ws) };
Copy link
Collaborator

@yihuaf yihuaf Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw a saying that because nix is slow to update, some projects now directly use libc.

Can you post a link here if you can still find it? I would love to see it. I will also read up on ioctl from nix, but from a quick scanning, it looks to be quite messy so for this simple case, it may be worth to just go with the libc version.

// We mask all signals here and forward most of the signals to the container
// init process.
resize(socket_path.as_ref())?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also want to use with_context to add some error context. Soon, we will try to move away from anyhow is a number of places in favor of thiserror for more rusty error handling, but for the moment, it would be nice to continue with anyhow context, so we make the debugging easier.

};
let ret = unsafe { libc::ioctl(tty_file.as_raw_fd(), libc::TIOCGWINSZ, &mut ws) };
if ret != 0 {
return Err(anyhow::format_err!(std::io::Error::last_os_error()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add a proper error message to identify that the error is for resizing. Otherwise, we get an error number with no idea where the error is coming from. For example, I can't debug a EINVAL with no additional information.


// get socket_file file fd to set socket_file termios winsize
// let path = socket_path.unwrap();
if let Some(path) = socket_path {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: In general, if there is one if branch, I am in favor of using if let .... If there are both the if let ... else, then I am in favor a bit more to using match. Functionally, there are the same, and this is a more of a personal taste thing. Up to you.

}

#[test]
fn test_foreground_sigwatch() -> Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you copy pasted the previous test function. Can you clean up the comments because it doesn't make sense anymore. For example, what is the expected behavior after the child process receives SIGWINCH?

@utam0k
Copy link
Member

utam0k commented Apr 30, 2023

Hi @lengrongfu, it appears that there are conflicts between this PR and the main branch. Could you kindly review and update it? Thank you.

@lengrongfu lengrongfu changed the title Handle SIGWINCH signal add resize logic Draf: Handle SIGWINCH signal add resize logic May 6, 2023
@lengrongfu
Copy link
Collaborator Author

I referred to runc and found that the current console does not support resize logic, because we lack a scene to create a tty:
https://github.com/opencontainers/runc/blob/5a9266b068b86f37c66330c37067250a32c4076a/utils_linux.go#L98-L110

current youki console steup code:
https://github.com/containers/youki/blob/e4110f89269395a6efef0f36492a319e284594ba/crates/libcontainer/src/tty.rs#L20-L45

So this PR may have to be suspended first, and another console logic needs to be implemented first.

@yihuaf @utam0k

@utam0k
Copy link
Member

utam0k commented May 10, 2023

@lengrongfu 👍

So this PR may have to be suspended first, and another console logic needs to be implemented first.

@utam0k utam0k marked this pull request as draft May 10, 2023 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants