N'otu oge, n'oge a na-agba ajụjụ ọnụ, a jụrụ m, gịnị ka ị ga-eme ma ọ bụrụ na ịchọta ọrụ adịghị arụ ọrụ n'ihi na diski ahụ agwụla ohere?
N'ezie, m zara ya na m ga-ahụ ihe ndị dị n'ebe a na, ọ bụrụ na ọ ga-ekwe omume, m ga-ehichapụ ebe ahụ.
Mgbe ahụ, onye na-agba ajụjụ jụrụ, gịnị ma ọ bụrụ na enweghị ohere efu na nkebi, ma ị hụghị faịlụ ọ bụla ga-eweghara ohere niile?
N'okwu a, ekwuru m na ị nwere ike ilele ndị na-akọwa faịlụ na-emeghe mgbe niile, dịka ọmụmaatụ na iwu lsof, ma ghọta ngwa nke weghaara ohere niile dịnụ, mgbe ahụ ị nwere ike ime ihe dịka ọnọdụ ahụ si dị, dabere ma ọ dị mkpa data ahụ. .
Onye na-agba ajụjụ ahụ kwụsịrị m n'okwu ikpeazụ, na-agbakwụnye na ajụjụ ya: "Ọ bụrụ na anyị achọghị data ahụ, ọ bụ naanị ihe ndekọ debug, mana ngwa ahụ anaghị arụ ọrụ n'ihi na ọ nweghị ike ide ihe nbibi"?
“Ọ dị mma,” ka m zaghachiri, “anyị nwere ike gbanyụọ debug na nhazi ngwa wee malitegharịa ya.”
Onye gbara ajụjụ ọnụ jụrụ, sị: "Ee e, anyị enweghị ike ịmalitegharị ngwa ahụ, anyị ka nwere data dị mkpa echekwara na ebe nchekwa, yana ndị ahịa dị mkpa jikọtara ya na ọrụ ahụ n'onwe ya, nke anyị enweghị ike ịmanye ijikọ ọzọ."
"Ọ dị mma," ka m kwuru, "ọ bụrụ na anyị enweghị ike ịmalitegharị ngwa ahụ ma data ahụ adịghị anyị mkpa, mgbe ahụ anyị nwere ike kpochapụ faịlụ a mepere emepe site na onye na-akọwa faịlụ, ọ bụrụgodị na anyị ahụghị ya na ls iwu. na sistemụ faịlụ."
Obi dị onye na-agba ajụjụ ọnụ ụtọ, ma ahụghị m.
Mgbe ahụ, echere m, gịnị kpatara onye na-anwale ihe ọmụma m adịghị gwuba miri emi? Ma gịnị ma ọ bụrụ na data dị mkpa mgbe niile? Kedu ihe ma ọ bụrụ na anyị enweghị ike ịmalitegharị usoro, usoro ahụ na-edekwa usoro faịlụ na nkebi na-enweghị ohere efu? Kedu ihe ma ọ bụrụ na anyị enweghị ike ịlafu ọ bụghị naanị data ederelarị, kamakwa data nke usoro a na-ede ma ọ bụ gbalịa ide?
Tuzik
Na mbido ọrụ m, agbalịrị m ịmepụta obere ngwa nke chọrọ iji chekwaa ozi onye ọrụ. Ma mgbe ahụ, echere m, kedu ka m ga-esi kwekọọ onye ọrụ na data ya. Dịka ọmụmaatụ, enwere m Ivanov Ivan Ivanovich, o nwekwara ozi ụfọdụ, ma olee otu m ga-esi mee enyi? Enwere m ike ịkọwa ozugbo na nkịta aha ya bụ "Tuzik" bụ nke Ivan. Ma gịnị ma ọ bụrụ na ọ gbanwere aha ya na kama Ivan na-aghọ, dịka ọmụmaatụ, Olya? Mgbe ahụ, ọ ga-apụta na Olya Ivanovna Ivanova anyị agaghị enwe nkịta, na Tuzik anyị ka ga-abụ nke Ivan na-adịghị adị. Ebe nchekwa data nke nyere onye ọrụ ọ bụla ihe nchọpụta pụrụ iche (ID) nyere aka dozie nsogbu a, na Tuzik m jikọtara na ID a, nke, n'ezie, bụ naanị nọmba nọmba. Ya mere, onye nwe ace nwere nọmba ID 2, na oge ụfọdụ Ivan nọ n'okpuru ID a, mgbe ahụ Olya ghọrọ n'okpuru otu ID ahụ. A kwụsịrị nsogbu nke ụmụ mmadụ na ịzụ anụmanụ.
Nkọwa faịlụ
Nsogbu nke faịlụ na mmemme na-arụ ọrụ na faịlụ a bụ ihe dịka nke nkịta na mmadụ anyị. Ka e were ya na m meghere faịlụ a na-akpọ ivan.txt wee malite ide okwu tuzik n'ime ya, mana enwere ike dee mkpụrụedemede mbụ "t" na faịlụ ahụ, onye ọzọ degharịrị aha faịlụ a, dịka ọmụmaatụ, olya.txt. Mana faịlụ ahụ ka dị otu, ma m ka chọrọ ịdekọ ace m na ya. Mgbe ọ bụla faịlụ mepere site na oku sistemụ
Na Linux, ọbá akwụkwọ libc na-emepe faịlụ nkọwa 3 maka ngwa ọ bụla na-agba ọsọ (usoro), akara 0,1,2. Enwere ike ịchọta ozi ndị ọzọ na njikọ
- A na-akpọ onye nkọwa faịlụ 0 STDIN ma jikọtara ya na ntinye ngwa
- A na-akpọ onye nkọwa faịlụ 1 STDOUT ma ngwa na-eji wepụta data, dị ka iwu ibipụta
- A na-akpọ onye nkọwa faịlụ 2 STDERR ma ngwa na-eji wepụta ozi mperi.
Ọ bụrụ na n'ime mmemme gị imepe faịlụ ọ bụla maka ịgụ ma ọ bụ ide ihe, ọ ga-abụ na ị ga-enweta ID mbụ n'efu na ọ ga-abụ nọmba 3.
Enwere ike ịlele ndepụta nkọwa faịlụ maka usoro ọ bụla ma ọ bụrụ na ị maara PID ya.
Dịka ọmụmaatụ, ka anyị mepee console bash wee lelee PID nke usoro anyị
[user@localhost ]$ echo $$
15771
Na console nke abụọ ka anyị gbaa ọsọ
[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
Ị nwere ike ileghara nọmba nkọwa faịlụ 255 n'enweghị nsogbu maka ebumnuche nke akụkọ a; Emeghere ya maka mkpa ya site na bash n'onwe ya, ọ bụghị site na ọba akwụkwọ jikọtara ya.
Ugbu a faịlụ nkọwa 3 niile jikọtara ya na ngwaọrụ ọnụ pseudo
[user@localhost ]$ echo "hello world" > /proc/15771/fd/0
Na nke mbụ console anyị ga-ahụ
[user@localhost ]$ hello world
Redirect na Pipe
Ị nwere ike ịkwụsị ngwa ngwa faịlụ nkọwa 3 ndị a na usoro ọ bụla, gụnyere na bash, dịka ọmụmaatụ site na ọkpọkọ na-ejikọta usoro abụọ, lee.
[user@localhost ]$ cat /dev/zero | sleep 10000
Ị nwere ike iji aka gị mee iwu a eriri -f ma hụ ihe na-eme n'ime, mana m ga-agwa gị nkenke.
Usoro bash nne na nna anyị na PID 15771 na-atụgharị iwu anyị wee ghọta kpọmkwem iwu ole anyị chọrọ ịgba ọsọ, n'ọnọdụ anyị enwere abụọ n'ime ha: pusi na ụra. Bash maara na ọ dị mkpa ịmepụta usoro ụmụaka abụọ, ma jikọta ha n'otu ọkpọkọ. Na mkpokọta, bash ga-achọ usoro ụmụaka 2 na otu ọkpọkọ.
Bash na-agba usoro oku tupu ịmepụta usoro ụmụaka
Maka usoro nne na nna, ọ dị ka enweelarị ọkpọkọ, mana enwebeghị usoro ụmụaka:
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
Mgbe ahụ na-eji usoro oku oku
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
Echefula na clone na-emechi usoro ahụ yana ndị na-akọwa faịlụ niile, n'ihi ya, ha ga-abụ otu na usoro nne na nna na ụmụaka. Ọrụ nke usoro nne na nna na PID 15771 bụ nyochaa usoro ụmụaka, ya mere ọ na-echere nzaghachi site na ụmụaka.
Ya mere, ọ dịghị mkpa ọkpọkọ, ọ na-emechikwa nkọwa faịlụ nọmba 3 na 4.
N'ime usoro bash nwata mbụ na PID 9004, oku sistemụ
N'ime usoro ụmụaka nke abụọ na PID 9005, bash na-eji dup2 gbanwee onye na-akọwa faịlụ STDIN nọmba 0. Ugbu a ihe ọ bụla nke abụọ bash anyị na PID 9005 ga-agụ ga-agụ site na ọkpọkọ.
Mgbe nke a gasịrị, a na-emechikwa ndị na-akọwa faịlụ nke nọmba 3 na 4 na usoro ụmụaka, ebe ọ bụ na anaghịzi eji ha eme ihe.
Ana m kpachara anya na-eleghara onye na-akọwa faịlụ 255; a na-eji ya eme ihe maka ime ime site na bash n'onwe ya, a ga-emechikwa ya na usoro ụmụaka.
Na-esote, na usoro nwata mbụ na PID 9004, bash na-amalite iji oku usoro
N'ime usoro ụmụaka nke abụọ na PID 9005, bash na-arụ ọrụ nke abụọ anyị akọwapụtara, n'ọnọdụ anyị / usr / bin/sleep.
Oku sistemu exec anaghị emechi aka faịlụ ọ gwụla ma ejiri ọkọlọtọ O_CLOEXEC meghere ha n'oge akpọrọ oku mepere emepe. N'ọnọdụ anyị, mgbe ịmalitechara faịlụ ndị a na-arụ ọrụ, a ga-echekwa ndị na-akọwa faịlụ niile dị ugbu a.
Lelee na console:
[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
Dị ka ị pụrụ ịhụ, ọnụ ọgụgụ pụrụ iche nke ọkpọkọ anyị bụ otu na usoro abụọ ahụ. Ya mere, anyị nwere njikọ n'etiti usoro abụọ dị iche iche na otu nne na nna.
Maka ndị na-amaghị nke ọma na oku usoro nke bash na-eji, ana m akwado ịkwado iwu site na strace na ịhụ ihe na-eme n'ime, dịka ọmụmaatụ dị ka nke a:
strace -s 1024 -f bash -c "ls | grep hello"
Ka anyị laghachi na nsogbu anyị na obere ohere diski na-agbalị ichekwa data na-enweghị malitegharịa usoro. Ka anyị dee obere mmemme nke ga-ede ihe dịka 1 megabyte kwa sekọnd na diski. Ọzọkwa, ọ bụrụ n'ihi ihe ụfọdụ anyị enweghị ike ide data na diski ahụ, anyị ga-eleghara nke a anya ma gbalịa ide data ọzọ n'ime sekọnd. N'ihe atụ m na-eji Python, ị nwere ike iji asụsụ mmemme ọ bụla ọzọ.
[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
Ka anyị mee mmemme wee lelee ndị na-akọwa faịlụ
[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
Dị ka ị pụrụ ịhụ, anyị nwere anyị 3 ọkọlọtọ faịlụ nkọwa na otu ọzọ na anyị meghere. Ka anyị lelee nha faịlụ:
[user@localhost ]$ ls -lah 123.txt
-rw-rw-r-- 1 user user 117M Oct 7 16:30 123.txt
A na-ede data a, anyị na-agbalị ịgbanwe ikike dị na faịlụ ahụ:
[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
Anyị na-ahụ na a ka na-ede data ahụ, n'agbanyeghị na onye ọrụ anyị enweghị ikike ide na faịlụ ahụ. Ka anyị gbalịa wepụ ya:
[user@localhost ]$ sudo rm 123.txt
[user@localhost ]$ ls 123.txt
ls: cannot access 123.txt: No such file or directory
Ebee ka edere data? Edekwara ha ma ọlị? Anyị na-enyocha:
[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)
Ee, onye na-akọwa faịlụ anyị ka dị, anyị nwere ike na-emeso onye na-akọwa faịlụ a dị ka faịlụ ochie anyị, anyị nwere ike ịgụ, kpochapụ ma detuo ya.
Ka anyị lee nha faịlụ ahụ:
[user@localhost ]$ lsof | grep 123.txt
python 31083 user 3w REG 8,5 19923457 2621522 /home/user/123.txt
Ogo faịlụ ahụ bụ 19923457. Ka anyị gbalịa ikpochapụ faịlụ ahụ:
[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
Dịka ị pụrụ ịhụ, nha faịlụ na-abawanye ma ogwe anyị anaghị arụ ọrụ. Ka anyị lelee akwụkwọ ịkpọ oku sistemụ
with open("123.txt", "w") as f:
anyị ga-etinye
with open("123.txt", "a") as f:
Jiri ọkọlọtọ “w” na-elele
[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
ya na ọkọlọtọ "a".
[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
Ịmepụta usoro na-agba ọsọ ugbua
Ọtụtụ mgbe, ndị mmemme, mgbe ha na-emepụta ma na-anwale mmemme, na-eji ihe nbibi (dịka ọmụmaatụ GDB) ma ọ bụ ọkwa dị iche iche nke ịbanye na ngwa ahụ. Linux na-enye ikike ịde ma gbanwee mmemme na-agba ọsọ, dịka ọmụmaatụ, gbanwee ụkpụrụ nke mgbanwe, tọọ ebe nkwụsịtụ, wdg, wdg.
Ịlaghachi na ajụjụ mbụ gbasara enweghị ohere diski iji dee faịlụ, ka anyị gbalịa ime ka nsogbu ahụ pụta.
Ka anyị mepụta faịlụ maka nkebi anyị, nke anyị ga-ebuli dị ka diski dị iche:
[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 ~]$
Ka anyị mepụta usoro faịlụ:
[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 ~]$
Mepụta usoro faịlụ:
[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
Anyị na onye nwe anyị mepụta ndekọ aha:
[user@localhost ~]$ sudo mkdir /mnt/logs
[user@localhost ~]$ sudo chown user: /mnt/logs
Ka anyị mepee faịlụ maka ide naanị na mmemme anyị:
with open("/mnt/logs/123.txt", "w") as f:
Mwepụta
[user@localhost ]$ python openforwrite.py
Anyị na-echere sekọnd ole na ole
[user@localhost ~]$ df -h | grep mnt
/dev/loop0 8.7M 8.0M 0 100% /mnt
Ya mere, anyị nwere nsogbu ahụ akọwara ná mmalite isiokwu a. Oghere efu 0, 100% nwere.
Anyị na-echeta na dịka ọnọdụ nke ọrụ ahụ si dị, anyị na-agbalị ịdekọ data dị oke mkpa nke enweghị ike ịla n'iyi. Ma n'otu oge ahụ, anyị kwesịrị idozi ọrụ ahụ na-enweghị ịmalitegharị usoro ahụ.
Ka anyị kwuo na anyị ka nwere ohere diski, mana na nkebi dị iche, dịka ọmụmaatụ n'ime / ụlọ.
Ka anyị gbalịa ịmegharị koodu anyị.
Ka anyị leba anya na PID nke usoro anyị, nke ririla ohere diski niile:
[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
Jikọọ na usoro site na gdb
[user@localhost ~]$ gdb -p 10078
...
(gdb)
Ka anyị lee nkọwa nkọwa faịlụ mepere emepe:
(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
Anyị na-elele ozi gbasara nọmba nkọwa faịlụ 3, nke masịrị anyị
(gdb) shell cat /proc/10078/fdinfo/3
pos: 8189952
flags: 0100001
mnt_id: 482
N'iburu n'uche ihe usoro a na-akpọ Python na-eme (lee n'elu ebe anyị na-agba ọsọ wee chọta oku mepere emepe), mgbe anyị na-edozi koodu anyị imeghe faịlụ, anyị na-eme otu ihe ahụ n'onwe anyị maka usoro anyị, mana anyị chọrọ O_WRONLY|O_CREAT| O_TRUNC ibe n'ibe jiri uru ọnụọgụ dochie. Iji mee nke a, mepee isi mmalite kernel, dịka ọmụmaatụ
# kọwapụta O_ỌZỌ 00000001
# kọwapụta O_CREAT 00000100
# kọwapụta O_TRUNC 00001000
Anyị na-ejikọta ụkpụrụ niile n'ime otu, anyị na-enweta 00001101
Anyị na-agba ọsọ anyị oku site gdb
(gdb) call open("/home/user/123.txt", 00001101,0666)
$1 = 4
Yabụ na anyị nwetara onye nkọwa faịlụ ọhụrụ nwere nọmba 4 yana faịlụ mepere emepe ọhụrụ na nkebi ọzọ, anyị na-elele:
(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
Anyị na-echeta ihe atụ na ọkpọkọ - ka bash si agbanwe nkọwa faịlụ, ma anyị amụtalarị oku usoro dup2.
Anyị na-agbalị iji ọzọ dochie otu faịlụ nkọwa
(gdb) call dup2(4,3)
$2 = 3
Anyị na-elele:
(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
Anyị na-emechi onye na-akọwa faịlụ 4, ebe ọ bụ na anyị achọghị ya:
(gdb) call close (4)
$1 = 0
Wepu 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
Na-enyocha faịlụ ọhụrụ:
[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
Dịka ị na-ahụ, edere data ahụ na faịlụ ọhụrụ, ka anyị lelee nke ochie:
[user@localhost ~]$ ls -lah /mnt/logs/123.txt
-rw-rw-r-- 1 user user 7.9M Oct 8 11:08 /mnt/logs/123.txt
Enweghị data efunahụ, ngwa ahụ na-arụ ọrụ, edere ndekọ na ebe ọhụrụ.
Ka anyị gbakọọ ọrụ ahụ ntakịrị
Ka anyị were ya na data ahụ dị anyị mkpa, mana anyị enweghị ohere diski na nkebi ọ bụla ma anyị enweghị ike ijikọ diski ahụ.
Ihe anyị nwere ike ime bụ redirect data anyị ebe, dịka ọmụmaatụ na ọkpọkọ, na n'aka redirect data site na ọkpọkọ gaa na netwọk site na ụfọdụ mmemme, dịka ọmụmaatụ netcat.
Anyị nwere ike ịmepụta ọkpọkọ aha ya na iwu mkfifo. Ọ ga-emepụta faịlụ pseudo na sistemụ faịlụ ọbụlagodi na enweghị ohere efu na ya.
Malitegharịa ngwa ahụ wee lelee:
[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
Enweghị ohere diski, mana anyị mepụtara ọkpọkọ aha ya nke ọma n'ebe ahụ:
[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
Ugbu a, anyị kwesịrị kechie data niile na-abanye na ọkpọkọ a na sava ọzọ site na netwọkụ ahụ; otu netcat dabara maka nke a.
Na ihe nkesa remote-server.example.com anyị na-ebube
[user@localhost ~]$ nc -l 7777 > 123.txt
Na sava anyị nwere nsogbu anyị na-ebunye na ọdụ dị iche
[user@localhost ~]$ nc remote-server.example.com 7777 < /mnt/logs/megapipe
Ugbu a, data niile na-agwụ na ọkpọkọ ga-akpaghị aka na stdin na netcat, nke ga-eziga ya na netwọk na ọdụ ụgbọ mmiri 7777.
Naanị ihe anyị ga-eme bụ ibido ide data anyị n'ime ọkpọkọ a aha ya bụ.
Anyị enweelarị ngwa na-arụ ọrụ:
[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
N'ime ọkọlọtọ niile, naanị O_WRONLY anyị chọrọ ebe faịlụ ahụ adịlarị ma anyị achọghị ikpochapụ ya.
[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
Na-elele ihe nkesa dịpụrụ adịpụ remote-server.example.com
[user@localhost ~]$ ls -lah 123.txt
-rw-rw-r-- 1 user user 38M Oct 8 14:21 123.txt
Data na-abịa, anyị na-elele ihe nkesa nsogbu
[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
A na-echekwa data ahụ, edozi nsogbu ahụ.
Ana m eji ohere a kelee ndị otu m si Degiro.
Gee ntị na pọdkastị redio-T.
O diri mmadu niile.
Dị ka ihe omume ụlọ, m na-atụ aro ka ị na-eche banyere ihe ga-abụ na usoro faịlụ descriptors cat na ụra ma ọ bụrụ na ị na-agba ọsọ na-esonụ iwu:
[user@localhost ~]$ cat /dev/zero 2>/dev/null| sleep 10000
isi: www.habr.com