brho / akaros

Akaros Operating System
http://akaros.cs.berkeley.edu/
Other
314 stars 61 forks source link

dup2 of pipes misbehaves #41

Closed dvyukov closed 7 years ago

dvyukov commented 7 years ago

Program is:

#include <unistd.h>
#include <stdio.h>
#include <errno.h>

void readfd(int fd)
{
    char buf[100];
    int n = read(fd, buf, sizeof(buf) - 1);
    if (n < 0) {
        printf("read(%d) failed: %d\n", fd, errno);
        return;
    }
    buf[n] = 0;
    printf("read(%d): n=%d '%s'\n", fd, n, buf);
}

int main()
{
    if (dup2(0, 100) == -1) {
        printf("dup2 failed: %d\n", errno);
        return 1;
    }
    if (close(0)) {
        printf("close(0) failed: %d\n", errno);
        return 1;
    }
    readfd(100);
    readfd(0);
    readfd(5);
    return 0;
}

Actual output:

$ echo -n Hello | ./a.out
read(100) failed: 9
read(0) failed: 9
read(5): n=5 'Hello'

Expected output:

$ echo -n Hello | ./a.out
read(100): n=5 'Hello'
read(0) failed: 9
read(5) failed: 9

checkout is on 6344ed04e307ba30df879d1d407b10a1b3236784