blob: 791bcbbb678c7bee7466b1216970efdbc7ea940d [file] [log] [blame]
Rob Pike20a02662008-07-26 14:49:21 -07001// 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 Tao6a186d32011-04-20 09:57:05 +10005// Package syscall contains an interface to the low-level operating system
Christopher Nielsen39835b42012-12-17 22:50:00 +08006// 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 Pike6f077962009-03-06 17:20:53 -080015// For details of the functions and data types in this package consult
16// the manuals for the appropriate operating system.
Russ Coxc017a822011-11-13 22:44:52 -050017// 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 Pike160b2462014-08-12 15:28:45 -070020//
21// NOTE: This package is locked down. Code outside the standard
22// Go repository should be migrated to use the corresponding
Joe Shaw11779ef2014-12-22 15:27:46 -050023// package in the golang.org/x/sys repository. That is also where updates
Rob Pike160b2462014-08-12 15:28:45 -070024// required by new systems or versions should be applied.
25// See https://golang.org/s/go1.4-syscall for more information.
26//
Rob Pike20a02662008-07-26 14:49:21 -070027package syscall
28
Russ Coxcf622d72014-09-08 16:59:59 -040029import "unsafe"
30
Shenghou Ma3925a7c2015-05-18 15:50:00 -040031// StringByteSlice converts a string to a NUL-terminated []byte,
Alexey Borzenkova1083692012-08-05 17:24:32 -040032// If s contains a NUL byte this function panics instead of
33// returning an error.
Shenghou Ma3925a7c2015-05-18 15:50:00 -040034//
35// Deprecated: Use ByteSliceFromString instead.
Russ Cox602a4462009-06-01 22:14:57 -070036func StringByteSlice(s string) []byte {
Alexey Borzenkova1083692012-08-05 17:24:32 -040037 a, err := ByteSliceFromString(s)
38 if err != nil {
39 panic("syscall: string with NUL passed to StringByteSlice")
40 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080041 return a
Russ Cox602a4462009-06-01 22:14:57 -070042}
43
Alexey Borzenkova1083692012-08-05 17:24:32 -040044// 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).
47func 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 Ma3925a7c2015-05-18 15:50:00 -040058// 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 Griesemerd65a5cc2009-12-15 15:40:16 -080063func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] }
Russ Cox56faae22011-02-09 14:28:47 -050064
Alexey Borzenkova1083692012-08-05 17:24:32 -040065// 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).
68func 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 Cox56faae22011-02-09 14:28:47 -050076// Single-word zero for use when we need a valid pointer to 0 bytes.
Ian Lance Taylor7959aeb2011-10-25 12:49:51 -070077// See mksyscall.pl.
Russ Cox56faae22011-02-09 14:28:47 -050078var _zero uintptr
Brad Fitzpatrick7d418ae2012-01-18 19:05:44 -080079
80func (ts *Timespec) Unix() (sec int64, nsec int64) {
81 return int64(ts.Sec), int64(ts.Nsec)
82}
83
84func (tv *Timeval) Unix() (sec int64, nsec int64) {
85 return int64(tv.Sec), int64(tv.Usec) * 1000
86}
Russ Cox32f011e2012-02-06 18:04:12 -050087
88func (ts *Timespec) Nano() int64 {
89 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
90}
91
92func (tv *Timeval) Nano() int64 {
93 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
94}
Russ Coxcf622d72014-09-08 16:59:59 -040095
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
99func use(p unsafe.Pointer)