Blame sysdeps/unix/sysv/linux/tcsetattr.c

Packit 6c4009
/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <termios.h>
Packit 6c4009
#include <sys/ioctl.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
/* The difference here is that the termios structure used in the
Packit 6c4009
   kernel is not the same as we use in the libc.  Therefore we must
Packit 6c4009
   translate it here.  */
Packit 6c4009
#include <kernel_termios.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This is a gross hack around a kernel bug.  If the cfsetispeed functions
Packit 6c4009
   is called with the SPEED argument set to zero this means use the same
Packit 6c4009
   speed as for output.  But we don't have independent input and output
Packit 6c4009
   speeds and therefore cannot record this.
Packit 6c4009
Packit 6c4009
   We use an unused bit in the `c_iflag' field to keep track of this
Packit 6c4009
   use of `cfsetispeed'.  The value here must correspond to the one used
Packit 6c4009
   in `speed.c'.  */
Packit 6c4009
#define IBAUD0	020000000000
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Set the state of FD to *TERMIOS_P.  */
Packit 6c4009
int
Packit 6c4009
__tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
Packit 6c4009
{
Packit 6c4009
  struct __kernel_termios k_termios;
Packit 6c4009
  unsigned long int cmd;
Packit Bot 6eebc2
  int retval;
Packit 6c4009
Packit 6c4009
  switch (optional_actions)
Packit 6c4009
    {
Packit 6c4009
    case TCSANOW:
Packit 6c4009
      cmd = TCSETS;
Packit 6c4009
      break;
Packit 6c4009
    case TCSADRAIN:
Packit 6c4009
      cmd = TCSETSW;
Packit 6c4009
      break;
Packit 6c4009
    case TCSAFLUSH:
Packit 6c4009
      cmd = TCSETSF;
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
Packit 6c4009
  k_termios.c_oflag = termios_p->c_oflag;
Packit 6c4009
  k_termios.c_cflag = termios_p->c_cflag;
Packit 6c4009
  k_termios.c_lflag = termios_p->c_lflag;
Packit 6c4009
  k_termios.c_line = termios_p->c_line;
Packit 6c4009
#if defined _HAVE_C_ISPEED && defined _HAVE_STRUCT_TERMIOS_C_ISPEED
Packit 6c4009
  k_termios.c_ispeed = termios_p->c_ispeed;
Packit 6c4009
#endif
Packit 6c4009
#if defined _HAVE_C_OSPEED && defined _HAVE_STRUCT_TERMIOS_C_OSPEED
Packit 6c4009
  k_termios.c_ospeed = termios_p->c_ospeed;
Packit 6c4009
#endif
Packit 6c4009
  memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
Packit 6c4009
	  __KERNEL_NCCS * sizeof (cc_t));
Packit 6c4009
Packit Bot 6eebc2
  retval = INLINE_SYSCALL (ioctl, 3, fd, cmd, &k_termios);
Packit Bot 6eebc2
Packit Bot 6eebc2
  if (retval == 0 && cmd == TCSETS)
Packit Bot 6eebc2
    {
Packit Bot 6eebc2
      /* The Linux kernel has a bug which silently ignore the invalid
Packit Bot 6eebc2
        c_cflag on pty. We have to check it here. */
Packit Bot 6eebc2
      int save = errno;
Packit Bot 6eebc2
      retval = INLINE_SYSCALL (ioctl, 3, fd, TCGETS, &k_termios);
Packit Bot 6eebc2
      if (retval)
Packit Bot 6eebc2
       {
Packit Bot 6eebc2
         /* We cannot verify if the setting is ok. We don't return
Packit Bot 6eebc2
            an error (?). */
Packit Bot 6eebc2
         __set_errno (save);
Packit Bot 6eebc2
         retval = 0;
Packit Bot 6eebc2
       }
Packit Bot 6eebc2
      else if ((termios_p->c_cflag & (PARENB | CREAD))
Packit Bot 6eebc2
              != (k_termios.c_cflag & (PARENB | CREAD))
Packit Bot 6eebc2
              || ((termios_p->c_cflag & CSIZE)
Packit Bot 6eebc2
                  && ((termios_p->c_cflag & CSIZE)
Packit Bot 6eebc2
                      != (k_termios.c_cflag & CSIZE))))
Packit Bot 6eebc2
       {
Packit Bot 6eebc2
         /* It looks like the Linux kernel silently changed the
Packit Bot 6eebc2
            PARENB/CREAD/CSIZE bits in c_cflag. Report it as an
Packit Bot 6eebc2
            error. */
Packit Bot 6eebc2
         __set_errno (EINVAL);
Packit Bot 6eebc2
         retval = -1;
Packit Bot 6eebc2
       }
Packit Bot 6eebc2
    }
Packit Bot 6eebc2
Packit Bot 6eebc2
  return retval;
Packit 6c4009
}
Packit 6c4009
weak_alias (__tcsetattr, tcsetattr)
Packit 6c4009
libc_hidden_def (tcsetattr)