|
26 | 26 | //! that they don't outlive the resource they point to. These are safe to
|
27 | 27 | //! use. `BorrowedFd` values may be used in APIs which provide safe access to
|
28 | 28 | //! any system call except for:
|
| 29 | +//! |
29 | 30 | //! - `close`, because that would end the dynamic lifetime of the resource
|
30 | 31 | //! without ending the lifetime of the file descriptor.
|
| 32 | +//! |
31 | 33 | //! - `dup2`/`dup3`, in the second argument, because this argument is
|
32 | 34 | //! closed and assigned a new resource, which may break the assumptions
|
33 | 35 | //! other code using that file descriptor.
|
34 |
| -//! This list doesn't include `mmap`, since `mmap` does do a proper borrow of |
35 |
| -//! its file descriptor argument. That said, `mmap` is unsafe for other |
36 |
| -//! reasons: it operates on raw pointers, and it can have undefined behavior if |
37 |
| -//! the underlying storage is mutated. Mutations may come from other processes, |
38 |
| -//! or from the same process if the API provides `BorrowedFd` access, since as |
39 |
| -//! mentioned earlier, `BorrowedFd` values may be used in APIs which provide |
40 |
| -//! safe access to any system call. Consequently, code using `mmap` and |
41 |
| -//! presenting a safe API must take full responsibility for ensuring that safe |
42 |
| -//! Rust code cannot evoke undefined behavior through it. |
| 36 | +//! |
| 37 | +//! `BorrowedFd` values may be used in APIs which provide safe access to `dup` |
| 38 | +//! system calls, so types implementing `AsFd` or `From<OwnedFd>` should not |
| 39 | +//! assume they always have exclusive access to the underlying file |
| 40 | +//! description. |
| 41 | +//! |
| 42 | +//! `BorrowedFd` values may also be used with `mmap`, since `mmap` uses the |
| 43 | +//! provided file descriptor in a manner similar to `dup` and does not require |
| 44 | +//! the `BorrowedFd` passed to it to live for the lifetime of the resulting |
| 45 | +//! mapping. That said, `mmap` is unsafe for other reasons: it operates on raw |
| 46 | +//! pointers, and it can have undefined behavior if the underlying storage is |
| 47 | +//! mutated. Mutations may come from other processes, or from the same process |
| 48 | +//! if the API provides `BorrowedFd` access, since as mentioned earlier, |
| 49 | +//! `BorrowedFd` values may be used in APIs which provide safe access to any |
| 50 | +//! system call. Consequently, code using `mmap` and presenting a safe API must |
| 51 | +//! take full responsibility for ensuring that safe Rust code cannot evoke |
| 52 | +//! undefined behavior through it. |
43 | 53 | //!
|
44 | 54 | //! Like boxes, `OwnedFd` values conceptually own the resource they point to,
|
45 | 55 | //! and free (close) it when they are dropped.
|
|
0 commit comments