mirror of
https://github.com/brain-hackers/u-boot-brain
synced 2024-09-29 16:10:24 +09:00
sandbox: Restore blocking I/O on exit
At present sandbox sets non-blocking I/O as soon as any input is read from the terminal. However it does not restore the previous state on exit. Fix this and drop the old os_read_no_block() function. This means that we always enable blocking I/O in sandbox (if input is a terminal) whereas previously it would only happen on the first call to tstc() or getc(). However, the difference is likely not important. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
66613f5dd2
commit
4af3e9ad8a
@ -38,14 +38,6 @@ ssize_t os_read(int fd, void *buf, size_t count)
|
|||||||
return read(fd, buf, count);
|
return read(fd, buf, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t os_read_no_block(int fd, void *buf, size_t count)
|
|
||||||
{
|
|
||||||
const int flags = fcntl(fd, F_GETFL, 0);
|
|
||||||
|
|
||||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
||||||
return os_read(fd, buf, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t os_write(int fd, const void *buf, size_t count)
|
ssize_t os_write(int fd, const void *buf, size_t count)
|
||||||
{
|
{
|
||||||
return write(fd, buf, count);
|
return write(fd, buf, count);
|
||||||
@ -129,11 +121,18 @@ int os_write_file(const char *name, const void *buf, int size)
|
|||||||
/* Restore tty state when we exit */
|
/* Restore tty state when we exit */
|
||||||
static struct termios orig_term;
|
static struct termios orig_term;
|
||||||
static bool term_setup;
|
static bool term_setup;
|
||||||
|
static bool term_nonblock;
|
||||||
|
|
||||||
void os_fd_restore(void)
|
void os_fd_restore(void)
|
||||||
{
|
{
|
||||||
if (term_setup) {
|
if (term_setup) {
|
||||||
|
int flags;
|
||||||
|
|
||||||
tcsetattr(0, TCSANOW, &orig_term);
|
tcsetattr(0, TCSANOW, &orig_term);
|
||||||
|
if (term_nonblock) {
|
||||||
|
flags = fcntl(0, F_GETFL, 0);
|
||||||
|
fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
|
||||||
|
}
|
||||||
term_setup = false;
|
term_setup = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,6 +141,7 @@ void os_fd_restore(void)
|
|||||||
void os_tty_raw(int fd, bool allow_sigs)
|
void os_tty_raw(int fd, bool allow_sigs)
|
||||||
{
|
{
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
int flags;
|
||||||
|
|
||||||
if (term_setup)
|
if (term_setup)
|
||||||
return;
|
return;
|
||||||
@ -158,6 +158,13 @@ void os_tty_raw(int fd, bool allow_sigs)
|
|||||||
if (tcsetattr(fd, TCSANOW, &term))
|
if (tcsetattr(fd, TCSANOW, &term))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
flags = fcntl(fd, F_GETFL, 0);
|
||||||
|
if (!(flags & O_NONBLOCK)) {
|
||||||
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
|
||||||
|
return;
|
||||||
|
term_nonblock = true;
|
||||||
|
}
|
||||||
|
|
||||||
term_setup = true;
|
term_setup = true;
|
||||||
atexit(os_fd_restore);
|
atexit(os_fd_restore);
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ static int sandbox_serial_pending(struct udevice *dev, bool input)
|
|||||||
if (next_index == serial_buf_read)
|
if (next_index == serial_buf_read)
|
||||||
return 1; /* buffer full */
|
return 1; /* buffer full */
|
||||||
|
|
||||||
count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
|
count = os_read(0, &serial_buf[serial_buf_write], 1);
|
||||||
if (count == 1)
|
if (count == 1)
|
||||||
serial_buf_write = next_index;
|
serial_buf_write = next_index;
|
||||||
|
|
||||||
|
10
include/os.h
10
include/os.h
@ -26,16 +26,6 @@ struct sandbox_state;
|
|||||||
*/
|
*/
|
||||||
ssize_t os_read(int fd, void *buf, size_t count);
|
ssize_t os_read(int fd, void *buf, size_t count);
|
||||||
|
|
||||||
/**
|
|
||||||
* Access to the OS read() system call with non-blocking access
|
|
||||||
*
|
|
||||||
* \param fd File descriptor as returned by os_open()
|
|
||||||
* \param buf Buffer to place data
|
|
||||||
* \param count Number of bytes to read
|
|
||||||
* \return number of bytes read, or -1 on error
|
|
||||||
*/
|
|
||||||
ssize_t os_read_no_block(int fd, void *buf, size_t count);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Access to the OS write() system call
|
* Access to the OS write() system call
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user