Tải bản đầy đủ - 0 (trang)
5 Processes, Descriptors, and Sockets

5 Processes, Descriptors, and Sockets

Tải bản đầy đủ - 0trang

Thefirstargumenttoafunction

implementingasystemcallisalwaysp,a

pointertotheprocstructureofthecalling

process.Theprocstructurerepresentsthe

kernel'snotionofaprocess.Withinthe

procstructure,p_fdpointstoafiledesc

structure,whichmanagesthedescriptor

tablepointedtobyfd_ofiles.The

descriptortableisdynamicallysizedand

consistsofanarrayofpointerstofile

structures.Eachfilestructuredescribesa

singleopenfileandcanbesharedbetween

multipleprocesses.

Onlyasinglefilestructureisshownin

Figure15.13.Itisaccessedbyp->p_fd>fd_ofiles[fd].Withinthefilestructure,

twomembersareofinteresttous:f_ops







andf_data.TheimplementationofI/O

systemcallssuchasreadandwritevaries

accordingtowhattypeofI/Oobjectis

associatedwithadescriptor.f_opspoints

toafileopsstructurecontainingalistof

functionpointersthatimplementtheread,

write,ioctl,select,andclosesystemcalls

fortheassociatedI/Oobject.Figure15.13

showsf_opspointingtoaglobalfileops

structure,socketops,whichcontains

pointerstothefunctionsforsockets.

f_datapointstoprivatedatausedbythe

associatedI/Oobject.Forsockets,f_data

pointstothesocketstructureassociated

withthedescriptor.Finally,weseethat

so_protointhesocketstructurepointsto

theprotoswstructurefortheprotocol

selectedwhenthesocketiscreated.Recall

thateachprotoswstructureissharedby

allsocketsassociatedwiththeprotocol.

Wenowproceedtodiscussthesystem

calls.



Team-Fly









Top



Team-Fly



TCP/IPIllustrated,Volume2:TheImplementationByGaryR.Wrig

TableofContents



Chapter15.SocketLayer



15.6socketSystemCall



Thesocketsystemcallcreatesanewsocketandassoc

domain,type,andprotocolargumentsspecifiedbythe

15.14)allocatesanewdescriptor,whichidentifiesthe

thedescriptortotheprocess.



Figure15.14.sockets



42-55



Beforeeachsystemcallastructureisdefinedtodescri

tothekernel.Inthiscase,theargumentsarepassedw

socket-layersystemcallshavethreearguments:p,ap

process;uap,apointertoastructurecontainingthear

systemcall;andretval,avalueresultargumentthatpo

call.Normally,weignorethepandretvalargumentsa

pointedtobyuapastheargumentstothesystemcall.

56-60



fallocallocatesanewfilestructureandslotinthefd_o

newstructureandfdistheindexofthestructureinth

structureforreadandwriteaccessandmarksitasas



sharedbyallsockets,isattachedtothefilestructureb

initializedatcompiletimeasshowninFigure15.15



Figure15.15.socketops:theglobalfile



60-69



socreateallocatesandinitializesasocketstructure.Ifs

error,thefilestructureisreleased,andthedescriptors

settopointtothesocketstructureandestablishesthe

thesocket.fdisreturnedtotheprocessthrough*retv

returnedbysocreate.



socreateFunction



Mostsocketsystemcallsaredividedintoatleasttwof

socreateare.Thefirstfunctionretrievesfromtheproc

soxxxfunctiontodothework,andthenreturnsanyre

thesecondfunctioncanbecalleddirectlybykernel-ba

socreateisshowninFigure15.16.



Figure15.16.socreate



43-52



Thefourargumentstosocreateare:dom,therequeste

whichapointertoanewsocketstructureisreturned;

SOCK_STREAM);andproto,therequestedprotocol.



Findprotocolswitchtable

53-60



Ifprotoisnonzero,pffindprotolooksforthespecificpr

0,pffindtypelooksforaprotocolwithinthespecifiedd

type.Bothfunctionsreturnapointertoaprotoswstruc

pointer(Section7.6).



Allocateandinitializesocketstructure

61-66



socreateallocatesanewsocketstructure,fillsitwith0

processhassuperuserprivileges,turnsonSS_PRIVin



PRU_ATTACHrequest

67-69



Thefirstexampleoftheprotocol-independentsocketla

appearsinsocreate.RecallfromSection7.4and

pointertotheuser-requestfunctionoftheprotocolass

providesthisfunctioninordertohandlecommunicatio

prototypeforthefunctionis:



intpr_usrreq(structsocket*so,



Thefirstargument,so,isapointertotherelevantsock

particularrequest.Thenextthreearguments(m0,m1

Theyarealwayspassedaspointerstombufstructures



usedwhennecessarytoavoidwarningsfromthecomp



Figure15.17showstherequestsavailablethroughthe

requestdependontheparticularprotocolservicingthe



Figure15.17.pr_usrre



PRU_CONNECT2issupportedonlywithintheUnixdo

toeachother.Unixpipesareimplementedinthisway



Cleanupandreturn

70-77



Returningtosocreate,thefunctionattachestheprotoc

issuesthePRU_ATTACHrequesttonotifytheprotocolo

mostprotocols,includingTCPandUDP,toallocateand



supportthenewendpoint.



SuperuserPrivileges



Figure15.18summarizesthenetworkingoperationsth



Figure15.18.Superuserpri



Themulticastioctlcommands(SIOCADDMULTIandS

superuserprocesseswhentheyareinvokedindirectly

IP_DROP_MEMBERSHIPsocketoptions(Sections12.1



InFigure15.18,the"Process"columnidentifiesreques

process,andthe"Socket"columnidentifiesrequeststh

asuperuserprocess(i.e.,theprocessdoesnotneedsu

socket,Exercise15.1).InNet/3,thesuserfunctionde

superuserprivileges,andtheSS_PRIVflagdetermines

process.



Sincerip_usrreqtestsSS_PRIVimmediatelyaftercrea

functionasaccessibleonlyfromasuperuserprocess.



Team-Fly



Team-Fly











TCP/IPIllustrated,Volume2:The

ImplementationByGaryR.Wright,

W.RichardStevens

TableofContents



Chapter15.SocketLayer



15.7getsockandsockargs

Functions

Thesefunctionsappearrepeatedlyinthe

implementationofthesocketsystemcalls.

getsockmapsadescriptortoafiletable

entryandsockargscopiesargumentsfrom

theprocesstoanewlyallocatedmbufin

thekernel.Bothfunctionscheckforinvalid

argumentsandreturnanonzeroerror

codeaccordingly.

Figure15.19showsthegetsockfunction.



Figure15.19.getsockfunction.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5 Processes, Descriptors, and Sockets

Tải bản đầy đủ ngay(0 tr)

×