ืคืขื ืืืช, ืืจืืืื, ืฉืืื ืืืชื ืื ืืืืชื ืขืืฉืื ืื ืชืืฆืื ืฉืืจืืช ืืงืืืงื ืืืื ืืขืืืื ืฉื ืืืจ ืืืงืื ืืืืกืง?
ืืืืื, ืขื ืืชื ืฉืืจืื ืื ืืืงืื ืืื ืขืืฉื ืืื ืืคืฉืจ, ืื ืงื ืืช ืืืงืื.
ืืื ืืืจืืืื ืฉืื, ืื ืื ืืื ืืงืื ืคื ืื ืืืืืฆื, ืืื ืืชื ืื ืื ืจืืื ืืช ืืงืืฆืื ืฉืืชืคืกื ืืช ืื ืืืงืื?
ืขื ืื ืืืจืชื ืฉืชืืื ืืคืฉืจ ืืืกืชืื ืขื ืืชืืจื ืงืืฆืื ืคืชืืืื, ืืืฉื ืขื ืืคืงืืื lsof ืืืืืื ืืืื ืืคืืืงืฆืื ืืงืื ืืช ืื ืืฉืื ืืคื ืื, ืืื ืืคืฉืจ ืืคืขืื ืืืชืื ืื ืกืืืืช, ืชืืื ืื ืืฉ ืฆืืจื ืื ืชืื ืื .
ืืืจืืืื ืงืืข ืืืชื ืืืืื ืืืืจืื ื ืืืืกืืฃ ืืฉืืืชื: "ื ื ืื ืฉืื ืื ื ืื ืฆืจืืืื ืืช ืื ืชืื ืื, ืื ืจืง ืืืื ื ืืคืื ืืืืื, ืืื ืืืคืืืงืฆืื ืืืฉืืชืช ืื ืืื ืื ืืืืื ืืืชืื ืืช ืืืืืื"?
"ืืกืืจ," ืขื ืืชื, "ื ืืื ืืืืืช ืืช ื ืืคืื ืืืืืื ืืชืฆืืจืช ืืืคืืืงืฆืื ืืืืคืขืื ืืืชื ืืืืฉ."
ืืืจืืืื ืืชื ืื: "ืื, ืื ืื ื ืื ืืืืืื ืืืคืขืื ืืืืฉ ืืช ืืืคืืืงืฆืื, ืขืืืื ืืฉ ืื ื ื ืชืื ืื ืืฉืืืื ืืืืืจืื, ืืืงืืืืช ืืฉืืืื ืืืืืจืื ืืฉืืจืืช ืขืฆืื, ืฉืื ืื ื ืื ืืืืืื ืืืืจืื ืืืชืืืจ ืืืืฉ".
"ืืกืืจ," ืืืจืชื, "ืื ืื ื ืืื ืืืคืขืื ืืืืฉ ืืช ืืืคืืืงืฆืื ืืื ืืืคืช ืื ื ืืื ืชืื ืื, ื ืืื ืคืฉืื ืื ืงืืช ืืช ืืงืืืฅ ืืคืชืื ืืื ืืจื ืืชืืจ ืืงืืืฅ, ืื ืื ืืื ื ื ืจืืืื ืืืชื ื-ls ืคืงืืื ืืืขืจืืช ืืงืืฆืื."
ืืืจืืืื ืืื ืืจืืฆื, ืืื ืื ื ืื.
ืืื ืืฉืืชื, ืืื ืืืื ืฉืืืืง ืืช ืืืืข ืฉืื ืื ืืืคืจ ืืขืืืง? ืืื ืื ืื ืื ืชืื ืื ืืื ืืืช ืืฉืืืื? ืื ืื ืื ืื ื ืื ืืืืืื ืืืคืขืื ืืืืฉ ืืช ืืชืืืื, ืืืืงืืื ืืชืืืื ืืื ืืืชื ืืืขืจืืช ืืงืืฆืื ืขื ืืืืฆื ืฉืืื ืื ืืงืื ืคื ืื? ืื ืื ืื ื ืืื ืืืื ืื ืจืง ืืช ืื ืชืื ืื ืฉืืืจ ื ืืชืื, ืืื ืื ืืช ืื ืชืื ืื ืฉืืชืืืื ืืื ืืืชื ืื ืื ืกื ืืืชืื?
ืืืืืง
ืืชืืืืช ืืงืจืืืจื ืฉืื, ื ืืกืืชื ืืืฆืืจ ืืคืืืงืฆืื ืงืื ื ืฉืฆืจืืื ืืืืกื ืืืืข ืขื ืืฉืชืืฉืื. ืืื ืืฉืืชื, ืืื ืื ื ืืืื ืืืชืืื ืืช ืืืฉืชืืฉ ืื ืชืื ืื ืฉืื. ืืืืืื, ืืฉ ืื ืืืื ืื ืืืื ืืืื ืืืืฅ', ืืืฉ ืื ืืื ื ืชืื ืื, ืืื ืืื ืืืชืืืื ืืืชื? ืื ื ืืืื ืืฆืืื ืืฉืืจืืช ืฉืืืื ืืฉื "ืืืืืง" ืฉืืื ืืืืชื ืืืืื. ืืื ืื ืื ืืื ืืฉื ื ืืช ืฉืื ืืืืงืื ืืืื ืืืคืื, ืืืฉื, ืืืืืื? ืื ืืชืืจืจ ืฉืืืืืื ืืืื ืืื ื ืืืื ืืื ืฉืื ื ืืืจ ืื ืืืื ืืื, ืืืืืืืง ืฉืื ื ืขืืืื ืืืื ืฉืืื ืืืืืื ืืื ืงืืื. ืืกืืก ืื ืชืื ืื ืขืืจ ืืคืชืืจ ืืช ืืืขืื ืืื, ืฉื ืชื ื ืืื ืืฉืชืืฉ ืืืื ืืืืืื (ID), ืื-Tuzik ืฉืื ืืื ืงืฉืืจ ืืืืื ืืื, ืฉืืืขืฉื ืืื ืจืง โโืืกืคืจ ืกืืืืจื. ืืคืืื, ืืืขืืื ืฉื ืืืืืืง ืืื ืขื ืืกืคืจ ืชืขืืืช ืืืืช 2, ืืืฉืื ืืกืืื ืืืื ืืื ืืชืขืืืช ืืืืช ืื, ืืื ืืืืื ืืคืื ืืืืชื ืชืขืืืช ืืืืช. ืืขืืืช ืืื ืืฉืืช ืืืืืื ืืขืื ืืืืื ื ืคืชืจื ืืืขืฉื.
ืืชืืจ ืงืืืฅ
ืืืขืื ืฉื ืงืืืฅ ืืชืืื ืืช ืฉืขืืืืื ืขื ืืงืืืฅ ืืื ืืื ืืขืจื ืืื ืืืื ืืืืื ืฉืื ื. ื ื ืื ืฉืคืชืืชื ืงืืืฅ ืืฉื ivan.txt ืืืชืืืชื ืืืชืื ืื ืืช ืืืืื tuzik, ืืื ืืฆืืืชื ืืืชืื ืจืง ืืช ืืืืช ืืจืืฉืื ื "t" ืืงืืืฅ, ืืืงืืืฅ ืืื ืฉืื ื ืขื ืืื ืืืฉืื, ืืืฉื, ื-olya.txt. ืืื ืืงืืืฅ ืืื ืืื ื ืขืืืื ืจืืฆื ืืืชืื ืื ืืช ืืืก ืฉืื. ืืื ืคืขื ืฉืืชื ืคืืชื ืงืืืฅ ืขื ืงืจืืืช ืืขืจืืช
ืืืื ืืงืก, ืกืคืจืืืช libc ืคืืชืืช 3 ืงืืฆื ืชืืืืจ ืขืืืจ ืื ืืืฉืื (ืชืืืื) ืืคืืขื, ืขื ืืืกืคืจืื 0,1,2. ืืืืข ื ืืกืฃ ืชืืื ืืืฆืื ืืงืืฉืืจืื
- ืืชืืจ ืืงืืืฅ 0 ื ืงืจื STDIN ืืืื ืืฉืืื ืืงืื ืืืคืืืงืฆืื.
- ืืชืืจ ืงืืืฅ 1 ื ืงืจื STDOUT ืืืฉืืฉ ืืืืฉืืื ืคืื ืืืื ืคืงืืืืช ืืืคืกื.
- ืืชืืจ ืงืืืฅ 2 ื ืงืจื STDERR ืืืฉืืฉ ืขื ืืื ืืืฉืืืื ืืคืื ืืืืขืืช ืฉืืืื.
ืื ืืชืืื ืืช ืฉืื ืชืคืชื ืงืืืฅ ืืืฉืื ืืงืจืืื ืื ืืชืืื, ืกืืืจ ืืื ืื ืฉืชืงืื ืืช ืืืืื ืืืื ืื ืืจืืฉืื ืืืื ืืืื ืืกืคืจ 3.
ืืชื ืืืื ืืจืืืช ืืช ืจืฉืืืช ืืชืืจื ืืงืืฆืื ืขืืืจ ืื ืชืืืื ืื ืืชื ืืืืข ืืช ื-PID ืฉืื.
ืืืืืื, ืืืื ื ืคืชื ืงืื ืกืืื ืขื bash ืื ืจืื ืืช ื-PID ืฉื ืืชืืืื ืฉืื ื
[user@localhost ]$ echo $$
15771
ืืืกืืฃ ืืฉื ื, ืืคืขื
[user@localhost ]$ ls -lah /proc/15771/fd/
total 0
dr-x------ 2 user user 0 Oct 7 15:42 .
dr-xr-xr-x 9 user user 0 Oct 7 15:42 ..
lrwx------ 1 user user 64 Oct 7 15:42 0 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 1 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 2 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 255 -> /dev/pts/21
ืืชื ืืืื ืืืชืขืื ืืืืื ืืชืืจ ืืงืืืฅ ืขื ืืกืคืจ 255 ืืืกืืจืช ืืืืจ ืื, ืืื ื ืคืชื ืืฆืจืืื ืฉืื ืขื ืืื bash ืขืฆืื, ืืื ืขื ืืื ืืกืคืจืืื ืืืงืืฉืจืช.
ืืขืช ืื 3 ืงืืฆื ืืชืืืืจ ืืฉืืืืื ืืืชืงื ืืคืกืืืื-ืืจืืื ืื
[user@localhost ]$ echo "hello world" > /proc/15771/fd/0
ืืืงืื ืกืืื ืืจืืฉืื ื ื ืจืื
[user@localhost ]$ hello world
Redirect ื-Pipe
ืืชื ืืืื ืืงืืืช ืืขืงืืฃ ืืช 3 ืงืืฆื ืืชืืืืจ ืืืื ืืื ืชืืืื, ืืืื ื-bash, ืืืฉื, ืืจื ืฆืื ืืจ (ืฆืื ืืจ) ืืืืืจ ืฉื ื ืชืืืืืื, ืจืื
[user@localhost ]$ cat /dev/zero | sleep 10000
ืืชื ืืืื ืืืคืขืื ืืช ืืคืงืืื ืืื ืืขืฆืื strace -f ืืืจืืืช ืื ืงืืจื ืืคื ืื, ืืื ืื ื ืืขืฉื ืืช ืื ืงืฆืจ.
ืชืืืื ืืืืจืืช ืฉืื ื ืขื PID 15771 ืื ืชื ืืช ืืคืงืืื ืฉืื ื ืืืืื ืืืืืง ืืื ืคืงืืืืช ืื ืื ื ืจืืฆืื ืืืคืขืื, ืืืงืจื ืฉืื ื ืืฉ ืฉืชืืื ืืื: ืืชืื ืืฉืื ื. ืืฉ ืืืืข ืฉืฆืจืื ืืืฆืืจ ืฉื ื ืชืืืืื ืืื, ืืืืื ืืืชื ืืฆืื ืืจ ืืื. ืืกื ืืื, bash ืืฆืืจื 2 ืชืืืืื ืฆืืฆื ืืฆืื ืืจ ืืื.
ืืคื ื ืืฆืืจืช ืชืืืืื ืฆืืฆื, bash ืืคืขืื ืงืจืืืช ืืขืจืืช
ืขืืืจ ืชืืืื ืืื, ื ืจืื ืฉืืฆืื ืืจ ืืืจ ืฉื, ืืื ืขืืืื ืืื ืชืืืืื ืฆืืฆื:
PID command
15771 bash
lrwx------ 1 user user 64 Oct 7 15:42 0 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 1 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 2 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 3 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:42 4 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:42 255 -> /dev/pts/21
ืืืืจ ืืื ืืืืฆืขืืช ืฉืืืช ืืืขืจืืช
PID command
15771 bash
lrwx------ 1 user user 64 Oct 7 15:42 0 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 1 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 2 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 3 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:42 4 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:42 255 -> /dev/pts/21
PID command
9004 bash
lrwx------ 1 user user 64 Oct 7 15:57 0 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:57 1 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:57 2 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:57 3 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:57 4 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:57 255 -> /dev/pts/21
PID command
9005 bash
lrwx------ 1 user user 64 Oct 7 15:57 0 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:57 1 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:57 2 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:57 3 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:57 4 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:57 255 -> /dev/pts/21
ืื ืชืฉืื ืฉืืฉืืืื ืืฉืืคื ืืช ืืชืืืื ืืื ืขื ืื ืืชืืจื ืืงืืฆืื, ืื ืฉืื ืืืื ืืืื ืืชืืืื ืืื ืืืืื ืฉื ืืืื. ืืืฉืืื ืฉื ืชืืืื ืืื ืขื PID 15771 ืืื ืืคืงื ืขื ืชืืืืื ืืฆืืฆื, ืื ืืื ืจืง ืืืื ืืชืืืื ืืืืืืื.
ืืื ืืื ืื ืฆืจืื ืฆืื ืืจ, ืืืื ืกืืืจ ืืช ืืชืืจื ืืงืืฆืื ืืืกืคืจืื 3 ื-4.
ืืชืืืื ืืืื ืืจืืฉืื ืฉื Bash ืขื PID 9004, ืืืขืจืืช ืงืืจืืช
ืืชืืืื ืืืื ืืฉื ื ืขื PID 9005, bash dup2s ืืช ืืงืืืฅ ื-STDIN descriptor number 0. ืืขืช ืื ืื ืฉื-bash ืืฉื ื ืฉืื ื ืขื PID 9005 ืืงืจื ืืืงืจื ืืืฆืื ืืจ.
ืืืืจ ืืื, ืืชืืจื ืงืืฆืื ืขื ืืืกืคืจืื 3 ื-4 ื ืกืืจืื ืื ืืชืืืืื ืฆืืฆื, ืืืืืื ืฉืื ืืื ื ืืฉืืืืฉ ืขืื.
ืื ื ืืชืขืื ืืืืื ื ืืชืืจ ืงืืืฅ 255, ืืื ื ืืฆื ืืฉืืืืฉ ืคื ืืื ืฉื bash ืขืฆืื ืืื ืืืกืืจ ืืชืืืืื ืฆืืฆื.
ืืืืจ ืืื, ืืชืืืื ืืืื ืืจืืฉืื ืขื PID 9004, bash ืืชืืื ืืงืจืืืช ืืขืจืืช
ืืชืืืื ืืืื ืืฉื ื ืขื PID 9005, bash ืืจืืฅ ืืช ืงืืืฅ ืืืคืขืื ืืฉื ื ืฉืฆืืื ื, ืืืงืจื ืฉืื ื /usr/bin/sleep.
ืืงืจืืื ืืืขืจืืช exec ืืื ื ืกืืืจืช ืืชืืจื ืงืืฆืื ืืื ืื ืื ืื ื ืคืชืื ืขื ืืื O_CLOEXEC ืืืื ืืืฆืืข ืืงืจืืื ืืคืชืืื. ืืืงืจื ืฉืื ื, ืืืืจ ืืคืขืืช ืงืืฆื ืืืคืขืื, ืื ืืชืืจื ืืงืืฆืื ืื ืืืืืื ืืืฉืืจื.
ืื ืื ื ืืืืงืื ืืงืื ืกืืื:
[user@localhost ]$ pgrep -P 15771
9004
9005
[user@localhost ]$ ls -lah /proc/15771/fd/
total 0
dr-x------ 2 user user 0 Oct 7 15:42 .
dr-xr-xr-x 9 user user 0 Oct 7 15:42 ..
lrwx------ 1 user user 64 Oct 7 15:42 0 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 1 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 2 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:42 255 -> /dev/pts/21
[user@localhost ]$ ls -lah /proc/9004/fd
total 0
dr-x------ 2 user user 0 Oct 7 15:57 .
dr-xr-xr-x 9 user user 0 Oct 7 15:57 ..
lrwx------ 1 user user 64 Oct 7 15:57 0 -> /dev/pts/21
l-wx------ 1 user user 64 Oct 7 15:57 1 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:57 2 -> /dev/pts/21
lr-x------ 1 user user 64 Oct 7 15:57 3 -> /dev/zero
[user@localhost ]$ ls -lah /proc/9005/fd
total 0
dr-x------ 2 user user 0 Oct 7 15:57 .
dr-xr-xr-x 9 user user 0 Oct 7 15:57 ..
lr-x------ 1 user user 64 Oct 7 15:57 0 -> pipe:[253543032]
lrwx------ 1 user user 64 Oct 7 15:57 1 -> /dev/pts/21
lrwx------ 1 user user 64 Oct 7 15:57 2 -> /dev/pts/21
[user@localhost ]$ ps -up 9004
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 9004 0.0 0.0 107972 620 pts/21 S+ 15:57 0:00 cat /dev/zero
[user@localhost ]$ ps -up 9005
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 9005 0.0 0.0 107952 360 pts/21 S+ 15:57 0:00 sleep 10000
ืืคื ืฉืืชื ืืืื ืืจืืืช, ืืืกืคืจ ืืืืืืื ืฉื ืืฆืื ืืจ ืฉืื ื ืืื ืืฉื ื ืืชืืืืืื. ืื, ืืฉ ืื ื ืงืฉืจ ืืื ืฉื ื ืชืืืืืื ืฉืื ืื ืขื ืืืชื ืืืจื.
ืืื ืฉืื ืืืืจ ืืช ืงืจืืืืช ืืืขืจืืช ืฉืืื bash ืืฉืชืืฉ, ืื ื ืืืืืฅ ืืืื ืืืจืืฅ ืคืงืืืืช ืืจื strace ืืืจืืืช ืื ืงืืจื ืืคื ืื, ืืืฉื, ืื:
strace -s 1024 -f bash -c "ls | grep hello"
ืืื ื ืืืืจ ืืืขืื ืฉืื ื ืขื ืืืื ืฉืื ืืืกืง ืื ืืกืืื ืืฉืืืจ ื ืชืื ืื ืืืื ืืืคืขืื ืืืืฉ ืืช ืืชืืืื. ืืื ื ืืชืื ืชืืื ืืช ืงืื ื ืฉืชืืชืื ืืืืกืง ืืขืจื 1 ืืื ืืืื ืืฉื ืืื. ืืชืจื ืืื, ืื ืืกืืื ืืืฉืื ืื ืืฆืืื ื ืืืชืื ื ืชืื ืื ืืืืกืง, ืคืฉืื ื ืชืขืื ืืื ืื ื ืกื ืืืชืื ืืช ืื ืชืื ืื ืฉืื ืืขืื ืฉื ืืื. ืืืืืื ืฉืื ื ืืฉืชืืฉ ื-Python, ืืชื ืืืื ืืืฉืชืืฉ ืืื ืฉืคืช ืชืื ืืช ืืืจืช.
[user@localhost ]$ cat openforwrite.py
import datetime
import time
mystr="a"*1024*1024+"n"
with open("123.txt", "w") as f:
while True:
try:
f.write(str(datetime.datetime.now()))
f.write(mystr)
f.flush()
time.sleep(1)
except:
pass
ืืคืขื ืืช ืืชืืื ืืช ืืืกืชืื ืขื ืืชืืจื ืืงืืฆืื
[user@localhost ]$ python openforwrite.py &
[1] 3762
[user@localhost ]$ ps axuf | grep [o]penforwrite
user 3762 0.0 0.0 128600 5744 pts/22 S+ 16:28 0:00 | _ python openforwrite.py
[user@localhost ]$ ls -la /proc/3762/fd
total 0
dr-x------ 2 user user 0 Oct 7 16:29 .
dr-xr-xr-x 9 user user 0 Oct 7 16:29 ..
lrwx------ 1 user user 64 Oct 7 16:29 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 7 16:29 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 7 16:29 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 7 16:29 3 -> /home/user/123.txt
ืืคื ืฉืืชื ืืืื ืืจืืืช, ืืฉ ืื ื 3 ืืชืืจื ืงืืฆืื ืกืื ืืจืืืื ืืขืื ืืื ืฉืคืชืื ื. ืืื ื ืืืืง ืืช ืืืื ืืงืืืฅ:
[user@localhost ]$ ls -lah 123.txt
-rw-rw-r-- 1 user user 117M Oct 7 16:30 123.txt
ืื ืชืื ืื ื ืืชืืื, ืื ื ืื ืกืื ืืฉื ืืช ืืช ืืืืืืืช ืืงืืืฅ:
[user@localhost ]$ sudo chown root: 123.txt
[user@localhost ]$ ls -lah 123.txt
-rw-rw-r-- 1 root root 168M Oct 7 16:31 123.txt
[user@localhost ]$ ls -lah 123.txt
-rw-rw-r-- 1 root root 172M Oct 7 16:31 123.txt
ืื ื ืจืืืื ืฉืื ืชืื ืื ืขืืืื ื ืืชืืื, ืืืจืืช ืฉืืืฉืชืืฉ ืฉืื ื ืืื ืืืืช ืืชืืื ืืงืืืฅ. ืืื ื ื ืกื ืืืกืืจ ืืืชื:
[user@localhost ]$ sudo rm 123.txt
[user@localhost ]$ ls 123.txt
ls: cannot access 123.txt: No such file or directory
ืืืื ืืชืืืื ืื ืชืื ืื? ืืืื ืื ืืชืืืื ืืืื? ืื ืื ื ืืืืงืื:
[user@localhost ]$ ls -la /proc/3762/fd
total 0
dr-x------ 2 user user 0 Oct 7 16:29 .
dr-xr-xr-x 9 user user 0 Oct 7 16:29 ..
lrwx------ 1 user user 64 Oct 7 16:29 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 7 16:29 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 7 16:29 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 7 16:29 3 -> /home/user/123.txt (deleted)
ืื, ืืชืืจ ืืงืืืฅ ืฉืื ื ืขืืืื ืงืืื, ืืื ืื ื ืืืืืื ืืขืืื ืขื ืืชืืจ ืืงืืืฅ ืืื ืืื ืืงืืืฅ ืืืฉื ืฉืื ื, ืื ืื ื ืืืืืื ืืงืจืื ืืืชื, ืื ืงืืช ืืืชื ืืืืขืชืืง ืืืชื.
ืชืกืชืื ืขื ืืืื ืืงืืืฅ:
[user@localhost ]$ lsof | grep 123.txt
python 31083 user 3w REG 8,5 19923457 2621522 /home/user/123.txt
ืืืื ืืงืืืฅ ืืื 19923457. ืื ืกื ืื ืงืืช ืืช ืืงืืืฅ:
[user@localhost ]$ truncate -s 0 /proc/31083/fd/3
[user@localhost ]$ lsof | grep 123.txt
python 31083 user 3w REG 8,5 136318390 2621522 /home/user/123.txt
ืืคื ืฉืืชื ืืืื ืืจืืืช, ืืืื ืืงืืืฅ ืจืง ืืื ืืืชื ืืืืขื ืฉืื ื ืื ืขืื. ื ืคื ื ืืชืืขืื ืขื ืฉืืืช ืืืขืจืืช
with open("123.txt", "w") as f:
ืื ืื ื ืฆืจืืืื ืืฉืื
with open("123.txt", "a") as f:
ืืืืง ืขื ืืื "w".
[user@localhost ]$ strace -e trace=open python openforwrite.py 2>&1| grep 123.txt
open("123.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
ืืขื ืืื "ื".
[user@localhost ]$ strace -e trace=open python openforwrite.py 2>&1| grep 123.txt
open("123.txt", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
ืชืื ืืช ืชืืืื ืฉืืืจ ืคืืขื
ืืขืชืื ืงืจืืืืช, ืืขืช ืืฆืืจื ืืืืืงื ืฉื ืชืืื ืืช, ืืชืื ืชืื ืืฉืชืืฉืื ืืืืจืื (ืืืืืื, GDB) ืื ืืจืืืช ืฉืื ืืช ืฉื ืจืืฉืื ืืืคืืืงืฆืื. ืืื ืืงืก ืืกืคืงืช ืืช ืืืืืืช ืืืชืื ืืืฉื ืืช ืชืืื ืืช ืฉืืืจ ืคืืขืืช, ืืื ืฉืื ืื ืขืจืื ืืฉืชื ืื, ืืืืจืช ื ืงืืืช ืฉืืืจื ืืื' ืืื'.
ืื ื ืืืืจ ืืฉืืื ืืืงืืจืืช ืขื ืืืกืจ ืืงืื ืืืืกืง ืืืชืืืช ืงืืืฅ, ืืืื ื ื ืกื ืืืืืช ืืช ืืืขืื.
ืืืื ื ืืฆืืจ ืงืืืฅ ืขืืืจ ืืืืืฆื ืฉืื ื, ืืืชื ื ืืขื ืืืื ื ื ืคืจื:
[user@localhost ~]$ dd if=/dev/zero of=~/tempfile_for_article.dd bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00525929 s, 2.0 GB/s
[user@localhost ~]$
ืืืื ื ืืฆืืจ ืืขืจืืช ืงืืฆืื:
[user@localhost ~]$ mkfs.ext4 ~/tempfile_for_article.dd
mke2fs 1.42.9 (28-Dec-2013)
/home/user/tempfile_for_article.dd is not a block special device.
Proceed anyway? (y,n) y
...
Writing superblocks and filesystem accounting information: done
[user@localhost ~]$
ืืืื ื ืขืื ืืช ืืขืจืืช ืืงืืฆืื:
[user@localhost ~]$ sudo mount ~/tempfile_for_article.dd /mnt/
[sudo] password for user:
[user@localhost ~]$ df -h | grep mnt
/dev/loop0 8.7M 172K 7.9M 3% /mnt
ืฆืืจ ืกืคืจืืื ืขื ืืืขืืื ืฉืื ื:
[user@localhost ~]$ sudo mkdir /mnt/logs
[user@localhost ~]$ sudo chown user: /mnt/logs
ืืืื ื ืคืชื ืืช ืืงืืืฅ ืืืชืืื ืจืง ืืชืืื ืืช ืฉืื ื:
with open("/mnt/logs/123.txt", "w") as f:
ืจืืฅ
[user@localhost ]$ python openforwrite.py
ืืืื ืืื ืฉื ืืืช
[user@localhost ~]$ df -h | grep mnt
/dev/loop0 8.7M 8.0M 0 100% /mnt
ืื ืงืืืื ื ืืช ืืืขืื ืฉืชืืืจื ืืชืืืืช ืืืืจ ืื. ืืงืื ืคื ืื 0, ืชืคืืก 100%.
ืื ื ืืืืจืื ืฉืืคื ืชื ืื ืืืขืื ืื ื ืื ืกืื ืืจืฉืื ื ืชืื ืื ืืฉืืืื ืืืืชืจ ืฉืื ื ืืชื ืืืื. ืืืื, ืขืืื ื ืืชืงื ืืช ืืฉืืจืืช ืืืื ืืืคืขืื ืืืืฉ ืืช ืืชืืืื.
ื ื ืื ืฉืขืืืื ืืฉ ืื ื ืฉืื ืืืกืง, ืืื ืืืืืฆื ืืืจืช, ืืืฉื, ื/ืืืช.
ืืืื ื ื ืกื "ืืชืื ืช ืืืืฉ ืชืื ืืื ืชื ืืขื" ืืช ืืงืื ืฉืื ื.
ืื ื ืืกืชืืืื ืขื ื-PID ืฉื ืืชืืืื ืฉืื ื, ืฉืืื ืืช ืื ืฉืื ืืืืกืง:
[user@localhost ~]$ ps axuf | grep [o]penfor
user 10078 27.2 0.0 128600 5744 pts/22 R+ 11:06 0:02 | _ python openforwrite.py
ืืชืืืจืืช ืืชืืืื ืขื gdb
[user@localhost ~]$ gdb -p 10078
...
(gdb)
ืื ื ืืกืชืืืื ืขื ืืชืืจื ืงืืฆืื ืคืชืืืื:
(gdb) shell ls -lah /proc/10078/fd/
total 0
dr-x------ 2 user user 0 Oct 8 11:06 .
dr-xr-xr-x 9 user user 0 Oct 8 11:06 ..
lrwx------ 1 user user 64 Oct 8 11:09 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:09 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:06 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:09 3 -> /mnt/logs/123.txt
ืื ืื ื ืืกืชืืืื ืขื ืืืืข ืขื ืืชืืจ ืืงืืืฅ ืขื ืืกืคืจ 3, ืฉืืขื ืืื ืืืชื ื
(gdb) shell cat /proc/10078/fdinfo/3
pos: 8189952
flags: 0100001
mnt_id: 482
ืชืื ืืชืืฉืืืช ืืืืื ืงืจืืืช ืืขืจืืช Python ืขืืฉื (ืจืื ืืืขืื ืืืื ืืจืฆื ื ืืช ื-strace ืืืฆืื ื ืงืจืืื ืคืชืืื), ืืืื ืขืืืื ืืงืื ืฉืื ื ืืคืชืืืช ืงืืืฅ, ืื ืื ื ืขืืฉืื ืืช ืืืชื ืืืืจ ืืขืฆืื ื ืืฉื ืืชืืืื ืฉืื ื, ืืื ืื ืื ื ืฆืจืืืื ืืช ื-O_WRONLY|O_CREAT| ืกืืืืืช O_TRUNC ืืืืืคืืช ืืขืจื ืืกืคืจื. ืืฉื ืื, ืคืชื ืืช ืืงืืจืืช ืืืืื, ืืืฉื
#define O_WRONLY 00000001
#define O_CREAT 00000100
#define O_TRUNC 00001000
ืื ื ืืฉืืืื ืืช ืื ืืขืจืืื ืืืื, ื ืงืื 00001101
ืืคืขืื ืืช ืืฉืืื ืฉืื ื ื-gdb
(gdb) call open("/home/user/123.txt", 00001101,0666)
$1 = 4
ืื ืงืืืื ื ืืชืืจ ืงืืืฅ ืืืฉ ืขื ืืกืคืจ 4 ืืงืืืฅ ืคืชืื ืืืฉ ืืืืืฆื ืืืจืช, ืืืืง:
(gdb) shell ls -lah /proc/10078/fd/
total 0
dr-x------ 2 user user 0 Oct 8 11:06 .
dr-xr-xr-x 9 user user 0 Oct 8 11:06 ..
lrwx------ 1 user user 64 Oct 8 11:09 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:09 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:06 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:09 3 -> /mnt/logs/123.txt
l-wx------ 1 user user 64 Oct 8 11:15 4 -> /home/user/123.txt
ืื ืื ื ืืืืจืื ืืช ืืืืืื ืขื pipe - ืืื bash ืืฉื ื ืืชืืจื ืงืืฆืื, ืืืืจ ืืืื ื ืืช ืงืจืืืช ืืืขืจืืช dup2.
ืื ืกื ืืืืืืฃ ืืชืืจ ืงืืืฅ ืืื ืืืืจ
(gdb) call dup2(4,3)
$2 = 3
ืื ื ืืืืงืื:
(gdb) shell ls -lah /proc/10078/fd/
total 0
dr-x------ 2 user user 0 Oct 8 11:06 .
dr-xr-xr-x 9 user user 0 Oct 8 11:06 ..
lrwx------ 1 user user 64 Oct 8 11:09 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:09 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:06 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:09 3 -> /home/user/123.txt
l-wx------ 1 user user 64 Oct 8 11:15 4 -> /home/user/123.txt
ืกืืืจ ืืช ืืชืืจ ืงืืืฅ 4, ืืืืืื ืฉืื ื ืื ืฆืจืืืื ืืืชื:
(gdb) call close (4)
$1 = 0
ืืฆื ื-gdb
(gdb) quit
A debugging session is active.
Inferior 1 [process 10078] will be detached.
Quit anyway? (y or n) y
Detaching from program: /usr/bin/python2.7, process 10078
ืืืืงืช ืืงืืืฅ ืืืืฉ:
[user@localhost ~]$ ls -lah /home/user/123.txt
-rw-rw-r-- 1 user user 5.1M Oct 8 11:18 /home/user/123.txt
[user@localhost ~]$ ls -lah /home/user/123.txt
-rw-rw-r-- 1 user user 7.1M Oct 8 11:18 /home/user/123.txt
ืืคื ืฉืืชื ืืืื ืืจืืืช, ืื ืชืื ืื ื ืืชืืื ืืงืืืฅ ืืืฉ, ืื ื ืืืืงืื ืืช ืืืฉื:
[user@localhost ~]$ ls -lah /mnt/logs/123.txt
-rw-rw-r-- 1 user user 7.9M Oct 8 11:08 /mnt/logs/123.txt
ืื ืชืื ืื ืื ืืืืืื ืืืืืื, ืืืคืืืงืฆืื ืขืืืืช, ืืืืื ืื ื ืืชืืื ืืืืงืื ืืืฉ.
ืืืื ื ืขืฉื ืืช ืืืืจืื ืงืฆืช ืืืชืจ ืงืฉืื
ืชืืจื ืืขืฆืืื ืฉืื ืชืื ืื ืืฉืืืื ืื ื, ืืื ืืื ืื ื ืฉืื ืืืกืง ืืืฃ ืืืช ืืืืืืฆืืช ืืื ื ืืื ืืืืจ ืืช ืืืืกืง.
ืื ืฉืื ืื ื ืืืืืื ืืขืฉืืช ืื ืืืคื ืืช ืืช ืื ืชืื ืื ืฉืื ื ืืืงืื ืืืฉืื, โโืืืฉื, ืืฆืื ืืจ, ืืื ืชื ืืืืฉ ืืช ืื ืชืื ืื ืืืฆืื ืืจ ืืจืฉืช ืืจื ืชืืื ื ืืืฉืื, ืืื netcat.
ืื ืื ื ืืืืืื ืืืฆืืจ ืฆืื ืืจ ืขื ืืคืงืืื mkfifo. ืื ืืฆืืจ ืงืืืฅ ืคืกืืืื ืืืขืจืืช ืืงืืฆืื, ืื ืื ืืื ืื ืืงืื ืคื ืื.
ืืคืขื ืืืืฉ ืืช ืืืืฉืื ืืืืืง:
[user@localhost ]$ python openforwrite.py
[user@localhost ~]$ ps axuf | grep [o]pen
user 5946 72.9 0.0 128600 5744 pts/22 R+ 11:27 0:20 | _ python openforwrite.py
[user@localhost ~]$ ls -lah /proc/5946/fd
total 0
dr-x------ 2 user user 0 Oct 8 11:27 .
dr-xr-xr-x 9 user user 0 Oct 8 11:27 ..
lrwx------ 1 user user 64 Oct 8 11:28 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:28 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:27 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:28 3 -> /mnt/logs/123.txt
[user@localhost ~]$ df -h | grep mnt
/dev/loop0 8.7M 8.0M 0 100% /mnt
ืืื ืืงืื ืืืืกืง, ืืื ืื ืื ื ืืืฆืจืื ืฉื ืืืฆืืื ืฆืื ืืจ ืืฉื:
[user@localhost ~]$ mkfifo /mnt/logs/megapipe
[user@localhost ~]$ ls -lah /mnt/logs/megapipe
prw-rw-r-- 1 user user 0 Oct 8 11:28 /mnt/logs/megapipe
ืขืืฉืื ืื ืื ื ืฆืจืืืื ืืืืฉืื ืืขืืืฃ ืืช ืื ืื ืชืื ืื ืฉื ืื ืกืื ืืฆืื ืืจ ืืื ืืฉืจืช ืืืจ ืืจื ืืจืฉืช, ืืืชื netcat ืืชืืื ืืื.
ืืฉืจืช Remote-server.example.com, ืืคืขื
[user@localhost ~]$ nc -l 7777 > 123.txt
ืืฉืจืช ืืืขืืืชื ืฉืื ื, ืืคืขื ืืืกืืฃ ื ืคืจื
[user@localhost ~]$ nc remote-server.example.com 7777 < /mnt/logs/megapipe
ืืขืช ืื ืื ืชืื ืื ืฉืืืื ืกื ืืฆืื ืืจ ืืขืืจื ืืืืืืืืช ืื stdin ื-netcat, ืืฉืจ ืืฉืื ืืืชื ืืจืฉืช ืืคืืจื 7777.
ืื ืฉืขืืื ื ืืขืฉืืช ืืื ืืืชืืื ืืืชืื ืืช ืื ืชืื ืื ืฉืื ื ืืฆืื ืืจ ืืขื ืืฉื ืืื.
ืืืจ ืืฉ ืื ื ืืคืืืงืฆืื ืคืืขืืช:
[user@localhost ~]$ ps axuf | grep [o]pen
user 5946 99.8 0.0 128600 5744 pts/22 R+ 11:27 169:27 | _ python openforwrite.py
[user@localhost ~]$ ls -lah /proc/5946/fd
total 0
dr-x------ 2 user user 0 Oct 8 11:27 .
dr-xr-xr-x 9 user user 0 Oct 8 11:27 ..
lrwx------ 1 user user 64 Oct 8 11:28 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:28 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:27 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:28 3 -> /mnt/logs/123.txt
ืืื ืืืืืื, ืื ืื ื ืฆืจืืืื ืจืง O_WRONLY ืืืืืื ืฉืืงืืืฅ ืืืจ ืงืืื ืืืื ื ื ืฆืจืืืื ืื ืงืืช ืืืชื
[user@localhost ~]$ gdb -p 5946
...
(gdb) call open("/mnt/logs/megapipe", 00000001,0666)
$1 = 4
(gdb) shell ls -lah /proc/5946/fd
total 0
dr-x------ 2 user user 0 Oct 8 11:27 .
dr-xr-xr-x 9 user user 0 Oct 8 11:27 ..
lrwx------ 1 user user 64 Oct 8 11:28 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:28 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:27 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:28 3 -> /mnt/logs/123.txt
l-wx------ 1 user user 64 Oct 8 14:20 4 -> /mnt/logs/megapipe
(gdb) call dup2(4,3)
$2 = 3
(gdb) shell ls -lah /proc/5946/fd
total 0
dr-x------ 2 user user 0 Oct 8 11:27 .
dr-xr-xr-x 9 user user 0 Oct 8 11:27 ..
lrwx------ 1 user user 64 Oct 8 11:28 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:28 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:27 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:28 3 -> /mnt/logs/megapipe
l-wx------ 1 user user 64 Oct 8 14:20 4 -> /mnt/logs/megapipe
(gdb) call close(4)
$3 = 0
(gdb) shell ls -lah /proc/5946/fd
total 0
dr-x------ 2 user user 0 Oct 8 11:27 .
dr-xr-xr-x 9 user user 0 Oct 8 11:27 ..
lrwx------ 1 user user 64 Oct 8 11:28 0 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:28 1 -> /dev/pts/22
lrwx------ 1 user user 64 Oct 8 11:27 2 -> /dev/pts/22
l-wx------ 1 user user 64 Oct 8 11:28 3 -> /mnt/logs/megapipe
(gdb) quit
A debugging session is active.
Inferior 1 [process 5946] will be detached.
Quit anyway? (y or n) y
Detaching from program: /usr/bin/python2.7, process 5946
ืืืืงืช ืืฉืจืช ืืืจืืืง remote-server.example.com
[user@localhost ~]$ ls -lah 123.txt
-rw-rw-r-- 1 user user 38M Oct 8 14:21 123.txt
ื ืชืื ืื ืืืืขืื, ืื ืื ื ืืืืงืื ืืช ืืฉืจืช ืืืขืืืชื
[user@localhost ~]$ ls -lah /mnt/logs/
total 7.9M
drwxr-xr-x 2 user user 1.0K Oct 8 11:28 .
drwxr-xr-x 4 root root 1.0K Oct 8 10:55 ..
-rw-rw-r-- 1 user user 7.9M Oct 8 14:17 123.txt
prw-rw-r-- 1 user user 0 Oct 8 14:22 megapipe
ืื ืชืื ืื ื ืฉืืจืื, ืืืขืื ื ืคืชืจื.
ืื ื ืื ืฆื ืืืืื ืืช ืื ืืืืจ ืฉืืื ืืขืืืชืื ืืืืืจื.
ืืืืื ื ืืคืืืงืืกืืื ืฉื Radio-T.
ืืื ืืืืื.
ืืชืืจ ืฉืืขืืจื ืืืช, ืื ื ืืฆืืข ืืืฉืื ืื ืืืื ืืชืืืืจื ืืงืืฆืื ืฉื ืชืืืื ืืืชืื ืืืฉืื ื ืื ืชืคืขืื ืืช ืืคืงืืื ืืืื:
[user@localhost ~]$ cat /dev/zero 2>/dev/null| sleep 10000
ืืงืืจ: www.habr.com