ืžืชืืจ ืงื•ื‘ืฅ ื‘ืœื™ื ื•ืงืก ืขื ื“ื•ื’ืžืื•ืช

ืคืขื ืื—ืช, ื‘ืจืื™ื•ืŸ, ืฉืืœื• ืื•ืชื™ ืžื” ื”ื™ื™ืชื ืขื•ืฉื™ื ืื ืชืžืฆืื• ืฉื™ืจื•ืช ืžืงื•ืœืงืœ ื‘ื’ืœืœ ื”ืขื•ื‘ื“ื” ืฉื ื’ืžืจ ื”ืžืงื•ื ื‘ื“ื™ืกืง?

ื›ืžื•ื‘ืŸ, ืขื ื™ืชื™ ืฉืืจืื” ืžื” ื”ืžืงื•ื ื”ื–ื” ืขื•ืฉื” ื•ืื ืืคืฉืจ, ืื ืงื” ืืช ื”ืžืงื•ื.
ื•ืื– ื”ืžืจืื™ื™ืŸ ืฉืืœ, ืžื” ืื ืื™ืŸ ืžืงื•ื ืคื ื•ื™ ื‘ืžื—ื™ืฆื”, ืื‘ืœ ืืชื” ื’ื ืœื ืจื•ืื” ืืช ื”ืงื‘ืฆื™ื ืฉื™ืชืคืกื• ืืช ื›ืœ ื”ืžืงื•ื?

ืขืœ ื›ืš ืืžืจืชื™ ืฉืชืžื™ื“ ืืคืฉืจ ืœื”ืกืชื›ืœ ืขืœ ืžืชืืจื™ ืงื‘ืฆื™ื ืคืชื•ื—ื™ื, ืœืžืฉืœ ืขื ื”ืคืงื•ื“ื” lsof ื•ืœื”ื‘ื™ืŸ ืื™ื–ื• ืืคืœื™ืงืฆื™ื” ืœืงื—ื” ืืช ื›ืœ ื”ืฉื˜ื— ื”ืคื ื•ื™, ื•ืื– ืืคืฉืจ ืœืคืขื•ืœ ื‘ื”ืชืื ืœื ืกื™ื‘ื•ืช, ืชืœื•ื™ ืื ื™ืฉ ืฆื•ืจืš ื‘ื ืชื•ื ื™ื .

ื”ืžืจืื™ื™ืŸ ืงื˜ืข ืื•ืชื™ ื‘ืžื™ืœื” ื”ืื—ืจื•ื ื” ื•ื”ื•ืกื™ืฃ ืœืฉืืœืชื•: "ื ื ื™ื— ืฉืื ื—ื ื• ืœื ืฆืจื™ื›ื™ื ืืช ื”ื ืชื•ื ื™ื, ื–ื” ืจืง ื™ื•ืžืŸ ื ื™ืคื•ื™ ื‘ืื’ื™ื, ืื‘ืœ ื”ืืคืœื™ืงืฆื™ื” ืžื•ืฉื‘ืชืช ื›ื™ ื”ื™ื ืœื ื™ื›ื•ืœื” ืœื›ืชื•ื‘ ืืช ื”ื‘ืื’ื™ื"?

"ื‘ืกื“ืจ," ืขื ื™ืชื™, "ื ื•ื›ืœ ืœื›ื‘ื•ืช ืืช ื ื™ืคื•ื™ ื”ื‘ืื’ื™ื ื‘ืชืฆื•ืจืช ื”ืืคืœื™ืงืฆื™ื” ื•ืœื”ืคืขื™ืœ ืื•ืชื” ืžื—ื“ืฉ."
ื”ืžืจืื™ื™ืŸ ื”ืชื ื’ื“: "ืœื, ืื ื—ื ื• ืœื ื™ื›ื•ืœื™ื ืœื”ืคืขื™ืœ ืžื—ื“ืฉ ืืช ื”ืืคืœื™ืงืฆื™ื”, ืขื“ื™ื™ืŸ ื™ืฉ ืœื ื• ื ืชื•ื ื™ื ื—ืฉื•ื‘ื™ื ื‘ื–ื™ื›ืจื•ืŸ, ื•ืœืงื•ื—ื•ืช ื—ืฉื•ื‘ื™ื ืžื—ื•ื‘ืจื™ื ืœืฉื™ืจื•ืช ืขืฆืžื•, ืฉืื ื—ื ื• ืœื ื™ื›ื•ืœื™ื ืœื”ื›ืจื™ื— ืœื”ืชื—ื‘ืจ ืžื—ื“ืฉ".

"ื‘ืกื“ืจ," ืืžืจืชื™, "ืื ืœื ื ื•ื›ืœ ืœื”ืคืขื™ืœ ืžื—ื“ืฉ ืืช ื”ืืคืœื™ืงืฆื™ื” ื•ืœื ืื›ืคืช ืœื ื• ืžื”ื ืชื•ื ื™ื, ื ื•ื›ืœ ืคืฉื•ื˜ ืœื ืงื•ืช ืืช ื”ืงื•ื‘ืฅ ื”ืคืชื•ื— ื”ื–ื” ื“ืจืš ืžืชืืจ ื”ืงื•ื‘ืฅ, ื’ื ืื ืื™ื ื ื• ืจื•ืื™ื ืื•ืชื• ื‘-ls ืคืงื•ื“ื” ื‘ืžืขืจื›ืช ื”ืงื‘ืฆื™ื."

ื”ืžืจืื™ื™ืŸ ื”ื™ื” ืžืจื•ืฆื”, ืื‘ืœ ืื ื™ ืœื.

ื•ืื– ื—ืฉื‘ืชื™, ืœืžื” ื”ืื“ื ืฉื‘ื•ื“ืง ืืช ื”ื™ื“ืข ืฉืœื™ ืœื ื—ื•ืคืจ ืœืขื•ืžืง? ืื‘ืœ ืžื” ืื ื”ื ืชื•ื ื™ื ื‘ื›ืœ ื–ืืช ื—ืฉื•ื‘ื™ื? ืžื” ืื ืื ื—ื ื• ืœื ื™ื›ื•ืœื™ื ืœื”ืคืขื™ืœ ืžื—ื“ืฉ ืืช ื”ืชื”ืœื™ืš, ื•ื‘ืžืงื‘ื™ืœ ื”ืชื”ืœื™ืš ื”ื–ื” ื›ื•ืชื‘ ืœืžืขืจื›ืช ื”ืงื‘ืฆื™ื ืขืœ ืžื—ื™ืฆื” ืฉืื™ืŸ ืœื” ืžืงื•ื ืคื ื•ื™? ืžื” ืื ืœื ื ื•ื›ืœ ืœืื‘ื“ ืœื ืจืง ืืช ื”ื ืชื•ื ื™ื ืฉื›ื‘ืจ ื ื›ืชื‘ื•, ืืœื ื’ื ืืช ื”ื ืชื•ื ื™ื ืฉื”ืชื”ืœื™ืš ื”ื–ื” ื›ื•ืชื‘ ืื• ืžื ืกื” ืœื›ืชื•ื‘?

ื˜ื•ื–ื™ืง

ื‘ืชื—ื™ืœืช ื”ืงืจื™ื™ืจื” ืฉืœื™, ื ื™ืกื™ืชื™ ืœื™ืฆื•ืจ ืืคืœื™ืงืฆื™ื” ืงื˜ื ื” ืฉืฆืจื™ื›ื” ืœืื—ืกืŸ ืžื™ื“ืข ืขืœ ืžืฉืชืžืฉื™ื. ื•ืื– ื—ืฉื‘ืชื™, ืื™ืš ืื ื™ ื™ื›ื•ืœ ืœื”ืชืื™ื ืืช ื”ืžืฉืชืžืฉ ืœื ืชื•ื ื™ื ืฉืœื•. ืœื“ื•ื’ืžื”, ื™ืฉ ืœื™ ืื™ื‘ื ื•ื‘ ืื™ื‘ืŸ ืื™ื‘ื ื•ื‘ื™ืฅ', ื•ื™ืฉ ืœื• ื›ืžื” ื ืชื•ื ื™ื, ืื‘ืœ ืื™ืš ืœื”ืชื™ื™ื“ื“ ืื™ืชื? ืื ื™ ื™ื›ื•ืœ ืœืฆื™ื™ืŸ ื™ืฉื™ืจื•ืช ืฉื”ื›ืœื‘ ื‘ืฉื "ื˜ื•ื–ื™ืง" ืฉื™ื™ืš ืœืื•ืชื• ืื™ื•ื•ืŸ. ืื‘ืœ ืžื” ืื ื”ื•ื ื™ืฉื ื” ืืช ืฉืžื• ื•ื‘ืžืงื•ื ืื™ื‘ืŸ ื™ื”ืคื•ืš, ืœืžืฉืœ, ืœืื•ืœื™ื”? ืื– ื™ืชื‘ืจืจ ืฉืœืื•ืœื™ื” ืื™ื‘ื ื•ื‘ื ื” ืื™ื‘ื ื•ื‘ื” ืฉืœื ื• ื›ื‘ืจ ืœื ื™ื”ื™ื” ื›ืœื‘, ื•ื”ื˜ื•ื–ื™ืง ืฉืœื ื• ืขื“ื™ื™ืŸ ื™ื”ื™ื” ืฉื™ื™ืš ืœืื™ื•ื•ืŸ ื”ืœื ืงื™ื™ื. ื‘ืกื™ืก ื”ื ืชื•ื ื™ื ืขื–ืจ ืœืคืชื•ืจ ืืช ื”ื‘ืขื™ื” ื”ื–ื•, ืฉื ืชื ื” ืœื›ืœ ืžืฉืชืžืฉ ืžื–ื”ื” ื™ื™ื—ื•ื“ื™ (ID), ื•ื”-Tuzik ืฉืœื™ ื”ื™ื” ืงืฉื•ืจ ืœืžื–ื”ื” ื”ื–ื”, ืฉืœืžืขืฉื” ื”ื™ื” ืจืง โ€‹โ€‹ืžืกืคืจ ืกื™ื“ื•ืจื™. ืœืคื™ื›ืš, ื”ื‘ืขืœื™ื ืฉืœ ื”ื˜ื•ื–ื™ืง ื”ื™ื” ืขื ืžืกืคืจ ืชืขื•ื“ืช ื–ื”ื•ืช 2, ื•ื‘ืฉืœื‘ ืžืกื•ื™ื ืื™ื‘ืŸ ื”ื™ื” ื‘ืชืขื•ื“ืช ื–ื”ื•ืช ื–ื•, ื•ืื– ืื•ืœื™ื” ื”ืคื›ื” ืœืื•ืชื” ืชืขื•ื“ืช ื–ื”ื•ืช. ื‘ืขื™ื™ืช ื”ืื ื•ืฉื•ืช ื•ื’ื™ื“ื•ืœ ื‘ืขืœื™ ื”ื—ื™ื™ื ื ืคืชืจื” ืœืžืขืฉื”.

ืžืชืืจ ืงื•ื‘ืฅ

ื”ื‘ืขื™ื” ืฉืœ ืงื•ื‘ืฅ ื•ืชื•ื›ื ื™ืช ืฉืขื•ื‘ื“ื™ื ืขื ื”ืงื•ื‘ืฅ ื”ื–ื” ื”ื™ื ื‘ืขืจืš ื›ืžื• ื”ื›ืœื‘ ื•ื”ืื“ื ืฉืœื ื•. ื ื ื™ื— ืฉืคืชื—ืชื™ ืงื•ื‘ืฅ ื‘ืฉื ivan.txt ื•ื”ืชื—ืœืชื™ ืœื›ืชื•ื‘ ื‘ื• ืืช ื”ืžื™ืœื” tuzik, ืื‘ืœ ื”ืฆืœื—ืชื™ ืœื›ืชื•ื‘ ืจืง ืืช ื”ืื•ืช ื”ืจืืฉื•ื ื” "t" ืœืงื•ื‘ืฅ, ื•ื”ืงื•ื‘ืฅ ื”ื–ื” ืฉื•ื ื” ืขืœ ื™ื“ื™ ืžื™ืฉื”ื•, ืœืžืฉืœ, ืœ-olya.txt. ืื‘ืœ ื”ืงื•ื‘ืฅ ื–ื”ื” ื•ืื ื™ ืขื“ื™ื™ืŸ ืจื•ืฆื” ืœื›ืชื•ื‘ ืœื• ืืช ื”ืืก ืฉืœื™. ื‘ื›ืœ ืคืขื ืฉืืชื” ืคื•ืชื— ืงื•ื‘ืฅ ืขื ืงืจื™ืืช ืžืขืจื›ืช ืœืคืชื•ื— ื‘ื›ืœ ืฉืคืช ืชื›ื ื•ืช, ืื ื™ ืžืงื‘ืœ ืžื–ื”ื” ื™ื™ื—ื•ื“ื™ ืฉืžืคื ื” ืื•ืชื™ ืœืงื•ื‘ืฅ, ืžื–ื”ื” ื–ื” ื”ื•ื ืžืชืืจ ื”ืงื•ื‘ืฅ. ื•ื–ื” ื‘ื›ืœืœ ืœื ืžืฉื ื” ืžื” ื•ืžื™ ื™ืขืฉื” ื”ืœืื” ืขื ื”ืงื•ื‘ืฅ ื”ื–ื”, ืืคืฉืจ ืœืžื—ื•ืง ืื•ืชื•, ืืคืฉืจ ืœืฉื ื•ืช ืืช ืฉืžื•, ื”ื•ื ื™ื›ื•ืœ ืœืฉื ื•ืช ืืช ื”ื‘ืขืœื™ื ืฉืœื• ืื• ืœืงื—ืช ืžืžื ื• ืืช ื–ื›ื•ื™ื•ืช ื”ืงืจื™ืื” ื•ื”ื›ืชื™ื‘ื”, ืขื“ื™ื™ืŸ ืชื”ื™ื” ืœื™ ื’ื™ืฉื” ืืœื™ื•, ื›ื™ ื‘ื–ืžืŸ ืคืชื™ื—ืช ื”ืงื•ื‘ืฅ ื”ื™ื• ืœื™ ื–ื›ื•ื™ื•ืช ืœืงืจื•ื ื•/ืื• ืœื›ืชื•ื‘ ืื•ืชื• ื•ื”ืฆืœื—ืชื™ ืœื”ืชื—ื™ืœ ืœืขื‘ื•ื“ ืื™ืชื•, ืžื” ืฉืื•ืžืจ ืฉืื ื™ ื—ื™ื™ื‘ ืœื”ืžืฉื™ืš ืœืขืฉื•ืช ื–ืืช.

ื‘ืœื™ื ื•ืงืก, ืกืคืจื™ื™ืช libc ืคื•ืชื—ืช 3 ืงื‘ืฆื™ ืชื™ืื•ืจ ืขื‘ื•ืจ ื›ืœ ื™ื™ืฉื•ื (ืชื”ืœื™ืš) ื”ืคื•ืขืœ, ืขื ื”ืžืกืคืจื™ื 0,1,2. ืžื™ื“ืข ื ื•ืกืฃ ืชื•ื›ืœ ืœืžืฆื•ื ื‘ืงื™ืฉื•ืจื™ื man stdio ะธ ื’ื‘ืจ ืžืฆื˜ื™ื™ืŸ

  • ืžืชืืจ ื”ืงื•ื‘ืฅ 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 ืงื‘ืฆื™ ื”ืชื™ืื•ืจ ืžืฉื•ื™ื›ื™ื ืœื”ืชืงืŸ ื”ืคืกืื•ื“ื•-ื˜ืจืžื™ื ืœื™ /dev/pts, ืื‘ืœ ืื ื—ื ื• ืขื“ื™ื™ืŸ ื™ื›ื•ืœื™ื ืœืชืคืขืœ ืื•ืชื, ืœืžืฉืœ, ืœื”ืคืขื™ืœ ื‘ืงื•ื ืกื•ืœื” ื”ืฉื ื™ื™ื”

[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

ืœืื—ืจ ืžื›ืŸ ื‘ืืžืฆืขื•ืช ืฉื™ื—ืช ื”ืžืขืจื›ืช ืœืฉื‘ื˜ 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
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, ื”ืžืขืจื›ืช ืงื•ืจืืช dup2, ืžืฉื ื” ืืช ืžืชืืจ ื”ืงื•ื‘ืฅ STDOUT ืžืกืคืจ 1 ืœืชื™ืื•ืจ ืงื•ื‘ืฅ ื”ืžืฆื‘ื™ืข ืขืœ ืฆื™ื ื•ืจ, ื‘ืžืงืจื” ืฉืœื ื• ื”ื•ื ืžืกืคืจ 3. ืœืคื™ื›ืš, ื›ืœ ืžื” ืฉืชื”ืœื™ืš ื”ื™ืœื“ ื”ืจืืฉื•ืŸ ืขื PID 9004 ื›ื•ืชื‘ ืœ-STDOUT ื™ื™ืคื•ืœ ืื•ื˜ื•ืžื˜ื™ืช ืœืžืื’ืจ ื”ืฆื™ื ื•ืจื•ืช.

ื‘ืชื”ืœื™ืš ื”ื™ืœื“ ื”ืฉื ื™ ืขื PID 9005, bash dup2s ืืช ื”ืงื•ื‘ืฅ ืœ-STDIN descriptor number 0. ื›ืขืช ื›ืœ ืžื” ืฉื”-bash ื”ืฉื ื™ ืฉืœื ื• ืขื PID 9005 ื™ืงืจื ื™ื™ืงืจื ืžื”ืฆื™ื ื•ืจ.

ืœืื—ืจ ืžื›ืŸ, ืžืชืืจื™ ืงื‘ืฆื™ื ืขื ื”ืžืกืคืจื™ื 3 ื•-4 ื ืกื’ืจื™ื ื’ื ื‘ืชื”ืœื™ื›ื™ ืฆืืฆื, ืžื›ื™ื•ื•ืŸ ืฉื”ื ืื™ื ื ื‘ืฉื™ืžื•ืฉ ืขื•ื“.

ืื ื™ ืžืชืขืœื ื‘ื›ื•ื•ื ื” ืžืชืืจ ืงื•ื‘ืฅ 255, ื”ื•ื ื ืžืฆื ื‘ืฉื™ืžื•ืฉ ืคื ื™ืžื™ ืฉืœ bash ืขืฆืžื• ื•ื’ื ื™ื™ืกื’ืจ ื‘ืชื”ืœื™ื›ื™ ืฆืืฆื.

ืœืื—ืจ ืžื›ืŸ, ื‘ืชื”ืœื™ืš ื”ื™ืœื“ ื”ืจืืฉื•ืŸ ืขื PID 9004, bash ืžืชื—ื™ืœ ื‘ืงืจื™ืืช ืžืขืจื›ืช Exec ืงื•ื‘ืฅ ื”ื”ืคืขืœื” ืฉืฆื™ื™ื ื• ื‘ืฉื•ืจืช ื”ืคืงื•ื“ื”, ื‘ืžืงืจื” ืฉืœื ื• ื–ื” /usr/bin/cat.

ื‘ืชื”ืœื™ืš ื”ื™ืœื“ ื”ืฉื ื™ ืขื 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

ื›ืคื™ ืฉืืชื” ื™ื›ื•ืœ ืœืจืื•ืช, ื’ื•ื“ืœ ื”ืงื•ื‘ืฅ ืจืง ื’ื“ืœ ื•ื”ืชื ื”ืžื˜ืขืŸ ืฉืœื ื• ืœื ืขื‘ื“. ื ืคื ื” ืœืชื™ืขื•ื“ ืขืœ ืฉื™ื—ืช ื”ืžืขืจื›ืช ืœืคืชื•ื—. ืื ืื ื• ืžืฉืชืžืฉื™ื ื‘ื“ื’ืœ O_APPEND ื‘ืขืช ืคืชื™ื—ืช ืงื•ื‘ืฅ, ืื– ืขื ื›ืœ ื›ืชื™ื‘ื”, ืžืขืจื›ืช ื”ื”ืคืขืœื” ื‘ื•ื“ืงืช ืืช ื’ื•ื“ืœ ื”ืงื•ื‘ืฅ ื•ื›ื•ืชื‘ืช ื ืชื•ื ื™ื ืขื“ ืกื•ืฃ ื”ืงื•ื‘ืฅ, ื•ืขื•ืฉื” ื–ืืช ื‘ืฆื•ืจื” ืื˜ื•ืžื™ืช. ื–ื” ืžืืคืฉืจ ืœืžืกืคืจ ืฉืจืฉื•ืจื™ื ืื• ืชื”ืœื™ื›ื™ื ืœื›ืชื•ื‘ ืœืื•ืชื• ืงื•ื‘ืฅ. ืื‘ืœ ื‘ืงื•ื“ ืฉืœื ื• ืื ื—ื ื• ืœื ืžืฉืชืžืฉื™ื ื‘ื“ื’ืœ ื”ื–ื”. ื ื•ื›ืœ ืœืจืื•ืช ื’ื•ื“ืœ ืงื•ื‘ืฅ ืฉื•ื ื” ื‘-lsof ืื—ืจื™ trunk ืจืง ืื ื ืคืชื— ืืช ื”ืงื•ื‘ืฅ ืœื›ืชื™ื‘ื”, ืžื” ืฉืื•ืžืจ ืฉื‘ืงื•ื“ ืฉืœื ื•, ื‘ืžืงื•ื

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

ื”ื•ืกืคืช ืชื’ื•ื‘ื”