-
Notifications
You must be signed in to change notification settings - Fork 1.2k
update linux-syscall-5 #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
linux-syscall-5 翻译完成
SysCall/linux-syscall-5.md
Outdated
|
||
I don't know how about you, but it is interesting to me not only how an operating system works, but how do my software interacts with it. As you may know, our programs interacts with the kernel through the special mechanism which is called [system call](https://en.wikipedia.org/wiki/System_call). So, I've decided to write series of parts which will describe implementation and behavior of system calls which we are using every day like `read`, `write`, `open`, `close`, `dup` and etc. | ||
你觉得怎么样,我认为这非常有趣耶,操作系统如何工作,我们的软件如何与(系统)交互呢。你或许了解,我们的程序通过特定的机制和内核进行交互,这个机制就是[系统调用](https://en.wikipedia.org/wiki/System_call)。因此,我决定去写一些系统调用的实现及其行为,比如我们每天会用到的 `read`,`write`,`open`,`close`,`dup` 等等。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- “你觉得怎么样,我认为这非常有趣耶,操作系统如何工作,我们的软件如何与(系统)交互呢。” -> “我不知道你觉得怎么样,但是我认为这些非常有趣,不仅包括操作系统如何工作,也包括我的软件如何与系统交互。”
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第一行比较偏口语化,所以就意译了😀
SysCall/linux-syscall-5.md
Outdated
|
||
```C | ||
op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; | ||
``` | ||
|
||
or just to `LOOKUP_OPEN` intention. Additionally we set `LOOKUP_CREATE` intention if we want to create new file and to be sure that a file didn't exist before with `O_EXCL` flag: | ||
或仅仅为了 `LOOK_OPEN` 目的。如果我们想要新建文件,我们可以设置 `LOOKUP_CREATE`,并且使用 `O_EXEC` 标志来确认文件之前不存在: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOOK_OPEN
-> "LOOKUP_OPEN"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
谢谢,这里对接上一段,我重新看了下,这样翻不正确的,改为
"否则 open_flags
会被设置为 LOOKUP_OPEN
"
更贴切,已 fix up
SysCall/linux-syscall-5.md
Outdated
|
||
The next field of the `open_flags` structure is - `intent`. It allows us to know about our intention or in other words what do we really want to do with file, open it, create, rename it or something else. So we set it to zero if our flags contains the `O_PATH` flag as we can't do anything related to a file content with this flag: | ||
`open_flags` 中接下来的字段是 - `intent`。它允许我们知道我们的目的,换句话说就是我们真正想对文件做什么,打开,新建,重命名等等操作。如果我们的 flags 参数包含这个 `O_PATH` 标志,`open_flags` 会被设置为 0 : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- "如果我们的 flags 参数包含这个
O_PATH
标志,open_flags
会被设置为 0 :" -> "如果我们的 flags 参数包含这个O_PATH
标志,即我们不能对文件内容做任何事情,open_flags
会被设置为 0 :"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我漏译了😂,谢谢,已 fix up
SysCall/linux-syscall-5.md
Outdated
@@ -296,20 +301,20 @@ op->lookup_flags = lookup_flags; | |||
return 0; | |||
``` | |||
|
|||
We fill it with `LOOKUP_DIRECTORY` if we want to open a directory and `LOOKUP_FOLLOW` if we don't want to follow (open) [symlink](https://en.wikipedia.org/wiki/Symbolic_link). That's all. It is the end of the `build_open_flags` function. The `open_flags` structure is filled with modes and flags for a file opening and we can return back to the `do_sys_open`. | |||
如果我们想要打开一个目录并且遍历但不想使用[软链接](https://en.wikipedia.org/wiki/Symbolic_link),我们可以使用 `LOOKUP_DIRECTORY` 。这就是 `build_open_flags` 函数的全部内容了。`open_flags` 结构体也用各种打开文件的 modes 和 flags 填完了。我们可以返回到 `do_sys_open`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已改为:
“如果我们想要打开一个目录,我们可以使用 LOOKUP_DIRECTORY
;如果想要遍历但不想使用软链接,可以使用 LOOKUP_FOLLOW
。”
谢谢
SysCall/linux-syscall-5.md
Outdated
|
||
Now let's take a short look at the implementation of the `do_filp_open` function. This function is defined in the [fs/namei.c](https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/fs/namei.c) linux kernel source code file and starts from initialization of the `nameidata` structure. This structure will provide a link to a file [inode](https://en.wikipedia.org/wiki/Inode). Actually this is one of the main point of the `do_filp_open` function to acquire an `inode` by the filename given to `open` system call. After the `nameidata` structure will be initialized, the `path_openat` function will be called: | ||
现在让我们来简短看下 `do_filp_open()` 函数的实现。这个函数定义在 [fs/namei.c](https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/fs/namei.c) Linux 内核源码中,函数开始就初始化了 `nameidata` 结构体。这个结构体提供了一个链接到文件 [inode](https://en.wikipedia.org/wiki/Inode)。事实上,这就是 `do_filp_open()` 函数指针,这个函数获取一个传递给 open 系统调用的文件名 `inode` ,在 `nameidata` 结构体被初始化后,`path_openat` 函数会被调用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- “ to acquire an
inode
by the filename given toopen
system call” -> "通过传递到open
系统调用的的文件名获取inode
"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
谢谢,这样确实更易理解,已 fix up
|
||
I have decided to start from the description of the [open](http://man7.org/linux/man-pages/man2/open.2.html) system call. if you have written at least one `C` program, you should know that before we are able to read/write or execute other manipulations with a file we need to open it with the `open` function: | ||
我决定从 `open` 系统调用开始。如果你对 C 程序有了解,你应该知道在我们能对一个文件进行读写或执行其他操作前,我们需要使用 `open` 函数打开这个文件: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我觉得不应该删除open的链接
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😀,已经添加了,谢谢
SysCall/linux-syscall-5.md
Outdated
@@ -96,19 +105,19 @@ and | |||
> in length. When compiling with _FILE_OFFSET_BITS == 64 this type | |||
> is available under the name off_t. | |||
|
|||
So it is not hard to guess that the `off_t`, `off64_t` and `O_LARGEFILE` are about a file size. In the case of the Linux kernel, the `O_LARGEFILE` is used to disallow opening large files on 32bit systems if the caller didn't specify `O_LARGEFILE` flag during opening of a file. On 64bit systems we force on this flag in open system call. And the `force_o_largefile` macro from the [include/linux/fcntl.h](https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/include/linux/fcntl.h#L7) linux kernel header file confirms this: | |||
因此不难猜到 `off_t`,`off64_t` 和 `O_LARGEFILE` 是关于文件大小的。就 Linux 内核而言,在32 位系统中打开大文件时如果调用者没有加上 `O_LARGEFILE` 标志,打开大文件会操作会被禁止。在 64 位系统上,我们在 `open` 系统调用时强制加上了这个标志。[include/linux/fcntl.h](https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/include/linux/fcntl.h#L7) linux 内核头文件中详述了 `force_o_largefile` 宏: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
打开大文件会操作会被禁止->打开大文件的操作就会被禁止。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂,谢谢指正,已 fix up
|
||
The last argument - `op` is represented with the `open_flags` structure: | ||
最后的参数 - `op` 在 `open_flags` 结构体中表示如下: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最后的参数 -> 最后一个参数
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
谢谢,已 fix up
* to perform operations that act purely at the file descriptor level. | ||
|
||
So, in this case the file itself is not opened, but operations like `dup`, `fcntl` and other can be used. So, if all file content related operations like `read`, `write` and other are permitted, only `O_DIRECTORY | O_NOFOLLOW | O_PATH` flags can be used. We have finished with flags for this moment in the `build_open_flags` for this moment and we may fill our `open_flags->open_flag` with them: | ||
因此,在这种情况下文件自身是没有被打开的,但是像 `dup`,`fcntl` 等其他操作能使用。因此,如果所有的文件相关的操作,像 `read`,`write` 和其他操作是允许的,仅 `O_DIRECTORY | O_NOFOLLOW | O_PATH` 标志能被使用。现在我们已经在 `build_open_flags` 函数中分析完成了这些标志,我们可以使用下列代码填充我们的 `open_flags->open_flag` : | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“So“ ,感觉是原文作者的一个口头语,没必要全部翻译出来(个人感觉)。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里重新翻译了:
“在这种情况下文件自身是没有被打开的,但是像 dup
, fcntl
等操作能被使用。因此如果想使用所有与文件内容相关的操作,像 read
, write
等,就(必须)使用 O_DIRECTORY | O_NOFOLLOW | O_PATH
标志。”
谢谢😀
@xindoo 谢谢指出PR中存在的问题。 |
linux-syscall-5 翻译完成