static void wpa_supplicant_fd_workaround(void)
{
#ifdef __linux__
int s, i;
/* When started from pcmcia-cs scripts, wpa_supplicant might start with
* fd 0, 1, and 2 closed. This will cause some issues because many
* places in wpa_supplicant are still printing out to stdout. As a
* workaround, make sure that fd's 0, 1, and 2 are not used for other
* sockets. */
for (i = 0; i < 3; i++) {
s = open("/dev/null", O_RDWR);
if (s > 2) {
close(s);
break;
}
}
#endif /* __linux__ */
}
代码如上。
写入/dev/null的东西会被系统丢掉
就像注释写的那样,对stdin/stdout/stderr进行保留。
代码中利用while先把文件描述符0,1,2(fd0,fd1,fd2)分配出去,
以后再分配的时候就不会将stdin/stdout/stderr打开,以达到保留目的。