- 论坛徽章:
- 0
|
Linux : openat() support in Linux
January 20, 2006 - 4:23amSubmitted by
Kedar Sovani
-->
Submitted by
Kedar Sovani
on January 20, 2006 - 4:23am.
![]()
Recently,
patches
have been included in the upcoming Linux kernel, to add support to the openat() class of system calls. In the patch log, author, Ulrich Drepper, states "[A] total 13 new system calls which take a file descriptor/filename pair instead of a single file name [have been added]". These system calls interpret the filename relative to the file descriptor passed.
An example should help clarify the point :
int open(const char *path, int oflag, /* mode_t mode */...);
int openat(int dirfd, const char *relative_path, int oflag, /* mode_t mode */...);
This is of great help for implementing virtual per-thread current working directory. In the abscence of this, approaches like cwd_save/restore (which affects the entire process) or "/proc/self/fd/dirfd/relative_path" were being used. Apart from being faster than the older approaches, the new system calls also allow for race-free filesystem traversals. The new system calls include, openat, mkdirat, mknodat, fchownat, futimesat, newfstatat, unlinkat, renameat, linkat, symlinkat, readlinkat, fchmodat, faccessat.
These system calls
exist
in Solaris for quite a while now. The new Linux openat-variant system calls do not support the extended attribute feature, as is found with the Solaris openat-variants.
理解:
引用openat是方便一个进程内的各线程可拥有不同的当前目录,传统的chdir会影响整个进程,而使用openat只需要每个线程在初始化时打开一个目录(调用open),然后就可以以openat在“当前目录”操作文件了,如:
int dirfd = open("/tmp"); // 相当于chdir到“/tmp”
int filefd = openat(dirfd, "myfile"); // 在/tmp目录下打开“myfile”文件
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45170/showart_474275.html |
|