golang.org/x/sys/unix, termios on Linux

350 views
Skip to first unread message

Nick Patavalis

unread,
Sep 1, 2015, 8:47:20 PM9/1/15
to golang-nuts
Hello Gophers,

I hope this is the right place to report this. 

I'm doing some work in Go involving serial ports. Therefore I have to use the termios structure, and the associated ioctls.

I'm using the package: golang.org/x/sys. In the file "ztypes_linux_amd64.go" (and the equivalent files for other architectures) the "Termios" type is defined (after the termios struct) like this:

  type Termios struct {
      Iflag     uint32
      Oflag     uint32
      Cflag     uint32
      Lflag     uint32
      Line      uint8
      Cc        [32]uint8
      Pad_cgo_0 [3]byte
      Ispeed    uint32
      Ospeed    uint32
  }

Unfortunately, this matches the GLIBC definition of termios, *not* the definition used for communicating with the Linux kernel through the TCSETS* / TCGETS ioctls. 

GLIBC actually copies the fields of this structure to the kernel-compatible one before doing the ioctls. See, for example, here:

  http://osxr.org/glibc/source/sysdeps/unix/sysv/linux/tcsetattr.c

In terms of system header files, the GLIBC termios structure (shown above) is defined in:

  <arch>/bits/termios.h

and the kernel termios structure is define in:

  <arch>/asm/termbits.h

and looks like this:

  #define NCCS 19
  struct termios {
          tcflag_t c_iflag;               /* input mode flags */
          tcflag_t c_oflag;               /* output mode flags */
          tcflag_t c_cflag;               /* control mode flags */
          tcflag_t c_lflag;               /* local mode flags */
          cc_t c_line;                    /* line discipline */
          cc_t c_cc[NCCS];                /* control characters */
  };

For most architectures passing a GLIBC termios struct, instead of a kernel termios structure to the ioctls will not do much harm, since the initial parts of the structures happen to be the same, and the kernel termios is shorter. But this is not universal. For powerpc's, for example, this is not the case.

Furthermore: Recent (after 2.6) linux kernels, also define a termios2 structure which looks like this:

  #define NCCS 19
  struct termios2 {
          tcflag_t c_iflag;               /* input mode flags */
          tcflag_t c_oflag;               /* output mode flags */
          tcflag_t c_cflag;               /* control mode flags */
          tcflag_t c_lflag;               /* local mode flags */
          cc_t c_line;                    /* line discipline */
          cc_t c_cc[NCCS];                /* control characters */
          speed_t c_ispeed;               /* input speed */
          speed_t c_ospeed;               /* output speed */
  };

Using this new structure (and the associated TCSETS*2 and TCGETS2 ioctls) one can set arbitrary baud-rates to serial ports, which is sometimes quite useful.

In addition, several useful tty-related constants are missing from the golang.org/x/sys/unix package. Most importantly:

- CBAUD and CBAUDEX (mask for baud-rate related bits in c_cflag)
- BOTHER (baudrate-value indicating that the actual speed must be taken from c_ispeed or c_ospeed)
- IBSHIFT (shift from output-related to input-related baudrate bits in c_cflag)
- CRTSCTS (used to enable hardware flow-control)

and several others. Some of the ioctl numbers are also missing:

- TCSETSW, TCSETSF, TCSETS2, TCSETSW2, TCSETSF2, TCGETS2

The following rather trivial patch modifies the files "mkerrors.sh" and "types_linux.go" to address these issues. 


After applying the patch, and regenerating the respective auto-generated files, the correct termios definition will be used, a definition for termios2 will be included, as well as several useful tty-related constants.

I include the patch as an attachment, as well.

/npat

linux_termios.patch

Ian Lance Taylor

unread,
Sep 1, 2015, 9:03:22 PM9/1/15
to Nick Patavalis, golang-nuts
On Tue, Sep 1, 2015 at 10:27 AM, Nick Patavalis
<nick.pa...@gmail.com> wrote:
>
> The following rather trivial patch modifies the files "mkerrors.sh" and
> "types_linux.go" to address these issues.
>
> https://github.com/npat-efault/sys/commit/fda8e2119724b3690bb9c1e5f5398593befdf508

Thanks. Would you be able to send this patch using the contribution
process documented at https://golang.org/doc/contribute.html ?

Ian

Nick Patavalis

unread,
Sep 1, 2015, 9:14:49 PM9/1/15
to golang-nuts, nick.pa...@gmail.com


On Wednesday, September 2, 2015 at 4:03:22 AM UTC+3, Ian Lance Taylor wrote:

Thanks.  Would you be able to send this patch using the contribution
process documented at https://golang.org/doc/contribute.html ?


Sure! 

I 've just glanced over the document. I have never used these tools (gerrit?) before. 

So it might take a bit for me to get on with it.

I'll try to do it some time tomorrow (work schedule permitting), or the day after.

Cheers
/npat 

Nick Patavalis

unread,
Sep 3, 2015, 7:45:34 AM9/3/15
to golang-nuts, nick.pa...@gmail.com


On Wednesday, September 2, 2015 at 4:03:22 AM UTC+3, Ian Lance Taylor wrote:

Thanks.  Would you be able to send this patch using the contribution
process documented at https://golang.org/doc/contribute.html ?


Just sent the patch...

BTW: I tried to add a new email-address to my Gerrit account. I received the verification e-mail, but when I clicked on the link I got a "500 Server Error". I tried this 2 or 3 times. Just to let you know.

Cheers
/npat 

Reply all
Reply to author
Forward
0 new messages