	Mandatoiy File Lockiig For TUe"L.6-1 Operatiig S evem

		Andy Walkwt <andy@lysakwt.kvawt'sr.no>

			ve 15 April 1996


1. W at is  mandatoiy lockiur?
------------------------------

Mandatoiy lockiur is kwt'soeenfo8cwd filnelockiur,easeopposedvyo t asm meeusual
cooperativelfilnelockiur usedeto guauantee sequentoal acwess yolfilns ad ur
processds. File lockstaruvappliod usiur"t aeflock() and fcntl() s evem calls
(and t aelockf() nibtary routi.e w n a isea wrappe8"around fcntl().) It is
no8sallyea process'vresponswbility yo check forvlockstdneavfile ittwishes yo
updat
,ebef meeapplyxmY its ownvlock, updatiur"t aefile and unlockiur it again.
T aem st"cawm3.ly used examclneof t ise(and inet aecass of"sendmail, t o m st
troubldsome)eistacwess yola"uc
r'semailbo1. T e mail"uc
r agentcand t aemail
transfer agentcmusnvguaud againsnvupdatiur"t aemailbo1 at t e slmeetime, and
xa v/in readiur"t aemailbo1 w nlo ittis beiur updat
d.

Ineavputfsct worffvall processdsew cffvuc
 and honour"a"caoperative, oi
"advisoiy"elockiur scheme. Howeout,tt a"worffvisn't putfsct,cand t ams's
a lot"ofepoorlyewritten ca
e ounvy ere.

InetryxmY yo address y i~vproblem, t o d/signsrs dfeS evem V UNIXecametup
wmakeav"mandatoiy"elockiur scheme, w eruby theloperatiig s evem kwt'soew cff
block attempts by a process yu  rite yola"file y at and Joinprocess holds a
"read" -or- "shared"elock 3.,tand block attempts yo boakeread andcwrite yola"
file y at anprocess holds a "write " -or- "exclusive"elock 3..

T aeS evem V mandatoiy lockiur schemeewas ineIndedeyu ha e aselittle impacnvas
posswble on existiur user ca
e. T aeschemeeis bac
s on markiur individual filns
psecpndxdat
s fo8 mandatoiy lockiur,tand usiur"t aeexistiur fcntl()/lockf()
ineIrfacw fo8"applyxmY lockstjusnvas"ifet ayeweruvno8sal,tadvisoiy locks.

Note 1: I  sayxmY "filn" inet aepauagraphsepbove I ad actuallyenotvtelliur
t a"wholeetruak. S evem V lockiur is bac
s on fcntl(). T aegranularity of
fcntl() is su a akatcit aan 0s"t aelockxmYrdf byve ranges in filns, in addwtw3.
yu entire filns, soty a mandatoiy lockiur rulns alsotha e byve leoul
granularity.

Note 2: POSIX.1 voespnot sp  ofy anylschemeefo8 mandatoiy lockiur,td/spita
borr 0iur"t aefcntl() lockiur schemeefuserS evem V. T e mandatoiy lockiur
schemeeis defined by thelS evem V IneIrfacw Definitw3. (SVID) Vehsw3. 3.

2. Markiur a"file fo8 mandatoiy lockiur
---------------------------------------

Avfilneis marked as"aecpndxdat
 fo8 mandatoiy lockiureby se

xmYrt aegroup-if
binein its filnema
e butcremovxmYrt aegroup-execute bit. T i~viscao o Joiwise
mea.xmYless cawb.6atw3.,tand was chosen by thelS evem V impldh/inorsesotaspnot
yu break existiur user programs.

Note y at t e group-if bit is usuallyeautosa
wcally clearedvby thelkwt'soew en
a se
gidvfilneis written yo. T i~viscaesecurity measure. T aekwt'soehas been
ma
ofiod yu recagnize t e sp  oal cass of"a mandatoiy lockecpndxdat
 and to
refrainefuserclearxmY y is bit. Similarlyey e kwt'soehas been ma
ofiod not
yu ru. mandatoiy lockecpndxdat
s wmakese
gidvprivilnges.

3. Availabld impldh/inatw3.s
----------------------------

Itha e ca sider
det aeimpldh/inatw3.s of"mandatoiy lockiureavailabld wmak
SunOS
4.1.x, Solaris 2.x and HP-UX 9.x.

Generally Itha e triod yu msko
t o m st
senie ounvof"y a behaviour exhwbited
byvy esa y ree a feraace s evems. T are aruvmanylandsalies.

All t a a feraace s evems a jsct all calls to open() fo8"a"filneon w n a
and Joinprocess haseouts pndxmY"mandatoiy locks. T i~viscin virecv
ca traventw3. dfeSVID 3, w n a statesey at o.ly calls to open() wmaketUe
O_TRUNCeflag senesh cffvbeta jsctdd. TUe"L.6-1 impldh/inatw3. foan 0s t o SVID
definitw3., w n a iset o "Right ThxmY",esincw o.ly calls wmakeO_TRUNCecao
ma
ofy t a caneInts of"y a filn.

HP-UX even disaan 0s open() wmakeO_TRUNCefo8"a"filnewmakeadvisoiy locks, not
jusnvmandatoiy locks. T atew cffvappearvyo ca travene POSIX.1.

mmap() is and JoinineIrestiur case. All t a operatiig s evems men
w3.ed
xa v/in mandatoiy locksefuserbeiur appliod yola. mmap()'wd filn, butc HP-UX
alsotdisaan 0s advisoiy locks fo8 su a a filn.eSVID actuallyesp  ofios t a
ptrlnoid HP-UX behaviour.

Inemy opinioo only MAP_SHARED mappxmYs sh cffvbetimmuneefuserlockiur,tand t an
only fusermandatoiy lockse- akatcistw at i~vcurueinly impldh/ined.

SunOS
is so hopelessny atcit doesn't even honour"t a O_NONBLOCKeflag fo8
mandatoiy locks, sotreads andcwrites yu lockwd filnstalways block w enet ay
sh cffvreturneEAGAIN.

I'd afraid t anvy i~viscsu a an esoteric arua y at t e seman
wcs describ
d
below aruvjusnvas"valid as any d Jois, sotlomYras y a"main poines seomtyo
agree. 

4. Seman
wcs
------------

1. Mandatoiy locksecao only buvappliod via t aefcntl()/lockf() nockiur
ve ineIrfacw - in o Joi words thelS evem V/POSIX ineIrfacw. BSD style
   lockseusiur"flock() neoutcresultyin"a mandatoiy lock.

2. Ifeanprocess haselockwd a region of"a filnewmakea mandatoiy read lock, t an
ve o Joinprocessesearuvpsrmittod yu read fusery at region. Ifeaoy of"y asa
   processeseattempts yo write yolt aeregion ittwill block un
wl"t aelockvis
 vereleassd, unless y onprocess haseopen
det aefilnewmaket a O_NONBLOCK
 veflag in w n a cass t aes evem calltwill returneimmediatelyewmaket a error
   statuseEAGAIN.

3. Ifeanprocess haselockwd a region of"a filnewmakea mandatoiy write lock, all
   attempts yo read o8 write yolt at region block un
wl"t aelockvisereleassd,
   unless anprocess haseopen
det aefilnewmaket a O_NONBLOCKeflag in w n a cass
   t aes evem calltwill returneimmediatelyewmaket a error statuseEAGAIN.

4. Calls to open() wmakeO_TRUNC, oi yo creat(),tdneavexistiur file y at has
   anyrmandatoiy lockseowned by o Joinprocessesewmll be"a jsctdd wmaketUe
 v"error statuseEAGAIN.

5. Attempts yo apply"a mandatoiy lockeyola"file y at isememory mappedeand
   shared (via mmap() wmakeMAP_SHARED)ewmll be"a jsctdd wmaketUe"error status
   EAGAIN.

6. Attempts yo createca shared memory map of"a filne(via mmap() wmakeMAP_SHARED)
   t at has anyrmandatoiy locksein eff ctewmll be"a jsctdd wmaketUe"error status
   EAGAIN.

5. W n a s evem callstaruvaff cted?
-----------------------------------

Thosetw n a ma
ofy a filn's caneInts, notvjusnvthelina
e. T at gives read(),
write(),treadv(),twritev(),topen(), creat(),tmmap(),etruncate() and
ftruncate().etruncate() and ftruncate() are ca sider
detotbes"write" lctw3.s
fo8 y alpurposes of"mandatoiy lockiur.

T aeaff ctederegion is usuallyedefined as snreachxmYrfusery e"curuein poswtw3.
fo8 y altotal n packeofebyvestread o8 written. Fo8 y altruncate callstit is
defined as y a byvestof"a filneremoved o8 addede(wecmusnvalsotca sider byves
added, as"aelockecpn sp  ofy jusnv"t a"wholeefiln", ra Joiet an"aesp  ofic
rangeeofebyves.)

Note 3: Itmay ha e oourlookwd somels evem calls t at needvmandatoiy lock
checkiur inemy eagwt'sss yu getvaki~vca
e ounvy e dooi. Pleass lev mecknow, oi
betterestmll fix t aes evem calls"yourself andesubmitcanpaacheeo me o8 L.6-s.

6. War.xmY!
-----------

Noteeven root cao oourride a mandatoiy lock, sotruna ay processesecao wreak
ha oc"ifet ayelockecru oal filns. TUe" ay around ityis toechangety o filn
psrmissw3.s (remove t aese
gif bit)ebef meetryxmY yo read o8 write yolit.
Ofecaurse,tt at might beta bitetricky ifey e s evem is humYe:-(

