Q: Notify device driver about process termination

Q: Notify device driver about process termination

Post by Stan Shkolny » Thu, 06 May 1999 04:00:00



Hi All,

I'm writing a character dev.driver that provides services to processes.
The process calls open(), then several ioctl()s and finally close().
The driver is supposed to allow several processes to have it open at the
same time. The driver allocates some resources on behalf of the process
and then frees those resources when the process calls close(). The problem
is that the driver's close routine is only called when _the last_ process
calls close(). Thus, no cleanup is possible until the last file descriptor
is closed.

I hoped to use struct proc* parameter of driver ioctl() and close()    
functions to "tag" and then free the resources but run into the problem.

What is it possible to do to receive a notification on each process
termination? Did I make a design error when I had planned to do it this
way? If so, what is the "official" way to achieve an automatic
process-specific cleanup?

Thank you,
Stan

 
 
 

Q: Notify device driver about process termination

Post by DM » Thu, 06 May 1999 04:00:00


I'm not quite sure at what you're driving at, sorry for the pun.
But what you're probably looking for is the u as defined in
sys/user.h. You may consider registering the u.u_procp in your
open() and comparing them in your close(). However, this
solution is highly un-portable, uses an undocumented interface,
and is causing me mountains of trouble right now, since I
haven't found a good way of getting rid of it.
Good luck.

: Hi All,

: I'm writing a character dev.driver that provides services to processes.
: The process calls open(), then several ioctl()s and finally close().
: The driver is supposed to allow several processes to have it open at the
: same time. The driver allocates some resources on behalf of the process
: and then frees those resources when the process calls close(). The problem
: is that the driver's close routine is only called when _the last_ process
: calls close(). Thus, no cleanup is possible until the last file descriptor
: is closed.

: I hoped to use struct proc* parameter of driver ioctl() and close()    
: functions to "tag" and then free the resources but run into the problem.

: What is it possible to do to receive a notification on each process
: termination? Did I make a design error when I had planned to do it this
: way? If so, what is the "official" way to achieve an automatic
: process-specific cleanup?

: Thank you,
: Stan

 
 
 

Q: Notify device driver about process termination

Post by Satish Pagar » Thu, 06 May 1999 04:00:00


Could anybody send a nice tutorial for device drivers... ???
Pleeeese...

Thanx in advance.
Satish/


> Hi All,

> I'm writing a character dev.driver that provides services to processes.
> The process calls open(), then several ioctl()s and finally close().
> The driver is supposed to allow several processes to have it open at the
> same time. The driver allocates some resources on behalf of the process
> and then frees those resources when the process calls close(). The problem
> is that the driver's close routine is only called when _the last_ process
> calls close(). Thus, no cleanup is possible until the last file descriptor
> is closed.

> I hoped to use struct proc* parameter of driver ioctl() and close()
> functions to "tag" and then free the resources but run into the problem.

> What is it possible to do to receive a notification on each process
> termination? Did I make a design error when I had planned to do it this
> way? If so, what is the "official" way to achieve an automatic
> process-specific cleanup?

> Thank you,
> Stan

--
:--------------------------------------------:
   Who am I to know ? = Who am I to live ?
                            John Galt.
   (Who is John Galt?)
:--------------------------------------------:

Satish K. Pagare,
Software Engineer.
Visit me : http://satish.cjb.net
INFORMIX India Development Center, Tel:829 04 02 (extn. 203).
ICIL, Unit No:26, SDF-I, SEEPZ, Andheri(E), Mumbai - 400 096, INDIA.

 
 
 

Q: Notify device driver about process termination

Post by Stan Shkolny » Thu, 06 May 1999 04:00:00


I think it would be better to illustrate what I mean. Actually, the open()
operation itself is not important for me, since the driver does nothing on
open(). What is important are ioctl() calls that establish the calling
process's temporary ownership upon driver's internal data objects.
When 2 processes do that at the same time, it looks like this

P1: open()
P2: open()

P2: ioctl(acquire a resource) -> driver: resource_cnt--
P2: ioctl(acquire a resource) -> driver: resource_cnt--

Here P2 is forcibly terminated without calling special ioctl()s to free
the resources it has acquired but nobody issues the close() call to the
driver, though the process 2 doesn't exist anymore!!!

P1: ioctl(acquire a resource) -> driver: resource_cnt--
P1: ioctl(acquire a resource) -> driver: resource_cnt--

And now we have a situation when 4 resources are marked busy, but
Process1 uses only 2 and 2 others are stall until the P1 terminates no
matter how, but, at this time, the close() will be issued by the O/S
because the _last_ file descriptor is gone.

I would like to be able to automatically free the 2 resources when the P2
was terminated, but system doesn't give me a chance to know about the
termination (or I don't know how to ask it).

The system, FreeBSD 2.2.6, gives struct proc* to each of ioctl() and
close() calls. In described example, my close() is called only once
and sees only the ptr to P1 and never the ptr to P2.

When I planned the driver, I made the assumption that I will see close()
for each process, when the process's last file descriptor to my driver is
closed, and I planned to use struct proc* to "tag" acquired resources.

Yesterday I looked into "Linux device drivers" by A.Rubini and found out
that Linux device driver functions are given struct file* instead of
struct proc*. I'll be working on the Linux version soon, so it would be
interesting to see whether the same problem exists in Linux. I'm looking
forward to try to use struct file* for the same purpose.  

In NT, there is no such problem, since O/S creates a "file object" when it
opens a device. This "file object" is process-specific and drivers always
see close( file object*) when the process dies, so the automatic cleanup
is based on using this file object* as a tag.

So, here is my problem. I might be making a principial mistake trying to
do it this way, maybe I shouldn't be a characted device driver to be able
to play a role of server for processes (with process-specific resource
allocation). If so, what should I be instead?

Thank you.


> I'm not quite sure at what you're driving at, sorry for the pun.
> But what you're probably looking for is the u as defined in
> sys/user.h. You may consider registering the u.u_procp in your
> open() and comparing them in your close(). However, this
> solution is highly un-portable, uses an undocumented interface,
> and is causing me mountains of trouble right now, since I
> haven't found a good way of getting rid of it.
> Good luck.


> : Hi All,

> : I'm writing a character dev.driver that provides services to processes.
> : The process calls open(), then several ioctl()s and finally close().
> : The driver is supposed to allow several processes to have it open at the
> : same time. The driver allocates some resources on behalf of the process
> : and then frees those resources when the process calls close(). The problem
> : is that the driver's close routine is only called when _the last_ process
> : calls close(). Thus, no cleanup is possible until the last file descriptor
> : is closed.

> : I hoped to use struct proc* parameter of driver ioctl() and close()    
> : functions to "tag" and then free the resources but run into the problem.

> : What is it possible to do to receive a notification on each process
> : termination? Did I make a design error when I had planned to do it this
> : way? If so, what is the "official" way to achieve an automatic
> : process-specific cleanup?

> : Thank you,
> : Stan

 
 
 

1. Process Termination Indication in the Device Driver

Hi,
        I am new to Linux Kernel. I am experimenting with Device Driver in Kernel
version 2.4. Is there a method by which the device driver could be indicated
by the
kernel of the termination of a "user process" asynchronously.

I know of the function find_task_by_pid that the driver could call to get
the task
structure given the pid.

Thanks in Advance

Srini

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in

More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

2. win98 boot as default doesn't work...

3. Driver driver peculiarity on process termination

4. disappearing kernel?

5. How to notify a user process from within a driver

6. Tape Controller Suppliers

7. Device driver calling another device driver.

8. HP 7200e CD Burner setup?

9. Device driver question (generic device driver)

10. How make a process notify me when it dies

11. Qs on attack by high rate process creation

12. Termination for external SCSI device

13. blocked process resists termination