Tlhaloso ea faele ho Linux e nang le mehlala

Ka lekhetlo le leng, nakong ea lipuisano, ke ile ka botsoa, ​​​​u tla etsa'ng haeba u fumana tšebeletso e sa sebetse ka lebaka la hore disk e feletsoe ke sebaka?

Ha e le hantle, ke ile ka araba ka hore ke tla bona se neng se tšoaretsoe sebaka sena, 'me haeba ho khoneha, ke tla hloekisa sebaka seo.
Eaba motho ea botsang lipotso o botsa, ho thoe'ng haeba ho se na sebaka sa mahala ho karohano, empa hape ha u bone lifaele tse ka nkang sebaka sohle?

Ho sena ke boletse hore u ka lula u sheba litlhaloso tsa lifaele tse bulehileng, mohlala ka taelo ea lsof, 'me u utloisise hore na ke kopo efe e nkileng sebaka sohle se teng, ebe u ka nka khato ho ea ka maemo, ho itšetlehile ka hore na data e hlokahala. .

Motho ea botsang lipotso o ile a nkhathatsa ka lentsoe la ho qetela, a phaella potsong ea hae: "A re re ha re hloke boitsebiso, ke feela "debug log", empa kopo ha e sebetse hobane e ke ke ea ngola "debug"?

"Ho lokile," ke ile ka araba, "re ka tima debug ho tlhophiso ea kopo ebe re e qala hape."
Motho ea botsang lipotso o ile a hana: "Che, re ke ke ra qala kopo hape, re ntse re e-na le lintlha tsa bohlokoa tse bolokiloeng mohopolong, 'me bareki ba bohlokoa ba hokahane le ts'ebeletso ka boeona, eo re ke keng ra e qobella ho e hokahanya hape."

"Ho lokile," ke ile ka re, "haeba re sa khone ho qala kopo hape mme data ha e bohlokoa ho rona, re ka hlakola faele ena e bulehileng ka tlhaloso ea faele, leha re sa e bone ho ls taelo. tsamaisong ea lifaele."

Motho ea neng a botsa lipotso o ne a thabile, empa ha kea ka ka thaba.

Eaba kea nahana, ke hobane'ng ha motho ea lekang tsebo ea ka a sa cheke ho teba? Empa ho thoe'ng haeba data e le ea bohlokoa ka mor'a tsohle? Ho thoe'ng haeba re sa khone ho qala ts'ebetso hape, mme ts'ebetso e ngolla sistimi ea faele ka karohano e se nang sebaka sa mahala? Ho thoe'ng haeba re ke ke ra lahleheloa ke feela data e seng e ngotsoe, empa hape le lintlha tseo mokhoa ona o li ngolang kapa o lekang ho li ngola?

Tuzik

Qalong ea mosebetsi oa ka, ke ile ka leka ho etsa kopo e nyenyane e neng e hloka ho boloka boitsebiso ba mosebedisi. Eaba ke nahana, nka bapisa mosebelisi joang le data ea hae. Ka mohlala, ke na le Ivanov Ivan Ivanovich, 'me o na le boitsebiso bo itseng, empa nka etsa setsoalle le bona joang? Nka supa ka ho toba hore ntja e bitsoang "Tuzik" ke ea Ivan enoa. Empa ho thoe'ng haeba a fetola lebitso la hae 'me ho e-na le Ivan e ba, mohlala, Olya? Joale ho tla bonahala hore Olya Ivanovna Ivanova oa rona ha a sa tla hlola a e-ba le ntja, 'me Tuzik ea rona e ntse e tla ba ea Ivan ea seng a le teng. Database e ileng ea fa mosebelisi e mong le e mong sekhetho se ikhethang (ID) e thusitse ho rarolla bothata bona, 'me Tuzik ea ka e ne e tlameletsoe ho ID ena, eo, ha e le hantle, e neng e le nomoro ea serial feela. Ka hona, mong'a tuzik o ne a e-na le nomoro ea 2 ea ID, 'me ka nako e' ngoe Ivan o ne a le tlas'a ID ena, ebe Olya ea e-ba tlas'a ID ena. Bothata ba botho le leruo la liphoofolo bo ile ba rarolloa ka mokhoa o hlakileng.

Tlhaloso ea faele

Bothata ba faele le lenaneo le sebetsang le faele ena li batla li tšoana le tsa ntja ea rona le motho. A re re ke ile ka bula faele e bitsoang ivan.txt 'me ka qala ho ngola lentsoe tuzik ho eona, empa ke khona ho ngola lengolo la pele "t" faeleng,' me faele ena e ile ea rehoa ke motho e mong, ka mohlala, ho olya.txt. Empa faele e ntse e tšoana, 'me ke ntse ke batla ho rekota ace ea ka ho eona. Nako le nako ha faele e buloa ka mohala oa sistimi bulehetse ka puo efe kapa efe ea lenaneo ke fumana ID e ikhethang e nkhombang faeleng, ID ena ke tlhaloso ea faele. Ebile ha ho na taba hore na file ena o e etsa joang, e ka hlakoloa, ea rehoa, mong'a eona a ka fetoloa, kapa litokelo tsa ho bala le ho ngola li ka tlosoa, ke ntse ke tla ba le phihlello. ho eona, hobane ka nako ea ho bula faele, ke ne ke e-na le litokelo tsa ho e bala le/kapa ho e ngola ’me ka khona ho qala ho sebetsa le eona, ho bolelang hore ke tlameha ho tsoela pele ho etsa joalo.

Ho Linux, laebrari ea libc e bula lifaele tse 3 tse hlalosang bakeng sa ts'ebeliso e 'ngoe le e' ngoe e sebetsang (ts'ebetso), e baloang 0,1,2. Lintlha tse ling li ka fumanoa lihokelong monna stdio и monna stdout

  • Tlhaloso ea faele 0 e bitsoa STDIN 'me e amahanngoa le tlhahiso ea kopo
  • Tlhaloso ea faele 1 e bitsoa STDOUT 'me e sebelisoa ke lits'ebetso ho hlahisa lintlha, joalo ka litaelo tsa khatiso
  • Tlhaloso ea faele 2 e bitsoa STDERR 'me e sebelisoa ke lits'ebetso ho hlahisa melaetsa ea liphoso.

Haeba lenaneong la hau u bula faele efe kapa efe bakeng sa ho bala kapa ho ngola, mohlomong u tla fumana ID ea pele ea mahala mme e tla ba nomoro ea 3.

Lethathamo la litlhaloso tsa faele le ka bonoa bakeng sa ts'ebetso efe kapa efe haeba u tseba PID ea eona.

Mohlala, ha re bule bash console mme re shebe PID ea ts'ebetso ea rona

[user@localhost ]$ echo $$
15771

Ho console ea bobeli ha re mathe

[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

O ka hlokomoloha ka mokhoa o sireletsehileng nomoro e hlalosang faele ea 255 molemong oa sengoloa sena; e ile ea buloa bakeng sa litlhoko tsa eona ka bash ka boeona, eseng ka laeborari e hokahaneng.

Hona joale lifaele tsohle tse 3 tse hlalosang li amahanngoa le sesebelisoa sa pseudo terminal /dev/pts, empa re ntse re ka li qhekella, ka mohlala, ra li tsamaisa ka khomphutha ea bobeli

[user@localhost ]$ echo "hello world" > /proc/15771/fd/0

'Me ho console ea pele re tla bona

[user@localhost ]$ hello world

Tsamaisa hape le Pipe

O ka fetisa lifaele tsena tse hlalosang 3 habonolo ts'ebetsong efe kapa efe, ho kenyeletsoa le bash, mohlala ka phala e hokahanyang lits'ebetso tse peli, bona.

[user@localhost ]$ cat /dev/zero | sleep 10000

U ka tsamaisa taelo ena ka bouena lesela -f mme le bone se etsahalang ka hare, empa ke tla le bolella ka bokgutshwane.

Ts'ebetso ea rona ea bash ea motsoali ka PID 15771 e fetisa taelo ea rona mme e utloisisa hantle hore na re batla ho tsamaisa litaelo tse kae, molemong oa rona ho na le tse peli tsa tsona: katse le boroko. Bash oa tseba hore e hloka ho theha lits'ebetso tse peli tsa bana, le ho li kopanya ho phala e le 'ngoe. Ka kakaretso, bash e tla hloka lits'ebetso tse 2 tsa bana le phala e le 'ngoe.

Bash o tsamaisa mohala oa sistimi pele a theha lits'ebetso tsa bana phala mme e fumana ditlhaloso tse ntjha tsa difaele ho buffer ya nakwana ya phaephe, empa buffer ena ha e so hokela ditshebetso tsa rona tsa bana ba babedi.

Bakeng sa ts'ebetso ea motsoali, ho bonahala eka ho se ho na le phala, empa ha ho na mekhoa ea bana:

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

Ebe u sebelisa mohala oa sistimi clone bash e theha lits'ebetso tse peli tsa bana, 'me lits'ebetso tsa rona tse tharo li tla shebahala tjena:

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

U se ke ua lebala hore clone e kopanya ts'ebetso hammoho le litlhaloso tsohle tsa lifaele, kahoo li tla tšoana ts'ebetsong ea motsoali le ho bana. Mosebetsi oa ts'ebetso ea motsoali ka PID 15771 ke ho beha leihlo lits'ebetso tsa ngoana, ka hona e emela karabo ho tsoa ho bana.

Ka hona, ha e hloke phala, 'me e koala litlhaloso tsa faele tse nang le nomoro ea 3 le ea 4.

Ts'ebetsong ea bash ea bana ba pele ka PID 9004, mohala oa sistimi dup2, e fetola tlhaloso ea faele ea rona ea STDOUT nomoro ea 1 ho tlhaloso ea faele e bontšang phala, molemong oa rona ke nomoro ea 3. Ka hona, ntho e 'ngoe le e' ngoe eo ngoana oa pele a sebetsanang le PID 9004 a e ngollang STDOUT e tla qetella e le ka har'a buffer ea pipe.

Ts'ebetsong ea ngoana oa bobeli le PID 9005, bash e sebelisa dup2 ho fetola tlhaloso ea faele ea STDIN nomoro 0. Hona joale ntho e 'ngoe le e' ngoe eo bash ea rona ea bobeli e nang le PID 9005 e tla e bala e tla baloa ho tloha phala.

Ka mor'a sena, litlhaloso tsa lifaele tse nang le nomoro ea 3 le 4 li boetse li koetsoe lits'ebetsong tsa bana, kaha ha li sa sebelisoa.

Ke hlokomoloha ka boomo descriptor 255; e sebelisetsoa merero ea ka hare ke bash ka boeona 'me e tla koaloa le lits'ebetsong tsa bana.

E latelang, ts'ebetsong ea ngoana oa pele ka PID 9004, bash e qala ho sebelisa mohala oa sistimi phetha faele e sebetsang eo re e boletseng molaong oa taelo, molemong oa rona ke /usr/bin/cat.

Ts'ebetsong ea ngoana oa bobeli ka PID 9005, bash e tsamaisa ea bobeli e phethisoang eo re e boletseng, molemong oa rona /usr/bin/sleep.

Exec system call ha e koale lithupa tsa faele ntle le haeba li butsoe ka folakha ea O_CLOEXEC nakong eo mohala o bulehileng o neng o etsoa. Tabeng ea rona, ka mor'a ho qala lifaele tse sebetsang, litlhaloso tsohle tsa hona joale tsa faele li tla bolokeha.

Sheba ka har'a 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

Joalokaha u ka bona, palo e ikhethang ea pipe ea rona e tšoana le mekhoa e 'meli. Kahoo re na le kamano pakeng tsa mekhoa e 'meli e fapaneng le motsoali a le mong.

Bakeng sa ba sa tloaelaneng le mehala ea sistimi eo bash e e sebelisang, ke khothaletsa haholo ho tsamaisa litaelo ka marang-rang le ho bona se etsahalang kahare, mohlala o kang ona:

strace -s 1024 -f bash -c "ls | grep hello"

Ha re khutleleng bothateng ba rona ka sebaka se tlase sa disk mme re leke ho boloka data ntle le ho qala ts'ebetso hape. Ha re ngole lenaneo le lenyenyane le tla ngola hoo e ka bang megabyte e le 1 motsotsoana ho disk. Ho feta moo, haeba ka lebaka le itseng re sa khone ho ngola data ho disk, re tla hlokomoloha sena ebe re leka ho ngola lintlha hape ka motsotsoana. Mohlala oo ke o sebelisang Python, o ka sebelisa puo efe kapa efe ea lenaneo.

[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

Ha re tsamaiseng lenaneo mme re shebe litlhaloso tsa lifaele

[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

Joalokaha u bona, re na le litlhaloso tsa rona tse 3 tse tloaelehileng tsa faele le e 'ngoe hape eo re e butseng. Ha re hlahlobe boholo ba faele:

[user@localhost ]$ ls -lah 123.txt 
-rw-rw-r-- 1 user user 117M Oct  7 16:30 123.txt

Lintlha li ntse li ngoloa, re leka ho fetola litumello faeleng:

[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

Rea bona hore data e ntse e ngoloa, leha mosebelisi oa rona a se na tumello ea ho ngolla faele. Ha re leke ho e tlosa:

[user@localhost ]$ sudo rm 123.txt 
[user@localhost ]$ ls 123.txt
ls: cannot access 123.txt: No such file or directory

Lintlha li ngotsoe hokae? Hona na li ngotsoe ho hang? Re hlahloba:

[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)

E, tlhaloso ea faele ea rona e ntse e le teng 'me re ka tšoara tlhaloso ea faele ena joaloka faele ea rona ea khale, re ka e bala, ra e hlakola le ho e kopitsa.

Ha re shebeng boholo ba faele:

[user@localhost ]$ lsof | grep 123.txt
python    31083             user    3w      REG                8,5   19923457   2621522 /home/user/123.txt

Boholo ba faele ke 19923457. Ha re leke ho hlakola faele:

[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

Joalokaha u ka bona, boholo ba faele bo ntse bo eketseha mme kutu ea rona ha ea ka ea sebetsa. Ha re shebeng litokomane tsa mohala oa sistimi bulehetse. Haeba re sebelisa folakha ea O_APPEND ha re bula faele, joale ka mongolo o mong le o mong, mokhoa oa ho sebetsa o hlahloba boholo ba faele mme o ngola lintlha ho isa qetellong ea faele, 'me o etsa sena ka atomo. Sena se lumella likhoele tse ngata kapa lits'ebetso ho ngolla faeleng e le 'ngoe. Empa khoutu ea rona ha re sebelise folakha ena. Re ka bona boholo bo fapaneng ba faele ka lsof kamora kutu ha feela re bula faele bakeng sa mongolo o eketsehileng, ho bolelang hore khoutu ea rona

with open("123.txt", "w") as f:

re tlameha ho beha

with open("123.txt", "a") as f:

Ho sheba ka "w" folakha

[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

le ka "folakha".

[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

Lenaneo le se le ntse le sebetsa

Hangata baetsi ba mananeo, ha ba theha le ho lekola mananeo, ba sebelisa li-debugger (mohlala, GDB) kapa maemo a fapaneng a ho rema lits'ebetso. Linux e fana ka bokhoni ba ho ngola le ho fetola lenaneo le seng le ntse le sebetsa, mohlala, ho fetola boleng ba mefuta-futa, ho beha sebaka sa phomolo, joalo-joalo, joalo-joalo.

Ha re khutlela potsong ea pele mabapi le sebaka se lekaneng sa disk ho ngola faele, a re lekeng ho etsisa bothata.

Ha re theheng faele bakeng sa karohano ea rona, eo re tla e beha joalo ka disk e arohaneng:

[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 ~]$

Ha re theheng sistimi ea faele:

[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 ~]$

Kenya sistimi ea faele:

[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

Re theha bukana le mong'a rona:

[user@localhost ~]$ sudo mkdir /mnt/logs
[user@localhost ~]$ sudo chown user: /mnt/logs

Ha re bule faele bakeng sa ho ngola feela lenaneong la rona:

with open("/mnt/logs/123.txt", "w") as f:

Qala

[user@localhost ]$ python openforwrite.py 

Re ema metsotsoana e seng mekae

[user@localhost ~]$ df -h | grep mnt
/dev/loop0      8.7M  8.0M     0 100% /mnt

Kahoo, re na le bothata bo hlalositsoeng qalong ea sehlooho sena. Sebaka sa mahala 0, 100% se tšoaretsoe.

Re hopola hore ho ea ka maemo a mosebetsi, re leka ho ngola lintlha tsa bohlokoa haholo tse ke keng tsa lahleha. Mme ka nako e ts'oanang, re hloka ho lokisa ts'ebeletso ntle le ho qala ts'ebetso hape.

Ha re re re ntse re na le sebaka sa disk, empa ka karohano e fapaneng, mohlala ho /home.

Ha re leke ho "reprogram hang-hang" khoutu ea rona.

Ha re shebeng PID ea ts'ebetso ea rona, e jeleng sebaka sohle sa disk:

[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

Hokela ts'ebetsong ka gdb

[user@localhost ~]$ gdb -p 10078
...
(gdb) 

Ha re shebeng litlhaloso tsa lifaele tse bulehileng:

(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

Re sheba tlhahisoleseding e mabapi le nomoro ea tlhaloso ea faele ea 3, e re thahasellang

(gdb) shell cat /proc/10078/fdinfo/3
pos:    8189952
flags:  0100001
mnt_id: 482

Ha re ntse re hopola hore na Python e bitsa tsamaiso efe (bona ka holimo moo re ileng ra matha teng 'me ra fumana mohala o bulehileng), ha re sebetsana le khoutu ea rona ho bula faele, re etsa se tšoanang molemong oa ts'ebetso ea rona, empa re hloka O_WRONLY|O_CREAT| O_TRUNC li-bits li nkela sebaka ka boleng ba linomoro. Ho etsa sena, bula mehloli ea kernel, mohlala mona le ho sheba hore na ke lifolakha life tse ikarabellang bakeng sa eng

#define O_WRONLY 00000001
# hlalosa O_CREAT 00000100
#define O_TRUNC 00001000

Re kopanya litekanyetso tsohle ho e le 'ngoe, re fumana 00001101

Re tsamaisa mohala oa rona ho tsoa ho gdb

(gdb) call open("/home/user/123.txt", 00001101,0666)
$1 = 4

Kahoo re na le tlhaloso e ncha ea faele e nang le nomoro ea 4 le faele e ncha e bulehileng karolong e 'ngoe, re hlahloba:

(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

Re hopola mohlala ka pipe - kamoo bash e fetolang litlhaloso tsa faele, 'me re se re ithutile pitso ea tsamaiso ea dup2.

Re leka ho fetola tlhaloso ea faele ka e 'ngoe

(gdb) call dup2(4,3)
$2 = 3

Re hlahloba:

(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

Re koala tlhaloso ea faele 4, kaha ha re e hloke:

(gdb) call close (4)
$1 = 0

Ebe u tsoa 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

Ho hlahloba faele e ncha:

[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

Joalokaha u bona, data e ngoletsoe faele e ncha, a re hlahlobeng ea khale:

[user@localhost ~]$ ls -lah /mnt/logs/123.txt 
-rw-rw-r-- 1 user user 7.9M Oct  8 11:08 /mnt/logs/123.txt

Ha ho na data e lahlehileng, kopo e sebetsa, li-log li ngotsoe sebakeng se secha.

Ha re thatafatse mosebetsi hanyane

A re ke re nahane hore data ke ea bohlokoa ho rona, empa ha re na sebaka sa disk leha e le efe ea likaroloana 'me re ke ke ra kopanya disk.

Seo re ka se etsang ke ho tsamaisa lintlha tsa rona kae-kae, mohlala ho pipe, 'me re fetisetsa data ho tloha pipe ho ea ho marang-rang ka lenaneo le itseng, mohlala netcat.
Re ka etsa phala e nang le lebitso ka taelo ea mkfifo. E tla theha faele ea pseudo ho sistimi ea faele leha ho se na sebaka sa mahala ho eona.

Qala kopo bocha 'me u hlahlobe:

[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

Ha ho na sebaka sa disk, empa re atlehile ho theha pipe e nang le lebitso moo:

[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

Hona joale re hloka ho phuthela ka mokhoa o itseng data eohle e kenang phala ena ho seva se seng ka marang-rang; netcat e tšoanang e loketse sena.

Ho seva remote-server.example.com re matha

[user@localhost ~]$ nc -l 7777 > 123.txt 

Ho seva sa rona se nang le mathata re qala ho terminal e arohaneng

[user@localhost ~]$ nc remote-server.example.com 7777 < /mnt/logs/megapipe 

Hona joale lintlha tsohle tse qetellang ka pipe li tla ea ho stdin ho netcat, e tla e romella marang-rang ho port 7777.

Seo re lokelang ho se etsa feela ke ho qala ho ngola lintlha tsa rona ho pipe ena e bitsoang.

Re se re ntse re e-na le sesebelisoa se sebetsang:

[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

Har'a lifolakha tsohle, re hloka feela O_WRONLY kaha faele e se e ntse e le teng, 'me ha ho hlokahale hore re e hlakole.

[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

Ho hlahloba seva e hole-server.example.com

[user@localhost ~]$ ls -lah 123.txt 
-rw-rw-r-- 1 user user 38M Oct  8 14:21 123.txt

Lintlha lia tla, re hlahloba seva ea bothata

[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

Lintlha li bolokiloe, bothata bo rarollotsoe.

Ke nka monyetla ona ho lumelisa basebetsi-'moho le 'na ba Degiro.
Mamela li-podcasts tsa Radio-T.

Mahlohonolo.

Joalo ka mosebetsi oa sekolo o etsetsoang hae, ke khothaletsa hore u nahane ka se tla ba teng ts'ebetsong ea li-descriptors cat le ho robala haeba u tsamaisa taelo e latelang:

[user@localhost ~]$ cat /dev/zero 2>/dev/null| sleep 10000

Source: www.habr.com

Eketsa ka tlhaloso