ioctl.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // 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. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
  5. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
  6. package unix
  7. import (
  8. "runtime"
  9. "unsafe"
  10. )
  11. // ioctl itself should not be exposed directly, but additional get/set
  12. // functions for specific types are permissible.
  13. // IoctlSetInt performs an ioctl operation which sets an integer value
  14. // on fd, using the specified request number.
  15. func IoctlSetInt(fd int, req uint, value int) error {
  16. return ioctl(fd, req, uintptr(value))
  17. }
  18. // IoctlSetPointerInt performs an ioctl operation which sets an
  19. // integer value on fd, using the specified request number. The ioctl
  20. // argument is called with a pointer to the integer value, rather than
  21. // passing the integer value directly.
  22. func IoctlSetPointerInt(fd int, req uint, value int) error {
  23. v := int32(value)
  24. return ioctl(fd, req, uintptr(unsafe.Pointer(&v)))
  25. }
  26. // IoctlSetWinsize performs an ioctl on fd with a *Winsize argument.
  27. //
  28. // To change fd's window size, the req argument should be TIOCSWINSZ.
  29. func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
  30. // TODO: if we get the chance, remove the req parameter and
  31. // hardcode TIOCSWINSZ.
  32. err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  33. runtime.KeepAlive(value)
  34. return err
  35. }
  36. // IoctlSetTermios performs an ioctl on fd with a *Termios.
  37. //
  38. // The req value will usually be TCSETA or TIOCSETA.
  39. func IoctlSetTermios(fd int, req uint, value *Termios) error {
  40. // TODO: if we get the chance, remove the req parameter.
  41. err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  42. runtime.KeepAlive(value)
  43. return err
  44. }
  45. // IoctlGetInt performs an ioctl operation which gets an integer value
  46. // from fd, using the specified request number.
  47. //
  48. // A few ioctl requests use the return value as an output parameter;
  49. // for those, IoctlRetInt should be used instead of this function.
  50. func IoctlGetInt(fd int, req uint) (int, error) {
  51. var value int
  52. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  53. return value, err
  54. }
  55. func 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. func IoctlGetTermios(fd int, req uint) (*Termios, error) {
  61. var value Termios
  62. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  63. return &value, err
  64. }