bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

read() may fail due to nonblocking stdin


From: Siteshwar Vashisht
Subject: read() may fail due to nonblocking stdin
Date: Sun, 22 Jan 2017 14:32:02 -0500 (EST)

If a child process sets stdin to non-blocking and does not set it back to 
blocking before exiting, other processes may fail to read from stdin.

Reproducer steps :

$ cat set_nonblock.c 
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
        char buff[256];
        int flags = fcntl (0, F_GETFL);
        if (fcntl(0, F_SETFL, flags | O_NONBLOCK)) {
                perror("fcntl failed");
        }
        if (read(0, buff, 256) == -1) {
                perror("read failed");
        }
}

$ cc -o set_nonblock set_nonblock.c
$ cat test.sh 
#!/bin/bash

./set_nonblock

cat

$ ./test.sh 
read failed: Resource temporarily unavailable
cat: -: Resource temporarily unavailable

Attached patch sets standard file descriptors to blocking before child process 
starts.

-- 
--
Siteshwar Vashisht

Attachment: 0001-Make-stdin-blocking-when-command-starts.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]