关闭它们后重新打开stdout和标准输入文件描述符 [英] Re-opening stdout and stdin file descriptors after closing them
问题描述
我正在写一个函数,该函数,有一个参数,要么在标准输出重定向到文件或从文件中读取标准输入。要做到这一点,我关闭与标准输出或标准输入相关的文件描述符,这样,当我打开文件时,它是我刚刚闭幕的描述下打开。这工作,但问题是,一旦做到这一点,我需要恢复的标准输出和标准输入到他们确实应该。
我能为标准输出要做的就是打开(为/ dev / tty的,O_WRONLY);但我不知道为什么这工作的,更重要的是,我不知道标准输入等效声明的。
所以我有,标准输出
关闭(1);
如果(穿心莲(文件路径,O_RDWR)== -1)
{
出口(1);
}
和标准输入
关闭(0);
如果(开放(文件路径,O_RDONLY)== -1)
{
出口(1);
}
您应该使用DUP()和dup2()克隆的文件描述符。
INT stdin_copy = DUP(0);
INT stdout_copy = DUP(1);
关闭(0);
关闭(1);INT文件1 =开(...);
INT文件2 =开(...);<做你的工作。 file1和file2的必须是0和1,因为开放总是返回最低未使用FD>关闭(文件1);
关闭(文件2);
dup2(stdin_copy,0);
dup2(stdout_copy,1);
关闭(stdin_copy);
关闭(stdout_copy);
然而,有可能要小心(从人DUP)一个小细节:
这两个描述符不共享文件描述符标志(该
近距离上execflag)。在近on-exec标志(FD_CLOEXEC;请参阅fcntl(2))
对于重复的描述是关闭的。
块引用>如果这是一个问题,您可能需要恢复近on-exec标志,可能使用dup3()而不是dup2(),以避免竞争条件。
此外,要知道,如果你的程序是多线程的,其他线程可能会意外地写入/读给你重新映射标准输入/输出。
I'm writing a function, which, given an argument, will either redirect the stdout to a file or read the stdin from a file. To do this I close the file descriptor associated with the stdout or stdin, so that when I open the file it opens under the descriptor that I just closed. This works, but the problem is that once this is done, I need to restore the stdout and stdin to what they should really be.
What I can do for stdout is open("/dev/tty",O_WRONLY); But I'm not sure why this works, and more importantly I don't know of an equivalent statement for stdin.
So I have, for stdout
close(1); if (creat(filePath, O_RDWR) == -1) { exit(1); }
and for stdin
close(0); if (open(filePath, O_RDONLY) == -1) { exit(1); }
解决方案You should use dup() and dup2() to clone a file descriptor.
int stdin_copy = dup(0); int stdout_copy = dup(1); close(0); close(1); int file1 = open(...); int file2 = open(...); < do your work. file1 and file2 must be 0 and 1, because open always returns lowest unused fd > close(file1); close(file2); dup2(stdin_copy, 0); dup2(stdout_copy, 1); close(stdin_copy); close(stdout_copy);
However, there's a minor detail you might want to be careful with (from man dup):
The two descriptors do not share file descriptor flags (the close-on-execflag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.
If this is a problem, you might have to restore the close-on-exec flag, possibly using dup3() instead of dup2() to avoid race conditions.
Also, be aware that if your program is multi-threaded, other threads may accidentally write/read to your remapped stdin/stdout.
这篇关于关闭它们后重新打开stdout和标准输入文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!