blob: 5641678613c97438c0883d76744a377783df106a [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// 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
Scott Baker8461e152019-10-01 14:44:30 -07009import (
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}
khenaidooac637102019-01-14 15:44:34 -050022
Andrea Campanella3614a922021-02-25 12:40:42 +010023// IoctlSetPointerInt performs an ioctl operation which sets an
24// integer value on fd, using the specified request number. The ioctl
25// argument is called with a pointer to the integer value, rather than
26// passing the integer value directly.
27func IoctlSetPointerInt(fd int, req uint, value int) error {
28 v := int32(value)
29 return ioctl(fd, req, uintptr(unsafe.Pointer(&v)))
30}
31
khenaidooac637102019-01-14 15:44:34 -050032// IoctlSetWinsize performs an ioctl on fd with a *Winsize argument.
33//
34// To change fd's window size, the req argument should be TIOCSWINSZ.
35func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
36 // TODO: if we get the chance, remove the req parameter and
37 // hardcode TIOCSWINSZ.
Scott Baker8461e152019-10-01 14:44:30 -070038 err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
khenaidooac637102019-01-14 15:44:34 -050039 runtime.KeepAlive(value)
40 return err
41}
42
43// IoctlSetTermios performs an ioctl on fd with a *Termios.
44//
45// The req value will usually be TCSETA or TIOCSETA.
46func IoctlSetTermios(fd int, req uint, value *Termios) error {
47 // TODO: if we get the chance, remove the req parameter.
Scott Baker8461e152019-10-01 14:44:30 -070048 err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
khenaidooac637102019-01-14 15:44:34 -050049 runtime.KeepAlive(value)
50 return err
51}
Scott Baker8461e152019-10-01 14:44:30 -070052
53// IoctlGetInt performs an ioctl operation which gets an integer value
54// from fd, using the specified request number.
55//
56// A few ioctl requests use the return value as an output parameter;
57// for those, IoctlRetInt should be used instead of this function.
58func IoctlGetInt(fd int, req uint) (int, error) {
59 var value int
60 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
61 return value, err
62}
63
64func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
65 var value Winsize
66 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
67 return &value, err
68}
69
70func IoctlGetTermios(fd int, req uint) (*Termios, error) {
71 var value Termios
72 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
73 return &value, err
74}