如何使用命令 SIOCGIFFLAGS 和 SIOCSIFFLAGS 以原子方式调用 ioctl

发布于 2024-12-12 03:40:15 字数 412 浏览 0 评论 0原文

有没有办法以原子方式使用命令 SIOCGIFFLAGSSIOCSIFFLAGS 调用 ioctl(该问题对所有系统调用也有效)?例如,如果我将 IFF_PROMISC 标志添加到接口:

...
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "eth0");

if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0)
...

ifr.ifr_flags |= IFF_PROMISC;
if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
...

如何确保这两个调用以原子方式进行?

谢谢大家!

Is there a way to call ioctl (the question is also valid for all sys calls) with commands SIOCGIFFLAGS and SIOCSIFFLAGS in an atomic manner? For example if i would add the IFF_PROMISC flag to an interface:

...
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "eth0");

if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0)
...

ifr.ifr_flags |= IFF_PROMISC;
if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
...

how can i ensure that these two calls are made atomically?

Thank you all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

树深时见影 2024-12-19 03:40:15

简单的答案是你不能 - 没有办法保证另一个进程没有更改这些调用之间的标志。

The simple answer is that you can't - there is no way to guarantee that another process hasn't changed the flags in between those calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文