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 Service b4dbef
  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 Service b4dbef
  retval = INLINE_SYSCALL (ioctl, 3, fd, cmd, &k_termios);
Packit Service b4dbef
Packit Service b4dbef
  if (retval == 0 && cmd == TCSETS)
Packit Service b4dbef
    {
Packit Service b4dbef
      /* The Linux kernel has a bug which silently ignore the invalid
Packit Service b4dbef
        c_cflag on pty. We have to check it here. */
Packit Service b4dbef
      int save = errno;
Packit Service b4dbef
      retval = INLINE_SYSCALL (ioctl, 3, fd, TCGETS, &k_termios);
Packit Service b4dbef
      if (retval)
Packit Service b4dbef
       {
Packit Service b4dbef
         /* We cannot verify if the setting is ok. We don't return
Packit Service b4dbef
            an error (?). */
Packit Service b4dbef
         __set_errno (save);
Packit Service b4dbef
         retval = 0;
Packit Service b4dbef
       }
Packit Service b4dbef
      else if ((termios_p->c_cflag & (PARENB | CREAD))
Packit Service b4dbef
              != (k_termios.c_cflag & (PARENB | CREAD))
Packit Service b4dbef
              || ((termios_p->c_cflag & CSIZE)
Packit Service b4dbef
                  && ((termios_p->c_cflag & CSIZE)
Packit Service b4dbef
                      != (k_termios.c_cflag & CSIZE))))
Packit Service b4dbef
       {
Packit Service b4dbef
         /* It looks like the Linux kernel silently changed the
Packit Service b4dbef
            PARENB/CREAD/CSIZE bits in c_cflag. Report it as an
Packit Service b4dbef
            error. */
Packit Service b4dbef
         __set_errno (EINVAL);
Packit Service b4dbef
         retval = -1;
Packit Service b4dbef
       }
Packit Service b4dbef
    }
Packit Service b4dbef
Packit Service b4dbef
  return retval;
Packit 6c4009
}
Packit 6c4009
weak_alias (__tcsetattr, tcsetattr)
Packit 6c4009
libc_hidden_def (tcsetattr)