Subject: Re: Kernel source tree specifications
To: None <ianzag@megasignal.com>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 06/20/2003 18:00:31
> A question is: is it supposed to get close call on *every* open call
> in driver code ?

No.  The driver's open routine is called for every open, but the close
routine is called only for _last_ close, ie, when _nobody_ has the
device open any longer.

It has been this way approximately forever; I can personally seen it as
far back as 4.2BSD and it probably goes back well before that.

Why was it done that way?  I wasn't involved when that decision was
made, so I can only speculate.  But my guess would be that the driver's
open is called every time so that the device can impose any
restrictions it may want to (exclusive-open, maybe, or only processes
with certain attributes), but the close routine is intended not to
balance opens but rather to deactivate the device when it's no longer
in use, and as such needs to be called only on last close.  If the
close routine were called on every close, it would be impossible to
tell when the device is no longer in use.  (No, counting opens and
matching them with closes won't help, unless every dup (or equivalent,
eg the implicit dup that happens on fork) also called open - and, since
those "can't fail", they can't really do that.  I suppose closes could
be called when open file table entries, rather than file descriptors,
are closed....)

It would make conceptual sense to have a per-close routine and a
last-close routine, or pass a flag to close when it's the last close.
But in any case, that isn't the way it works now.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B