usb-serial does not return read errors after disconnect

Mike Frysinger <[email protected]>
Newsgroups gmane.linux.usb.user
Organization wh0rd.org
Message-ID <[email protected]>
i noticed that while using minicom on a usb-serial device (PL2303), 
disconnecting it would make minicom spike using all the cpu.  looking into 
it, minicom does a simple select()/read() on the fd on the device, but those 
functions werent returning errors so it kept on looping.  a simple test 
program shows similar behavior: open a usb serial device, set the fd to 
nonblocking, and then in a loop just keep doing a read().  while the usb 
cable is plugged in, you get back the normal EAGAIN.  but when you unplug the 
device, read() just returns 0 rather than ENODEV or EIO.

i tried pokin around the usb-serial layer, but nothing obvious stands out.  
attached test case shows the issue: run it, watch the EAGAIN, and then unplug 
the device and watch no more output.
-mike

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#define X(call) ({ \
		int _ret = call; \
		printf("%s = ", #call); \
		printf("%i\n", _ret); \
		_ret; \
	})

int hitit = 1;
void quitit(int sig)
{
	X(hitit = 0);
}

int main(int argc, char *argv[])
{
	const char *tty = (argc > 2 ? argv[2] : "/dev/ttyUSB0");
	int fd, ret;
	char buf[1024];

	setbuf(stdout, NULL);

	signal(SIGINT, quitit);

	fd = X(open(tty, O_RDONLY|O_NONBLOCK));

	while (hitit) {
		ret = X(read(fd, buf, sizeof(buf)));
		if (ret == -1)
			printf("\terrno = %i (%s)\n", errno, strerror(errno));
		sleep(1);
	}

	X(close(fd));

	return 0;
}

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/

_______________________________________________
[email protected]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-users
signature.asc (application/pgp-signature, 827 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.6 (GNU/Linux)

iQIVAwUARtcbkEFjO5/oN/WBAQJjzRAAwPCpqIUK5p8WJQlYfepgX1yw9nxQsCMC
Z7mHZd88cVv1ATQwWZdV5nxq5yE+BCJjkJGMWs+DVTOXxlgbzQIxJMYC/W48iytT
lWfkYMWQ2+dfWjUbghJIDHGtYyOUV1E6/TvdDiKjY3MOmoMH0qyO3qqwAGBR5MFN
X17eDL6XTesDfEAodv41krIAWYn7ozvJj+yG0d+6CNXGdZzUjEndWrIaERN027bY
gd5gltd+U3nXcZVmEsQeUHUHpa1kW+yrqs7oqIKVGcC/635wGz3PNd7cyUI+xI47
zpjXeFzBv6h5b3ZcnpKEEfmK/8NL1GMW62hogTVxBB73dKMHFG1vwCdGAYjK9WuU
AIvjYf2TzHcOE28/MMpnUzTLqjHKj9HDoseZ9j1scOJtUmrV8PG54+qi79nKjIu5
mnxPFE5W8vKe2TCraBa3Rre0V4IX87lMRicc8bMk0UEA8A+GMYjLhK3UFl9VhOEX
N7jpGwiiejRs2aJ8gGdyNTrl1IxAk8FmTZMGsphxjHmMlIFl5KDADUL5HxzKKJ2r
UAPGxAd65Gk2cRz75jpcw+qleJYqwjlX9SZ4htfRl0M3xwU6OQ+bABdWrdg/BnNC
t46RWoazwNBzgvGrC5yc3XrAF/R8Dfu4yAwR2qh+xyX3rZ9GMz2QsJiwMkuqizrb
R2yogH5CIvw=
=2u6L
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.