GNU/Linux - Open函数使用的O_CLOEXEC flag

在 Linux 中,“O_CLOEXEC ”标志与 “open ”系统调用一起使用,用于指定在使用 “exec ”系列函数(如 “execve”、“execl ”等)执行新程序时,“open ”返回的文件描述符应自动关闭。

In Linux, the `O_CLOEXEC` flag is used with the `open` system call to specify that the file descriptor returned by `open` should be automatically closed when executing a new program using one of the `exec` family of functions (such as `execve`, `execl`, etc.).

How it works:

- 文件描述符 程序打开文件时,会获得一个文件描述符 (FD),这是一个代表打开文件的小整数。

- 文件描述符和 `exec`: 默认情况下,当进程调用 `exec` 函数时,进程中打开的文件描述符在新程序中仍保持打开状态。这可能是不可取的,尤其是出于安全原因,因为它可能会无意中将文件描述符泄露给子进程。

- O_CLOEXEC` 标志: 在使用 `open` 系统调用时使用 `O_CLOEXEC` 标志,会为文件描述符设置执行时关闭 (FD_CLOEXEC) 标志。这意味着在执行新程序时,文件描述符将自动关闭。

- File Descriptors: When a program opens a file, it gets a file descriptor (FD), which is a small integer representing the open file.

- File Descriptors and `exec`: By default, when a process calls an `exec` function, the file descriptors that were open in the process remain open in the new program. This can be undesirable, especially for security reasons, as it may inadvertently leak file descriptors to child processes.

- `O_CLOEXEC` Flag: When you use the `O_CLOEXEC` flag with the `open` system call, it sets the close-on-exec (FD_CLOEXEC) flag for the file descriptor. This means that the file descriptor will be automatically closed when a new program is executed.

Example:

int fd = open("example.txt", O_RDONLY | O_CLOEXEC);

if (fd == -1) {

    // handle error

}

在此示例中,如果进程随后调用 `exec` 函数,文件描述符 `fd` 将自动关闭。

In this example, the file descriptor `fd` will be automatically closed if the process later calls an `exec` function.

Why use `O_CLOEXEC`?

- 安全性 防止文件描述符被新程序无意继承,降低敏感信息泄露的风险。

- 资源管理: 确保资源在过渡到新程序时被正确释放。

在需要确保文件描述符不会泄漏到使用 `exec` 创建的子进程中时,使用 `O_CLOEXEC` 是一种常见的最佳做法。

- Security: Prevents file descriptors from being unintentionally inherited by new programs, reducing the risk of leaking sensitive information.

- Resource Management: Ensures that resources are properly released when transitioning to a new program.

Using `O_CLOEXEC` is a common best practice in scenarios where you need to ensure that file descriptors do not leak into child processes created with `exec`.

Check for working CXX compiler: /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++ -- broken CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCXXCompiler.cmake:53 (message): The C++ compiler "/media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++" is not able to compile a simple test program. It fails with the following output: Change Dir: /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/Build/rpclib-v2.2.1_c5-c10-libcxx-build/CMakeFiles/CMakeTmp Run Build Command(s):/usr/bin/ninja cmTC_3c146 && [1/2] Building CXX object CMakeFiles/cmTC_3c146.dir/testCXXCompiler.cxx.o clang++: warning: -Wl,-L/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu: 'linker' input unused [-Wunused-command-line-argument] [2/2] Linking CXX executable cmTC_3c146 FAILED: cmTC_3c146 : && /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++ -fPIC -std=c++14 -stdlib=libc++ -I/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1 -Wl,-L/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS --sysroot=/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/ CMakeFiles/cmTC_3c146.dir/testCXXCompiler.cxx.o -o cmTC_3c146 && : /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find crt1.o: No such file or directory /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find crti.o: No such file or directory /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find -lc++ /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find -lm /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find -lgcc_s /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find -lc /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find -lgcc_s /media/wangbaihui/1ecf654b-afad-4dab-af7b-e34b00dda87a/carla/UnrealEngine-4.26/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find crtn.o: No such file or directory clang++: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值