Re: ttyS1 hangs, cua1 works fine

Richard B. Johnson (root@chaos.analogic.com)
Sun, 5 Apr 1998 18:59:24 -0400 (EDT)


On Wed, 1 Apr 1998, mlord wrote:

> tytso@mit.edu wrote:
> >
> > From: gert@greenie.muc.de (Gert Doering)
> > Date: Sun, 29 Mar 1998 21:47:19 +0200 (MEST)
> >
> > Ted, what do you think about automatically resetting the port to "CLOCAL
> > defaults to 'set'" after the last close()? Does POSIX say anything about
> > the default values of the c_cflag termios flags?
> >
> > POSIX says nothing about the default values of the c_cflag termios
> > flags. Setting CLOCAL after the last close is a possibility; I can
> > imagine people asking the question of "why CLOCAL and not with other
> > flags", and the ability to set termios flags from the shell with
> > intervening periods where the port isn't open is a nice feature to
> > keep. So it's a possibility, but my preferred solution is to add new
> > functionality to stty to make it easier to set the termios flags, even
> > when CLOCAL is clear.
>
> The answer to that question is rather easy and conclusive:
>
> If CLOCAL is not reset, then nobody can even run stty(1) to fix it.
> So long as CLOCAL has been reset, any other flags can be fixed
> using stty(1).
>
> Changing the syntax/invocation of stty(1) is probably not the best
> way to address this issue -- there are going to be a million+ confused
> people when 2.2 hits the streets.
>
> -ml
>
> - To unsubscribe from this list: send the line "unsubscribe
> linux-kernel" in the body of a message to majordomo@vger.rutgers.edu
>

This snippet of code opens ttyS? devices, sets the device to raw mode,
does I/O (snipped out), then sets it back to whatever it was except
the baud-rate is set to 38400.

This works on old kernels, new kernels, old 'C' libraries and new
'C' libraries. Some old kernels sent a SIGHUP to whomever opened the
device, even if it wasn't the controlling terminal. Hense it is
trapped. The Seyon modem-terminal program follows this scheme although
TheCodeIsHardToFollow!

struct termios io_mod;
struct termios io_orig;
int fd, i;
signal(SIGHUP, SIG_IGN); /* Trap Hangup signal */
signal(SIGTTOU, SIG_IGN); /* Whatever this is? */
signal(SIGTTIN, SIG_IGN); /* Ditto */
for(i=0;;)
{
if((fd = open(argv[1], O_RDWR|O_NDELAY)) != -1)
break;
if(errno != ENOENT)
{
(void)sleep(1);
if(i++ < 10) continue;
}
fprintf(stderr, "Failed to open %s (%s)\n",
argv[1], strerror(errno));
ERROR_EXIT;
}
(void)sleep(1); /* Wait for modem to settle */
if((flags = fcntl(fd, F_GETFL)) < 0)
{
fprintf(stderr, "fcntl of %s failed getting flags (%s)\n",
argv[1], strerror(errno));
ERROR_EXIT;
}
flags &= ~O_NDELAY;
if(fcntl(fd, F_SETFL, flags) < 0)
{
fprintf(stderr, "fcntl of %s failed setting flags (%s)\n",
argv[1], strerror(errno));
ERROR_EXIT;
}
if(ioctl(fd, TCGETS, &io_orig) < 0) /* Get terminal params */
{
sprintf(buffer, "ioctl of %s failed getting parameters (%s)\n",
argv[1], strerror(errno));
ERROR_EXIT;
}
/*
* Set to dumb RAW mode with no echo and no character interpretation.
*/
memset(&io_mod, 0x00, sizeof(io_mod)); /* bzero() is obsolete */
io_mod.c_cflag = B38400|CS8|CREAD|CLOCAL;
io_mod.c_iflag = IGNBRK|IGNPAR;
io_mod.c_cc[VMIN] = (cc_t) 1;
io_mod.c_cc[VTIME] = (cc_t) 1;
if(ioctl(fd, TCSETS, &io_mod) < 0)
{
fprintf(stderr, "ioctl of %s failed setting parameters (%s)\n",
argv[1], strerror(errno));
ERROR_EXIT;
}
/* Here is where we do something with the line....... */

code();

/* We are done. Set everything back except leave the new baud-rate */

io_orig.c_cflag &= ~CBAUD; /* Mask old baud-rate */
io_orig.c_cflag |= B38400; /* Leave new baud-rate */
if(ioctl(fd, TCSETS, &io_orig) < 0) /* Set terminal back */
{
fprintf(stderr, "ioctl of %s failed setting parameters (%s)\n",
argv[1], strerror(errno));
}
(void)close(fd);

Cheers,

Dick Johnson
***** FILE SYSTEM MODIFIED *****
Penguin : Linux version 2.1.92 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu