blob: 3559e5dcb29ee112d2f0703985dc154591efe518 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
6
7package unix
8
divyadesai19009132020-03-04 12:58:08 +00009import (
10 "runtime"
11 "unsafe"
12)
13
14// ioctl itself should not be exposed directly, but additional get/set
15// functions for specific types are permissible.
16
17// IoctlSetInt performs an ioctl operation which sets an integer value
18// on fd, using the specified request number.
19func IoctlSetInt(fd int, req uint, value int) error {
20 return ioctl(fd, req, uintptr(value))
21}
Zack Williamse940c7a2019-08-21 14:25:39 -070022
23// IoctlSetWinsize performs an ioctl on fd with a *Winsize argument.
24//
25// To change fd's window size, the req argument should be TIOCSWINSZ.
26func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
27 // TODO: if we get the chance, remove the req parameter and
28 // hardcode TIOCSWINSZ.
divyadesai19009132020-03-04 12:58:08 +000029 err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
Zack Williamse940c7a2019-08-21 14:25:39 -070030 runtime.KeepAlive(value)
31 return err
32}
33
34// IoctlSetTermios performs an ioctl on fd with a *Termios.
35//
36// The req value will usually be TCSETA or TIOCSETA.
37func IoctlSetTermios(fd int, req uint, value *Termios) error {
38 // TODO: if we get the chance, remove the req parameter.
divyadesai19009132020-03-04 12:58:08 +000039 err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
Zack Williamse940c7a2019-08-21 14:25:39 -070040 runtime.KeepAlive(value)
41 return err
42}
divyadesai19009132020-03-04 12:58:08 +000043
44// IoctlGetInt performs an ioctl operation which gets an integer value
45// from fd, using the specified request number.
46//
47// A few ioctl requests use the return value as an output parameter;
48// for those, IoctlRetInt should be used instead of this function.
49func IoctlGetInt(fd int, req uint) (int, error) {
50 var value int
51 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
52 return value, err
53}
54
55func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
56 var value Winsize
57 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
58 return &value, err
59}
60
61func IoctlGetTermios(fd int, req uint) (*Termios, error) {
62 var value Termios
63 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
64 return &value, err
65}