Skip to contentSkip to conversation

question on tty open and close

DISCUSSION Oliver Neukum 5 participants 9 messages Mar 28, 2007

lore
Share & tools
lore
#0 lore raw

Hi,

in tty_io.c::tty_open():

	if (!retval) {
		if (tty->driver->open)
			retval = tty->driver->open(tty, filp);
		else
			retval = -ENODEV;
	}
	filp->f_flags = saved_flags;

	if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
		retval = -EBUSY;

	if (retval) {
#ifdef TTY_DEBUG_HANGUP
		printk(KERN_DEBUG "error %d in opening %s...", retval,
		       tty->name);
#endif
		release_dev(filp);

We find that a failure in open() leads to release_dev() being called. release_dev() calls close():

	if (tty->driver->close)
		tty->driver->close(tty, filp);

So we have a file that's closed although open() never succeeded? What's the reason for that?

Regards
Oliver

#1 lore raw

From: [email protected]

Quoted 1 line

in tty_io.c::tty_open():

[snip]

Quoted 7 lines

We find that a failure in open() leads to release_dev() being called. release_dev() calls close():

	if (tty->driver->close)
		tty->driver->close(tty, filp);

So we have a file that's closed although open() never succeeded?

That's correct! It's been a pain in my butt for years.

Quoted 1 line

What's the reason for that?

No idea. Would *love* to hear an explanation.

..Stu

#2 lore raw

Am Mittwoch, 28. März 2007 15:10 schrieb Stuart MacDonald:

Quoted 9 lines
Quoted 7 lines

We find that a failure in open() leads to release_dev() being called. release_dev() calls close():

      if (tty->driver->close)
              tty->driver->close(tty, filp);

So we have a file that's closed although open() never succeeded?

That's correct! It's been a pain in my butt for years.

How did you deal with that proctological issue?

Regards
Oliver

#3 lore raw

From: Oliver Neukum [mailto:[email protected]]

Quoted 5 lines

Am Mittwoch, 28. März 2007 15:10 schrieb Stuart MacDonald:

Quoted 1 line
Quoted 1 line

We find that a failure in open() leads to release_dev()

being called.

Quoted 2 lines
Quoted 2 lines

release_dev() calls close():

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

      if (tty->driver->close)
              tty->driver->close(tty, filp);

So we have a file that's closed although open() never succeeded?

That's correct! It's been a pain in my butt for years.

How did you deal with that proctological issue?

Just make sure the close() handles the situation properly. It makes reference counting... fun.

The serial driver has always handled it like this.

..Stu

#4 lore raw
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

So we have a file that's closed although open() never succeeded?

That's correct! It's been a pain in my butt for years.

How did you deal with that proctological issue?

Just make sure the close() handles the situation properly. It makes reference counting... fun.

The serial driver has always handled it like this.

I'm also not aware of any reason other than history, which means if someone cares to double check the other drivers there really shouldn't be an obstacle to "fixing" this behaviour.

Unless anyone knows different ?

Alan

#5 lore raw
Quoted 5 lines · Alan Cox

I'm also not aware of any reason other than history, which means if someone cares to double check the other drivers there really shouldn't be an obstacle to "fixing" this behaviour.

Unless anyone knows different ?

As long as the new behavior continues to call driver->close() if driver->open() succeeds then I see no problem.

If individual drivers are correctly doing internal reference counting to handle driver->close() calls without a preceding successful driver->open() call (usually just doing nothing), then they should continue to operate correctly without the extra and unnecessary driver->close().

Signature · 2 lines
Paul Fulghum
Microgate Systems, Ltd.
#6 lore raw
Quoted 3 lines

As long as the new behavior continues to call
driver->close() if driver->open() succeeds
then I see no problem.

It breaks if any existing driver is doing no cleanup in ->open() when it fails but relying upon ->close() being called. That is what needs auditing first of all.

#7 lore raw
Quoted 3 lines · Alan Cox

It breaks if any existing driver is doing no cleanup in ->open() when it fails but relying upon ->close() being called. That is what needs auditing first of all.

Yes, I did not think of that.

Signature · 2 lines
Paul Fulghum
Microgate Systems, Ltd.
#8 lore raw
In reply to Alan Cox

Hi Alan,

Quoted 7 lines
Quoted 3 lines

As long as the new behavior continues to call
driver->close() if driver->open() succeeds
then I see no problem.

It breaks if any existing driver is doing no cleanup in ->open() when it fails but relying upon ->close() being called. That is what needs auditing first of all.

I know at least that the Bluetooth TTY emulation (RFCOMM) will break since I have a big fat warning in my code:

        /* We don't leak this refcount. For reasons which are not entirely
           clear, the TTY layer will call our ->close() method even if the
           open fails. We decrease the refcount there, and decreasing it
           here too would cause breakage. */
        dev = rfcomm_dev_get(id);
        if (!dev)
                return -ENODEV;

Regards

Marcel