open函数

本文介绍了解决Linux环境下wpa_supplicant启动时可能遇到的文件描述符冲突问题的方法,通过确保fd0, fd1, fd2不被用于其他socket,以保留对stdin/stdout/stderr的访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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打开,以达到保留目的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值