Skip to content
Advertisement

reading from stdin after execl() bash return an eio(Input/output error)

The following code can act as expected if executed by a shell.

But if I set this program as a user’s shell and ssh into the host to execute this program as a shell, the read(0, &buf123, 1); will return an EIO(Input/output error):

JavaScript

But if a change execl(bash) into non-interactive bash execl(bash -c "id") or other program rather than bash, the read(0, &buf123, 1); will success.

So to reproduce this error, two conditions need to be met:

JavaScript

Could anyone help me figure out why and how to avoid this?

The following is the strace result:

JavaScript

Thanks in advance!

Advertisement

Answer

I happens because your sub-shell is a login interactive shell and so it took control over the terminal (set it as a its session control terminal). Then your process is disconnected from the terminal and cannot read on it anymore.

Of course if you use a non interactive shell, it don’t need the control over the terminal leaving it as is for your process.

Read about POSIX Terminal, sessions and processes group.

Advertisement