syscall.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2009 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. // Package syscall contains an interface to the low-level operating system
  5. // primitives. The details vary depending on the underlying system, and
  6. // by default, godoc will display the syscall documentation for the current
  7. // system. If you want godoc to display syscall documentation for another
  8. // system, set $GOOS and $GOARCH to the desired system. For example, if
  9. // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS
  10. // to freebsd and $GOARCH to arm.
  11. // The primary use of syscall is inside other packages that provide a more
  12. // portable interface to the system, such as "os", "time" and "net". Use
  13. // those packages rather than this one if you can.
  14. // For details of the functions and data types in this package consult
  15. // the manuals for the appropriate operating system.
  16. // These calls return err == nil to indicate success; otherwise
  17. // err is an operating system error describing the failure.
  18. // On most systems, that error has type syscall.Errno.
  19. //
  20. // Deprecated: this package is locked down. Callers should use the
  21. // corresponding package in the golang.org/x/sys repository instead.
  22. // That is also where updates required by new systems or versions
  23. // should be applied. See https://golang.org/s/go1.4-syscall for more
  24. // information.
  25. //
  26. package syscall
  27. import "unsafe"
  28. //go:generate go run ./mksyscall_windows.go -systemdll -output zsyscall_windows.go syscall_windows.go security_windows.go
  29. // StringByteSlice converts a string to a NUL-terminated []byte,
  30. // If s contains a NUL byte this function panics instead of
  31. // returning an error.
  32. //
  33. // Deprecated: Use ByteSliceFromString instead.
  34. func StringByteSlice(s string) []byte {
  35. a, err := ByteSliceFromString(s)
  36. if err != nil {
  37. panic("syscall: string with NUL passed to StringByteSlice")
  38. }
  39. return a
  40. }
  41. // ByteSliceFromString returns a NUL-terminated slice of bytes
  42. // containing the text of s. If s contains a NUL byte at any
  43. // location, it returns (nil, EINVAL).
  44. func ByteSliceFromString(s string) ([]byte, error) {
  45. for i := 0; i < len(s); i++ {
  46. if s[i] == 0 {
  47. return nil, EINVAL
  48. }
  49. }
  50. a := make([]byte, len(s)+1)
  51. copy(a, s)
  52. return a, nil
  53. }
  54. // StringBytePtr returns a pointer to a NUL-terminated array of bytes.
  55. // If s contains a NUL byte this function panics instead of returning
  56. // an error.
  57. //
  58. // Deprecated: Use BytePtrFromString instead.
  59. func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] }
  60. // BytePtrFromString returns a pointer to a NUL-terminated array of
  61. // bytes containing the text of s. If s contains a NUL byte at any
  62. // location, it returns (nil, EINVAL).
  63. func BytePtrFromString(s string) (*byte, error) {
  64. a, err := ByteSliceFromString(s)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &a[0], nil
  69. }
  70. // Single-word zero for use when we need a valid pointer to 0 bytes.
  71. // See mksyscall.pl.
  72. var _zero uintptr
  73. var dummy *byte
  74. const sizeofPtr uintptr = uintptr(unsafe.Sizeof(dummy))
  75. // Unix returns the time stored in ts as seconds plus nanoseconds.
  76. func (ts *Timespec) Unix() (sec int64, nsec int64) {
  77. return int64(ts.Sec), int64(ts.Nsec)
  78. }
  79. // Unix returns the time stored in tv as seconds plus nanoseconds.
  80. func (tv *Timeval) Unix() (sec int64, nsec int64) {
  81. return int64(tv.Sec), int64(tv.Usec) * 1000
  82. }
  83. // Nano returns the time stored in ts as nanoseconds.
  84. func (ts *Timespec) Nano() int64 {
  85. return int64(ts.Sec)*1e9 + int64(ts.Nsec)
  86. }
  87. // Nano returns the time stored in tv as nanoseconds.
  88. func (tv *Timeval) Nano() int64 {
  89. return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
  90. }
  91. // Getpagesize and Exit are provided by the runtime.
  92. func Getpagesize() int
  93. func Exit(code int)