Unix/Linux系統調用
accept()函數 Unix/Linux
access()函數 Unix/Linux
acct()函數 Unix/Linux
add_key()函數 Unix/Linux
adjtimex()函數 Unix/Linux
afs_syscall()函數 Unix/Linux
alarm()函數 Unix/Linux
alloc_hugepages()函數 Unix/Linux
arch_prctl()函數 Unix/Linux
bdflush()函數 Unix/Linux
bind()函數 Unix/Linux
break未實現 Unix/Linux
brk()函數 Unix/Linux
cacheflush()函數 Unix/Linux
chdir()函數 Unix/Linux
chmod()函數 Unix/Linux
chown()函數 Unix/Linux
chroot()函數 Unix/Linux
clone()函數 Unix/Linux
close()函數 Unix/Linux
connect()函數 Unix/Linux
create_module()函數 Unix/Linux
open()函數 Unix/Linux
dup2()函數 Unix/Linux
dup()函數 Unix/Linux
epoll_create()函數 Unix/Linux
epoll_ctl()函數 Unix/Linux
epoll_wait()函數 Unix/Linux
execve()函數 Unix/Linux
exit_group函數 Unix/Linux
_exit()函數 Unix/Linux
exit()函數 Unix/Linux
faccessat()函數 Unix/Linux
fattach()函數 Unix/Linux
fchdir()函數 Unix/Linux
fchmodat()函數 Unix/Linux
fchmod()函數 Unix/Linux
fchownat()函數 Unix/Linux
fchown()函數 Unix/Linux
fcntl()函數 Unix/Linux
fdatasync()函數 Unix/Linux
fdetach()函數 Unix/Linux
flock()函數 Unix/Linux
fork()函數 Unix/Linux
alloc_hugepages()函數 Unix/Linux
fstatat()函數 Unix/Linux
statfs()函數 Unix/Linux
stat()函數 Unix/Linux
statvfs()函數 Unix/Linux
fsync()函數 Unix/Linux
truncate()函數 Unix/Linux
futex()函數 Unix/Linux
futimesat()函數 Unix/Linux
getcontext()函數 Unix/Linux
getcwd()函數 Unix/Linux
getdents()函數 Unix/Linux
getdomainname()函數 Unix/Linux
getdtablesize()函數 Unix/Linux
getgid()函數 Unix/Linux
getuid()函數 Unix/Linux
getgroups()函數 Unix/Linux
getgroups()函數 Unix/Linux
gethostname()函數 Unix/Linux
getitimer()函數 Unix/Linux
get_kernel_syms()函數 Unix/Linux
unimplemented()函數 Unix/Linux
getpagesize()函數 Unix/Linux
getpeername()函數 Unix/Linux
setpgid()函數 Unix/Linux
getpgrp()函數 Unix/Linux
getpid()函數 Unix/Linux
getpmsg()函數 Unix/Linux
getppid()函數 Unix/Linux
getpriority()函數 Unix/Linux
getresuid()函數 Unix/Linux
getrlimit()函數 Unix/Linux
get_robust_list()函數 Unix/Linux
getrusage()函數 Unix/Linux
getsid()函數 Unix/Linux
getsockname()函數 Unix/Linux
getsockopt()函數 Unix/Linux
get_thread_area()函數 Unix/Linux
gettid()函數 Unix/Linux
gettimeofday()函數 Unix/Linux
getuid()函數 Unix/Linux
getunwind()函數 Unix/Linux
gtty()函數 Unix/Linux
idle()函數 Unix/Linux
outb()函數 Unix/Linux
inb_p()函數 Unix/Linux
inl()函數 Unix/Linux
inl_p()函數 Unix/Linux
inotify_add_watch()函數 Unix/Linux
inotify_init()函數 Unix/Linux
inotify_rm_watch()函數 Unix/Linux
outb()函數 Unix/Linux
insl()函數 Unix/Linux
insw()函數 Unix/Linux
intro()函數 Unix/Linux
inw()函數 Unix/Linux
inw_p()函數 Unix/Linux
io_cancel()函數 Unix/Linux
ioctl()函數 Unix/Linux
ioctl_list()函數 Unix/Linux
io_destroy()函數 Unix/Linux
io_getevents()函數 Unix/Linux
ioperm()函數 Unix/Linux
iopl()函數 Unix/Linux
ioprio_set()函數 Unix/Linux
io_setup()函數 Unix/Linux
io_submit()函數 Unix/Linux
ipc()函數 Unix/Linux
isastream()函數 Unix/Linux
kexec_load()函數 Unix/Linux
keyctl()函數 Unix/Linux
kill()函數 Unix/Linux
killpg()函數 Unix/Linux
lchown()函數 Unix/Linux
linkat()函數 Unix/Linux
link()函數 Unix/Linux
listen()函數 Unix/Linux
_llseek()函數 Unix/Linux
llseek()函數 Unix/Linux
lock()函數 Unix/Linux
lookup_dcookie()函數 Unix/Linux
lseek()函數 Unix/Linux
lstat()函數 Unix/Linux
madvise()函數 Unix/Linux
mincore()函數 Unix/Linux
mkdirat()函數 Unix/Linux
mkdir()函數 Unix/Linux
mknod()函數 Unix/Linux
mlockall()函數 Unix/Linux
mlock()函數 Unix/Linux
mmap2()函數 Unix/Linux
mmap()函數 Unix/Linux
modify_ldt()函數 Unix/Linux
mount()函數 Unix/Linux
move_pages()函數 Unix/Linux
mprotect()函數 Unix/Linux
mpx()函數 Unix/Linux
mq_getsetattr()函數 Unix/Linux
mremap()函數 Unix/Linux
msgctl()函數 Unix/Linux
msgget()函數 Unix/Linux
msgop()函數 Unix/Linux
msgsnd()函數 Unix/Linux
msync()函數 Unix/Linux
multiplexer()函數 Unix/Linux
munlockall()函數 Unix/Linux
munlock()函數 Unix/Linux
munmap()函數 Unix/Linux
nanosleep()函數 Unix/Linux
_newselect()函數 Unix/Linux
nfsservctl()函數 Unix/Linux
nice()函數 Unix/Linux
obsolete()函數 Unix/Linux
oldfstat()函數 Unix/Linux
oldlstat()函數 Unix/Linux
oldolduname()函數 Unix/Linux
oldstat()函數 Unix/Linux
olduname()函數 Unix/Linux
openat()函數 Unix/Linux
open()函數 Unix/Linux
outb()函數 Unix/Linux
outb_p()函數 Unix/Linux
outsb()函數 Unix/Linux
outsl()函數 Unix/Linux
outsw()函數 Unix/Linux
outw()函數 Unix/Linux
outw_p()函數 Unix/Linux
path_resolution()函數 Unix/Linux
pause()函數 Unix/Linux
perfmonctl()函數 Unix/Linux
personality()函數 Unix/Linux
pipe()函數 Unix/Linux
pivot_root()函數 Unix/Linux
poll()函數 Unix/Linux
posix_fadvise()函數 Unix/Linux
ppoll()函數 Unix/Linux
prctl()函數 Unix/Linux
pread()函數 Unix/Linux
prof()函數 Unix/Linux
pselect()函數 Unix/Linux
ptrace()函數 Unix/Linux
putmsg()函數 Unix/Linux
putpmsg()函數 Unix/Linux
pwrite()函數 Unix/Linux
query_module()函數 Unix/Linux
quotactl()函數 Unix/Linux
readahead()函數 Unix/Linux
readdir()函數 Unix/Linux
read()函數 Unix/Linux
readlinkat()函數 Unix/Linux
readlink()函數 Unix/Linux
readv()函數 Unix/Linux
reboot()函數 Unix/Linux
recvfrom()函數 Unix/Linux
recv()函數 Unix/Linux
recvmsg()函數 Unix/Linux
remap_file_pages()函數 Unix/Linux
renameat()函數 Unix/Linux
rename()函數 Unix/Linux
request_key()函數 Unix/Linux
rmdir()函數 Unix/Linux
sbrk()函數 Unix/Linux
sched_setaffinity()函數 Unix/Linux
sched_getparam()函數 Unix/Linux
sched_get_priority_max()函數 Unix/Linux
sched_get_priority_min()函數 Unix/Linux
sched_setscheduler()函數 Unix/Linux
sched_rr_get_interval()函數 Unix/Linux
sched_setparam()函數 Unix/Linux
sched_yield()函數 Unix/Linux
security()函數 Unix/Linux
select()函數 Unix/Linux
select_tut()函數 Unix/Linux
semctl()函數 Unix/Linux

path_resolution()函數 Unix/Linux

Unix / Linux路徑解析 - 查找一個文件名所指的文件

描述

一些Unix/ Linux的系統調用作爲參數的一個或多個文件名。文件名(或路徑)的解析方式如下。

Step 1: 開始解析過程

If the pathname starts with the ’/’ character, the starting lookup directory is the root directory of the current process. (A process inherits its root directory from its parent. Usually this will be the root directory of the file hierarchy. A process may get a different root directory by use of the  chroot (2) system call. A process may get an entirely private namespace in case it — or one of its ancestors — was started by an invocation of the clone (2) system call that had the CLONE_NEWNS flag set.) This handles the ’/’ part of the pathname.

If the pathname does not start with the ’/’ character, the starting lookup directory of the resolution process is the current working directory of the process. (This is also inherited from the parent. It can be changed by use of the chdir(2) system call.)

Pathnames starting with a ’/’ character are called absolute pathnames. Pathnames not starting with a ’/’ are called relative pathnames.

Step 2: 沿着路徑走

Set the current lookup directory to the starting lookup directory. Now, for each non-final component of the pathname, where a component is a substring delimited by ’/’ characters, this component is looked up in the current lookup directory.

If the process does not have search permission on the current lookup directory, an EACCES error is returned ("Permission denied").

If the component is not found, an ENOENT error is returned ("No such file or directory").

If the component is found, but is neither a directory nor a symbolic link, an ENOTDIR error is returned ("Not a directory").

If the component is found and is a directory, we set the current lookup directory to that directory, and go to the next component.

If the component is found and is a symbolic link (symlink), we first resolve this symbolic link (with the current lookup directory as starting lookup directory). Upon error, that error is returned. If the result is not a directory, an ENOTDIR error is returned. If the resolution of the symlink is successful and returns a directory, we set the current lookup directory to that directory, and go to the next component. Note that the resolution process here involves recursion. In order to protect the kernel against stack overflow, and also to protect against denial of service, there are limits on the maximum recursion depth, and on the maximum number of symlinks followed. An ELOOP error is returned when the maximum is exceeded ("Too many levels of symbolic links").

Step 3: 找到最後一項

The lookup of the final component of the pathname goes just like that of all other components, as described in the previous step, with two differences: (i) the final component need not be a directory (at least as far as the path resolution process is concerned — it may have to be a directory, or a non-directory, because of the requirements of the specific system call), and (ii) it is not necessarily an error if the component is not found — maybe we are just creating it. The details on the treatment of the final entry are described in the manual pages of the specific system calls.

. and ..

By convention, every directory has the entries "." and "..", which refer to the directory itself and to its parent directory, respectively.

The path resolution process will assume that these entries have their conventional meanings, regardless of whether they are actually present in the physical filesystem.

One cannot walk down past the root: "/.." is the same as "/".

掛載點

After a "mount dev path" command, the pathname "path" refers to the root of the filesystem hierarchy on the device "dev", and no longer to whatever it referred to earlier.

One can walk out of a mounted filesystem: "path/.." refers to the parent directory of "path", outside of the filesystem hierarchy on "dev".

尾隨斜線

If a pathname ends in a ’/’, that forces resolution of the preceding component as in Step 2: it has to exist and resolve to a directory. Otherwise a trailing ’/’ is ignored. (Or, equivalently, a pathname with a trailing ’/’ is equivalent to the pathname obtained by appending ’.’ to it.)

最後的符號鏈接

If the last component of a pathname is a symbolic link, then it depends on the system call whether the file referred to will be the symbolic link or the result of path resolution on its contents. For example, the system call  lstat (2) will operate on the symlink, while stat (2) operates on the file pointed to by the symlink.

長度限制

There is a maximum length for pathnames. If the pathname (or some intermediate pathname obtained while resolving symbolic links) is too long, an ENAMETOOLONG error is returned ("File name too long").

空路徑名

In the original Unix, the empty pathname referred to the current directory. Nowadays POSIX decrees that an empty pathname must not be resolved successfully. Linux returns ENOENT in this case.

權限

The permission bits of a file consist of three groups of three bits, cf.  chmod (1) and stat (2). The first group of three is used when the effective user ID of the current process equals the owner ID of the file. The second group of three is used when the group ID of the file either equals the effective group ID of the current process, or is one of the supplementary group IDs of the current process (as set by  setgroups (2)). When neither holds, the third group is used.

Of the three bits used, the first bit determines read permission, the second write permission, and the last execute permission in case of ordinary files, or search permission in case of directories.

Linux uses the fsuid instead of the effective user ID in permission checks. Ordinarily the fsuid will equal the effective user ID, but the fsuid can be changed by the system callsetfsuid(2).

(Here "fsuid" stands for something like "file system user ID". The concept was required for the implementation of a user space NFS server at a time when processes could send a signal to a process with the same effective user ID. It is obsolete now. Nobody should use setfsuid(2).)

Similarly, Linux uses the fsgid ("file system group ID") instead of the effective group ID. See setfsgid(2).

繞過權限檢查:超級用戶和功能

On a traditional Unix system, the superuser ( root , user ID 0) is all-powerful, and bypasses all permissions restrictions when accessing files.

On Linux, superuser privileges are divided into capabilities (see capabilities(7)). Two capabilities are relevant for file permissions checks: CAP_DAC_OVERRIDE and CAP_DAC_READ_SEARCH. (A process has these capabilities if its fsuid is 0.)

The CAP_DAC_OVERRIDE capability overrides all permission checking, but only grants execute permission when at least one of the file’s three execute permission bits is set.

The CAP_DAC_READ_SEARCH capability grants read and search permission on directories, and read permission on ordinary files.

另請參閱