Rob Pike | 20a0266 | 2008-07-26 14:49:21 -0700 | [diff] [blame] | 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 | |
Nigel Tao | 6a186d3 | 2011-04-20 09:57:05 +1000 | [diff] [blame] | 5 | // Package syscall contains an interface to the low-level operating system |
Christopher Nielsen | 39835b4 | 2012-12-17 22:50:00 +0800 | [diff] [blame] | 6 | // primitives. The details vary depending on the underlying system, and |
| 7 | // by default, godoc will display the syscall documentation for the current |
| 8 | // system. If you want godoc to display syscall documentation for another |
| 9 | // system, set $GOOS and $GOARCH to the desired system. For example, if |
| 10 | // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS |
| 11 | // to freebsd and $GOARCH to arm. |
| 12 | // The primary use of syscall is inside other packages that provide a more |
| 13 | // portable interface to the system, such as "os", "time" and "net". Use |
| 14 | // those packages rather than this one if you can. |
Rob Pike | 6f07796 | 2009-03-06 17:20:53 -0800 | [diff] [blame] | 15 | // For details of the functions and data types in this package consult |
| 16 | // the manuals for the appropriate operating system. |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 17 | // These calls return err == nil to indicate success; otherwise |
| 18 | // err is an operating system error describing the failure. |
| 19 | // On most systems, that error has type syscall.Errno. |
Rob Pike | 160b246 | 2014-08-12 15:28:45 -0700 | [diff] [blame] | 20 | // |
| 21 | // NOTE: This package is locked down. Code outside the standard |
| 22 | // Go repository should be migrated to use the corresponding |
Joe Shaw | 11779ef | 2014-12-22 15:27:46 -0500 | [diff] [blame] | 23 | // package in the golang.org/x/sys repository. That is also where updates |
Rob Pike | 160b246 | 2014-08-12 15:28:45 -0700 | [diff] [blame] | 24 | // required by new systems or versions should be applied. |
| 25 | // See https://golang.org/s/go1.4-syscall for more information. |
| 26 | // |
Rob Pike | 20a0266 | 2008-07-26 14:49:21 -0700 | [diff] [blame] | 27 | package syscall |
| 28 | |
Russ Cox | cf622d7 | 2014-09-08 16:59:59 -0400 | [diff] [blame] | 29 | import "unsafe" |
| 30 | |
Shenghou Ma | 3925a7c | 2015-05-18 15:50:00 -0400 | [diff] [blame] | 31 | // StringByteSlice converts a string to a NUL-terminated []byte, |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 32 | // If s contains a NUL byte this function panics instead of |
| 33 | // returning an error. |
Shenghou Ma | 3925a7c | 2015-05-18 15:50:00 -0400 | [diff] [blame] | 34 | // |
| 35 | // Deprecated: Use ByteSliceFromString instead. |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 36 | func StringByteSlice(s string) []byte { |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 37 | a, err := ByteSliceFromString(s) |
| 38 | if err != nil { |
| 39 | panic("syscall: string with NUL passed to StringByteSlice") |
| 40 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 41 | return a |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 44 | // ByteSliceFromString returns a NUL-terminated slice of bytes |
| 45 | // containing the text of s. If s contains a NUL byte at any |
| 46 | // location, it returns (nil, EINVAL). |
| 47 | func ByteSliceFromString(s string) ([]byte, error) { |
| 48 | for i := 0; i < len(s); i++ { |
| 49 | if s[i] == 0 { |
| 50 | return nil, EINVAL |
| 51 | } |
| 52 | } |
| 53 | a := make([]byte, len(s)+1) |
| 54 | copy(a, s) |
| 55 | return a, nil |
| 56 | } |
| 57 | |
Shenghou Ma | 3925a7c | 2015-05-18 15:50:00 -0400 | [diff] [blame] | 58 | // StringBytePtr returns a pointer to a NUL-terminated array of bytes. |
| 59 | // If s contains a NUL byte this function panics instead of returning |
| 60 | // an error. |
| 61 | // |
| 62 | // Deprecated: Use BytePtrFromString instead. |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 63 | func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] } |
Russ Cox | 56faae2 | 2011-02-09 14:28:47 -0500 | [diff] [blame] | 64 | |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 65 | // BytePtrFromString returns a pointer to a NUL-terminated array of |
| 66 | // bytes containing the text of s. If s contains a NUL byte at any |
| 67 | // location, it returns (nil, EINVAL). |
| 68 | func BytePtrFromString(s string) (*byte, error) { |
| 69 | a, err := ByteSliceFromString(s) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | return &a[0], nil |
| 74 | } |
| 75 | |
Russ Cox | 56faae2 | 2011-02-09 14:28:47 -0500 | [diff] [blame] | 76 | // Single-word zero for use when we need a valid pointer to 0 bytes. |
Ian Lance Taylor | 7959aeb | 2011-10-25 12:49:51 -0700 | [diff] [blame] | 77 | // See mksyscall.pl. |
Russ Cox | 56faae2 | 2011-02-09 14:28:47 -0500 | [diff] [blame] | 78 | var _zero uintptr |
Brad Fitzpatrick | 7d418ae | 2012-01-18 19:05:44 -0800 | [diff] [blame] | 79 | |
| 80 | func (ts *Timespec) Unix() (sec int64, nsec int64) { |
| 81 | return int64(ts.Sec), int64(ts.Nsec) |
| 82 | } |
| 83 | |
| 84 | func (tv *Timeval) Unix() (sec int64, nsec int64) { |
| 85 | return int64(tv.Sec), int64(tv.Usec) * 1000 |
| 86 | } |
Russ Cox | 32f011e | 2012-02-06 18:04:12 -0500 | [diff] [blame] | 87 | |
| 88 | func (ts *Timespec) Nano() int64 { |
| 89 | return int64(ts.Sec)*1e9 + int64(ts.Nsec) |
| 90 | } |
| 91 | |
| 92 | func (tv *Timeval) Nano() int64 { |
| 93 | return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 |
| 94 | } |
Russ Cox | cf622d7 | 2014-09-08 16:59:59 -0400 | [diff] [blame] | 95 | |
| 96 | // use is a no-op, but the compiler cannot see that it is. |
| 97 | // Calling use(p) ensures that p is kept live until that point. |
| 98 | //go:noescape |
| 99 | func use(p unsafe.Pointer) |