tcdrain() on socket yields EFAULT (expected EINVAL/ENOTTY)

Bug #72200 reported by Jörg Höhle
2
Affects Status Importance Assigned to Milestone
Linux
Fix Released
Medium
linux-source-2.6.15 (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

tcdrain(3) returns EFAULT when invoked on a socket. EFAULT is a serious error meaning that the tcdrain()
function has passed to the kernel an invalid memory address.

Expected error values from passing a non-tty to tcdrain() are EINVAL, ENOTTY (or possibly EOPNOTSUPP on Mac OSX, but we're talking about Linux now).

Here's a program with test results for files, pipes, pty and sockets.

/* tcdrain(3) returns EFAULT (a serious error) on sockets */
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main () {
  int i;
  for (i=0; i<=2; i++) {
    int retval, save;
    errno = 0;
    retval = tcdrain(i);
    save = errno;
    printf("tcdrain(%d)=(%d,%d)\n",i,retval,save);
    /* printf("tcdrain(%d)=(%d,%d=%s)\n",i,retval,save,
    save ? strerror(save) : ""); */
  }
  return 0;
}
/*
$ ~/Bugs/tcdrain
tcdrain(0)=(0,0)
tcdrain(1)=(0,0)
tcdrain(2)=(0,0)
$ ~/Bugs/tcdrain | cat
tcdrain(0)=(0,0)
tcdrain(1)=(-1,22) # EINVAL = Invalid argument
tcdrain(2)=(0,0)
$ ~/Bugs/tcdrain 2>/dev/null
tcdrain(0)=(0,0=)
tcdrain(1)=(0,0=)
tcdrain(2)=(-1,25) # ENOTTY = Inappropriate ioctl for device
$ echo foo | ~/Bugs/tcdrain
tcdrain(0)=(-1,22)
tcdrain(1)=(0,0)
tcdrain(2)=(0,0)
$ ~/Bugs/tcdrain <~/Bugs/tcdrain
tcdrain(0)=(-1,25=Inappropriate ioctl for device)
tcdrain(1)=(0,0=)
tcdrain(2)=(0,0=)
# Now I'd expect an error similar to the previous ones,
 or possibly EOPNOTSUPP, but not EFAULT:
$ ssh localhost ~/Bugs/tcdrain
tcdrain(0)=(-1,14) # EFAULT = Bad address
tcdrain(1)=(-1,14) # EFAULT
tcdrain(2)=(-1,14) # EFAULT
$ ssh -t localhost ~/Bugs/tcdrain
tcdrain(0)=(0,0)
tcdrain(1)=(0,0)
tcdrain(2)=(0,0)
Connection to localhost closed.
$ uname -a # Dapper 6.06 Ubuntu Debian
Linux jchlaptop 2.6.15-27-686 #1 SMP PREEMPT Sat Sep 16 02:13:27 UTC 2006 i686 GNU/Linux

Reference:
http://sourceforge.net/tracker/index.php?func=detail&aid=1592343&group_id=1355&atid=101355
*/

ssh has nothing to do with the bug, but I don't know how to connect I/O to a socket on the command line, short of writing a tiny program that does socket() calls (and implement a tiny server). You will obtain the same errno if you pass tcdrain such a socket fd, without intervening ssh.

This is for sure a bug unrelated to Ubuntu. However the bugzilla.kernel.org FAQ asks to contact the distribution first.

Regards,
 Jörg Höhle.

Revision history for this message
Sebastien Bacher (seb128) wrote :

Thank you for you bug. You opened that bug on gnome-applets though, which might not be the best place for that tcdrain() problem you are mentionning. What sort of reply to you expect for it? Is gnome-applets using that API incorrectly?

Changed in gnome-applets:
status: Unconfirmed → Needs Info
Revision history for this message
Jörg Höhle (joerg-cyril-hoehle) wrote : not a bug on gnome-applets, but in linux-image-686

I'm sorry. The bug has nothing at all to do with gnome-applets.
I should file a *usability bug* to launchpad: it always confuses me as to where the bug eventually gets submitted. :-( :-(

When submitting the bug, I choose from the pop up window the package linux-image-686, because it's a kernel bug (as seen in my Dapper 2.6.15-27 image).
I don't know why my choice of this package does not show up in launchpad.

Another launchpad "feature" which confuses me is "initially uploaded to: Ubuntu Feisty" in the left hand bar. I'm using Dapper and cannot comment on Feisty.

I don't know how this bug can be reassigned to the kernel package instead. Maybe only you can do this as an administrator?

I'm sorry for the confusion.

Thank you for your help,
 Jörg Höhle

Revision history for this message
Sebastien Bacher (seb128) wrote :

no problem, reassigning the bug, feel free to open bug on launchpads for the problems you have face with it (maybe look if they are already known before)

Changed in gnome-applets:
status: Needs Info → Unconfirmed
Revision history for this message
Jörg Höhle (joerg-cyril-hoehle) wrote :

Well, a clisp user reported that
ssh xyhost clisp --version
breaks because of that (cf. sourceforge URL in original post).
But IMHO there's no need to add an extra launchpad item for that.

Revision history for this message
Jörg Höhle (joerg-cyril-hoehle) wrote : ioctl(TCSBRK,1) on socket yields EFAULT (expected EINVAL/ENOTTY)

I was told the Linux kernel hackers want code that goes straight to the bones, i.e. use ioctl() instead of the tcdrain() wrapper. Here it is.

/* tcdrain() returns EFAULT (a serious error) on sockets
 * tcdrain() is equivalent to ioctl(TCSBRK) on Linux and Solaris
 */
#include <sys/ioctl.h>
#include <termios.h> /* TCSBRK is 0x5409 */
#include <errno.h>
#include <stdio.h>

int main () {
  int i;
  for (i=0; i<=2; i++) {
    int retval, save;
    errno = 0;
    retval = ioctl(i,TCSBRK,1);
    save = errno;
    printf("ioctl(%d,TCSBRK,1)=(%d,%d)\n",i,retval,save);
    /* printf("ioctl(%d,TCSBRK,1)=(%d,%d=%s)\n",i,retval,save,
    save ? strerror(save) : ""); */
  }
  return 0;
}

Revision history for this message
Jörg Höhle (joerg-cyril-hoehle) wrote : submitted as kernel bug #7635
Changed in linux:
status: Unknown → Fix Released
Revision history for this message
Carlos Diener / emonkey (emonkey) wrote :

Thank you for taking the time to report this bug and helping to make Ubuntu better. You reported this bug a while ago and there hasn't been any activity in it recently. We were wondering is this still an issue for you? Can you try with latest Ubuntu release? Thanks in advance.

Changed in linux-source-2.6.15:
status: New → Fix Released
Changed in linux:
importance: Unknown → Medium
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.