1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
/*
* Small C program to verify that standard input is not closed but returns EOF
* on any read and that all file descriptors higher than 2 are closed.
*
* Written by Russ Allbery <eagle@eyrie.org>
* Copyright 2007-2008
* The Board of Trustees of the Leland Stanford Junior University
*
* SPDX-License-Identifier: MIT
*/
#include <config.h>
#include <portable/system.h>
#include <errno.h>
int
main(void)
{
char buffer;
ssize_t count;
int i;
/* First check that standard input is not closed but returns EOF. */
count = read(0, &buffer, 1);
if (count > 0) {
printf("Read %d bytes\n", (int) count);
exit(1);
} else if (count < 0) {
printf("Failed with error: %s\n", strerror(errno));
exit(2);
}
/*
* Now, check that all higher file descriptors are closed. (We only go up
* to 31; it's very unlikely that there will be problems higher than
* that.)
*/
for (i = 3; i < 32; i++)
if (close(i) >= 0 || errno != EBADF) {
printf("File descriptor %d was open\n", i);
exit(3);
}
printf("Okay");
exit(0);
}
|