diff --git a/bpf/http_sock.c b/bpf/http_sock.c index 6401bc952..e17205125 100644 --- a/bpf/http_sock.c +++ b/bpf/http_sock.c @@ -367,6 +367,16 @@ int BPF_KPROBE(kprobe_tcp_close, struct sock *sk, long timeout) { ensure_sent_event(id, &sock_p); + + pid_connection_info_t info = {}; + + if (parse_sock_info(sk, &info.conn)) { + sort_connection_info(&info.conn); + //dbg_print_http_connection_info(&info.conn); + info.pid = pid_from_pid_tgid(id); + bpf_map_delete_elem(&ongoing_tcp_req, &info); + } + bpf_map_delete_elem(&active_send_args, &id); bpf_map_delete_elem(&active_send_sock_args, &sock_p); diff --git a/bpf/http_sock.h b/bpf/http_sock.h index a9a22d2e1..e9cc48667 100644 --- a/bpf/http_sock.h +++ b/bpf/http_sock.h @@ -384,7 +384,7 @@ static __always_inline void handle_http_response(unsigned char *small_buf, pid_c } } -static __always_inline void http2_grpc_start(http2_conn_stream_t *s_key, void *u_buf, int len, u8 direction) { +static __always_inline void http2_grpc_start(http2_conn_stream_t *s_key, void *u_buf, int len, u8 direction, u8 ssl) { http2_grpc_request_t *h2g_info = empty_http2_info(); if (h2g_info) { http_connection_metadata_t *meta = connection_meta(&s_key->pid_conn, direction, PACKET_TYPE_REQUEST); @@ -396,6 +396,7 @@ static __always_inline void http2_grpc_start(http2_conn_stream_t *s_key, void *u h2g_info->flags = EVENT_K_HTTP2_REQUEST; h2g_info->start_monotime_ns = bpf_ktime_get_ns(); h2g_info->len = len; + h2g_info->ssl = ssl; h2g_info->conn_info = s_key->pid_conn.conn; if (meta) { // keep verifier happy h2g_info->pid = meta->pid; @@ -423,7 +424,7 @@ static __always_inline void http2_grpc_end(http2_conn_stream_t *stream, http2_gr bpf_map_delete_elem(&ongoing_http2_grpc, stream); } -static __always_inline void process_http2_grpc_frames(pid_connection_info_t *pid_conn, void *u_buf, int bytes_len, u8 direction) { +static __always_inline void process_http2_grpc_frames(pid_connection_info_t *pid_conn, void *u_buf, int bytes_len, u8 direction, u8 ssl) { int pos = 0; u8 found_start_frame = 0; u8 found_end_frame = 0; @@ -488,7 +489,7 @@ static __always_inline void process_http2_grpc_frames(pid_connection_info_t *pid } if (found_start_frame) { - http2_grpc_start(&stream, (void *)((u8 *)u_buf + pos), bytes_len, direction); + http2_grpc_start(&stream, (void *)((u8 *)u_buf + pos), bytes_len, direction, ssl); } else { // We only loop 6 times looking for the stream termination. If the data packed is large we'll miss the // frame saying the stream closed. In that case we try this backup path. @@ -550,6 +551,16 @@ static __always_inline void handle_unknown_tcp_connection(pid_connection_info_t bpf_ringbuf_submit(trace, get_flags()); } bpf_map_delete_elem(&ongoing_tcp_req, pid_conn); + } else if (existing->len > 0 && existing->len < (K_TCP_MAX_LEN/2)) { + // Attempt to append one more packet. I couldn't convince the verifier + // to use a variable (K_TCP_MAX_LEN-existing->len). If needed we may need + // to try harder. Mainly needed for userspace detection of missed gRPC, where + // the protocol may sent a RST frame after we've done creating the event, so + // the next event has an RST frame prepended. + u32 off = existing->len; + bpf_clamp_umax(off, (K_TCP_MAX_LEN/2)); + bpf_probe_read(existing->buf + off, (K_TCP_MAX_LEN/2), u_buf); + existing->len += bytes_len; } } @@ -628,7 +639,7 @@ static __always_inline void handle_buf_with_connection(pid_connection_info_t *pi } else { u8 *h2g = bpf_map_lookup_elem(&ongoing_http2_connections, pid_conn); if (h2g && *h2g == ssl) { - process_http2_grpc_frames(pid_conn, u_buf, bytes_len, direction); + process_http2_grpc_frames(pid_conn, u_buf, bytes_len, direction, ssl); } else { // large request tracking http_info_t *info = bpf_map_lookup_elem(&ongoing_http, pid_conn); diff --git a/bpf/http_types.h b/bpf/http_types.h index 4913adc29..453a827cf 100644 --- a/bpf/http_types.h +++ b/bpf/http_types.h @@ -139,6 +139,7 @@ typedef struct http2_grpc_request { // also to filter traces from unsolicited processes that share the executable // with other instrumented processes pid_info pid; + u8 ssl; tp_info_t tp; } http2_grpc_request_t; diff --git a/go.mod b/go.mod index cd1ab6c9a..4cfb2b155 100644 --- a/go.mod +++ b/go.mod @@ -25,8 +25,7 @@ require ( github.com/prometheus/client_model v0.6.0 github.com/prometheus/common v0.48.0 github.com/prometheus/procfs v0.12.0 - github.com/shirou/gopsutil v3.21.11+incompatible - github.com/shirou/gopsutil/v3 v3.24.2 + github.com/shirou/gopsutil/v3 v3.24.4 github.com/stretchr/testify v1.9.0 github.com/vishvananda/netlink v1.1.0 github.com/vladimirvivien/gexe v0.2.0 @@ -36,6 +35,7 @@ require ( go.opentelemetry.io/collector/config/configgrpc v0.97.0 go.opentelemetry.io/collector/config/confighttp v0.97.0 go.opentelemetry.io/collector/config/configopaque v1.4.0 + go.opentelemetry.io/collector/config/configretry v0.97.0 go.opentelemetry.io/collector/config/configtelemetry v0.97.0 go.opentelemetry.io/collector/config/configtls v0.97.0 go.opentelemetry.io/collector/consumer v0.97.0 @@ -131,7 +131,6 @@ require ( github.com/rs/cors v1.10.1 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/objx v0.5.2 // indirect github.com/tklauser/go-sysconf v0.3.13 // indirect github.com/tklauser/numcpus v0.7.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect @@ -142,7 +141,6 @@ require ( go.opentelemetry.io/collector/config/configauth v0.97.0 // indirect go.opentelemetry.io/collector/config/configcompression v1.4.0 // indirect go.opentelemetry.io/collector/config/confignet v0.97.0 // indirect - go.opentelemetry.io/collector/config/configretry v0.97.0 // indirect go.opentelemetry.io/collector/config/internal v0.97.0 // indirect go.opentelemetry.io/collector/confmap v0.97.0 // indirect go.opentelemetry.io/collector/extension v0.97.0 // indirect diff --git a/go.sum b/go.sum index cf6a3e789..404b6d14f 100644 --- a/go.sum +++ b/go.sum @@ -207,10 +207,8 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo= github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= -github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= -github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= -github.com/shirou/gopsutil/v3 v3.24.2 h1:kcR0erMbLg5/3LcInpw0X/rrPSqq4CDPyI6A6ZRC18Y= -github.com/shirou/gopsutil/v3 v3.24.2/go.mod h1:tSg/594BcA+8UdQU2XcW803GWYgdtauFFPgJCJKZlVk= +github.com/shirou/gopsutil/v3 v3.24.4 h1:dEHgzZXt4LMNm+oYELpzl9YCqV65Yr/6SfrvgRBtXeU= +github.com/shirou/gopsutil/v3 v3.24.4/go.mod h1:lTd2mdiOspcqLgAnr9/nGi71NkeMpWKdmhuxm9GusH8= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU= @@ -220,7 +218,6 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -374,7 +371,7 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= diff --git a/pkg/internal/discover/matcher.go b/pkg/internal/discover/matcher.go index ea21ca55c..72c8702f8 100644 --- a/pkg/internal/discover/matcher.go +++ b/pkg/internal/discover/matcher.go @@ -9,7 +9,7 @@ import ( "slices" "github.com/mariomac/pipes/pipe" - "github.com/shirou/gopsutil/process" + "github.com/shirou/gopsutil/v3/process" "github.com/grafana/beyla/pkg/beyla" "github.com/grafana/beyla/pkg/services" diff --git a/pkg/internal/discover/watcher_proc.go b/pkg/internal/discover/watcher_proc.go index 6b1ea067b..3e35d09db 100644 --- a/pkg/internal/discover/watcher_proc.go +++ b/pkg/internal/discover/watcher_proc.go @@ -11,8 +11,8 @@ import ( "time" "github.com/mariomac/pipes/pipe" - "github.com/shirou/gopsutil/net" - "github.com/shirou/gopsutil/process" + "github.com/shirou/gopsutil/v3/net" + "github.com/shirou/gopsutil/v3/process" "github.com/grafana/beyla/pkg/beyla" "github.com/grafana/beyla/pkg/internal/ebpf" diff --git a/pkg/internal/ebpf/bhpack/hpack.go b/pkg/internal/ebpf/bhpack/hpack.go new file mode 100644 index 000000000..60fec0b6d --- /dev/null +++ b/pkg/internal/ebpf/bhpack/hpack.go @@ -0,0 +1,525 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package hpack implements HPACK, a compression format for +// efficiently representing HTTP header fields in the context of HTTP/2. +// +// See http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-09 + +// nolint +package bhpack + +import ( + "bytes" + "errors" + "fmt" +) + +// A DecodingError is something the spec defines as a decoding error. +type DecodingError struct { + Err error +} + +func (de DecodingError) Error() string { + return fmt.Sprintf("decoding error: %v", de.Err) +} + +// An InvalidIndexError is returned when an encoder references a table +// entry before the static table or after the end of the dynamic table. +type InvalidIndexError int + +func (e InvalidIndexError) Error() string { + return fmt.Sprintf("invalid indexed representation index %d", int(e)) +} + +// A HeaderField is a name-value pair. Both the name and value are +// treated as opaque sequences of octets. +type HeaderField struct { + Name, Value string + + // Sensitive means that this header field should never be + // indexed. + Sensitive bool +} + +// IsPseudo reports whether the header field is an http2 pseudo header. +// That is, it reports whether it starts with a colon. +// It is not otherwise guaranteed to be a valid pseudo header field, +// though. +func (hf HeaderField) IsPseudo() bool { + return len(hf.Name) != 0 && hf.Name[0] == ':' +} + +func (hf HeaderField) String() string { + var suffix string + if hf.Sensitive { + suffix = " (sensitive)" + } + return fmt.Sprintf("header field %q = %q%s", hf.Name, hf.Value, suffix) +} + +// Size returns the size of an entry per RFC 7541 section 4.1. +func (hf HeaderField) Size() uint32 { + // https://httpwg.org/specs/rfc7541.html#rfc.section.4.1 + // "The size of the dynamic table is the sum of the size of + // its entries. The size of an entry is the sum of its name's + // length in octets (as defined in Section 5.2), its value's + // length in octets (see Section 5.2), plus 32. The size of + // an entry is calculated using the length of the name and + // value without any Huffman encoding applied." + + // This can overflow if somebody makes a large HeaderField + // Name and/or Value by hand, but we don't care, because that + // won't happen on the wire because the encoding doesn't allow + // it. + return uint32(len(hf.Name) + len(hf.Value) + 32) +} + +// A Decoder is the decoding context for incremental processing of +// header blocks. +type Decoder struct { + dynTab dynamicTable + emit func(f HeaderField) + + emitEnabled bool // whether calls to emit are enabled + maxStrLen int // 0 means unlimited + + // buf is the unparsed buffer. It's only written to + // saveBuf if it was truncated in the middle of a header + // block. Because it's usually not owned, we can only + // process it under Write. + buf []byte // not owned; only valid during Write + + // saveBuf is previous data passed to Write which we weren't able + // to fully parse before. Unlike buf, we own this data. + saveBuf bytes.Buffer + + firstField bool // processing the first field of the header block +} + +// NewDecoder returns a new decoder with the provided maximum dynamic +// table size. The emitFunc will be called for each valid field +// parsed, in the same goroutine as calls to Write, before Write returns. +func NewDecoder(maxDynamicTableSize uint32, emitFunc func(f HeaderField)) *Decoder { + d := &Decoder{ + emit: emitFunc, + emitEnabled: true, + firstField: true, + } + d.dynTab.table.init() + d.dynTab.allowedMaxSize = maxDynamicTableSize + d.dynTab.setMaxSize(maxDynamicTableSize) + return d +} + +// ErrStringLength is returned by Decoder.Write when the max string length +// (as configured by Decoder.SetMaxStringLength) would be violated. +var ErrStringLength = errors.New("hpack: string too long") + +// SetMaxStringLength sets the maximum size of a HeaderField name or +// value string. If a string exceeds this length (even after any +// decompression), Write will return ErrStringLength. +// A value of 0 means unlimited and is the default from NewDecoder. +func (d *Decoder) SetMaxStringLength(n int) { + d.maxStrLen = n +} + +// SetEmitFunc changes the callback used when new header fields +// are decoded. +// It must be non-nil. It does not affect EmitEnabled. +func (d *Decoder) SetEmitFunc(emitFunc func(f HeaderField)) { + d.emit = emitFunc +} + +// SetEmitEnabled controls whether the emitFunc provided to NewDecoder +// should be called. The default is true. +// +// This facility exists to let servers enforce MAX_HEADER_LIST_SIZE +// while still decoding and keeping in-sync with decoder state, but +// without doing unnecessary decompression or generating unnecessary +// garbage for header fields past the limit. +func (d *Decoder) SetEmitEnabled(v bool) { d.emitEnabled = v } + +// EmitEnabled reports whether calls to the emitFunc provided to NewDecoder +// are currently enabled. The default is true. +func (d *Decoder) EmitEnabled() bool { return d.emitEnabled } + +// TODO: add method *Decoder.Reset(maxSize, emitFunc) to let callers re-use Decoders and their +// underlying buffers for garbage reasons. + +func (d *Decoder) SetMaxDynamicTableSize(v uint32) { + d.dynTab.setMaxSize(v) +} + +// SetAllowedMaxDynamicTableSize sets the upper bound that the encoded +// stream (via dynamic table size updates) may set the maximum size +// to. +func (d *Decoder) SetAllowedMaxDynamicTableSize(v uint32) { + d.dynTab.allowedMaxSize = v +} + +type dynamicTable struct { + // https://httpwg.org/specs/rfc7541.html#rfc.section.2.3.2 + table headerFieldTable + size uint32 // in bytes + maxSize uint32 // current maxSize + allowedMaxSize uint32 // maxSize may go up to this, inclusive +} + +func (dt *dynamicTable) setMaxSize(v uint32) { + dt.maxSize = v + dt.evict() +} + +func (dt *dynamicTable) add(f HeaderField) { + dt.table.addEntry(f) + dt.size += f.Size() + dt.evict() +} + +// If we're too big, evict old stuff. +func (dt *dynamicTable) evict() { + var n int + for dt.size > dt.maxSize && n < dt.table.len() { + dt.size -= dt.table.ents[n].Size() + n++ + } + dt.table.evictOldest(n) +} + +func (d *Decoder) maxTableIndex() int { + // This should never overflow. RFC 7540 Section 6.5.2 limits the size of + // the dynamic table to 2^32 bytes, where each entry will occupy more than + // one byte. Further, the staticTable has a fixed, small length. + return d.dynTab.table.len() + staticTable.len() +} + +func (d *Decoder) at(i uint64) (hf HeaderField, ok bool) { + // See Section 2.3.3. + if i == 0 { + return + } + if i <= uint64(staticTable.len()) { + return staticTable.ents[i-1], true + } + if i > uint64(d.maxTableIndex()) { + return + } + // In the dynamic table, newer entries have lower indices. + // However, dt.ents[0] is the oldest entry. Hence, dt.ents is + // the reversed dynamic table. + dt := d.dynTab.table + return dt.ents[dt.len()-(int(i)-staticTable.len())], true +} + +// DecodeFull decodes an entire block. +// +// TODO: remove this method and make it incremental later? This is +// easier for debugging now. +func (d *Decoder) DecodeFull(p []byte) ([]HeaderField, error) { + var hf []HeaderField + saveFunc := d.emit + defer func() { d.emit = saveFunc }() + d.emit = func(f HeaderField) { hf = append(hf, f) } + if _, err := d.Write(p); err != nil { + return nil, err + } + if err := d.Close(); err != nil { + return nil, err + } + return hf, nil +} + +// Close declares that the decoding is complete and resets the Decoder +// to be reused again for a new header block. If there is any remaining +// data in the decoder's buffer, Close returns an error. +func (d *Decoder) Close() error { + if d.saveBuf.Len() > 0 { + d.saveBuf.Reset() + return DecodingError{errors.New("truncated headers")} + } + d.firstField = true + return nil +} + +func (d *Decoder) Write(p []byte) (n int, err error) { + if len(p) == 0 { + // Prevent state machine CPU attacks (making us redo + // work up to the point of finding out we don't have + // enough data) + return + } + // Only copy the data if we have to. Optimistically assume + // that p will contain a complete header block. + if d.saveBuf.Len() == 0 { + d.buf = p + } else { + d.saveBuf.Write(p) + d.buf = d.saveBuf.Bytes() + d.saveBuf.Reset() + } + + for len(d.buf) > 0 { + err = d.parseHeaderFieldRepr() + if err == errNeedMore { + // Extra paranoia, making sure saveBuf won't + // get too large. All the varint and string + // reading code earlier should already catch + // overlong things and return ErrStringLength, + // but keep this as a last resort. + const varIntOverhead = 8 // conservative + if d.maxStrLen != 0 && int64(len(d.buf)) > 2*(int64(d.maxStrLen)+varIntOverhead) { + return 0, ErrStringLength + } + d.saveBuf.Write(d.buf) + return len(p), nil + } + d.firstField = false + if err != nil { + break + } + } + return len(p), err +} + +// errNeedMore is an internal sentinel error value that means the +// buffer is truncated and we need to read more data before we can +// continue parsing. +var errNeedMore = errors.New("need more data") + +type indexType int + +const ( + indexedTrue indexType = iota + indexedFalse + indexedNever +) + +func (v indexType) indexed() bool { return v == indexedTrue } +func (v indexType) sensitive() bool { return v == indexedNever } + +// returns errNeedMore if there isn't enough data available. +// any other error is fatal. +// consumes d.buf iff it returns nil. +// precondition: must be called with len(d.buf) > 0 +func (d *Decoder) parseHeaderFieldRepr() error { + b := d.buf[0] + switch { + case b&128 != 0: + // Indexed representation. + // High bit set? + // https://httpwg.org/specs/rfc7541.html#rfc.section.6.1 + return d.parseFieldIndexed() + case b&192 == 64: + // 6.2.1 Literal Header Field with Incremental Indexing + // 0b10xxxxxx: top two bits are 10 + // https://httpwg.org/specs/rfc7541.html#rfc.section.6.2.1 + return d.parseFieldLiteral(6, indexedTrue) + case b&240 == 0: + // 6.2.2 Literal Header Field without Indexing + // 0b0000xxxx: top four bits are 0000 + // https://httpwg.org/specs/rfc7541.html#rfc.section.6.2.2 + return d.parseFieldLiteral(4, indexedFalse) + case b&240 == 16: + // 6.2.3 Literal Header Field never Indexed + // 0b0001xxxx: top four bits are 0001 + // https://httpwg.org/specs/rfc7541.html#rfc.section.6.2.3 + return d.parseFieldLiteral(4, indexedNever) + case b&224 == 32: + // 6.3 Dynamic Table Size Update + // Top three bits are '001'. + // https://httpwg.org/specs/rfc7541.html#rfc.section.6.3 + return d.parseDynamicTableSizeUpdate() + } + + return DecodingError{errors.New("invalid encoding")} +} + +// (same invariants and behavior as parseHeaderFieldRepr) +func (d *Decoder) parseFieldIndexed() error { + buf := d.buf + idx, buf, err := readVarInt(7, buf) + if err != nil { + return err + } + hf, ok := d.at(idx) + d.buf = buf + if !ok { + return d.callEmit(HeaderField{Name: "", Value: ""}) + } + return d.callEmit(HeaderField{Name: hf.Name, Value: hf.Value}) +} + +// (same invariants and behavior as parseHeaderFieldRepr) +func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error { + buf := d.buf + nameIdx, buf, err := readVarInt(n, buf) + if err != nil { + return err + } + + var hf HeaderField + wantStr := d.emitEnabled || it.indexed() + var undecodedName undecodedString + if nameIdx > 0 { + ihf, ok := d.at(nameIdx) + if !ok { + return DecodingError{InvalidIndexError(nameIdx)} + } + hf.Name = ihf.Name + } else { + undecodedName, buf, err = d.readString(buf) + if err != nil { + return err + } + } + undecodedValue, buf, err := d.readString(buf) + if err != nil { + return err + } + if wantStr { + if nameIdx <= 0 { + hf.Name, err = d.decodeString(undecodedName) + if err != nil { + return err + } + } + hf.Value, err = d.decodeString(undecodedValue) + if err != nil { + return err + } + } + d.buf = buf + if it.indexed() { + d.dynTab.add(hf) + } + hf.Sensitive = it.sensitive() + return d.callEmit(hf) +} + +func (d *Decoder) callEmit(hf HeaderField) error { + if d.maxStrLen != 0 { + if len(hf.Name) > d.maxStrLen || len(hf.Value) > d.maxStrLen { + return ErrStringLength + } + } + if d.emitEnabled { + d.emit(hf) + } + return nil +} + +// (same invariants and behavior as parseHeaderFieldRepr) +func (d *Decoder) parseDynamicTableSizeUpdate() error { + // RFC 7541, sec 4.2: This dynamic table size update MUST occur at the + // beginning of the first header block following the change to the dynamic table size. + if !d.firstField && d.dynTab.size > 0 { + return DecodingError{errors.New("dynamic table size update MUST occur at the beginning of a header block")} + } + + buf := d.buf + size, buf, err := readVarInt(5, buf) + if err != nil { + return err + } + if size > uint64(d.dynTab.allowedMaxSize) { + return DecodingError{errors.New("dynamic table size update too large")} + } + d.dynTab.setMaxSize(uint32(size)) + d.buf = buf + return nil +} + +var errVarintOverflow = DecodingError{errors.New("varint integer overflow")} + +// readVarInt reads an unsigned variable length integer off the +// beginning of p. n is the parameter as described in +// https://httpwg.org/specs/rfc7541.html#rfc.section.5.1. +// +// n must always be between 1 and 8. +// +// The returned remain buffer is either a smaller suffix of p, or err != nil. +// The error is errNeedMore if p doesn't contain a complete integer. +func readVarInt(n byte, p []byte) (i uint64, remain []byte, err error) { + if n < 1 || n > 8 { + panic("bad n") + } + if len(p) == 0 { + return 0, p, errNeedMore + } + i = uint64(p[0]) + if n < 8 { + i &= (1 << uint64(n)) - 1 + } + if i < (1< 0 { + b := p[0] + p = p[1:] + i += uint64(b&127) << m + if b&128 == 0 { + return i, p, nil + } + m += 7 + if m >= 63 { // TODO: proper overflow check. making this up. + return 0, origP, errVarintOverflow + } + } + return 0, origP, errNeedMore +} + +// readString reads an hpack string from p. +// +// It returns a reference to the encoded string data to permit deferring decode costs +// until after the caller verifies all data is present. +func (d *Decoder) readString(p []byte) (u undecodedString, remain []byte, err error) { + if len(p) == 0 { + return u, p, errNeedMore + } + isHuff := p[0]&128 != 0 + strLen, p, err := readVarInt(7, p) + if err != nil { + return u, p, err + } + if d.maxStrLen != 0 && strLen > uint64(d.maxStrLen) { + // Returning an error here means Huffman decoding errors + // for non-indexed strings past the maximum string length + // are ignored, but the server is returning an error anyway + // and because the string is not indexed the error will not + // affect the decoding state. + return u, nil, ErrStringLength + } + if uint64(len(p)) < strLen { + return u, p, errNeedMore + } + u.isHuff = isHuff + u.b = p[:strLen] + return u, p[strLen:], nil +} + +type undecodedString struct { + isHuff bool + b []byte +} + +func (d *Decoder) decodeString(u undecodedString) (string, error) { + if !u.isHuff { + return string(u.b), nil + } + buf := bufPool.Get().(*bytes.Buffer) + buf.Reset() // don't trust others + var s string + err := huffmanDecode(buf, d.maxStrLen, u.b) + if err == nil { + s = buf.String() + } + buf.Reset() // be nice to GC + bufPool.Put(buf) + return s, err +} diff --git a/pkg/internal/ebpf/bhpack/huffman.go b/pkg/internal/ebpf/bhpack/huffman.go new file mode 100644 index 000000000..be029e03b --- /dev/null +++ b/pkg/internal/ebpf/bhpack/huffman.go @@ -0,0 +1,227 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// nolint +package bhpack + +import ( + "bytes" + "errors" + "io" + "sync" +) + +var bufPool = sync.Pool{ + New: func() interface{} { return new(bytes.Buffer) }, +} + +// HuffmanDecode decodes the string in v and writes the expanded +// result to w, returning the number of bytes written to w and the +// Write call's return value. At most one Write call is made. +func HuffmanDecode(w io.Writer, v []byte) (int, error) { + buf := bufPool.Get().(*bytes.Buffer) + buf.Reset() + defer bufPool.Put(buf) + if err := huffmanDecode(buf, 0, v); err != nil { + return 0, err + } + return w.Write(buf.Bytes()) +} + +// HuffmanDecodeToString decodes the string in v. +func HuffmanDecodeToString(v []byte) (string, error) { + buf := bufPool.Get().(*bytes.Buffer) + buf.Reset() + defer bufPool.Put(buf) + if err := huffmanDecode(buf, 0, v); err != nil { + return "", err + } + return buf.String(), nil +} + +// ErrInvalidHuffman is returned for errors found decoding +// Huffman-encoded strings. +var ErrInvalidHuffman = errors.New("hpack: invalid Huffman-encoded data") + +// huffmanDecode decodes v to buf. +// If maxLen is greater than 0, attempts to write more to buf than +// maxLen bytes will return ErrStringLength. +func huffmanDecode(buf *bytes.Buffer, maxLen int, v []byte) error { + rootHuffmanNode := getRootHuffmanNode() + n := rootHuffmanNode + // cur is the bit buffer that has not been fed into n. + // cbits is the number of low order bits in cur that are valid. + // sbits is the number of bits of the symbol prefix being decoded. + cur, cbits, sbits := uint(0), uint8(0), uint8(0) + for _, b := range v { + cur = cur<<8 | uint(b) + cbits += 8 + sbits += 8 + for cbits >= 8 { + idx := byte(cur >> (cbits - 8)) + n = n.children[idx] + if n == nil { + return ErrInvalidHuffman + } + if n.children == nil { + if maxLen != 0 && buf.Len() == maxLen { + return ErrStringLength + } + buf.WriteByte(n.sym) + cbits -= n.codeLen + n = rootHuffmanNode + sbits = cbits + } else { + cbits -= 8 + } + } + } + for cbits > 0 { + n = n.children[byte(cur<<(8-cbits))] + if n == nil { + return ErrInvalidHuffman + } + if n.children != nil || n.codeLen > cbits { + break + } + if maxLen != 0 && buf.Len() == maxLen { + return ErrStringLength + } + buf.WriteByte(n.sym) + cbits -= n.codeLen + n = rootHuffmanNode + sbits = cbits + } + if sbits > 7 { + // Either there was an incomplete symbol, or overlong padding. + // Both are decoding errors per RFC 7541 section 5.2. + return ErrInvalidHuffman + } + if mask := uint(1< 8 { + codeLen -= 8 + i := uint8(code >> codeLen) + if cur.children[i] == nil { + cur.children[i] = newInternalNode() + } + cur = cur.children[i] + } + shift := 8 - codeLen + start, end := int(uint8(code<= 32 { + n %= 32 // Normally would be -= 32 but %= 32 informs compiler 0 <= n <= 31 for upcoming shift + y := uint32(x >> n) // Compiler doesn't combine memory writes if y isn't uint32 + dst = append(dst, byte(y>>24), byte(y>>16), byte(y>>8), byte(y)) + } + } + // Add padding bits if necessary + if over := n % 8; over > 0 { + const ( + eosCode = 0x3fffffff + eosNBits = 30 + eosPadByte = eosCode >> (eosNBits - 8) + ) + pad := 8 - over + x = (x << pad) | (eosPadByte >> over) + n += pad // 8 now divides into n exactly + } + // n in (0, 8, 16, 24, 32) + switch n / 8 { + case 0: + return dst + case 1: + return append(dst, byte(x)) + case 2: + y := uint16(x) + return append(dst, byte(y>>8), byte(y)) + case 3: + y := uint16(x >> 8) + return append(dst, byte(y>>8), byte(y), byte(x)) + } + // case 4: + y := uint32(x) + return append(dst, byte(y>>24), byte(y>>16), byte(y>>8), byte(y)) +} + +// HuffmanEncodeLength returns the number of bytes required to encode +// s in Huffman codes. The result is round up to byte boundary. +func HuffmanEncodeLength(s string) uint64 { + n := uint64(0) + for i := 0; i < len(s); i++ { + n += uint64(huffmanCodeLen[s[i]]) + } + return (n + 7) / 8 +} diff --git a/pkg/internal/ebpf/bhpack/static_table.go b/pkg/internal/ebpf/bhpack/static_table.go new file mode 100644 index 000000000..6e5c4e518 --- /dev/null +++ b/pkg/internal/ebpf/bhpack/static_table.go @@ -0,0 +1,190 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// nolint +package bhpack + +var staticTable = &headerFieldTable{ + evictCount: 0, + byName: map[string]uint64{ + ":authority": 1, + ":method": 3, + ":path": 5, + ":scheme": 7, + ":status": 14, + "accept-charset": 15, + "accept-encoding": 16, + "accept-language": 17, + "accept-ranges": 18, + "accept": 19, + "access-control-allow-origin": 20, + "age": 21, + "allow": 22, + "authorization": 23, + "cache-control": 24, + "content-disposition": 25, + "content-encoding": 26, + "content-language": 27, + "content-length": 28, + "content-location": 29, + "content-range": 30, + "content-type": 31, + "cookie": 32, + "date": 33, + "etag": 34, + "expect": 35, + "expires": 36, + "from": 37, + "host": 38, + "if-match": 39, + "if-modified-since": 40, + "if-none-match": 41, + "if-range": 42, + "if-unmodified-since": 43, + "last-modified": 44, + "link": 45, + "location": 46, + "max-forwards": 47, + "proxy-authenticate": 48, + "proxy-authorization": 49, + "range": 50, + "referer": 51, + "refresh": 52, + "retry-after": 53, + "server": 54, + "set-cookie": 55, + "strict-transport-security": 56, + "transfer-encoding": 57, + "user-agent": 58, + "vary": 59, + "via": 60, + "www-authenticate": 61, + }, + byNameValue: map[pairNameValue]uint64{ + {name: ":authority", value: ""}: 1, + {name: ":method", value: "GET"}: 2, + {name: ":method", value: "POST"}: 3, + {name: ":path", value: "/"}: 4, + {name: ":path", value: "/index.html"}: 5, + {name: ":scheme", value: "http"}: 6, + {name: ":scheme", value: "https"}: 7, + {name: ":status", value: "200"}: 8, + {name: ":status", value: "204"}: 9, + {name: ":status", value: "206"}: 10, + {name: ":status", value: "304"}: 11, + {name: ":status", value: "400"}: 12, + {name: ":status", value: "404"}: 13, + {name: ":status", value: "500"}: 14, + {name: "accept-charset", value: ""}: 15, + {name: "accept-encoding", value: "gzip, deflate"}: 16, + {name: "accept-language", value: ""}: 17, + {name: "accept-ranges", value: ""}: 18, + {name: "accept", value: ""}: 19, + {name: "access-control-allow-origin", value: ""}: 20, + {name: "age", value: ""}: 21, + {name: "allow", value: ""}: 22, + {name: "authorization", value: ""}: 23, + {name: "cache-control", value: ""}: 24, + {name: "content-disposition", value: ""}: 25, + {name: "content-encoding", value: ""}: 26, + {name: "content-language", value: ""}: 27, + {name: "content-length", value: ""}: 28, + {name: "content-location", value: ""}: 29, + {name: "content-range", value: ""}: 30, + {name: "content-type", value: ""}: 31, + {name: "cookie", value: ""}: 32, + {name: "date", value: ""}: 33, + {name: "etag", value: ""}: 34, + {name: "expect", value: ""}: 35, + {name: "expires", value: ""}: 36, + {name: "from", value: ""}: 37, + {name: "host", value: ""}: 38, + {name: "if-match", value: ""}: 39, + {name: "if-modified-since", value: ""}: 40, + {name: "if-none-match", value: ""}: 41, + {name: "if-range", value: ""}: 42, + {name: "if-unmodified-since", value: ""}: 43, + {name: "last-modified", value: ""}: 44, + {name: "link", value: ""}: 45, + {name: "location", value: ""}: 46, + {name: "max-forwards", value: ""}: 47, + {name: "proxy-authenticate", value: ""}: 48, + {name: "proxy-authorization", value: ""}: 49, + {name: "range", value: ""}: 50, + {name: "referer", value: ""}: 51, + {name: "refresh", value: ""}: 52, + {name: "retry-after", value: ""}: 53, + {name: "server", value: ""}: 54, + {name: "set-cookie", value: ""}: 55, + {name: "strict-transport-security", value: ""}: 56, + {name: "transfer-encoding", value: ""}: 57, + {name: "user-agent", value: ""}: 58, + {name: "vary", value: ""}: 59, + {name: "via", value: ""}: 60, + {name: "www-authenticate", value: ""}: 61, + }, + ents: []HeaderField{ + {Name: ":authority", Value: "", Sensitive: false}, + {Name: ":method", Value: "GET", Sensitive: false}, + {Name: ":method", Value: "POST", Sensitive: false}, + {Name: ":path", Value: "/", Sensitive: false}, + {Name: ":path", Value: "/index.html", Sensitive: false}, + {Name: ":scheme", Value: "http", Sensitive: false}, + {Name: ":scheme", Value: "https", Sensitive: false}, + {Name: ":status", Value: "200", Sensitive: false}, + {Name: ":status", Value: "204", Sensitive: false}, + {Name: ":status", Value: "206", Sensitive: false}, + {Name: ":status", Value: "304", Sensitive: false}, + {Name: ":status", Value: "400", Sensitive: false}, + {Name: ":status", Value: "404", Sensitive: false}, + {Name: ":status", Value: "500", Sensitive: false}, + {Name: "accept-charset", Value: "", Sensitive: false}, + {Name: "accept-encoding", Value: "gzip, deflate", Sensitive: false}, + {Name: "accept-language", Value: "", Sensitive: false}, + {Name: "accept-ranges", Value: "", Sensitive: false}, + {Name: "accept", Value: "", Sensitive: false}, + {Name: "access-control-allow-origin", Value: "", Sensitive: false}, + {Name: "age", Value: "", Sensitive: false}, + {Name: "allow", Value: "", Sensitive: false}, + {Name: "authorization", Value: "", Sensitive: false}, + {Name: "cache-control", Value: "", Sensitive: false}, + {Name: "content-disposition", Value: "", Sensitive: false}, + {Name: "content-encoding", Value: "", Sensitive: false}, + {Name: "content-language", Value: "", Sensitive: false}, + {Name: "content-length", Value: "", Sensitive: false}, + {Name: "content-location", Value: "", Sensitive: false}, + {Name: "content-range", Value: "", Sensitive: false}, + {Name: "content-type", Value: "", Sensitive: false}, + {Name: "cookie", Value: "", Sensitive: false}, + {Name: "date", Value: "", Sensitive: false}, + {Name: "etag", Value: "", Sensitive: false}, + {Name: "expect", Value: "", Sensitive: false}, + {Name: "expires", Value: "", Sensitive: false}, + {Name: "from", Value: "", Sensitive: false}, + {Name: "host", Value: "", Sensitive: false}, + {Name: "if-match", Value: "", Sensitive: false}, + {Name: "if-modified-since", Value: "", Sensitive: false}, + {Name: "if-none-match", Value: "", Sensitive: false}, + {Name: "if-range", Value: "", Sensitive: false}, + {Name: "if-unmodified-since", Value: "", Sensitive: false}, + {Name: "last-modified", Value: "", Sensitive: false}, + {Name: "link", Value: "", Sensitive: false}, + {Name: "location", Value: "", Sensitive: false}, + {Name: "max-forwards", Value: "", Sensitive: false}, + {Name: "proxy-authenticate", Value: "", Sensitive: false}, + {Name: "proxy-authorization", Value: "", Sensitive: false}, + {Name: "range", Value: "", Sensitive: false}, + {Name: "referer", Value: "", Sensitive: false}, + {Name: "refresh", Value: "", Sensitive: false}, + {Name: "retry-after", Value: "", Sensitive: false}, + {Name: "server", Value: "", Sensitive: false}, + {Name: "set-cookie", Value: "", Sensitive: false}, + {Name: "strict-transport-security", Value: "", Sensitive: false}, + {Name: "transfer-encoding", Value: "", Sensitive: false}, + {Name: "user-agent", Value: "", Sensitive: false}, + {Name: "vary", Value: "", Sensitive: false}, + {Name: "via", Value: "", Sensitive: false}, + {Name: "www-authenticate", Value: "", Sensitive: false}, + }, +} diff --git a/pkg/internal/ebpf/bhpack/tables.go b/pkg/internal/ebpf/bhpack/tables.go new file mode 100644 index 000000000..eb5e36086 --- /dev/null +++ b/pkg/internal/ebpf/bhpack/tables.go @@ -0,0 +1,404 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// nolint +package bhpack + +import ( + "fmt" +) + +// headerFieldTable implements a list of HeaderFields. +// This is used to implement the static and dynamic tables. +type headerFieldTable struct { + // For static tables, entries are never evicted. + // + // For dynamic tables, entries are evicted from ents[0] and added to the end. + // Each entry has a unique id that starts at one and increments for each + // entry that is added. This unique id is stable across evictions, meaning + // it can be used as a pointer to a specific entry. As in hpack, unique ids + // are 1-based. The unique id for ents[k] is k + evictCount + 1. + // + // Zero is not a valid unique id. + // + // evictCount should not overflow in any remotely practical situation. In + // practice, we will have one dynamic table per HTTP/2 connection. If we + // assume a very powerful server that handles 1M QPS per connection and each + // request adds (then evicts) 100 entries from the table, it would still take + // 2M years for evictCount to overflow. + ents []HeaderField + evictCount uint64 + + // byName maps a HeaderField name to the unique id of the newest entry with + // the same name. See above for a definition of "unique id". + byName map[string]uint64 + + // byNameValue maps a HeaderField name/value pair to the unique id of the newest + // entry with the same name and value. See above for a definition of "unique id". + byNameValue map[pairNameValue]uint64 +} + +type pairNameValue struct { + name, value string +} + +func (t *headerFieldTable) init() { + t.byName = make(map[string]uint64) + t.byNameValue = make(map[pairNameValue]uint64) +} + +// len reports the number of entries in the table. +func (t *headerFieldTable) len() int { + return len(t.ents) +} + +// addEntry adds a new entry. +func (t *headerFieldTable) addEntry(f HeaderField) { + id := uint64(t.len()) + t.evictCount + 1 + t.byName[f.Name] = id + t.byNameValue[pairNameValue{f.Name, f.Value}] = id + t.ents = append(t.ents, f) +} + +// evictOldest evicts the n oldest entries in the table. +func (t *headerFieldTable) evictOldest(n int) { + if n > t.len() { + panic(fmt.Sprintf("evictOldest(%v) on table with %v entries", n, t.len())) + } + for k := 0; k < n; k++ { + f := t.ents[k] + id := t.evictCount + uint64(k) + 1 + if t.byName[f.Name] == id { + delete(t.byName, f.Name) + } + if p := (pairNameValue{f.Name, f.Value}); t.byNameValue[p] == id { + delete(t.byNameValue, p) + } + } + copy(t.ents, t.ents[n:]) + for k := t.len() - n; k < t.len(); k++ { + t.ents[k] = HeaderField{} // so strings can be garbage collected + } + t.ents = t.ents[:t.len()-n] + if t.evictCount+uint64(n) < t.evictCount { + panic("evictCount overflow") + } + t.evictCount += uint64(n) +} + +// search finds f in the table. If there is no match, i is 0. +// If both name and value match, i is the matched index and nameValueMatch +// becomes true. If only name matches, i points to that index and +// nameValueMatch becomes false. +// +// The returned index is a 1-based HPACK index. For dynamic tables, HPACK says +// that index 1 should be the newest entry, but t.ents[0] is the oldest entry, +// meaning t.ents is reversed for dynamic tables. Hence, when t is a dynamic +// table, the return value i actually refers to the entry t.ents[t.len()-i]. +// +// All tables are assumed to be a dynamic tables except for the global staticTable. +// +// See Section 2.3.3. +func (t *headerFieldTable) search(f HeaderField) (i uint64, nameValueMatch bool) { + if !f.Sensitive { + if id := t.byNameValue[pairNameValue{f.Name, f.Value}]; id != 0 { + return t.idToIndex(id), true + } + } + if id := t.byName[f.Name]; id != 0 { + return t.idToIndex(id), false + } + return 0, false +} + +// idToIndex converts a unique id to an HPACK index. +// See Section 2.3.3. +func (t *headerFieldTable) idToIndex(id uint64) uint64 { + if id <= t.evictCount { + panic(fmt.Sprintf("id (%v) <= evictCount (%v)", id, t.evictCount)) + } + k := id - t.evictCount - 1 // convert id to an index t.ents[k] + if t != staticTable { + return uint64(t.len()) - k // dynamic table + } + return k + 1 +} + +var huffmanCodes = [256]uint32{ + 0x1ff8, + 0x7fffd8, + 0xfffffe2, + 0xfffffe3, + 0xfffffe4, + 0xfffffe5, + 0xfffffe6, + 0xfffffe7, + 0xfffffe8, + 0xffffea, + 0x3ffffffc, + 0xfffffe9, + 0xfffffea, + 0x3ffffffd, + 0xfffffeb, + 0xfffffec, + 0xfffffed, + 0xfffffee, + 0xfffffef, + 0xffffff0, + 0xffffff1, + 0xffffff2, + 0x3ffffffe, + 0xffffff3, + 0xffffff4, + 0xffffff5, + 0xffffff6, + 0xffffff7, + 0xffffff8, + 0xffffff9, + 0xffffffa, + 0xffffffb, + 0x14, + 0x3f8, + 0x3f9, + 0xffa, + 0x1ff9, + 0x15, + 0xf8, + 0x7fa, + 0x3fa, + 0x3fb, + 0xf9, + 0x7fb, + 0xfa, + 0x16, + 0x17, + 0x18, + 0x0, + 0x1, + 0x2, + 0x19, + 0x1a, + 0x1b, + 0x1c, + 0x1d, + 0x1e, + 0x1f, + 0x5c, + 0xfb, + 0x7ffc, + 0x20, + 0xffb, + 0x3fc, + 0x1ffa, + 0x21, + 0x5d, + 0x5e, + 0x5f, + 0x60, + 0x61, + 0x62, + 0x63, + 0x64, + 0x65, + 0x66, + 0x67, + 0x68, + 0x69, + 0x6a, + 0x6b, + 0x6c, + 0x6d, + 0x6e, + 0x6f, + 0x70, + 0x71, + 0x72, + 0xfc, + 0x73, + 0xfd, + 0x1ffb, + 0x7fff0, + 0x1ffc, + 0x3ffc, + 0x22, + 0x7ffd, + 0x3, + 0x23, + 0x4, + 0x24, + 0x5, + 0x25, + 0x26, + 0x27, + 0x6, + 0x74, + 0x75, + 0x28, + 0x29, + 0x2a, + 0x7, + 0x2b, + 0x76, + 0x2c, + 0x8, + 0x9, + 0x2d, + 0x77, + 0x78, + 0x79, + 0x7a, + 0x7b, + 0x7ffe, + 0x7fc, + 0x3ffd, + 0x1ffd, + 0xffffffc, + 0xfffe6, + 0x3fffd2, + 0xfffe7, + 0xfffe8, + 0x3fffd3, + 0x3fffd4, + 0x3fffd5, + 0x7fffd9, + 0x3fffd6, + 0x7fffda, + 0x7fffdb, + 0x7fffdc, + 0x7fffdd, + 0x7fffde, + 0xffffeb, + 0x7fffdf, + 0xffffec, + 0xffffed, + 0x3fffd7, + 0x7fffe0, + 0xffffee, + 0x7fffe1, + 0x7fffe2, + 0x7fffe3, + 0x7fffe4, + 0x1fffdc, + 0x3fffd8, + 0x7fffe5, + 0x3fffd9, + 0x7fffe6, + 0x7fffe7, + 0xffffef, + 0x3fffda, + 0x1fffdd, + 0xfffe9, + 0x3fffdb, + 0x3fffdc, + 0x7fffe8, + 0x7fffe9, + 0x1fffde, + 0x7fffea, + 0x3fffdd, + 0x3fffde, + 0xfffff0, + 0x1fffdf, + 0x3fffdf, + 0x7fffeb, + 0x7fffec, + 0x1fffe0, + 0x1fffe1, + 0x3fffe0, + 0x1fffe2, + 0x7fffed, + 0x3fffe1, + 0x7fffee, + 0x7fffef, + 0xfffea, + 0x3fffe2, + 0x3fffe3, + 0x3fffe4, + 0x7ffff0, + 0x3fffe5, + 0x3fffe6, + 0x7ffff1, + 0x3ffffe0, + 0x3ffffe1, + 0xfffeb, + 0x7fff1, + 0x3fffe7, + 0x7ffff2, + 0x3fffe8, + 0x1ffffec, + 0x3ffffe2, + 0x3ffffe3, + 0x3ffffe4, + 0x7ffffde, + 0x7ffffdf, + 0x3ffffe5, + 0xfffff1, + 0x1ffffed, + 0x7fff2, + 0x1fffe3, + 0x3ffffe6, + 0x7ffffe0, + 0x7ffffe1, + 0x3ffffe7, + 0x7ffffe2, + 0xfffff2, + 0x1fffe4, + 0x1fffe5, + 0x3ffffe8, + 0x3ffffe9, + 0xffffffd, + 0x7ffffe3, + 0x7ffffe4, + 0x7ffffe5, + 0xfffec, + 0xfffff3, + 0xfffed, + 0x1fffe6, + 0x3fffe9, + 0x1fffe7, + 0x1fffe8, + 0x7ffff3, + 0x3fffea, + 0x3fffeb, + 0x1ffffee, + 0x1ffffef, + 0xfffff4, + 0xfffff5, + 0x3ffffea, + 0x7ffff4, + 0x3ffffeb, + 0x7ffffe6, + 0x3ffffec, + 0x3ffffed, + 0x7ffffe7, + 0x7ffffe8, + 0x7ffffe9, + 0x7ffffea, + 0x7ffffeb, + 0xffffffe, + 0x7ffffec, + 0x7ffffed, + 0x7ffffee, + 0x7ffffef, + 0x7fffff0, + 0x3ffffee, +} + +var huffmanCodeLen = [256]uint8{ + 13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28, + 28, 28, 28, 28, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6, + 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10, + 13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6, + 15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5, + 6, 7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28, + 20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23, 23, 23, 24, 23, + 24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24, + 22, 21, 20, 22, 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23, + 21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22, 23, 22, 22, 23, + 26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25, + 19, 21, 26, 27, 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27, + 20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25, 24, 24, 26, 23, + 26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26, +} diff --git a/pkg/internal/ebpf/common/bpf_bpfel_arm64.go b/pkg/internal/ebpf/common/bpf_bpfel_arm64.go index b72ad3b32..a10efba51 100644 --- a/pkg/internal/ebpf/common/bpf_bpfel_arm64.go +++ b/pkg/internal/ebpf/common/bpf_bpfel_arm64.go @@ -36,8 +36,9 @@ type bpfHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/common/bpf_bpfel_arm64.o b/pkg/internal/ebpf/common/bpf_bpfel_arm64.o index a92dce46e..fefab1133 100644 Binary files a/pkg/internal/ebpf/common/bpf_bpfel_arm64.o and b/pkg/internal/ebpf/common/bpf_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/common/bpf_bpfel_x86.go b/pkg/internal/ebpf/common/bpf_bpfel_x86.go index c49061fe4..ab5cc9317 100644 --- a/pkg/internal/ebpf/common/bpf_bpfel_x86.go +++ b/pkg/internal/ebpf/common/bpf_bpfel_x86.go @@ -36,8 +36,9 @@ type bpfHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/common/bpf_bpfel_x86.o b/pkg/internal/ebpf/common/bpf_bpfel_x86.o index a92dce46e..fefab1133 100644 Binary files a/pkg/internal/ebpf/common/bpf_bpfel_x86.o and b/pkg/internal/ebpf/common/bpf_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/common/common.go b/pkg/internal/ebpf/common/common.go index 667c8fb88..e40f3d91a 100644 --- a/pkg/internal/ebpf/common/common.go +++ b/pkg/internal/ebpf/common/common.go @@ -25,6 +25,7 @@ type HTTPRequestTrace bpfHttpRequestTrace type SQLRequestTrace bpfSqlRequestTrace type BPFHTTPInfo bpfHttpInfoT type BPFConnInfo bpfConnectionInfoT +type TCPRequestInfo bpfTcpReqT const EventTypeSQL = 5 // EVENT_SQL_CLIENT const EventTypeKHTTP = 6 // HTTP Events generated by kprobes @@ -87,6 +88,13 @@ type Filter struct { Fd int } +type MisclassifiedEvent struct { + EventType int + TCPInfo *TCPRequestInfo +} + +var MisclassifiedEvents = make(chan MisclassifiedEvent) + func ptlog() *slog.Logger { return slog.With("component", "ebpf.ProcessTracer") } func ReadHTTPRequestTraceAsSpan(record *ringbuf.Record) (request.Span, bool, error) { diff --git a/pkg/internal/ebpf/common/http2grpc_transform.go b/pkg/internal/ebpf/common/http2grpc_transform.go index 220e0b9da..3cceb1ebb 100644 --- a/pkg/internal/ebpf/common/http2grpc_transform.go +++ b/pkg/internal/ebpf/common/http2grpc_transform.go @@ -11,8 +11,8 @@ import ( lru "github.com/hashicorp/golang-lru/v2" "go.opentelemetry.io/otel/trace" "golang.org/x/net/http2" - "golang.org/x/net/http2/hpack" + "github.com/grafana/beyla/pkg/internal/ebpf/bhpack" "github.com/grafana/beyla/pkg/internal/request" "github.com/grafana/beyla/pkg/internal/svc" ) @@ -28,12 +28,16 @@ const ( GRPC ) -var hdec = hpack.NewDecoder(0, nil) +type h2Connection struct { + hdec *bhpack.Decoder + hdecRet *bhpack.Decoder + protocol Protocol +} // not all requests for a given stream specify the protocol, but one must // we remember if we see grpc mentioned and tag the rest of the streams for // a given connection as grpc. default assumes plain HTTP2 -var activeGRPCConnections, _ = lru.New[BPFConnInfo, Protocol](1024) +var activeGRPCConnections, _ = lru.New[BPFConnInfo, h2Connection](1024 * 10) func byteFramer(data []uint8) *http2.Framer { buf := bytes.NewBuffer(data) @@ -42,25 +46,44 @@ func byteFramer(data []uint8) *http2.Framer { return fr } -func defaultProtocol(conn *BPFConnInfo) Protocol { - proto, ok := activeGRPCConnections.Get(*conn) +func getOrInitH2Conn(conn *BPFConnInfo) *h2Connection { + v, ok := activeGRPCConnections.Get(*conn) + if !ok { - proto = HTTP2 + h := h2Connection{ + hdec: bhpack.NewDecoder(0, nil), + hdecRet: bhpack.NewDecoder(0, nil), + protocol: HTTP2, + } + activeGRPCConnections.Add(*conn, h) + v, ok = activeGRPCConnections.Get(*conn) + if !ok { + return nil + } } - return proto + return &v } func protocolIsGRPC(conn *BPFConnInfo) { - activeGRPCConnections.Add(*conn, GRPC) + h2c := getOrInitH2Conn(conn) + if h2c != nil { + h2c.protocol = GRPC + } } -func readMetaFrame(conn *BPFConnInfo, fr *http2.Framer, hf *http2.HeadersFrame) (string, string, Protocol) { +func readMetaFrame(conn *BPFConnInfo, fr *http2.Framer, hf *http2.HeadersFrame) (string, string, string) { + h2c := getOrInitH2Conn(conn) + method := "" path := "" - proto := defaultProtocol(conn) + contentType := "" - hdec.SetEmitFunc(func(hf hpack.HeaderField) { + if h2c == nil { + return method, path, contentType + } + + h2c.hdec.SetEmitFunc(func(hf bhpack.HeaderField) { hfKey := strings.ToLower(hf.Name) switch hfKey { case ":method": @@ -68,30 +91,30 @@ func readMetaFrame(conn *BPFConnInfo, fr *http2.Framer, hf *http2.HeadersFrame) case ":path": path = hf.Value case "content-type": - if strings.ToLower(hf.Value) == "application/grpc" { + contentType = strings.ToLower(hf.Value) + if contentType == "application/grpc" { protocolIsGRPC(conn) - proto = GRPC } } }) // Lose reference to MetaHeadersFrame: - defer hdec.SetEmitFunc(func(_ hpack.HeaderField) {}) + defer h2c.hdec.SetEmitFunc(func(_ bhpack.HeaderField) {}) for { frag := hf.HeaderBlockFragment() - if _, err := hdec.Write(frag); err != nil { - return method, path, proto + if _, err := h2c.hdec.Write(frag); err != nil { + return method, path, contentType } if hf.HeadersEnded() { break } if _, err := fr.ReadFrame(); err != nil { - return method, path, proto + return method, path, contentType } } - return method, path, proto + return method, path, contentType } func http2grpcStatus(status int) int { @@ -105,11 +128,17 @@ func http2grpcStatus(status int) int { return 2 // Unknown } -func readRetMetaFrame(conn *BPFConnInfo, fr *http2.Framer, hf *http2.HeadersFrame) (int, Protocol) { +func readRetMetaFrame(conn *BPFConnInfo, fr *http2.Framer, hf *http2.HeadersFrame) (int, bool) { + h2c := getOrInitH2Conn(conn) + status := 0 - proto := defaultProtocol(conn) + grpc := false + + if h2c == nil { + return status, grpc + } - hdec.SetEmitFunc(func(hf hpack.HeaderField) { + h2c.hdecRet.SetEmitFunc(func(hf bhpack.HeaderField) { hfKey := strings.ToLower(hf.Name) // grpc requests may have :status and grpc-status. :status will be HTTP code. // we prefer the grpc one if it exists, it's always later since : tagged headers @@ -117,31 +146,30 @@ func readRetMetaFrame(conn *BPFConnInfo, fr *http2.Framer, hf *http2.HeadersFram switch hfKey { case ":status": status, _ = strconv.Atoi(hf.Value) - proto = HTTP2 case "grpc-status": status, _ = strconv.Atoi(hf.Value) protocolIsGRPC(conn) - proto = GRPC + grpc = true } }) // Lose reference to MetaHeadersFrame: - defer hdec.SetEmitFunc(func(_ hpack.HeaderField) {}) + defer h2c.hdecRet.SetEmitFunc(func(_ bhpack.HeaderField) {}) for { frag := hf.HeaderBlockFragment() - if _, err := hdec.Write(frag); err != nil { - return status, proto + if _, err := h2c.hdecRet.Write(frag); err != nil { + return status, grpc } if hf.HeadersEnded() { break } if _, err := fr.ReadFrame(); err != nil { - return status, proto + return status, grpc } } - return status, proto + return status, grpc } var genericServiceID = svc.ID{SDKLanguage: svc.InstrumentableGeneric} @@ -203,6 +231,7 @@ func (event *BPFHTTP2Info) hostInfo() (source, target string) { return src.String(), dst.String() } +// nolint:cyclop func ReadHTTP2InfoIntoSpan(record *ringbuf.Record) (request.Span, bool, error) { var event BPFHTTP2Info @@ -220,34 +249,50 @@ func ReadHTTP2InfoIntoSpan(record *ringbuf.Record) (request.Span, bool, error) { // we can and terminate without an error when things fail to decode because of // partial buffers. - retF, _ := retFramer.ReadFrame() - status := 0 eventType := HTTP2 - if ff, ok := retF.(*http2.HeadersFrame); ok { - status, eventType = readRetMetaFrame((*BPFConnInfo)(&event.ConnInfo), retFramer, ff) - } + for { + f, err := framer.ReadFrame() - f, _ := framer.ReadFrame() + if err != nil { + break + } - if ff, ok := f.(*http2.HeadersFrame); ok { - method, path, proto := readMetaFrame((*BPFConnInfo)(&event.ConnInfo), framer, ff) + if ff, ok := f.(*http2.HeadersFrame); ok { + method, path, contentType := readMetaFrame((*BPFConnInfo)(&event.ConnInfo), framer, ff) - if eventType != GRPC && proto == GRPC { - eventType = proto - status = http2grpcStatus(status) - } + grpcInStatus := false - peer := "" - host := "" - if event.ConnInfo.S_port != 0 || event.ConnInfo.D_port != 0 { - source, target := event.hostInfo() - host = target - peer = source - } + for { + retF, err := retFramer.ReadFrame() + + if err != nil { + break + } - return http2InfoToSpan(&event, method, path, peer, host, status, eventType), false, nil + if ff, ok := retF.(*http2.HeadersFrame); ok { + status, grpcInStatus = readRetMetaFrame((*BPFConnInfo)(&event.ConnInfo), retFramer, ff) + break + } + } + + // if we don't have protocol, assume gRPC if it's not ssl. HTTP2 is almost always SSL. + if eventType != GRPC && (grpcInStatus || contentType == "application/grpc" || (contentType == "" && event.Ssl == 0)) { + eventType = GRPC + status = http2grpcStatus(status) + } + + peer := "" + host := "" + if event.ConnInfo.S_port != 0 || event.ConnInfo.D_port != 0 { + source, target := event.hostInfo() + host = target + peer = source + } + + return http2InfoToSpan(&event, method, path, peer, host, status, eventType), false, nil + } } return request.Span{}, true, nil // ignore if we couldn't parse it diff --git a/pkg/internal/ebpf/common/tcp_detect_transform.go b/pkg/internal/ebpf/common/tcp_detect_transform.go index 81ddce8e9..23e7d7be7 100644 --- a/pkg/internal/ebpf/common/tcp_detect_transform.go +++ b/pkg/internal/ebpf/common/tcp_detect_transform.go @@ -8,13 +8,12 @@ import ( "github.com/cilium/ebpf/ringbuf" trace2 "go.opentelemetry.io/otel/trace" + "golang.org/x/net/http2" "github.com/grafana/beyla/pkg/internal/request" "github.com/grafana/beyla/pkg/internal/sqlprune" ) -type TCPRequestInfo bpfTcpReqT - func ReadTCPRequestIntoSpan(record *ringbuf.Record) (request.Span, bool, error) { var event TCPRequestInfo @@ -36,6 +35,8 @@ func ReadTCPRequestIntoSpan(record *ringbuf.Record) (request.Span, bool, error) sqlIndex := isSQL(buf) if sqlIndex >= 0 { return TCPToSQLToSpan(&event, buf[sqlIndex:]), false, nil + } else if isHTTP2(b, &event) { + MisclassifiedEvents <- MisclassifiedEvent{EventType: EventTypeKHTTP2, TCPInfo: &event} } return request.Span{}, true, nil // ignore if we couldn't parse it @@ -115,3 +116,22 @@ func TCPToSQLToSpan(trace *TCPRequestInfo, s string) request.Span { Statement: sql, } } + +func isHTTP2(data []uint8, event *TCPRequestInfo) bool { + framer := byteFramer(data) + + for { + f, err := framer.ReadFrame() + + if err != nil { + break + } + + if ff, ok := f.(*http2.HeadersFrame); ok { + method, path, _ := readMetaFrame((*BPFConnInfo)(&event.ConnInfo), framer, ff) + return method != "" || path != "" + } + } + + return false +} diff --git a/pkg/internal/ebpf/goruntime/bpf_bpfel_arm64.o b/pkg/internal/ebpf/goruntime/bpf_bpfel_arm64.o index c4bd4a419..d8938b885 100644 Binary files a/pkg/internal/ebpf/goruntime/bpf_bpfel_arm64.o and b/pkg/internal/ebpf/goruntime/bpf_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/goruntime/bpf_bpfel_x86.o b/pkg/internal/ebpf/goruntime/bpf_bpfel_x86.o index b3ba060d8..2cac940b3 100644 Binary files a/pkg/internal/ebpf/goruntime/bpf_bpfel_x86.o and b/pkg/internal/ebpf/goruntime/bpf_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_arm64.o b/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_arm64.o index 53dfdde86..e9c44dccf 100644 Binary files a/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_arm64.o and b/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_x86.o b/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_x86.o index 8fd77f3c2..9fd163689 100644 Binary files a/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_x86.o and b/pkg/internal/ebpf/goruntime/bpf_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_bpfel_arm64.o b/pkg/internal/ebpf/grpc/bpf_bpfel_arm64.o index 4e35088a6..a90a91da4 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_bpfel_arm64.o and b/pkg/internal/ebpf/grpc/bpf_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_bpfel_x86.o b/pkg/internal/ebpf/grpc/bpf_bpfel_x86.o index 741d354df..39cdd5dcf 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_bpfel_x86.o and b/pkg/internal/ebpf/grpc/bpf_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_debug_bpfel_arm64.o b/pkg/internal/ebpf/grpc/bpf_debug_bpfel_arm64.o index f675af49b..b080d84fe 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_debug_bpfel_arm64.o and b/pkg/internal/ebpf/grpc/bpf_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_debug_bpfel_x86.o b/pkg/internal/ebpf/grpc/bpf_debug_bpfel_x86.o index ca4c72976..b276917e2 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_debug_bpfel_x86.o and b/pkg/internal/ebpf/grpc/bpf_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_tp_bpfel_arm64.o b/pkg/internal/ebpf/grpc/bpf_tp_bpfel_arm64.o index 9bf344a29..90eb4f1a8 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_tp_bpfel_arm64.o and b/pkg/internal/ebpf/grpc/bpf_tp_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_tp_bpfel_x86.o b/pkg/internal/ebpf/grpc/bpf_tp_bpfel_x86.o index 6275ef069..3b20766d0 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_tp_bpfel_x86.o and b/pkg/internal/ebpf/grpc/bpf_tp_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_arm64.o b/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_arm64.o index c44ab54e7..94eaeb3d4 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_arm64.o and b/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_x86.o b/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_x86.o index e57d7a8d7..27e77d730 100644 Binary files a/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_x86.o and b/pkg/internal/ebpf/grpc/bpf_tp_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.go b/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.go index b6c6cda31..1014c1877 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpfHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.o b/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.o index 7138aada0..acda4f766 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.o and b/pkg/internal/ebpf/httpfltr/bpf_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.go b/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.go index 7d8e2e761..a629d7b64 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.go +++ b/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.go @@ -41,8 +41,9 @@ type bpfHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.o b/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.o index 2287f20b5..0cd73c015 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.o and b/pkg/internal/ebpf/httpfltr/bpf_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.go b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.go index 17381d097..b3a83d8d6 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpf_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.o b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.o index 9b40ec121..9607c45a6 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.o and b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.go b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.go index 6b66d597c..3aba5a437 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.go +++ b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.go @@ -41,8 +41,9 @@ type bpf_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.o b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.o index 5f7627c51..aa6515cfa 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.o and b/pkg/internal/ebpf/httpfltr/bpf_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.go b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.go index ee42b1e9f..2f3273729 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpf_tpHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.o b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.o index 6f98d6f92..25569c870 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.o and b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.go b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.go index 9a9733015..4d6bc614a 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.go +++ b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.go @@ -41,8 +41,9 @@ type bpf_tpHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.o b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.o index bad7708fb..df5f8454e 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.o and b/pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.go b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.go index fa1453ab3..cd67e088b 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpf_tp_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.o b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.o index 4439f27ec..c3a9604b9 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.o and b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.go b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.go index 9357cabbc..ad2f98a27 100644 --- a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.go +++ b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.go @@ -41,8 +41,9 @@ type bpf_tp_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.o b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.o index 68d3eed01..b5d601812 100644 Binary files a/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.o and b/pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpfltr/httpfltr.go b/pkg/internal/ebpf/httpfltr/httpfltr.go index b61d4979a..1ba7766fc 100644 --- a/pkg/internal/ebpf/httpfltr/httpfltr.go +++ b/pkg/internal/ebpf/httpfltr/httpfltr.go @@ -247,6 +247,7 @@ func (p *Tracer) Run(ctx context.Context, eventsChan chan<- []request.Span) { timeoutTicker := time.NewTicker(2 * time.Second) + go p.watchForMisclassifedEvents() go p.lookForTimeouts(timeoutTicker, eventsChan) defer timeoutTicker.Stop() @@ -309,3 +310,19 @@ func (p *Tracer) lookForTimeouts(ticker *time.Ticker, eventsChan chan<- []reques } } } + +func (p *Tracer) watchForMisclassifedEvents() { + for e := range ebpfcommon.MisclassifiedEvents { + if e.EventType == ebpfcommon.EventTypeKHTTP2 { + if p.bpfObjects.OngoingHttp2Connections != nil { + err := p.bpfObjects.OngoingHttp2Connections.Put( + &bpfPidConnectionInfoT{Conn: bpfConnectionInfoT(e.TCPInfo.ConnInfo), Pid: e.TCPInfo.Pid.HostPid}, + uint8(e.TCPInfo.Ssl), + ) + if err != nil { + p.log.Debug("error writing HTTP2/gRPC connection info", "error", err) + } + } + } + } +} diff --git a/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.go b/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.go index c7ffe695d..14f62f1a8 100644 --- a/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpfHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.o b/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.o index fbb04279a..9bb675aab 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.o and b/pkg/internal/ebpf/httpssl/bpf_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.go b/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.go index b79cf1711..a33a25c47 100644 --- a/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.go +++ b/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.go @@ -41,8 +41,9 @@ type bpfHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.o b/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.o index fe1d6519d..457dc4978 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.o and b/pkg/internal/ebpf/httpssl/bpf_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.go b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.go index 9399db579..b39ff8832 100644 --- a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpf_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.o b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.o index 8221f9e37..83d5b6ce3 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.o and b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.go b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.go index e112a65a6..6379cd469 100644 --- a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.go +++ b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.go @@ -41,8 +41,9 @@ type bpf_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.o b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.o index dcdd68aeb..966ce77a3 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.o and b/pkg/internal/ebpf/httpssl/bpf_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.go b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.go index be58dfb53..3f98f76ab 100644 --- a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpf_tpHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.o b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.o index ec90904a9..e10856190 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.o and b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.go b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.go index 2726a2ac0..ee867d5f7 100644 --- a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.go +++ b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.go @@ -41,8 +41,9 @@ type bpf_tpHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.o b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.o index 96b88f1b7..961e01c51 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.o and b/pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.go b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.go index bb126a7c9..8b804eaf7 100644 --- a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.go +++ b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.go @@ -41,8 +41,9 @@ type bpf_tp_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.o b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.o index 7b814e94f..a0c31b7c9 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.o and b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.go b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.go index a4d6fa4b9..54cc0384c 100644 --- a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.go +++ b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.go @@ -41,8 +41,9 @@ type bpf_tp_debugHttp2GrpcRequestT struct { UserPid uint32 Ns uint32 } - _ [4]byte - Tp struct { + Ssl uint8 + _ [3]byte + Tp struct { TraceId [16]uint8 SpanId [8]uint8 ParentId [8]uint8 diff --git a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.o b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.o index d32272ebc..d78581f07 100644 Binary files a/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.o and b/pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_bpfel_arm64.o b/pkg/internal/ebpf/nethttp/bpf_bpfel_arm64.o index c6b010467..a9a328351 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_bpfel_arm64.o and b/pkg/internal/ebpf/nethttp/bpf_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_bpfel_x86.o b/pkg/internal/ebpf/nethttp/bpf_bpfel_x86.o index 81bfeccee..f78f25c0d 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_bpfel_x86.o and b/pkg/internal/ebpf/nethttp/bpf_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_arm64.o b/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_arm64.o index 2e925b415..4969b177b 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_arm64.o and b/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_x86.o b/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_x86.o index fd5f59319..9a0bfc7ae 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_x86.o and b/pkg/internal/ebpf/nethttp/bpf_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_arm64.o b/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_arm64.o index af179c9f6..5a4d5bc5d 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_arm64.o and b/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_x86.o b/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_x86.o index 626d8bc72..453786b75 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_x86.o and b/pkg/internal/ebpf/nethttp/bpf_tp_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_arm64.o b/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_arm64.o index 52a8af64e..60a02110c 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_arm64.o and b/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_x86.o b/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_x86.o index 5588ff952..641825f20 100644 Binary files a/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_x86.o and b/pkg/internal/ebpf/nethttp/bpf_tp_debug_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/watcher/bpf_bpfel_arm64.o b/pkg/internal/ebpf/watcher/bpf_bpfel_arm64.o index c373be5aa..bac66b52b 100644 Binary files a/pkg/internal/ebpf/watcher/bpf_bpfel_arm64.o and b/pkg/internal/ebpf/watcher/bpf_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/watcher/bpf_bpfel_x86.o b/pkg/internal/ebpf/watcher/bpf_bpfel_x86.o index 95a3a99a6..c1effca5c 100644 Binary files a/pkg/internal/ebpf/watcher/bpf_bpfel_x86.o and b/pkg/internal/ebpf/watcher/bpf_bpfel_x86.o differ diff --git a/pkg/internal/ebpf/watcher/bpf_debug_bpfel_arm64.o b/pkg/internal/ebpf/watcher/bpf_debug_bpfel_arm64.o index b59e391e2..0065ed246 100644 Binary files a/pkg/internal/ebpf/watcher/bpf_debug_bpfel_arm64.o and b/pkg/internal/ebpf/watcher/bpf_debug_bpfel_arm64.o differ diff --git a/pkg/internal/ebpf/watcher/bpf_debug_bpfel_x86.o b/pkg/internal/ebpf/watcher/bpf_debug_bpfel_x86.o index 01babe5d9..9dbee6684 100644 Binary files a/pkg/internal/ebpf/watcher/bpf_debug_bpfel_x86.o and b/pkg/internal/ebpf/watcher/bpf_debug_bpfel_x86.o differ diff --git a/pkg/internal/export/otel/traces.go b/pkg/internal/export/otel/traces.go index 1351088df..67686f233 100644 --- a/pkg/internal/export/otel/traces.go +++ b/pkg/internal/export/otel/traces.go @@ -15,6 +15,7 @@ import ( "go.opentelemetry.io/collector/config/configgrpc" "go.opentelemetry.io/collector/config/confighttp" "go.opentelemetry.io/collector/config/configopaque" + "go.opentelemetry.io/collector/config/configretry" "go.opentelemetry.io/collector/config/configtelemetry" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/exporter" @@ -65,6 +66,15 @@ type TracesConfig struct { BatchTimeout time.Duration `yaml:"batch_timeout" env:"BEYLA_OTLP_TRACES_BATCH_TIMEOUT"` ExportTimeout time.Duration `yaml:"export_timeout" env:"BEYLA_OTLP_TRACES_EXPORT_TIMEOUT"` + // Configuration optiosn for BackOffConfig of the traces exporter. + // See https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configretry/backoff.go + // BackOffInitialInterval the time to wait after the first failure before retrying. + BackOffInitialInterval time.Duration `yaml:"backoff_initial_interval" env:"BEYLA_BACKOFF_INITIAL_INTERVAL"` + // BackOffMaxInterval is the upper bound on backoff interval. + BackOffMaxInterval time.Duration `yaml:"backoff_max_interval" env:"BEYLA_BACKOFF_MAX_INTERVAL"` + // BackOffMaxElapsedTime is the maximum amount of time (including retries) spent trying to send a request/batch. + BackOffMaxElapsedTime time.Duration `yaml:"backoff_max_elapsed_time" env:"BEYLA_BACKOFF_MAX_ELAPSED_TIME"` + ReportersCacheLen int `yaml:"reporters_cache_len" env:"BEYLA_TRACES_REPORT_CACHE_LEN"` // SDKLogLevel works independently from the global LogLevel because it prints GBs of logs in Debug mode @@ -204,6 +214,7 @@ func getTracesExporter(ctx context.Context, cfg TracesConfig, ctxInfo *global.Co factory := otlphttpexporter.NewFactory() config := factory.CreateDefaultConfig().(*otlphttpexporter.Config) config.QueueConfig.Enabled = false + config.RetryConfig = getRetrySettings(cfg) config.ClientConfig = confighttp.ClientConfig{ Endpoint: endpoint.String(), TLSSetting: configtls.ClientConfig{ @@ -235,6 +246,7 @@ func getTracesExporter(ctx context.Context, cfg TracesConfig, ctxInfo *global.Co factory := otlpexporter.NewFactory() config := factory.CreateDefaultConfig().(*otlpexporter.Config) config.QueueConfig.Enabled = false + config.RetryConfig = getRetrySettings(cfg) config.ClientConfig = configgrpc.ClientConfig{ Endpoint: endpoint.String(), TLSSetting: configtls.ClientConfig{ @@ -289,6 +301,20 @@ func getTraceSettings(ctxInfo *global.ContextInfo, cfg TracesConfig, in trace.Sp } } +func getRetrySettings(cfg TracesConfig) configretry.BackOffConfig { + backOffCfg := configretry.NewDefaultBackOffConfig() + if cfg.BackOffInitialInterval > 0 { + backOffCfg.InitialInterval = cfg.BackOffInitialInterval + } + if cfg.BackOffMaxInterval > 0 { + backOffCfg.MaxInterval = cfg.BackOffMaxInterval + } + if cfg.BackOffMaxElapsedTime > 0 { + backOffCfg.MaxElapsedTime = cfg.BackOffMaxElapsedTime + } + return backOffCfg +} + // GenerateTraces creates a ptrace.Traces from a request.Span func GenerateTraces(span *request.Span, userAttrs map[attr.Name]struct{}) ptrace.Traces { t := span.Timings() diff --git a/vendor/github.com/shirou/gopsutil/LICENSE b/vendor/github.com/shirou/gopsutil/LICENSE deleted file mode 100644 index da71a5e72..000000000 --- a/vendor/github.com/shirou/gopsutil/LICENSE +++ /dev/null @@ -1,61 +0,0 @@ -gopsutil is distributed under BSD license reproduced below. - -Copyright (c) 2014, WAKAYAMA Shirou -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of the gopsutil authors nor the names of its contributors - may be used to endorse or promote products derived from this software without - specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -------- -internal/common/binary.go in the gopsutil is copied and modifid from golang/encoding/binary.go. - - - -Copyright (c) 2009 The Go Authors. All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu.go b/vendor/github.com/shirou/gopsutil/cpu/cpu.go deleted file mode 100644 index 24a81167d..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu.go +++ /dev/null @@ -1,185 +0,0 @@ -package cpu - -import ( - "context" - "encoding/json" - "fmt" - "math" - "strconv" - "strings" - "sync" - "time" - - "github.com/shirou/gopsutil/internal/common" -) - -// TimesStat contains the amounts of time the CPU has spent performing different -// kinds of work. Time units are in seconds. It is based on linux /proc/stat file. -type TimesStat struct { - CPU string `json:"cpu"` - User float64 `json:"user"` - System float64 `json:"system"` - Idle float64 `json:"idle"` - Nice float64 `json:"nice"` - Iowait float64 `json:"iowait"` - Irq float64 `json:"irq"` - Softirq float64 `json:"softirq"` - Steal float64 `json:"steal"` - Guest float64 `json:"guest"` - GuestNice float64 `json:"guestNice"` -} - -type InfoStat struct { - CPU int32 `json:"cpu"` - VendorID string `json:"vendorId"` - Family string `json:"family"` - Model string `json:"model"` - Stepping int32 `json:"stepping"` - PhysicalID string `json:"physicalId"` - CoreID string `json:"coreId"` - Cores int32 `json:"cores"` - ModelName string `json:"modelName"` - Mhz float64 `json:"mhz"` - CacheSize int32 `json:"cacheSize"` - Flags []string `json:"flags"` - Microcode string `json:"microcode"` -} - -type lastPercent struct { - sync.Mutex - lastCPUTimes []TimesStat - lastPerCPUTimes []TimesStat -} - -var lastCPUPercent lastPercent -var invoke common.Invoker = common.Invoke{} - -func init() { - lastCPUPercent.Lock() - lastCPUPercent.lastCPUTimes, _ = Times(false) - lastCPUPercent.lastPerCPUTimes, _ = Times(true) - lastCPUPercent.Unlock() -} - -// Counts returns the number of physical or logical cores in the system -func Counts(logical bool) (int, error) { - return CountsWithContext(context.Background(), logical) -} - -func (c TimesStat) String() string { - v := []string{ - `"cpu":"` + c.CPU + `"`, - `"user":` + strconv.FormatFloat(c.User, 'f', 1, 64), - `"system":` + strconv.FormatFloat(c.System, 'f', 1, 64), - `"idle":` + strconv.FormatFloat(c.Idle, 'f', 1, 64), - `"nice":` + strconv.FormatFloat(c.Nice, 'f', 1, 64), - `"iowait":` + strconv.FormatFloat(c.Iowait, 'f', 1, 64), - `"irq":` + strconv.FormatFloat(c.Irq, 'f', 1, 64), - `"softirq":` + strconv.FormatFloat(c.Softirq, 'f', 1, 64), - `"steal":` + strconv.FormatFloat(c.Steal, 'f', 1, 64), - `"guest":` + strconv.FormatFloat(c.Guest, 'f', 1, 64), - `"guestNice":` + strconv.FormatFloat(c.GuestNice, 'f', 1, 64), - } - - return `{` + strings.Join(v, ",") + `}` -} - -// Total returns the total number of seconds in a CPUTimesStat -func (c TimesStat) Total() float64 { - total := c.User + c.System + c.Nice + c.Iowait + c.Irq + c.Softirq + - c.Steal + c.Idle - return total -} - -func (c InfoStat) String() string { - s, _ := json.Marshal(c) - return string(s) -} - -func getAllBusy(t TimesStat) (float64, float64) { - busy := t.User + t.System + t.Nice + t.Iowait + t.Irq + - t.Softirq + t.Steal - return busy + t.Idle, busy -} - -func calculateBusy(t1, t2 TimesStat) float64 { - t1All, t1Busy := getAllBusy(t1) - t2All, t2Busy := getAllBusy(t2) - - if t2Busy <= t1Busy { - return 0 - } - if t2All <= t1All { - return 100 - } - return math.Min(100, math.Max(0, (t2Busy-t1Busy)/(t2All-t1All)*100)) -} - -func calculateAllBusy(t1, t2 []TimesStat) ([]float64, error) { - // Make sure the CPU measurements have the same length. - if len(t1) != len(t2) { - return nil, fmt.Errorf( - "received two CPU counts: %d != %d", - len(t1), len(t2), - ) - } - - ret := make([]float64, len(t1)) - for i, t := range t2 { - ret[i] = calculateBusy(t1[i], t) - } - return ret, nil -} - -// Percent calculates the percentage of cpu used either per CPU or combined. -// If an interval of 0 is given it will compare the current cpu times against the last call. -// Returns one value per cpu, or a single value if percpu is set to false. -func Percent(interval time.Duration, percpu bool) ([]float64, error) { - return PercentWithContext(context.Background(), interval, percpu) -} - -func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool) ([]float64, error) { - if interval <= 0 { - return percentUsedFromLastCall(percpu) - } - - // Get CPU usage at the start of the interval. - cpuTimes1, err := Times(percpu) - if err != nil { - return nil, err - } - - if err := common.Sleep(ctx, interval); err != nil { - return nil, err - } - - // And at the end of the interval. - cpuTimes2, err := Times(percpu) - if err != nil { - return nil, err - } - - return calculateAllBusy(cpuTimes1, cpuTimes2) -} - -func percentUsedFromLastCall(percpu bool) ([]float64, error) { - cpuTimes, err := Times(percpu) - if err != nil { - return nil, err - } - lastCPUPercent.Lock() - defer lastCPUPercent.Unlock() - var lastTimes []TimesStat - if percpu { - lastTimes = lastCPUPercent.lastPerCPUTimes - lastCPUPercent.lastPerCPUTimes = cpuTimes - } else { - lastTimes = lastCPUPercent.lastCPUTimes - lastCPUPercent.lastCPUTimes = cpuTimes - } - - if lastTimes == nil { - return nil, fmt.Errorf("error getting times for cpu percent. lastTimes was nil") - } - return calculateAllBusy(lastTimes, cpuTimes) -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin.go deleted file mode 100644 index 421f1e16b..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin.go +++ /dev/null @@ -1,112 +0,0 @@ -// +build darwin - -package cpu - -import ( - "context" - "strconv" - "strings" - - "github.com/tklauser/go-sysconf" - "golang.org/x/sys/unix" -) - -// sys/resource.h -const ( - CPUser = 0 - CPNice = 1 - CPSys = 2 - CPIntr = 3 - CPIdle = 4 - CPUStates = 5 -) - -// default value. from time.h -var ClocksPerSec = float64(128) - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClocksPerSec = float64(clkTck) - } -} - -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - if percpu { - return perCPUTimes() - } - - return allCPUTimes() -} - -// Returns only one CPUInfoStat on FreeBSD -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - var ret []InfoStat - - c := InfoStat{} - c.ModelName, _ = unix.Sysctl("machdep.cpu.brand_string") - family, _ := unix.SysctlUint32("machdep.cpu.family") - c.Family = strconv.FormatUint(uint64(family), 10) - model, _ := unix.SysctlUint32("machdep.cpu.model") - c.Model = strconv.FormatUint(uint64(model), 10) - stepping, _ := unix.SysctlUint32("machdep.cpu.stepping") - c.Stepping = int32(stepping) - features, err := unix.Sysctl("machdep.cpu.features") - if err == nil { - for _, v := range strings.Fields(features) { - c.Flags = append(c.Flags, strings.ToLower(v)) - } - } - leaf7Features, err := unix.Sysctl("machdep.cpu.leaf7_features") - if err == nil { - for _, v := range strings.Fields(leaf7Features) { - c.Flags = append(c.Flags, strings.ToLower(v)) - } - } - extfeatures, err := unix.Sysctl("machdep.cpu.extfeatures") - if err == nil { - for _, v := range strings.Fields(extfeatures) { - c.Flags = append(c.Flags, strings.ToLower(v)) - } - } - cores, _ := unix.SysctlUint32("machdep.cpu.core_count") - c.Cores = int32(cores) - cacheSize, _ := unix.SysctlUint32("machdep.cpu.cache.size") - c.CacheSize = int32(cacheSize) - c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor") - - // Use the rated frequency of the CPU. This is a static value and does not - // account for low power or Turbo Boost modes. - cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency") - if err != nil { - return ret, err - } - c.Mhz = float64(cpuFrequency) / 1000000.0 - - return append(ret, c), nil -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - var cpuArgument string - if logical { - cpuArgument = "hw.logicalcpu" - } else { - cpuArgument = "hw.physicalcpu" - } - - count, err := unix.SysctlUint32(cpuArgument) - if err != nil { - return 0, err - } - - return int(count), nil -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin_cgo.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin_cgo.go deleted file mode 100644 index 2a7d4a115..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin_cgo.go +++ /dev/null @@ -1,112 +0,0 @@ -// +build darwin -// +build cgo - -package cpu - -/* -#include -#include -#include -#include -#include -#include -#include -#if TARGET_OS_MAC -#include -#endif -#include -#include -*/ -import "C" - -import ( - "bytes" - "encoding/binary" - "fmt" - "unsafe" -) - -// these CPU times for darwin is borrowed from influxdb/telegraf. - -func perCPUTimes() ([]TimesStat, error) { - var ( - count C.mach_msg_type_number_t - cpuload *C.processor_cpu_load_info_data_t - ncpu C.natural_t - ) - - status := C.host_processor_info(C.host_t(C.mach_host_self()), - C.PROCESSOR_CPU_LOAD_INFO, - &ncpu, - (*C.processor_info_array_t)(unsafe.Pointer(&cpuload)), - &count) - - if status != C.KERN_SUCCESS { - return nil, fmt.Errorf("host_processor_info error=%d", status) - } - - // jump through some cgo casting hoops and ensure we properly free - // the memory that cpuload points to - target := C.vm_map_t(C.mach_task_self_) - address := C.vm_address_t(uintptr(unsafe.Pointer(cpuload))) - defer C.vm_deallocate(target, address, C.vm_size_t(ncpu)) - - // the body of struct processor_cpu_load_info - // aka processor_cpu_load_info_data_t - var cpu_ticks [C.CPU_STATE_MAX]uint32 - - // copy the cpuload array to a []byte buffer - // where we can binary.Read the data - size := int(ncpu) * binary.Size(cpu_ticks) - buf := (*[1 << 30]byte)(unsafe.Pointer(cpuload))[:size:size] - - bbuf := bytes.NewBuffer(buf) - - var ret []TimesStat - - for i := 0; i < int(ncpu); i++ { - err := binary.Read(bbuf, binary.LittleEndian, &cpu_ticks) - if err != nil { - return nil, err - } - - c := TimesStat{ - CPU: fmt.Sprintf("cpu%d", i), - User: float64(cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec, - System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, - Nice: float64(cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, - Idle: float64(cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec, - } - - ret = append(ret, c) - } - - return ret, nil -} - -func allCPUTimes() ([]TimesStat, error) { - var count C.mach_msg_type_number_t - var cpuload C.host_cpu_load_info_data_t - - count = C.HOST_CPU_LOAD_INFO_COUNT - - status := C.host_statistics(C.host_t(C.mach_host_self()), - C.HOST_CPU_LOAD_INFO, - C.host_info_t(unsafe.Pointer(&cpuload)), - &count) - - if status != C.KERN_SUCCESS { - return nil, fmt.Errorf("host_statistics error=%d", status) - } - - c := TimesStat{ - CPU: "cpu-total", - User: float64(cpuload.cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec, - System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, - Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, - Idle: float64(cpuload.cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec, - } - - return []TimesStat{c}, nil - -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin_nocgo.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin_nocgo.go deleted file mode 100644 index 242b4a8e7..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_darwin_nocgo.go +++ /dev/null @@ -1,14 +0,0 @@ -// +build darwin -// +build !cgo - -package cpu - -import "github.com/shirou/gopsutil/internal/common" - -func perCPUTimes() ([]TimesStat, error) { - return []TimesStat{}, common.ErrNotImplementedError -} - -func allCPUTimes() ([]TimesStat, error) { - return []TimesStat{}, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_dragonfly.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_dragonfly.go deleted file mode 100644 index 45094df1d..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_dragonfly.go +++ /dev/null @@ -1,154 +0,0 @@ -package cpu - -import ( - "context" - "fmt" - "reflect" - "regexp" - "runtime" - "strconv" - "strings" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" - "github.com/tklauser/go-sysconf" - "golang.org/x/sys/unix" -) - -var ClocksPerSec = float64(128) -var cpuMatch = regexp.MustCompile(`^CPU:`) -var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`) -var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`) -var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`) -var cpuEnd = regexp.MustCompile(`^Trying to mount root`) -var cpuTimesSize int -var emptyTimes cpuTimes - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClocksPerSec = float64(clkTck) - } -} - -func timeStat(name string, t *cpuTimes) *TimesStat { - return &TimesStat{ - User: float64(t.User) / ClocksPerSec, - Nice: float64(t.Nice) / ClocksPerSec, - System: float64(t.Sys) / ClocksPerSec, - Idle: float64(t.Idle) / ClocksPerSec, - Irq: float64(t.Intr) / ClocksPerSec, - CPU: name, - } -} - -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - if percpu { - buf, err := unix.SysctlRaw("kern.cp_times") - if err != nil { - return nil, err - } - - // We can't do this in init due to the conflict with cpu.init() - if cpuTimesSize == 0 { - cpuTimesSize = int(reflect.TypeOf(cpuTimes{}).Size()) - } - - ncpus := len(buf) / cpuTimesSize - ret := make([]TimesStat, 0, ncpus) - for i := 0; i < ncpus; i++ { - times := (*cpuTimes)(unsafe.Pointer(&buf[i*cpuTimesSize])) - if *times == emptyTimes { - // CPU not present - continue - } - ret = append(ret, *timeStat(fmt.Sprintf("cpu%d", len(ret)), times)) - } - return ret, nil - } - - buf, err := unix.SysctlRaw("kern.cp_time") - if err != nil { - return nil, err - } - - times := (*cpuTimes)(unsafe.Pointer(&buf[0])) - return []TimesStat{*timeStat("cpu-total", times)}, nil -} - -// Returns only one InfoStat on DragonflyBSD. The information regarding core -// count, however is accurate and it is assumed that all InfoStat attributes -// are the same across CPUs. -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - const dmesgBoot = "/var/run/dmesg.boot" - - c, err := parseDmesgBoot(dmesgBoot) - if err != nil { - return nil, err - } - - var u32 uint32 - if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil { - return nil, err - } - c.Mhz = float64(u32) - - var num int - var buf string - if buf, err = unix.Sysctl("hw.cpu_topology.tree"); err != nil { - return nil, err - } - num = strings.Count(buf, "CHIP") - c.Cores = int32(strings.Count(string(buf), "CORE") / num) - - if c.ModelName, err = unix.Sysctl("hw.model"); err != nil { - return nil, err - } - - ret := make([]InfoStat, num) - for i := 0; i < num; i++ { - ret[i] = c - } - - return ret, nil -} - -func parseDmesgBoot(fileName string) (InfoStat, error) { - c := InfoStat{} - lines, _ := common.ReadLines(fileName) - for _, line := range lines { - if matches := cpuEnd.FindStringSubmatch(line); matches != nil { - break - } else if matches := originMatch.FindStringSubmatch(line); matches != nil { - c.VendorID = matches[1] - t, err := strconv.ParseInt(matches[2], 10, 32) - if err != nil { - return c, fmt.Errorf("unable to parse DragonflyBSD CPU stepping information from %q: %v", line, err) - } - c.Stepping = int32(t) - } else if matches := featuresMatch.FindStringSubmatch(line); matches != nil { - for _, v := range strings.Split(matches[1], ",") { - c.Flags = append(c.Flags, strings.ToLower(v)) - } - } else if matches := featuresMatch2.FindStringSubmatch(line); matches != nil { - for _, v := range strings.Split(matches[1], ",") { - c.Flags = append(c.Flags, strings.ToLower(v)) - } - } - } - - return c, nil -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - return runtime.NumCPU(), nil -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_dragonfly_amd64.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_dragonfly_amd64.go deleted file mode 100644 index 57e14528d..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_dragonfly_amd64.go +++ /dev/null @@ -1,9 +0,0 @@ -package cpu - -type cpuTimes struct { - User uint64 - Nice uint64 - Sys uint64 - Intr uint64 - Idle uint64 -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_fallback.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_fallback.go deleted file mode 100644 index 5551c49d1..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_fallback.go +++ /dev/null @@ -1,30 +0,0 @@ -// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly - -package cpu - -import ( - "context" - "runtime" - - "github.com/shirou/gopsutil/internal/common" -) - -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - return []TimesStat{}, common.ErrNotImplementedError -} - -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - return []InfoStat{}, common.ErrNotImplementedError -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - return runtime.NumCPU(), nil -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd.go deleted file mode 100644 index 24527af20..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd.go +++ /dev/null @@ -1,166 +0,0 @@ -package cpu - -import ( - "context" - "fmt" - "reflect" - "regexp" - "runtime" - "strconv" - "strings" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" - "github.com/tklauser/go-sysconf" - "golang.org/x/sys/unix" -) - -var ClocksPerSec = float64(128) -var cpuMatch = regexp.MustCompile(`^CPU:`) -var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`) -var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`) -var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`) -var cpuEnd = regexp.MustCompile(`^Trying to mount root`) -var cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`) -var cpuTimesSize int -var emptyTimes cpuTimes - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClocksPerSec = float64(clkTck) - } -} - -func timeStat(name string, t *cpuTimes) *TimesStat { - return &TimesStat{ - User: float64(t.User) / ClocksPerSec, - Nice: float64(t.Nice) / ClocksPerSec, - System: float64(t.Sys) / ClocksPerSec, - Idle: float64(t.Idle) / ClocksPerSec, - Irq: float64(t.Intr) / ClocksPerSec, - CPU: name, - } -} - -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - if percpu { - buf, err := unix.SysctlRaw("kern.cp_times") - if err != nil { - return nil, err - } - - // We can't do this in init due to the conflict with cpu.init() - if cpuTimesSize == 0 { - cpuTimesSize = int(reflect.TypeOf(cpuTimes{}).Size()) - } - - ncpus := len(buf) / cpuTimesSize - ret := make([]TimesStat, 0, ncpus) - for i := 0; i < ncpus; i++ { - times := (*cpuTimes)(unsafe.Pointer(&buf[i*cpuTimesSize])) - if *times == emptyTimes { - // CPU not present - continue - } - ret = append(ret, *timeStat(fmt.Sprintf("cpu%d", len(ret)), times)) - } - return ret, nil - } - - buf, err := unix.SysctlRaw("kern.cp_time") - if err != nil { - return nil, err - } - - times := (*cpuTimes)(unsafe.Pointer(&buf[0])) - return []TimesStat{*timeStat("cpu-total", times)}, nil -} - -// Returns only one InfoStat on FreeBSD. The information regarding core -// count, however is accurate and it is assumed that all InfoStat attributes -// are the same across CPUs. -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - const dmesgBoot = "/var/run/dmesg.boot" - - c, num, err := parseDmesgBoot(dmesgBoot) - if err != nil { - return nil, err - } - - var u32 uint32 - if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil { - return nil, err - } - c.Mhz = float64(u32) - - if u32, err = unix.SysctlUint32("hw.ncpu"); err != nil { - return nil, err - } - c.Cores = int32(u32) - - if c.ModelName, err = unix.Sysctl("hw.model"); err != nil { - return nil, err - } - - ret := make([]InfoStat, num) - for i := 0; i < num; i++ { - ret[i] = c - } - - return ret, nil -} - -func parseDmesgBoot(fileName string) (InfoStat, int, error) { - c := InfoStat{} - lines, _ := common.ReadLines(fileName) - cpuNum := 1 // default cpu num is 1 - for _, line := range lines { - if matches := cpuEnd.FindStringSubmatch(line); matches != nil { - break - } else if matches := originMatch.FindStringSubmatch(line); matches != nil { - c.VendorID = matches[1] - c.Family = matches[3] - c.Model = matches[4] - t, err := strconv.ParseInt(matches[5], 10, 32) - if err != nil { - return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %v", line, err) - } - c.Stepping = int32(t) - } else if matches := featuresMatch.FindStringSubmatch(line); matches != nil { - for _, v := range strings.Split(matches[1], ",") { - c.Flags = append(c.Flags, strings.ToLower(v)) - } - } else if matches := featuresMatch2.FindStringSubmatch(line); matches != nil { - for _, v := range strings.Split(matches[1], ",") { - c.Flags = append(c.Flags, strings.ToLower(v)) - } - } else if matches := cpuCores.FindStringSubmatch(line); matches != nil { - t, err := strconv.ParseInt(matches[1], 10, 32) - if err != nil { - return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %v", line, err) - } - cpuNum = int(t) - t2, err := strconv.ParseInt(matches[2], 10, 32) - if err != nil { - return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %v", line, err) - } - c.Cores = int32(t2) - } - } - - return c, cpuNum, nil -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - return runtime.NumCPU(), nil -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_386.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_386.go deleted file mode 100644 index 8b7f4c321..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_386.go +++ /dev/null @@ -1,9 +0,0 @@ -package cpu - -type cpuTimes struct { - User uint32 - Nice uint32 - Sys uint32 - Intr uint32 - Idle uint32 -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_amd64.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_amd64.go deleted file mode 100644 index 57e14528d..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_amd64.go +++ /dev/null @@ -1,9 +0,0 @@ -package cpu - -type cpuTimes struct { - User uint64 - Nice uint64 - Sys uint64 - Intr uint64 - Idle uint64 -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_arm.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_arm.go deleted file mode 100644 index 8b7f4c321..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_arm.go +++ /dev/null @@ -1,9 +0,0 @@ -package cpu - -type cpuTimes struct { - User uint32 - Nice uint32 - Sys uint32 - Intr uint32 - Idle uint32 -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_arm64.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_arm64.go deleted file mode 100644 index 57e14528d..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_freebsd_arm64.go +++ /dev/null @@ -1,9 +0,0 @@ -package cpu - -type cpuTimes struct { - User uint64 - Nice uint64 - Sys uint64 - Intr uint64 - Idle uint64 -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go deleted file mode 100644 index 21006bc33..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go +++ /dev/null @@ -1,406 +0,0 @@ -// +build linux - -package cpu - -import ( - "context" - "errors" - "fmt" - "path/filepath" - "strconv" - "strings" - - "github.com/shirou/gopsutil/internal/common" - "github.com/tklauser/go-sysconf" -) - -var ClocksPerSec = float64(100) - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClocksPerSec = float64(clkTck) - } -} - -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - filename := common.HostProc("stat") - var lines = []string{} - if percpu { - statlines, err := common.ReadLines(filename) - if err != nil || len(statlines) < 2 { - return []TimesStat{}, nil - } - for _, line := range statlines[1:] { - if !strings.HasPrefix(line, "cpu") { - break - } - lines = append(lines, line) - } - } else { - lines, _ = common.ReadLinesOffsetN(filename, 0, 1) - } - - ret := make([]TimesStat, 0, len(lines)) - - for _, line := range lines { - ct, err := parseStatLine(line) - if err != nil { - continue - } - ret = append(ret, *ct) - - } - return ret, nil -} - -func sysCPUPath(cpu int32, relPath string) string { - return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath) -} - -func finishCPUInfo(c *InfoStat) error { - var lines []string - var err error - var value float64 - - if len(c.CoreID) == 0 { - lines, err = common.ReadLines(sysCPUPath(c.CPU, "topology/core_id")) - if err == nil { - c.CoreID = lines[0] - } - } - - // override the value of c.Mhz with cpufreq/cpuinfo_max_freq regardless - // of the value from /proc/cpuinfo because we want to report the maximum - // clock-speed of the CPU for c.Mhz, matching the behaviour of Windows - lines, err = common.ReadLines(sysCPUPath(c.CPU, "cpufreq/cpuinfo_max_freq")) - // if we encounter errors below such as there are no cpuinfo_max_freq file, - // we just ignore. so let Mhz is 0. - if err != nil || len(lines) == 0 { - return nil - } - value, err = strconv.ParseFloat(lines[0], 64) - if err != nil { - return nil - } - c.Mhz = value / 1000.0 // value is in kHz - if c.Mhz > 9999 { - c.Mhz = c.Mhz / 1000.0 // value in Hz - } - return nil -} - -// CPUInfo on linux will return 1 item per physical thread. -// -// CPUs have three levels of counting: sockets, cores, threads. -// Cores with HyperThreading count as having 2 threads per core. -// Sockets often come with many physical CPU cores. -// For example a single socket board with two cores each with HT will -// return 4 CPUInfoStat structs on Linux and the "Cores" field set to 1. -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - filename := common.HostProc("cpuinfo") - lines, _ := common.ReadLines(filename) - - var ret []InfoStat - var processorName string - - c := InfoStat{CPU: -1, Cores: 1} - for _, line := range lines { - fields := strings.Split(line, ":") - if len(fields) < 2 { - continue - } - key := strings.TrimSpace(fields[0]) - value := strings.TrimSpace(fields[1]) - - switch key { - case "Processor": - processorName = value - case "processor": - if c.CPU >= 0 { - err := finishCPUInfo(&c) - if err != nil { - return ret, err - } - ret = append(ret, c) - } - c = InfoStat{Cores: 1, ModelName: processorName} - t, err := strconv.ParseInt(value, 10, 64) - if err != nil { - return ret, err - } - c.CPU = int32(t) - case "vendorId", "vendor_id": - c.VendorID = value - case "CPU implementer": - if v, err := strconv.ParseUint(value, 0, 8); err == nil { - switch v { - case 0x41: - c.VendorID = "ARM" - case 0x42: - c.VendorID = "Broadcom" - case 0x43: - c.VendorID = "Cavium" - case 0x44: - c.VendorID = "DEC" - case 0x46: - c.VendorID = "Fujitsu" - case 0x48: - c.VendorID = "HiSilicon" - case 0x49: - c.VendorID = "Infineon" - case 0x4d: - c.VendorID = "Motorola/Freescale" - case 0x4e: - c.VendorID = "NVIDIA" - case 0x50: - c.VendorID = "APM" - case 0x51: - c.VendorID = "Qualcomm" - case 0x56: - c.VendorID = "Marvell" - case 0x61: - c.VendorID = "Apple" - case 0x69: - c.VendorID = "Intel" - case 0xc0: - c.VendorID = "Ampere" - } - } - case "cpu family": - c.Family = value - case "model", "CPU part": - c.Model = value - case "model name", "cpu": - c.ModelName = value - if strings.Contains(value, "POWER8") || - strings.Contains(value, "POWER7") { - c.Model = strings.Split(value, " ")[0] - c.Family = "POWER" - c.VendorID = "IBM" - } - case "stepping", "revision", "CPU revision": - val := value - - if key == "revision" { - val = strings.Split(value, ".")[0] - } - - t, err := strconv.ParseInt(val, 10, 64) - if err != nil { - return ret, err - } - c.Stepping = int32(t) - case "cpu MHz", "clock": - // treat this as the fallback value, thus we ignore error - if t, err := strconv.ParseFloat(strings.Replace(value, "MHz", "", 1), 64); err == nil { - c.Mhz = t - } - case "cache size": - t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64) - if err != nil { - return ret, err - } - c.CacheSize = int32(t) - case "physical id": - c.PhysicalID = value - case "core id": - c.CoreID = value - case "flags", "Features": - c.Flags = strings.FieldsFunc(value, func(r rune) bool { - return r == ',' || r == ' ' - }) - case "microcode": - c.Microcode = value - } - } - if c.CPU >= 0 { - err := finishCPUInfo(&c) - if err != nil { - return ret, err - } - ret = append(ret, c) - } - return ret, nil -} - -func parseStatLine(line string) (*TimesStat, error) { - fields := strings.Fields(line) - - if len(fields) == 0 { - return nil, errors.New("stat does not contain cpu info") - } - - if strings.HasPrefix(fields[0], "cpu") == false { - return nil, errors.New("not contain cpu") - } - - cpu := fields[0] - if cpu == "cpu" { - cpu = "cpu-total" - } - user, err := strconv.ParseFloat(fields[1], 64) - if err != nil { - return nil, err - } - nice, err := strconv.ParseFloat(fields[2], 64) - if err != nil { - return nil, err - } - system, err := strconv.ParseFloat(fields[3], 64) - if err != nil { - return nil, err - } - idle, err := strconv.ParseFloat(fields[4], 64) - if err != nil { - return nil, err - } - iowait, err := strconv.ParseFloat(fields[5], 64) - if err != nil { - return nil, err - } - irq, err := strconv.ParseFloat(fields[6], 64) - if err != nil { - return nil, err - } - softirq, err := strconv.ParseFloat(fields[7], 64) - if err != nil { - return nil, err - } - - ct := &TimesStat{ - CPU: cpu, - User: user / ClocksPerSec, - Nice: nice / ClocksPerSec, - System: system / ClocksPerSec, - Idle: idle / ClocksPerSec, - Iowait: iowait / ClocksPerSec, - Irq: irq / ClocksPerSec, - Softirq: softirq / ClocksPerSec, - } - if len(fields) > 8 { // Linux >= 2.6.11 - steal, err := strconv.ParseFloat(fields[8], 64) - if err != nil { - return nil, err - } - ct.Steal = steal / ClocksPerSec - } - if len(fields) > 9 { // Linux >= 2.6.24 - guest, err := strconv.ParseFloat(fields[9], 64) - if err != nil { - return nil, err - } - ct.Guest = guest / ClocksPerSec - } - if len(fields) > 10 { // Linux >= 3.2.0 - guestNice, err := strconv.ParseFloat(fields[10], 64) - if err != nil { - return nil, err - } - ct.GuestNice = guestNice / ClocksPerSec - } - - return ct, nil -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - if logical { - ret := 0 - // https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_pslinux.py#L599 - procCpuinfo := common.HostProc("cpuinfo") - lines, err := common.ReadLines(procCpuinfo) - if err == nil { - for _, line := range lines { - line = strings.ToLower(line) - if strings.HasPrefix(line, "processor") { - _, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:])) - if err == nil { - ret++ - } - } - } - } - if ret == 0 { - procStat := common.HostProc("stat") - lines, err = common.ReadLines(procStat) - if err != nil { - return 0, err - } - for _, line := range lines { - if len(line) >= 4 && strings.HasPrefix(line, "cpu") && '0' <= line[3] && line[3] <= '9' { // `^cpu\d` regexp matching - ret++ - } - } - } - return ret, nil - } - // physical cores - // https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/_pslinux.py#L615-L628 - var threadSiblingsLists = make(map[string]bool) - // These 2 files are the same but */core_cpus_list is newer while */thread_siblings_list is deprecated and may disappear in the future. - // https://www.kernel.org/doc/Documentation/admin-guide/cputopology.rst - // https://github.com/giampaolo/psutil/pull/1727#issuecomment-707624964 - // https://lkml.org/lkml/2019/2/26/41 - for _, glob := range []string{"devices/system/cpu/cpu[0-9]*/topology/core_cpus_list", "devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list"} { - if files, err := filepath.Glob(common.HostSys(glob)); err == nil { - for _, file := range files { - lines, err := common.ReadLines(file) - if err != nil || len(lines) != 1 { - continue - } - threadSiblingsLists[lines[0]] = true - } - ret := len(threadSiblingsLists) - if ret != 0 { - return ret, nil - } - } - } - // https://github.com/giampaolo/psutil/blob/122174a10b75c9beebe15f6c07dcf3afbe3b120d/psutil/_pslinux.py#L631-L652 - filename := common.HostProc("cpuinfo") - lines, err := common.ReadLines(filename) - if err != nil { - return 0, err - } - mapping := make(map[int]int) - currentInfo := make(map[string]int) - for _, line := range lines { - line = strings.ToLower(strings.TrimSpace(line)) - if line == "" { - // new section - id, okID := currentInfo["physical id"] - cores, okCores := currentInfo["cpu cores"] - if okID && okCores { - mapping[id] = cores - } - currentInfo = make(map[string]int) - continue - } - fields := strings.Split(line, ":") - if len(fields) < 2 { - continue - } - fields[0] = strings.TrimSpace(fields[0]) - if fields[0] == "physical id" || fields[0] == "cpu cores" { - val, err := strconv.Atoi(strings.TrimSpace(fields[1])) - if err != nil { - continue - } - currentInfo[fields[0]] = val - } - } - ret := 0 - for _, v := range mapping { - ret += v - } - return ret, nil -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_openbsd.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_openbsd.go deleted file mode 100644 index c761aa21d..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_openbsd.go +++ /dev/null @@ -1,178 +0,0 @@ -// +build openbsd - -package cpu - -import ( - "bytes" - "context" - "encoding/binary" - "fmt" - "runtime" - "strconv" - "strings" - "syscall" - - "github.com/shirou/gopsutil/internal/common" - "github.com/tklauser/go-sysconf" - "golang.org/x/sys/unix" -) - -// sys/sched.h -var ( - CPUser = 0 - CPNice = 1 - CPSys = 2 - CPIntr = 3 - CPIdle = 4 - CPUStates = 5 -) - -// sys/sysctl.h -const ( - CTLKern = 1 // "high kernel": proc, limits - CTLHw = 6 // CTL_HW - SMT = 24 // HW_SMT - KernCptime = 40 // KERN_CPTIME - KernCptime2 = 71 // KERN_CPTIME2 -) - -var ClocksPerSec = float64(128) - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClocksPerSec = float64(clkTck) - } - - func() { - v, err := unix.Sysctl("kern.osrelease") // can't reuse host.PlatformInformation because of circular import - if err != nil { - return - } - v = strings.ToLower(v) - version, err := strconv.ParseFloat(v, 64) - if err != nil { - return - } - if version >= 6.4 { - CPIntr = 4 - CPIdle = 5 - CPUStates = 6 - } - }() -} - -func smt() (bool, error) { - mib := []int32{CTLHw, SMT} - buf, _, err := common.CallSyscall(mib) - if err != nil { - return false, err - } - - var ret bool - br := bytes.NewReader(buf) - if err := binary.Read(br, binary.LittleEndian, &ret); err != nil { - return false, err - } - - return ret, nil -} - -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - var ret []TimesStat - - var ncpu int - if percpu { - ncpu, _ = Counts(true) - } else { - ncpu = 1 - } - - smt, err := smt() - if err == syscall.EOPNOTSUPP { - // if hw.smt is not applicable for this platform (e.g. i386), - // pretend it's enabled - smt = true - } else if err != nil { - return nil, err - } - - for i := 0; i < ncpu; i++ { - j := i - if !smt { - j *= 2 - } - - var cpuTimes = make([]int32, CPUStates) - var mib []int32 - if percpu { - mib = []int32{CTLKern, KernCptime2, int32(j)} - } else { - mib = []int32{CTLKern, KernCptime} - } - buf, _, err := common.CallSyscall(mib) - if err != nil { - return ret, err - } - - br := bytes.NewReader(buf) - err = binary.Read(br, binary.LittleEndian, &cpuTimes) - if err != nil { - return ret, err - } - c := TimesStat{ - User: float64(cpuTimes[CPUser]) / ClocksPerSec, - Nice: float64(cpuTimes[CPNice]) / ClocksPerSec, - System: float64(cpuTimes[CPSys]) / ClocksPerSec, - Idle: float64(cpuTimes[CPIdle]) / ClocksPerSec, - Irq: float64(cpuTimes[CPIntr]) / ClocksPerSec, - } - if percpu { - c.CPU = fmt.Sprintf("cpu%d", j) - } else { - c.CPU = "cpu-total" - } - ret = append(ret, c) - } - - return ret, nil -} - -// Returns only one (minimal) CPUInfoStat on OpenBSD -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - var ret []InfoStat - var err error - - c := InfoStat{} - - mhz, err := unix.SysctlUint32("hw.cpuspeed") - if err != nil { - return nil, err - } - c.Mhz = float64(mhz) - - ncpu, err := unix.SysctlUint32("hw.ncpuonline") - if err != nil { - return nil, err - } - c.Cores = int32(ncpu) - - if c.ModelName, err = unix.Sysctl("hw.model"); err != nil { - return nil, err - } - - return append(ret, c), nil -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - return runtime.NumCPU(), nil -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_solaris.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_solaris.go deleted file mode 100644 index 4e3dea02a..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_solaris.go +++ /dev/null @@ -1,281 +0,0 @@ -package cpu - -import ( - "context" - "errors" - "fmt" - "os/exec" - "regexp" - "runtime" - "sort" - "strconv" - "strings" - - "github.com/tklauser/go-sysconf" -) - -var ClocksPerSec = float64(128) - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClocksPerSec = float64(clkTck) - } -} - -//sum all values in a float64 map with float64 keys -func msum(x map[float64]float64) float64 { - total := 0.0 - for _, y := range x { - total += y - } - return total -} - -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - kstatSys, err := exec.LookPath("kstat") - if err != nil { - return nil, fmt.Errorf("cannot find kstat: %s", err) - } - cpu := make(map[float64]float64) - idle := make(map[float64]float64) - user := make(map[float64]float64) - kern := make(map[float64]float64) - iowt := make(map[float64]float64) - //swap := make(map[float64]float64) - kstatSysOut, err := invoke.CommandWithContext(ctx, kstatSys, "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/") - if err != nil { - return nil, fmt.Errorf("cannot execute kstat: %s", err) - } - re := regexp.MustCompile(`[:\s]+`) - for _, line := range strings.Split(string(kstatSysOut), "\n") { - fields := re.Split(line, -1) - if fields[0] != "cpu_stat" { - continue - } - cpuNumber, err := strconv.ParseFloat(fields[1], 64) - if err != nil { - return nil, fmt.Errorf("cannot parse cpu number: %s", err) - } - cpu[cpuNumber] = cpuNumber - switch fields[3] { - case "idle": - idle[cpuNumber], err = strconv.ParseFloat(fields[4], 64) - if err != nil { - return nil, fmt.Errorf("cannot parse idle: %s", err) - } - case "user": - user[cpuNumber], err = strconv.ParseFloat(fields[4], 64) - if err != nil { - return nil, fmt.Errorf("cannot parse user: %s", err) - } - case "kernel": - kern[cpuNumber], err = strconv.ParseFloat(fields[4], 64) - if err != nil { - return nil, fmt.Errorf("cannot parse kernel: %s", err) - } - case "iowait": - iowt[cpuNumber], err = strconv.ParseFloat(fields[4], 64) - if err != nil { - return nil, fmt.Errorf("cannot parse iowait: %s", err) - } - //not sure how this translates, don't report, add to kernel, something else? - /*case "swap": - swap[cpuNumber], err = strconv.ParseFloat(fields[4], 64) - if err != nil { - return nil, fmt.Errorf("cannot parse swap: %s", err) - } */ - } - } - ret := make([]TimesStat, 0, len(cpu)) - if percpu { - for _, c := range cpu { - ct := &TimesStat{ - CPU: fmt.Sprintf("cpu%d", int(cpu[c])), - Idle: idle[c] / ClocksPerSec, - User: user[c] / ClocksPerSec, - System: kern[c] / ClocksPerSec, - Iowait: iowt[c] / ClocksPerSec, - } - ret = append(ret, *ct) - } - } else { - ct := &TimesStat{ - CPU: "cpu-total", - Idle: msum(idle) / ClocksPerSec, - User: msum(user) / ClocksPerSec, - System: msum(kern) / ClocksPerSec, - Iowait: msum(iowt) / ClocksPerSec, - } - ret = append(ret, *ct) - } - return ret, nil -} - -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - psrInfo, err := exec.LookPath("psrinfo") - if err != nil { - return nil, fmt.Errorf("cannot find psrinfo: %s", err) - } - psrInfoOut, err := invoke.CommandWithContext(ctx, psrInfo, "-p", "-v") - if err != nil { - return nil, fmt.Errorf("cannot execute psrinfo: %s", err) - } - - isaInfo, err := exec.LookPath("isainfo") - if err != nil { - return nil, fmt.Errorf("cannot find isainfo: %s", err) - } - isaInfoOut, err := invoke.CommandWithContext(ctx, isaInfo, "-b", "-v") - if err != nil { - return nil, fmt.Errorf("cannot execute isainfo: %s", err) - } - - procs, err := parseProcessorInfo(string(psrInfoOut)) - if err != nil { - return nil, fmt.Errorf("error parsing psrinfo output: %s", err) - } - - flags, err := parseISAInfo(string(isaInfoOut)) - if err != nil { - return nil, fmt.Errorf("error parsing isainfo output: %s", err) - } - - result := make([]InfoStat, 0, len(flags)) - for _, proc := range procs { - procWithFlags := proc - procWithFlags.Flags = flags - result = append(result, procWithFlags) - } - - return result, nil -} - -var flagsMatch = regexp.MustCompile(`[\w\.]+`) - -func parseISAInfo(cmdOutput string) ([]string, error) { - words := flagsMatch.FindAllString(cmdOutput, -1) - - // Sanity check the output - if len(words) < 4 || words[1] != "bit" || words[3] != "applications" { - return nil, errors.New("attempted to parse invalid isainfo output") - } - - flags := make([]string, len(words)-4) - for i, val := range words[4:] { - flags[i] = val - } - sort.Strings(flags) - - return flags, nil -} - -var psrInfoMatch = regexp.MustCompile(`The physical processor has (?:([\d]+) virtual processors? \(([\d-]+)\)|([\d]+) cores and ([\d]+) virtual processors[^\n]+)\n(?:\s+ The core has.+\n)*\s+.+ \((\w+) ([\S]+) family (.+) model (.+) step (.+) clock (.+) MHz\)\n[\s]*(.*)`) - -const ( - psrNumCoresOffset = 1 - psrNumCoresHTOffset = 3 - psrNumHTOffset = 4 - psrVendorIDOffset = 5 - psrFamilyOffset = 7 - psrModelOffset = 8 - psrStepOffset = 9 - psrClockOffset = 10 - psrModelNameOffset = 11 -) - -func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) { - matches := psrInfoMatch.FindAllStringSubmatch(cmdOutput, -1) - - var infoStatCount int32 - result := make([]InfoStat, 0, len(matches)) - for physicalIndex, physicalCPU := range matches { - var step int32 - var clock float64 - - if physicalCPU[psrStepOffset] != "" { - stepParsed, err := strconv.ParseInt(physicalCPU[psrStepOffset], 10, 32) - if err != nil { - return nil, fmt.Errorf("cannot parse value %q for step as 32-bit integer: %s", physicalCPU[9], err) - } - step = int32(stepParsed) - } - - if physicalCPU[psrClockOffset] != "" { - clockParsed, err := strconv.ParseInt(physicalCPU[psrClockOffset], 10, 64) - if err != nil { - return nil, fmt.Errorf("cannot parse value %q for clock as 32-bit integer: %s", physicalCPU[10], err) - } - clock = float64(clockParsed) - } - - var err error - var numCores int64 - var numHT int64 - switch { - case physicalCPU[psrNumCoresOffset] != "": - numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresOffset], 10, 32) - if err != nil { - return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[1], err) - } - - for i := 0; i < int(numCores); i++ { - result = append(result, InfoStat{ - CPU: infoStatCount, - PhysicalID: strconv.Itoa(physicalIndex), - CoreID: strconv.Itoa(i), - Cores: 1, - VendorID: physicalCPU[psrVendorIDOffset], - ModelName: physicalCPU[psrModelNameOffset], - Family: physicalCPU[psrFamilyOffset], - Model: physicalCPU[psrModelOffset], - Stepping: step, - Mhz: clock, - }) - infoStatCount++ - } - case physicalCPU[psrNumCoresHTOffset] != "": - numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresHTOffset], 10, 32) - if err != nil { - return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[3], err) - } - - numHT, err = strconv.ParseInt(physicalCPU[psrNumHTOffset], 10, 32) - if err != nil { - return nil, fmt.Errorf("cannot parse value %q for hyperthread count as 32-bit integer: %s", physicalCPU[4], err) - } - - for i := 0; i < int(numCores); i++ { - result = append(result, InfoStat{ - CPU: infoStatCount, - PhysicalID: strconv.Itoa(physicalIndex), - CoreID: strconv.Itoa(i), - Cores: int32(numHT) / int32(numCores), - VendorID: physicalCPU[psrVendorIDOffset], - ModelName: physicalCPU[psrModelNameOffset], - Family: physicalCPU[psrFamilyOffset], - Model: physicalCPU[psrModelOffset], - Stepping: step, - Mhz: clock, - }) - infoStatCount++ - } - default: - return nil, errors.New("values for cores with and without hyperthreading are both set") - } - } - return result, nil -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - return runtime.NumCPU(), nil -} diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_windows.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_windows.go deleted file mode 100644 index 0176c4775..000000000 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_windows.go +++ /dev/null @@ -1,265 +0,0 @@ -// +build windows - -package cpu - -import ( - "context" - "fmt" - "strings" - "unsafe" - - "github.com/yusufpapurcu/wmi" - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/windows" -) - -var ( - procGetActiveProcessorCount = common.Modkernel32.NewProc("GetActiveProcessorCount") - procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo") -) - -type Win32_Processor struct { - Win32_ProcessorWithoutLoadPct - LoadPercentage *uint16 -} - -// LoadPercentage takes a linearly more time as the number of sockets increases. -// For vSphere by default corespersocket = 1, meaning for a 40 vCPU VM Get Processor Info -// could take more than half a minute. -type Win32_ProcessorWithoutLoadPct struct { - Family uint16 - Manufacturer string - Name string - NumberOfLogicalProcessors uint32 - NumberOfCores uint32 - ProcessorID *string - Stepping *string - MaxClockSpeed uint32 -} - -// SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION -// defined in windows api doc with the following -// https://docs.microsoft.com/en-us/windows/desktop/api/winternl/nf-winternl-ntquerysysteminformation#system_processor_performance_information -// additional fields documented here -// https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/ex/sysinfo/processor_performance.htm -type win32_SystemProcessorPerformanceInformation struct { - IdleTime int64 // idle time in 100ns (this is not a filetime). - KernelTime int64 // kernel time in 100ns. kernel time includes idle time. (this is not a filetime). - UserTime int64 // usertime in 100ns (this is not a filetime). - DpcTime int64 // dpc time in 100ns (this is not a filetime). - InterruptTime int64 // interrupt time in 100ns - InterruptCount uint32 -} - -// Win32_PerfFormattedData_PerfOS_System struct to have count of processes and processor queue length -type Win32_PerfFormattedData_PerfOS_System struct { - Processes uint32 - ProcessorQueueLength uint32 -} - -const ( - ClocksPerSec = 10000000.0 - - // systemProcessorPerformanceInformationClass information class to query with NTQuerySystemInformation - // https://processhacker.sourceforge.io/doc/ntexapi_8h.html#ad5d815b48e8f4da1ef2eb7a2f18a54e0 - win32_SystemProcessorPerformanceInformationClass = 8 - - // size of systemProcessorPerformanceInfoSize in memory - win32_SystemProcessorPerformanceInfoSize = uint32(unsafe.Sizeof(win32_SystemProcessorPerformanceInformation{})) -) - -// Times returns times stat per cpu and combined for all CPUs -func Times(percpu bool) ([]TimesStat, error) { - return TimesWithContext(context.Background(), percpu) -} - -func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { - if percpu { - return perCPUTimes() - } - - var ret []TimesStat - var lpIdleTime common.FILETIME - var lpKernelTime common.FILETIME - var lpUserTime common.FILETIME - r, _, _ := common.ProcGetSystemTimes.Call( - uintptr(unsafe.Pointer(&lpIdleTime)), - uintptr(unsafe.Pointer(&lpKernelTime)), - uintptr(unsafe.Pointer(&lpUserTime))) - if r == 0 { - return ret, windows.GetLastError() - } - - LOT := float64(0.0000001) - HIT := (LOT * 4294967296.0) - idle := ((HIT * float64(lpIdleTime.DwHighDateTime)) + (LOT * float64(lpIdleTime.DwLowDateTime))) - user := ((HIT * float64(lpUserTime.DwHighDateTime)) + (LOT * float64(lpUserTime.DwLowDateTime))) - kernel := ((HIT * float64(lpKernelTime.DwHighDateTime)) + (LOT * float64(lpKernelTime.DwLowDateTime))) - system := (kernel - idle) - - ret = append(ret, TimesStat{ - CPU: "cpu-total", - Idle: float64(idle), - User: float64(user), - System: float64(system), - }) - return ret, nil -} - -func Info() ([]InfoStat, error) { - return InfoWithContext(context.Background()) -} - -func InfoWithContext(ctx context.Context) ([]InfoStat, error) { - var ret []InfoStat - var dst []Win32_ProcessorWithoutLoadPct - q := wmi.CreateQuery(&dst, "") - q = strings.ReplaceAll(q, "Win32_ProcessorWithoutLoadPct", "Win32_Processor") - if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil { - return ret, err - } - - var procID string - for i, l := range dst { - procID = "" - if l.ProcessorID != nil { - procID = *l.ProcessorID - } - - cpu := InfoStat{ - CPU: int32(i), - Family: fmt.Sprintf("%d", l.Family), - VendorID: l.Manufacturer, - ModelName: l.Name, - Cores: int32(l.NumberOfLogicalProcessors), - PhysicalID: procID, - Mhz: float64(l.MaxClockSpeed), - Flags: []string{}, - } - ret = append(ret, cpu) - } - - return ret, nil -} - -// ProcInfo returns processes count and processor queue length in the system. -// There is a single queue for processor even on multiprocessors systems. -func ProcInfo() ([]Win32_PerfFormattedData_PerfOS_System, error) { - return ProcInfoWithContext(context.Background()) -} - -func ProcInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_PerfOS_System, error) { - var ret []Win32_PerfFormattedData_PerfOS_System - q := wmi.CreateQuery(&ret, "") - err := common.WMIQueryWithContext(ctx, q, &ret) - if err != nil { - return []Win32_PerfFormattedData_PerfOS_System{}, err - } - return ret, err -} - -// perCPUTimes returns times stat per cpu, per core and overall for all CPUs -func perCPUTimes() ([]TimesStat, error) { - var ret []TimesStat - stats, err := perfInfo() - if err != nil { - return nil, err - } - for core, v := range stats { - c := TimesStat{ - CPU: fmt.Sprintf("cpu%d", core), - User: float64(v.UserTime) / ClocksPerSec, - System: float64(v.KernelTime-v.IdleTime) / ClocksPerSec, - Idle: float64(v.IdleTime) / ClocksPerSec, - Irq: float64(v.InterruptTime) / ClocksPerSec, - } - ret = append(ret, c) - } - return ret, nil -} - -// makes call to Windows API function to retrieve performance information for each core -func perfInfo() ([]win32_SystemProcessorPerformanceInformation, error) { - // Make maxResults large for safety. - // We can't invoke the api call with a results array that's too small. - // If we have more than 2056 cores on a single host, then it's probably the future. - maxBuffer := 2056 - // buffer for results from the windows proc - resultBuffer := make([]win32_SystemProcessorPerformanceInformation, maxBuffer) - // size of the buffer in memory - bufferSize := uintptr(win32_SystemProcessorPerformanceInfoSize) * uintptr(maxBuffer) - // size of the returned response - var retSize uint32 - - // Invoke windows api proc. - // The returned err from the windows dll proc will always be non-nil even when successful. - // See https://godoc.org/golang.org/x/sys/windows#LazyProc.Call for more information - retCode, _, err := common.ProcNtQuerySystemInformation.Call( - win32_SystemProcessorPerformanceInformationClass, // System Information Class -> SystemProcessorPerformanceInformation - uintptr(unsafe.Pointer(&resultBuffer[0])), // pointer to first element in result buffer - bufferSize, // size of the buffer in memory - uintptr(unsafe.Pointer(&retSize)), // pointer to the size of the returned results the windows proc will set this - ) - - // check return code for errors - if retCode != 0 { - return nil, fmt.Errorf("call to NtQuerySystemInformation returned %d. err: %s", retCode, err.Error()) - } - - // calculate the number of returned elements based on the returned size - numReturnedElements := retSize / win32_SystemProcessorPerformanceInfoSize - - // trim results to the number of returned elements - resultBuffer = resultBuffer[:numReturnedElements] - - return resultBuffer, nil -} - -// SystemInfo is an equivalent representation of SYSTEM_INFO in the Windows API. -// https://msdn.microsoft.com/en-us/library/ms724958%28VS.85%29.aspx?f=255&MSPPError=-2147217396 -// https://github.com/elastic/go-windows/blob/bb1581babc04d5cb29a2bfa7a9ac6781c730c8dd/kernel32.go#L43 -type systemInfo struct { - wProcessorArchitecture uint16 - wReserved uint16 - dwPageSize uint32 - lpMinimumApplicationAddress uintptr - lpMaximumApplicationAddress uintptr - dwActiveProcessorMask uintptr - dwNumberOfProcessors uint32 - dwProcessorType uint32 - dwAllocationGranularity uint32 - wProcessorLevel uint16 - wProcessorRevision uint16 -} - -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - if logical { - // https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L97 - err := procGetActiveProcessorCount.Find() - if err == nil { // Win7+ - ret, _, _ := procGetActiveProcessorCount.Call(uintptr(0xffff)) // ALL_PROCESSOR_GROUPS is 0xffff according to Rust's winapi lib https://docs.rs/winapi/*/x86_64-pc-windows-msvc/src/winapi/shared/ntdef.rs.html#120 - if ret != 0 { - return int(ret), nil - } - } - var systemInfo systemInfo - _, _, err = procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo))) - if systemInfo.dwNumberOfProcessors == 0 { - return 0, err - } - return int(systemInfo.dwNumberOfProcessors), nil - } - // physical cores https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L499 - // for the time being, try with unreliable and slow WMI call… - var dst []Win32_ProcessorWithoutLoadPct - q := wmi.CreateQuery(&dst, "") - q = strings.ReplaceAll(q, "Win32_ProcessorWithoutLoadPct", "Win32_Processor") - if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil { - return 0, err - } - var count uint32 - for _, d := range dst { - count += d.NumberOfCores - } - return int(count), nil -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/binary.go b/vendor/github.com/shirou/gopsutil/internal/common/binary.go deleted file mode 100644 index bf385fd4f..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/binary.go +++ /dev/null @@ -1,634 +0,0 @@ -package common - -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package binary implements simple translation between numbers and byte -// sequences and encoding and decoding of varints. -// -// Numbers are translated by reading and writing fixed-size values. -// A fixed-size value is either a fixed-size arithmetic -// type (int8, uint8, int16, float32, complex64, ...) -// or an array or struct containing only fixed-size values. -// -// The varint functions encode and decode single integer values using -// a variable-length encoding; smaller values require fewer bytes. -// For a specification, see -// http://code.google.com/apis/protocolbuffers/docs/encoding.html. -// -// This package favors simplicity over efficiency. Clients that require -// high-performance serialization, especially for large data structures, -// should look at more advanced solutions such as the encoding/gob -// package or protocol buffers. -import ( - "errors" - "io" - "math" - "reflect" -) - -// A ByteOrder specifies how to convert byte sequences into -// 16-, 32-, or 64-bit unsigned integers. -type ByteOrder interface { - Uint16([]byte) uint16 - Uint32([]byte) uint32 - Uint64([]byte) uint64 - PutUint16([]byte, uint16) - PutUint32([]byte, uint32) - PutUint64([]byte, uint64) - String() string -} - -// LittleEndian is the little-endian implementation of ByteOrder. -var LittleEndian littleEndian - -// BigEndian is the big-endian implementation of ByteOrder. -var BigEndian bigEndian - -type littleEndian struct{} - -func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 } - -func (littleEndian) PutUint16(b []byte, v uint16) { - b[0] = byte(v) - b[1] = byte(v >> 8) -} - -func (littleEndian) Uint32(b []byte) uint32 { - return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 -} - -func (littleEndian) PutUint32(b []byte, v uint32) { - b[0] = byte(v) - b[1] = byte(v >> 8) - b[2] = byte(v >> 16) - b[3] = byte(v >> 24) -} - -func (littleEndian) Uint64(b []byte) uint64 { - return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | - uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 -} - -func (littleEndian) PutUint64(b []byte, v uint64) { - b[0] = byte(v) - b[1] = byte(v >> 8) - b[2] = byte(v >> 16) - b[3] = byte(v >> 24) - b[4] = byte(v >> 32) - b[5] = byte(v >> 40) - b[6] = byte(v >> 48) - b[7] = byte(v >> 56) -} - -func (littleEndian) String() string { return "LittleEndian" } - -func (littleEndian) GoString() string { return "binary.LittleEndian" } - -type bigEndian struct{} - -func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 } - -func (bigEndian) PutUint16(b []byte, v uint16) { - b[0] = byte(v >> 8) - b[1] = byte(v) -} - -func (bigEndian) Uint32(b []byte) uint32 { - return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 -} - -func (bigEndian) PutUint32(b []byte, v uint32) { - b[0] = byte(v >> 24) - b[1] = byte(v >> 16) - b[2] = byte(v >> 8) - b[3] = byte(v) -} - -func (bigEndian) Uint64(b []byte) uint64 { - return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | - uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 -} - -func (bigEndian) PutUint64(b []byte, v uint64) { - b[0] = byte(v >> 56) - b[1] = byte(v >> 48) - b[2] = byte(v >> 40) - b[3] = byte(v >> 32) - b[4] = byte(v >> 24) - b[5] = byte(v >> 16) - b[6] = byte(v >> 8) - b[7] = byte(v) -} - -func (bigEndian) String() string { return "BigEndian" } - -func (bigEndian) GoString() string { return "binary.BigEndian" } - -// Read reads structured binary data from r into data. -// Data must be a pointer to a fixed-size value or a slice -// of fixed-size values. -// Bytes read from r are decoded using the specified byte order -// and written to successive fields of the data. -// When reading into structs, the field data for fields with -// blank (_) field names is skipped; i.e., blank field names -// may be used for padding. -// When reading into a struct, all non-blank fields must be exported. -func Read(r io.Reader, order ByteOrder, data interface{}) error { - // Fast path for basic types and slices. - if n := intDataSize(data); n != 0 { - var b [8]byte - var bs []byte - if n > len(b) { - bs = make([]byte, n) - } else { - bs = b[:n] - } - if _, err := io.ReadFull(r, bs); err != nil { - return err - } - switch data := data.(type) { - case *int8: - *data = int8(b[0]) - case *uint8: - *data = b[0] - case *int16: - *data = int16(order.Uint16(bs)) - case *uint16: - *data = order.Uint16(bs) - case *int32: - *data = int32(order.Uint32(bs)) - case *uint32: - *data = order.Uint32(bs) - case *int64: - *data = int64(order.Uint64(bs)) - case *uint64: - *data = order.Uint64(bs) - case []int8: - for i, x := range bs { // Easier to loop over the input for 8-bit values. - data[i] = int8(x) - } - case []uint8: - copy(data, bs) - case []int16: - for i := range data { - data[i] = int16(order.Uint16(bs[2*i:])) - } - case []uint16: - for i := range data { - data[i] = order.Uint16(bs[2*i:]) - } - case []int32: - for i := range data { - data[i] = int32(order.Uint32(bs[4*i:])) - } - case []uint32: - for i := range data { - data[i] = order.Uint32(bs[4*i:]) - } - case []int64: - for i := range data { - data[i] = int64(order.Uint64(bs[8*i:])) - } - case []uint64: - for i := range data { - data[i] = order.Uint64(bs[8*i:]) - } - } - return nil - } - - // Fallback to reflect-based decoding. - v := reflect.ValueOf(data) - size := -1 - switch v.Kind() { - case reflect.Ptr: - v = v.Elem() - size = dataSize(v) - case reflect.Slice: - size = dataSize(v) - } - if size < 0 { - return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String()) - } - d := &decoder{order: order, buf: make([]byte, size)} - if _, err := io.ReadFull(r, d.buf); err != nil { - return err - } - d.value(v) - return nil -} - -// Write writes the binary representation of data into w. -// Data must be a fixed-size value or a slice of fixed-size -// values, or a pointer to such data. -// Bytes written to w are encoded using the specified byte order -// and read from successive fields of the data. -// When writing structs, zero values are written for fields -// with blank (_) field names. -func Write(w io.Writer, order ByteOrder, data interface{}) error { - // Fast path for basic types and slices. - if n := intDataSize(data); n != 0 { - var b [8]byte - var bs []byte - if n > len(b) { - bs = make([]byte, n) - } else { - bs = b[:n] - } - switch v := data.(type) { - case *int8: - bs = b[:1] - b[0] = byte(*v) - case int8: - bs = b[:1] - b[0] = byte(v) - case []int8: - for i, x := range v { - bs[i] = byte(x) - } - case *uint8: - bs = b[:1] - b[0] = *v - case uint8: - bs = b[:1] - b[0] = v - case []uint8: - bs = v - case *int16: - bs = b[:2] - order.PutUint16(bs, uint16(*v)) - case int16: - bs = b[:2] - order.PutUint16(bs, uint16(v)) - case []int16: - for i, x := range v { - order.PutUint16(bs[2*i:], uint16(x)) - } - case *uint16: - bs = b[:2] - order.PutUint16(bs, *v) - case uint16: - bs = b[:2] - order.PutUint16(bs, v) - case []uint16: - for i, x := range v { - order.PutUint16(bs[2*i:], x) - } - case *int32: - bs = b[:4] - order.PutUint32(bs, uint32(*v)) - case int32: - bs = b[:4] - order.PutUint32(bs, uint32(v)) - case []int32: - for i, x := range v { - order.PutUint32(bs[4*i:], uint32(x)) - } - case *uint32: - bs = b[:4] - order.PutUint32(bs, *v) - case uint32: - bs = b[:4] - order.PutUint32(bs, v) - case []uint32: - for i, x := range v { - order.PutUint32(bs[4*i:], x) - } - case *int64: - bs = b[:8] - order.PutUint64(bs, uint64(*v)) - case int64: - bs = b[:8] - order.PutUint64(bs, uint64(v)) - case []int64: - for i, x := range v { - order.PutUint64(bs[8*i:], uint64(x)) - } - case *uint64: - bs = b[:8] - order.PutUint64(bs, *v) - case uint64: - bs = b[:8] - order.PutUint64(bs, v) - case []uint64: - for i, x := range v { - order.PutUint64(bs[8*i:], x) - } - } - _, err := w.Write(bs) - return err - } - - // Fallback to reflect-based encoding. - v := reflect.Indirect(reflect.ValueOf(data)) - size := dataSize(v) - if size < 0 { - return errors.New("binary.Write: invalid type " + reflect.TypeOf(data).String()) - } - buf := make([]byte, size) - e := &encoder{order: order, buf: buf} - e.value(v) - _, err := w.Write(buf) - return err -} - -// Size returns how many bytes Write would generate to encode the value v, which -// must be a fixed-size value or a slice of fixed-size values, or a pointer to such data. -// If v is neither of these, Size returns -1. -func Size(v interface{}) int { - return dataSize(reflect.Indirect(reflect.ValueOf(v))) -} - -// dataSize returns the number of bytes the actual data represented by v occupies in memory. -// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice -// it returns the length of the slice times the element size and does not count the memory -// occupied by the header. If the type of v is not acceptable, dataSize returns -1. -func dataSize(v reflect.Value) int { - if v.Kind() == reflect.Slice { - if s := sizeof(v.Type().Elem()); s >= 0 { - return s * v.Len() - } - return -1 - } - return sizeof(v.Type()) -} - -// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable. -func sizeof(t reflect.Type) int { - switch t.Kind() { - case reflect.Array: - if s := sizeof(t.Elem()); s >= 0 { - return s * t.Len() - } - - case reflect.Struct: - sum := 0 - for i, n := 0, t.NumField(); i < n; i++ { - s := sizeof(t.Field(i).Type) - if s < 0 { - return -1 - } - sum += s - } - return sum - - case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, - reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Ptr: - return int(t.Size()) - } - - return -1 -} - -type coder struct { - order ByteOrder - buf []byte -} - -type decoder coder -type encoder coder - -func (d *decoder) uint8() uint8 { - x := d.buf[0] - d.buf = d.buf[1:] - return x -} - -func (e *encoder) uint8(x uint8) { - e.buf[0] = x - e.buf = e.buf[1:] -} - -func (d *decoder) uint16() uint16 { - x := d.order.Uint16(d.buf[0:2]) - d.buf = d.buf[2:] - return x -} - -func (e *encoder) uint16(x uint16) { - e.order.PutUint16(e.buf[0:2], x) - e.buf = e.buf[2:] -} - -func (d *decoder) uint32() uint32 { - x := d.order.Uint32(d.buf[0:4]) - d.buf = d.buf[4:] - return x -} - -func (e *encoder) uint32(x uint32) { - e.order.PutUint32(e.buf[0:4], x) - e.buf = e.buf[4:] -} - -func (d *decoder) uint64() uint64 { - x := d.order.Uint64(d.buf[0:8]) - d.buf = d.buf[8:] - return x -} - -func (e *encoder) uint64(x uint64) { - e.order.PutUint64(e.buf[0:8], x) - e.buf = e.buf[8:] -} - -func (d *decoder) int8() int8 { return int8(d.uint8()) } - -func (e *encoder) int8(x int8) { e.uint8(uint8(x)) } - -func (d *decoder) int16() int16 { return int16(d.uint16()) } - -func (e *encoder) int16(x int16) { e.uint16(uint16(x)) } - -func (d *decoder) int32() int32 { return int32(d.uint32()) } - -func (e *encoder) int32(x int32) { e.uint32(uint32(x)) } - -func (d *decoder) int64() int64 { return int64(d.uint64()) } - -func (e *encoder) int64(x int64) { e.uint64(uint64(x)) } - -func (d *decoder) value(v reflect.Value) { - switch v.Kind() { - case reflect.Array: - l := v.Len() - for i := 0; i < l; i++ { - d.value(v.Index(i)) - } - - case reflect.Struct: - t := v.Type() - l := v.NumField() - for i := 0; i < l; i++ { - // Note: Calling v.CanSet() below is an optimization. - // It would be sufficient to check the field name, - // but creating the StructField info for each field is - // costly (run "go test -bench=ReadStruct" and compare - // results when making changes to this code). - if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" { - d.value(v) - } else { - d.skip(v) - } - } - - case reflect.Slice: - l := v.Len() - for i := 0; i < l; i++ { - d.value(v.Index(i)) - } - - case reflect.Int8: - v.SetInt(int64(d.int8())) - case reflect.Int16: - v.SetInt(int64(d.int16())) - case reflect.Int32: - v.SetInt(int64(d.int32())) - case reflect.Int64: - v.SetInt(d.int64()) - - case reflect.Uint8: - v.SetUint(uint64(d.uint8())) - case reflect.Uint16: - v.SetUint(uint64(d.uint16())) - case reflect.Uint32: - v.SetUint(uint64(d.uint32())) - case reflect.Uint64: - v.SetUint(d.uint64()) - - case reflect.Float32: - v.SetFloat(float64(math.Float32frombits(d.uint32()))) - case reflect.Float64: - v.SetFloat(math.Float64frombits(d.uint64())) - - case reflect.Complex64: - v.SetComplex(complex( - float64(math.Float32frombits(d.uint32())), - float64(math.Float32frombits(d.uint32())), - )) - case reflect.Complex128: - v.SetComplex(complex( - math.Float64frombits(d.uint64()), - math.Float64frombits(d.uint64()), - )) - } -} - -func (e *encoder) value(v reflect.Value) { - switch v.Kind() { - case reflect.Array: - l := v.Len() - for i := 0; i < l; i++ { - e.value(v.Index(i)) - } - - case reflect.Struct: - t := v.Type() - l := v.NumField() - for i := 0; i < l; i++ { - // see comment for corresponding code in decoder.value() - if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" { - e.value(v) - } else { - e.skip(v) - } - } - - case reflect.Slice: - l := v.Len() - for i := 0; i < l; i++ { - e.value(v.Index(i)) - } - - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - switch v.Type().Kind() { - case reflect.Int8: - e.int8(int8(v.Int())) - case reflect.Int16: - e.int16(int16(v.Int())) - case reflect.Int32: - e.int32(int32(v.Int())) - case reflect.Int64: - e.int64(v.Int()) - } - - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - switch v.Type().Kind() { - case reflect.Uint8: - e.uint8(uint8(v.Uint())) - case reflect.Uint16: - e.uint16(uint16(v.Uint())) - case reflect.Uint32: - e.uint32(uint32(v.Uint())) - case reflect.Uint64: - e.uint64(v.Uint()) - } - - case reflect.Float32, reflect.Float64: - switch v.Type().Kind() { - case reflect.Float32: - e.uint32(math.Float32bits(float32(v.Float()))) - case reflect.Float64: - e.uint64(math.Float64bits(v.Float())) - } - - case reflect.Complex64, reflect.Complex128: - switch v.Type().Kind() { - case reflect.Complex64: - x := v.Complex() - e.uint32(math.Float32bits(float32(real(x)))) - e.uint32(math.Float32bits(float32(imag(x)))) - case reflect.Complex128: - x := v.Complex() - e.uint64(math.Float64bits(real(x))) - e.uint64(math.Float64bits(imag(x))) - } - } -} - -func (d *decoder) skip(v reflect.Value) { - d.buf = d.buf[dataSize(v):] -} - -func (e *encoder) skip(v reflect.Value) { - n := dataSize(v) - for i := range e.buf[0:n] { - e.buf[i] = 0 - } - e.buf = e.buf[n:] -} - -// intDataSize returns the size of the data required to represent the data when encoded. -// It returns zero if the type cannot be implemented by the fast path in Read or Write. -func intDataSize(data interface{}) int { - switch data := data.(type) { - case int8, *int8, *uint8: - return 1 - case []int8: - return len(data) - case []uint8: - return len(data) - case int16, *int16, *uint16: - return 2 - case []int16: - return 2 * len(data) - case []uint16: - return 2 * len(data) - case int32, *int32, *uint32: - return 4 - case []int32: - return 4 * len(data) - case []uint32: - return 4 * len(data) - case int64, *int64, *uint64: - return 8 - case []int64: - return 8 * len(data) - case []uint64: - return 8 * len(data) - } - return 0 -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/common.go b/vendor/github.com/shirou/gopsutil/internal/common/common.go deleted file mode 100644 index 63462885e..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/common.go +++ /dev/null @@ -1,379 +0,0 @@ -package common - -// -// gopsutil is a port of psutil(http://pythonhosted.org/psutil/). -// This covers these architectures. -// - linux (amd64, arm) -// - freebsd (amd64) -// - windows (amd64) -import ( - "bufio" - "bytes" - "context" - "errors" - "fmt" - "io/ioutil" - "net/url" - "os" - "os/exec" - "path" - "path/filepath" - "reflect" - "runtime" - "strconv" - "strings" - "time" -) - -var ( - Timeout = 3 * time.Second - ErrTimeout = errors.New("command timed out") -) - -type Invoker interface { - Command(string, ...string) ([]byte, error) - CommandWithContext(context.Context, string, ...string) ([]byte, error) -} - -type Invoke struct{} - -func (i Invoke) Command(name string, arg ...string) ([]byte, error) { - ctx, cancel := context.WithTimeout(context.Background(), Timeout) - defer cancel() - return i.CommandWithContext(ctx, name, arg...) -} - -func (i Invoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) { - cmd := exec.CommandContext(ctx, name, arg...) - - var buf bytes.Buffer - cmd.Stdout = &buf - cmd.Stderr = &buf - - if err := cmd.Start(); err != nil { - return buf.Bytes(), err - } - - if err := cmd.Wait(); err != nil { - return buf.Bytes(), err - } - - return buf.Bytes(), nil -} - -type FakeInvoke struct { - Suffix string // Suffix species expected file name suffix such as "fail" - Error error // If Error specfied, return the error. -} - -// Command in FakeInvoke returns from expected file if exists. -func (i FakeInvoke) Command(name string, arg ...string) ([]byte, error) { - if i.Error != nil { - return []byte{}, i.Error - } - - arch := runtime.GOOS - - commandName := filepath.Base(name) - - fname := strings.Join(append([]string{commandName}, arg...), "") - fname = url.QueryEscape(fname) - fpath := path.Join("testdata", arch, fname) - if i.Suffix != "" { - fpath += "_" + i.Suffix - } - if PathExists(fpath) { - return ioutil.ReadFile(fpath) - } - return []byte{}, fmt.Errorf("could not find testdata: %s", fpath) -} - -func (i FakeInvoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) { - return i.Command(name, arg...) -} - -var ErrNotImplementedError = errors.New("not implemented yet") - -// ReadFile reads contents from a file. -func ReadFile(filename string) (string, error) { - content, err := ioutil.ReadFile(filename) - - if err != nil { - return "", err - } - - return string(content), nil -} - -// ReadLines reads contents from a file and splits them by new lines. -// A convenience wrapper to ReadLinesOffsetN(filename, 0, -1). -func ReadLines(filename string) ([]string, error) { - return ReadLinesOffsetN(filename, 0, -1) -} - -// ReadLinesOffsetN reads contents from file and splits them by new line. -// The offset tells at which line number to start. -// The count determines the number of lines to read (starting from offset): -// n >= 0: at most n lines -// n < 0: whole file -func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) { - f, err := os.Open(filename) - if err != nil { - return []string{""}, err - } - defer f.Close() - - var ret []string - - r := bufio.NewReader(f) - for i := 0; i < n+int(offset) || n < 0; i++ { - line, err := r.ReadString('\n') - if err != nil { - break - } - if i < int(offset) { - continue - } - ret = append(ret, strings.Trim(line, "\n")) - } - - return ret, nil -} - -func IntToString(orig []int8) string { - ret := make([]byte, len(orig)) - size := -1 - for i, o := range orig { - if o == 0 { - size = i - break - } - ret[i] = byte(o) - } - if size == -1 { - size = len(orig) - } - - return string(ret[0:size]) -} - -func UintToString(orig []uint8) string { - ret := make([]byte, len(orig)) - size := -1 - for i, o := range orig { - if o == 0 { - size = i - break - } - ret[i] = o - } - if size == -1 { - size = len(orig) - } - - return string(ret[0:size]) -} - -func ByteToString(orig []byte) string { - n := -1 - l := -1 - for i, b := range orig { - // skip left side null - if l == -1 && b == 0 { - continue - } - if l == -1 { - l = i - } - - if b == 0 { - break - } - n = i + 1 - } - if n == -1 { - return string(orig) - } - return string(orig[l:n]) -} - -// ReadInts reads contents from single line file and returns them as []int32. -func ReadInts(filename string) ([]int64, error) { - f, err := os.Open(filename) - if err != nil { - return []int64{}, err - } - defer f.Close() - - var ret []int64 - - r := bufio.NewReader(f) - - // The int files that this is concerned with should only be one liners. - line, err := r.ReadString('\n') - if err != nil { - return []int64{}, err - } - - i, err := strconv.ParseInt(strings.Trim(line, "\n"), 10, 32) - if err != nil { - return []int64{}, err - } - ret = append(ret, i) - - return ret, nil -} - -// HexToUint32 parses Hex to uint32 without error. -func HexToUint32(hex string) uint32 { - vv, _ := strconv.ParseUint(hex, 16, 32) - return uint32(vv) -} - -// mustParseInt32 parses to int32 without error. -func mustParseInt32(val string) int32 { - vv, _ := strconv.ParseInt(val, 10, 32) - return int32(vv) -} - -// mustParseUint64 parses to uint64 without error. -func mustParseUint64(val string) uint64 { - vv, _ := strconv.ParseInt(val, 10, 64) - return uint64(vv) -} - -// mustParseFloat64 parses to Float64 without error. -func mustParseFloat64(val string) float64 { - vv, _ := strconv.ParseFloat(val, 64) - return vv -} - -// StringsHas checks the target string slice contains src or not. -func StringsHas(target []string, src string) bool { - for _, t := range target { - if strings.TrimSpace(t) == src { - return true - } - } - return false -} - -// StringsContains checks the src in any string of the target string slice. -func StringsContains(target []string, src string) bool { - for _, t := range target { - if strings.Contains(t, src) { - return true - } - } - return false -} - -// IntContains checks the src in any int of the target int slice. -func IntContains(target []int, src int) bool { - for _, t := range target { - if src == t { - return true - } - } - return false -} - -// get struct attributes. -// This method is used only for debugging platform dependent code. -func attributes(m interface{}) map[string]reflect.Type { - typ := reflect.TypeOf(m) - if typ.Kind() == reflect.Ptr { - typ = typ.Elem() - } - - attrs := make(map[string]reflect.Type) - if typ.Kind() != reflect.Struct { - return nil - } - - for i := 0; i < typ.NumField(); i++ { - p := typ.Field(i) - if !p.Anonymous { - attrs[p.Name] = p.Type - } - } - - return attrs -} - -func PathExists(filename string) bool { - if _, err := os.Stat(filename); err == nil { - return true - } - return false -} - -// GetEnv retrieves the environment variable key. If it does not exist it returns the default. -func GetEnv(key string, dfault string, combineWith ...string) string { - value := os.Getenv(key) - if value == "" { - value = dfault - } - - switch len(combineWith) { - case 0: - return value - case 1: - return filepath.Join(value, combineWith[0]) - default: - all := make([]string, len(combineWith)+1) - all[0] = value - copy(all[1:], combineWith) - return filepath.Join(all...) - } -} - -func HostProc(combineWith ...string) string { - return GetEnv("HOST_PROC", "/proc", combineWith...) -} - -func HostSys(combineWith ...string) string { - return GetEnv("HOST_SYS", "/sys", combineWith...) -} - -func HostEtc(combineWith ...string) string { - return GetEnv("HOST_ETC", "/etc", combineWith...) -} - -func HostVar(combineWith ...string) string { - return GetEnv("HOST_VAR", "/var", combineWith...) -} - -func HostRun(combineWith ...string) string { - return GetEnv("HOST_RUN", "/run", combineWith...) -} - -func HostDev(combineWith ...string) string { - return GetEnv("HOST_DEV", "/dev", combineWith...) -} - -// MockEnv set environment variable and return revert function. -// MockEnv should be used testing only. -func MockEnv(key string, value string) func() { - original := os.Getenv(key) - os.Setenv(key, value) - return func() { - os.Setenv(key, original) - } -} - -// getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running -// sysctl commands (see DoSysctrl). -func getSysctrlEnv(env []string) []string { - foundLC := false - for i, line := range env { - if strings.HasPrefix(line, "LC_ALL") { - env[i] = "LC_ALL=C" - foundLC = true - } - } - if !foundLC { - env = append(env, "LC_ALL=C") - } - return env -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/common_darwin.go b/vendor/github.com/shirou/gopsutil/internal/common/common_darwin.go deleted file mode 100644 index be46af3d9..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/common_darwin.go +++ /dev/null @@ -1,69 +0,0 @@ -// +build darwin - -package common - -import ( - "context" - "os" - "os/exec" - "strings" - "unsafe" - - "golang.org/x/sys/unix" -) - -func DoSysctrlWithContext(ctx context.Context, mib string) ([]string, error) { - sysctl, err := exec.LookPath("sysctl") - if err != nil { - return []string{}, err - } - cmd := exec.CommandContext(ctx, sysctl, "-n", mib) - cmd.Env = getSysctrlEnv(os.Environ()) - out, err := cmd.Output() - if err != nil { - return []string{}, err - } - v := strings.Replace(string(out), "{ ", "", 1) - v = strings.Replace(string(v), " }", "", 1) - values := strings.Fields(string(v)) - - return values, nil -} - -func CallSyscall(mib []int32) ([]byte, uint64, error) { - miblen := uint64(len(mib)) - - // get required buffer size - length := uint64(0) - _, _, err := unix.Syscall6( - 202, // unix.SYS___SYSCTL https://github.com/golang/sys/blob/76b94024e4b621e672466e8db3d7f084e7ddcad2/unix/zsysnum_darwin_amd64.go#L146 - uintptr(unsafe.Pointer(&mib[0])), - uintptr(miblen), - 0, - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - var b []byte - return b, length, err - } - if length == 0 { - var b []byte - return b, length, err - } - // get proc info itself - buf := make([]byte, length) - _, _, err = unix.Syscall6( - 202, // unix.SYS___SYSCTL https://github.com/golang/sys/blob/76b94024e4b621e672466e8db3d7f084e7ddcad2/unix/zsysnum_darwin_amd64.go#L146 - uintptr(unsafe.Pointer(&mib[0])), - uintptr(miblen), - uintptr(unsafe.Pointer(&buf[0])), - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - return buf, length, err - } - - return buf, length, nil -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/common_freebsd.go b/vendor/github.com/shirou/gopsutil/internal/common/common_freebsd.go deleted file mode 100644 index 85bda0e22..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/common_freebsd.go +++ /dev/null @@ -1,85 +0,0 @@ -// +build freebsd openbsd - -package common - -import ( - "fmt" - "os" - "os/exec" - "strings" - "unsafe" - - "golang.org/x/sys/unix" -) - -func SysctlUint(mib string) (uint64, error) { - buf, err := unix.SysctlRaw(mib) - if err != nil { - return 0, err - } - if len(buf) == 8 { // 64 bit - return *(*uint64)(unsafe.Pointer(&buf[0])), nil - } - if len(buf) == 4 { // 32bit - t := *(*uint32)(unsafe.Pointer(&buf[0])) - return uint64(t), nil - } - return 0, fmt.Errorf("unexpected size: %s, %d", mib, len(buf)) -} - -func DoSysctrl(mib string) ([]string, error) { - sysctl, err := exec.LookPath("sysctl") - if err != nil { - return []string{}, err - } - cmd := exec.Command(sysctl, "-n", mib) - cmd.Env = getSysctrlEnv(os.Environ()) - out, err := cmd.Output() - if err != nil { - return []string{}, err - } - v := strings.Replace(string(out), "{ ", "", 1) - v = strings.Replace(string(v), " }", "", 1) - values := strings.Fields(string(v)) - - return values, nil -} - -func CallSyscall(mib []int32) ([]byte, uint64, error) { - mibptr := unsafe.Pointer(&mib[0]) - miblen := uint64(len(mib)) - - // get required buffer size - length := uint64(0) - _, _, err := unix.Syscall6( - unix.SYS___SYSCTL, - uintptr(mibptr), - uintptr(miblen), - 0, - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - var b []byte - return b, length, err - } - if length == 0 { - var b []byte - return b, length, err - } - // get proc info itself - buf := make([]byte, length) - _, _, err = unix.Syscall6( - unix.SYS___SYSCTL, - uintptr(mibptr), - uintptr(miblen), - uintptr(unsafe.Pointer(&buf[0])), - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - return buf, length, err - } - - return buf, length, nil -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/common_linux.go b/vendor/github.com/shirou/gopsutil/internal/common/common_linux.go deleted file mode 100644 index ca01c7557..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/common_linux.go +++ /dev/null @@ -1,291 +0,0 @@ -// +build linux - -package common - -import ( - "context" - "fmt" - "os" - "os/exec" - "path/filepath" - "strconv" - "strings" - "sync" - "time" -) - -func DoSysctrl(mib string) ([]string, error) { - sysctl, err := exec.LookPath("sysctl") - if err != nil { - return []string{}, err - } - cmd := exec.Command(sysctl, "-n", mib) - cmd.Env = getSysctrlEnv(os.Environ()) - out, err := cmd.Output() - if err != nil { - return []string{}, err - } - v := strings.Replace(string(out), "{ ", "", 1) - v = strings.Replace(v, " }", "", 1) - values := strings.Fields(v) - - return values, nil -} - -func NumProcs() (uint64, error) { - f, err := os.Open(HostProc()) - if err != nil { - return 0, err - } - defer f.Close() - - list, err := f.Readdirnames(-1) - if err != nil { - return 0, err - } - var cnt uint64 - - for _, v := range list { - if _, err = strconv.ParseUint(v, 10, 64); err == nil { - cnt++ - } - } - - return cnt, nil -} - -func BootTimeWithContext(ctx context.Context) (uint64, error) { - system, role, err := Virtualization() - if err != nil { - return 0, err - } - - statFile := "stat" - if system == "lxc" && role == "guest" { - // if lxc, /proc/uptime is used. - statFile = "uptime" - } else if system == "docker" && role == "guest" { - // also docker, guest - statFile = "uptime" - } - - filename := HostProc(statFile) - lines, err := ReadLines(filename) - if err != nil { - return 0, err - } - - if statFile == "uptime" { - if len(lines) != 1 { - return 0, fmt.Errorf("wrong uptime format") - } - f := strings.Fields(lines[0]) - b, err := strconv.ParseFloat(f[0], 64) - if err != nil { - return 0, err - } - t := uint64(time.Now().Unix()) - uint64(b) - return t, nil - } - if statFile == "stat" { - for _, line := range lines { - if strings.HasPrefix(line, "btime") { - f := strings.Fields(line) - if len(f) != 2 { - return 0, fmt.Errorf("wrong btime format") - } - b, err := strconv.ParseInt(f[1], 10, 64) - if err != nil { - return 0, err - } - t := uint64(b) - return t, nil - } - } - } - - return 0, fmt.Errorf("could not find btime") -} - -func Virtualization() (string, string, error) { - return VirtualizationWithContext(context.Background()) -} - -// required variables for concurrency safe virtualization caching. -var ( - cachedVirtMap map[string]string - cachedVirtMutex sync.RWMutex - cachedVirtOnce sync.Once -) - -func VirtualizationWithContext(ctx context.Context) (string, string, error) { - var system, role string - - // if cached already, return from cache - cachedVirtMutex.RLock() // unlock won't be deferred so concurrent reads don't wait for long - if cachedVirtMap != nil { - cachedSystem, cachedRole := cachedVirtMap["system"], cachedVirtMap["role"] - cachedVirtMutex.RUnlock() - return cachedSystem, cachedRole, nil - } - cachedVirtMutex.RUnlock() - - filename := HostProc("xen") - if PathExists(filename) { - system = "xen" - role = "guest" // assume guest - - if PathExists(filepath.Join(filename, "capabilities")) { - contents, err := ReadLines(filepath.Join(filename, "capabilities")) - if err == nil && StringsContains(contents, "control_d") { - role = "host" - } - } - } - - filename = HostProc("modules") - if PathExists(filename) { - contents, err := ReadLines(filename) - if err == nil { - switch { - case StringsContains(contents, "kvm"): - system = "kvm" - role = "host" - case StringsContains(contents, "vboxdrv"): - system = "vbox" - role = "host" - case StringsContains(contents, "vboxguest"): - system = "vbox" - role = "guest" - case StringsContains(contents, "vmware"): - system = "vmware" - role = "guest" - } - } - } - - filename = HostProc("cpuinfo") - if PathExists(filename) { - contents, err := ReadLines(filename) - if err == nil { - if StringsContains(contents, "QEMU Virtual CPU") || - StringsContains(contents, "Common KVM processor") || - StringsContains(contents, "Common 32-bit KVM processor") { - system = "kvm" - role = "guest" - } - } - } - - filename = HostProc("bus/pci/devices") - if PathExists(filename) { - contents, err := ReadLines(filename) - if err == nil { - if StringsContains(contents, "virtio-pci") { - role = "guest" - } - } - } - - filename = HostProc() - if PathExists(filepath.Join(filename, "bc", "0")) { - system = "openvz" - role = "host" - } else if PathExists(filepath.Join(filename, "vz")) { - system = "openvz" - role = "guest" - } - - // not use dmidecode because it requires root - if PathExists(filepath.Join(filename, "self", "status")) { - contents, err := ReadLines(filepath.Join(filename, "self", "status")) - if err == nil { - if StringsContains(contents, "s_context:") || - StringsContains(contents, "VxID:") { - system = "linux-vserver" - } - // TODO: guest or host - } - } - - if PathExists(filepath.Join(filename, "1", "environ")) { - contents, err := ReadFile(filepath.Join(filename, "1", "environ")) - - if err == nil { - if strings.Contains(contents, "container=lxc") { - system = "lxc" - role = "guest" - } - } - } - - if PathExists(filepath.Join(filename, "self", "cgroup")) { - contents, err := ReadLines(filepath.Join(filename, "self", "cgroup")) - if err == nil { - switch { - case StringsContains(contents, "lxc"): - system = "lxc" - role = "guest" - case StringsContains(contents, "docker"): - system = "docker" - role = "guest" - case StringsContains(contents, "machine-rkt"): - system = "rkt" - role = "guest" - case PathExists("/usr/bin/lxc-version"): - system = "lxc" - role = "host" - } - } - } - - if PathExists(HostEtc("os-release")) { - p, _, err := GetOSRelease() - if err == nil && p == "coreos" { - system = "rkt" // Is it true? - role = "host" - } - } - - // before returning for the first time, cache the system and role - cachedVirtOnce.Do(func() { - cachedVirtMutex.Lock() - defer cachedVirtMutex.Unlock() - cachedVirtMap = map[string]string{ - "system": system, - "role": role, - } - }) - - return system, role, nil -} - -func GetOSRelease() (platform string, version string, err error) { - contents, err := ReadLines(HostEtc("os-release")) - if err != nil { - return "", "", nil // return empty - } - for _, line := range contents { - field := strings.Split(line, "=") - if len(field) < 2 { - continue - } - switch field[0] { - case "ID": // use ID for lowercase - platform = trimQuotes(field[1]) - case "VERSION": - version = trimQuotes(field[1]) - } - } - return platform, version, nil -} - -// trimQuotes removes quotes in the source string. -func trimQuotes(s string) string { - if len(s) >= 2 { - if s[0] == '"' && s[len(s)-1] == '"' { - return s[1 : len(s)-1] - } - } - return s -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/common_openbsd.go b/vendor/github.com/shirou/gopsutil/internal/common/common_openbsd.go deleted file mode 100644 index ba73a7eb5..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/common_openbsd.go +++ /dev/null @@ -1,69 +0,0 @@ -// +build openbsd - -package common - -import ( - "os" - "os/exec" - "strings" - "unsafe" - - "golang.org/x/sys/unix" -) - -func DoSysctrl(mib string) ([]string, error) { - sysctl, err := exec.LookPath("sysctl") - if err != nil { - return []string{}, err - } - cmd := exec.Command(sysctl, "-n", mib) - cmd.Env = getSysctrlEnv(os.Environ()) - out, err := cmd.Output() - if err != nil { - return []string{}, err - } - v := strings.Replace(string(out), "{ ", "", 1) - v = strings.Replace(string(v), " }", "", 1) - values := strings.Fields(string(v)) - - return values, nil -} - -func CallSyscall(mib []int32) ([]byte, uint64, error) { - mibptr := unsafe.Pointer(&mib[0]) - miblen := uint64(len(mib)) - - // get required buffer size - length := uint64(0) - _, _, err := unix.Syscall6( - unix.SYS___SYSCTL, - uintptr(mibptr), - uintptr(miblen), - 0, - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - var b []byte - return b, length, err - } - if length == 0 { - var b []byte - return b, length, err - } - // get proc info itself - buf := make([]byte, length) - _, _, err = unix.Syscall6( - unix.SYS___SYSCTL, - uintptr(mibptr), - uintptr(miblen), - uintptr(unsafe.Pointer(&buf[0])), - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - return buf, length, err - } - - return buf, length, nil -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/common_unix.go b/vendor/github.com/shirou/gopsutil/internal/common/common_unix.go deleted file mode 100644 index 605202815..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/common_unix.go +++ /dev/null @@ -1,66 +0,0 @@ -// +build linux freebsd darwin openbsd - -package common - -import ( - "context" - "os/exec" - "strconv" - "strings" -) - -func CallLsofWithContext(ctx context.Context, invoke Invoker, pid int32, args ...string) ([]string, error) { - var cmd []string - if pid == 0 { // will get from all processes. - cmd = []string{"-a", "-n", "-P"} - } else { - cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))} - } - cmd = append(cmd, args...) - lsof, err := exec.LookPath("lsof") - if err != nil { - return []string{}, err - } - out, err := invoke.CommandWithContext(ctx, lsof, cmd...) - if err != nil { - // if no pid found, lsof returns code 1. - if err.Error() == "exit status 1" && len(out) == 0 { - return []string{}, nil - } - } - lines := strings.Split(string(out), "\n") - - var ret []string - for _, l := range lines[1:] { - if len(l) == 0 { - continue - } - ret = append(ret, l) - } - return ret, nil -} - -func CallPgrepWithContext(ctx context.Context, invoke Invoker, pid int32) ([]int32, error) { - cmd := []string{"-P", strconv.Itoa(int(pid))} - pgrep, err := exec.LookPath("pgrep") - if err != nil { - return []int32{}, err - } - out, err := invoke.CommandWithContext(ctx, pgrep, cmd...) - if err != nil { - return []int32{}, err - } - lines := strings.Split(string(out), "\n") - ret := make([]int32, 0, len(lines)) - for _, l := range lines { - if len(l) == 0 { - continue - } - i, err := strconv.Atoi(l) - if err != nil { - continue - } - ret = append(ret, int32(i)) - } - return ret, nil -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/common_windows.go b/vendor/github.com/shirou/gopsutil/internal/common/common_windows.go deleted file mode 100644 index ba20136da..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/common_windows.go +++ /dev/null @@ -1,300 +0,0 @@ -// +build windows - -package common - -import ( - "context" - "fmt" - "path/filepath" - "reflect" - "strings" - "syscall" - "unsafe" - - "github.com/yusufpapurcu/wmi" - "golang.org/x/sys/windows" -) - -// for double values -type PDH_FMT_COUNTERVALUE_DOUBLE struct { - CStatus uint32 - DoubleValue float64 -} - -// for 64 bit integer values -type PDH_FMT_COUNTERVALUE_LARGE struct { - CStatus uint32 - LargeValue int64 -} - -// for long values -type PDH_FMT_COUNTERVALUE_LONG struct { - CStatus uint32 - LongValue int32 - padding [4]byte -} - -// windows system const -const ( - ERROR_SUCCESS = 0 - ERROR_FILE_NOT_FOUND = 2 - DRIVE_REMOVABLE = 2 - DRIVE_FIXED = 3 - HKEY_LOCAL_MACHINE = 0x80000002 - RRF_RT_REG_SZ = 0x00000002 - RRF_RT_REG_DWORD = 0x00000010 - PDH_FMT_LONG = 0x00000100 - PDH_FMT_DOUBLE = 0x00000200 - PDH_FMT_LARGE = 0x00000400 - PDH_INVALID_DATA = 0xc0000bc6 - PDH_INVALID_HANDLE = 0xC0000bbc - PDH_NO_DATA = 0x800007d5 - - STATUS_BUFFER_OVERFLOW = 0x80000005 - STATUS_BUFFER_TOO_SMALL = 0xC0000023 - STATUS_INFO_LENGTH_MISMATCH = 0xC0000004 -) - -const ( - ProcessBasicInformation = 0 - ProcessWow64Information = 26 - ProcessQueryInformation = windows.PROCESS_DUP_HANDLE | windows.PROCESS_QUERY_INFORMATION - - SystemExtendedHandleInformationClass = 64 -) - -var ( - Modkernel32 = windows.NewLazySystemDLL("kernel32.dll") - ModNt = windows.NewLazySystemDLL("ntdll.dll") - ModPdh = windows.NewLazySystemDLL("pdh.dll") - ModPsapi = windows.NewLazySystemDLL("psapi.dll") - - ProcGetSystemTimes = Modkernel32.NewProc("GetSystemTimes") - ProcNtQuerySystemInformation = ModNt.NewProc("NtQuerySystemInformation") - ProcRtlGetNativeSystemInformation = ModNt.NewProc("RtlGetNativeSystemInformation") - ProcRtlNtStatusToDosError = ModNt.NewProc("RtlNtStatusToDosError") - ProcNtQueryInformationProcess = ModNt.NewProc("NtQueryInformationProcess") - ProcNtReadVirtualMemory = ModNt.NewProc("NtReadVirtualMemory") - ProcNtWow64QueryInformationProcess64 = ModNt.NewProc("NtWow64QueryInformationProcess64") - ProcNtWow64ReadVirtualMemory64 = ModNt.NewProc("NtWow64ReadVirtualMemory64") - - PdhOpenQuery = ModPdh.NewProc("PdhOpenQuery") - PdhAddEnglishCounterW = ModPdh.NewProc("PdhAddEnglishCounterW") - PdhCollectQueryData = ModPdh.NewProc("PdhCollectQueryData") - PdhGetFormattedCounterValue = ModPdh.NewProc("PdhGetFormattedCounterValue") - PdhCloseQuery = ModPdh.NewProc("PdhCloseQuery") - - procQueryDosDeviceW = Modkernel32.NewProc("QueryDosDeviceW") -) - -type FILETIME struct { - DwLowDateTime uint32 - DwHighDateTime uint32 -} - -// borrowed from net/interface_windows.go -func BytePtrToString(p *uint8) string { - a := (*[10000]uint8)(unsafe.Pointer(p)) - i := 0 - for a[i] != 0 { - i++ - } - return string(a[:i]) -} - -// CounterInfo struct is used to track a windows performance counter -// copied from https://github.com/mackerelio/mackerel-agent/ -type CounterInfo struct { - PostName string - CounterName string - Counter windows.Handle -} - -// CreateQuery with a PdhOpenQuery call -// copied from https://github.com/mackerelio/mackerel-agent/ -func CreateQuery() (windows.Handle, error) { - var query windows.Handle - r, _, err := PdhOpenQuery.Call(0, 0, uintptr(unsafe.Pointer(&query))) - if r != 0 { - return 0, err - } - return query, nil -} - -// CreateCounter with a PdhAddEnglishCounterW call -func CreateCounter(query windows.Handle, pname, cname string) (*CounterInfo, error) { - var counter windows.Handle - r, _, err := PdhAddEnglishCounterW.Call( - uintptr(query), - uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(cname))), - 0, - uintptr(unsafe.Pointer(&counter))) - if r != 0 { - return nil, err - } - return &CounterInfo{ - PostName: pname, - CounterName: cname, - Counter: counter, - }, nil -} - -// GetCounterValue get counter value from handle -// adapted from https://github.com/mackerelio/mackerel-agent/ -func GetCounterValue(counter windows.Handle) (float64, error) { - var value PDH_FMT_COUNTERVALUE_DOUBLE - r, _, err := PdhGetFormattedCounterValue.Call(uintptr(counter), PDH_FMT_DOUBLE, uintptr(0), uintptr(unsafe.Pointer(&value))) - if r != 0 && r != PDH_INVALID_DATA { - return 0.0, err - } - return value.DoubleValue, nil -} - -type Win32PerformanceCounter struct { - PostName string - CounterName string - Query windows.Handle - Counter windows.Handle -} - -func NewWin32PerformanceCounter(postName, counterName string) (*Win32PerformanceCounter, error) { - query, err := CreateQuery() - if err != nil { - return nil, err - } - var counter = Win32PerformanceCounter{ - Query: query, - PostName: postName, - CounterName: counterName, - } - r, _, err := PdhAddEnglishCounterW.Call( - uintptr(counter.Query), - uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(counter.CounterName))), - 0, - uintptr(unsafe.Pointer(&counter.Counter)), - ) - if r != 0 { - return nil, err - } - return &counter, nil -} - -func (w *Win32PerformanceCounter) GetValue() (float64, error) { - r, _, err := PdhCollectQueryData.Call(uintptr(w.Query)) - if r != 0 && err != nil { - if r == PDH_NO_DATA { - return 0.0, fmt.Errorf("%w: this counter has not data", err) - } - return 0.0, err - } - - return GetCounterValue(w.Counter) -} - -func ProcessorQueueLengthCounter() (*Win32PerformanceCounter, error) { - return NewWin32PerformanceCounter("processor_queue_length", `\System\Processor Queue Length`) -} - -// WMIQueryWithContext - wraps wmi.Query with a timed-out context to avoid hanging -func WMIQueryWithContext(ctx context.Context, query string, dst interface{}, connectServerArgs ...interface{}) error { - if _, ok := ctx.Deadline(); !ok { - ctxTimeout, cancel := context.WithTimeout(ctx, Timeout) - defer cancel() - ctx = ctxTimeout - } - - errChan := make(chan error, 1) - go func() { - errChan <- wmi.Query(query, dst, connectServerArgs...) - }() - - select { - case <-ctx.Done(): - return ctx.Err() - case err := <-errChan: - return err - } -} - -// Convert paths using native DOS format like: -// "\Device\HarddiskVolume1\Windows\systemew\file.txt" -// into: -// "C:\Windows\systemew\file.txt" -func ConvertDOSPath(p string) string { - rawDrive := strings.Join(strings.Split(p, `\`)[:3], `\`) - - for d := 'A'; d <= 'Z'; d++ { - szDeviceName := string(d) + ":" - szTarget := make([]uint16, 512) - ret, _, _ := procQueryDosDeviceW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(szDeviceName))), - uintptr(unsafe.Pointer(&szTarget[0])), - uintptr(len(szTarget))) - if ret != 0 && windows.UTF16ToString(szTarget[:]) == rawDrive { - return filepath.Join(szDeviceName, p[len(rawDrive):]) - } - } - return p -} - -type NtStatus uint32 - -func (s NtStatus) Error() error { - if s == 0 { - return nil - } - return fmt.Errorf("NtStatus 0x%08x", uint32(s)) -} - -func (s NtStatus) IsError() bool { - return s>>30 == 3 -} - -type SystemExtendedHandleTableEntryInformation struct { - Object uintptr - UniqueProcessId uintptr - HandleValue uintptr - GrantedAccess uint32 - CreatorBackTraceIndex uint16 - ObjectTypeIndex uint16 - HandleAttributes uint32 - Reserved uint32 -} - -type SystemExtendedHandleInformation struct { - NumberOfHandles uintptr - Reserved uintptr - Handles [1]SystemExtendedHandleTableEntryInformation -} - -// CallWithExpandingBuffer https://github.com/hillu/go-ntdll -func CallWithExpandingBuffer(fn func() NtStatus, buf *[]byte, resultLength *uint32) NtStatus { - for { - if st := fn(); st == STATUS_BUFFER_OVERFLOW || st == STATUS_BUFFER_TOO_SMALL || st == STATUS_INFO_LENGTH_MISMATCH { - if int(*resultLength) <= cap(*buf) { - (*reflect.SliceHeader)(unsafe.Pointer(buf)).Len = int(*resultLength) - } else { - *buf = make([]byte, int(*resultLength)) - } - continue - } else { - if !st.IsError() { - *buf = (*buf)[:int(*resultLength)] - } - return st - } - } -} - -func NtQuerySystemInformation( - SystemInformationClass uint32, - SystemInformation *byte, - SystemInformationLength uint32, - ReturnLength *uint32, -) NtStatus { - r0, _, _ := ProcNtQuerySystemInformation.Call( - uintptr(SystemInformationClass), - uintptr(unsafe.Pointer(SystemInformation)), - uintptr(SystemInformationLength), - uintptr(unsafe.Pointer(ReturnLength))) - return NtStatus(r0) -} diff --git a/vendor/github.com/shirou/gopsutil/internal/common/sleep.go b/vendor/github.com/shirou/gopsutil/internal/common/sleep.go deleted file mode 100644 index ee27e54d4..000000000 --- a/vendor/github.com/shirou/gopsutil/internal/common/sleep.go +++ /dev/null @@ -1,18 +0,0 @@ -package common - -import ( - "context" - "time" -) - -// Sleep awaits for provided interval. -// Can be interrupted by context cancelation. -func Sleep(ctx context.Context, interval time.Duration) error { - var timer = time.NewTimer(interval) - select { - case <-ctx.Done(): - return ctx.Err() - case <-timer.C: - return nil - } -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem.go b/vendor/github.com/shirou/gopsutil/mem/mem.go deleted file mode 100644 index c7e9880b7..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem.go +++ /dev/null @@ -1,116 +0,0 @@ -package mem - -import ( - "encoding/json" - - "github.com/shirou/gopsutil/internal/common" -) - -var invoke common.Invoker = common.Invoke{} - -// Memory usage statistics. Total, Available and Used contain numbers of bytes -// for human consumption. -// -// The other fields in this struct contain kernel specific values. -type VirtualMemoryStat struct { - // Total amount of RAM on this system - Total uint64 `json:"total"` - - // RAM available for programs to allocate - // - // This value is computed from the kernel specific values. - Available uint64 `json:"available"` - - // RAM used by programs - // - // This value is computed from the kernel specific values. - Used uint64 `json:"used"` - - // Percentage of RAM used by programs - // - // This value is computed from the kernel specific values. - UsedPercent float64 `json:"usedPercent"` - - // This is the kernel's notion of free memory; RAM chips whose bits nobody - // cares about the value of right now. For a human consumable number, - // Available is what you really want. - Free uint64 `json:"free"` - - // OS X / BSD specific numbers: - // http://www.macyourself.com/2010/02/17/what-is-free-wired-active-and-inactive-system-memory-ram/ - Active uint64 `json:"active"` - Inactive uint64 `json:"inactive"` - Wired uint64 `json:"wired"` - - // FreeBSD specific numbers: - // https://reviews.freebsd.org/D8467 - Laundry uint64 `json:"laundry"` - - // Linux specific numbers - // https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html - // https://www.kernel.org/doc/Documentation/filesystems/proc.txt - // https://www.kernel.org/doc/Documentation/vm/overcommit-accounting - Buffers uint64 `json:"buffers"` - Cached uint64 `json:"cached"` - Writeback uint64 `json:"writeback"` - Dirty uint64 `json:"dirty"` - WritebackTmp uint64 `json:"writebacktmp"` - Shared uint64 `json:"shared"` - Slab uint64 `json:"slab"` - SReclaimable uint64 `json:"sreclaimable"` - SUnreclaim uint64 `json:"sunreclaim"` - PageTables uint64 `json:"pagetables"` - SwapCached uint64 `json:"swapcached"` - CommitLimit uint64 `json:"commitlimit"` - CommittedAS uint64 `json:"committedas"` - HighTotal uint64 `json:"hightotal"` - HighFree uint64 `json:"highfree"` - LowTotal uint64 `json:"lowtotal"` - LowFree uint64 `json:"lowfree"` - SwapTotal uint64 `json:"swaptotal"` - SwapFree uint64 `json:"swapfree"` - Mapped uint64 `json:"mapped"` - VMallocTotal uint64 `json:"vmalloctotal"` - VMallocUsed uint64 `json:"vmallocused"` - VMallocChunk uint64 `json:"vmallocchunk"` - HugePagesTotal uint64 `json:"hugepagestotal"` - HugePagesFree uint64 `json:"hugepagesfree"` - HugePageSize uint64 `json:"hugepagesize"` -} - -type SwapMemoryStat struct { - Total uint64 `json:"total"` - Used uint64 `json:"used"` - Free uint64 `json:"free"` - UsedPercent float64 `json:"usedPercent"` - Sin uint64 `json:"sin"` - Sout uint64 `json:"sout"` - PgIn uint64 `json:"pgin"` - PgOut uint64 `json:"pgout"` - PgFault uint64 `json:"pgfault"` - - // Linux specific numbers - // https://www.kernel.org/doc/Documentation/cgroup-v2.txt - PgMajFault uint64 `json:"pgmajfault"` -} - -func (m VirtualMemoryStat) String() string { - s, _ := json.Marshal(m) - return string(s) -} - -func (m SwapMemoryStat) String() string { - s, _ := json.Marshal(m) - return string(s) -} - -type SwapDevice struct { - Name string `json:"name"` - UsedBytes uint64 `json:"usedBytes"` - FreeBytes uint64 `json:"freeBytes"` -} - -func (m SwapDevice) String() string { - s, _ := json.Marshal(m) - return string(s) -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_bsd.go b/vendor/github.com/shirou/gopsutil/mem/mem_bsd.go deleted file mode 100644 index 88db76209..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_bsd.go +++ /dev/null @@ -1,91 +0,0 @@ -// +build freebsd openbsd - -package mem - -import ( - "context" - "fmt" - "os/exec" - "strconv" - "strings" -) - -const swapCommand = "swapctl" - -// swapctl column indexes -const ( - nameCol = 0 - totalKiBCol = 1 - usedKiBCol = 2 -) - -func SwapDevices() ([]*SwapDevice, error) { - return SwapDevicesWithContext(context.Background()) -} - -func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { - swapCommandPath, err := exec.LookPath(swapCommand) - if err != nil { - return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err) - } - output, err := invoke.CommandWithContext(ctx, swapCommandPath, "-lk") - if err != nil { - return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err) - } - - return parseSwapctlOutput(string(output)) -} - -func parseSwapctlOutput(output string) ([]*SwapDevice, error) { - lines := strings.Split(output, "\n") - if len(lines) == 0 { - return nil, fmt.Errorf("could not parse output of %q: no lines in %q", swapCommand, output) - } - - // Check header headerFields are as expected. - header := lines[0] - header = strings.ToLower(header) - header = strings.ReplaceAll(header, ":", "") - headerFields := strings.Fields(header) - if len(headerFields) < usedKiBCol { - return nil, fmt.Errorf("couldn't parse %q: too few fields in header %q", swapCommand, header) - } - if headerFields[nameCol] != "device" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[nameCol], "device") - } - if headerFields[totalKiBCol] != "1kb-blocks" && headerFields[totalKiBCol] != "1k-blocks" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[totalKiBCol], "1kb-blocks") - } - if headerFields[usedKiBCol] != "used" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[usedKiBCol], "used") - } - - var swapDevices []*SwapDevice - for _, line := range lines[1:] { - if line == "" { - continue // the terminal line is typically empty - } - fields := strings.Fields(line) - if len(fields) < usedKiBCol { - return nil, fmt.Errorf("couldn't parse %q: too few fields", swapCommand) - } - - totalKiB, err := strconv.ParseUint(fields[totalKiBCol], 10, 64) - if err != nil { - return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapCommand, err) - } - - usedKiB, err := strconv.ParseUint(fields[usedKiBCol], 10, 64) - if err != nil { - return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapCommand, err) - } - - swapDevices = append(swapDevices, &SwapDevice{ - Name: fields[nameCol], - UsedBytes: usedKiB * 1024, - FreeBytes: (totalKiB - usedKiB) * 1024, - }) - } - - return swapDevices, nil -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_darwin.go b/vendor/github.com/shirou/gopsutil/mem/mem_darwin.go deleted file mode 100644 index 4b8f069d3..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_darwin.go +++ /dev/null @@ -1,70 +0,0 @@ -// +build darwin - -package mem - -import ( - "context" - "fmt" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/unix" -) - -func getHwMemsize() (uint64, error) { - total, err := unix.SysctlUint64("hw.memsize") - if err != nil { - return 0, err - } - return total, nil -} - -// xsw_usage in sys/sysctl.h -type swapUsage struct { - Total uint64 - Avail uint64 - Used uint64 - Pagesize int32 - Encrypted bool -} - -// SwapMemory returns swapinfo. -func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) -} - -func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - // https://github.com/yanllearnn/go-osstat/blob/ae8a279d26f52ec946a03698c7f50a26cfb427e3/memory/memory_darwin.go - var ret *SwapMemoryStat - - value, err := unix.SysctlRaw("vm.swapusage") - if err != nil { - return ret, err - } - if len(value) != 32 { - return ret, fmt.Errorf("unexpected output of sysctl vm.swapusage: %v (len: %d)", value, len(value)) - } - swap := (*swapUsage)(unsafe.Pointer(&value[0])) - - u := float64(0) - if swap.Total != 0 { - u = ((float64(swap.Total) - float64(swap.Avail)) / float64(swap.Total)) * 100.0 - } - - ret = &SwapMemoryStat{ - Total: swap.Total, - Used: swap.Used, - Free: swap.Avail, - UsedPercent: u, - } - - return ret, nil -} - -func SwapDevices() ([]*SwapDevice, error) { - return SwapDevicesWithContext(context.Background()) -} - -func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { - return nil, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_darwin_cgo.go b/vendor/github.com/shirou/gopsutil/mem/mem_darwin_cgo.go deleted file mode 100644 index ade3cecd4..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_darwin_cgo.go +++ /dev/null @@ -1,58 +0,0 @@ -// +build darwin,cgo - -package mem - -/* -#include -*/ -import "C" - -import ( - "context" - "fmt" - "unsafe" - - "golang.org/x/sys/unix" -) - -// VirtualMemory returns VirtualmemoryStat. -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - count := C.mach_msg_type_number_t(C.HOST_VM_INFO_COUNT) - var vmstat C.vm_statistics_data_t - - status := C.host_statistics(C.host_t(C.mach_host_self()), - C.HOST_VM_INFO, - C.host_info_t(unsafe.Pointer(&vmstat)), - &count) - - if status != C.KERN_SUCCESS { - return nil, fmt.Errorf("host_statistics error=%d", status) - } - - pageSize := uint64(unix.Getpagesize()) - total, err := getHwMemsize() - if err != nil { - return nil, err - } - totalCount := C.natural_t(total / pageSize) - - availableCount := vmstat.inactive_count + vmstat.free_count - usedPercent := 100 * float64(totalCount-availableCount) / float64(totalCount) - - usedCount := totalCount - availableCount - - return &VirtualMemoryStat{ - Total: total, - Available: pageSize * uint64(availableCount), - Used: pageSize * uint64(usedCount), - UsedPercent: usedPercent, - Free: pageSize * uint64(vmstat.free_count), - Active: pageSize * uint64(vmstat.active_count), - Inactive: pageSize * uint64(vmstat.inactive_count), - Wired: pageSize * uint64(vmstat.wire_count), - }, nil -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_darwin_nocgo.go b/vendor/github.com/shirou/gopsutil/mem/mem_darwin_nocgo.go deleted file mode 100644 index 2e847cbe4..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_darwin_nocgo.go +++ /dev/null @@ -1,93 +0,0 @@ -// +build darwin,!cgo - -package mem - -import ( - "context" - "os/exec" - "strconv" - "strings" - - "golang.org/x/sys/unix" -) - -// Runs vm_stat and returns Free and inactive pages -func getVMStat(vms *VirtualMemoryStat) error { - vm_stat, err := exec.LookPath("vm_stat") - if err != nil { - return err - } - out, err := invoke.Command(vm_stat) - if err != nil { - return err - } - return parseVMStat(string(out), vms) -} - -func parseVMStat(out string, vms *VirtualMemoryStat) error { - var err error - - lines := strings.Split(out, "\n") - pagesize := uint64(unix.Getpagesize()) - for _, line := range lines { - fields := strings.Split(line, ":") - if len(fields) < 2 { - continue - } - key := strings.TrimSpace(fields[0]) - value := strings.Trim(fields[1], " .") - switch key { - case "Pages free": - free, e := strconv.ParseUint(value, 10, 64) - if e != nil { - err = e - } - vms.Free = free * pagesize - case "Pages inactive": - inactive, e := strconv.ParseUint(value, 10, 64) - if e != nil { - err = e - } - vms.Inactive = inactive * pagesize - case "Pages active": - active, e := strconv.ParseUint(value, 10, 64) - if e != nil { - err = e - } - vms.Active = active * pagesize - case "Pages wired down": - wired, e := strconv.ParseUint(value, 10, 64) - if e != nil { - err = e - } - vms.Wired = wired * pagesize - } - } - return err -} - -// VirtualMemory returns VirtualmemoryStat. -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - ret := &VirtualMemoryStat{} - - total, err := getHwMemsize() - if err != nil { - return nil, err - } - err = getVMStat(ret) - if err != nil { - return nil, err - } - - ret.Available = ret.Free + ret.Inactive - ret.Total = total - - ret.Used = ret.Total - ret.Available - ret.UsedPercent = 100 * float64(ret.Used) / float64(ret.Total) - - return ret, nil -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_fallback.go b/vendor/github.com/shirou/gopsutil/mem/mem_fallback.go deleted file mode 100644 index 02e87d7a9..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_fallback.go +++ /dev/null @@ -1,33 +0,0 @@ -// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows - -package mem - -import ( - "context" - - "github.com/shirou/gopsutil/internal/common" -) - -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - return nil, common.ErrNotImplementedError -} - -func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) -} - -func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - return nil, common.ErrNotImplementedError -} - -func SwapDevices() ([]*SwapDevice, error) { - return SwapDevicesWithContext(context.Background()) -} - -func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { - return nil, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_freebsd.go b/vendor/github.com/shirou/gopsutil/mem/mem_freebsd.go deleted file mode 100644 index f91efc9e3..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_freebsd.go +++ /dev/null @@ -1,167 +0,0 @@ -// +build freebsd - -package mem - -import ( - "context" - "errors" - "unsafe" - - "golang.org/x/sys/unix" - - "github.com/shirou/gopsutil/internal/common" -) - -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - pageSize, err := common.SysctlUint("vm.stats.vm.v_page_size") - if err != nil { - return nil, err - } - physmem, err := common.SysctlUint("hw.physmem") - if err != nil { - return nil, err - } - - free, err := common.SysctlUint("vm.stats.vm.v_free_count") - if err != nil { - return nil, err - } - active, err := common.SysctlUint("vm.stats.vm.v_active_count") - if err != nil { - return nil, err - } - inactive, err := common.SysctlUint("vm.stats.vm.v_inactive_count") - if err != nil { - return nil, err - } - buffers, err := common.SysctlUint("vfs.bufspace") - if err != nil { - return nil, err - } - wired, err := common.SysctlUint("vm.stats.vm.v_wire_count") - if err != nil { - return nil, err - } - var cached, laundry uint64 - osreldate, _ := common.SysctlUint("kern.osreldate") - if osreldate < 1102000 { - cached, err = common.SysctlUint("vm.stats.vm.v_cache_count") - if err != nil { - return nil, err - } - } else { - laundry, err = common.SysctlUint("vm.stats.vm.v_laundry_count") - if err != nil { - return nil, err - } - } - - p := pageSize - ret := &VirtualMemoryStat{ - Total: physmem, - Free: free * p, - Active: active * p, - Inactive: inactive * p, - Cached: cached * p, - Buffers: buffers, - Wired: wired * p, - Laundry: laundry * p, - } - - ret.Available = ret.Inactive + ret.Cached + ret.Free + ret.Laundry - ret.Used = ret.Total - ret.Available - ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0 - - return ret, nil -} - -// Return swapinfo -func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) -} - -// Constants from vm/vm_param.h -// nolint: golint -const ( - XSWDEV_VERSION11 = 1 - XSWDEV_VERSION = 2 -) - -// Types from vm/vm_param.h -type xswdev struct { - Version uint32 // Version is the version - Dev uint64 // Dev is the device identifier - Flags int32 // Flags is the swap flags applied to the device - NBlks int32 // NBlks is the total number of blocks - Used int32 // Used is the number of blocks used -} - -// xswdev11 is a compatibility for under FreeBSD 11 -// sys/vm/swap_pager.c -type xswdev11 struct { - Version uint32 // Version is the version - Dev uint32 // Dev is the device identifier - Flags int32 // Flags is the swap flags applied to the device - NBlks int32 // NBlks is the total number of blocks - Used int32 // Used is the number of blocks used -} - -func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - // FreeBSD can have multiple swap devices so we total them up - i, err := common.SysctlUint("vm.nswapdev") - if err != nil { - return nil, err - } - - if i == 0 { - return nil, errors.New("no swap devices found") - } - - c := int(i) - - i, err = common.SysctlUint("vm.stats.vm.v_page_size") - if err != nil { - return nil, err - } - pageSize := i - - var buf []byte - s := &SwapMemoryStat{} - for n := 0; n < c; n++ { - buf, err = unix.SysctlRaw("vm.swap_info", n) - if err != nil { - return nil, err - } - - // first, try to parse with version 2 - xsw := (*xswdev)(unsafe.Pointer(&buf[0])) - if xsw.Version == XSWDEV_VERSION11 { - // this is version 1, so try to parse again - xsw := (*xswdev11)(unsafe.Pointer(&buf[0])) - if xsw.Version != XSWDEV_VERSION11 { - return nil, errors.New("xswdev version mismatch(11)") - } - s.Total += uint64(xsw.NBlks) - s.Used += uint64(xsw.Used) - } else if xsw.Version != XSWDEV_VERSION { - return nil, errors.New("xswdev version mismatch") - } else { - s.Total += uint64(xsw.NBlks) - s.Used += uint64(xsw.Used) - } - - } - - if s.Total != 0 { - s.UsedPercent = float64(s.Used) / float64(s.Total) * 100 - } - s.Total *= pageSize - s.Used *= pageSize - s.Free = s.Total - s.Used - - return s, nil -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_linux.go b/vendor/github.com/shirou/gopsutil/mem/mem_linux.go deleted file mode 100644 index cf3c64e9c..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_linux.go +++ /dev/null @@ -1,514 +0,0 @@ -// +build linux - -package mem - -import ( - "bufio" - "context" - "encoding/json" - "fmt" - "io" - "math" - "os" - "strconv" - "strings" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/unix" -) - -type VirtualMemoryExStat struct { - ActiveFile uint64 `json:"activefile"` - InactiveFile uint64 `json:"inactivefile"` - ActiveAnon uint64 `json:"activeanon"` - InactiveAnon uint64 `json:"inactiveanon"` - Unevictable uint64 `json:"unevictable"` -} - -func (v VirtualMemoryExStat) String() string { - s, _ := json.Marshal(v) - return string(s) -} - -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - vm, _, err := fillFromMeminfoWithContext(ctx) - if err != nil { - return nil, err - } - return vm, nil -} - -func VirtualMemoryEx() (*VirtualMemoryExStat, error) { - return VirtualMemoryExWithContext(context.Background()) -} - -func VirtualMemoryExWithContext(ctx context.Context) (*VirtualMemoryExStat, error) { - _, vmEx, err := fillFromMeminfoWithContext(ctx) - if err != nil { - return nil, err - } - return vmEx, nil -} - -func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *VirtualMemoryExStat, error) { - filename := common.HostProc("meminfo") - lines, _ := common.ReadLines(filename) - - // flag if MemAvailable is in /proc/meminfo (kernel 3.14+) - memavail := false - activeFile := false // "Active(file)" not available: 2.6.28 / Dec 2008 - inactiveFile := false // "Inactive(file)" not available: 2.6.28 / Dec 2008 - sReclaimable := false // "SReclaimable:" not available: 2.6.19 / Nov 2006 - - ret := &VirtualMemoryStat{} - retEx := &VirtualMemoryExStat{} - - for _, line := range lines { - fields := strings.Split(line, ":") - if len(fields) != 2 { - continue - } - key := strings.TrimSpace(fields[0]) - value := strings.TrimSpace(fields[1]) - value = strings.Replace(value, " kB", "", -1) - - switch key { - case "MemTotal": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Total = t * 1024 - case "MemFree": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Free = t * 1024 - case "MemAvailable": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - memavail = true - ret.Available = t * 1024 - case "Buffers": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Buffers = t * 1024 - case "Cached": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Cached = t * 1024 - case "Active": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Active = t * 1024 - case "Inactive": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Inactive = t * 1024 - case "Active(anon)": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - retEx.ActiveAnon = t * 1024 - case "Inactive(anon)": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - retEx.InactiveAnon = t * 1024 - case "Active(file)": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - activeFile = true - retEx.ActiveFile = t * 1024 - case "Inactive(file)": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - inactiveFile = true - retEx.InactiveFile = t * 1024 - case "Unevictable": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - retEx.Unevictable = t * 1024 - case "Writeback": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Writeback = t * 1024 - case "WritebackTmp": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.WritebackTmp = t * 1024 - case "Dirty": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Dirty = t * 1024 - case "Shmem": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Shared = t * 1024 - case "Slab": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Slab = t * 1024 - case "SReclaimable": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - sReclaimable = true - ret.SReclaimable = t * 1024 - case "SUnreclaim": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.SUnreclaim = t * 1024 - case "PageTables": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.PageTables = t * 1024 - case "SwapCached": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.SwapCached = t * 1024 - case "CommitLimit": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.CommitLimit = t * 1024 - case "Committed_AS": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.CommittedAS = t * 1024 - case "HighTotal": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.HighTotal = t * 1024 - case "HighFree": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.HighFree = t * 1024 - case "LowTotal": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.LowTotal = t * 1024 - case "LowFree": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.LowFree = t * 1024 - case "SwapTotal": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.SwapTotal = t * 1024 - case "SwapFree": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.SwapFree = t * 1024 - case "Mapped": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.Mapped = t * 1024 - case "VmallocTotal": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.VMallocTotal = t * 1024 - case "VmallocUsed": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.VMallocUsed = t * 1024 - case "VmallocChunk": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.VMallocChunk = t * 1024 - case "HugePages_Total": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.HugePagesTotal = t - case "HugePages_Free": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.HugePagesFree = t - case "Hugepagesize": - t, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return ret, retEx, err - } - ret.HugePageSize = t * 1024 - } - } - - ret.Cached += ret.SReclaimable - - if !memavail { - if activeFile && inactiveFile && sReclaimable { - ret.Available = calcuateAvailVmem(ret, retEx) - } else { - ret.Available = ret.Cached + ret.Free - } - } - - ret.Used = ret.Total - ret.Free - ret.Buffers - ret.Cached - ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0 - - return ret, retEx, nil -} - -func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) -} - -func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - sysinfo := &unix.Sysinfo_t{} - - if err := unix.Sysinfo(sysinfo); err != nil { - return nil, err - } - ret := &SwapMemoryStat{ - Total: uint64(sysinfo.Totalswap) * uint64(sysinfo.Unit), - Free: uint64(sysinfo.Freeswap) * uint64(sysinfo.Unit), - } - ret.Used = ret.Total - ret.Free - //check Infinity - if ret.Total != 0 { - ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0 - } else { - ret.UsedPercent = 0 - } - filename := common.HostProc("vmstat") - lines, _ := common.ReadLines(filename) - for _, l := range lines { - fields := strings.Fields(l) - if len(fields) < 2 { - continue - } - switch fields[0] { - case "pswpin": - value, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - continue - } - ret.Sin = value * 4 * 1024 - case "pswpout": - value, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - continue - } - ret.Sout = value * 4 * 1024 - case "pgpgin": - value, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - continue - } - ret.PgIn = value * 4 * 1024 - case "pgpgout": - value, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - continue - } - ret.PgOut = value * 4 * 1024 - case "pgfault": - value, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - continue - } - ret.PgFault = value * 4 * 1024 - case "pgmajfault": - value, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - continue - } - ret.PgMajFault = value * 4 * 1024 - } - } - return ret, nil -} - -// calcuateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide -// "MemAvailable:" column. It reimplements an algorithm from the link below -// https://github.com/giampaolo/psutil/pull/890 -func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 { - var watermarkLow uint64 - - fn := common.HostProc("zoneinfo") - lines, err := common.ReadLines(fn) - - if err != nil { - return ret.Free + ret.Cached // fallback under kernel 2.6.13 - } - - pagesize := uint64(os.Getpagesize()) - watermarkLow = 0 - - for _, line := range lines { - fields := strings.Fields(line) - - if strings.HasPrefix(fields[0], "low") { - lowValue, err := strconv.ParseUint(fields[1], 10, 64) - - if err != nil { - lowValue = 0 - } - watermarkLow += lowValue - } - } - - watermarkLow *= pagesize - - availMemory := ret.Free - watermarkLow - pageCache := retEx.ActiveFile + retEx.InactiveFile - pageCache -= uint64(math.Min(float64(pageCache/2), float64(watermarkLow))) - availMemory += pageCache - availMemory += ret.SReclaimable - uint64(math.Min(float64(ret.SReclaimable/2.0), float64(watermarkLow))) - - if availMemory < 0 { - availMemory = 0 - } - - return availMemory -} - -const swapsFilename = "swaps" - -// swaps file column indexes -const ( - nameCol = 0 - // typeCol = 1 - totalCol = 2 - usedCol = 3 - // priorityCol = 4 -) - -func SwapDevices() ([]*SwapDevice, error) { - return SwapDevicesWithContext(context.Background()) -} - -func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { - swapsFilePath := common.HostProc(swapsFilename) - f, err := os.Open(swapsFilePath) - if err != nil { - return nil, err - } - defer f.Close() - - return parseSwapsFile(f) -} - -func parseSwapsFile(r io.Reader) ([]*SwapDevice, error) { - swapsFilePath := common.HostProc(swapsFilename) - scanner := bufio.NewScanner(r) - if !scanner.Scan() { - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("couldn't read file %q: %w", swapsFilePath, err) - } - return nil, fmt.Errorf("unexpected end-of-file in %q", swapsFilePath) - - } - - // Check header headerFields are as expected - headerFields := strings.Fields(scanner.Text()) - if len(headerFields) < usedCol { - return nil, fmt.Errorf("couldn't parse %q: too few fields in header", swapsFilePath) - } - if headerFields[nameCol] != "Filename" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsFilePath, headerFields[nameCol], "Filename") - } - if headerFields[totalCol] != "Size" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsFilePath, headerFields[totalCol], "Size") - } - if headerFields[usedCol] != "Used" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsFilePath, headerFields[usedCol], "Used") - } - - var swapDevices []*SwapDevice - for scanner.Scan() { - fields := strings.Fields(scanner.Text()) - if len(fields) < usedCol { - return nil, fmt.Errorf("couldn't parse %q: too few fields", swapsFilePath) - } - - totalKiB, err := strconv.ParseUint(fields[totalCol], 10, 64) - if err != nil { - return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapsFilePath, err) - } - - usedKiB, err := strconv.ParseUint(fields[usedCol], 10, 64) - if err != nil { - return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapsFilePath, err) - } - - swapDevices = append(swapDevices, &SwapDevice{ - Name: fields[nameCol], - UsedBytes: usedKiB * 1024, - FreeBytes: (totalKiB - usedKiB) * 1024, - }) - } - - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("couldn't read file %q: %w", swapsFilePath, err) - } - - return swapDevices, nil -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd.go b/vendor/github.com/shirou/gopsutil/mem/mem_openbsd.go deleted file mode 100644 index 7ecdae9fd..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd.go +++ /dev/null @@ -1,105 +0,0 @@ -// +build openbsd - -package mem - -import ( - "bytes" - "context" - "encoding/binary" - "errors" - "fmt" - "os/exec" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/unix" -) - -func GetPageSize() (uint64, error) { - return GetPageSizeWithContext(context.Background()) -} - -func GetPageSizeWithContext(ctx context.Context) (uint64, error) { - uvmexp, err := unix.SysctlUvmexp("vm.uvmexp") - if err != nil { - return 0, err - } - return uint64(uvmexp.Pagesize), nil -} - -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - uvmexp, err := unix.SysctlUvmexp("vm.uvmexp") - if err != nil { - return nil, err - } - p := uint64(uvmexp.Pagesize) - - ret := &VirtualMemoryStat{ - Total: uint64(uvmexp.Npages) * p, - Free: uint64(uvmexp.Free) * p, - Active: uint64(uvmexp.Active) * p, - Inactive: uint64(uvmexp.Inactive) * p, - Cached: 0, // not available - Wired: uint64(uvmexp.Wired) * p, - } - - ret.Available = ret.Inactive + ret.Cached + ret.Free - ret.Used = ret.Total - ret.Available - ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0 - - mib := []int32{CTLVfs, VfsGeneric, VfsBcacheStat} - buf, length, err := common.CallSyscall(mib) - if err != nil { - return nil, err - } - if length < sizeOfBcachestats { - return nil, fmt.Errorf("short syscall ret %d bytes", length) - } - var bcs Bcachestats - br := bytes.NewReader(buf) - err = common.Read(br, binary.LittleEndian, &bcs) - if err != nil { - return nil, err - } - ret.Buffers = uint64(bcs.Numbufpages) * p - - return ret, nil -} - -// Return swapctl summary info -func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) -} - -func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - swapctl, err := exec.LookPath("swapctl") - if err != nil { - return nil, err - } - - out, err := invoke.CommandWithContext(ctx, swapctl, "-sk") - if err != nil { - return &SwapMemoryStat{}, nil - } - - line := string(out) - var total, used, free uint64 - - _, err = fmt.Sscanf(line, - "total: %d 1K-blocks allocated, %d used, %d available", - &total, &used, &free) - if err != nil { - return nil, errors.New("failed to parse swapctl output") - } - - percent := float64(used) / float64(total) * 100 - return &SwapMemoryStat{ - Total: total * 1024, - Used: used * 1024, - Free: free * 1024, - UsedPercent: percent, - }, nil -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_386.go b/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_386.go deleted file mode 100644 index 0fa65d9c7..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_386.go +++ /dev/null @@ -1,37 +0,0 @@ -// +build openbsd,386 - -// Code generated by cmd/cgo -godefs; DO NOT EDIT. -// cgo -godefs mem/types_openbsd.go - -package mem - -const ( - CTLVfs = 10 - VfsGeneric = 0 - VfsBcacheStat = 3 -) - -const ( - sizeOfBcachestats = 0x90 -) - -type Bcachestats struct { - Numbufs int64 - Numbufpages int64 - Numdirtypages int64 - Numcleanpages int64 - Pendingwrites int64 - Pendingreads int64 - Numwrites int64 - Numreads int64 - Cachehits int64 - Busymapped int64 - Dmapages int64 - Highpages int64 - Delwribufs int64 - Kvaslots int64 - Avail int64 - Highflips int64 - Highflops int64 - Dmaflips int64 -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_amd64.go b/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_amd64.go deleted file mode 100644 index d187abf01..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_amd64.go +++ /dev/null @@ -1,32 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_openbsd.go - -package mem - -const ( - CTLVfs = 10 - VfsGeneric = 0 - VfsBcacheStat = 3 -) - -const ( - sizeOfBcachestats = 0x78 -) - -type Bcachestats struct { - Numbufs int64 - Numbufpages int64 - Numdirtypages int64 - Numcleanpages int64 - Pendingwrites int64 - Pendingreads int64 - Numwrites int64 - Numreads int64 - Cachehits int64 - Busymapped int64 - Dmapages int64 - Highpages int64 - Delwribufs int64 - Kvaslots int64 - Avail int64 -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_arm64.go b/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_arm64.go deleted file mode 100644 index 35f8517b6..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_openbsd_arm64.go +++ /dev/null @@ -1,37 +0,0 @@ -// +build openbsd,arm64 - -// Code generated by cmd/cgo -godefs; DO NOT EDIT. -// cgo -godefs mem/types_openbsd.go - -package mem - -const ( - CTLVfs = 10 - VfsGeneric = 0 - VfsBcacheStat = 3 -) - -const ( - sizeOfBcachestats = 0x90 -) - -type Bcachestats struct { - Numbufs int64 - Numbufpages int64 - Numdirtypages int64 - Numcleanpages int64 - Pendingwrites int64 - Pendingreads int64 - Numwrites int64 - Numreads int64 - Cachehits int64 - Busymapped int64 - Dmapages int64 - Highpages int64 - Delwribufs int64 - Kvaslots int64 - Avail int64 - Highflips int64 - Highflops int64 - Dmaflips int64 -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_solaris.go b/vendor/github.com/shirou/gopsutil/mem/mem_solaris.go deleted file mode 100644 index 8b549588a..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_solaris.go +++ /dev/null @@ -1,204 +0,0 @@ -// +build solaris - -package mem - -import ( - "context" - "fmt" - "os/exec" - "regexp" - "strconv" - "strings" - - "github.com/shirou/gopsutil/internal/common" -) - -// VirtualMemory for Solaris is a minimal implementation which only returns -// what Nomad needs. It does take into account global vs zone, however. -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - result := &VirtualMemoryStat{} - - zoneName, err := zoneName() - if err != nil { - return nil, err - } - - if zoneName == "global" { - cap, err := globalZoneMemoryCapacity() - if err != nil { - return nil, err - } - result.Total = cap - } else { - cap, err := nonGlobalZoneMemoryCapacity() - if err != nil { - return nil, err - } - result.Total = cap - } - - return result, nil -} - -func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) -} - -func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - return nil, common.ErrNotImplementedError -} - -func zoneName() (string, error) { - zonename, err := exec.LookPath("zonename") - if err != nil { - return "", err - } - - ctx := context.Background() - out, err := invoke.CommandWithContext(ctx, zonename) - if err != nil { - return "", err - } - - return strings.TrimSpace(string(out)), nil -} - -var globalZoneMemoryCapacityMatch = regexp.MustCompile(`[Mm]emory size: (\d+) Megabytes`) - -func globalZoneMemoryCapacity() (uint64, error) { - prtconf, err := exec.LookPath("prtconf") - if err != nil { - return 0, err - } - - ctx := context.Background() - out, err := invoke.CommandWithContext(ctx, prtconf) - if err != nil { - return 0, err - } - - match := globalZoneMemoryCapacityMatch.FindAllStringSubmatch(string(out), -1) - if len(match) != 1 { - return 0, fmt.Errorf("memory size not contained in output of %q", prtconf) - } - - totalMB, err := strconv.ParseUint(match[0][1], 10, 64) - if err != nil { - return 0, err - } - - return totalMB * 1024 * 1024, nil -} - -var kstatMatch = regexp.MustCompile(`(\S+)\s+(\S*)`) - -func nonGlobalZoneMemoryCapacity() (uint64, error) { - kstat, err := exec.LookPath("kstat") - if err != nil { - return 0, err - } - - ctx := context.Background() - out, err := invoke.CommandWithContext(ctx, kstat, "-p", "-c", "zone_memory_cap", "memory_cap:*:*:physcap") - if err != nil { - return 0, err - } - - kstats := kstatMatch.FindAllStringSubmatch(string(out), -1) - if len(kstats) != 1 { - return 0, fmt.Errorf("expected 1 kstat, found %d", len(kstats)) - } - - memSizeBytes, err := strconv.ParseUint(kstats[0][2], 10, 64) - if err != nil { - return 0, err - } - - return memSizeBytes, nil -} - -const swapCommand = "swap" - -// The blockSize as reported by `swap -l`. See https://docs.oracle.com/cd/E23824_01/html/821-1459/fsswap-52195.html -const blockSize = 512 - -// swapctl column indexes -const ( - nameCol = 0 - // devCol = 1 - // swaploCol = 2 - totalBlocksCol = 3 - freeBlocksCol = 4 -) - -func SwapDevices() ([]*SwapDevice, error) { - return SwapDevicesWithContext(context.Background()) -} - -func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { - swapCommandPath, err := exec.LookPath(swapCommand) - if err != nil { - return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err) - } - output, err := invoke.CommandWithContext(ctx, swapCommandPath, "-l") - if err != nil { - return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err) - } - - return parseSwapsCommandOutput(string(output)) -} - -func parseSwapsCommandOutput(output string) ([]*SwapDevice, error) { - lines := strings.Split(output, "\n") - if len(lines) == 0 { - return nil, fmt.Errorf("could not parse output of %q: no lines in %q", swapCommand, output) - } - - // Check header headerFields are as expected. - headerFields := strings.Fields(lines[0]) - if len(headerFields) < freeBlocksCol { - return nil, fmt.Errorf("couldn't parse %q: too few fields in header %q", swapCommand, lines[0]) - } - if headerFields[nameCol] != "swapfile" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[nameCol], "swapfile") - } - if headerFields[totalBlocksCol] != "blocks" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[totalBlocksCol], "blocks") - } - if headerFields[freeBlocksCol] != "free" { - return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[freeBlocksCol], "free") - } - - var swapDevices []*SwapDevice - for _, line := range lines[1:] { - if line == "" { - continue // the terminal line is typically empty - } - fields := strings.Fields(line) - if len(fields) < freeBlocksCol { - return nil, fmt.Errorf("couldn't parse %q: too few fields", swapCommand) - } - - totalBlocks, err := strconv.ParseUint(fields[totalBlocksCol], 10, 64) - if err != nil { - return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapCommand, err) - } - - freeBlocks, err := strconv.ParseUint(fields[freeBlocksCol], 10, 64) - if err != nil { - return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapCommand, err) - } - - swapDevices = append(swapDevices, &SwapDevice{ - Name: fields[nameCol], - UsedBytes: (totalBlocks - freeBlocks) * blockSize, - FreeBytes: freeBlocks * blockSize, - }) - } - - return swapDevices, nil -} diff --git a/vendor/github.com/shirou/gopsutil/mem/mem_windows.go b/vendor/github.com/shirou/gopsutil/mem/mem_windows.go deleted file mode 100644 index 7c2f3bc68..000000000 --- a/vendor/github.com/shirou/gopsutil/mem/mem_windows.go +++ /dev/null @@ -1,165 +0,0 @@ -// +build windows - -package mem - -import ( - "context" - "sync" - "syscall" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/windows" -) - -var ( - procEnumPageFilesW = common.ModPsapi.NewProc("EnumPageFilesW") - procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo") - procGetPerformanceInfo = common.ModPsapi.NewProc("GetPerformanceInfo") - procGlobalMemoryStatusEx = common.Modkernel32.NewProc("GlobalMemoryStatusEx") -) - -type memoryStatusEx struct { - cbSize uint32 - dwMemoryLoad uint32 - ullTotalPhys uint64 // in bytes - ullAvailPhys uint64 - ullTotalPageFile uint64 - ullAvailPageFile uint64 - ullTotalVirtual uint64 - ullAvailVirtual uint64 - ullAvailExtendedVirtual uint64 -} - -func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) -} - -func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - var memInfo memoryStatusEx - memInfo.cbSize = uint32(unsafe.Sizeof(memInfo)) - mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo))) - if mem == 0 { - return nil, windows.GetLastError() - } - - ret := &VirtualMemoryStat{ - Total: memInfo.ullTotalPhys, - Available: memInfo.ullAvailPhys, - Free: memInfo.ullAvailPhys, - UsedPercent: float64(memInfo.dwMemoryLoad), - } - - ret.Used = ret.Total - ret.Available - return ret, nil -} - -type performanceInformation struct { - cb uint32 - commitTotal uint64 - commitLimit uint64 - commitPeak uint64 - physicalTotal uint64 - physicalAvailable uint64 - systemCache uint64 - kernelTotal uint64 - kernelPaged uint64 - kernelNonpaged uint64 - pageSize uint64 - handleCount uint32 - processCount uint32 - threadCount uint32 -} - -func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) -} - -func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - var perfInfo performanceInformation - perfInfo.cb = uint32(unsafe.Sizeof(perfInfo)) - mem, _, _ := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&perfInfo)), uintptr(perfInfo.cb)) - if mem == 0 { - return nil, windows.GetLastError() - } - tot := perfInfo.commitLimit * perfInfo.pageSize - used := perfInfo.commitTotal * perfInfo.pageSize - free := tot - used - var usedPercent float64 - if tot == 0 { - usedPercent = 0 - } else { - usedPercent = float64(used) / float64(tot) * 100 - } - ret := &SwapMemoryStat{ - Total: tot, - Used: used, - Free: free, - UsedPercent: usedPercent, - } - - return ret, nil -} - -var ( - pageSize uint64 - pageSizeOnce sync.Once -) - -type systemInfo struct { - wProcessorArchitecture uint16 - wReserved uint16 - dwPageSize uint32 - lpMinimumApplicationAddress uintptr - lpMaximumApplicationAddress uintptr - dwActiveProcessorMask uintptr - dwNumberOfProcessors uint32 - dwProcessorType uint32 - dwAllocationGranularity uint32 - wProcessorLevel uint16 - wProcessorRevision uint16 -} - -// system type as defined in https://docs.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-enum_page_file_information -type enumPageFileInformation struct { - cb uint32 - reserved uint32 - totalSize uint64 - totalInUse uint64 - peakUsage uint64 -} - -func SwapDevices() ([]*SwapDevice, error) { - return SwapDevicesWithContext(context.Background()) -} - -func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { - pageSizeOnce.Do(func() { - var sysInfo systemInfo - procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&sysInfo))) - pageSize = uint64(sysInfo.dwPageSize) - }) - - // the following system call invokes the supplied callback function once for each page file before returning - // see https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumpagefilesw - var swapDevices []*SwapDevice - result, _, _ := procEnumPageFilesW.Call(windows.NewCallback(pEnumPageFileCallbackW), uintptr(unsafe.Pointer(&swapDevices))) - if result == 0 { - return nil, windows.GetLastError() - } - - return swapDevices, nil -} - -// system callback as defined in https://docs.microsoft.com/en-us/windows/win32/api/psapi/nc-psapi-penum_page_file_callbackw -func pEnumPageFileCallbackW(swapDevices *[]*SwapDevice, enumPageFileInfo *enumPageFileInformation, lpFilenamePtr *[syscall.MAX_LONG_PATH]uint16) *bool { - *swapDevices = append(*swapDevices, &SwapDevice{ - Name: syscall.UTF16ToString((*lpFilenamePtr)[:]), - UsedBytes: enumPageFileInfo.totalInUse * pageSize, - FreeBytes: (enumPageFileInfo.totalSize - enumPageFileInfo.totalInUse) * pageSize, - }) - - // return true to continue enumerating page files - ret := true - return &ret -} diff --git a/vendor/github.com/shirou/gopsutil/net/net.go b/vendor/github.com/shirou/gopsutil/net/net.go deleted file mode 100644 index f1f99dc3a..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net.go +++ /dev/null @@ -1,263 +0,0 @@ -package net - -import ( - "context" - "encoding/json" - "net" - - "github.com/shirou/gopsutil/internal/common" -) - -var invoke common.Invoker = common.Invoke{} - -type IOCountersStat struct { - Name string `json:"name"` // interface name - BytesSent uint64 `json:"bytesSent"` // number of bytes sent - BytesRecv uint64 `json:"bytesRecv"` // number of bytes received - PacketsSent uint64 `json:"packetsSent"` // number of packets sent - PacketsRecv uint64 `json:"packetsRecv"` // number of packets received - Errin uint64 `json:"errin"` // total number of errors while receiving - Errout uint64 `json:"errout"` // total number of errors while sending - Dropin uint64 `json:"dropin"` // total number of incoming packets which were dropped - Dropout uint64 `json:"dropout"` // total number of outgoing packets which were dropped (always 0 on OSX and BSD) - Fifoin uint64 `json:"fifoin"` // total number of FIFO buffers errors while receiving - Fifoout uint64 `json:"fifoout"` // total number of FIFO buffers errors while sending - -} - -// Addr is implemented compatibility to psutil -type Addr struct { - IP string `json:"ip"` - Port uint32 `json:"port"` -} - -type ConnectionStat struct { - Fd uint32 `json:"fd"` - Family uint32 `json:"family"` - Type uint32 `json:"type"` - Laddr Addr `json:"localaddr"` - Raddr Addr `json:"remoteaddr"` - Status string `json:"status"` - Uids []int32 `json:"uids"` - Pid int32 `json:"pid"` -} - -// System wide stats about different network protocols -type ProtoCountersStat struct { - Protocol string `json:"protocol"` - Stats map[string]int64 `json:"stats"` -} - -// NetInterfaceAddr is designed for represent interface addresses -type InterfaceAddr struct { - Addr string `json:"addr"` -} - -type InterfaceStat struct { - Index int `json:"index"` - MTU int `json:"mtu"` // maximum transmission unit - Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100" - HardwareAddr string `json:"hardwareaddr"` // IEEE MAC-48, EUI-48 and EUI-64 form - Flags []string `json:"flags"` // e.g., FlagUp, FlagLoopback, FlagMulticast - Addrs []InterfaceAddr `json:"addrs"` -} - -type FilterStat struct { - ConnTrackCount int64 `json:"conntrackCount"` - ConnTrackMax int64 `json:"conntrackMax"` -} - -// ConntrackStat has conntrack summary info -type ConntrackStat struct { - Entries uint32 `json:"entries"` // Number of entries in the conntrack table - Searched uint32 `json:"searched"` // Number of conntrack table lookups performed - Found uint32 `json:"found"` // Number of searched entries which were successful - New uint32 `json:"new"` // Number of entries added which were not expected before - Invalid uint32 `json:"invalid"` // Number of packets seen which can not be tracked - Ignore uint32 `json:"ignore"` // Packets seen which are already connected to an entry - Delete uint32 `json:"delete"` // Number of entries which were removed - DeleteList uint32 `json:"delete_list"` // Number of entries which were put to dying list - Insert uint32 `json:"insert"` // Number of entries inserted into the list - InsertFailed uint32 `json:"insert_failed"` // # insertion attempted but failed (same entry exists) - Drop uint32 `json:"drop"` // Number of packets dropped due to conntrack failure. - EarlyDrop uint32 `json:"early_drop"` // Dropped entries to make room for new ones, if maxsize reached - IcmpError uint32 `json:"icmp_error"` // Subset of invalid. Packets that can't be tracked d/t error - ExpectNew uint32 `json:"expect_new"` // Entries added after an expectation was already present - ExpectCreate uint32 `json:"expect_create"` // Expectations added - ExpectDelete uint32 `json:"expect_delete"` // Expectations deleted - SearchRestart uint32 `json:"search_restart"` // Conntrack table lookups restarted due to hashtable resizes -} - -func NewConntrackStat(e uint32, s uint32, f uint32, n uint32, inv uint32, ign uint32, del uint32, dlst uint32, ins uint32, insfail uint32, drop uint32, edrop uint32, ie uint32, en uint32, ec uint32, ed uint32, sr uint32) *ConntrackStat { - return &ConntrackStat{ - Entries: e, - Searched: s, - Found: f, - New: n, - Invalid: inv, - Ignore: ign, - Delete: del, - DeleteList: dlst, - Insert: ins, - InsertFailed: insfail, - Drop: drop, - EarlyDrop: edrop, - IcmpError: ie, - ExpectNew: en, - ExpectCreate: ec, - ExpectDelete: ed, - SearchRestart: sr, - } -} - -type ConntrackStatList struct { - items []*ConntrackStat -} - -func NewConntrackStatList() *ConntrackStatList { - return &ConntrackStatList{ - items: []*ConntrackStat{}, - } -} - -func (l *ConntrackStatList) Append(c *ConntrackStat) { - l.items = append(l.items, c) -} - -func (l *ConntrackStatList) Items() []ConntrackStat { - items := make([]ConntrackStat, len(l.items), len(l.items)) - for i, el := range l.items { - items[i] = *el - } - return items -} - -// Summary returns a single-element list with totals from all list items. -func (l *ConntrackStatList) Summary() []ConntrackStat { - summary := NewConntrackStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - for _, cs := range l.items { - summary.Entries += cs.Entries - summary.Searched += cs.Searched - summary.Found += cs.Found - summary.New += cs.New - summary.Invalid += cs.Invalid - summary.Ignore += cs.Ignore - summary.Delete += cs.Delete - summary.DeleteList += cs.DeleteList - summary.Insert += cs.Insert - summary.InsertFailed += cs.InsertFailed - summary.Drop += cs.Drop - summary.EarlyDrop += cs.EarlyDrop - summary.IcmpError += cs.IcmpError - summary.ExpectNew += cs.ExpectNew - summary.ExpectCreate += cs.ExpectCreate - summary.ExpectDelete += cs.ExpectDelete - summary.SearchRestart += cs.SearchRestart - } - return []ConntrackStat{*summary} -} - -func (n IOCountersStat) String() string { - s, _ := json.Marshal(n) - return string(s) -} - -func (n ConnectionStat) String() string { - s, _ := json.Marshal(n) - return string(s) -} - -func (n ProtoCountersStat) String() string { - s, _ := json.Marshal(n) - return string(s) -} - -func (a Addr) String() string { - s, _ := json.Marshal(a) - return string(s) -} - -func (n InterfaceStat) String() string { - s, _ := json.Marshal(n) - return string(s) -} - -func (n InterfaceAddr) String() string { - s, _ := json.Marshal(n) - return string(s) -} - -func (n ConntrackStat) String() string { - s, _ := json.Marshal(n) - return string(s) -} - -func Interfaces() ([]InterfaceStat, error) { - return InterfacesWithContext(context.Background()) -} - -func InterfacesWithContext(ctx context.Context) ([]InterfaceStat, error) { - is, err := net.Interfaces() - if err != nil { - return nil, err - } - ret := make([]InterfaceStat, 0, len(is)) - for _, ifi := range is { - - var flags []string - if ifi.Flags&net.FlagUp != 0 { - flags = append(flags, "up") - } - if ifi.Flags&net.FlagBroadcast != 0 { - flags = append(flags, "broadcast") - } - if ifi.Flags&net.FlagLoopback != 0 { - flags = append(flags, "loopback") - } - if ifi.Flags&net.FlagPointToPoint != 0 { - flags = append(flags, "pointtopoint") - } - if ifi.Flags&net.FlagMulticast != 0 { - flags = append(flags, "multicast") - } - - r := InterfaceStat{ - Index: ifi.Index, - Name: ifi.Name, - MTU: ifi.MTU, - HardwareAddr: ifi.HardwareAddr.String(), - Flags: flags, - } - addrs, err := ifi.Addrs() - if err == nil { - r.Addrs = make([]InterfaceAddr, 0, len(addrs)) - for _, addr := range addrs { - r.Addrs = append(r.Addrs, InterfaceAddr{ - Addr: addr.String(), - }) - } - - } - ret = append(ret, r) - } - - return ret, nil -} - -func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) { - r := IOCountersStat{ - Name: "all", - } - for _, nic := range n { - r.BytesRecv += nic.BytesRecv - r.PacketsRecv += nic.PacketsRecv - r.Errin += nic.Errin - r.Dropin += nic.Dropin - r.BytesSent += nic.BytesSent - r.PacketsSent += nic.PacketsSent - r.Errout += nic.Errout - r.Dropout += nic.Dropout - } - - return []IOCountersStat{r}, nil -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_aix.go b/vendor/github.com/shirou/gopsutil/net/net_aix.go deleted file mode 100644 index 4ac849701..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_aix.go +++ /dev/null @@ -1,425 +0,0 @@ -// +build aix - -package net - -import ( - "context" - "fmt" - "os/exec" - "regexp" - "strconv" - "strings" - "syscall" - - "github.com/shirou/gopsutil/internal/common" -) - -func parseNetstatI(output string) ([]IOCountersStat, error) { - lines := strings.Split(string(output), "\n") - ret := make([]IOCountersStat, 0, len(lines)-1) - exists := make([]string, 0, len(ret)) - - // Check first line is header - if len(lines) > 0 && strings.Fields(lines[0])[0] != "Name" { - return nil, fmt.Errorf("not a 'netstat -i' output") - } - - for _, line := range lines[1:] { - values := strings.Fields(line) - if len(values) < 1 || values[0] == "Name" { - continue - } - if common.StringsHas(exists, values[0]) { - // skip if already get - continue - } - exists = append(exists, values[0]) - - if len(values) < 9 { - continue - } - - base := 1 - // sometimes Address is omitted - if len(values) < 10 { - base = 0 - } - - parsed := make([]uint64, 0, 5) - vv := []string{ - values[base+3], // Ipkts == PacketsRecv - values[base+4], // Ierrs == Errin - values[base+5], // Opkts == PacketsSent - values[base+6], // Oerrs == Errout - values[base+8], // Drops == Dropout - } - - for _, target := range vv { - if target == "-" { - parsed = append(parsed, 0) - continue - } - - t, err := strconv.ParseUint(target, 10, 64) - if err != nil { - return nil, err - } - parsed = append(parsed, t) - } - - n := IOCountersStat{ - Name: values[0], - PacketsRecv: parsed[0], - Errin: parsed[1], - PacketsSent: parsed[2], - Errout: parsed[3], - Dropout: parsed[4], - } - ret = append(ret, n) - } - return ret, nil -} - -func IOCounters(pernic bool) ([]IOCountersStat, error) { - return IOCountersWithContext(context.Background(), pernic) -} - -func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { - netstat, err := exec.LookPath("netstat") - if err != nil { - return nil, err - } - out, err := invoke.CommandWithContext(ctx, netstat, "-idn") - if err != nil { - return nil, err - } - - iocounters, err := parseNetstatI(string(out)) - if err != nil { - return nil, err - } - if pernic == false { - return getIOCountersAll(iocounters) - } - return iocounters, nil - -} - -// NetIOCountersByFile is an method which is added just a compatibility for linux. -func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { - return IOCountersByFileWithContext(context.Background(), pernic, filename) -} - -func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { - return IOCounters(pernic) -} - -func FilterCounters() ([]FilterStat, error) { - return FilterCountersWithContext(context.Background()) -} - -func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, common.ErrNotImplementedError -} - -func ConntrackStats(percpu bool) ([]ConntrackStat, error) { - return ConntrackStatsWithContext(context.Background(), percpu) -} - -func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, common.ErrNotImplementedError -} - -func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { - return ProtoCountersWithContext(context.Background(), protocols) -} - -func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func parseNetstatNetLine(line string) (ConnectionStat, error) { - f := strings.Fields(line) - if len(f) < 5 { - return ConnectionStat{}, fmt.Errorf("wrong line,%s", line) - } - - var netType, netFamily uint32 - switch f[0] { - case "tcp", "tcp4": - netType = syscall.SOCK_STREAM - netFamily = syscall.AF_INET - case "udp", "udp4": - netType = syscall.SOCK_DGRAM - netFamily = syscall.AF_INET - case "tcp6": - netType = syscall.SOCK_STREAM - netFamily = syscall.AF_INET6 - case "udp6": - netType = syscall.SOCK_DGRAM - netFamily = syscall.AF_INET6 - default: - return ConnectionStat{}, fmt.Errorf("unknown type, %s", f[0]) - } - - laddr, raddr, err := parseNetstatAddr(f[3], f[4], netFamily) - if err != nil { - return ConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s %s", f[3], f[4]) - } - - n := ConnectionStat{ - Fd: uint32(0), // not supported - Family: uint32(netFamily), - Type: uint32(netType), - Laddr: laddr, - Raddr: raddr, - Pid: int32(0), // not supported - } - if len(f) == 6 { - n.Status = f[5] - } - - return n, nil -} - -var portMatch = regexp.MustCompile(`(.*)\.(\d+)$`) - -// This function only works for netstat returning addresses with a "." -// before the port (0.0.0.0.22 instead of 0.0.0.0:22). -func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, raddr Addr, err error) { - parse := func(l string) (Addr, error) { - matches := portMatch.FindStringSubmatch(l) - if matches == nil { - return Addr{}, fmt.Errorf("wrong addr, %s", l) - } - host := matches[1] - port := matches[2] - if host == "*" { - switch family { - case syscall.AF_INET: - host = "0.0.0.0" - case syscall.AF_INET6: - host = "::" - default: - return Addr{}, fmt.Errorf("unknown family, %d", family) - } - } - lport, err := strconv.Atoi(port) - if err != nil { - return Addr{}, err - } - return Addr{IP: host, Port: uint32(lport)}, nil - } - - laddr, err = parse(local) - if remote != "*.*" { // remote addr exists - raddr, err = parse(remote) - if err != nil { - return laddr, raddr, err - } - } - - return laddr, raddr, err -} - -func parseNetstatUnixLine(f []string) (ConnectionStat, error) { - if len(f) < 8 { - return ConnectionStat{}, fmt.Errorf("wrong number of fields: expected >=8 got %d", len(f)) - } - - var netType uint32 - - switch f[1] { - case "dgram": - netType = syscall.SOCK_DGRAM - case "stream": - netType = syscall.SOCK_STREAM - default: - return ConnectionStat{}, fmt.Errorf("unknown type: %s", f[1]) - } - - // Some Unix Socket don't have any address associated - addr := "" - if len(f) == 9 { - addr = f[8] - } - - c := ConnectionStat{ - Fd: uint32(0), // not supported - Family: uint32(syscall.AF_UNIX), - Type: uint32(netType), - Laddr: Addr{ - IP: addr, - }, - Status: "NONE", - Pid: int32(0), // not supported - } - - return c, nil -} - -// Return true if proto is the corresponding to the kind parameter -// Only for Inet lines -func hasCorrectInetProto(kind, proto string) bool { - switch kind { - case "all", "inet": - return true - case "unix": - return false - case "inet4": - return !strings.HasSuffix(proto, "6") - case "inet6": - return strings.HasSuffix(proto, "6") - case "tcp": - return proto == "tcp" || proto == "tcp4" || proto == "tcp6" - case "tcp4": - return proto == "tcp" || proto == "tcp4" - case "tcp6": - return proto == "tcp6" - case "udp": - return proto == "udp" || proto == "udp4" || proto == "udp6" - case "udp4": - return proto == "udp" || proto == "udp4" - case "udp6": - return proto == "udp6" - } - return false -} - -func parseNetstatA(output string, kind string) ([]ConnectionStat, error) { - var ret []ConnectionStat - lines := strings.Split(string(output), "\n") - - for _, line := range lines { - fields := strings.Fields(line) - if len(fields) < 1 { - continue - } - - if strings.HasPrefix(fields[0], "f1") { - // Unix lines - if len(fields) < 2 { - // every unix connections have two lines - continue - } - - c, err := parseNetstatUnixLine(fields) - if err != nil { - return nil, fmt.Errorf("failed to parse Unix Address (%s): %s", line, err) - } - - ret = append(ret, c) - - } else if strings.HasPrefix(fields[0], "tcp") || strings.HasPrefix(fields[0], "udp") { - // Inet lines - if !hasCorrectInetProto(kind, fields[0]) { - continue - } - - // On AIX, netstat display some connections with "*.*" as local addresses - // Skip them as they aren't real connections. - if fields[3] == "*.*" { - continue - } - - c, err := parseNetstatNetLine(line) - if err != nil { - return nil, fmt.Errorf("failed to parse Inet Address (%s): %s", line, err) - } - - ret = append(ret, c) - } else { - // Header lines - continue - } - } - - return ret, nil - -} - -func Connections(kind string) ([]ConnectionStat, error) { - return ConnectionsWithContext(context.Background(), kind) -} - -func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - - args := []string{"-na"} - switch strings.ToLower(kind) { - default: - fallthrough - case "": - kind = "all" - case "all": - // nothing to add - case "inet", "inet4", "inet6": - args = append(args, "-finet") - case "tcp", "tcp4", "tcp6": - args = append(args, "-finet") - case "udp", "udp4", "udp6": - args = append(args, "-finet") - case "unix": - args = append(args, "-funix") - } - - netstat, err := exec.LookPath("netstat") - if err != nil { - return nil, err - } - out, err := invoke.CommandWithContext(ctx, netstat, args...) - - if err != nil { - return nil, err - } - - ret, err := parseNetstatA(string(out), kind) - if err != nil { - return nil, err - } - - return ret, nil - -} - -func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { - return ConnectionsMaxWithContext(context.Background(), kind, max) -} - -func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} - -// Return a list of network connections opened, omitting `Uids`. -// WithoutUids functions are reliant on implementation details. They may be altered to be an alias for Connections or be -// removed from the API in the future. -func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error) { - return ConnectionsWithoutUidsWithContext(context.Background(), kind) -} - -func ConnectionsWithoutUidsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsMaxWithoutUidsWithContext(ctx, kind, 0) -} - -func ConnectionsMaxWithoutUidsWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, 0, max) -} - -func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithoutUidsWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithoutUidsWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, 0) -} - -func ConnectionsPidMaxWithoutUids(kind string, pid int32, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(context.Background(), kind, pid, max) -} - -func ConnectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max) -} - -func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_darwin.go b/vendor/github.com/shirou/gopsutil/net/net_darwin.go deleted file mode 100644 index 8e6e7f953..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_darwin.go +++ /dev/null @@ -1,293 +0,0 @@ -// +build darwin - -package net - -import ( - "context" - "errors" - "fmt" - "github.com/shirou/gopsutil/internal/common" - "os/exec" - "regexp" - "strconv" - "strings" -) - -var ( - errNetstatHeader = errors.New("Can't parse header of netstat output") - netstatLinkRegexp = regexp.MustCompile(`^$`) -) - -const endOfLine = "\n" - -func parseNetstatLine(line string) (stat *IOCountersStat, linkID *uint, err error) { - var ( - numericValue uint64 - columns = strings.Fields(line) - ) - - if columns[0] == "Name" { - err = errNetstatHeader - return - } - - // try to extract the numeric value from - if subMatch := netstatLinkRegexp.FindStringSubmatch(columns[2]); len(subMatch) == 2 { - numericValue, err = strconv.ParseUint(subMatch[1], 10, 64) - if err != nil { - return - } - linkIDUint := uint(numericValue) - linkID = &linkIDUint - } - - base := 1 - numberColumns := len(columns) - // sometimes Address is omitted - if numberColumns < 12 { - base = 0 - } - if numberColumns < 11 || numberColumns > 13 { - err = fmt.Errorf("Line %q do have an invalid number of columns %d", line, numberColumns) - return - } - - parsed := make([]uint64, 0, 7) - vv := []string{ - columns[base+3], // Ipkts == PacketsRecv - columns[base+4], // Ierrs == Errin - columns[base+5], // Ibytes == BytesRecv - columns[base+6], // Opkts == PacketsSent - columns[base+7], // Oerrs == Errout - columns[base+8], // Obytes == BytesSent - } - if len(columns) == 12 { - vv = append(vv, columns[base+10]) - } - - for _, target := range vv { - if target == "-" { - parsed = append(parsed, 0) - continue - } - - if numericValue, err = strconv.ParseUint(target, 10, 64); err != nil { - return - } - parsed = append(parsed, numericValue) - } - - stat = &IOCountersStat{ - Name: strings.Trim(columns[0], "*"), // remove the * that sometimes is on right on interface - PacketsRecv: parsed[0], - Errin: parsed[1], - BytesRecv: parsed[2], - PacketsSent: parsed[3], - Errout: parsed[4], - BytesSent: parsed[5], - } - if len(parsed) == 7 { - stat.Dropout = parsed[6] - } - return -} - -type netstatInterface struct { - linkID *uint - stat *IOCountersStat -} - -func parseNetstatOutput(output string) ([]netstatInterface, error) { - var ( - err error - lines = strings.Split(strings.Trim(output, endOfLine), endOfLine) - ) - - // number of interfaces is number of lines less one for the header - numberInterfaces := len(lines) - 1 - - interfaces := make([]netstatInterface, numberInterfaces) - // no output beside header - if numberInterfaces == 0 { - return interfaces, nil - } - - for index := 0; index < numberInterfaces; index++ { - nsIface := netstatInterface{} - if nsIface.stat, nsIface.linkID, err = parseNetstatLine(lines[index+1]); err != nil { - return nil, err - } - interfaces[index] = nsIface - } - return interfaces, nil -} - -// map that hold the name of a network interface and the number of usage -type mapInterfaceNameUsage map[string]uint - -func newMapInterfaceNameUsage(ifaces []netstatInterface) mapInterfaceNameUsage { - output := make(mapInterfaceNameUsage) - for index := range ifaces { - if ifaces[index].linkID != nil { - ifaceName := ifaces[index].stat.Name - usage, ok := output[ifaceName] - if ok { - output[ifaceName] = usage + 1 - } else { - output[ifaceName] = 1 - } - } - } - return output -} - -func (min mapInterfaceNameUsage) isTruncated() bool { - for _, usage := range min { - if usage > 1 { - return true - } - } - return false -} - -func (min mapInterfaceNameUsage) notTruncated() []string { - output := make([]string, 0) - for ifaceName, usage := range min { - if usage == 1 { - output = append(output, ifaceName) - } - } - return output -} - -// example of `netstat -ibdnW` output on yosemite -// Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Drop -// lo0 16384 869107 0 169411755 869107 0 169411755 0 0 -// lo0 16384 ::1/128 ::1 869107 - 169411755 869107 - 169411755 - - -// lo0 16384 127 127.0.0.1 869107 - 169411755 869107 - 169411755 - - -func IOCounters(pernic bool) ([]IOCountersStat, error) { - return IOCountersWithContext(context.Background(), pernic) -} - -func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { - var ( - ret []IOCountersStat - retIndex int - ) - - netstat, err := exec.LookPath("netstat") - if err != nil { - return nil, err - } - - // try to get all interface metrics, and hope there won't be any truncated - out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW") - if err != nil { - return nil, err - } - - nsInterfaces, err := parseNetstatOutput(string(out)) - if err != nil { - return nil, err - } - - ifaceUsage := newMapInterfaceNameUsage(nsInterfaces) - notTruncated := ifaceUsage.notTruncated() - ret = make([]IOCountersStat, len(notTruncated)) - - if !ifaceUsage.isTruncated() { - // no truncated interface name, return stats of all interface with - for index := range nsInterfaces { - if nsInterfaces[index].linkID != nil { - ret[retIndex] = *nsInterfaces[index].stat - retIndex++ - } - } - } else { - // duplicated interface, list all interfaces - ifconfig, err := exec.LookPath("ifconfig") - if err != nil { - return nil, err - } - if out, err = invoke.CommandWithContext(ctx, ifconfig, "-l"); err != nil { - return nil, err - } - interfaceNames := strings.Fields(strings.TrimRight(string(out), endOfLine)) - - // for each of the interface name, run netstat if we don't have any stats yet - for _, interfaceName := range interfaceNames { - truncated := true - for index := range nsInterfaces { - if nsInterfaces[index].linkID != nil && nsInterfaces[index].stat.Name == interfaceName { - // handle the non truncated name to avoid execute netstat for them again - ret[retIndex] = *nsInterfaces[index].stat - retIndex++ - truncated = false - break - } - } - if truncated { - // run netstat with -I$ifacename - if out, err = invoke.CommandWithContext(ctx, netstat, "-ibdnWI"+interfaceName); err != nil { - return nil, err - } - parsedIfaces, err := parseNetstatOutput(string(out)) - if err != nil { - return nil, err - } - if len(parsedIfaces) == 0 { - // interface had been removed since `ifconfig -l` had been executed - continue - } - for index := range parsedIfaces { - if parsedIfaces[index].linkID != nil { - ret = append(ret, *parsedIfaces[index].stat) - break - } - } - } - } - } - - if pernic == false { - return getIOCountersAll(ret) - } - return ret, nil -} - -// NetIOCountersByFile is an method which is added just a compatibility for linux. -func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { - return IOCountersByFileWithContext(context.Background(), pernic, filename) -} - -func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { - return IOCounters(pernic) -} - -func FilterCounters() ([]FilterStat, error) { - return FilterCountersWithContext(context.Background()) -} - -func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, common.ErrNotImplementedError -} - -func ConntrackStats(percpu bool) ([]ConntrackStat, error) { - return ConntrackStatsWithContext(context.Background(), percpu) -} - -func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, common.ErrNotImplementedError -} - -// NetProtoCounters returns network statistics for the entire system -// If protocols is empty then all protocols are returned, otherwise -// just the protocols in the list are returned. -// Not Implemented for Darwin -func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { - return ProtoCountersWithContext(context.Background(), protocols) -} - -func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_fallback.go b/vendor/github.com/shirou/gopsutil/net/net_fallback.go deleted file mode 100644 index 7d9a26591..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_fallback.go +++ /dev/null @@ -1,92 +0,0 @@ -// +build !aix,!darwin,!linux,!freebsd,!openbsd,!windows - -package net - -import ( - "context" - - "github.com/shirou/gopsutil/internal/common" -) - -func IOCounters(pernic bool) ([]IOCountersStat, error) { - return IOCountersWithContext(context.Background(), pernic) -} - -func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { - return []IOCountersStat{}, common.ErrNotImplementedError -} - -func FilterCounters() ([]FilterStat, error) { - return FilterCountersWithContext(context.Background()) -} - -func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return []FilterStat{}, common.ErrNotImplementedError -} - -func ConntrackStats(percpu bool) ([]ConntrackStat, error) { - return ConntrackStatsWithContext(context.Background(), percpu) -} - -func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, common.ErrNotImplementedError -} - -func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { - return ProtoCountersWithContext(context.Background(), protocols) -} - -func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return []ProtoCountersStat{}, common.ErrNotImplementedError -} - -func Connections(kind string) ([]ConnectionStat, error) { - return ConnectionsWithContext(context.Background(), kind) -} - -func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} - -func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { - return ConnectionsMaxWithContext(context.Background(), kind, max) -} - -func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} - -// Return a list of network connections opened, omitting `Uids`. -// WithoutUids functions are reliant on implementation details. They may be altered to be an alias for Connections or be -// removed from the API in the future. -func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error) { - return ConnectionsWithoutUidsWithContext(context.Background(), kind) -} - -func ConnectionsWithoutUidsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsMaxWithoutUidsWithContext(ctx, kind, 0) -} - -func ConnectionsMaxWithoutUidsWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, 0, max) -} - -func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithoutUidsWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithoutUidsWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, 0) -} - -func ConnectionsPidMaxWithoutUids(kind string, pid int32, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(context.Background(), kind, pid, max) -} - -func ConnectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max) -} - -func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_freebsd.go b/vendor/github.com/shirou/gopsutil/net/net_freebsd.go deleted file mode 100644 index 1204f5977..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_freebsd.go +++ /dev/null @@ -1,132 +0,0 @@ -// +build freebsd - -package net - -import ( - "context" - "os/exec" - "strconv" - "strings" - - "github.com/shirou/gopsutil/internal/common" -) - -func IOCounters(pernic bool) ([]IOCountersStat, error) { - return IOCountersWithContext(context.Background(), pernic) -} - -func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { - netstat, err := exec.LookPath("netstat") - if err != nil { - return nil, err - } - out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW") - if err != nil { - return nil, err - } - - lines := strings.Split(string(out), "\n") - ret := make([]IOCountersStat, 0, len(lines)-1) - exists := make([]string, 0, len(ret)) - - for _, line := range lines { - values := strings.Fields(line) - if len(values) < 1 || values[0] == "Name" { - continue - } - if common.StringsHas(exists, values[0]) { - // skip if already get - continue - } - exists = append(exists, values[0]) - - if len(values) < 12 { - continue - } - base := 1 - // sometimes Address is omitted - if len(values) < 13 { - base = 0 - } - - parsed := make([]uint64, 0, 8) - vv := []string{ - values[base+3], // PacketsRecv - values[base+4], // Errin - values[base+5], // Dropin - values[base+6], // BytesRecvn - values[base+7], // PacketSent - values[base+8], // Errout - values[base+9], // BytesSent - values[base+11], // Dropout - } - for _, target := range vv { - if target == "-" { - parsed = append(parsed, 0) - continue - } - - t, err := strconv.ParseUint(target, 10, 64) - if err != nil { - return nil, err - } - parsed = append(parsed, t) - } - - n := IOCountersStat{ - Name: values[0], - PacketsRecv: parsed[0], - Errin: parsed[1], - Dropin: parsed[2], - BytesRecv: parsed[3], - PacketsSent: parsed[4], - Errout: parsed[5], - BytesSent: parsed[6], - Dropout: parsed[7], - } - ret = append(ret, n) - } - - if pernic == false { - return getIOCountersAll(ret) - } - - return ret, nil -} - -// NetIOCountersByFile is an method which is added just a compatibility for linux. -func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { - return IOCountersByFileWithContext(context.Background(), pernic, filename) -} - -func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { - return IOCounters(pernic) -} - -func FilterCounters() ([]FilterStat, error) { - return FilterCountersWithContext(context.Background()) -} - -func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, common.ErrNotImplementedError -} - -func ConntrackStats(percpu bool) ([]ConntrackStat, error) { - return ConntrackStatsWithContext(context.Background(), percpu) -} - -func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, common.ErrNotImplementedError -} - -// NetProtoCounters returns network statistics for the entire system -// If protocols is empty then all protocols are returned, otherwise -// just the protocols in the list are returned. -// Not Implemented for FreeBSD -func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { - return ProtoCountersWithContext(context.Background(), protocols) -} - -func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_linux.go b/vendor/github.com/shirou/gopsutil/net/net_linux.go deleted file mode 100644 index ed5d027b8..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_linux.go +++ /dev/null @@ -1,884 +0,0 @@ -// +build linux - -package net - -import ( - "bytes" - "context" - "encoding/hex" - "errors" - "fmt" - "io" - "io/ioutil" - "net" - "os" - "strconv" - "strings" - "syscall" - - "github.com/shirou/gopsutil/internal/common" -) - -const ( // Conntrack Column numbers - CT_ENTRIES = iota - CT_SEARCHED - CT_FOUND - CT_NEW - CT_INVALID - CT_IGNORE - CT_DELETE - CT_DELETE_LIST - CT_INSERT - CT_INSERT_FAILED - CT_DROP - CT_EARLY_DROP - CT_ICMP_ERROR - CT_EXPECT_NEW - CT_EXPECT_CREATE - CT_EXPECT_DELETE - CT_SEARCH_RESTART -) - -// NetIOCounters returnes network I/O statistics for every network -// interface installed on the system. If pernic argument is false, -// return only sum of all information (which name is 'all'). If true, -// every network interface installed on the system is returned -// separately. -func IOCounters(pernic bool) ([]IOCountersStat, error) { - return IOCountersWithContext(context.Background(), pernic) -} - -func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { - filename := common.HostProc("net/dev") - return IOCountersByFileWithContext(ctx, pernic, filename) -} - -func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { - return IOCountersByFileWithContext(context.Background(), pernic, filename) -} - -func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { - lines, err := common.ReadLines(filename) - if err != nil { - return nil, err - } - - parts := make([]string, 2) - - statlen := len(lines) - 1 - - ret := make([]IOCountersStat, 0, statlen) - - for _, line := range lines[2:] { - separatorPos := strings.LastIndex(line, ":") - if separatorPos == -1 { - continue - } - parts[0] = line[0:separatorPos] - parts[1] = line[separatorPos+1:] - - interfaceName := strings.TrimSpace(parts[0]) - if interfaceName == "" { - continue - } - - fields := strings.Fields(strings.TrimSpace(parts[1])) - bytesRecv, err := strconv.ParseUint(fields[0], 10, 64) - if err != nil { - return ret, err - } - packetsRecv, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - return ret, err - } - errIn, err := strconv.ParseUint(fields[2], 10, 64) - if err != nil { - return ret, err - } - dropIn, err := strconv.ParseUint(fields[3], 10, 64) - if err != nil { - return ret, err - } - fifoIn, err := strconv.ParseUint(fields[4], 10, 64) - if err != nil { - return ret, err - } - bytesSent, err := strconv.ParseUint(fields[8], 10, 64) - if err != nil { - return ret, err - } - packetsSent, err := strconv.ParseUint(fields[9], 10, 64) - if err != nil { - return ret, err - } - errOut, err := strconv.ParseUint(fields[10], 10, 64) - if err != nil { - return ret, err - } - dropOut, err := strconv.ParseUint(fields[11], 10, 64) - if err != nil { - return ret, err - } - fifoOut, err := strconv.ParseUint(fields[12], 10, 64) - if err != nil { - return ret, err - } - - nic := IOCountersStat{ - Name: interfaceName, - BytesRecv: bytesRecv, - PacketsRecv: packetsRecv, - Errin: errIn, - Dropin: dropIn, - Fifoin: fifoIn, - BytesSent: bytesSent, - PacketsSent: packetsSent, - Errout: errOut, - Dropout: dropOut, - Fifoout: fifoOut, - } - ret = append(ret, nic) - } - - if pernic == false { - return getIOCountersAll(ret) - } - - return ret, nil -} - -var netProtocols = []string{ - "ip", - "icmp", - "icmpmsg", - "tcp", - "udp", - "udplite", -} - -// NetProtoCounters returns network statistics for the entire system -// If protocols is empty then all protocols are returned, otherwise -// just the protocols in the list are returned. -// Available protocols: -// ip,icmp,icmpmsg,tcp,udp,udplite -func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { - return ProtoCountersWithContext(context.Background(), protocols) -} - -func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - if len(protocols) == 0 { - protocols = netProtocols - } - - stats := make([]ProtoCountersStat, 0, len(protocols)) - protos := make(map[string]bool, len(protocols)) - for _, p := range protocols { - protos[p] = true - } - - filename := common.HostProc("net/snmp") - lines, err := common.ReadLines(filename) - if err != nil { - return nil, err - } - - linecount := len(lines) - for i := 0; i < linecount; i++ { - line := lines[i] - r := strings.IndexRune(line, ':') - if r == -1 { - return nil, errors.New(filename + " is not fomatted correctly, expected ':'.") - } - proto := strings.ToLower(line[:r]) - if !protos[proto] { - // skip protocol and data line - i++ - continue - } - - // Read header line - statNames := strings.Split(line[r+2:], " ") - - // Read data line - i++ - statValues := strings.Split(lines[i][r+2:], " ") - if len(statNames) != len(statValues) { - return nil, errors.New(filename + " is not fomatted correctly, expected same number of columns.") - } - stat := ProtoCountersStat{ - Protocol: proto, - Stats: make(map[string]int64, len(statNames)), - } - for j := range statNames { - value, err := strconv.ParseInt(statValues[j], 10, 64) - if err != nil { - return nil, err - } - stat.Stats[statNames[j]] = value - } - stats = append(stats, stat) - } - return stats, nil -} - -// NetFilterCounters returns iptables conntrack statistics -// the currently in use conntrack count and the max. -// If the file does not exist or is invalid it will return nil. -func FilterCounters() ([]FilterStat, error) { - return FilterCountersWithContext(context.Background()) -} - -func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - countfile := common.HostProc("sys/net/netfilter/nf_conntrack_count") - maxfile := common.HostProc("sys/net/netfilter/nf_conntrack_max") - - count, err := common.ReadInts(countfile) - - if err != nil { - return nil, err - } - stats := make([]FilterStat, 0, 1) - - max, err := common.ReadInts(maxfile) - if err != nil { - return nil, err - } - - payload := FilterStat{ - ConnTrackCount: count[0], - ConnTrackMax: max[0], - } - - stats = append(stats, payload) - return stats, nil -} - -// ConntrackStats returns more detailed info about the conntrack table -func ConntrackStats(percpu bool) ([]ConntrackStat, error) { - return ConntrackStatsWithContext(context.Background(), percpu) -} - -// ConntrackStatsWithContext returns more detailed info about the conntrack table -func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return conntrackStatsFromFile(common.HostProc("net/stat/nf_conntrack"), percpu) -} - -// conntrackStatsFromFile returns more detailed info about the conntrack table -// from `filename` -// If 'percpu' is false, the result will contain exactly one item with totals/summary -func conntrackStatsFromFile(filename string, percpu bool) ([]ConntrackStat, error) { - lines, err := common.ReadLines(filename) - if err != nil { - return nil, err - } - - statlist := NewConntrackStatList() - - for _, line := range lines { - fields := strings.Fields(line) - if len(fields) == 17 && fields[0] != "entries" { - statlist.Append(NewConntrackStat( - common.HexToUint32(fields[CT_ENTRIES]), - common.HexToUint32(fields[CT_SEARCHED]), - common.HexToUint32(fields[CT_FOUND]), - common.HexToUint32(fields[CT_NEW]), - common.HexToUint32(fields[CT_INVALID]), - common.HexToUint32(fields[CT_IGNORE]), - common.HexToUint32(fields[CT_DELETE]), - common.HexToUint32(fields[CT_DELETE_LIST]), - common.HexToUint32(fields[CT_INSERT]), - common.HexToUint32(fields[CT_INSERT_FAILED]), - common.HexToUint32(fields[CT_DROP]), - common.HexToUint32(fields[CT_EARLY_DROP]), - common.HexToUint32(fields[CT_ICMP_ERROR]), - common.HexToUint32(fields[CT_EXPECT_NEW]), - common.HexToUint32(fields[CT_EXPECT_CREATE]), - common.HexToUint32(fields[CT_EXPECT_DELETE]), - common.HexToUint32(fields[CT_SEARCH_RESTART]), - )) - } - } - - if percpu { - return statlist.Items(), nil - } - return statlist.Summary(), nil -} - -// http://students.mimuw.edu.pl/lxr/source/include/net/tcp_states.h -var TCPStatuses = map[string]string{ - "01": "ESTABLISHED", - "02": "SYN_SENT", - "03": "SYN_RECV", - "04": "FIN_WAIT1", - "05": "FIN_WAIT2", - "06": "TIME_WAIT", - "07": "CLOSE", - "08": "CLOSE_WAIT", - "09": "LAST_ACK", - "0A": "LISTEN", - "0B": "CLOSING", -} - -type netConnectionKindType struct { - family uint32 - sockType uint32 - filename string -} - -var kindTCP4 = netConnectionKindType{ - family: syscall.AF_INET, - sockType: syscall.SOCK_STREAM, - filename: "tcp", -} -var kindTCP6 = netConnectionKindType{ - family: syscall.AF_INET6, - sockType: syscall.SOCK_STREAM, - filename: "tcp6", -} -var kindUDP4 = netConnectionKindType{ - family: syscall.AF_INET, - sockType: syscall.SOCK_DGRAM, - filename: "udp", -} -var kindUDP6 = netConnectionKindType{ - family: syscall.AF_INET6, - sockType: syscall.SOCK_DGRAM, - filename: "udp6", -} -var kindUNIX = netConnectionKindType{ - family: syscall.AF_UNIX, - filename: "unix", -} - -var netConnectionKindMap = map[string][]netConnectionKindType{ - "all": {kindTCP4, kindTCP6, kindUDP4, kindUDP6, kindUNIX}, - "tcp": {kindTCP4, kindTCP6}, - "tcp4": {kindTCP4}, - "tcp6": {kindTCP6}, - "udp": {kindUDP4, kindUDP6}, - "udp4": {kindUDP4}, - "udp6": {kindUDP6}, - "unix": {kindUNIX}, - "inet": {kindTCP4, kindTCP6, kindUDP4, kindUDP6}, - "inet4": {kindTCP4, kindUDP4}, - "inet6": {kindTCP6, kindUDP6}, -} - -type inodeMap struct { - pid int32 - fd uint32 -} - -type connTmp struct { - fd uint32 - family uint32 - sockType uint32 - laddr Addr - raddr Addr - status string - pid int32 - boundPid int32 - path string -} - -// Return a list of network connections opened. -func Connections(kind string) ([]ConnectionStat, error) { - return ConnectionsWithContext(context.Background(), kind) -} - -func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsPid(kind, 0) -} - -// Return a list of network connections opened returning at most `max` -// connections for each running process. -func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { - return ConnectionsMaxWithContext(context.Background(), kind, max) -} - -func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return ConnectionsPidMax(kind, 0, max) -} - -// Return a list of network connections opened, omitting `Uids`. -// WithoutUids functions are reliant on implementation details. They may be altered to be an alias for Connections or be -// removed from the API in the future. -func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error) { - return ConnectionsWithoutUidsWithContext(context.Background(), kind) -} - -func ConnectionsWithoutUidsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsMaxWithoutUidsWithContext(ctx, kind, 0) -} - -func ConnectionsMaxWithoutUidsWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, 0, max) -} - -// Return a list of network connections opened by a process. -func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithoutUidsWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithContext(ctx, kind, pid, 0) -} - -func ConnectionsPidWithoutUidsWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, 0) -} - -// Return up to `max` network connections opened by a process. -func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithContext(context.Background(), kind, pid, max) -} - -func ConnectionsPidMaxWithoutUids(kind string, pid int32, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(context.Background(), kind, pid, max) -} - -func ConnectionsPidMaxWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max, false) -} - -func ConnectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max, true) -} - -func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int, skipUids bool) ([]ConnectionStat, error) { - tmap, ok := netConnectionKindMap[kind] - if !ok { - return nil, fmt.Errorf("invalid kind, %s", kind) - } - root := common.HostProc() - var err error - var inodes map[string][]inodeMap - if pid == 0 { - inodes, err = getProcInodesAll(root, max) - } else { - inodes, err = getProcInodes(root, pid, max) - if len(inodes) == 0 { - // no connection for the pid - return []ConnectionStat{}, nil - } - } - if err != nil { - return nil, fmt.Errorf("cound not get pid(s), %d: %s", pid, err) - } - return statsFromInodes(root, pid, tmap, inodes, skipUids) -} - -func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap, skipUids bool) ([]ConnectionStat, error) { - dupCheckMap := make(map[string]struct{}) - var ret []ConnectionStat - - var err error - for _, t := range tmap { - var path string - var connKey string - var ls []connTmp - if pid == 0 { - path = fmt.Sprintf("%s/net/%s", root, t.filename) - } else { - path = fmt.Sprintf("%s/%d/net/%s", root, pid, t.filename) - } - switch t.family { - case syscall.AF_INET, syscall.AF_INET6: - ls, err = processInet(path, t, inodes, pid) - case syscall.AF_UNIX: - ls, err = processUnix(path, t, inodes, pid) - } - if err != nil { - return nil, err - } - for _, c := range ls { - // Build TCP key to id the connection uniquely - // socket type, src ip, src port, dst ip, dst port and state should be enough - // to prevent duplications. - connKey = fmt.Sprintf("%d-%s:%d-%s:%d-%s", c.sockType, c.laddr.IP, c.laddr.Port, c.raddr.IP, c.raddr.Port, c.status) - if _, ok := dupCheckMap[connKey]; ok { - continue - } - - conn := ConnectionStat{ - Fd: c.fd, - Family: c.family, - Type: c.sockType, - Laddr: c.laddr, - Raddr: c.raddr, - Status: c.status, - Pid: c.pid, - } - if c.pid == 0 { - conn.Pid = c.boundPid - } else { - conn.Pid = c.pid - } - - if !skipUids { - // fetch process owner Real, effective, saved set, and filesystem UIDs - proc := process{Pid: conn.Pid} - conn.Uids, _ = proc.getUids() - } - - ret = append(ret, conn) - dupCheckMap[connKey] = struct{}{} - } - - } - - return ret, nil -} - -// getProcInodes returnes fd of the pid. -func getProcInodes(root string, pid int32, max int) (map[string][]inodeMap, error) { - ret := make(map[string][]inodeMap) - - dir := fmt.Sprintf("%s/%d/fd", root, pid) - f, err := os.Open(dir) - if err != nil { - return ret, err - } - defer f.Close() - files, err := f.Readdir(max) - if err != nil { - return ret, err - } - for _, fd := range files { - inodePath := fmt.Sprintf("%s/%d/fd/%s", root, pid, fd.Name()) - - inode, err := os.Readlink(inodePath) - if err != nil { - continue - } - if !strings.HasPrefix(inode, "socket:[") { - continue - } - // the process is using a socket - l := len(inode) - inode = inode[8 : l-1] - _, ok := ret[inode] - if !ok { - ret[inode] = make([]inodeMap, 0) - } - fd, err := strconv.Atoi(fd.Name()) - if err != nil { - continue - } - - i := inodeMap{ - pid: pid, - fd: uint32(fd), - } - ret[inode] = append(ret[inode], i) - } - return ret, nil -} - -// Pids retunres all pids. -// Note: this is a copy of process_linux.Pids() -// FIXME: Import process occures import cycle. -// move to common made other platform breaking. Need consider. -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { - var ret []int32 - - d, err := os.Open(common.HostProc()) - if err != nil { - return nil, err - } - defer d.Close() - - fnames, err := d.Readdirnames(-1) - if err != nil { - return nil, err - } - for _, fname := range fnames { - pid, err := strconv.ParseInt(fname, 10, 32) - if err != nil { - // if not numeric name, just skip - continue - } - ret = append(ret, int32(pid)) - } - - return ret, nil -} - -// Note: the following is based off process_linux structs and methods -// we need these to fetch the owner of a process ID -// FIXME: Import process occures import cycle. -// see remarks on pids() -type process struct { - Pid int32 `json:"pid"` - uids []int32 -} - -// Uids returns user ids of the process as a slice of the int -func (p *process) getUids() ([]int32, error) { - err := p.fillFromStatus() - if err != nil { - return []int32{}, err - } - return p.uids, nil -} - -// Get status from /proc/(pid)/status -func (p *process) fillFromStatus() error { - pid := p.Pid - statPath := common.HostProc(strconv.Itoa(int(pid)), "status") - contents, err := ioutil.ReadFile(statPath) - if err != nil { - return err - } - lines := strings.Split(string(contents), "\n") - for _, line := range lines { - tabParts := strings.SplitN(line, "\t", 2) - if len(tabParts) < 2 { - continue - } - value := tabParts[1] - switch strings.TrimRight(tabParts[0], ":") { - case "Uid": - p.uids = make([]int32, 0, 4) - for _, i := range strings.Split(value, "\t") { - v, err := strconv.ParseInt(i, 10, 32) - if err != nil { - return err - } - p.uids = append(p.uids, int32(v)) - } - } - } - return nil -} - -func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) { - pids, err := Pids() - if err != nil { - return nil, err - } - ret := make(map[string][]inodeMap) - - for _, pid := range pids { - t, err := getProcInodes(root, pid, max) - if err != nil { - // skip if permission error or no longer exists - if os.IsPermission(err) || os.IsNotExist(err) || err == io.EOF { - continue - } - return ret, err - } - if len(t) == 0 { - continue - } - // TODO: update ret. - ret = updateMap(ret, t) - } - return ret, nil -} - -// decodeAddress decode addresse represents addr in proc/net/* -// ex: -// "0500000A:0016" -> "10.0.0.5", 22 -// "0085002452100113070057A13F025401:0035" -> "2400:8500:1301:1052:a157:7:154:23f", 53 -func decodeAddress(family uint32, src string) (Addr, error) { - t := strings.Split(src, ":") - if len(t) != 2 { - return Addr{}, fmt.Errorf("does not contain port, %s", src) - } - addr := t[0] - port, err := strconv.ParseUint(t[1], 16, 16) - if err != nil { - return Addr{}, fmt.Errorf("invalid port, %s", src) - } - decoded, err := hex.DecodeString(addr) - if err != nil { - return Addr{}, fmt.Errorf("decode error, %s", err) - } - var ip net.IP - // Assumes this is little_endian - if family == syscall.AF_INET { - ip = net.IP(Reverse(decoded)) - } else { // IPv6 - ip, err = parseIPv6HexString(decoded) - if err != nil { - return Addr{}, err - } - } - return Addr{ - IP: ip.String(), - Port: uint32(port), - }, nil -} - -// Reverse reverses array of bytes. -func Reverse(s []byte) []byte { - return ReverseWithContext(context.Background(), s) -} - -func ReverseWithContext(ctx context.Context, s []byte) []byte { - for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { - s[i], s[j] = s[j], s[i] - } - return s -} - -// parseIPv6HexString parse array of bytes to IPv6 string -func parseIPv6HexString(src []byte) (net.IP, error) { - if len(src) != 16 { - return nil, fmt.Errorf("invalid IPv6 string") - } - - buf := make([]byte, 0, 16) - for i := 0; i < len(src); i += 4 { - r := Reverse(src[i : i+4]) - buf = append(buf, r...) - } - return net.IP(buf), nil -} - -func processInet(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) { - - if strings.HasSuffix(file, "6") && !common.PathExists(file) { - // IPv6 not supported, return empty. - return []connTmp{}, nil - } - - // Read the contents of the /proc file with a single read sys call. - // This minimizes duplicates in the returned connections - // For more info: - // https://github.com/shirou/gopsutil/pull/361 - contents, err := ioutil.ReadFile(file) - if err != nil { - return nil, err - } - - lines := bytes.Split(contents, []byte("\n")) - - var ret []connTmp - // skip first line - for _, line := range lines[1:] { - l := strings.Fields(string(line)) - if len(l) < 10 { - continue - } - laddr := l[1] - raddr := l[2] - status := l[3] - inode := l[9] - pid := int32(0) - fd := uint32(0) - i, exists := inodes[inode] - if exists { - pid = i[0].pid - fd = i[0].fd - } - if filterPid > 0 && filterPid != pid { - continue - } - if kind.sockType == syscall.SOCK_STREAM { - status = TCPStatuses[status] - } else { - status = "NONE" - } - la, err := decodeAddress(kind.family, laddr) - if err != nil { - continue - } - ra, err := decodeAddress(kind.family, raddr) - if err != nil { - continue - } - - ret = append(ret, connTmp{ - fd: fd, - family: kind.family, - sockType: kind.sockType, - laddr: la, - raddr: ra, - status: status, - pid: pid, - }) - } - - return ret, nil -} - -func processUnix(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) { - // Read the contents of the /proc file with a single read sys call. - // This minimizes duplicates in the returned connections - // For more info: - // https://github.com/shirou/gopsutil/pull/361 - contents, err := ioutil.ReadFile(file) - if err != nil { - return nil, err - } - - lines := bytes.Split(contents, []byte("\n")) - - var ret []connTmp - // skip first line - for _, line := range lines[1:] { - tokens := strings.Fields(string(line)) - if len(tokens) < 6 { - continue - } - st, err := strconv.Atoi(tokens[4]) - if err != nil { - return nil, err - } - - inode := tokens[6] - - var pairs []inodeMap - pairs, exists := inodes[inode] - if !exists { - pairs = []inodeMap{ - {}, - } - } - for _, pair := range pairs { - if filterPid > 0 && filterPid != pair.pid { - continue - } - var path string - if len(tokens) == 8 { - path = tokens[len(tokens)-1] - } - ret = append(ret, connTmp{ - fd: pair.fd, - family: kind.family, - sockType: uint32(st), - laddr: Addr{ - IP: path, - }, - pid: pair.pid, - status: "NONE", - path: path, - }) - } - } - - return ret, nil -} - -func updateMap(src map[string][]inodeMap, add map[string][]inodeMap) map[string][]inodeMap { - for key, value := range add { - a, exists := src[key] - if !exists { - src[key] = value - continue - } - src[key] = append(a, value...) - } - return src -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_openbsd.go b/vendor/github.com/shirou/gopsutil/net/net_openbsd.go deleted file mode 100644 index cfed2bee4..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_openbsd.go +++ /dev/null @@ -1,319 +0,0 @@ -// +build openbsd - -package net - -import ( - "context" - "fmt" - "os/exec" - "regexp" - "strconv" - "strings" - "syscall" - - "github.com/shirou/gopsutil/internal/common" -) - -var portMatch = regexp.MustCompile(`(.*)\.(\d+)$`) - -func ParseNetstat(output string, mode string, - iocs map[string]IOCountersStat) error { - lines := strings.Split(output, "\n") - - exists := make([]string, 0, len(lines)-1) - - columns := 6 - if mode == "ind" { - columns = 10 - } - for _, line := range lines { - values := strings.Fields(line) - if len(values) < 1 || values[0] == "Name" { - continue - } - if common.StringsHas(exists, values[0]) { - // skip if already get - continue - } - - if len(values) < columns { - continue - } - base := 1 - // sometimes Address is omitted - if len(values) < columns { - base = 0 - } - - parsed := make([]uint64, 0, 8) - var vv []string - if mode == "inb" { - vv = []string{ - values[base+3], // BytesRecv - values[base+4], // BytesSent - } - } else { - vv = []string{ - values[base+3], // Ipkts - values[base+4], // Ierrs - values[base+5], // Opkts - values[base+6], // Oerrs - values[base+8], // Drops - } - } - for _, target := range vv { - if target == "-" { - parsed = append(parsed, 0) - continue - } - - t, err := strconv.ParseUint(target, 10, 64) - if err != nil { - return err - } - parsed = append(parsed, t) - } - exists = append(exists, values[0]) - - n, present := iocs[values[0]] - if !present { - n = IOCountersStat{Name: values[0]} - } - if mode == "inb" { - n.BytesRecv = parsed[0] - n.BytesSent = parsed[1] - } else { - n.PacketsRecv = parsed[0] - n.Errin = parsed[1] - n.PacketsSent = parsed[2] - n.Errout = parsed[3] - n.Dropin = parsed[4] - n.Dropout = parsed[4] - } - - iocs[n.Name] = n - } - return nil -} - -func IOCounters(pernic bool) ([]IOCountersStat, error) { - return IOCountersWithContext(context.Background(), pernic) -} - -func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { - netstat, err := exec.LookPath("netstat") - if err != nil { - return nil, err - } - out, err := invoke.CommandWithContext(ctx, netstat, "-inb") - if err != nil { - return nil, err - } - out2, err := invoke.CommandWithContext(ctx, netstat, "-ind") - if err != nil { - return nil, err - } - iocs := make(map[string]IOCountersStat) - - lines := strings.Split(string(out), "\n") - ret := make([]IOCountersStat, 0, len(lines)-1) - - err = ParseNetstat(string(out), "inb", iocs) - if err != nil { - return nil, err - } - err = ParseNetstat(string(out2), "ind", iocs) - if err != nil { - return nil, err - } - - for _, ioc := range iocs { - ret = append(ret, ioc) - } - - if pernic == false { - return getIOCountersAll(ret) - } - - return ret, nil -} - -// NetIOCountersByFile is an method which is added just a compatibility for linux. -func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { - return IOCountersByFileWithContext(context.Background(), pernic, filename) -} - -func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { - return IOCounters(pernic) -} - -func FilterCounters() ([]FilterStat, error) { - return FilterCountersWithContext(context.Background()) -} - -func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, common.ErrNotImplementedError -} - -func ConntrackStats(percpu bool) ([]ConntrackStat, error) { - return ConntrackStatsWithContext(context.Background(), percpu) -} - -func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, common.ErrNotImplementedError -} - -// NetProtoCounters returns network statistics for the entire system -// If protocols is empty then all protocols are returned, otherwise -// just the protocols in the list are returned. -// Not Implemented for OpenBSD -func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { - return ProtoCountersWithContext(context.Background(), protocols) -} - -func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func parseNetstatLine(line string) (ConnectionStat, error) { - f := strings.Fields(line) - if len(f) < 5 { - return ConnectionStat{}, fmt.Errorf("wrong line,%s", line) - } - - var netType, netFamily uint32 - switch f[0] { - case "tcp": - netType = syscall.SOCK_STREAM - netFamily = syscall.AF_INET - case "udp": - netType = syscall.SOCK_DGRAM - netFamily = syscall.AF_INET - case "tcp6": - netType = syscall.SOCK_STREAM - netFamily = syscall.AF_INET6 - case "udp6": - netType = syscall.SOCK_DGRAM - netFamily = syscall.AF_INET6 - default: - return ConnectionStat{}, fmt.Errorf("unknown type, %s", f[0]) - } - - laddr, raddr, err := parseNetstatAddr(f[3], f[4], netFamily) - if err != nil { - return ConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s %s", f[3], f[4]) - } - - n := ConnectionStat{ - Fd: uint32(0), // not supported - Family: uint32(netFamily), - Type: uint32(netType), - Laddr: laddr, - Raddr: raddr, - Pid: int32(0), // not supported - } - if len(f) == 6 { - n.Status = f[5] - } - - return n, nil -} - -func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, raddr Addr, err error) { - parse := func(l string) (Addr, error) { - matches := portMatch.FindStringSubmatch(l) - if matches == nil { - return Addr{}, fmt.Errorf("wrong addr, %s", l) - } - host := matches[1] - port := matches[2] - if host == "*" { - switch family { - case syscall.AF_INET: - host = "0.0.0.0" - case syscall.AF_INET6: - host = "::" - default: - return Addr{}, fmt.Errorf("unknown family, %d", family) - } - } - lport, err := strconv.Atoi(port) - if err != nil { - return Addr{}, err - } - return Addr{IP: host, Port: uint32(lport)}, nil - } - - laddr, err = parse(local) - if remote != "*.*" { // remote addr exists - raddr, err = parse(remote) - if err != nil { - return laddr, raddr, err - } - } - - return laddr, raddr, err -} - -// Return a list of network connections opened. -func Connections(kind string) ([]ConnectionStat, error) { - return ConnectionsWithContext(context.Background(), kind) -} - -func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - var ret []ConnectionStat - - args := []string{"-na"} - switch strings.ToLower(kind) { - default: - fallthrough - case "": - fallthrough - case "all": - fallthrough - case "inet": - // nothing to add - case "inet4": - args = append(args, "-finet") - case "inet6": - args = append(args, "-finet6") - case "tcp": - args = append(args, "-ptcp") - case "tcp4": - args = append(args, "-ptcp", "-finet") - case "tcp6": - args = append(args, "-ptcp", "-finet6") - case "udp": - args = append(args, "-pudp") - case "udp4": - args = append(args, "-pudp", "-finet") - case "udp6": - args = append(args, "-pudp", "-finet6") - case "unix": - return ret, common.ErrNotImplementedError - } - - netstat, err := exec.LookPath("netstat") - if err != nil { - return nil, err - } - out, err := invoke.CommandWithContext(ctx, netstat, args...) - - if err != nil { - return nil, err - } - lines := strings.Split(string(out), "\n") - for _, line := range lines { - if !(strings.HasPrefix(line, "tcp") || strings.HasPrefix(line, "udp")) { - continue - } - n, err := parseNetstatLine(line) - if err != nil { - continue - } - - ret = append(ret, n) - } - - return ret, nil -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_unix.go b/vendor/github.com/shirou/gopsutil/net/net_unix.go deleted file mode 100644 index abe25c4ed..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_unix.go +++ /dev/null @@ -1,224 +0,0 @@ -// +build freebsd darwin - -package net - -import ( - "context" - "fmt" - "net" - "strconv" - "strings" - "syscall" - - "github.com/shirou/gopsutil/internal/common" -) - -// Return a list of network connections opened. -func Connections(kind string) ([]ConnectionStat, error) { - return ConnectionsWithContext(context.Background(), kind) -} - -func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsPid(kind, 0) -} - -// Return a list of network connections opened returning at most `max` -// connections for each running process. -func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { - return ConnectionsMaxWithContext(context.Background(), kind, max) -} - -func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} - -// Return a list of network connections opened by a process. -func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - var ret []ConnectionStat - - args := []string{"-i"} - switch strings.ToLower(kind) { - default: - fallthrough - case "": - fallthrough - case "all": - fallthrough - case "inet": - args = append(args, "tcp", "-i", "udp") - case "inet4": - args = append(args, "4") - case "inet6": - args = append(args, "6") - case "tcp": - args = append(args, "tcp") - case "tcp4": - args = append(args, "4tcp") - case "tcp6": - args = append(args, "6tcp") - case "udp": - args = append(args, "udp") - case "udp4": - args = append(args, "4udp") - case "udp6": - args = append(args, "6udp") - case "unix": - args = []string{"-U"} - } - - r, err := common.CallLsofWithContext(ctx, invoke, pid, args...) - if err != nil { - return nil, err - } - for _, rr := range r { - if strings.HasPrefix(rr, "COMMAND") { - continue - } - n, err := parseNetLine(rr) - if err != nil { - - continue - } - - ret = append(ret, n) - } - - return ret, nil -} - -var constMap = map[string]int{ - "unix": syscall.AF_UNIX, - "TCP": syscall.SOCK_STREAM, - "UDP": syscall.SOCK_DGRAM, - "IPv4": syscall.AF_INET, - "IPv6": syscall.AF_INET6, -} - -func parseNetLine(line string) (ConnectionStat, error) { - f := strings.Fields(line) - if len(f) < 8 { - return ConnectionStat{}, fmt.Errorf("wrong line,%s", line) - } - - if len(f) == 8 { - f = append(f, f[7]) - f[7] = "unix" - } - - pid, err := strconv.Atoi(f[1]) - if err != nil { - return ConnectionStat{}, err - } - fd, err := strconv.Atoi(strings.Trim(f[3], "u")) - if err != nil { - return ConnectionStat{}, fmt.Errorf("unknown fd, %s", f[3]) - } - netFamily, ok := constMap[f[4]] - if !ok { - return ConnectionStat{}, fmt.Errorf("unknown family, %s", f[4]) - } - netType, ok := constMap[f[7]] - if !ok { - return ConnectionStat{}, fmt.Errorf("unknown type, %s", f[7]) - } - - var laddr, raddr Addr - if f[7] == "unix" { - laddr.IP = f[8] - } else { - laddr, raddr, err = parseNetAddr(f[8]) - if err != nil { - return ConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s", f[8]) - } - } - - n := ConnectionStat{ - Fd: uint32(fd), - Family: uint32(netFamily), - Type: uint32(netType), - Laddr: laddr, - Raddr: raddr, - Pid: int32(pid), - } - if len(f) == 10 { - n.Status = strings.Trim(f[9], "()") - } - - return n, nil -} - -func parseNetAddr(line string) (laddr Addr, raddr Addr, err error) { - parse := func(l string) (Addr, error) { - host, port, err := net.SplitHostPort(l) - if err != nil { - return Addr{}, fmt.Errorf("wrong addr, %s", l) - } - lport, err := strconv.Atoi(port) - if err != nil { - return Addr{}, err - } - return Addr{IP: host, Port: uint32(lport)}, nil - } - - addrs := strings.Split(line, "->") - if len(addrs) == 0 { - return laddr, raddr, fmt.Errorf("wrong netaddr, %s", line) - } - laddr, err = parse(addrs[0]) - if len(addrs) == 2 { // remote addr exists - raddr, err = parse(addrs[1]) - if err != nil { - return laddr, raddr, err - } - } - - return laddr, raddr, err -} - -// Return up to `max` network connections opened by a process. -func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithContext(context.Background(), kind, pid, max) -} - -func ConnectionsPidMaxWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} - -// Return a list of network connections opened, omitting `Uids`. -// WithoutUids functions are reliant on implementation details. They may be altered to be an alias for Connections or be -// removed from the API in the future. -func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error) { - return ConnectionsWithoutUidsWithContext(context.Background(), kind) -} - -func ConnectionsWithoutUidsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsMaxWithoutUidsWithContext(ctx, kind, 0) -} - -func ConnectionsMaxWithoutUidsWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, 0, max) -} - -func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithoutUidsWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithoutUidsWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, 0) -} - -func ConnectionsPidMaxWithoutUids(kind string, pid int32, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(context.Background(), kind, pid, max) -} - -func ConnectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max) -} - -func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/net/net_windows.go b/vendor/github.com/shirou/gopsutil/net/net_windows.go deleted file mode 100644 index bdc928798..000000000 --- a/vendor/github.com/shirou/gopsutil/net/net_windows.go +++ /dev/null @@ -1,773 +0,0 @@ -// +build windows - -package net - -import ( - "context" - "fmt" - "net" - "os" - "syscall" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/windows" -) - -var ( - modiphlpapi = windows.NewLazySystemDLL("iphlpapi.dll") - procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable") - procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable") - procGetIfEntry2 = modiphlpapi.NewProc("GetIfEntry2") -) - -const ( - TCPTableBasicListener = iota - TCPTableBasicConnections - TCPTableBasicAll - TCPTableOwnerPIDListener - TCPTableOwnerPIDConnections - TCPTableOwnerPIDAll - TCPTableOwnerModuleListener - TCPTableOwnerModuleConnections - TCPTableOwnerModuleAll -) - -type netConnectionKindType struct { - family uint32 - sockType uint32 - filename string -} - -var kindTCP4 = netConnectionKindType{ - family: syscall.AF_INET, - sockType: syscall.SOCK_STREAM, - filename: "tcp", -} -var kindTCP6 = netConnectionKindType{ - family: syscall.AF_INET6, - sockType: syscall.SOCK_STREAM, - filename: "tcp6", -} -var kindUDP4 = netConnectionKindType{ - family: syscall.AF_INET, - sockType: syscall.SOCK_DGRAM, - filename: "udp", -} -var kindUDP6 = netConnectionKindType{ - family: syscall.AF_INET6, - sockType: syscall.SOCK_DGRAM, - filename: "udp6", -} - -var netConnectionKindMap = map[string][]netConnectionKindType{ - "all": {kindTCP4, kindTCP6, kindUDP4, kindUDP6}, - "tcp": {kindTCP4, kindTCP6}, - "tcp4": {kindTCP4}, - "tcp6": {kindTCP6}, - "udp": {kindUDP4, kindUDP6}, - "udp4": {kindUDP4}, - "udp6": {kindUDP6}, - "inet": {kindTCP4, kindTCP6, kindUDP4, kindUDP6}, - "inet4": {kindTCP4, kindUDP4}, - "inet6": {kindTCP6, kindUDP6}, -} - -// https://github.com/microsoft/ethr/blob/aecdaf923970e5a9b4c461b4e2e3963d781ad2cc/plt_windows.go#L114-L170 -type guid struct { - Data1 uint32 - Data2 uint16 - Data3 uint16 - Data4 [8]byte -} - -const ( - maxStringSize = 256 - maxPhysAddressLength = 32 - pad0for64_4for32 = 0 -) - -type mibIfRow2 struct { - InterfaceLuid uint64 - InterfaceIndex uint32 - InterfaceGuid guid - Alias [maxStringSize + 1]uint16 - Description [maxStringSize + 1]uint16 - PhysicalAddressLength uint32 - PhysicalAddress [maxPhysAddressLength]uint8 - PermanentPhysicalAddress [maxPhysAddressLength]uint8 - Mtu uint32 - Type uint32 - TunnelType uint32 - MediaType uint32 - PhysicalMediumType uint32 - AccessType uint32 - DirectionType uint32 - InterfaceAndOperStatusFlags uint32 - OperStatus uint32 - AdminStatus uint32 - MediaConnectState uint32 - NetworkGuid guid - ConnectionType uint32 - padding1 [pad0for64_4for32]byte - TransmitLinkSpeed uint64 - ReceiveLinkSpeed uint64 - InOctets uint64 - InUcastPkts uint64 - InNUcastPkts uint64 - InDiscards uint64 - InErrors uint64 - InUnknownProtos uint64 - InUcastOctets uint64 - InMulticastOctets uint64 - InBroadcastOctets uint64 - OutOctets uint64 - OutUcastPkts uint64 - OutNUcastPkts uint64 - OutDiscards uint64 - OutErrors uint64 - OutUcastOctets uint64 - OutMulticastOctets uint64 - OutBroadcastOctets uint64 - OutQLen uint64 -} - -func IOCounters(pernic bool) ([]IOCountersStat, error) { - return IOCountersWithContext(context.Background(), pernic) -} - -func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { - ifs, err := net.Interfaces() - if err != nil { - return nil, err - } - var counters []IOCountersStat - - err = procGetIfEntry2.Find() - if err == nil { // Vista+, uint64 values (issue#693) - for _, ifi := range ifs { - c := IOCountersStat{ - Name: ifi.Name, - } - - row := mibIfRow2{InterfaceIndex: uint32(ifi.Index)} - ret, _, err := procGetIfEntry2.Call(uintptr(unsafe.Pointer(&row))) - if ret != 0 { - return nil, os.NewSyscallError("GetIfEntry2", err) - } - c.BytesSent = uint64(row.OutOctets) - c.BytesRecv = uint64(row.InOctets) - c.PacketsSent = uint64(row.OutUcastPkts) - c.PacketsRecv = uint64(row.InUcastPkts) - c.Errin = uint64(row.InErrors) - c.Errout = uint64(row.OutErrors) - c.Dropin = uint64(row.InDiscards) - c.Dropout = uint64(row.OutDiscards) - - counters = append(counters, c) - } - } else { // WinXP fallback, uint32 values - for _, ifi := range ifs { - c := IOCountersStat{ - Name: ifi.Name, - } - - row := windows.MibIfRow{Index: uint32(ifi.Index)} - err = windows.GetIfEntry(&row) - if err != nil { - return nil, os.NewSyscallError("GetIfEntry", err) - } - c.BytesSent = uint64(row.OutOctets) - c.BytesRecv = uint64(row.InOctets) - c.PacketsSent = uint64(row.OutUcastPkts) - c.PacketsRecv = uint64(row.InUcastPkts) - c.Errin = uint64(row.InErrors) - c.Errout = uint64(row.OutErrors) - c.Dropin = uint64(row.InDiscards) - c.Dropout = uint64(row.OutDiscards) - - counters = append(counters, c) - } - } - - if !pernic { - return getIOCountersAll(counters) - } - return counters, nil -} - -// NetIOCountersByFile is an method which is added just a compatibility for linux. -func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { - return IOCountersByFileWithContext(context.Background(), pernic, filename) -} - -func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { - return IOCounters(pernic) -} - -// Return a list of network connections -// Available kind: -// reference to netConnectionKindMap -func Connections(kind string) ([]ConnectionStat, error) { - return ConnectionsWithContext(context.Background(), kind) -} - -func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsPidWithContext(ctx, kind, 0) -} - -// ConnectionsPid Return a list of network connections opened by a process -func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - tmap, ok := netConnectionKindMap[kind] - if !ok { - return nil, fmt.Errorf("invalid kind, %s", kind) - } - return getProcInet(tmap, pid) -} - -func getProcInet(kinds []netConnectionKindType, pid int32) ([]ConnectionStat, error) { - stats := make([]ConnectionStat, 0) - - for _, kind := range kinds { - s, err := getNetStatWithKind(kind) - if err != nil { - continue - } - - if pid == 0 { - stats = append(stats, s...) - } else { - for _, ns := range s { - if ns.Pid != pid { - continue - } - stats = append(stats, ns) - } - } - } - - return stats, nil -} - -func getNetStatWithKind(kindType netConnectionKindType) ([]ConnectionStat, error) { - if kindType.filename == "" { - return nil, fmt.Errorf("kind filename must be required") - } - - switch kindType.filename { - case kindTCP4.filename: - return getTCPConnections(kindTCP4.family) - case kindTCP6.filename: - return getTCPConnections(kindTCP6.family) - case kindUDP4.filename: - return getUDPConnections(kindUDP4.family) - case kindUDP6.filename: - return getUDPConnections(kindUDP6.family) - } - - return nil, fmt.Errorf("invalid kind filename, %s", kindType.filename) -} - -// Return a list of network connections opened returning at most `max` -// connections for each running process. -func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { - return ConnectionsMaxWithContext(context.Background(), kind, max) -} - -func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} - -// Return a list of network connections opened, omitting `Uids`. -// WithoutUids functions are reliant on implementation details. They may be altered to be an alias for Connections or be -// removed from the API in the future. -func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error) { - return ConnectionsWithoutUidsWithContext(context.Background(), kind) -} - -func ConnectionsWithoutUidsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsMaxWithoutUidsWithContext(ctx, kind, 0) -} - -func ConnectionsMaxWithoutUidsWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, 0, max) -} - -func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidWithoutUidsWithContext(context.Background(), kind, pid) -} - -func ConnectionsPidWithoutUidsWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, 0) -} - -func ConnectionsPidMaxWithoutUids(kind string, pid int32, max int) ([]ConnectionStat, error) { - return ConnectionsPidMaxWithoutUidsWithContext(context.Background(), kind, pid, max) -} - -func ConnectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max) -} - -func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) { - return []ConnectionStat{}, common.ErrNotImplementedError -} - -func FilterCounters() ([]FilterStat, error) { - return FilterCountersWithContext(context.Background()) -} - -func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, common.ErrNotImplementedError -} - -func ConntrackStats(percpu bool) ([]ConntrackStat, error) { - return ConntrackStatsWithContext(context.Background(), percpu) -} - -func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, common.ErrNotImplementedError -} - - -// NetProtoCounters returns network statistics for the entire system -// If protocols is empty then all protocols are returned, otherwise -// just the protocols in the list are returned. -// Not Implemented for Windows -func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { - return ProtoCountersWithContext(context.Background(), protocols) -} - -func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func getTableUintptr(family uint32, buf []byte) uintptr { - var ( - pmibTCPTable pmibTCPTableOwnerPidAll - pmibTCP6Table pmibTCP6TableOwnerPidAll - - p uintptr - ) - switch family { - case kindTCP4.family: - if len(buf) > 0 { - pmibTCPTable = (*mibTCPTableOwnerPid)(unsafe.Pointer(&buf[0])) - p = uintptr(unsafe.Pointer(pmibTCPTable)) - } else { - p = uintptr(unsafe.Pointer(pmibTCPTable)) - } - case kindTCP6.family: - if len(buf) > 0 { - pmibTCP6Table = (*mibTCP6TableOwnerPid)(unsafe.Pointer(&buf[0])) - p = uintptr(unsafe.Pointer(pmibTCP6Table)) - } else { - p = uintptr(unsafe.Pointer(pmibTCP6Table)) - } - } - return p -} - -func getTableInfo(filename string, table interface{}) (index, step, length int) { - switch filename { - case kindTCP4.filename: - index = int(unsafe.Sizeof(table.(pmibTCPTableOwnerPidAll).DwNumEntries)) - step = int(unsafe.Sizeof(table.(pmibTCPTableOwnerPidAll).Table)) - length = int(table.(pmibTCPTableOwnerPidAll).DwNumEntries) - case kindTCP6.filename: - index = int(unsafe.Sizeof(table.(pmibTCP6TableOwnerPidAll).DwNumEntries)) - step = int(unsafe.Sizeof(table.(pmibTCP6TableOwnerPidAll).Table)) - length = int(table.(pmibTCP6TableOwnerPidAll).DwNumEntries) - case kindUDP4.filename: - index = int(unsafe.Sizeof(table.(pmibUDPTableOwnerPid).DwNumEntries)) - step = int(unsafe.Sizeof(table.(pmibUDPTableOwnerPid).Table)) - length = int(table.(pmibUDPTableOwnerPid).DwNumEntries) - case kindUDP6.filename: - index = int(unsafe.Sizeof(table.(pmibUDP6TableOwnerPid).DwNumEntries)) - step = int(unsafe.Sizeof(table.(pmibUDP6TableOwnerPid).Table)) - length = int(table.(pmibUDP6TableOwnerPid).DwNumEntries) - } - - return -} - -func getTCPConnections(family uint32) ([]ConnectionStat, error) { - var ( - p uintptr - buf []byte - size uint32 - - pmibTCPTable pmibTCPTableOwnerPidAll - pmibTCP6Table pmibTCP6TableOwnerPidAll - ) - - if family == 0 { - return nil, fmt.Errorf("faimly must be required") - } - - for { - switch family { - case kindTCP4.family: - if len(buf) > 0 { - pmibTCPTable = (*mibTCPTableOwnerPid)(unsafe.Pointer(&buf[0])) - p = uintptr(unsafe.Pointer(pmibTCPTable)) - } else { - p = uintptr(unsafe.Pointer(pmibTCPTable)) - } - case kindTCP6.family: - if len(buf) > 0 { - pmibTCP6Table = (*mibTCP6TableOwnerPid)(unsafe.Pointer(&buf[0])) - p = uintptr(unsafe.Pointer(pmibTCP6Table)) - } else { - p = uintptr(unsafe.Pointer(pmibTCP6Table)) - } - } - - err := getExtendedTcpTable(p, - &size, - true, - family, - tcpTableOwnerPidAll, - 0) - if err == nil { - break - } - if err != windows.ERROR_INSUFFICIENT_BUFFER { - return nil, err - } - buf = make([]byte, size) - } - - var ( - index, step int - length int - ) - - stats := make([]ConnectionStat, 0) - switch family { - case kindTCP4.family: - index, step, length = getTableInfo(kindTCP4.filename, pmibTCPTable) - case kindTCP6.family: - index, step, length = getTableInfo(kindTCP6.filename, pmibTCP6Table) - } - - if length == 0 { - return nil, nil - } - - for i := 0; i < length; i++ { - switch family { - case kindTCP4.family: - mibs := (*mibTCPRowOwnerPid)(unsafe.Pointer(&buf[index])) - ns := mibs.convertToConnectionStat() - stats = append(stats, ns) - case kindTCP6.family: - mibs := (*mibTCP6RowOwnerPid)(unsafe.Pointer(&buf[index])) - ns := mibs.convertToConnectionStat() - stats = append(stats, ns) - } - - index += step - } - return stats, nil -} - -func getUDPConnections(family uint32) ([]ConnectionStat, error) { - var ( - p uintptr - buf []byte - size uint32 - - pmibUDPTable pmibUDPTableOwnerPid - pmibUDP6Table pmibUDP6TableOwnerPid - ) - - if family == 0 { - return nil, fmt.Errorf("faimly must be required") - } - - for { - switch family { - case kindUDP4.family: - if len(buf) > 0 { - pmibUDPTable = (*mibUDPTableOwnerPid)(unsafe.Pointer(&buf[0])) - p = uintptr(unsafe.Pointer(pmibUDPTable)) - } else { - p = uintptr(unsafe.Pointer(pmibUDPTable)) - } - case kindUDP6.family: - if len(buf) > 0 { - pmibUDP6Table = (*mibUDP6TableOwnerPid)(unsafe.Pointer(&buf[0])) - p = uintptr(unsafe.Pointer(pmibUDP6Table)) - } else { - p = uintptr(unsafe.Pointer(pmibUDP6Table)) - } - } - - err := getExtendedUdpTable( - p, - &size, - true, - family, - udpTableOwnerPid, - 0, - ) - if err == nil { - break - } - if err != windows.ERROR_INSUFFICIENT_BUFFER { - return nil, err - } - buf = make([]byte, size) - } - - var ( - index, step, length int - ) - - stats := make([]ConnectionStat, 0) - switch family { - case kindUDP4.family: - index, step, length = getTableInfo(kindUDP4.filename, pmibUDPTable) - case kindUDP6.family: - index, step, length = getTableInfo(kindUDP6.filename, pmibUDP6Table) - } - - if length == 0 { - return nil, nil - } - - for i := 0; i < length; i++ { - switch family { - case kindUDP4.family: - mibs := (*mibUDPRowOwnerPid)(unsafe.Pointer(&buf[index])) - ns := mibs.convertToConnectionStat() - stats = append(stats, ns) - case kindUDP6.family: - mibs := (*mibUDP6RowOwnerPid)(unsafe.Pointer(&buf[index])) - ns := mibs.convertToConnectionStat() - stats = append(stats, ns) - } - - index += step - } - return stats, nil -} - -// tcpStatuses https://msdn.microsoft.com/en-us/library/windows/desktop/bb485761(v=vs.85).aspx -var tcpStatuses = map[mibTCPState]string{ - 1: "CLOSED", - 2: "LISTEN", - 3: "SYN_SENT", - 4: "SYN_RECEIVED", - 5: "ESTABLISHED", - 6: "FIN_WAIT_1", - 7: "FIN_WAIT_2", - 8: "CLOSE_WAIT", - 9: "CLOSING", - 10: "LAST_ACK", - 11: "TIME_WAIT", - 12: "DELETE", -} - -func getExtendedTcpTable(pTcpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass tcpTableClass, reserved uint32) (errcode error) { - r1, _, _ := syscall.Syscall6(procGetExtendedTCPTable.Addr(), 6, pTcpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved)) - if r1 != 0 { - errcode = syscall.Errno(r1) - } - return -} - -func getExtendedUdpTable(pUdpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass udpTableClass, reserved uint32) (errcode error) { - r1, _, _ := syscall.Syscall6(procGetExtendedUDPTable.Addr(), 6, pUdpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved)) - if r1 != 0 { - errcode = syscall.Errno(r1) - } - return -} - -func getUintptrFromBool(b bool) uintptr { - if b { - return 1 - } - return 0 -} - -const anySize = 1 - -// type MIB_TCP_STATE int32 -type mibTCPState int32 - -type tcpTableClass int32 - -const ( - tcpTableBasicListener tcpTableClass = iota - tcpTableBasicConnections - tcpTableBasicAll - tcpTableOwnerPidListener - tcpTableOwnerPidConnections - tcpTableOwnerPidAll - tcpTableOwnerModuleListener - tcpTableOwnerModuleConnections - tcpTableOwnerModuleAll -) - -type udpTableClass int32 - -const ( - udpTableBasic udpTableClass = iota - udpTableOwnerPid - udpTableOwnerModule -) - -// TCP - -type mibTCPRowOwnerPid struct { - DwState uint32 - DwLocalAddr uint32 - DwLocalPort uint32 - DwRemoteAddr uint32 - DwRemotePort uint32 - DwOwningPid uint32 -} - -func (m *mibTCPRowOwnerPid) convertToConnectionStat() ConnectionStat { - ns := ConnectionStat{ - Family: kindTCP4.family, - Type: kindTCP4.sockType, - Laddr: Addr{ - IP: parseIPv4HexString(m.DwLocalAddr), - Port: uint32(decodePort(m.DwLocalPort)), - }, - Raddr: Addr{ - IP: parseIPv4HexString(m.DwRemoteAddr), - Port: uint32(decodePort(m.DwRemotePort)), - }, - Pid: int32(m.DwOwningPid), - Status: tcpStatuses[mibTCPState(m.DwState)], - } - - return ns -} - -type mibTCPTableOwnerPid struct { - DwNumEntries uint32 - Table [anySize]mibTCPRowOwnerPid -} - -type mibTCP6RowOwnerPid struct { - UcLocalAddr [16]byte - DwLocalScopeId uint32 - DwLocalPort uint32 - UcRemoteAddr [16]byte - DwRemoteScopeId uint32 - DwRemotePort uint32 - DwState uint32 - DwOwningPid uint32 -} - -func (m *mibTCP6RowOwnerPid) convertToConnectionStat() ConnectionStat { - ns := ConnectionStat{ - Family: kindTCP6.family, - Type: kindTCP6.sockType, - Laddr: Addr{ - IP: parseIPv6HexString(m.UcLocalAddr), - Port: uint32(decodePort(m.DwLocalPort)), - }, - Raddr: Addr{ - IP: parseIPv6HexString(m.UcRemoteAddr), - Port: uint32(decodePort(m.DwRemotePort)), - }, - Pid: int32(m.DwOwningPid), - Status: tcpStatuses[mibTCPState(m.DwState)], - } - - return ns -} - -type mibTCP6TableOwnerPid struct { - DwNumEntries uint32 - Table [anySize]mibTCP6RowOwnerPid -} - -type pmibTCPTableOwnerPidAll *mibTCPTableOwnerPid -type pmibTCP6TableOwnerPidAll *mibTCP6TableOwnerPid - -// UDP - -type mibUDPRowOwnerPid struct { - DwLocalAddr uint32 - DwLocalPort uint32 - DwOwningPid uint32 -} - -func (m *mibUDPRowOwnerPid) convertToConnectionStat() ConnectionStat { - ns := ConnectionStat{ - Family: kindUDP4.family, - Type: kindUDP4.sockType, - Laddr: Addr{ - IP: parseIPv4HexString(m.DwLocalAddr), - Port: uint32(decodePort(m.DwLocalPort)), - }, - Pid: int32(m.DwOwningPid), - } - - return ns -} - -type mibUDPTableOwnerPid struct { - DwNumEntries uint32 - Table [anySize]mibUDPRowOwnerPid -} - -type mibUDP6RowOwnerPid struct { - UcLocalAddr [16]byte - DwLocalScopeId uint32 - DwLocalPort uint32 - DwOwningPid uint32 -} - -func (m *mibUDP6RowOwnerPid) convertToConnectionStat() ConnectionStat { - ns := ConnectionStat{ - Family: kindUDP6.family, - Type: kindUDP6.sockType, - Laddr: Addr{ - IP: parseIPv6HexString(m.UcLocalAddr), - Port: uint32(decodePort(m.DwLocalPort)), - }, - Pid: int32(m.DwOwningPid), - } - - return ns -} - -type mibUDP6TableOwnerPid struct { - DwNumEntries uint32 - Table [anySize]mibUDP6RowOwnerPid -} - -type pmibUDPTableOwnerPid *mibUDPTableOwnerPid -type pmibUDP6TableOwnerPid *mibUDP6TableOwnerPid - -func decodePort(port uint32) uint16 { - return syscall.Ntohs(uint16(port)) -} - -func parseIPv4HexString(addr uint32) string { - return fmt.Sprintf("%d.%d.%d.%d", addr&255, addr>>8&255, addr>>16&255, addr>>24&255) -} - -func parseIPv6HexString(addr [16]byte) string { - var ret [16]byte - for i := 0; i < 16; i++ { - ret[i] = uint8(addr[i]) - } - - // convert []byte to net.IP - ip := net.IP(ret[:]) - return ip.String() -} diff --git a/vendor/github.com/shirou/gopsutil/process/process.go b/vendor/github.com/shirou/gopsutil/process/process.go deleted file mode 100644 index fc1c5f4cb..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process.go +++ /dev/null @@ -1,549 +0,0 @@ -package process - -import ( - "context" - "encoding/json" - "errors" - "runtime" - "sort" - "sync" - "syscall" - "time" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/mem" - "github.com/shirou/gopsutil/net" -) - -var ( - invoke common.Invoker = common.Invoke{} - ErrorNoChildren = errors.New("process does not have children") - ErrorProcessNotRunning = errors.New("process does not exist") -) - -type Process struct { - Pid int32 `json:"pid"` - name string - status string - parent int32 - parentMutex sync.RWMutex // for windows ppid cache - numCtxSwitches *NumCtxSwitchesStat - uids []int32 - gids []int32 - groups []int32 - numThreads int32 - memInfo *MemoryInfoStat - sigInfo *SignalInfoStat - createTime int64 - - lastCPUTimes *cpu.TimesStat - lastCPUTime time.Time - - tgid int32 -} - -type OpenFilesStat struct { - Path string `json:"path"` - Fd uint64 `json:"fd"` -} - -type MemoryInfoStat struct { - RSS uint64 `json:"rss"` // bytes - VMS uint64 `json:"vms"` // bytes - HWM uint64 `json:"hwm"` // bytes - Data uint64 `json:"data"` // bytes - Stack uint64 `json:"stack"` // bytes - Locked uint64 `json:"locked"` // bytes - Swap uint64 `json:"swap"` // bytes -} - -type SignalInfoStat struct { - PendingProcess uint64 `json:"pending_process"` - PendingThread uint64 `json:"pending_thread"` - Blocked uint64 `json:"blocked"` - Ignored uint64 `json:"ignored"` - Caught uint64 `json:"caught"` -} - -type RlimitStat struct { - Resource int32 `json:"resource"` - Soft int32 `json:"soft"` //TODO too small. needs to be uint64 - Hard int32 `json:"hard"` //TODO too small. needs to be uint64 - Used uint64 `json:"used"` -} - -type IOCountersStat struct { - ReadCount uint64 `json:"readCount"` - WriteCount uint64 `json:"writeCount"` - ReadBytes uint64 `json:"readBytes"` - WriteBytes uint64 `json:"writeBytes"` -} - -type NumCtxSwitchesStat struct { - Voluntary int64 `json:"voluntary"` - Involuntary int64 `json:"involuntary"` -} - -type PageFaultsStat struct { - MinorFaults uint64 `json:"minorFaults"` - MajorFaults uint64 `json:"majorFaults"` - ChildMinorFaults uint64 `json:"childMinorFaults"` - ChildMajorFaults uint64 `json:"childMajorFaults"` -} - -// Resource limit constants are from /usr/include/x86_64-linux-gnu/bits/resource.h -// from libc6-dev package in Ubuntu 16.10 -const ( - RLIMIT_CPU int32 = 0 - RLIMIT_FSIZE int32 = 1 - RLIMIT_DATA int32 = 2 - RLIMIT_STACK int32 = 3 - RLIMIT_CORE int32 = 4 - RLIMIT_RSS int32 = 5 - RLIMIT_NPROC int32 = 6 - RLIMIT_NOFILE int32 = 7 - RLIMIT_MEMLOCK int32 = 8 - RLIMIT_AS int32 = 9 - RLIMIT_LOCKS int32 = 10 - RLIMIT_SIGPENDING int32 = 11 - RLIMIT_MSGQUEUE int32 = 12 - RLIMIT_NICE int32 = 13 - RLIMIT_RTPRIO int32 = 14 - RLIMIT_RTTIME int32 = 15 -) - -func (p Process) String() string { - s, _ := json.Marshal(p) - return string(s) -} - -func (o OpenFilesStat) String() string { - s, _ := json.Marshal(o) - return string(s) -} - -func (m MemoryInfoStat) String() string { - s, _ := json.Marshal(m) - return string(s) -} - -func (r RlimitStat) String() string { - s, _ := json.Marshal(r) - return string(s) -} - -func (i IOCountersStat) String() string { - s, _ := json.Marshal(i) - return string(s) -} - -func (p NumCtxSwitchesStat) String() string { - s, _ := json.Marshal(p) - return string(s) -} - -// Pids returns a slice of process ID list which are running now. -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { - pids, err := pidsWithContext(ctx) - sort.Slice(pids, func(i, j int) bool { return pids[i] < pids[j] }) - return pids, err -} - -// Processes returns a slice of pointers to Process structs for all -// currently running processes. -func Processes() ([]*Process, error) { - return ProcessesWithContext(context.Background()) -} - -// NewProcess creates a new Process instance, it only stores the pid and -// checks that the process exists. Other method on Process can be used -// to get more information about the process. An error will be returned -// if the process does not exist. -func NewProcess(pid int32) (*Process, error) { - return NewProcessWithContext(context.Background(), pid) -} - -func NewProcessWithContext(ctx context.Context, pid int32) (*Process, error) { - p := &Process{ - Pid: pid, - } - - exists, err := PidExistsWithContext(ctx, pid) - if err != nil { - return p, err - } - if !exists { - return p, ErrorProcessNotRunning - } - p.CreateTimeWithContext(ctx) - return p, nil -} - -func PidExists(pid int32) (bool, error) { - return PidExistsWithContext(context.Background(), pid) -} - -// Background returns true if the process is in background, false otherwise. -func (p *Process) Background() (bool, error) { - return p.BackgroundWithContext(context.Background()) -} - -func (p *Process) BackgroundWithContext(ctx context.Context) (bool, error) { - fg, err := p.ForegroundWithContext(ctx) - if err != nil { - return false, err - } - return !fg, err -} - -// If interval is 0, return difference from last call(non-blocking). -// If interval > 0, wait interval sec and return diffrence between start and end. -func (p *Process) Percent(interval time.Duration) (float64, error) { - return p.PercentWithContext(context.Background(), interval) -} - -func (p *Process) PercentWithContext(ctx context.Context, interval time.Duration) (float64, error) { - cpuTimes, err := p.TimesWithContext(ctx) - if err != nil { - return 0, err - } - now := time.Now() - - if interval > 0 { - p.lastCPUTimes = cpuTimes - p.lastCPUTime = now - if err := common.Sleep(ctx, interval); err != nil { - return 0, err - } - cpuTimes, err = p.TimesWithContext(ctx) - now = time.Now() - if err != nil { - return 0, err - } - } else { - if p.lastCPUTimes == nil { - // invoked first time - p.lastCPUTimes = cpuTimes - p.lastCPUTime = now - return 0, nil - } - } - - numcpu := runtime.NumCPU() - delta := (now.Sub(p.lastCPUTime).Seconds()) * float64(numcpu) - ret := calculatePercent(p.lastCPUTimes, cpuTimes, delta, numcpu) - p.lastCPUTimes = cpuTimes - p.lastCPUTime = now - return ret, nil -} - -// IsRunning returns whether the process is still running or not. -func (p *Process) IsRunning() (bool, error) { - return p.IsRunningWithContext(context.Background()) -} - -func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) { - createTime, err := p.CreateTimeWithContext(ctx) - if err != nil { - return false, err - } - p2, err := NewProcessWithContext(ctx, p.Pid) - if err == ErrorProcessNotRunning { - return false, nil - } - createTime2, err := p2.CreateTimeWithContext(ctx) - if err != nil { - return false, err - } - return createTime == createTime2, nil -} - -// CreateTime returns created time of the process in milliseconds since the epoch, in UTC. -func (p *Process) CreateTime() (int64, error) { - return p.CreateTimeWithContext(context.Background()) -} - -func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) { - if p.createTime != 0 { - return p.createTime, nil - } - createTime, err := p.createTimeWithContext(ctx) - p.createTime = createTime - return p.createTime, err -} - -func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64 { - if delta == 0 { - return 0 - } - delta_proc := t2.Total() - t1.Total() - overall_percent := ((delta_proc / delta) * 100) * float64(numcpu) - return overall_percent -} - -// MemoryPercent returns how many percent of the total RAM this process uses -func (p *Process) MemoryPercent() (float32, error) { - return p.MemoryPercentWithContext(context.Background()) -} - -func (p *Process) MemoryPercentWithContext(ctx context.Context) (float32, error) { - machineMemory, err := mem.VirtualMemoryWithContext(ctx) - if err != nil { - return 0, err - } - total := machineMemory.Total - - processMemory, err := p.MemoryInfoWithContext(ctx) - if err != nil { - return 0, err - } - used := processMemory.RSS - - return (100 * float32(used) / float32(total)), nil -} - -// CPU_Percent returns how many percent of the CPU time this process uses -func (p *Process) CPUPercent() (float64, error) { - return p.CPUPercentWithContext(context.Background()) -} - -func (p *Process) CPUPercentWithContext(ctx context.Context) (float64, error) { - crt_time, err := p.createTimeWithContext(ctx) - if err != nil { - return 0, err - } - - cput, err := p.TimesWithContext(ctx) - if err != nil { - return 0, err - } - - created := time.Unix(0, crt_time*int64(time.Millisecond)) - totalTime := time.Since(created).Seconds() - if totalTime <= 0 { - return 0, nil - } - - return 100 * cput.Total() / totalTime, nil -} - -// Groups returns all group IDs(include supplementary groups) of the process as a slice of the int -func (p *Process) Groups() ([]int32, error) { - return p.GroupsWithContext(context.Background()) -} - -// Ppid returns Parent Process ID of the process. -func (p *Process) Ppid() (int32, error) { - return p.PpidWithContext(context.Background()) -} - -// Name returns name of the process. -func (p *Process) Name() (string, error) { - return p.NameWithContext(context.Background()) -} - -// Exe returns executable path of the process. -func (p *Process) Exe() (string, error) { - return p.ExeWithContext(context.Background()) -} - -// Cmdline returns the command line arguments of the process as a string with -// each argument separated by 0x20 ascii character. -func (p *Process) Cmdline() (string, error) { - return p.CmdlineWithContext(context.Background()) -} - -// CmdlineSlice returns the command line arguments of the process as a slice with each -// element being an argument. -func (p *Process) CmdlineSlice() ([]string, error) { - return p.CmdlineSliceWithContext(context.Background()) -} - -// Cwd returns current working directory of the process. -func (p *Process) Cwd() (string, error) { - return p.CwdWithContext(context.Background()) -} - -// Parent returns parent Process of the process. -func (p *Process) Parent() (*Process, error) { - return p.ParentWithContext(context.Background()) -} - -// Status returns the process status. -// Return value could be one of these. -// R: Running S: Sleep T: Stop I: Idle -// Z: Zombie W: Wait L: Lock -// The character is same within all supported platforms. -func (p *Process) Status() (string, error) { - return p.StatusWithContext(context.Background()) -} - -// Foreground returns true if the process is in foreground, false otherwise. -func (p *Process) Foreground() (bool, error) { - return p.ForegroundWithContext(context.Background()) -} - -// Uids returns user ids of the process as a slice of the int -func (p *Process) Uids() ([]int32, error) { - return p.UidsWithContext(context.Background()) -} - -// Gids returns group ids of the process as a slice of the int -func (p *Process) Gids() ([]int32, error) { - return p.GidsWithContext(context.Background()) -} - -// Terminal returns a terminal which is associated with the process. -func (p *Process) Terminal() (string, error) { - return p.TerminalWithContext(context.Background()) -} - -// Nice returns a nice value (priority). -func (p *Process) Nice() (int32, error) { - return p.NiceWithContext(context.Background()) -} - -// IOnice returns process I/O nice value (priority). -func (p *Process) IOnice() (int32, error) { - return p.IOniceWithContext(context.Background()) -} - -// Rlimit returns Resource Limits. -func (p *Process) Rlimit() ([]RlimitStat, error) { - return p.RlimitWithContext(context.Background()) -} - -// RlimitUsage returns Resource Limits. -// If gatherUsed is true, the currently used value will be gathered and added -// to the resulting RlimitStat. -func (p *Process) RlimitUsage(gatherUsed bool) ([]RlimitStat, error) { - return p.RlimitUsageWithContext(context.Background(), gatherUsed) -} - -// IOCounters returns IO Counters. -func (p *Process) IOCounters() (*IOCountersStat, error) { - return p.IOCountersWithContext(context.Background()) -} - -// NumCtxSwitches returns the number of the context switches of the process. -func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) { - return p.NumCtxSwitchesWithContext(context.Background()) -} - -// NumFDs returns the number of File Descriptors used by the process. -func (p *Process) NumFDs() (int32, error) { - return p.NumFDsWithContext(context.Background()) -} - -// NumThreads returns the number of threads used by the process. -func (p *Process) NumThreads() (int32, error) { - return p.NumThreadsWithContext(context.Background()) -} - -func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) { - return p.ThreadsWithContext(context.Background()) -} - -// Times returns CPU times of the process. -func (p *Process) Times() (*cpu.TimesStat, error) { - return p.TimesWithContext(context.Background()) -} - -// CPUAffinity returns CPU affinity of the process. -func (p *Process) CPUAffinity() ([]int32, error) { - return p.CPUAffinityWithContext(context.Background()) -} - -// MemoryInfo returns generic process memory information, -// such as RSS and VMS. -func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { - return p.MemoryInfoWithContext(context.Background()) -} - -// MemoryInfoEx returns platform-specific process memory information. -func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { - return p.MemoryInfoExWithContext(context.Background()) -} - -// PageFaultsInfo returns the process's page fault counters. -func (p *Process) PageFaults() (*PageFaultsStat, error) { - return p.PageFaultsWithContext(context.Background()) -} - -// Children returns the children of the process represented as a slice -// of pointers to Process type. -func (p *Process) Children() ([]*Process, error) { - return p.ChildrenWithContext(context.Background()) -} - -// OpenFiles returns a slice of OpenFilesStat opend by the process. -// OpenFilesStat includes a file path and file descriptor. -func (p *Process) OpenFiles() ([]OpenFilesStat, error) { - return p.OpenFilesWithContext(context.Background()) -} - -// Connections returns a slice of net.ConnectionStat used by the process. -// This returns all kind of the connection. This means TCP, UDP or UNIX. -func (p *Process) Connections() ([]net.ConnectionStat, error) { - return p.ConnectionsWithContext(context.Background()) -} - -// Connections returns a slice of net.ConnectionStat used by the process at most `max`. -func (p *Process) ConnectionsMax(max int) ([]net.ConnectionStat, error) { - return p.ConnectionsMaxWithContext(context.Background(), max) -} - -// NetIOCounters returns NetIOCounters of the process. -func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) { - return p.NetIOCountersWithContext(context.Background(), pernic) -} - -// MemoryMaps get memory maps from /proc/(pid)/smaps -func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { - return p.MemoryMapsWithContext(context.Background(), grouped) -} - -// Tgid returns thread group id of the process. -func (p *Process) Tgid() (int32, error) { - return p.TgidWithContext(context.Background()) -} - -// SendSignal sends a unix.Signal to the process. -func (p *Process) SendSignal(sig syscall.Signal) error { - return p.SendSignalWithContext(context.Background(), sig) -} - -// Suspend sends SIGSTOP to the process. -func (p *Process) Suspend() error { - return p.SuspendWithContext(context.Background()) -} - -// Resume sends SIGCONT to the process. -func (p *Process) Resume() error { - return p.ResumeWithContext(context.Background()) -} - -// Terminate sends SIGTERM to the process. -func (p *Process) Terminate() error { - return p.TerminateWithContext(context.Background()) -} - -// Kill sends SIGKILL to the process. -func (p *Process) Kill() error { - return p.KillWithContext(context.Background()) -} - -// Username returns a username of the process. -func (p *Process) Username() (string, error) { - return p.UsernameWithContext(context.Background()) -} - -// Environ returns the environment variables of the process. -func (p *Process) Environ() ([]string, error) { - return p.EnvironWithContext(context.Background()) -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_bsd.go b/vendor/github.com/shirou/gopsutil/process/process_bsd.go deleted file mode 100644 index 545e44f31..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_bsd.go +++ /dev/null @@ -1,84 +0,0 @@ -// +build darwin freebsd openbsd - -package process - -import ( - "bytes" - "context" - "encoding/binary" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/net" -) - -type MemoryInfoExStat struct{} - -type MemoryMapsStat struct{} - -func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) CwdWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ThreadsWithContext(ctx context.Context) (map[int32]*cpu.TimesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) { - return nil, common.ErrNotImplementedError -} - -func parseKinfoProc(buf []byte) (KinfoProc, error) { - var k KinfoProc - br := bytes.NewReader(buf) - err := common.Read(br, binary.LittleEndian, &k) - return k, err -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_darwin.go b/vendor/github.com/shirou/gopsutil/process/process_darwin.go deleted file mode 100644 index 383e0994d..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_darwin.go +++ /dev/null @@ -1,485 +0,0 @@ -// +build darwin - -package process - -import ( - "context" - "fmt" - "os/exec" - "path/filepath" - "strconv" - "strings" - "time" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/net" - "github.com/tklauser/go-sysconf" - "golang.org/x/sys/unix" -) - -// copied from sys/sysctl.h -const ( - CTLKern = 1 // "high kernel": proc, limits - KernProc = 14 // struct: process entries - KernProcPID = 1 // by process id - KernProcProc = 8 // only return procs - KernProcAll = 0 // everything - KernProcPathname = 12 // path to executable -) - -var ClockTicks = 100 // default value - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClockTicks = int(clkTck) - } -} - -type _Ctype_struct___0 struct { - Pad uint64 -} - -func pidsWithContext(ctx context.Context) ([]int32, error) { - var ret []int32 - - pids, err := callPsWithContext(ctx, "pid", 0, false, false) - if err != nil { - return ret, err - } - - for _, pid := range pids { - v, err := strconv.Atoi(pid[0]) - if err != nil { - return ret, err - } - ret = append(ret, int32(v)) - } - - return ret, nil -} - -func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - r, err := callPsWithContext(ctx, "ppid", p.Pid, false, false) - if err != nil { - return 0, err - } - - v, err := strconv.Atoi(r[0][0]) - if err != nil { - return 0, err - } - - return int32(v), err -} - -func (p *Process) NameWithContext(ctx context.Context) (string, error) { - k, err := p.getKProc() - if err != nil { - return "", err - } - name := common.IntToString(k.Proc.P_comm[:]) - - if len(name) >= 15 { - cmdName, err := p.cmdNameWithContext(ctx) - if err != nil { - return "", err - } - if len(cmdName) > 0 { - extendedName := filepath.Base(cmdName[0]) - if strings.HasPrefix(extendedName, p.name) { - name = extendedName - } else { - name = cmdName[0] - } - } - } - - return name, nil -} - -func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { - r, err := callPsWithContext(ctx, "command", p.Pid, false, false) - if err != nil { - return "", err - } - return strings.Join(r[0], " "), err -} - -// cmdNameWithContext returns the command name (including spaces) without any arguments -func (p *Process) cmdNameWithContext(ctx context.Context) ([]string, error) { - r, err := callPsWithContext(ctx, "command", p.Pid, false, true) - if err != nil { - return nil, err - } - return r[0], err -} - -// CmdlineSliceWithContext returns the command line arguments of the process as a slice with each -// element being an argument. Because of current deficiencies in the way that the command -// line arguments are found, single arguments that have spaces in the will actually be -// reported as two separate items. In order to do something better CGO would be needed -// to use the native darwin functions. -func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { - r, err := callPsWithContext(ctx, "command", p.Pid, false, false) - if err != nil { - return nil, err - } - return r[0], err -} - -func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - r, err := callPsWithContext(ctx, "etime", p.Pid, false, false) - if err != nil { - return 0, err - } - - elapsedSegments := strings.Split(strings.Replace(r[0][0], "-", ":", 1), ":") - var elapsedDurations []time.Duration - for i := len(elapsedSegments) - 1; i >= 0; i-- { - p, err := strconv.ParseInt(elapsedSegments[i], 10, 0) - if err != nil { - return 0, err - } - elapsedDurations = append(elapsedDurations, time.Duration(p)) - } - - var elapsed = time.Duration(elapsedDurations[0]) * time.Second - if len(elapsedDurations) > 1 { - elapsed += time.Duration(elapsedDurations[1]) * time.Minute - } - if len(elapsedDurations) > 2 { - elapsed += time.Duration(elapsedDurations[2]) * time.Hour - } - if len(elapsedDurations) > 3 { - elapsed += time.Duration(elapsedDurations[3]) * time.Hour * 24 - } - - start := time.Now().Add(-elapsed) - return start.Unix() * 1000, nil -} - -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") - if err != nil { - return nil, err - } - for _, line := range out { - if len(line) >= 1 && line[0] == 'R' { - v, err := strconv.Atoi(line[1:]) - if err != nil { - return nil, err - } - return NewProcessWithContext(ctx, int32(v)) - } - } - return nil, fmt.Errorf("could not find parent line") -} - -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - r, err := callPsWithContext(ctx, "state", p.Pid, false, false) - if err != nil { - return "", err - } - - return r[0][0][0:1], err -} - -func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { - // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details - pid := p.Pid - ps, err := exec.LookPath("ps") - if err != nil { - return false, err - } - out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid))) - if err != nil { - return false, err - } - return strings.IndexByte(string(out), '+') != -1, nil -} - -func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - // See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html - userEffectiveUID := int32(k.Eproc.Ucred.UID) - - return []int32{userEffectiveUID}, nil -} - -func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - gids := make([]int32, 0, 3) - gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Ucred.Ngroups), int32(k.Eproc.Pcred.P_svgid)) - - return gids, nil -} - -func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError - // k, err := p.getKProc() - // if err != nil { - // return nil, err - // } - - // groups := make([]int32, k.Eproc.Ucred.Ngroups) - // for i := int16(0); i < k.Eproc.Ucred.Ngroups; i++ { - // groups[i] = int32(k.Eproc.Ucred.Groups[i]) - // } - - // return groups, nil -} - -func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError - /* - k, err := p.getKProc() - if err != nil { - return "", err - } - - ttyNr := uint64(k.Eproc.Tdev) - termmap, err := getTerminalMap() - if err != nil { - return "", err - } - - return termmap[ttyNr], nil - */ -} - -func (p *Process) NiceWithContext(ctx context.Context) (int32, error) { - k, err := p.getKProc() - if err != nil { - return 0, err - } - return int32(k.Proc.P_nice), nil -} - -func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - r, err := callPsWithContext(ctx, "utime,stime", p.Pid, true, false) - if err != nil { - return 0, err - } - return int32(len(r)), nil -} - -func convertCPUTimes(s string) (ret float64, err error) { - var t int - var _tmp string - if strings.Contains(s, ":") { - _t := strings.Split(s, ":") - switch len(_t) { - case 3: - hour, err := strconv.Atoi(_t[0]) - if err != nil { - return ret, err - } - t += hour * 60 * 60 * ClockTicks - - mins, err := strconv.Atoi(_t[1]) - if err != nil { - return ret, err - } - t += mins * 60 * ClockTicks - _tmp = _t[2] - case 2: - mins, err := strconv.Atoi(_t[0]) - if err != nil { - return ret, err - } - t += mins * 60 * ClockTicks - _tmp = _t[1] - case 1, 0: - _tmp = s - default: - return ret, fmt.Errorf("wrong cpu time string") - } - } else { - _tmp = s - } - - _t := strings.Split(_tmp, ".") - if err != nil { - return ret, err - } - h, err := strconv.Atoi(_t[0]) - t += h * ClockTicks - h, err = strconv.Atoi(_t[1]) - t += h - return float64(t) / float64(ClockTicks), nil -} - -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - r, err := callPsWithContext(ctx, "utime,stime", p.Pid, false, false) - - if err != nil { - return nil, err - } - - utime, err := convertCPUTimes(r[0][0]) - if err != nil { - return nil, err - } - stime, err := convertCPUTimes(r[0][1]) - if err != nil { - return nil, err - } - - ret := &cpu.TimesStat{ - CPU: "cpu", - User: utime, - System: stime, - } - return ret, nil -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - r, err := callPsWithContext(ctx, "rss,vsize,pagein", p.Pid, false, false) - if err != nil { - return nil, err - } - rss, err := strconv.Atoi(r[0][0]) - if err != nil { - return nil, err - } - vms, err := strconv.Atoi(r[0][1]) - if err != nil { - return nil, err - } - pagein, err := strconv.Atoi(r[0][2]) - if err != nil { - return nil, err - } - - ret := &MemoryInfoStat{ - RSS: uint64(rss) * 1024, - VMS: uint64(vms) * 1024, - Swap: uint64(pagein), - } - - return ret, nil -} - -func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { - pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid) - if err != nil { - return nil, err - } - ret := make([]*Process, 0, len(pids)) - for _, pid := range pids { - np, err := NewProcessWithContext(ctx, pid) - if err != nil { - return nil, err - } - ret = append(ret, np) - } - return ret, nil -} - -func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { - return net.ConnectionsPidWithContext(ctx, "all", p.Pid) -} - -func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) { - return net.ConnectionsPidMaxWithContext(ctx, "all", p.Pid, max) -} - -func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - out := []*Process{} - - pids, err := PidsWithContext(ctx) - if err != nil { - return out, err - } - - for _, pid := range pids { - p, err := NewProcessWithContext(ctx, pid) - if err != nil { - continue - } - out = append(out, p) - } - - return out, nil -} - -// Returns a proc as defined here: -// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html -func (p *Process) getKProc() (*KinfoProc, error) { - buf, err := unix.SysctlRaw("kern.proc.pid", int(p.Pid)) - if err != nil { - return nil, err - } - k, err := parseKinfoProc(buf) - if err != nil { - return nil, err - } - - return &k, nil -} - -// call ps command. -// Return value deletes Header line(you must not input wrong arg). -// And split by space. Caller have responsibility to manage. -// If passed arg pid is 0, get information from all process. -func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption bool, nameOption bool) ([][]string, error) { - bin, err := exec.LookPath("ps") - if err != nil { - return [][]string{}, err - } - - var cmd []string - if pid == 0 { // will get from all processes. - cmd = []string{"-ax", "-o", arg} - } else if threadOption { - cmd = []string{"-x", "-o", arg, "-M", "-p", strconv.Itoa(int(pid))} - } else { - cmd = []string{"-x", "-o", arg, "-p", strconv.Itoa(int(pid))} - } - - if nameOption { - cmd = append(cmd, "-c") - } - out, err := invoke.CommandWithContext(ctx, bin, cmd...) - if err != nil { - return [][]string{}, err - } - lines := strings.Split(string(out), "\n") - - var ret [][]string - for _, l := range lines[1:] { - - var lr []string - if nameOption { - lr = append(lr, l) - } else { - for _, r := range strings.Split(l, " ") { - if r == "" { - continue - } - lr = append(lr, strings.TrimSpace(r)) - } - } - - if len(lr) != 0 { - ret = append(ret, lr) - } - } - - return ret, nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_darwin_386.go b/vendor/github.com/shirou/gopsutil/process/process_darwin_386.go deleted file mode 100644 index f8e922385..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_darwin_386.go +++ /dev/null @@ -1,234 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_darwin.go - -package process - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int32 - Pad_cgo_0 [4]byte -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur uint64 - Max uint64 -} - -type UGid_t uint32 - -type KinfoProc struct { - Proc ExternProc - Eproc Eproc -} - -type Eproc struct { - Paddr *uint64 - Sess *Session - Pcred Upcred - Ucred Uucred - Pad_cgo_0 [4]byte - Vm Vmspace - Ppid int32 - Pgid int32 - Jobc int16 - Pad_cgo_1 [2]byte - Tdev int32 - Tpgid int32 - Pad_cgo_2 [4]byte - Tsess *Session - Wmesg [8]int8 - Xsize int32 - Xrssize int16 - Xccount int16 - Xswrss int16 - Pad_cgo_3 [2]byte - Flag int32 - Login [12]int8 - Spare [4]int32 - Pad_cgo_4 [4]byte -} - -type Proc struct{} - -type Session struct{} - -type ucred struct { - Link _Ctype_struct___0 - Ref uint64 - Posix Posix_cred - Label *Label - Audit Au_session -} - -type Uucred struct { - Ref int32 - UID uint32 - Ngroups int16 - Pad_cgo_0 [2]byte - Groups [16]uint32 -} - -type Upcred struct { - Pc_lock [72]int8 - Pc_ucred *ucred - P_ruid uint32 - P_svuid uint32 - P_rgid uint32 - P_svgid uint32 - P_refcnt int32 - Pad_cgo_0 [4]byte -} - -type Vmspace struct { - Dummy int32 - Pad_cgo_0 [4]byte - Dummy2 *int8 - Dummy3 [5]int32 - Pad_cgo_1 [4]byte - Dummy4 [3]*int8 -} - -type Sigacts struct{} - -type ExternProc struct { - P_un [16]byte - P_vmspace uint64 - P_sigacts uint64 - Pad_cgo_0 [3]byte - P_flag int32 - P_stat int8 - P_pid int32 - P_oppid int32 - P_dupfd int32 - Pad_cgo_1 [4]byte - User_stack uint64 - Exit_thread uint64 - P_debugger int32 - Sigwait int32 - P_estcpu uint32 - P_cpticks int32 - P_pctcpu uint32 - Pad_cgo_2 [4]byte - P_wchan uint64 - P_wmesg uint64 - P_swtime uint32 - P_slptime uint32 - P_realtimer Itimerval - P_rtime Timeval - P_uticks uint64 - P_sticks uint64 - P_iticks uint64 - P_traceflag int32 - Pad_cgo_3 [4]byte - P_tracep uint64 - P_siglist int32 - Pad_cgo_4 [4]byte - P_textvp uint64 - P_holdcnt int32 - P_sigmask uint32 - P_sigignore uint32 - P_sigcatch uint32 - P_priority uint8 - P_usrpri uint8 - P_nice int8 - P_comm [17]int8 - Pad_cgo_5 [4]byte - P_pgrp uint64 - P_addr uint64 - P_xstat uint16 - P_acflag uint16 - Pad_cgo_6 [4]byte - P_ru uint64 -} - -type Itimerval struct { - Interval Timeval - Value Timeval -} - -type Vnode struct{} - -type Pgrp struct{} - -type UserStruct struct{} - -type Au_session struct { - Aia_p *AuditinfoAddr - Mask AuMask -} - -type Posix_cred struct { - UID uint32 - Ruid uint32 - Svuid uint32 - Ngroups int16 - Pad_cgo_0 [2]byte - Groups [16]uint32 - Rgid uint32 - Svgid uint32 - Gmuid uint32 - Flags int32 -} - -type Label struct{} - -type AuditinfoAddr struct { - Auid uint32 - Mask AuMask - Termid AuTidAddr - Asid int32 - Flags uint64 -} -type AuMask struct { - Success uint32 - Failure uint32 -} -type AuTidAddr struct { - Port int32 - Type uint32 - Addr [4]uint32 -} - -type UcredQueue struct { - Next *ucred - Prev **ucred -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_darwin_amd64.go b/vendor/github.com/shirou/gopsutil/process/process_darwin_amd64.go deleted file mode 100644 index f8e922385..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_darwin_amd64.go +++ /dev/null @@ -1,234 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_darwin.go - -package process - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int32 - Pad_cgo_0 [4]byte -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur uint64 - Max uint64 -} - -type UGid_t uint32 - -type KinfoProc struct { - Proc ExternProc - Eproc Eproc -} - -type Eproc struct { - Paddr *uint64 - Sess *Session - Pcred Upcred - Ucred Uucred - Pad_cgo_0 [4]byte - Vm Vmspace - Ppid int32 - Pgid int32 - Jobc int16 - Pad_cgo_1 [2]byte - Tdev int32 - Tpgid int32 - Pad_cgo_2 [4]byte - Tsess *Session - Wmesg [8]int8 - Xsize int32 - Xrssize int16 - Xccount int16 - Xswrss int16 - Pad_cgo_3 [2]byte - Flag int32 - Login [12]int8 - Spare [4]int32 - Pad_cgo_4 [4]byte -} - -type Proc struct{} - -type Session struct{} - -type ucred struct { - Link _Ctype_struct___0 - Ref uint64 - Posix Posix_cred - Label *Label - Audit Au_session -} - -type Uucred struct { - Ref int32 - UID uint32 - Ngroups int16 - Pad_cgo_0 [2]byte - Groups [16]uint32 -} - -type Upcred struct { - Pc_lock [72]int8 - Pc_ucred *ucred - P_ruid uint32 - P_svuid uint32 - P_rgid uint32 - P_svgid uint32 - P_refcnt int32 - Pad_cgo_0 [4]byte -} - -type Vmspace struct { - Dummy int32 - Pad_cgo_0 [4]byte - Dummy2 *int8 - Dummy3 [5]int32 - Pad_cgo_1 [4]byte - Dummy4 [3]*int8 -} - -type Sigacts struct{} - -type ExternProc struct { - P_un [16]byte - P_vmspace uint64 - P_sigacts uint64 - Pad_cgo_0 [3]byte - P_flag int32 - P_stat int8 - P_pid int32 - P_oppid int32 - P_dupfd int32 - Pad_cgo_1 [4]byte - User_stack uint64 - Exit_thread uint64 - P_debugger int32 - Sigwait int32 - P_estcpu uint32 - P_cpticks int32 - P_pctcpu uint32 - Pad_cgo_2 [4]byte - P_wchan uint64 - P_wmesg uint64 - P_swtime uint32 - P_slptime uint32 - P_realtimer Itimerval - P_rtime Timeval - P_uticks uint64 - P_sticks uint64 - P_iticks uint64 - P_traceflag int32 - Pad_cgo_3 [4]byte - P_tracep uint64 - P_siglist int32 - Pad_cgo_4 [4]byte - P_textvp uint64 - P_holdcnt int32 - P_sigmask uint32 - P_sigignore uint32 - P_sigcatch uint32 - P_priority uint8 - P_usrpri uint8 - P_nice int8 - P_comm [17]int8 - Pad_cgo_5 [4]byte - P_pgrp uint64 - P_addr uint64 - P_xstat uint16 - P_acflag uint16 - Pad_cgo_6 [4]byte - P_ru uint64 -} - -type Itimerval struct { - Interval Timeval - Value Timeval -} - -type Vnode struct{} - -type Pgrp struct{} - -type UserStruct struct{} - -type Au_session struct { - Aia_p *AuditinfoAddr - Mask AuMask -} - -type Posix_cred struct { - UID uint32 - Ruid uint32 - Svuid uint32 - Ngroups int16 - Pad_cgo_0 [2]byte - Groups [16]uint32 - Rgid uint32 - Svgid uint32 - Gmuid uint32 - Flags int32 -} - -type Label struct{} - -type AuditinfoAddr struct { - Auid uint32 - Mask AuMask - Termid AuTidAddr - Asid int32 - Flags uint64 -} -type AuMask struct { - Success uint32 - Failure uint32 -} -type AuTidAddr struct { - Port int32 - Type uint32 - Addr [4]uint32 -} - -type UcredQueue struct { - Next *ucred - Prev **ucred -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_darwin_arm64.go b/vendor/github.com/shirou/gopsutil/process/process_darwin_arm64.go deleted file mode 100644 index 92bd4259b..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_darwin_arm64.go +++ /dev/null @@ -1,212 +0,0 @@ -// +build darwin -// +build arm64 -// Code generated by cmd/cgo -godefs; DO NOT EDIT. -// cgo -godefs process/types_darwin.go - -package process - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int32 - Pad_cgo_0 [4]byte -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur uint64 - Max uint64 -} - -type UGid_t uint32 - -type KinfoProc struct { - Proc ExternProc - Eproc Eproc -} - -type Eproc struct { - Paddr *Proc - Sess *Session - Pcred Upcred - Ucred Uucred - Vm Vmspace - Ppid int32 - Pgid int32 - Jobc int16 - Tdev int32 - Tpgid int32 - Tsess *Session - Wmesg [8]int8 - Xsize int32 - Xrssize int16 - Xccount int16 - Xswrss int16 - Flag int32 - Login [12]int8 - Spare [4]int32 - Pad_cgo_0 [4]byte -} - -type Proc struct{} - -type Session struct{} - -type ucred struct{} - -type Uucred struct { - Ref int32 - UID uint32 - Ngroups int16 - Groups [16]uint32 -} - -type Upcred struct { - Pc_lock [72]int8 - Pc_ucred *ucred - P_ruid uint32 - P_svuid uint32 - P_rgid uint32 - P_svgid uint32 - P_refcnt int32 - Pad_cgo_0 [4]byte -} - -type Vmspace struct { - Dummy int32 - Dummy2 *int8 - Dummy3 [5]int32 - Dummy4 [3]*int8 -} - -type Sigacts struct{} - -type ExternProc struct { - P_un [16]byte - P_vmspace uint64 - P_sigacts uint64 - Pad_cgo_0 [3]byte - P_flag int32 - P_stat int8 - P_pid int32 - P_oppid int32 - P_dupfd int32 - Pad_cgo_1 [4]byte - User_stack uint64 - Exit_thread uint64 - P_debugger int32 - Sigwait int32 - P_estcpu uint32 - P_cpticks int32 - P_pctcpu uint32 - Pad_cgo_2 [4]byte - P_wchan uint64 - P_wmesg uint64 - P_swtime uint32 - P_slptime uint32 - P_realtimer Itimerval - P_rtime Timeval - P_uticks uint64 - P_sticks uint64 - P_iticks uint64 - P_traceflag int32 - Pad_cgo_3 [4]byte - P_tracep uint64 - P_siglist int32 - Pad_cgo_4 [4]byte - P_textvp uint64 - P_holdcnt int32 - P_sigmask uint32 - P_sigignore uint32 - P_sigcatch uint32 - P_priority uint8 - P_usrpri uint8 - P_nice int8 - P_comm [17]int8 - Pad_cgo_5 [4]byte - P_pgrp uint64 - P_addr uint64 - P_xstat uint16 - P_acflag uint16 - Pad_cgo_6 [4]byte - P_ru uint64 -} - -type Itimerval struct { - Interval Timeval - Value Timeval -} - -type Vnode struct{} - -type Pgrp struct{} - -type UserStruct struct{} - -type Au_session struct { - Aia_p *AuditinfoAddr - Mask AuMask -} - -type Posix_cred struct{} - -type Label struct{} - -type AuditinfoAddr struct { - Auid uint32 - Mask AuMask - Termid AuTidAddr - Asid int32 - Flags uint64 -} -type AuMask struct { - Success uint32 - Failure uint32 -} -type AuTidAddr struct { - Port int32 - Type uint32 - Addr [4]uint32 -} - -type UcredQueue struct { - Next *ucred - Prev **ucred -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_darwin_cgo.go b/vendor/github.com/shirou/gopsutil/process/process_darwin_cgo.go deleted file mode 100644 index a80817755..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_darwin_cgo.go +++ /dev/null @@ -1,30 +0,0 @@ -// +build darwin -// +build cgo - -package process - -// #include -// #include -import "C" -import ( - "context" - "fmt" - "unsafe" -) - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - var c C.char // need a var for unsafe.Sizeof need a var - const bufsize = C.PROC_PIDPATHINFO_MAXSIZE * unsafe.Sizeof(c) - buffer := (*C.char)(C.malloc(C.size_t(bufsize))) - defer C.free(unsafe.Pointer(buffer)) - - ret, err := C.proc_pidpath(C.int(p.Pid), unsafe.Pointer(buffer), C.uint32_t(bufsize)) - if err != nil { - return "", err - } - if ret <= 0 { - return "", fmt.Errorf("unknown error: proc_pidpath returned %d", ret) - } - - return C.GoString(buffer), nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_darwin_nocgo.go b/vendor/github.com/shirou/gopsutil/process/process_darwin_nocgo.go deleted file mode 100644 index 3583e1987..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_darwin_nocgo.go +++ /dev/null @@ -1,34 +0,0 @@ -// +build darwin -// +build !cgo - -package process - -import ( - "context" - "fmt" - "os/exec" - "strconv" - "strings" -) - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - lsof_bin, err := exec.LookPath("lsof") - if err != nil { - return "", err - } - out, err := invoke.CommandWithContext(ctx, lsof_bin, "-p", strconv.Itoa(int(p.Pid)), "-Fpfn") - if err != nil { - return "", fmt.Errorf("bad call to lsof: %s", err) - } - txtFound := 0 - lines := strings.Split(string(out), "\n") - for i := 1; i < len(lines); i++ { - if lines[i] == "ftxt" { - txtFound++ - if txtFound == 2 { - return lines[i-1][1:], nil - } - } - } - return "", fmt.Errorf("missing txt data returned by lsof") -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_fallback.go b/vendor/github.com/shirou/gopsutil/process/process_fallback.go deleted file mode 100644 index 0d54900b1..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_fallback.go +++ /dev/null @@ -1,209 +0,0 @@ -// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris - -package process - -import ( - "context" - "syscall" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/net" -) - -type MemoryMapsStat struct { - Path string `json:"path"` - Rss uint64 `json:"rss"` - Size uint64 `json:"size"` - Pss uint64 `json:"pss"` - SharedClean uint64 `json:"sharedClean"` - SharedDirty uint64 `json:"sharedDirty"` - PrivateClean uint64 `json:"privateClean"` - PrivateDirty uint64 `json:"privateDirty"` - Referenced uint64 `json:"referenced"` - Anonymous uint64 `json:"anonymous"` - Swap uint64 `json:"swap"` -} - -type MemoryInfoExStat struct { -} - -func pidsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - return nil, common.ErrNotImplementedError -} - -func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) { - return false, common.ErrNotImplementedError -} - -func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) NameWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) CwdWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { - return false, common.ErrNotImplementedError -} - -func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) NiceWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) ThreadsWithContext(ctx context.Context) (map[int32]*cpu.TimesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) SendSignalWithContext(ctx context.Context, sig syscall.Signal) error { - return common.ErrNotImplementedError -} - -func (p *Process) SuspendWithContext(ctx context.Context) error { - return common.ErrNotImplementedError -} - -func (p *Process) ResumeWithContext(ctx context.Context) error { - return common.ErrNotImplementedError -} - -func (p *Process) TerminateWithContext(ctx context.Context) error { - return common.ErrNotImplementedError -} - -func (p *Process) KillWithContext(ctx context.Context) error { - return common.ErrNotImplementedError -} - -func (p *Process) UsernameWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) { - return nil, common.ErrNotImplementedError -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_freebsd.go b/vendor/github.com/shirou/gopsutil/process/process_freebsd.go deleted file mode 100644 index 0666e7f42..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_freebsd.go +++ /dev/null @@ -1,338 +0,0 @@ -// +build freebsd - -package process - -import ( - "bytes" - "context" - "os/exec" - "path/filepath" - "strconv" - "strings" - - cpu "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - net "github.com/shirou/gopsutil/net" - "golang.org/x/sys/unix" -) - -func pidsWithContext(ctx context.Context) ([]int32, error) { - var ret []int32 - procs, err := ProcessesWithContext(ctx) - if err != nil { - return ret, nil - } - - for _, p := range procs { - ret = append(ret, p.Pid) - } - - return ret, nil -} - -func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - k, err := p.getKProc() - if err != nil { - return 0, err - } - - return k.Ppid, nil -} - -func (p *Process) NameWithContext(ctx context.Context) (string, error) { - k, err := p.getKProc() - if err != nil { - return "", err - } - name := common.IntToString(k.Comm[:]) - - if len(name) >= 15 { - cmdlineSlice, err := p.CmdlineSliceWithContext(ctx) - if err != nil { - return "", err - } - if len(cmdlineSlice) > 0 { - extendedName := filepath.Base(cmdlineSlice[0]) - if strings.HasPrefix(extendedName, p.name) { - name = extendedName - } else { - name = cmdlineSlice[0] - } - } - } - - return name, nil -} - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { - mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid} - buf, _, err := common.CallSyscall(mib) - if err != nil { - return "", err - } - ret := strings.FieldsFunc(string(buf), func(r rune) bool { - if r == '\u0000' { - return true - } - return false - }) - - return strings.Join(ret, " "), nil -} - -func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { - mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid} - buf, _, err := common.CallSyscall(mib) - if err != nil { - return nil, err - } - if len(buf) == 0 { - return nil, nil - } - if buf[len(buf)-1] == 0 { - buf = buf[:len(buf)-1] - } - parts := bytes.Split(buf, []byte{0}) - var strParts []string - for _, p := range parts { - strParts = append(strParts, string(p)) - } - - return strParts, nil -} - -func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - k, err := p.getKProc() - if err != nil { - return "", err - } - var s string - switch k.Stat { - case SIDL: - s = "I" - case SRUN: - s = "R" - case SSLEEP: - s = "S" - case SSTOP: - s = "T" - case SZOMB: - s = "Z" - case SWAIT: - s = "W" - case SLOCK: - s = "L" - } - - return s, nil -} - -func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { - // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details - pid := p.Pid - ps, err := exec.LookPath("ps") - if err != nil { - return false, err - } - out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid))) - if err != nil { - return false, err - } - return strings.IndexByte(string(out), '+') != -1, nil -} - -func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - uids := make([]int32, 0, 3) - - uids = append(uids, int32(k.Ruid), int32(k.Uid), int32(k.Svuid)) - - return uids, nil -} - -func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - gids := make([]int32, 0, 3) - gids = append(gids, int32(k.Rgid), int32(k.Ngroups), int32(k.Svgid)) - - return gids, nil -} - -func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - groups := make([]int32, k.Ngroups) - for i := int16(0); i < k.Ngroups; i++ { - groups[i] = int32(k.Groups[i]) - } - - return groups, nil -} - -func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { - k, err := p.getKProc() - if err != nil { - return "", err - } - - ttyNr := uint64(k.Tdev) - - termmap, err := getTerminalMap() - if err != nil { - return "", err - } - - return termmap[ttyNr], nil -} - -func (p *Process) NiceWithContext(ctx context.Context) (int32, error) { - k, err := p.getKProc() - if err != nil { - return 0, err - } - return int32(k.Nice), nil -} - -func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - return &IOCountersStat{ - ReadCount: uint64(k.Rusage.Inblock), - WriteCount: uint64(k.Rusage.Oublock), - }, nil -} - -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - k, err := p.getKProc() - if err != nil { - return 0, err - } - - return k.Numthreads, nil -} - -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - return &cpu.TimesStat{ - CPU: "cpu", - User: float64(k.Rusage.Utime.Sec) + float64(k.Rusage.Utime.Usec)/1000000, - System: float64(k.Rusage.Stime.Sec) + float64(k.Rusage.Stime.Usec)/1000000, - }, nil -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - v, err := unix.Sysctl("vm.stats.vm.v_page_size") - if err != nil { - return nil, err - } - pageSize := common.LittleEndian.Uint16([]byte(v)) - - return &MemoryInfoStat{ - RSS: uint64(k.Rssize) * uint64(pageSize), - VMS: uint64(k.Size), - }, nil -} - -func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { - pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid) - if err != nil { - return nil, err - } - ret := make([]*Process, 0, len(pids)) - for _, pid := range pids { - np, err := NewProcessWithContext(ctx, pid) - if err != nil { - return nil, err - } - ret = append(ret, np) - } - return ret, nil -} - -func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - results := []*Process{} - - mib := []int32{CTLKern, KernProc, KernProcProc, 0} - buf, length, err := common.CallSyscall(mib) - if err != nil { - return results, err - } - - // get kinfo_proc size - count := int(length / uint64(sizeOfKinfoProc)) - - // parse buf to procs - for i := 0; i < count; i++ { - b := buf[i*sizeOfKinfoProc : (i+1)*sizeOfKinfoProc] - k, err := parseKinfoProc(b) - if err != nil { - continue - } - p, err := NewProcessWithContext(ctx, int32(k.Pid)) - if err != nil { - continue - } - - results = append(results, p) - } - - return results, nil -} - -func (p *Process) getKProc() (*KinfoProc, error) { - mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid} - - buf, length, err := common.CallSyscall(mib) - if err != nil { - return nil, err - } - if length != sizeOfKinfoProc { - return nil, err - } - - k, err := parseKinfoProc(buf) - if err != nil { - return nil, err - } - return &k, nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_freebsd_386.go b/vendor/github.com/shirou/gopsutil/process/process_freebsd_386.go deleted file mode 100644 index 08ab333b4..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_freebsd_386.go +++ /dev/null @@ -1,192 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_freebsd.go - -package process - -const ( - CTLKern = 1 - KernProc = 14 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 7 -) - -const ( - sizeofPtr = 0x4 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x4 - sizeofLongLong = 0x8 -) - -const ( - sizeOfKinfoVmentry = 0x488 - sizeOfKinfoProc = 0x300 -) - -const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SWAIT = 6 - SLOCK = 7 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int32 - _C_long_long int64 -) - -type Timespec struct { - Sec int32 - Nsec int32 -} - -type Timeval struct { - Sec int32 - Usec int32 -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int32 - Ixrss int32 - Idrss int32 - Isrss int32 - Minflt int32 - Majflt int32 - Nswap int32 - Inblock int32 - Oublock int32 - Msgsnd int32 - Msgrcv int32 - Nsignals int32 - Nvcsw int32 - Nivcsw int32 -} - -type Rlimit struct { - Cur int64 - Max int64 -} - -type KinfoProc struct { - Structsize int32 - Layout int32 - Args int32 /* pargs */ - Paddr int32 /* proc */ - Addr int32 /* user */ - Tracep int32 /* vnode */ - Textvp int32 /* vnode */ - Fd int32 /* filedesc */ - Vmspace int32 /* vmspace */ - Wchan int32 - Pid int32 - Ppid int32 - Pgid int32 - Tpgid int32 - Sid int32 - Tsid int32 - Jobc int16 - Spare_short1 int16 - Tdev uint32 - Siglist [16]byte /* sigset */ - Sigmask [16]byte /* sigset */ - Sigignore [16]byte /* sigset */ - Sigcatch [16]byte /* sigset */ - Uid uint32 - Ruid uint32 - Svuid uint32 - Rgid uint32 - Svgid uint32 - Ngroups int16 - Spare_short2 int16 - Groups [16]uint32 - Size uint32 - Rssize int32 - Swrss int32 - Tsize int32 - Dsize int32 - Ssize int32 - Xstat uint16 - Acflag uint16 - Pctcpu uint32 - Estcpu uint32 - Slptime uint32 - Swtime uint32 - Cow uint32 - Runtime uint64 - Start Timeval - Childtime Timeval - Flag int32 - Kiflag int32 - Traceflag int32 - Stat int8 - Nice int8 - Lock int8 - Rqindex int8 - Oncpu uint8 - Lastcpu uint8 - Tdname [17]int8 - Wmesg [9]int8 - Login [18]int8 - Lockname [9]int8 - Comm [20]int8 - Emul [17]int8 - Loginclass [18]int8 - Sparestrings [50]int8 - Spareints [7]int32 - Flag2 int32 - Fibnum int32 - Cr_flags uint32 - Jid int32 - Numthreads int32 - Tid int32 - Pri Priority - Rusage Rusage - Rusage_ch Rusage - Pcb int32 /* pcb */ - Kstack int32 - Udata int32 - Tdaddr int32 /* thread */ - Spareptrs [6]int32 - Sparelongs [12]int32 - Sflag int32 - Tdflags int32 -} - -type Priority struct { - Class uint8 - Level uint8 - Native uint8 - User uint8 -} - -type KinfoVmentry struct { - Structsize int32 - Type int32 - Start uint64 - End uint64 - Offset uint64 - Vn_fileid uint64 - Vn_fsid uint32 - Flags int32 - Resident int32 - Private_resident int32 - Protection int32 - Ref_count int32 - Shadow_count int32 - Vn_type int32 - Vn_size uint64 - Vn_rdev uint32 - Vn_mode uint16 - Status uint16 - X_kve_ispare [12]int32 - Path [1024]int8 -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_freebsd_amd64.go b/vendor/github.com/shirou/gopsutil/process/process_freebsd_amd64.go deleted file mode 100644 index 560e627d2..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_freebsd_amd64.go +++ /dev/null @@ -1,192 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_freebsd.go - -package process - -const ( - CTLKern = 1 - KernProc = 14 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 7 -) - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -const ( - sizeOfKinfoVmentry = 0x488 - sizeOfKinfoProc = 0x440 -) - -const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SWAIT = 6 - SLOCK = 7 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int64 -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur int64 - Max int64 -} - -type KinfoProc struct { - Structsize int32 - Layout int32 - Args int64 /* pargs */ - Paddr int64 /* proc */ - Addr int64 /* user */ - Tracep int64 /* vnode */ - Textvp int64 /* vnode */ - Fd int64 /* filedesc */ - Vmspace int64 /* vmspace */ - Wchan int64 - Pid int32 - Ppid int32 - Pgid int32 - Tpgid int32 - Sid int32 - Tsid int32 - Jobc int16 - Spare_short1 int16 - Tdev uint32 - Siglist [16]byte /* sigset */ - Sigmask [16]byte /* sigset */ - Sigignore [16]byte /* sigset */ - Sigcatch [16]byte /* sigset */ - Uid uint32 - Ruid uint32 - Svuid uint32 - Rgid uint32 - Svgid uint32 - Ngroups int16 - Spare_short2 int16 - Groups [16]uint32 - Size uint64 - Rssize int64 - Swrss int64 - Tsize int64 - Dsize int64 - Ssize int64 - Xstat uint16 - Acflag uint16 - Pctcpu uint32 - Estcpu uint32 - Slptime uint32 - Swtime uint32 - Cow uint32 - Runtime uint64 - Start Timeval - Childtime Timeval - Flag int64 - Kiflag int64 - Traceflag int32 - Stat int8 - Nice int8 - Lock int8 - Rqindex int8 - Oncpu uint8 - Lastcpu uint8 - Tdname [17]int8 - Wmesg [9]int8 - Login [18]int8 - Lockname [9]int8 - Comm [20]int8 - Emul [17]int8 - Loginclass [18]int8 - Sparestrings [50]int8 - Spareints [7]int32 - Flag2 int32 - Fibnum int32 - Cr_flags uint32 - Jid int32 - Numthreads int32 - Tid int32 - Pri Priority - Rusage Rusage - Rusage_ch Rusage - Pcb int64 /* pcb */ - Kstack int64 - Udata int64 - Tdaddr int64 /* thread */ - Spareptrs [6]int64 - Sparelongs [12]int64 - Sflag int64 - Tdflags int64 -} - -type Priority struct { - Class uint8 - Level uint8 - Native uint8 - User uint8 -} - -type KinfoVmentry struct { - Structsize int32 - Type int32 - Start uint64 - End uint64 - Offset uint64 - Vn_fileid uint64 - Vn_fsid uint32 - Flags int32 - Resident int32 - Private_resident int32 - Protection int32 - Ref_count int32 - Shadow_count int32 - Vn_type int32 - Vn_size uint64 - Vn_rdev uint32 - Vn_mode uint16 - Status uint16 - X_kve_ispare [12]int32 - Path [1024]int8 -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_freebsd_arm.go b/vendor/github.com/shirou/gopsutil/process/process_freebsd_arm.go deleted file mode 100644 index 81ae0b9a8..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_freebsd_arm.go +++ /dev/null @@ -1,192 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_freebsd.go - -package process - -const ( - CTLKern = 1 - KernProc = 14 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 7 -) - -const ( - sizeofPtr = 0x4 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x4 - sizeofLongLong = 0x8 -) - -const ( - sizeOfKinfoVmentry = 0x488 - sizeOfKinfoProc = 0x440 -) - -const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SWAIT = 6 - SLOCK = 7 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int32 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int64 -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int32 - Ixrss int32 - Idrss int32 - Isrss int32 - Minflt int32 - Majflt int32 - Nswap int32 - Inblock int32 - Oublock int32 - Msgsnd int32 - Msgrcv int32 - Nsignals int32 - Nvcsw int32 - Nivcsw int32 -} - -type Rlimit struct { - Cur int32 - Max int32 -} - -type KinfoProc struct { - Structsize int32 - Layout int32 - Args int32 /* pargs */ - Paddr int32 /* proc */ - Addr int32 /* user */ - Tracep int32 /* vnode */ - Textvp int32 /* vnode */ - Fd int32 /* filedesc */ - Vmspace int32 /* vmspace */ - Wchan int32 - Pid int32 - Ppid int32 - Pgid int32 - Tpgid int32 - Sid int32 - Tsid int32 - Jobc int16 - Spare_short1 int16 - Tdev uint32 - Siglist [16]byte /* sigset */ - Sigmask [16]byte /* sigset */ - Sigignore [16]byte /* sigset */ - Sigcatch [16]byte /* sigset */ - Uid uint32 - Ruid uint32 - Svuid uint32 - Rgid uint32 - Svgid uint32 - Ngroups int16 - Spare_short2 int16 - Groups [16]uint32 - Size uint32 - Rssize int32 - Swrss int32 - Tsize int32 - Dsize int32 - Ssize int32 - Xstat uint16 - Acflag uint16 - Pctcpu uint32 - Estcpu uint32 - Slptime uint32 - Swtime uint32 - Cow uint32 - Runtime uint64 - Start Timeval - Childtime Timeval - Flag int32 - Kiflag int32 - Traceflag int32 - Stat int8 - Nice int8 - Lock int8 - Rqindex int8 - Oncpu uint8 - Lastcpu uint8 - Tdname [17]int8 - Wmesg [9]int8 - Login [18]int8 - Lockname [9]int8 - Comm [20]int8 - Emul [17]int8 - Loginclass [18]int8 - Sparestrings [50]int8 - Spareints [4]int32 - Flag2 int32 - Fibnum int32 - Cr_flags uint32 - Jid int32 - Numthreads int32 - Tid int32 - Pri Priority - Rusage Rusage - Rusage_ch Rusage - Pcb int32 /* pcb */ - Kstack int32 - Udata int32 - Tdaddr int32 /* thread */ - Spareptrs [6]int64 - Sparelongs [12]int64 - Sflag int64 - Tdflags int64 -} - -type Priority struct { - Class uint8 - Level uint8 - Native uint8 - User uint8 -} - -type KinfoVmentry struct { - Structsize int32 - Type int32 - Start uint64 - End uint64 - Offset uint64 - Vn_fileid uint64 - Vn_fsid uint32 - Flags int32 - Resident int32 - Private_resident int32 - Protection int32 - Ref_count int32 - Shadow_count int32 - Vn_type int32 - Vn_size uint64 - Vn_rdev uint32 - Vn_mode uint16 - Status uint16 - X_kve_ispare [12]int32 - Path [1024]int8 -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_freebsd_arm64.go b/vendor/github.com/shirou/gopsutil/process/process_freebsd_arm64.go deleted file mode 100644 index 99781d1a2..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_freebsd_arm64.go +++ /dev/null @@ -1,201 +0,0 @@ -// +build freebsd -// +build arm64 -// Code generated by cmd/cgo -godefs; DO NOT EDIT. -// cgo -godefs process/types_freebsd.go - -package process - -const ( - CTLKern = 1 - KernProc = 14 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 7 -) - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -const ( - sizeOfKinfoVmentry = 0x488 - sizeOfKinfoProc = 0x440 -) - -const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SWAIT = 6 - SLOCK = 7 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int64 -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur int64 - Max int64 -} - -type KinfoProc struct { - Structsize int32 - Layout int32 - Args *int64 /* pargs */ - Paddr *int64 /* proc */ - Addr *int64 /* user */ - Tracep *int64 /* vnode */ - Textvp *int64 /* vnode */ - Fd *int64 /* filedesc */ - Vmspace *int64 /* vmspace */ - Wchan *byte - Pid int32 - Ppid int32 - Pgid int32 - Tpgid int32 - Sid int32 - Tsid int32 - Jobc int16 - Spare_short1 int16 - Tdev_freebsd11 uint32 - Siglist [16]byte /* sigset */ - Sigmask [16]byte /* sigset */ - Sigignore [16]byte /* sigset */ - Sigcatch [16]byte /* sigset */ - Uid uint32 - Ruid uint32 - Svuid uint32 - Rgid uint32 - Svgid uint32 - Ngroups int16 - Spare_short2 int16 - Groups [16]uint32 - Size uint64 - Rssize int64 - Swrss int64 - Tsize int64 - Dsize int64 - Ssize int64 - Xstat uint16 - Acflag uint16 - Pctcpu uint32 - Estcpu uint32 - Slptime uint32 - Swtime uint32 - Cow uint32 - Runtime uint64 - Start Timeval - Childtime Timeval - Flag int64 - Kiflag int64 - Traceflag int32 - Stat uint8 - Nice int8 - Lock uint8 - Rqindex uint8 - Oncpu_old uint8 - Lastcpu_old uint8 - Tdname [17]uint8 - Wmesg [9]uint8 - Login [18]uint8 - Lockname [9]uint8 - Comm [20]int8 - Emul [17]uint8 - Loginclass [18]uint8 - Moretdname [4]uint8 - Sparestrings [46]uint8 - Spareints [2]int32 - Tdev uint64 - Oncpu int32 - Lastcpu int32 - Tracer int32 - Flag2 int32 - Fibnum int32 - Cr_flags uint32 - Jid int32 - Numthreads int32 - Tid int32 - Pri Priority - Rusage Rusage - Rusage_ch Rusage - Pcb *int64 /* pcb */ - Kstack *byte - Udata *byte - Tdaddr *int64 /* thread */ - Spareptrs [6]*byte - Sparelongs [12]int64 - Sflag int64 - Tdflags int64 -} - -type Priority struct { - Class uint8 - Level uint8 - Native uint8 - User uint8 -} - -type KinfoVmentry struct { - Structsize int32 - Type int32 - Start uint64 - End uint64 - Offset uint64 - Vn_fileid uint64 - Vn_fsid_freebsd11 uint32 - Flags int32 - Resident int32 - Private_resident int32 - Protection int32 - Ref_count int32 - Shadow_count int32 - Vn_type int32 - Vn_size uint64 - Vn_rdev_freebsd11 uint32 - Vn_mode uint16 - Status uint16 - Vn_fsid uint64 - Vn_rdev uint64 - X_kve_ispare [8]int32 - Path [1024]uint8 -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_linux.go b/vendor/github.com/shirou/gopsutil/process/process_linux.go deleted file mode 100644 index de742fbde..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_linux.go +++ /dev/null @@ -1,1195 +0,0 @@ -// +build linux - -package process - -import ( - "bufio" - "bytes" - "context" - "encoding/json" - "fmt" - "io/ioutil" - "math" - "os" - "path/filepath" - "strconv" - "strings" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/net" - "github.com/tklauser/go-sysconf" - "golang.org/x/sys/unix" -) - -var PageSize = uint64(os.Getpagesize()) - -const PrioProcess = 0 // linux/resource.h - -var ClockTicks = 100 // default value - -func init() { - clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) - // ignore errors - if err == nil { - ClockTicks = int(clkTck) - } -} - -// MemoryInfoExStat is different between OSes -type MemoryInfoExStat struct { - RSS uint64 `json:"rss"` // bytes - VMS uint64 `json:"vms"` // bytes - Shared uint64 `json:"shared"` // bytes - Text uint64 `json:"text"` // bytes - Lib uint64 `json:"lib"` // bytes - Data uint64 `json:"data"` // bytes - Dirty uint64 `json:"dirty"` // bytes -} - -func (m MemoryInfoExStat) String() string { - s, _ := json.Marshal(m) - return string(s) -} - -type MemoryMapsStat struct { - Path string `json:"path"` - Rss uint64 `json:"rss"` - Size uint64 `json:"size"` - Pss uint64 `json:"pss"` - SharedClean uint64 `json:"sharedClean"` - SharedDirty uint64 `json:"sharedDirty"` - PrivateClean uint64 `json:"privateClean"` - PrivateDirty uint64 `json:"privateDirty"` - Referenced uint64 `json:"referenced"` - Anonymous uint64 `json:"anonymous"` - Swap uint64 `json:"swap"` -} - -// String returns JSON value of the process. -func (m MemoryMapsStat) String() string { - s, _ := json.Marshal(m) - return string(s) -} - -func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - _, ppid, _, _, _, _, _, err := p.fillFromStatWithContext(ctx) - if err != nil { - return -1, err - } - return ppid, nil -} - -func (p *Process) NameWithContext(ctx context.Context) (string, error) { - if p.name == "" { - if err := p.fillNameWithContext(ctx); err != nil { - return "", err - } - } - return p.name, nil -} - -func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { - if p.tgid == 0 { - if err := p.fillFromStatusWithContext(ctx); err != nil { - return 0, err - } - } - return p.tgid, nil -} - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - return p.fillFromExeWithContext(ctx) -} - -func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { - return p.fillFromCmdlineWithContext(ctx) -} - -func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { - return p.fillSliceFromCmdlineWithContext(ctx) -} - -func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - _, _, _, createTime, _, _, _, err := p.fillFromStatWithContext(ctx) - if err != nil { - return 0, err - } - return createTime, nil -} - -func (p *Process) CwdWithContext(ctx context.Context) (string, error) { - return p.fillFromCwdWithContext(ctx) -} - -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - err := p.fillFromStatusWithContext(ctx) - if err != nil { - return nil, err - } - if p.parent == 0 { - return nil, fmt.Errorf("wrong number of parents") - } - return NewProcessWithContext(ctx, p.parent) -} - -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - err := p.fillFromStatusWithContext(ctx) - if err != nil { - return "", err - } - return p.status, nil -} - -func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { - // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details - pid := p.Pid - statPath := common.HostProc(strconv.Itoa(int(pid)), "stat") - contents, err := ioutil.ReadFile(statPath) - if err != nil { - return false, err - } - fields := strings.Fields(string(contents)) - if len(fields) < 8 { - return false, fmt.Errorf("insufficient data in %s", statPath) - } - pgid := fields[4] - tpgid := fields[7] - return pgid == tpgid, nil -} - -func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext(ctx) - if err != nil { - return []int32{}, err - } - return p.uids, nil -} - -func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext(ctx) - if err != nil { - return []int32{}, err - } - return p.gids, nil -} - -func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext(ctx) - if err != nil { - return []int32{}, err - } - return p.groups, nil -} - -func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { - t, _, _, _, _, _, _, err := p.fillFromStatWithContext(ctx) - if err != nil { - return "", err - } - termmap, err := getTerminalMap() - if err != nil { - return "", err - } - terminal := termmap[t] - return terminal, nil -} - -func (p *Process) NiceWithContext(ctx context.Context) (int32, error) { - _, _, _, _, _, nice, _, err := p.fillFromStatWithContext(ctx) - if err != nil { - return 0, err - } - return nice, nil -} - -func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) { - return p.RlimitUsageWithContext(ctx, false) -} - -func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) { - rlimits, err := p.fillFromLimitsWithContext(ctx) - if !gatherUsed || err != nil { - return rlimits, err - } - - _, _, _, _, rtprio, nice, _, err := p.fillFromStatWithContext(ctx) - if err != nil { - return nil, err - } - if err := p.fillFromStatusWithContext(ctx); err != nil { - return nil, err - } - - for i := range rlimits { - rs := &rlimits[i] - switch rs.Resource { - case RLIMIT_CPU: - times, err := p.TimesWithContext(ctx) - if err != nil { - return nil, err - } - rs.Used = uint64(times.User + times.System) - case RLIMIT_DATA: - rs.Used = uint64(p.memInfo.Data) - case RLIMIT_STACK: - rs.Used = uint64(p.memInfo.Stack) - case RLIMIT_RSS: - rs.Used = uint64(p.memInfo.RSS) - case RLIMIT_NOFILE: - n, err := p.NumFDsWithContext(ctx) - if err != nil { - return nil, err - } - rs.Used = uint64(n) - case RLIMIT_MEMLOCK: - rs.Used = uint64(p.memInfo.Locked) - case RLIMIT_AS: - rs.Used = uint64(p.memInfo.VMS) - case RLIMIT_LOCKS: - //TODO we can get the used value from /proc/$pid/locks. But linux doesn't enforce it, so not a high priority. - case RLIMIT_SIGPENDING: - rs.Used = p.sigInfo.PendingProcess - case RLIMIT_NICE: - // The rlimit for nice is a little unusual, in that 0 means the niceness cannot be decreased beyond the current value, but it can be increased. - // So effectively: if rs.Soft == 0 { rs.Soft = rs.Used } - rs.Used = uint64(nice) - case RLIMIT_RTPRIO: - rs.Used = uint64(rtprio) - } - } - - return rlimits, err -} - -func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - return p.fillFromIOWithContext(ctx) -} - -func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) { - err := p.fillFromStatusWithContext(ctx) - if err != nil { - return nil, err - } - return p.numCtxSwitches, nil -} - -func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { - _, fnames, err := p.fillFromfdListWithContext(ctx) - return int32(len(fnames)), err -} - -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - err := p.fillFromStatusWithContext(ctx) - if err != nil { - return 0, err - } - return p.numThreads, nil -} - -func (p *Process) ThreadsWithContext(ctx context.Context) (map[int32]*cpu.TimesStat, error) { - ret := make(map[int32]*cpu.TimesStat) - taskPath := common.HostProc(strconv.Itoa(int(p.Pid)), "task") - - tids, err := readPidsFromDir(taskPath) - if err != nil { - return nil, err - } - - for _, tid := range tids { - _, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStatWithContext(ctx, tid) - if err != nil { - return nil, err - } - ret[tid] = cpuTimes - } - - return ret, nil -} - -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - _, _, cpuTimes, _, _, _, _, err := p.fillFromStatWithContext(ctx) - if err != nil { - return nil, err - } - return cpuTimes, nil -} - -func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - meminfo, _, err := p.fillFromStatmWithContext(ctx) - if err != nil { - return nil, err - } - return meminfo, nil -} - -func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - _, memInfoEx, err := p.fillFromStatmWithContext(ctx) - if err != nil { - return nil, err - } - return memInfoEx, nil -} - -func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) { - _, _, _, _, _, _, pageFaults, err := p.fillFromStatWithContext(ctx) - if err != nil { - return nil, err - } - return pageFaults, nil - -} - -func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { - pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid) - if err != nil { - if len(pids) == 0 { - return nil, ErrorNoChildren - } - return nil, err - } - ret := make([]*Process, 0, len(pids)) - for _, pid := range pids { - np, err := NewProcessWithContext(ctx, pid) - if err != nil { - return nil, err - } - ret = append(ret, np) - } - return ret, nil -} - -func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) { - _, ofs, err := p.fillFromfdWithContext(ctx) - if err != nil { - return nil, err - } - ret := make([]OpenFilesStat, len(ofs)) - for i, o := range ofs { - ret[i] = *o - } - - return ret, nil -} - -func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { - return net.ConnectionsPidWithContext(ctx, "all", p.Pid) -} - -func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) { - return net.ConnectionsPidMaxWithContext(ctx, "all", p.Pid, max) -} - -func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) { - filename := common.HostProc(strconv.Itoa(int(p.Pid)), "net/dev") - return net.IOCountersByFileWithContext(ctx, pernic, filename) -} - -func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) { - pid := p.Pid - var ret []MemoryMapsStat - if grouped { - ret = make([]MemoryMapsStat, 1) - } - smapsPath := common.HostProc(strconv.Itoa(int(pid)), "smaps") - contents, err := ioutil.ReadFile(smapsPath) - if err != nil { - return nil, err - } - lines := strings.Split(string(contents), "\n") - - // function of parsing a block - getBlock := func(firstLine []string, block []string) (MemoryMapsStat, error) { - m := MemoryMapsStat{} - m.Path = firstLine[len(firstLine)-1] - - for _, line := range block { - if strings.Contains(line, "VmFlags") { - continue - } - field := strings.Split(line, ":") - if len(field) < 2 { - continue - } - v := strings.Trim(field[1], "kB") // remove last "kB" - v = strings.TrimSpace(v) - t, err := strconv.ParseUint(v, 10, 64) - if err != nil { - return m, err - } - - switch field[0] { - case "Size": - m.Size = t - case "Rss": - m.Rss = t - case "Pss": - m.Pss = t - case "Shared_Clean": - m.SharedClean = t - case "Shared_Dirty": - m.SharedDirty = t - case "Private_Clean": - m.PrivateClean = t - case "Private_Dirty": - m.PrivateDirty = t - case "Referenced": - m.Referenced = t - case "Anonymous": - m.Anonymous = t - case "Swap": - m.Swap = t - } - } - return m, nil - } - - var firstLine []string - blocks := make([]string, 0, 16) - for i, line := range lines { - fields := strings.Fields(line) - - if (len(fields) > 0 && !strings.HasSuffix(fields[0], ":")) || i == len(lines)-1 { - // new block section - if len(firstLine) > 0 && len(blocks) > 0 { - g, err := getBlock(firstLine, blocks) - if err != nil { - return &ret, err - } - if grouped { - ret[0].Size += g.Size - ret[0].Rss += g.Rss - ret[0].Pss += g.Pss - ret[0].SharedClean += g.SharedClean - ret[0].SharedDirty += g.SharedDirty - ret[0].PrivateClean += g.PrivateClean - ret[0].PrivateDirty += g.PrivateDirty - ret[0].Referenced += g.Referenced - ret[0].Anonymous += g.Anonymous - ret[0].Swap += g.Swap - } else { - ret = append(ret, g) - } - } - // starts new block - blocks = make([]string, 0, 16) - firstLine = fields - } else { - blocks = append(blocks, line) - } - } - - return &ret, nil -} - -func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) { - environPath := common.HostProc(strconv.Itoa(int(p.Pid)), "environ") - - environContent, err := ioutil.ReadFile(environPath) - if err != nil { - return nil, err - } - - return strings.Split(string(environContent), "\000"), nil -} - -/** -** Internal functions -**/ - -func limitToInt(val string) (int32, error) { - if val == "unlimited" { - return math.MaxInt32, nil - } else { - res, err := strconv.ParseInt(val, 10, 32) - if err != nil { - return 0, err - } - return int32(res), nil - } -} - -// Get name from /proc/(pid)/comm or /proc/(pid)/status -func (p *Process) fillNameWithContext(ctx context.Context) error { - err := p.fillFromCommWithContext(ctx) - if err == nil && p.name != "" && len(p.name) < 15 { - return nil - } - return p.fillFromStatusWithContext(ctx) -} - -// Get name from /proc/(pid)/comm -func (p *Process) fillFromCommWithContext(ctx context.Context) error { - pid := p.Pid - statPath := common.HostProc(strconv.Itoa(int(pid)), "comm") - contents, err := ioutil.ReadFile(statPath) - if err != nil { - return err - } - - p.name = strings.TrimSuffix(string(contents), "\n") - return nil -} - -// Get num_fds from /proc/(pid)/limits -func (p *Process) fillFromLimitsWithContext(ctx context.Context) ([]RlimitStat, error) { - pid := p.Pid - limitsFile := common.HostProc(strconv.Itoa(int(pid)), "limits") - d, err := os.Open(limitsFile) - if err != nil { - return nil, err - } - defer d.Close() - - var limitStats []RlimitStat - - limitsScanner := bufio.NewScanner(d) - for limitsScanner.Scan() { - var statItem RlimitStat - - str := strings.Fields(limitsScanner.Text()) - - // Remove the header line - if strings.Contains(str[len(str)-1], "Units") { - continue - } - - // Assert that last item is a Hard limit - statItem.Hard, err = limitToInt(str[len(str)-1]) - if err != nil { - // On error remove last item an try once again since it can be unit or header line - str = str[:len(str)-1] - statItem.Hard, err = limitToInt(str[len(str)-1]) - if err != nil { - return nil, err - } - } - // Remove last item from string - str = str[:len(str)-1] - - //Now last item is a Soft limit - statItem.Soft, err = limitToInt(str[len(str)-1]) - if err != nil { - return nil, err - } - // Remove last item from string - str = str[:len(str)-1] - - //The rest is a stats name - resourceName := strings.Join(str, " ") - switch resourceName { - case "Max cpu time": - statItem.Resource = RLIMIT_CPU - case "Max file size": - statItem.Resource = RLIMIT_FSIZE - case "Max data size": - statItem.Resource = RLIMIT_DATA - case "Max stack size": - statItem.Resource = RLIMIT_STACK - case "Max core file size": - statItem.Resource = RLIMIT_CORE - case "Max resident set": - statItem.Resource = RLIMIT_RSS - case "Max processes": - statItem.Resource = RLIMIT_NPROC - case "Max open files": - statItem.Resource = RLIMIT_NOFILE - case "Max locked memory": - statItem.Resource = RLIMIT_MEMLOCK - case "Max address space": - statItem.Resource = RLIMIT_AS - case "Max file locks": - statItem.Resource = RLIMIT_LOCKS - case "Max pending signals": - statItem.Resource = RLIMIT_SIGPENDING - case "Max msgqueue size": - statItem.Resource = RLIMIT_MSGQUEUE - case "Max nice priority": - statItem.Resource = RLIMIT_NICE - case "Max realtime priority": - statItem.Resource = RLIMIT_RTPRIO - case "Max realtime timeout": - statItem.Resource = RLIMIT_RTTIME - default: - continue - } - - limitStats = append(limitStats, statItem) - } - - if err := limitsScanner.Err(); err != nil { - return nil, err - } - - return limitStats, nil -} - -// Get list of /proc/(pid)/fd files -func (p *Process) fillFromfdListWithContext(ctx context.Context) (string, []string, error) { - pid := p.Pid - statPath := common.HostProc(strconv.Itoa(int(pid)), "fd") - d, err := os.Open(statPath) - if err != nil { - return statPath, []string{}, err - } - defer d.Close() - fnames, err := d.Readdirnames(-1) - return statPath, fnames, err -} - -// Get num_fds from /proc/(pid)/fd -func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFilesStat, error) { - statPath, fnames, err := p.fillFromfdListWithContext(ctx) - if err != nil { - return 0, nil, err - } - numFDs := int32(len(fnames)) - - var openfiles []*OpenFilesStat - for _, fd := range fnames { - fpath := filepath.Join(statPath, fd) - filepath, err := os.Readlink(fpath) - if err != nil { - continue - } - t, err := strconv.ParseUint(fd, 10, 64) - if err != nil { - return numFDs, openfiles, err - } - o := &OpenFilesStat{ - Path: filepath, - Fd: t, - } - openfiles = append(openfiles, o) - } - - return numFDs, openfiles, nil -} - -// Get cwd from /proc/(pid)/cwd -func (p *Process) fillFromCwdWithContext(ctx context.Context) (string, error) { - pid := p.Pid - cwdPath := common.HostProc(strconv.Itoa(int(pid)), "cwd") - cwd, err := os.Readlink(cwdPath) - if err != nil { - return "", err - } - return string(cwd), nil -} - -// Get exe from /proc/(pid)/exe -func (p *Process) fillFromExeWithContext(ctx context.Context) (string, error) { - pid := p.Pid - exePath := common.HostProc(strconv.Itoa(int(pid)), "exe") - exe, err := os.Readlink(exePath) - if err != nil { - return "", err - } - return string(exe), nil -} - -// Get cmdline from /proc/(pid)/cmdline -func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error) { - pid := p.Pid - cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) - if err != nil { - return "", err - } - ret := strings.FieldsFunc(string(cmdline), func(r rune) bool { - return r == '\u0000' - }) - - return strings.Join(ret, " "), nil -} - -func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string, error) { - pid := p.Pid - cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) - if err != nil { - return nil, err - } - if len(cmdline) == 0 { - return nil, nil - } - if cmdline[len(cmdline)-1] == 0 { - cmdline = cmdline[:len(cmdline)-1] - } - parts := bytes.Split(cmdline, []byte{0}) - var strParts []string - for _, p := range parts { - strParts = append(strParts, string(p)) - } - - return strParts, nil -} - -// Get IO status from /proc/(pid)/io -func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, error) { - pid := p.Pid - ioPath := common.HostProc(strconv.Itoa(int(pid)), "io") - ioline, err := ioutil.ReadFile(ioPath) - if err != nil { - return nil, err - } - lines := strings.Split(string(ioline), "\n") - ret := &IOCountersStat{} - - for _, line := range lines { - field := strings.Fields(line) - if len(field) < 2 { - continue - } - t, err := strconv.ParseUint(field[1], 10, 64) - if err != nil { - return nil, err - } - param := field[0] - if strings.HasSuffix(param, ":") { - param = param[:len(param)-1] - } - switch param { - case "syscr": - ret.ReadCount = t - case "syscw": - ret.WriteCount = t - case "read_bytes": - ret.ReadBytes = t - case "write_bytes": - ret.WriteBytes = t - } - } - - return ret, nil -} - -// Get memory info from /proc/(pid)/statm -func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat, *MemoryInfoExStat, error) { - pid := p.Pid - memPath := common.HostProc(strconv.Itoa(int(pid)), "statm") - contents, err := ioutil.ReadFile(memPath) - if err != nil { - return nil, nil, err - } - fields := strings.Split(string(contents), " ") - - vms, err := strconv.ParseUint(fields[0], 10, 64) - if err != nil { - return nil, nil, err - } - rss, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - return nil, nil, err - } - memInfo := &MemoryInfoStat{ - RSS: rss * PageSize, - VMS: vms * PageSize, - } - - shared, err := strconv.ParseUint(fields[2], 10, 64) - if err != nil { - return nil, nil, err - } - text, err := strconv.ParseUint(fields[3], 10, 64) - if err != nil { - return nil, nil, err - } - lib, err := strconv.ParseUint(fields[4], 10, 64) - if err != nil { - return nil, nil, err - } - dirty, err := strconv.ParseUint(fields[5], 10, 64) - if err != nil { - return nil, nil, err - } - - memInfoEx := &MemoryInfoExStat{ - RSS: rss * PageSize, - VMS: vms * PageSize, - Shared: shared * PageSize, - Text: text * PageSize, - Lib: lib * PageSize, - Dirty: dirty * PageSize, - } - - return memInfo, memInfoEx, nil -} - -// Get various status from /proc/(pid)/status -func (p *Process) fillFromStatusWithContext(ctx context.Context) error { - pid := p.Pid - statPath := common.HostProc(strconv.Itoa(int(pid)), "status") - contents, err := ioutil.ReadFile(statPath) - if err != nil { - return err - } - lines := strings.Split(string(contents), "\n") - p.numCtxSwitches = &NumCtxSwitchesStat{} - p.memInfo = &MemoryInfoStat{} - p.sigInfo = &SignalInfoStat{} - for _, line := range lines { - tabParts := strings.SplitN(line, "\t", 2) - if len(tabParts) < 2 { - continue - } - value := tabParts[1] - switch strings.TrimRight(tabParts[0], ":") { - case "Name": - p.name = strings.Trim(value, " \t") - if len(p.name) >= 15 { - cmdlineSlice, err := p.CmdlineSlice() - if err != nil { - return err - } - if len(cmdlineSlice) > 0 { - extendedName := filepath.Base(cmdlineSlice[0]) - if strings.HasPrefix(extendedName, p.name) { - p.name = extendedName - } else { - p.name = cmdlineSlice[0] - } - } - } - // Ensure we have a copy and not reference into slice - p.name = string([]byte(p.name)) - case "State": - p.status = value[0:1] - // Ensure we have a copy and not reference into slice - p.status = string([]byte(p.status)) - case "PPid", "Ppid": - pval, err := strconv.ParseInt(value, 10, 32) - if err != nil { - return err - } - p.parent = int32(pval) - case "Tgid": - pval, err := strconv.ParseInt(value, 10, 32) - if err != nil { - return err - } - p.tgid = int32(pval) - case "Uid": - p.uids = make([]int32, 0, 4) - for _, i := range strings.Split(value, "\t") { - v, err := strconv.ParseInt(i, 10, 32) - if err != nil { - return err - } - p.uids = append(p.uids, int32(v)) - } - case "Gid": - p.gids = make([]int32, 0, 4) - for _, i := range strings.Split(value, "\t") { - v, err := strconv.ParseInt(i, 10, 32) - if err != nil { - return err - } - p.gids = append(p.gids, int32(v)) - } - case "Groups": - groups := strings.Fields(value) - p.groups = make([]int32, 0, len(groups)) - for _, i := range groups { - v, err := strconv.ParseInt(i, 10, 32) - if err != nil { - return err - } - p.groups = append(p.groups, int32(v)) - } - case "Threads": - v, err := strconv.ParseInt(value, 10, 32) - if err != nil { - return err - } - p.numThreads = int32(v) - case "voluntary_ctxt_switches": - v, err := strconv.ParseInt(value, 10, 64) - if err != nil { - return err - } - p.numCtxSwitches.Voluntary = v - case "nonvoluntary_ctxt_switches": - v, err := strconv.ParseInt(value, 10, 64) - if err != nil { - return err - } - p.numCtxSwitches.Involuntary = v - case "VmRSS": - value := strings.Trim(value, " kB") // remove last "kB" - v, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - p.memInfo.RSS = v * 1024 - case "VmSize": - value := strings.Trim(value, " kB") // remove last "kB" - v, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - p.memInfo.VMS = v * 1024 - case "VmSwap": - value := strings.Trim(value, " kB") // remove last "kB" - v, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - p.memInfo.Swap = v * 1024 - case "VmHWM": - value := strings.Trim(value, " kB") // remove last "kB" - v, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - p.memInfo.HWM = v * 1024 - case "VmData": - value := strings.Trim(value, " kB") // remove last "kB" - v, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - p.memInfo.Data = v * 1024 - case "VmStk": - value := strings.Trim(value, " kB") // remove last "kB" - v, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - p.memInfo.Stack = v * 1024 - case "VmLck": - value := strings.Trim(value, " kB") // remove last "kB" - v, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - p.memInfo.Locked = v * 1024 - case "SigPnd": - if len(value) > 16 { - value = value[len(value)-16:] - } - v, err := strconv.ParseUint(value, 16, 64) - if err != nil { - return err - } - p.sigInfo.PendingThread = v - case "ShdPnd": - if len(value) > 16 { - value = value[len(value)-16:] - } - v, err := strconv.ParseUint(value, 16, 64) - if err != nil { - return err - } - p.sigInfo.PendingProcess = v - case "SigBlk": - if len(value) > 16 { - value = value[len(value)-16:] - } - v, err := strconv.ParseUint(value, 16, 64) - if err != nil { - return err - } - p.sigInfo.Blocked = v - case "SigIgn": - if len(value) > 16 { - value = value[len(value)-16:] - } - v, err := strconv.ParseUint(value, 16, 64) - if err != nil { - return err - } - p.sigInfo.Ignored = v - case "SigCgt": - if len(value) > 16 { - value = value[len(value)-16:] - } - v, err := strconv.ParseUint(value, 16, 64) - if err != nil { - return err - } - p.sigInfo.Caught = v - } - - } - return nil -} - -func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) { - pid := p.Pid - var statPath string - - if tid == -1 { - statPath = common.HostProc(strconv.Itoa(int(pid)), "stat") - } else { - statPath = common.HostProc(strconv.Itoa(int(pid)), "task", strconv.Itoa(int(tid)), "stat") - } - - contents, err := ioutil.ReadFile(statPath) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - // Indexing from one, as described in `man proc` about the file /proc/[pid]/stat - fields := splitProcStat(contents) - - terminal, err := strconv.ParseUint(fields[7], 10, 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - - ppid, err := strconv.ParseInt(fields[4], 10, 32) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - utime, err := strconv.ParseFloat(fields[14], 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - - stime, err := strconv.ParseFloat(fields[15], 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - - // There is no such thing as iotime in stat file. As an approximation, we - // will use delayacct_blkio_ticks (aggregated block I/O delays, as per Linux - // docs). Note: I am assuming at least Linux 2.6.18 - var iotime float64 - if len(fields) > 42 { - iotime, err = strconv.ParseFloat(fields[42], 64) - if err != nil { - iotime = 0 // Ancient linux version, most likely - } - } else { - iotime = 0 // e.g. SmartOS containers - } - - cpuTimes := &cpu.TimesStat{ - CPU: "cpu", - User: utime / float64(ClockTicks), - System: stime / float64(ClockTicks), - Iowait: iotime / float64(ClockTicks), - } - - bootTime, _ := common.BootTimeWithContext(ctx) - t, err := strconv.ParseUint(fields[22], 10, 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - ctime := (t / uint64(ClockTicks)) + uint64(bootTime) - createTime := int64(ctime * 1000) - - rtpriority, err := strconv.ParseInt(fields[18], 10, 32) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - if rtpriority < 0 { - rtpriority = rtpriority*-1 - 1 - } else { - rtpriority = 0 - } - - // p.Nice = mustParseInt32(fields[18]) - // use syscall instead of parse Stat file - snice, _ := unix.Getpriority(PrioProcess, int(pid)) - nice := int32(snice) // FIXME: is this true? - - minFault, err := strconv.ParseUint(fields[10], 10, 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - cMinFault, err := strconv.ParseUint(fields[11], 10, 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - majFault, err := strconv.ParseUint(fields[12], 10, 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - cMajFault, err := strconv.ParseUint(fields[13], 10, 64) - if err != nil { - return 0, 0, nil, 0, 0, 0, nil, err - } - - faults := &PageFaultsStat{ - MinorFaults: minFault, - MajorFaults: majFault, - ChildMinorFaults: cMinFault, - ChildMajorFaults: cMajFault, - } - - return terminal, int32(ppid), cpuTimes, createTime, uint32(rtpriority), nice, faults, nil -} - -func (p *Process) fillFromStatWithContext(ctx context.Context) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) { - return p.fillFromTIDStatWithContext(ctx, -1) -} - -func pidsWithContext(ctx context.Context) ([]int32, error) { - return readPidsFromDir(common.HostProc()) -} - -func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - out := []*Process{} - - pids, err := PidsWithContext(ctx) - if err != nil { - return out, err - } - - for _, pid := range pids { - p, err := NewProcessWithContext(ctx, pid) - if err != nil { - continue - } - out = append(out, p) - } - - return out, nil -} - -func readPidsFromDir(path string) ([]int32, error) { - var ret []int32 - - d, err := os.Open(path) - if err != nil { - return nil, err - } - defer d.Close() - - fnames, err := d.Readdirnames(-1) - if err != nil { - return nil, err - } - for _, fname := range fnames { - pid, err := strconv.ParseInt(fname, 10, 32) - if err != nil { - // if not numeric name, just skip - continue - } - ret = append(ret, int32(pid)) - } - - return ret, nil -} - -func splitProcStat(content []byte) []string { - nameStart := bytes.IndexByte(content, '(') - nameEnd := bytes.LastIndexByte(content, ')') - restFields := strings.Fields(string(content[nameEnd+2:])) // +2 skip ') ' - name := content[nameStart+1 : nameEnd] - pid := strings.TrimSpace(string(content[:nameStart])) - fields := make([]string, 3, len(restFields)+3) - fields[1] = string(pid) - fields[2] = string(name) - fields = append(fields, restFields...) - return fields -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_openbsd.go b/vendor/github.com/shirou/gopsutil/process/process_openbsd.go deleted file mode 100644 index 902664b06..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_openbsd.go +++ /dev/null @@ -1,395 +0,0 @@ -// +build openbsd - -package process - -import ( - "bytes" - "context" - "encoding/binary" - "fmt" - "io" - "os/exec" - "path/filepath" - "strconv" - "strings" - "unsafe" - - cpu "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - mem "github.com/shirou/gopsutil/mem" - net "github.com/shirou/gopsutil/net" - "golang.org/x/sys/unix" -) - -func pidsWithContext(ctx context.Context) ([]int32, error) { - var ret []int32 - procs, err := ProcessesWithContext(ctx) - if err != nil { - return ret, nil - } - - for _, p := range procs { - ret = append(ret, p.Pid) - } - - return ret, nil -} - -func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - k, err := p.getKProc() - if err != nil { - return 0, err - } - - return k.Ppid, nil -} - -func (p *Process) NameWithContext(ctx context.Context) (string, error) { - k, err := p.getKProc() - if err != nil { - return "", err - } - name := common.IntToString(k.Comm[:]) - - if len(name) >= 15 { - cmdlineSlice, err := p.CmdlineSliceWithContext(ctx) - if err != nil { - return "", err - } - if len(cmdlineSlice) > 0 { - extendedName := filepath.Base(cmdlineSlice[0]) - if strings.HasPrefix(extendedName, p.name) { - name = extendedName - } else { - name = cmdlineSlice[0] - } - } - } - - return name, nil -} - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { - mib := []int32{CTLKern, KernProcArgs, p.Pid, KernProcArgv} - buf, _, err := common.CallSyscall(mib) - - if err != nil { - return nil, err - } - - /* From man sysctl(2): - The buffer pointed to by oldp is filled with an array of char - pointers followed by the strings themselves. The last char - pointer is a NULL pointer. */ - var strParts []string - r := bytes.NewReader(buf) - baseAddr := uintptr(unsafe.Pointer(&buf[0])) - for { - argvp, err := readPtr(r) - if err != nil { - return nil, err - } - if argvp == 0 { // check for a NULL pointer - break - } - offset := argvp - baseAddr - length := uintptr(bytes.IndexByte(buf[offset:], 0)) - str := string(buf[offset : offset+length]) - strParts = append(strParts, str) - } - - return strParts, nil -} - -// readPtr reads a pointer data from a given reader. WARNING: only little -// endian architectures are supported. -func readPtr(r io.Reader) (uintptr, error) { - switch sizeofPtr { - case 4: - var p uint32 - if err := binary.Read(r, binary.LittleEndian, &p); err != nil { - return 0, err - } - return uintptr(p), nil - case 8: - var p uint64 - if err := binary.Read(r, binary.LittleEndian, &p); err != nil { - return 0, err - } - return uintptr(p), nil - default: - return 0, fmt.Errorf("unsupported pointer size") - } -} - -func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { - argv, err := p.CmdlineSliceWithContext(ctx) - if err != nil { - return "", err - } - return strings.Join(argv, " "), nil -} - -func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - k, err := p.getKProc() - if err != nil { - return "", err - } - var s string - switch k.Stat { - case SIDL: - case SRUN: - case SONPROC: - s = "R" - case SSLEEP: - s = "S" - case SSTOP: - s = "T" - case SDEAD: - s = "Z" - } - - return s, nil -} - -func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { - // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details - pid := p.Pid - ps, err := exec.LookPath("ps") - if err != nil { - return false, err - } - out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid))) - if err != nil { - return false, err - } - return strings.IndexByte(string(out), '+') != -1, nil -} - -func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - uids := make([]int32, 0, 3) - - uids = append(uids, int32(k.Ruid), int32(k.Uid), int32(k.Svuid)) - - return uids, nil -} - -func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - gids := make([]int32, 0, 3) - gids = append(gids, int32(k.Rgid), int32(k.Ngroups), int32(k.Svgid)) - - return gids, nil -} - -func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - - groups := make([]int32, k.Ngroups) - for i := int16(0); i < k.Ngroups; i++ { - groups[i] = int32(k.Groups[i]) - } - - return groups, nil -} - -func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { - k, err := p.getKProc() - if err != nil { - return "", err - } - - ttyNr := uint64(k.Tdev) - - termmap, err := getTerminalMap() - if err != nil { - return "", err - } - - return termmap[ttyNr], nil -} - -func (p *Process) NiceWithContext(ctx context.Context) (int32, error) { - k, err := p.getKProc() - if err != nil { - return 0, err - } - return int32(k.Nice), nil -} - -func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - return &IOCountersStat{ - ReadCount: uint64(k.Uru_inblock), - WriteCount: uint64(k.Uru_oublock), - }, nil -} - -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - /* not supported, just return 1 */ - return 1, nil -} - -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - return &cpu.TimesStat{ - CPU: "cpu", - User: float64(k.Uutime_sec) + float64(k.Uutime_usec)/1000000, - System: float64(k.Ustime_sec) + float64(k.Ustime_usec)/1000000, - }, nil -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } - pageSize, err := mem.GetPageSizeWithContext(ctx) - if err != nil { - return nil, err - } - - return &MemoryInfoStat{ - RSS: uint64(k.Vm_rssize) * pageSize, - VMS: uint64(k.Vm_tsize) + uint64(k.Vm_dsize) + - uint64(k.Vm_ssize), - }, nil -} - -func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { - pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid) - if err != nil { - return nil, err - } - ret := make([]*Process, 0, len(pids)) - for _, pid := range pids { - np, err := NewProcessWithContext(ctx, pid) - if err != nil { - return nil, err - } - ret = append(ret, np) - } - return ret, nil -} - -func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - results := []*Process{} - - buf, length, err := callKernProcSyscall(KernProcAll, 0) - - if err != nil { - return results, err - } - - // get kinfo_proc size - count := int(length / uint64(sizeOfKinfoProc)) - - // parse buf to procs - for i := 0; i < count; i++ { - b := buf[i*sizeOfKinfoProc : (i+1)*sizeOfKinfoProc] - k, err := parseKinfoProc(b) - if err != nil { - continue - } - p, err := NewProcessWithContext(ctx, int32(k.Pid)) - if err != nil { - continue - } - - results = append(results, p) - } - - return results, nil -} - -func (p *Process) getKProc() (*KinfoProc, error) { - buf, length, err := callKernProcSyscall(KernProcPID, p.Pid) - if err != nil { - return nil, err - } - if length != sizeOfKinfoProc { - return nil, err - } - - k, err := parseKinfoProc(buf) - if err != nil { - return nil, err - } - return &k, nil -} - -func callKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) { - mib := []int32{CTLKern, KernProc, op, arg, sizeOfKinfoProc, 0} - mibptr := unsafe.Pointer(&mib[0]) - miblen := uint64(len(mib)) - length := uint64(0) - _, _, err := unix.Syscall6( - unix.SYS___SYSCTL, - uintptr(mibptr), - uintptr(miblen), - 0, - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - return nil, length, err - } - - count := int32(length / uint64(sizeOfKinfoProc)) - mib = []int32{CTLKern, KernProc, op, arg, sizeOfKinfoProc, count} - mibptr = unsafe.Pointer(&mib[0]) - miblen = uint64(len(mib)) - // get proc info itself - buf := make([]byte, length) - _, _, err = unix.Syscall6( - unix.SYS___SYSCTL, - uintptr(mibptr), - uintptr(miblen), - uintptr(unsafe.Pointer(&buf[0])), - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if err != 0 { - return buf, length, err - } - - return buf, length, nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_openbsd_386.go b/vendor/github.com/shirou/gopsutil/process/process_openbsd_386.go deleted file mode 100644 index b89fb8dc2..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_openbsd_386.go +++ /dev/null @@ -1,201 +0,0 @@ -// +build openbsd -// +build 386 -// Code generated by cmd/cgo -godefs; DO NOT EDIT. -// cgo -godefs process/types_openbsd.go - -package process - -const ( - CTLKern = 1 - KernProc = 66 - KernProcAll = 0 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 55 - KernProcArgv = 1 - KernProcEnv = 3 -) - -const ( - ArgMax = 256 * 1024 -) - -const ( - sizeofPtr = 0x4 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x4 - sizeofLongLong = 0x8 -) - -const ( - sizeOfKinfoVmentry = 0x38 - sizeOfKinfoProc = 0x264 -) - -const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SDEAD = 6 - SONPROC = 7 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int32 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int32 -} - -type Timeval struct { - Sec int64 - Usec int32 -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int32 - Ixrss int32 - Idrss int32 - Isrss int32 - Minflt int32 - Majflt int32 - Nswap int32 - Inblock int32 - Oublock int32 - Msgsnd int32 - Msgrcv int32 - Nsignals int32 - Nvcsw int32 - Nivcsw int32 -} - -type Rlimit struct { - Cur uint64 - Max uint64 -} - -type KinfoProc struct { - Forw uint64 - Back uint64 - Paddr uint64 - Addr uint64 - Fd uint64 - Stats uint64 - Limit uint64 - Vmspace uint64 - Sigacts uint64 - Sess uint64 - Tsess uint64 - Ru uint64 - Eflag int32 - Exitsig int32 - Flag int32 - Pid int32 - Ppid int32 - Sid int32 - X_pgid int32 - Tpgid int32 - Uid uint32 - Ruid uint32 - Gid uint32 - Rgid uint32 - Groups [16]uint32 - Ngroups int16 - Jobc int16 - Tdev uint32 - Estcpu uint32 - Rtime_sec uint32 - Rtime_usec uint32 - Cpticks int32 - Pctcpu uint32 - Swtime uint32 - Slptime uint32 - Schedflags int32 - Uticks uint64 - Sticks uint64 - Iticks uint64 - Tracep uint64 - Traceflag int32 - Holdcnt int32 - Siglist int32 - Sigmask uint32 - Sigignore uint32 - Sigcatch uint32 - Stat int8 - Priority uint8 - Usrpri uint8 - Nice uint8 - Xstat uint16 - Acflag uint16 - Comm [24]int8 - Wmesg [8]int8 - Wchan uint64 - Login [32]int8 - Vm_rssize int32 - Vm_tsize int32 - Vm_dsize int32 - Vm_ssize int32 - Uvalid int64 - Ustart_sec uint64 - Ustart_usec uint32 - Uutime_sec uint32 - Uutime_usec uint32 - Ustime_sec uint32 - Ustime_usec uint32 - Uru_maxrss uint64 - Uru_ixrss uint64 - Uru_idrss uint64 - Uru_isrss uint64 - Uru_minflt uint64 - Uru_majflt uint64 - Uru_nswap uint64 - Uru_inblock uint64 - Uru_oublock uint64 - Uru_msgsnd uint64 - Uru_msgrcv uint64 - Uru_nsignals uint64 - Uru_nvcsw uint64 - Uru_nivcsw uint64 - Uctime_sec uint32 - Uctime_usec uint32 - Psflags int32 - Spare int32 - Svuid uint32 - Svgid uint32 - Emul [8]int8 - Rlim_rss_cur uint64 - Cpuid uint64 - Vm_map_size uint64 - Tid int32 - Rtableid uint32 -} - -type Priority struct{} - -type KinfoVmentry struct { - Start uint32 - End uint32 - Guard uint32 - Fspace uint32 - Fspace_augment uint32 - Offset uint64 - Wired_count int32 - Etype int32 - Protection int32 - Max_protection int32 - Advice int32 - Inheritance int32 - Flags uint8 - Pad_cgo_0 [3]byte -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_openbsd_amd64.go b/vendor/github.com/shirou/gopsutil/process/process_openbsd_amd64.go deleted file mode 100644 index 8607422b5..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_openbsd_amd64.go +++ /dev/null @@ -1,200 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_openbsd.go - -package process - -const ( - CTLKern = 1 - KernProc = 66 - KernProcAll = 0 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 55 - KernProcArgv = 1 - KernProcEnv = 3 -) - -const ( - ArgMax = 256 * 1024 -) - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -const ( - sizeOfKinfoVmentry = 0x50 - sizeOfKinfoProc = 0x268 -) - -const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SDEAD = 6 - SONPROC = 7 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int64 -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur uint64 - Max uint64 -} - -type KinfoProc struct { - Forw uint64 - Back uint64 - Paddr uint64 - Addr uint64 - Fd uint64 - Stats uint64 - Limit uint64 - Vmspace uint64 - Sigacts uint64 - Sess uint64 - Tsess uint64 - Ru uint64 - Eflag int32 - Exitsig int32 - Flag int32 - Pid int32 - Ppid int32 - Sid int32 - X_pgid int32 - Tpgid int32 - Uid uint32 - Ruid uint32 - Gid uint32 - Rgid uint32 - Groups [16]uint32 - Ngroups int16 - Jobc int16 - Tdev uint32 - Estcpu uint32 - Rtime_sec uint32 - Rtime_usec uint32 - Cpticks int32 - Pctcpu uint32 - Swtime uint32 - Slptime uint32 - Schedflags int32 - Uticks uint64 - Sticks uint64 - Iticks uint64 - Tracep uint64 - Traceflag int32 - Holdcnt int32 - Siglist int32 - Sigmask uint32 - Sigignore uint32 - Sigcatch uint32 - Stat int8 - Priority uint8 - Usrpri uint8 - Nice uint8 - Xstat uint16 - Acflag uint16 - Comm [24]int8 - Wmesg [8]int8 - Wchan uint64 - Login [32]int8 - Vm_rssize int32 - Vm_tsize int32 - Vm_dsize int32 - Vm_ssize int32 - Uvalid int64 - Ustart_sec uint64 - Ustart_usec uint32 - Uutime_sec uint32 - Uutime_usec uint32 - Ustime_sec uint32 - Ustime_usec uint32 - Pad_cgo_0 [4]byte - Uru_maxrss uint64 - Uru_ixrss uint64 - Uru_idrss uint64 - Uru_isrss uint64 - Uru_minflt uint64 - Uru_majflt uint64 - Uru_nswap uint64 - Uru_inblock uint64 - Uru_oublock uint64 - Uru_msgsnd uint64 - Uru_msgrcv uint64 - Uru_nsignals uint64 - Uru_nvcsw uint64 - Uru_nivcsw uint64 - Uctime_sec uint32 - Uctime_usec uint32 - Psflags int32 - Spare int32 - Svuid uint32 - Svgid uint32 - Emul [8]int8 - Rlim_rss_cur uint64 - Cpuid uint64 - Vm_map_size uint64 - Tid int32 - Rtableid uint32 -} - -type Priority struct{} - -type KinfoVmentry struct { - Start uint64 - End uint64 - Guard uint64 - Fspace uint64 - Fspace_augment uint64 - Offset uint64 - Wired_count int32 - Etype int32 - Protection int32 - Max_protection int32 - Advice int32 - Inheritance int32 - Flags uint8 - Pad_cgo_0 [7]byte -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_openbsd_arm64.go b/vendor/github.com/shirou/gopsutil/process/process_openbsd_arm64.go deleted file mode 100644 index 2d97fbc5c..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_openbsd_arm64.go +++ /dev/null @@ -1,202 +0,0 @@ -// +build openbsd -// +build arm64 -// Code generated by cmd/cgo -godefs; DO NOT EDIT. -// cgo -godefs process/types_openbsd.go - -package process - -const ( - CTLKern = 1 - KernProc = 66 - KernProcAll = 0 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 55 - KernProcArgv = 1 - KernProcEnv = 3 -) - -const ( - ArgMax = 256 * 1024 -) - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -const ( - sizeOfKinfoVmentry = 0x50 - sizeOfKinfoProc = 0x270 -) - -const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SDEAD = 6 - SONPROC = 7 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int64 -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur uint64 - Max uint64 -} - -type KinfoProc struct { - Forw uint64 - Back uint64 - Paddr uint64 - Addr uint64 - Fd uint64 - Stats uint64 - Limit uint64 - Vmspace uint64 - Sigacts uint64 - Sess uint64 - Tsess uint64 - Ru uint64 - Eflag int32 - Exitsig int32 - Flag int32 - Pid int32 - Ppid int32 - Sid int32 - X_pgid int32 - Tpgid int32 - Uid uint32 - Ruid uint32 - Gid uint32 - Rgid uint32 - Groups [16]uint32 - Ngroups int16 - Jobc int16 - Tdev uint32 - Estcpu uint32 - Rtime_sec uint32 - Rtime_usec uint32 - Cpticks int32 - Pctcpu uint32 - Swtime uint32 - Slptime uint32 - Schedflags int32 - Uticks uint64 - Sticks uint64 - Iticks uint64 - Tracep uint64 - Traceflag int32 - Holdcnt int32 - Siglist int32 - Sigmask uint32 - Sigignore uint32 - Sigcatch uint32 - Stat int8 - Priority uint8 - Usrpri uint8 - Nice uint8 - Xstat uint16 - Acflag uint16 - Comm [24]int8 - Wmesg [8]uint8 - Wchan uint64 - Login [32]uint8 - Vm_rssize int32 - Vm_tsize int32 - Vm_dsize int32 - Vm_ssize int32 - Uvalid int64 - Ustart_sec uint64 - Ustart_usec uint32 - Uutime_sec uint32 - Uutime_usec uint32 - Ustime_sec uint32 - Ustime_usec uint32 - Uru_maxrss uint64 - Uru_ixrss uint64 - Uru_idrss uint64 - Uru_isrss uint64 - Uru_minflt uint64 - Uru_majflt uint64 - Uru_nswap uint64 - Uru_inblock uint64 - Uru_oublock uint64 - Uru_msgsnd uint64 - Uru_msgrcv uint64 - Uru_nsignals uint64 - Uru_nvcsw uint64 - Uru_nivcsw uint64 - Uctime_sec uint32 - Uctime_usec uint32 - Psflags uint32 - Spare int32 - Svuid uint32 - Svgid uint32 - Emul [8]uint8 - Rlim_rss_cur uint64 - Cpuid uint64 - Vm_map_size uint64 - Tid int32 - Rtableid uint32 - Pledge uint64 -} - -type Priority struct{} - -type KinfoVmentry struct { - Start uint64 - End uint64 - Guard uint64 - Fspace uint64 - Fspace_augment uint64 - Offset uint64 - Wired_count int32 - Etype int32 - Protection int32 - Max_protection int32 - Advice int32 - Inheritance int32 - Flags uint8 - Pad_cgo_0 [7]byte -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_posix.go b/vendor/github.com/shirou/gopsutil/process/process_posix.go deleted file mode 100644 index 45e6d1389..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_posix.go +++ /dev/null @@ -1,180 +0,0 @@ -// +build linux freebsd openbsd darwin solaris - -package process - -import ( - "context" - "fmt" - "os" - "os/user" - "path/filepath" - "strconv" - "strings" - "syscall" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/unix" -) - -// POSIX -func getTerminalMap() (map[uint64]string, error) { - ret := make(map[uint64]string) - var termfiles []string - - d, err := os.Open("/dev") - if err != nil { - return nil, err - } - defer d.Close() - - devnames, err := d.Readdirnames(-1) - if err != nil { - return nil, err - } - for _, devname := range devnames { - if strings.HasPrefix(devname, "/dev/tty") { - termfiles = append(termfiles, "/dev/tty/"+devname) - } - } - - var ptsnames []string - ptsd, err := os.Open("/dev/pts") - if err != nil { - ptsnames, _ = filepath.Glob("/dev/ttyp*") - if ptsnames == nil { - return nil, err - } - } - defer ptsd.Close() - - if ptsnames == nil { - defer ptsd.Close() - ptsnames, err = ptsd.Readdirnames(-1) - if err != nil { - return nil, err - } - for _, ptsname := range ptsnames { - termfiles = append(termfiles, "/dev/pts/"+ptsname) - } - } else { - termfiles = ptsnames - } - - for _, name := range termfiles { - stat := unix.Stat_t{} - if err = unix.Stat(name, &stat); err != nil { - return nil, err - } - rdev := uint64(stat.Rdev) - ret[rdev] = strings.Replace(name, "/dev", "", -1) - } - return ret, nil -} - -// isMount is a port of python's os.path.ismount() -// https://github.com/python/cpython/blob/08ff4369afca84587b1c82034af4e9f64caddbf2/Lib/posixpath.py#L186-L216 -// https://docs.python.org/3/library/os.path.html#os.path.ismount -func isMount(path string) bool { - // Check symlinkness with os.Lstat; unix.DT_LNK is not portable - fileInfo, err := os.Lstat(path) - if err != nil { - return false - } - if fileInfo.Mode() & os.ModeSymlink != 0 { - return false - } - var stat1 unix.Stat_t - if err := unix.Lstat(path, &stat1); err != nil { - return false - } - parent := filepath.Join(path, "..") - var stat2 unix.Stat_t - if err := unix.Lstat(parent, &stat2); err != nil { - return false - } - return stat1.Dev != stat2.Dev || stat1.Ino == stat2.Ino -} - -func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) { - if pid <= 0 { - return false, fmt.Errorf("invalid pid %v", pid) - } - proc, err := os.FindProcess(int(pid)) - if err != nil { - return false, err - } - - if isMount(common.HostProc()) { // if //proc exists and is mounted, check if //proc/ folder exists - _, err := os.Stat(common.HostProc(strconv.Itoa(int(pid)))) - if os.IsNotExist(err) { - return false, nil - } - return err == nil, err - } - - // procfs does not exist or is not mounted, check PID existence by signalling the pid - err = proc.Signal(syscall.Signal(0)) - if err == nil { - return true, nil - } - if err.Error() == "os: process already finished" { - return false, nil - } - errno, ok := err.(syscall.Errno) - if !ok { - return false, err - } - switch errno { - case syscall.ESRCH: - return false, nil - case syscall.EPERM: - return true, nil - } - - return false, err -} - -func (p *Process) SendSignalWithContext(ctx context.Context, sig syscall.Signal) error { - process, err := os.FindProcess(int(p.Pid)) - if err != nil { - return err - } - - err = process.Signal(sig) - if err != nil { - return err - } - - return nil -} - -func (p *Process) SuspendWithContext(ctx context.Context) error { - return p.SendSignalWithContext(ctx, unix.SIGSTOP) -} - -func (p *Process) ResumeWithContext(ctx context.Context) error { - return p.SendSignalWithContext(ctx, unix.SIGCONT) -} - -func (p *Process) TerminateWithContext(ctx context.Context) error { - return p.SendSignalWithContext(ctx, unix.SIGTERM) -} - -func (p *Process) KillWithContext(ctx context.Context) error { - return p.SendSignalWithContext(ctx, unix.SIGKILL) -} - -func (p *Process) UsernameWithContext(ctx context.Context) (string, error) { - uids, err := p.UidsWithContext(ctx) - if err != nil { - return "", err - } - if len(uids) > 0 { - u, err := user.LookupId(strconv.Itoa(int(uids[0]))) - if err != nil { - return "", err - } - return u.Username, nil - } - return "", nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_solaris.go b/vendor/github.com/shirou/gopsutil/process/process_solaris.go deleted file mode 100644 index f24a327b1..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_solaris.go +++ /dev/null @@ -1,313 +0,0 @@ -package process - -import ( - "bytes" - "context" - "io/ioutil" - "os" - "strconv" - "strings" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/net" -) - -type MemoryMapsStat struct { - Path string `json:"path"` - Rss uint64 `json:"rss"` - Size uint64 `json:"size"` - Pss uint64 `json:"pss"` - SharedClean uint64 `json:"sharedClean"` - SharedDirty uint64 `json:"sharedDirty"` - PrivateClean uint64 `json:"privateClean"` - PrivateDirty uint64 `json:"privateDirty"` - Referenced uint64 `json:"referenced"` - Anonymous uint64 `json:"anonymous"` - Swap uint64 `json:"swap"` -} - -type MemoryInfoExStat struct { -} - -func pidsWithContext(ctx context.Context) ([]int32, error) { - return readPidsFromDir(common.HostProc()) -} - -func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - out := []*Process{} - - pids, err := PidsWithContext(ctx) - if err != nil { - return out, err - } - - for _, pid := range pids { - p, err := NewProcessWithContext(ctx, pid) - if err != nil { - continue - } - out = append(out, p) - } - - return out, nil -} - -func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) NameWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - exe, err := p.fillFromPathAOutWithContext(ctx) - if os.IsNotExist(err) { - exe, err = p.fillFromExecnameWithContext(ctx) - } - return exe, err -} - -func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { - return p.fillFromCmdlineWithContext(ctx) -} - -func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { - return p.fillSliceFromCmdlineWithContext(ctx) -} - -func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) CwdWithContext(ctx context.Context) (string, error) { - return p.fillFromPathCwdWithContext(ctx) -} - -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { - return false, common.ErrNotImplementedError -} - -func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) NiceWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { - _, fnames, err := p.fillFromfdListWithContext(ctx) - return int32(len(fnames)), err -} - -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) ThreadsWithContext(ctx context.Context) (map[int32]*cpu.TimesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) { - return nil, common.ErrNotImplementedError -} - -/** -** Internal functions -**/ - -func (p *Process) fillFromfdListWithContext(ctx context.Context) (string, []string, error) { - pid := p.Pid - statPath := common.HostProc(strconv.Itoa(int(pid)), "fd") - d, err := os.Open(statPath) - if err != nil { - return statPath, []string{}, err - } - defer d.Close() - fnames, err := d.Readdirnames(-1) - return statPath, fnames, err -} - -func (p *Process) fillFromPathCwdWithContext(ctx context.Context) (string, error) { - pid := p.Pid - cwdPath := common.HostProc(strconv.Itoa(int(pid)), "path", "cwd") - cwd, err := os.Readlink(cwdPath) - if err != nil { - return "", err - } - return cwd, nil -} - -func (p *Process) fillFromPathAOutWithContext(ctx context.Context) (string, error) { - pid := p.Pid - cwdPath := common.HostProc(strconv.Itoa(int(pid)), "path", "a.out") - exe, err := os.Readlink(cwdPath) - if err != nil { - return "", err - } - return exe, nil -} - -func (p *Process) fillFromExecnameWithContext(ctx context.Context) (string, error) { - pid := p.Pid - execNamePath := common.HostProc(strconv.Itoa(int(pid)), "execname") - exe, err := ioutil.ReadFile(execNamePath) - if err != nil { - return "", err - } - return string(exe), nil -} - -func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error) { - pid := p.Pid - cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) - if err != nil { - return "", err - } - ret := strings.FieldsFunc(string(cmdline), func(r rune) bool { - if r == '\u0000' { - return true - } - return false - }) - - return strings.Join(ret, " "), nil -} - -func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string, error) { - pid := p.Pid - cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) - if err != nil { - return nil, err - } - if len(cmdline) == 0 { - return nil, nil - } - if cmdline[len(cmdline)-1] == 0 { - cmdline = cmdline[:len(cmdline)-1] - } - parts := bytes.Split(cmdline, []byte{0}) - var strParts []string - for _, p := range parts { - strParts = append(strParts, string(p)) - } - - return strParts, nil -} - -func readPidsFromDir(path string) ([]int32, error) { - var ret []int32 - - d, err := os.Open(path) - if err != nil { - return nil, err - } - defer d.Close() - - fnames, err := d.Readdirnames(-1) - if err != nil { - return nil, err - } - for _, fname := range fnames { - pid, err := strconv.ParseInt(fname, 10, 32) - if err != nil { - // if not numeric name, just skip - continue - } - ret = append(ret, int32(pid)) - } - - return ret, nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_windows.go b/vendor/github.com/shirou/gopsutil/process/process_windows.go deleted file mode 100644 index e410a0eb1..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_windows.go +++ /dev/null @@ -1,1194 +0,0 @@ -// +build windows - -package process - -import ( - "bufio" - "context" - "errors" - "fmt" - "io" - "os" - "reflect" - "strings" - "syscall" - "time" - "unicode/utf16" - "unsafe" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/net" - "golang.org/x/sys/windows" -) - -var ( - modntdll = windows.NewLazySystemDLL("ntdll.dll") - procNtResumeProcess = modntdll.NewProc("NtResumeProcess") - procNtSuspendProcess = modntdll.NewProc("NtSuspendProcess") - - modpsapi = windows.NewLazySystemDLL("psapi.dll") - procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo") - procGetProcessImageFileNameW = modpsapi.NewProc("GetProcessImageFileNameW") - - advapi32 = windows.NewLazySystemDLL("advapi32.dll") - procLookupPrivilegeValue = advapi32.NewProc("LookupPrivilegeValueW") - procAdjustTokenPrivileges = advapi32.NewProc("AdjustTokenPrivileges") - - procQueryFullProcessImageNameW = common.Modkernel32.NewProc("QueryFullProcessImageNameW") - procGetPriorityClass = common.Modkernel32.NewProc("GetPriorityClass") - procGetProcessIoCounters = common.Modkernel32.NewProc("GetProcessIoCounters") - procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo") - - processorArchitecture uint -) - -const processQueryInformation = windows.PROCESS_QUERY_LIMITED_INFORMATION - -type SystemProcessInformation struct { - NextEntryOffset uint64 - NumberOfThreads uint64 - Reserved1 [48]byte - Reserved2 [3]byte - UniqueProcessID uintptr - Reserved3 uintptr - HandleCount uint64 - Reserved4 [4]byte - Reserved5 [11]byte - PeakPagefileUsage uint64 - PrivatePageCount uint64 - Reserved6 [6]uint64 -} - -type systemProcessorInformation struct { - ProcessorArchitecture uint16 - ProcessorLevel uint16 - ProcessorRevision uint16 - Reserved uint16 - ProcessorFeatureBits uint16 -} - -type systemInfo struct { - wProcessorArchitecture uint16 - wReserved uint16 - dwPageSize uint32 - lpMinimumApplicationAddress uintptr - lpMaximumApplicationAddress uintptr - dwActiveProcessorMask uintptr - dwNumberOfProcessors uint32 - dwProcessorType uint32 - dwAllocationGranularity uint32 - wProcessorLevel uint16 - wProcessorRevision uint16 -} - -// Memory_info_ex is different between OSes -type MemoryInfoExStat struct { -} - -type MemoryMapsStat struct { -} - -// ioCounters is an equivalent representation of IO_COUNTERS in the Windows API. -// https://docs.microsoft.com/windows/win32/api/winnt/ns-winnt-io_counters -type ioCounters struct { - ReadOperationCount uint64 - WriteOperationCount uint64 - OtherOperationCount uint64 - ReadTransferCount uint64 - WriteTransferCount uint64 - OtherTransferCount uint64 -} - -type processBasicInformation32 struct { - Reserved1 uint32 - PebBaseAddress uint32 - Reserved2 uint32 - Reserved3 uint32 - UniqueProcessId uint32 - Reserved4 uint32 -} - -type processBasicInformation64 struct { - Reserved1 uint64 - PebBaseAddress uint64 - Reserved2 uint64 - Reserved3 uint64 - UniqueProcessId uint64 - Reserved4 uint64 -} - -type processEnvironmentBlock32 struct { - Reserved1 [2]uint8 - BeingDebugged uint8 - Reserved2 uint8 - Reserved3 [2]uint32 - Ldr uint32 - ProcessParameters uint32 - // More fields which we don't use so far -} - -type processEnvironmentBlock64 struct { - Reserved1 [2]uint8 - BeingDebugged uint8 - Reserved2 uint8 - _ [4]uint8 // padding, since we are 64 bit, the next pointer is 64 bit aligned (when compiling for 32 bit, this is not the case without manual padding) - Reserved3 [2]uint64 - Ldr uint64 - ProcessParameters uint64 - // More fields which we don't use so far -} - -type rtlUserProcessParameters32 struct { - Reserved1 [16]uint8 - ConsoleHandle uint32 - ConsoleFlags uint32 - StdInputHandle uint32 - StdOutputHandle uint32 - StdErrorHandle uint32 - CurrentDirectoryPathNameLength uint16 - _ uint16 // Max Length - CurrentDirectoryPathAddress uint32 - CurrentDirectoryHandle uint32 - DllPathNameLength uint16 - _ uint16 // Max Length - DllPathAddress uint32 - ImagePathNameLength uint16 - _ uint16 // Max Length - ImagePathAddress uint32 - CommandLineLength uint16 - _ uint16 // Max Length - CommandLineAddress uint32 - EnvironmentAddress uint32 - // More fields which we don't use so far -} - -type rtlUserProcessParameters64 struct { - Reserved1 [16]uint8 - ConsoleHandle uint64 - ConsoleFlags uint64 - StdInputHandle uint64 - StdOutputHandle uint64 - StdErrorHandle uint64 - CurrentDirectoryPathNameLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - CurrentDirectoryPathAddress uint64 - CurrentDirectoryHandle uint64 - DllPathNameLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - DllPathAddress uint64 - ImagePathNameLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - ImagePathAddress uint64 - CommandLineLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - CommandLineAddress uint64 - EnvironmentAddress uint64 - // More fields which we don't use so far -} - -type winLUID struct { - LowPart winDWord - HighPart winLong -} - -// LUID_AND_ATTRIBUTES -type winLUIDAndAttributes struct { - Luid winLUID - Attributes winDWord -} - -// TOKEN_PRIVILEGES -type winTokenPriviledges struct { - PrivilegeCount winDWord - Privileges [1]winLUIDAndAttributes -} - -type winLong int32 -type winDWord uint32 - -func init() { - var systemInfo systemInfo - - procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo))) - processorArchitecture = uint(systemInfo.wProcessorArchitecture) - - // enable SeDebugPrivilege https://github.com/midstar/proci/blob/6ec79f57b90ba3d9efa2a7b16ef9c9369d4be875/proci_windows.go#L80-L119 - handle, err := syscall.GetCurrentProcess() - if err != nil { - return - } - - var token syscall.Token - err = syscall.OpenProcessToken(handle, 0x0028, &token) - if err != nil { - return - } - defer token.Close() - - tokenPriviledges := winTokenPriviledges{PrivilegeCount: 1} - lpName := syscall.StringToUTF16("SeDebugPrivilege") - ret, _, _ := procLookupPrivilegeValue.Call( - 0, - uintptr(unsafe.Pointer(&lpName[0])), - uintptr(unsafe.Pointer(&tokenPriviledges.Privileges[0].Luid))) - if ret == 0 { - return - } - - tokenPriviledges.Privileges[0].Attributes = 0x00000002 // SE_PRIVILEGE_ENABLED - - procAdjustTokenPrivileges.Call( - uintptr(token), - 0, - uintptr(unsafe.Pointer(&tokenPriviledges)), - uintptr(unsafe.Sizeof(tokenPriviledges)), - 0, - 0) -} - -func pidsWithContext(ctx context.Context) ([]int32, error) { - // inspired by https://gist.github.com/henkman/3083408 - // and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329 - var ret []int32 - var read uint32 = 0 - var psSize uint32 = 1024 - const dwordSize uint32 = 4 - - for { - ps := make([]uint32, psSize) - if err := windows.EnumProcesses(ps, &read); err != nil { - return nil, err - } - if uint32(len(ps)) == read { // ps buffer was too small to host every results, retry with a bigger one - psSize += 1024 - continue - } - for _, pid := range ps[:read/dwordSize] { - ret = append(ret, int32(pid)) - } - return ret, nil - - } - -} - -func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) { - if pid == 0 { // special case for pid 0 System Idle Process - return true, nil - } - if pid < 0 { - return false, fmt.Errorf("invalid pid %v", pid) - } - if pid%4 != 0 { - // OpenProcess will succeed even on non-existing pid here https://devblogs.microsoft.com/oldnewthing/20080606-00/?p=22043 - // so we list every pid just to be sure and be future-proof - pids, err := PidsWithContext(ctx) - if err != nil { - return false, err - } - for _, i := range pids { - if i == pid { - return true, err - } - } - return false, err - } - const STILL_ACTIVE = 259 // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess - h, err := windows.OpenProcess(processQueryInformation, false, uint32(pid)) - if err == windows.ERROR_ACCESS_DENIED { - return true, nil - } - if err == windows.ERROR_INVALID_PARAMETER { - return false, nil - } - if err != nil { - return false, err - } - defer syscall.CloseHandle(syscall.Handle(h)) - var exitCode uint32 - err = windows.GetExitCodeProcess(h, &exitCode) - return exitCode == STILL_ACTIVE, err -} - -func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - // if cached already, return from cache - cachedPpid := p.getPpid() - if cachedPpid != 0 { - return cachedPpid, nil - } - - ppid, _, _, err := getFromSnapProcess(p.Pid) - if err != nil { - return 0, err - } - - // no errors and not cached already, so cache it - p.setPpid(ppid) - - return ppid, nil -} - -func (p *Process) NameWithContext(ctx context.Context) (string, error) { - ppid, _, name, err := getFromSnapProcess(p.Pid) - if err != nil { - return "", fmt.Errorf("could not get Name: %s", err) - } - - // if no errors and not cached already, cache ppid - p.parent = ppid - if 0 == p.getPpid() { - p.setPpid(ppid) - } - - return name, nil -} - -func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - c, err := windows.OpenProcess(processQueryInformation, false, uint32(p.Pid)) - if err != nil { - return "", err - } - defer windows.CloseHandle(c) - buf := make([]uint16, syscall.MAX_LONG_PATH) - size := uint32(syscall.MAX_LONG_PATH) - if err := procQueryFullProcessImageNameW.Find(); err == nil { // Vista+ - ret, _, err := procQueryFullProcessImageNameW.Call( - uintptr(c), - uintptr(0), - uintptr(unsafe.Pointer(&buf[0])), - uintptr(unsafe.Pointer(&size))) - if ret == 0 { - return "", err - } - return windows.UTF16ToString(buf[:]), nil - } - // XP fallback - ret, _, err := procGetProcessImageFileNameW.Call(uintptr(c), uintptr(unsafe.Pointer(&buf[0])), uintptr(size)) - if ret == 0 { - return "", err - } - return common.ConvertDOSPath(windows.UTF16ToString(buf[:])), nil -} - -func (p *Process) CmdlineWithContext(_ context.Context) (string, error) { - cmdline, err := getProcessCommandLine(p.Pid) - if err != nil { - return "", fmt.Errorf("could not get CommandLine: %s", err) - } - return cmdline, nil -} - -func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { - cmdline, err := p.CmdlineWithContext(ctx) - if err != nil { - return nil, err - } - return strings.Split(cmdline, " "), nil -} - -func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - ru, err := getRusage(p.Pid) - if err != nil { - return 0, fmt.Errorf("could not get CreationDate: %s", err) - } - - return ru.CreationTime.Nanoseconds() / 1000000, nil -} - -func (p *Process) CwdWithContext(_ context.Context) (string, error) { - h, err := windows.OpenProcess(processQueryInformation|windows.PROCESS_VM_READ, false, uint32(p.Pid)) - if err == windows.ERROR_ACCESS_DENIED || err == windows.ERROR_INVALID_PARAMETER { - return "", nil - } - if err != nil { - return "", err - } - defer syscall.CloseHandle(syscall.Handle(h)) - - procIs32Bits := is32BitProcess(h) - - if procIs32Bits { - userProcParams, err := getUserProcessParams32(h) - if err != nil { - return "", err - } - if userProcParams.CurrentDirectoryPathNameLength > 0 { - cwd := readProcessMemory(syscall.Handle(h), procIs32Bits, uint64(userProcParams.CurrentDirectoryPathAddress), uint(userProcParams.CurrentDirectoryPathNameLength)) - if len(cwd) != int(userProcParams.CurrentDirectoryPathAddress) { - return "", errors.New("cannot read current working directory") - } - - return convertUTF16ToString(cwd), nil - } - } else { - userProcParams, err := getUserProcessParams64(h) - if err != nil { - return "", err - } - if userProcParams.CurrentDirectoryPathNameLength > 0 { - cwd := readProcessMemory(syscall.Handle(h), procIs32Bits, userProcParams.CurrentDirectoryPathAddress, uint(userProcParams.CurrentDirectoryPathNameLength)) - if len(cwd) != int(userProcParams.CurrentDirectoryPathNameLength) { - return "", errors.New("cannot read current working directory") - } - - return convertUTF16ToString(cwd), nil - } - } - - //if we reach here, we have no cwd - return "", nil -} - -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - ppid, err := p.PpidWithContext(ctx) - if err != nil { - return nil, fmt.Errorf("could not get ParentProcessID: %s", err) - } - - return NewProcessWithContext(ctx, ppid) -} - -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { - return false, common.ErrNotImplementedError -} - -func (p *Process) UsernameWithContext(ctx context.Context) (string, error) { - pid := p.Pid - c, err := windows.OpenProcess(processQueryInformation, false, uint32(pid)) - if err != nil { - return "", err - } - defer windows.CloseHandle(c) - - var token syscall.Token - err = syscall.OpenProcessToken(syscall.Handle(c), syscall.TOKEN_QUERY, &token) - if err != nil { - return "", err - } - defer token.Close() - tokenUser, err := token.GetTokenUser() - if err != nil { - return "", err - } - - user, domain, _, err := tokenUser.User.Sid.LookupAccount("") - return domain + "\\" + user, err -} - -func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError -} - -// priorityClasses maps a win32 priority class to its WMI equivalent Win32_Process.Priority -// https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getpriorityclass -// https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-process -var priorityClasses = map[int]int32{ - 0x00008000: 10, // ABOVE_NORMAL_PRIORITY_CLASS - 0x00004000: 6, // BELOW_NORMAL_PRIORITY_CLASS - 0x00000080: 13, // HIGH_PRIORITY_CLASS - 0x00000040: 4, // IDLE_PRIORITY_CLASS - 0x00000020: 8, // NORMAL_PRIORITY_CLASS - 0x00000100: 24, // REALTIME_PRIORITY_CLASS -} - -func (p *Process) NiceWithContext(ctx context.Context) (int32, error) { - c, err := windows.OpenProcess(processQueryInformation, false, uint32(p.Pid)) - if err != nil { - return 0, err - } - defer windows.CloseHandle(c) - ret, _, err := procGetPriorityClass.Call(uintptr(c)) - if ret == 0 { - return 0, err - } - priority, ok := priorityClasses[int(ret)] - if !ok { - return 0, fmt.Errorf("unknown priority class %v", ret) - } - return priority, nil -} - -func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - c, err := windows.OpenProcess(processQueryInformation, false, uint32(p.Pid)) - if err != nil { - return nil, err - } - defer windows.CloseHandle(c) - var ioCounters ioCounters - ret, _, err := procGetProcessIoCounters.Call(uintptr(c), uintptr(unsafe.Pointer(&ioCounters))) - if ret == 0 { - return nil, err - } - stats := &IOCountersStat{ - ReadCount: ioCounters.ReadOperationCount, - ReadBytes: ioCounters.ReadTransferCount, - WriteCount: ioCounters.WriteOperationCount, - WriteBytes: ioCounters.WriteTransferCount, - } - - return stats, nil -} - -func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError -} - -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - ppid, ret, _, err := getFromSnapProcess(p.Pid) - if err != nil { - return 0, err - } - - // if no errors and not cached already, cache ppid - p.parent = ppid - if 0 == p.getPpid() { - p.setPpid(ppid) - } - - return ret, nil -} - -func (p *Process) ThreadsWithContext(ctx context.Context) (map[int32]*cpu.TimesStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - sysTimes, err := getProcessCPUTimes(p.Pid) - if err != nil { - return nil, err - } - - // User and kernel times are represented as a FILETIME structure - // which contains a 64-bit value representing the number of - // 100-nanosecond intervals since January 1, 1601 (UTC): - // http://msdn.microsoft.com/en-us/library/ms724284(VS.85).aspx - // To convert it into a float representing the seconds that the - // process has executed in user/kernel mode I borrowed the code - // below from psutil's _psutil_windows.c, and in turn from Python's - // Modules/posixmodule.c - - user := float64(sysTimes.UserTime.HighDateTime)*429.4967296 + float64(sysTimes.UserTime.LowDateTime)*1e-7 - kernel := float64(sysTimes.KernelTime.HighDateTime)*429.4967296 + float64(sysTimes.KernelTime.LowDateTime)*1e-7 - - return &cpu.TimesStat{ - User: user, - System: kernel, - }, nil -} - -func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - mem, err := getMemoryInfo(p.Pid) - if err != nil { - return nil, err - } - - ret := &MemoryInfoStat{ - RSS: uint64(mem.WorkingSetSize), - VMS: uint64(mem.PagefileUsage), - } - - return ret, nil -} - -func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { - out := []*Process{} - snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, uint32(0)) - if err != nil { - return out, err - } - defer windows.CloseHandle(snap) - var pe32 windows.ProcessEntry32 - pe32.Size = uint32(unsafe.Sizeof(pe32)) - if err := windows.Process32First(snap, &pe32); err != nil { - return out, err - } - for { - if pe32.ParentProcessID == uint32(p.Pid) { - p, err := NewProcessWithContext(ctx, int32(pe32.ProcessID)) - if err == nil { - out = append(out, p) - } - } - if err = windows.Process32Next(snap, &pe32); err != nil { - break - } - } - return out, nil -} - -func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) { - files := make([]OpenFilesStat, 0) - fileExists := make(map[string]bool) - - process, err := windows.OpenProcess(common.ProcessQueryInformation, false, uint32(p.Pid)) - if err != nil { - return nil, err - } - - buffer := make([]byte, 1024) - var size uint32 - - st := common.CallWithExpandingBuffer( - func() common.NtStatus { - return common.NtQuerySystemInformation( - common.SystemExtendedHandleInformationClass, - &buffer[0], - uint32(len(buffer)), - &size, - ) - }, - &buffer, - &size, - ) - if st.IsError() { - return nil, st.Error() - } - - handlesList := (*common.SystemExtendedHandleInformation)(unsafe.Pointer(&buffer[0])) - handles := make([]common.SystemExtendedHandleTableEntryInformation, int(handlesList.NumberOfHandles)) - hdr := (*reflect.SliceHeader)(unsafe.Pointer(&handles)) - hdr.Data = uintptr(unsafe.Pointer(&handlesList.Handles[0])) - - currentProcess, err := windows.GetCurrentProcess() - if err != nil { - return nil, err - } - - for _, handle := range handles { - var file uintptr - if int32(handle.UniqueProcessId) != p.Pid { - continue - } - if windows.DuplicateHandle(process, windows.Handle(handle.HandleValue), currentProcess, (*windows.Handle)(&file), - 0, true, windows.DUPLICATE_SAME_ACCESS) != nil { - continue - } - fileType, _ := windows.GetFileType(windows.Handle(file)) - if fileType != windows.FILE_TYPE_DISK { - continue - } - - var fileName string - ch := make(chan struct{}) - - go func() { - var buf [syscall.MAX_LONG_PATH]uint16 - n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0) - if err != nil { - return - } - - fileName = string(utf16.Decode(buf[:n])) - ch <- struct{}{} - }() - - select { - case <-time.NewTimer(100 * time.Millisecond).C: - continue - case <-ch: - fileInfo, _ := os.Stat(fileName) - if fileInfo.IsDir() { - continue - } - - if _, exists := fileExists[fileName]; !exists { - files = append(files, OpenFilesStat{ - Path: fileName, - Fd: uint64(file), - }) - fileExists[fileName] = true - } - case <-ctx.Done(): - return files, ctx.Err() - } - } - - return files, nil -} - -func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { - return net.ConnectionsPidWithContext(ctx, "all", p.Pid) -} - -func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) { - return nil, common.ErrNotImplementedError -} - -func (p *Process) SendSignalWithContext(ctx context.Context, sig syscall.Signal) error { - return common.ErrNotImplementedError -} - -func (p *Process) SuspendWithContext(ctx context.Context) error { - c, err := windows.OpenProcess(windows.PROCESS_SUSPEND_RESUME, false, uint32(p.Pid)) - if err != nil { - return err - } - defer windows.CloseHandle(c) - - r1, _, _ := procNtSuspendProcess.Call(uintptr(c)) - if r1 != 0 { - // See https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55 - return fmt.Errorf("NtStatus='0x%.8X'", r1) - } - - return nil -} - -func (p *Process) ResumeWithContext(ctx context.Context) error { - c, err := windows.OpenProcess(windows.PROCESS_SUSPEND_RESUME, false, uint32(p.Pid)) - if err != nil { - return err - } - defer windows.CloseHandle(c) - - r1, _, _ := procNtResumeProcess.Call(uintptr(c)) - if r1 != 0 { - // See https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55 - return fmt.Errorf("NtStatus='0x%.8X'", r1) - } - - return nil -} - -func (p *Process) TerminateWithContext(ctx context.Context) error { - proc, err := windows.OpenProcess(windows.PROCESS_TERMINATE, false, uint32(p.Pid)) - if err != nil { - return err - } - err = windows.TerminateProcess(proc, 0) - windows.CloseHandle(proc) - return err -} - -func (p *Process) KillWithContext(ctx context.Context) error { - process, err := os.FindProcess(int(p.Pid)) - if err != nil { - return err - } - return process.Kill() -} - -func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) { - envVars, err := getProcessEnvironmentVariables(p.Pid, ctx) - if err != nil { - return nil, fmt.Errorf("could not get environment variables: %s", err) - } - return envVars, nil -} - -// retrieve Ppid in a thread-safe manner -func (p *Process) getPpid() int32 { - p.parentMutex.RLock() - defer p.parentMutex.RUnlock() - return p.parent -} - -// cache Ppid in a thread-safe manner (WINDOWS ONLY) -// see https://psutil.readthedocs.io/en/latest/#psutil.Process.ppid -func (p *Process) setPpid(ppid int32) { - p.parentMutex.Lock() - defer p.parentMutex.Unlock() - p.parent = ppid -} - -func getFromSnapProcess(pid int32) (int32, int32, string, error) { - snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, uint32(pid)) - if err != nil { - return 0, 0, "", err - } - defer windows.CloseHandle(snap) - var pe32 windows.ProcessEntry32 - pe32.Size = uint32(unsafe.Sizeof(pe32)) - if err = windows.Process32First(snap, &pe32); err != nil { - return 0, 0, "", err - } - for { - if pe32.ProcessID == uint32(pid) { - szexe := windows.UTF16ToString(pe32.ExeFile[:]) - return int32(pe32.ParentProcessID), int32(pe32.Threads), szexe, nil - } - if err = windows.Process32Next(snap, &pe32); err != nil { - break - } - } - return 0, 0, "", fmt.Errorf("couldn't find pid: %d", pid) -} - -func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - out := []*Process{} - - pids, err := PidsWithContext(ctx) - if err != nil { - return out, fmt.Errorf("could not get Processes %s", err) - } - - for _, pid := range pids { - p, err := NewProcessWithContext(ctx, pid) - if err != nil { - continue - } - out = append(out, p) - } - - return out, nil -} - -func getRusage(pid int32) (*windows.Rusage, error) { - var CPU windows.Rusage - - c, err := windows.OpenProcess(processQueryInformation, false, uint32(pid)) - if err != nil { - return nil, err - } - defer windows.CloseHandle(c) - - if err := windows.GetProcessTimes(c, &CPU.CreationTime, &CPU.ExitTime, &CPU.KernelTime, &CPU.UserTime); err != nil { - return nil, err - } - - return &CPU, nil -} - -func getMemoryInfo(pid int32) (PROCESS_MEMORY_COUNTERS, error) { - var mem PROCESS_MEMORY_COUNTERS - c, err := windows.OpenProcess(processQueryInformation, false, uint32(pid)) - if err != nil { - return mem, err - } - defer windows.CloseHandle(c) - if err := getProcessMemoryInfo(c, &mem); err != nil { - return mem, err - } - - return mem, err -} - -func getProcessMemoryInfo(h windows.Handle, mem *PROCESS_MEMORY_COUNTERS) (err error) { - r1, _, e1 := syscall.Syscall(procGetProcessMemoryInfo.Addr(), 3, uintptr(h), uintptr(unsafe.Pointer(mem)), uintptr(unsafe.Sizeof(*mem))) - if r1 == 0 { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -type SYSTEM_TIMES struct { - CreateTime syscall.Filetime - ExitTime syscall.Filetime - KernelTime syscall.Filetime - UserTime syscall.Filetime -} - -func getProcessCPUTimes(pid int32) (SYSTEM_TIMES, error) { - var times SYSTEM_TIMES - - h, err := windows.OpenProcess(processQueryInformation, false, uint32(pid)) - if err != nil { - return times, err - } - defer windows.CloseHandle(h) - - err = syscall.GetProcessTimes( - syscall.Handle(h), - ×.CreateTime, - ×.ExitTime, - ×.KernelTime, - ×.UserTime, - ) - - return times, err -} - -func getUserProcessParams32(handle windows.Handle) (rtlUserProcessParameters32, error) { - pebAddress, err := queryPebAddress(syscall.Handle(handle), true) - if err != nil { - return rtlUserProcessParameters32{}, fmt.Errorf("cannot locate process PEB: %w", err) - } - - buf := readProcessMemory(syscall.Handle(handle), true, pebAddress, uint(unsafe.Sizeof(processEnvironmentBlock32{}))) - if len(buf) != int(unsafe.Sizeof(processEnvironmentBlock32{})) { - return rtlUserProcessParameters32{}, fmt.Errorf("cannot read process PEB") - } - peb := (*processEnvironmentBlock32)(unsafe.Pointer(&buf[0])) - userProcessAddress := uint64(peb.ProcessParameters) - buf = readProcessMemory(syscall.Handle(handle), true, userProcessAddress, uint(unsafe.Sizeof(rtlUserProcessParameters32{}))) - if len(buf) != int(unsafe.Sizeof(rtlUserProcessParameters32{})) { - return rtlUserProcessParameters32{}, fmt.Errorf("cannot read user process parameters") - } - return *(*rtlUserProcessParameters32)(unsafe.Pointer(&buf[0])), nil -} - -func getUserProcessParams64(handle windows.Handle) (rtlUserProcessParameters64, error) { - pebAddress, err := queryPebAddress(syscall.Handle(handle), false) - if err != nil { - return rtlUserProcessParameters64{}, fmt.Errorf("cannot locate process PEB: %w", err) - } - - buf := readProcessMemory(syscall.Handle(handle), false, pebAddress, uint(unsafe.Sizeof(processEnvironmentBlock64{}))) - if len(buf) != int(unsafe.Sizeof(processEnvironmentBlock64{})) { - return rtlUserProcessParameters64{}, fmt.Errorf("cannot read process PEB") - } - peb := (*processEnvironmentBlock64)(unsafe.Pointer(&buf[0])) - userProcessAddress := peb.ProcessParameters - buf = readProcessMemory(syscall.Handle(handle), false, userProcessAddress, uint(unsafe.Sizeof(rtlUserProcessParameters64{}))) - if len(buf) != int(unsafe.Sizeof(rtlUserProcessParameters64{})) { - return rtlUserProcessParameters64{}, fmt.Errorf("cannot read user process parameters") - } - return *(*rtlUserProcessParameters64)(unsafe.Pointer(&buf[0])), nil -} - -func is32BitProcess(h windows.Handle) bool { - const ( - PROCESSOR_ARCHITECTURE_INTEL = 0 - PROCESSOR_ARCHITECTURE_ARM = 5 - PROCESSOR_ARCHITECTURE_ARM64 = 12 - PROCESSOR_ARCHITECTURE_IA64 = 6 - PROCESSOR_ARCHITECTURE_AMD64 = 9 - ) - - var procIs32Bits bool - switch processorArchitecture { - case PROCESSOR_ARCHITECTURE_INTEL: - fallthrough - case PROCESSOR_ARCHITECTURE_ARM: - procIs32Bits = true - case PROCESSOR_ARCHITECTURE_ARM64: - fallthrough - case PROCESSOR_ARCHITECTURE_IA64: - fallthrough - case PROCESSOR_ARCHITECTURE_AMD64: - var wow64 uint - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(h), - uintptr(common.ProcessWow64Information), - uintptr(unsafe.Pointer(&wow64)), - uintptr(unsafe.Sizeof(wow64)), - uintptr(0), - ) - if int(ret) >= 0 { - if wow64 != 0 { - procIs32Bits = true - } - } else { - //if the OS does not support the call, we fallback into the bitness of the app - if unsafe.Sizeof(wow64) == 4 { - procIs32Bits = true - } - } - - default: - //for other unknown platforms, we rely on process platform - if unsafe.Sizeof(processorArchitecture) == 8 { - procIs32Bits = false - } else { - procIs32Bits = true - } - } - return procIs32Bits -} - -func getProcessEnvironmentVariables(pid int32, ctx context.Context) ([]string, error) { - h, err := windows.OpenProcess(processQueryInformation|windows.PROCESS_VM_READ, false, uint32(pid)) - if err == windows.ERROR_ACCESS_DENIED || err == windows.ERROR_INVALID_PARAMETER { - return nil, nil - } - if err != nil { - return nil, err - } - defer syscall.CloseHandle(syscall.Handle(h)) - - procIs32Bits := is32BitProcess(h) - - var processParameterBlockAddress uint64 - - if procIs32Bits { - peb, err := getUserProcessParams32(h) - if err != nil { - return nil, err - } - processParameterBlockAddress = uint64(peb.EnvironmentAddress) - } else { - peb, err := getUserProcessParams64(h) - if err != nil { - return nil, err - } - processParameterBlockAddress = peb.EnvironmentAddress - } - envvarScanner := bufio.NewScanner(&processReader{ - processHandle: h, - is32BitProcess: procIs32Bits, - offset: processParameterBlockAddress, - }) - envvarScanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { - if atEOF && len(data) == 0 { - return 0, nil, nil - } - // Check for UTF-16 zero character - for i := 0; i < len(data)-1; i += 2 { - if data[i] == 0 && data[i+1] == 0 { - return i + 2, data[0:i], nil - } - } - if atEOF { - return len(data), data, nil - } - // Request more data - return 0, nil, nil - }) - var envVars []string - for envvarScanner.Scan() { - entry := envvarScanner.Bytes() - if len(entry) == 0 { - break // Block is finished - } - envVars = append(envVars, convertUTF16ToString(entry)) - select { - case <-ctx.Done(): - break - default: - continue - } - } - if err := envvarScanner.Err(); err != nil { - return nil, err - } - return envVars, nil -} - -type processReader struct { - processHandle windows.Handle - is32BitProcess bool - offset uint64 -} - -func (p *processReader) Read(buf []byte) (int, error) { - processMemory := readProcessMemory(syscall.Handle(p.processHandle), p.is32BitProcess, p.offset, uint(len(buf))) - if len(processMemory) == 0 { - return 0, io.EOF - } - copy(buf, processMemory) - p.offset += uint64(len(processMemory)) - return len(processMemory), nil -} - -func getProcessCommandLine(pid int32) (string, error) { - h, err := windows.OpenProcess(processQueryInformation|windows.PROCESS_VM_READ, false, uint32(pid)) - if err == windows.ERROR_ACCESS_DENIED || err == windows.ERROR_INVALID_PARAMETER { - return "", nil - } - if err != nil { - return "", err - } - defer syscall.CloseHandle(syscall.Handle(h)) - - procIs32Bits := is32BitProcess(h) - - if procIs32Bits { - userProcParams, err := getUserProcessParams32(h) - if err != nil { - return "", err - } - if userProcParams.CommandLineLength > 0 { - cmdLine := readProcessMemory(syscall.Handle(h), procIs32Bits, uint64(userProcParams.CommandLineAddress), uint(userProcParams.CommandLineLength)) - if len(cmdLine) != int(userProcParams.CommandLineLength) { - return "", errors.New("cannot read cmdline") - } - - return convertUTF16ToString(cmdLine), nil - } - } else { - userProcParams, err := getUserProcessParams64(h) - if err != nil { - return "", err - } - if userProcParams.CommandLineLength > 0 { - cmdLine := readProcessMemory(syscall.Handle(h), procIs32Bits, userProcParams.CommandLineAddress, uint(userProcParams.CommandLineLength)) - if len(cmdLine) != int(userProcParams.CommandLineLength) { - return "", errors.New("cannot read cmdline") - } - - return convertUTF16ToString(cmdLine), nil - } - } - - //if we reach here, we have no command line - return "", nil -} - -func convertUTF16ToString(src []byte) string { - srcLen := len(src) / 2 - - codePoints := make([]uint16, srcLen) - - srcIdx := 0 - for i := 0; i < srcLen; i++ { - codePoints[i] = uint16(src[srcIdx]) | uint16(src[srcIdx+1])<<8 - srcIdx += 2 - } - return syscall.UTF16ToString(codePoints) -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_windows_386.go b/vendor/github.com/shirou/gopsutil/process/process_windows_386.go deleted file mode 100644 index 71d4b2ff1..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_windows_386.go +++ /dev/null @@ -1,107 +0,0 @@ -// +build windows - -package process - -import ( - "errors" - "syscall" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/windows" -) - -type PROCESS_MEMORY_COUNTERS struct { - CB uint32 - PageFaultCount uint32 - PeakWorkingSetSize uint32 - WorkingSetSize uint32 - QuotaPeakPagedPoolUsage uint32 - QuotaPagedPoolUsage uint32 - QuotaPeakNonPagedPoolUsage uint32 - QuotaNonPagedPoolUsage uint32 - PagefileUsage uint32 - PeakPagefileUsage uint32 -} - -func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { - if is32BitProcess { - //we are on a 32-bit process reading an external 32-bit process - var info processBasicInformation32 - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return uint64(info.PebBaseAddress), nil - } else { - return 0, windows.NTStatus(ret) - } - } else { - //we are on a 32-bit process reading an external 64-bit process - if common.ProcNtWow64QueryInformationProcess64.Find() == nil { //avoid panic - var info processBasicInformation64 - - ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return info.PebBaseAddress, nil - } else { - return 0, windows.NTStatus(ret) - } - } else { - return 0, errors.New("can't find API to query 64 bit process from 32 bit") - } - } -} - -func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte { - if is32BitProcess { - var read uint - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtReadVirtualMemory.Call( - uintptr(h), - uintptr(address), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:read] - } - } else { - //reading a 64-bit process from a 32-bit one - if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { //avoid panic - var read uint64 - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call( - uintptr(h), - uintptr(address & 0xFFFFFFFF), //the call expects a 64-bit value - uintptr(address >> 32), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), //the call expects a 64-bit value - uintptr(0), //but size is 32-bit so pass zero as the high dword - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:uint(read)] - } - } - } - - //if we reach here, an error happened - return nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_windows_amd64.go b/vendor/github.com/shirou/gopsutil/process/process_windows_amd64.go deleted file mode 100644 index 14308e4d3..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_windows_amd64.go +++ /dev/null @@ -1,78 +0,0 @@ -// +build windows - -package process - -import ( - "syscall" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" - "golang.org/x/sys/windows" -) - -type PROCESS_MEMORY_COUNTERS struct { - CB uint32 - PageFaultCount uint32 - PeakWorkingSetSize uint64 - WorkingSetSize uint64 - QuotaPeakPagedPoolUsage uint64 - QuotaPagedPoolUsage uint64 - QuotaPeakNonPagedPoolUsage uint64 - QuotaNonPagedPoolUsage uint64 - PagefileUsage uint64 - PeakPagefileUsage uint64 -} - -func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { - if is32BitProcess { - //we are on a 64-bit process reading an external 32-bit process - var wow64 uint - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(procHandle), - uintptr(common.ProcessWow64Information), - uintptr(unsafe.Pointer(&wow64)), - uintptr(unsafe.Sizeof(wow64)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return uint64(wow64), nil - } else { - return 0, windows.NTStatus(ret) - } - } else { - //we are on a 64-bit process reading an external 64-bit process - var info processBasicInformation64 - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return info.PebBaseAddress, nil - } else { - return 0, windows.NTStatus(ret) - } - } -} - -func readProcessMemory(procHandle syscall.Handle, _ bool, address uint64, size uint) []byte { - var read uint - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtReadVirtualMemory.Call( - uintptr(procHandle), - uintptr(address), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:read] - } - return nil -} diff --git a/vendor/github.com/shirou/gopsutil/process/process_windows_arm64.go b/vendor/github.com/shirou/gopsutil/process/process_windows_arm64.go deleted file mode 100644 index ddaf19544..000000000 --- a/vendor/github.com/shirou/gopsutil/process/process_windows_arm64.go +++ /dev/null @@ -1,104 +0,0 @@ -//go:build windows && arm64 -// +build windows,arm64 - -package process - -import ( - "errors" - "syscall" - "unsafe" - - "github.com/shirou/gopsutil/internal/common" -) - -type PROCESS_MEMORY_COUNTERS struct { - CB uint32 - PageFaultCount uint32 - PeakWorkingSetSize uint32 - WorkingSetSize uint32 - QuotaPeakPagedPoolUsage uint32 - QuotaPagedPoolUsage uint32 - QuotaPeakNonPagedPoolUsage uint32 - QuotaNonPagedPoolUsage uint32 - PagefileUsage uint32 - PeakPagefileUsage uint32 -} - -func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { - if is32BitProcess { - //we are on a 32-bit process reading an external 32-bit process - var info processBasicInformation32 - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if int(ret) >= 0 { - return uint64(info.PebBaseAddress), nil - } - } else { - //we are on a 32-bit process reading an external 64-bit process - if common.ProcNtWow64QueryInformationProcess64.Find() == nil { //avoid panic - var info processBasicInformation64 - - ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if int(ret) >= 0 { - return info.PebBaseAddress, nil - } - } - } - - //return 0 on error - return 0, errors.New("could not query PEB address") -} - -func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte { - if is32BitProcess { - var read uint - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtReadVirtualMemory.Call( - uintptr(h), - uintptr(address), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:read] - } - } else { - //reading a 64-bit process from a 32-bit one - if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { //avoid panic - var read uint64 - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call( - uintptr(h), - uintptr(address&0xFFFFFFFF), //the call expects a 64-bit value - uintptr(address>>32), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), //the call expects a 64-bit value - uintptr(0), //but size is 32-bit so pass zero as the high dword - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:uint(read)] - } - } - } - - //if we reach here, an error happened - return nil -} diff --git a/vendor/github.com/shirou/gopsutil/v3/internal/common/sleep.go b/vendor/github.com/shirou/gopsutil/v3/internal/common/sleep.go index 9bed2419e..94cedfd34 100644 --- a/vendor/github.com/shirou/gopsutil/v3/internal/common/sleep.go +++ b/vendor/github.com/shirou/gopsutil/v3/internal/common/sleep.go @@ -6,7 +6,7 @@ import ( ) // Sleep awaits for provided interval. -// Can be interrupted by context cancelation. +// Can be interrupted by context cancellation. func Sleep(ctx context.Context, interval time.Duration) error { timer := time.NewTimer(interval) select { diff --git a/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go b/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go index 6e8ce67fb..20ca5470a 100644 --- a/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go +++ b/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go @@ -471,7 +471,7 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p } } if err != nil { - return nil, fmt.Errorf("cound not get pid(s), %d: %w", pid, err) + return nil, fmt.Errorf("could not get pid(s), %d: %w", pid, err) } return statsFromInodesWithContext(ctx, root, pid, tmap, inodes, skipUids) } diff --git a/vendor/github.com/shirou/gopsutil/v3/net/net_openbsd.go b/vendor/github.com/shirou/gopsutil/v3/net/net_openbsd.go index cf48f53e7..25bbe49ca 100644 --- a/vendor/github.com/shirou/gopsutil/v3/net/net_openbsd.go +++ b/vendor/github.com/shirou/gopsutil/v3/net/net_openbsd.go @@ -23,9 +23,9 @@ func ParseNetstat(output string, mode string, exists := make([]string, 0, len(lines)-1) - columns := 6 - if mode == "ind" { - columns = 10 + columns := 9 + if mode == "inb" { + columns = 6 } for _, line := range lines { values := strings.Fields(line) @@ -48,18 +48,23 @@ func ParseNetstat(output string, mode string, parsed := make([]uint64, 0, 8) var vv []string - if mode == "inb" { + switch mode { + case "inb": vv = []string{ values[base+3], // BytesRecv values[base+4], // BytesSent } - } else { + case "ind": vv = []string{ values[base+3], // Ipkts - values[base+4], // Ierrs + values[base+4], // Idrop values[base+5], // Opkts + values[base+6], // Odrops + } + case "ine": + vv = []string{ + values[base+4], // Ierrs values[base+6], // Oerrs - values[base+8], // Drops } } for _, target := range vv { @@ -80,16 +85,19 @@ func ParseNetstat(output string, mode string, if !present { n = IOCountersStat{Name: values[0]} } - if mode == "inb" { + + switch mode { + case "inb": n.BytesRecv = parsed[0] n.BytesSent = parsed[1] - } else { + case "ind": n.PacketsRecv = parsed[0] - n.Errin = parsed[1] + n.Dropin = parsed[1] n.PacketsSent = parsed[2] - n.Errout = parsed[3] - n.Dropin = parsed[4] - n.Dropout = parsed[4] + n.Dropout = parsed[3] + case "ine": + n.Errin = parsed[0] + n.Errout = parsed[1] } iocs[n.Name] = n @@ -114,6 +122,10 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, if err != nil { return nil, err } + out3, err := invoke.CommandWithContext(ctx, netstat, "-ine") + if err != nil { + return nil, err + } iocs := make(map[string]IOCountersStat) lines := strings.Split(string(out), "\n") @@ -127,6 +139,10 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, if err != nil { return nil, err } + err = ParseNetstat(string(out3), "ine", iocs) + if err != nil { + return nil, err + } for _, ioc := range iocs { ret = append(ret, ioc) diff --git a/vendor/github.com/shirou/gopsutil/v3/process/process_freebsd_arm64.go b/vendor/github.com/shirou/gopsutil/v3/process/process_freebsd_arm64.go index effd470a0..73ac08201 100644 --- a/vendor/github.com/shirou/gopsutil/v3/process/process_freebsd_arm64.go +++ b/vendor/github.com/shirou/gopsutil/v3/process/process_freebsd_arm64.go @@ -1,8 +1,7 @@ //go:build freebsd && arm64 -// +build freebsd,arm64 // Code generated by cmd/cgo -godefs; DO NOT EDIT. -// cgo -godefs process/types_freebsd.go +// cgo -godefs types_freebsd.go package process @@ -82,14 +81,14 @@ type Rlimit struct { type KinfoProc struct { Structsize int32 Layout int32 - Args *int64 /* pargs */ - Paddr *int64 /* proc */ - Addr *int64 /* user */ - Tracep *int64 /* vnode */ - Textvp *int64 /* vnode */ - Fd *int64 /* filedesc */ - Vmspace *int64 /* vmspace */ - Wchan *byte + Args int64 /* pargs */ + Paddr int64 /* proc */ + Addr int64 /* user */ + Tracep int64 /* vnode */ + Textvp int64 /* vnode */ + Fd int64 /* filedesc */ + Vmspace int64 /* vmspace */ + Wchan int64 Pid int32 Ppid int32 Pgid int32 @@ -140,7 +139,7 @@ type KinfoProc struct { Wmesg [9]uint8 Login [18]uint8 Lockname [9]uint8 - Comm [20]int8 + Comm [20]int8 // changed from uint8 by hand Emul [17]uint8 Loginclass [18]uint8 Moretdname [4]uint8 @@ -159,11 +158,12 @@ type KinfoProc struct { Pri Priority Rusage Rusage Rusage_ch Rusage - Pcb *int64 /* pcb */ - Kstack *byte - Udata *byte - Tdaddr *int64 /* thread */ - Spareptrs [6]*byte + Pcb int64 /* pcb */ + Kstack int64 + Udata int64 + Tdaddr int64 /* thread */ + Pd int64 /* pwddesc, not accurate */ + Spareptrs [5]int64 Sparelongs [12]int64 Sflag int64 Tdflags int64 @@ -195,7 +195,7 @@ type KinfoVmentry struct { Vn_rdev_freebsd11 uint32 Vn_mode uint16 Status uint16 - Vn_fsid uint64 + Type_spec [8]byte Vn_rdev uint64 X_kve_ispare [8]int32 Path [1024]uint8 diff --git a/vendor/github.com/stretchr/objx/.codeclimate.yml b/vendor/github.com/stretchr/objx/.codeclimate.yml deleted file mode 100644 index 559fa399c..000000000 --- a/vendor/github.com/stretchr/objx/.codeclimate.yml +++ /dev/null @@ -1,21 +0,0 @@ -engines: - gofmt: - enabled: true - golint: - enabled: true - govet: - enabled: true - -exclude_patterns: -- ".github/" -- "vendor/" -- "codegen/" -- "*.yml" -- ".*.yml" -- "*.md" -- "Gopkg.*" -- "doc.go" -- "type_specific_codegen_test.go" -- "type_specific_codegen.go" -- ".gitignore" -- "LICENSE" diff --git a/vendor/github.com/stretchr/objx/.gitignore b/vendor/github.com/stretchr/objx/.gitignore deleted file mode 100644 index ea58090bd..000000000 --- a/vendor/github.com/stretchr/objx/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -# Binaries for programs and plugins -*.exe -*.dll -*.so -*.dylib - -# Test binary, build with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out diff --git a/vendor/github.com/stretchr/objx/LICENSE b/vendor/github.com/stretchr/objx/LICENSE deleted file mode 100644 index 44d4d9d5a..000000000 --- a/vendor/github.com/stretchr/objx/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -The MIT License - -Copyright (c) 2014 Stretchr, Inc. -Copyright (c) 2017-2018 objx contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/vendor/github.com/stretchr/objx/README.md b/vendor/github.com/stretchr/objx/README.md deleted file mode 100644 index 78dc1f8b0..000000000 --- a/vendor/github.com/stretchr/objx/README.md +++ /dev/null @@ -1,80 +0,0 @@ -# Objx -[![Build Status](https://travis-ci.org/stretchr/objx.svg?branch=master)](https://travis-ci.org/stretchr/objx) -[![Go Report Card](https://goreportcard.com/badge/github.com/stretchr/objx)](https://goreportcard.com/report/github.com/stretchr/objx) -[![Maintainability](https://api.codeclimate.com/v1/badges/1d64bc6c8474c2074f2b/maintainability)](https://codeclimate.com/github/stretchr/objx/maintainability) -[![Test Coverage](https://api.codeclimate.com/v1/badges/1d64bc6c8474c2074f2b/test_coverage)](https://codeclimate.com/github/stretchr/objx/test_coverage) -[![Sourcegraph](https://sourcegraph.com/github.com/stretchr/objx/-/badge.svg)](https://sourcegraph.com/github.com/stretchr/objx) -[![GoDoc](https://pkg.go.dev/badge/github.com/stretchr/objx?utm_source=godoc)](https://pkg.go.dev/github.com/stretchr/objx) - -Objx - Go package for dealing with maps, slices, JSON and other data. - -Get started: - -- Install Objx with [one line of code](#installation), or [update it with another](#staying-up-to-date) -- Check out the API Documentation http://pkg.go.dev/github.com/stretchr/objx - -## Overview -Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes a powerful `Get` method (among others) that allows you to easily and quickly get access to data within the map, without having to worry too much about type assertions, missing data, default values etc. - -### Pattern -Objx uses a predictable pattern to make access data from within `map[string]interface{}` easy. Call one of the `objx.` functions to create your `objx.Map` to get going: - - m, err := objx.FromJSON(json) - -NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong, the rest will be optimistic and try to figure things out without panicking. - -Use `Get` to access the value you're interested in. You can use dot and array -notation too: - - m.Get("places[0].latlng") - -Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type. - - if m.Get("code").IsStr() { // Your code... } - -Or you can just assume the type, and use one of the strong type methods to extract the real value: - - m.Get("code").Int() - -If there's no value there (or if it's the wrong type) then a default value will be returned, or you can be explicit about the default value. - - Get("code").Int(-1) - -If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating, manipulating and selecting that data. You can find out more by exploring the index below. - -### Reading data -A simple example of how to use Objx: - - // Use MustFromJSON to make an objx.Map from some JSON - m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`) - - // Get the details - name := m.Get("name").Str() - age := m.Get("age").Int() - - // Get their nickname (or use their name if they don't have one) - nickname := m.Get("nickname").Str(name) - -### Ranging -Since `objx.Map` is a `map[string]interface{}` you can treat it as such. For example, to `range` the data, do what you would expect: - - m := objx.MustFromJSON(json) - for key, value := range m { - // Your code... - } - -## Installation -To install Objx, use go get: - - go get github.com/stretchr/objx - -### Staying up to date -To update Objx to the latest version, run: - - go get -u github.com/stretchr/objx - -### Supported go versions -We currently support the three recent major Go versions. - -## Contributing -Please feel free to submit issues, fork the repository and send pull requests! diff --git a/vendor/github.com/stretchr/objx/Taskfile.yml b/vendor/github.com/stretchr/objx/Taskfile.yml deleted file mode 100644 index 8a79e8d67..000000000 --- a/vendor/github.com/stretchr/objx/Taskfile.yml +++ /dev/null @@ -1,27 +0,0 @@ -version: '3' - -tasks: - default: - deps: [test] - - lint: - desc: Checks code style - cmds: - - gofmt -d -s *.go - - go vet ./... - silent: true - - lint-fix: - desc: Fixes code style - cmds: - - gofmt -w -s *.go - - test: - desc: Runs go tests - cmds: - - go test -race ./... - - test-coverage: - desc: Runs go tests and calculates test coverage - cmds: - - go test -race -coverprofile=c.out ./... diff --git a/vendor/github.com/stretchr/objx/accessors.go b/vendor/github.com/stretchr/objx/accessors.go deleted file mode 100644 index 72f1d1c1c..000000000 --- a/vendor/github.com/stretchr/objx/accessors.go +++ /dev/null @@ -1,197 +0,0 @@ -package objx - -import ( - "reflect" - "regexp" - "strconv" - "strings" -) - -const ( - // PathSeparator is the character used to separate the elements - // of the keypath. - // - // For example, `location.address.city` - PathSeparator string = "." - - // arrayAccessRegexString is the regex used to extract the array number - // from the access path - arrayAccessRegexString = `^(.+)\[([0-9]+)\]$` - - // mapAccessRegexString is the regex used to extract the map key - // from the access path - mapAccessRegexString = `^([^\[]*)\[([^\]]+)\](.*)$` -) - -// arrayAccessRegex is the compiled arrayAccessRegexString -var arrayAccessRegex = regexp.MustCompile(arrayAccessRegexString) - -// mapAccessRegex is the compiled mapAccessRegexString -var mapAccessRegex = regexp.MustCompile(mapAccessRegexString) - -// Get gets the value using the specified selector and -// returns it inside a new Obj object. -// -// If it cannot find the value, Get will return a nil -// value inside an instance of Obj. -// -// Get can only operate directly on map[string]interface{} and []interface. -// -// # Example -// -// To access the title of the third chapter of the second book, do: -// -// o.Get("books[1].chapters[2].title") -func (m Map) Get(selector string) *Value { - rawObj := access(m, selector, nil, false) - return &Value{data: rawObj} -} - -// Set sets the value using the specified selector and -// returns the object on which Set was called. -// -// Set can only operate directly on map[string]interface{} and []interface -// -// # Example -// -// To set the title of the third chapter of the second book, do: -// -// o.Set("books[1].chapters[2].title","Time to Go") -func (m Map) Set(selector string, value interface{}) Map { - access(m, selector, value, true) - return m -} - -// getIndex returns the index, which is hold in s by two branches. -// It also returns s without the index part, e.g. name[1] will return (1, name). -// If no index is found, -1 is returned -func getIndex(s string) (int, string) { - arrayMatches := arrayAccessRegex.FindStringSubmatch(s) - if len(arrayMatches) > 0 { - // Get the key into the map - selector := arrayMatches[1] - // Get the index into the array at the key - // We know this can't fail because arrayMatches[2] is an int for sure - index, _ := strconv.Atoi(arrayMatches[2]) - return index, selector - } - return -1, s -} - -// getKey returns the key which is held in s by two brackets. -// It also returns the next selector. -func getKey(s string) (string, string) { - selSegs := strings.SplitN(s, PathSeparator, 2) - thisSel := selSegs[0] - nextSel := "" - - if len(selSegs) > 1 { - nextSel = selSegs[1] - } - - mapMatches := mapAccessRegex.FindStringSubmatch(s) - if len(mapMatches) > 0 { - if _, err := strconv.Atoi(mapMatches[2]); err != nil { - thisSel = mapMatches[1] - nextSel = "[" + mapMatches[2] + "]" + mapMatches[3] - - if thisSel == "" { - thisSel = mapMatches[2] - nextSel = mapMatches[3] - } - - if nextSel == "" { - selSegs = []string{"", ""} - } else if nextSel[0] == '.' { - nextSel = nextSel[1:] - } - } - } - - return thisSel, nextSel -} - -// access accesses the object using the selector and performs the -// appropriate action. -func access(current interface{}, selector string, value interface{}, isSet bool) interface{} { - thisSel, nextSel := getKey(selector) - - indexes := []int{} - for strings.Contains(thisSel, "[") { - prevSel := thisSel - index := -1 - index, thisSel = getIndex(thisSel) - indexes = append(indexes, index) - if prevSel == thisSel { - break - } - } - - if curMap, ok := current.(Map); ok { - current = map[string]interface{}(curMap) - } - // get the object in question - switch current.(type) { - case map[string]interface{}: - curMSI := current.(map[string]interface{}) - if nextSel == "" && isSet { - curMSI[thisSel] = value - return nil - } - - _, ok := curMSI[thisSel].(map[string]interface{}) - if !ok { - _, ok = curMSI[thisSel].(Map) - } - - if (curMSI[thisSel] == nil || !ok) && len(indexes) == 0 && isSet { - curMSI[thisSel] = map[string]interface{}{} - } - - current = curMSI[thisSel] - default: - current = nil - } - - // do we need to access the item of an array? - if len(indexes) > 0 { - num := len(indexes) - for num > 0 { - num-- - index := indexes[num] - indexes = indexes[:num] - if array, ok := interSlice(current); ok { - if index < len(array) { - current = array[index] - } else { - current = nil - break - } - } - } - } - - if nextSel != "" { - current = access(current, nextSel, value, isSet) - } - return current -} - -func interSlice(slice interface{}) ([]interface{}, bool) { - if array, ok := slice.([]interface{}); ok { - return array, ok - } - - s := reflect.ValueOf(slice) - if s.Kind() != reflect.Slice { - return nil, false - } - - ret := make([]interface{}, s.Len()) - - for i := 0; i < s.Len(); i++ { - ret[i] = s.Index(i).Interface() - } - - return ret, true -} diff --git a/vendor/github.com/stretchr/objx/conversions.go b/vendor/github.com/stretchr/objx/conversions.go deleted file mode 100644 index 01c63d7d3..000000000 --- a/vendor/github.com/stretchr/objx/conversions.go +++ /dev/null @@ -1,280 +0,0 @@ -package objx - -import ( - "bytes" - "encoding/base64" - "encoding/json" - "errors" - "fmt" - "net/url" - "strconv" -) - -// SignatureSeparator is the character that is used to -// separate the Base64 string from the security signature. -const SignatureSeparator = "_" - -// URLValuesSliceKeySuffix is the character that is used to -// specify a suffix for slices parsed by URLValues. -// If the suffix is set to "[i]", then the index of the slice -// is used in place of i -// Ex: Suffix "[]" would have the form a[]=b&a[]=c -// OR Suffix "[i]" would have the form a[0]=b&a[1]=c -// OR Suffix "" would have the form a=b&a=c -var urlValuesSliceKeySuffix = "[]" - -const ( - URLValuesSliceKeySuffixEmpty = "" - URLValuesSliceKeySuffixArray = "[]" - URLValuesSliceKeySuffixIndex = "[i]" -) - -// SetURLValuesSliceKeySuffix sets the character that is used to -// specify a suffix for slices parsed by URLValues. -// If the suffix is set to "[i]", then the index of the slice -// is used in place of i -// Ex: Suffix "[]" would have the form a[]=b&a[]=c -// OR Suffix "[i]" would have the form a[0]=b&a[1]=c -// OR Suffix "" would have the form a=b&a=c -func SetURLValuesSliceKeySuffix(s string) error { - if s == URLValuesSliceKeySuffixEmpty || s == URLValuesSliceKeySuffixArray || s == URLValuesSliceKeySuffixIndex { - urlValuesSliceKeySuffix = s - return nil - } - - return errors.New("objx: Invalid URLValuesSliceKeySuffix provided.") -} - -// JSON converts the contained object to a JSON string -// representation -func (m Map) JSON() (string, error) { - for k, v := range m { - m[k] = cleanUp(v) - } - - result, err := json.Marshal(m) - if err != nil { - err = errors.New("objx: JSON encode failed with: " + err.Error()) - } - return string(result), err -} - -func cleanUpInterfaceArray(in []interface{}) []interface{} { - result := make([]interface{}, len(in)) - for i, v := range in { - result[i] = cleanUp(v) - } - return result -} - -func cleanUpInterfaceMap(in map[interface{}]interface{}) Map { - result := Map{} - for k, v := range in { - result[fmt.Sprintf("%v", k)] = cleanUp(v) - } - return result -} - -func cleanUpStringMap(in map[string]interface{}) Map { - result := Map{} - for k, v := range in { - result[k] = cleanUp(v) - } - return result -} - -func cleanUpMSIArray(in []map[string]interface{}) []Map { - result := make([]Map, len(in)) - for i, v := range in { - result[i] = cleanUpStringMap(v) - } - return result -} - -func cleanUpMapArray(in []Map) []Map { - result := make([]Map, len(in)) - for i, v := range in { - result[i] = cleanUpStringMap(v) - } - return result -} - -func cleanUp(v interface{}) interface{} { - switch v := v.(type) { - case []interface{}: - return cleanUpInterfaceArray(v) - case []map[string]interface{}: - return cleanUpMSIArray(v) - case map[interface{}]interface{}: - return cleanUpInterfaceMap(v) - case Map: - return cleanUpStringMap(v) - case []Map: - return cleanUpMapArray(v) - default: - return v - } -} - -// MustJSON converts the contained object to a JSON string -// representation and panics if there is an error -func (m Map) MustJSON() string { - result, err := m.JSON() - if err != nil { - panic(err.Error()) - } - return result -} - -// Base64 converts the contained object to a Base64 string -// representation of the JSON string representation -func (m Map) Base64() (string, error) { - var buf bytes.Buffer - - jsonData, err := m.JSON() - if err != nil { - return "", err - } - - encoder := base64.NewEncoder(base64.StdEncoding, &buf) - _, _ = encoder.Write([]byte(jsonData)) - _ = encoder.Close() - - return buf.String(), nil -} - -// MustBase64 converts the contained object to a Base64 string -// representation of the JSON string representation and panics -// if there is an error -func (m Map) MustBase64() string { - result, err := m.Base64() - if err != nil { - panic(err.Error()) - } - return result -} - -// SignedBase64 converts the contained object to a Base64 string -// representation of the JSON string representation and signs it -// using the provided key. -func (m Map) SignedBase64(key string) (string, error) { - base64, err := m.Base64() - if err != nil { - return "", err - } - - sig := HashWithKey(base64, key) - return base64 + SignatureSeparator + sig, nil -} - -// MustSignedBase64 converts the contained object to a Base64 string -// representation of the JSON string representation and signs it -// using the provided key and panics if there is an error -func (m Map) MustSignedBase64(key string) string { - result, err := m.SignedBase64(key) - if err != nil { - panic(err.Error()) - } - return result -} - -/* - URL Query - ------------------------------------------------ -*/ - -// URLValues creates a url.Values object from an Obj. This -// function requires that the wrapped object be a map[string]interface{} -func (m Map) URLValues() url.Values { - vals := make(url.Values) - - m.parseURLValues(m, vals, "") - - return vals -} - -func (m Map) parseURLValues(queryMap Map, vals url.Values, key string) { - useSliceIndex := false - if urlValuesSliceKeySuffix == "[i]" { - useSliceIndex = true - } - - for k, v := range queryMap { - val := &Value{data: v} - switch { - case val.IsObjxMap(): - if key == "" { - m.parseURLValues(val.ObjxMap(), vals, k) - } else { - m.parseURLValues(val.ObjxMap(), vals, key+"["+k+"]") - } - case val.IsObjxMapSlice(): - sliceKey := k - if key != "" { - sliceKey = key + "[" + k + "]" - } - - if useSliceIndex { - for i, sv := range val.MustObjxMapSlice() { - sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]" - m.parseURLValues(sv, vals, sk) - } - } else { - sliceKey = sliceKey + urlValuesSliceKeySuffix - for _, sv := range val.MustObjxMapSlice() { - m.parseURLValues(sv, vals, sliceKey) - } - } - case val.IsMSISlice(): - sliceKey := k - if key != "" { - sliceKey = key + "[" + k + "]" - } - - if useSliceIndex { - for i, sv := range val.MustMSISlice() { - sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]" - m.parseURLValues(New(sv), vals, sk) - } - } else { - sliceKey = sliceKey + urlValuesSliceKeySuffix - for _, sv := range val.MustMSISlice() { - m.parseURLValues(New(sv), vals, sliceKey) - } - } - case val.IsStrSlice(), val.IsBoolSlice(), - val.IsFloat32Slice(), val.IsFloat64Slice(), - val.IsIntSlice(), val.IsInt8Slice(), val.IsInt16Slice(), val.IsInt32Slice(), val.IsInt64Slice(), - val.IsUintSlice(), val.IsUint8Slice(), val.IsUint16Slice(), val.IsUint32Slice(), val.IsUint64Slice(): - - sliceKey := k - if key != "" { - sliceKey = key + "[" + k + "]" - } - - if useSliceIndex { - for i, sv := range val.StringSlice() { - sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]" - vals.Set(sk, sv) - } - } else { - sliceKey = sliceKey + urlValuesSliceKeySuffix - vals[sliceKey] = val.StringSlice() - } - - default: - if key == "" { - vals.Set(k, val.String()) - } else { - vals.Set(key+"["+k+"]", val.String()) - } - } - } -} - -// URLQuery gets an encoded URL query representing the given -// Obj. This function requires that the wrapped object be a -// map[string]interface{} -func (m Map) URLQuery() (string, error) { - return m.URLValues().Encode(), nil -} diff --git a/vendor/github.com/stretchr/objx/doc.go b/vendor/github.com/stretchr/objx/doc.go deleted file mode 100644 index b170af74b..000000000 --- a/vendor/github.com/stretchr/objx/doc.go +++ /dev/null @@ -1,66 +0,0 @@ -/* -Package objx provides utilities for dealing with maps, slices, JSON and other data. - -# Overview - -Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes -a powerful `Get` method (among others) that allows you to easily and quickly get -access to data within the map, without having to worry too much about type assertions, -missing data, default values etc. - -# Pattern - -Objx uses a predictable pattern to make access data from within `map[string]interface{}` easy. -Call one of the `objx.` functions to create your `objx.Map` to get going: - - m, err := objx.FromJSON(json) - -NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong, -the rest will be optimistic and try to figure things out without panicking. - -Use `Get` to access the value you're interested in. You can use dot and array -notation too: - - m.Get("places[0].latlng") - -Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type. - - if m.Get("code").IsStr() { // Your code... } - -Or you can just assume the type, and use one of the strong type methods to extract the real value: - - m.Get("code").Int() - -If there's no value there (or if it's the wrong type) then a default value will be returned, -or you can be explicit about the default value. - - Get("code").Int(-1) - -If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating, -manipulating and selecting that data. You can find out more by exploring the index below. - -# Reading data - -A simple example of how to use Objx: - - // Use MustFromJSON to make an objx.Map from some JSON - m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`) - - // Get the details - name := m.Get("name").Str() - age := m.Get("age").Int() - - // Get their nickname (or use their name if they don't have one) - nickname := m.Get("nickname").Str(name) - -# Ranging - -Since `objx.Map` is a `map[string]interface{}` you can treat it as such. -For example, to `range` the data, do what you would expect: - - m := objx.MustFromJSON(json) - for key, value := range m { - // Your code... - } -*/ -package objx diff --git a/vendor/github.com/stretchr/objx/map.go b/vendor/github.com/stretchr/objx/map.go deleted file mode 100644 index ab9f9ae67..000000000 --- a/vendor/github.com/stretchr/objx/map.go +++ /dev/null @@ -1,214 +0,0 @@ -package objx - -import ( - "encoding/base64" - "encoding/json" - "errors" - "io/ioutil" - "net/url" - "strings" -) - -// MSIConvertable is an interface that defines methods for converting your -// custom types to a map[string]interface{} representation. -type MSIConvertable interface { - // MSI gets a map[string]interface{} (msi) representing the - // object. - MSI() map[string]interface{} -} - -// Map provides extended functionality for working with -// untyped data, in particular map[string]interface (msi). -type Map map[string]interface{} - -// Value returns the internal value instance -func (m Map) Value() *Value { - return &Value{data: m} -} - -// Nil represents a nil Map. -var Nil = New(nil) - -// New creates a new Map containing the map[string]interface{} in the data argument. -// If the data argument is not a map[string]interface, New attempts to call the -// MSI() method on the MSIConvertable interface to create one. -func New(data interface{}) Map { - if _, ok := data.(map[string]interface{}); !ok { - if converter, ok := data.(MSIConvertable); ok { - data = converter.MSI() - } else { - return nil - } - } - return Map(data.(map[string]interface{})) -} - -// MSI creates a map[string]interface{} and puts it inside a new Map. -// -// The arguments follow a key, value pattern. -// -// Returns nil if any key argument is non-string or if there are an odd number of arguments. -// -// # Example -// -// To easily create Maps: -// -// m := objx.MSI("name", "Mat", "age", 29, "subobj", objx.MSI("active", true)) -// -// // creates an Map equivalent to -// m := objx.Map{"name": "Mat", "age": 29, "subobj": objx.Map{"active": true}} -func MSI(keyAndValuePairs ...interface{}) Map { - newMap := Map{} - keyAndValuePairsLen := len(keyAndValuePairs) - if keyAndValuePairsLen%2 != 0 { - return nil - } - for i := 0; i < keyAndValuePairsLen; i = i + 2 { - key := keyAndValuePairs[i] - value := keyAndValuePairs[i+1] - - // make sure the key is a string - keyString, keyStringOK := key.(string) - if !keyStringOK { - return nil - } - newMap[keyString] = value - } - return newMap -} - -// ****** Conversion Constructors - -// MustFromJSON creates a new Map containing the data specified in the -// jsonString. -// -// Panics if the JSON is invalid. -func MustFromJSON(jsonString string) Map { - o, err := FromJSON(jsonString) - if err != nil { - panic("objx: MustFromJSON failed with error: " + err.Error()) - } - return o -} - -// MustFromJSONSlice creates a new slice of Map containing the data specified in the -// jsonString. Works with jsons with a top level array -// -// Panics if the JSON is invalid. -func MustFromJSONSlice(jsonString string) []Map { - slice, err := FromJSONSlice(jsonString) - if err != nil { - panic("objx: MustFromJSONSlice failed with error: " + err.Error()) - } - return slice -} - -// FromJSON creates a new Map containing the data specified in the -// jsonString. -// -// Returns an error if the JSON is invalid. -func FromJSON(jsonString string) (Map, error) { - var m Map - err := json.Unmarshal([]byte(jsonString), &m) - if err != nil { - return Nil, err - } - return m, nil -} - -// FromJSONSlice creates a new slice of Map containing the data specified in the -// jsonString. Works with jsons with a top level array -// -// Returns an error if the JSON is invalid. -func FromJSONSlice(jsonString string) ([]Map, error) { - var slice []Map - err := json.Unmarshal([]byte(jsonString), &slice) - if err != nil { - return nil, err - } - return slice, nil -} - -// FromBase64 creates a new Obj containing the data specified -// in the Base64 string. -// -// The string is an encoded JSON string returned by Base64 -func FromBase64(base64String string) (Map, error) { - decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64String)) - decoded, err := ioutil.ReadAll(decoder) - if err != nil { - return nil, err - } - return FromJSON(string(decoded)) -} - -// MustFromBase64 creates a new Obj containing the data specified -// in the Base64 string and panics if there is an error. -// -// The string is an encoded JSON string returned by Base64 -func MustFromBase64(base64String string) Map { - result, err := FromBase64(base64String) - if err != nil { - panic("objx: MustFromBase64 failed with error: " + err.Error()) - } - return result -} - -// FromSignedBase64 creates a new Obj containing the data specified -// in the Base64 string. -// -// The string is an encoded JSON string returned by SignedBase64 -func FromSignedBase64(base64String, key string) (Map, error) { - parts := strings.Split(base64String, SignatureSeparator) - if len(parts) != 2 { - return nil, errors.New("objx: Signed base64 string is malformed") - } - - sig := HashWithKey(parts[0], key) - if parts[1] != sig { - return nil, errors.New("objx: Signature for base64 data does not match") - } - return FromBase64(parts[0]) -} - -// MustFromSignedBase64 creates a new Obj containing the data specified -// in the Base64 string and panics if there is an error. -// -// The string is an encoded JSON string returned by Base64 -func MustFromSignedBase64(base64String, key string) Map { - result, err := FromSignedBase64(base64String, key) - if err != nil { - panic("objx: MustFromSignedBase64 failed with error: " + err.Error()) - } - return result -} - -// FromURLQuery generates a new Obj by parsing the specified -// query. -// -// For queries with multiple values, the first value is selected. -func FromURLQuery(query string) (Map, error) { - vals, err := url.ParseQuery(query) - if err != nil { - return nil, err - } - m := Map{} - for k, vals := range vals { - m[k] = vals[0] - } - return m, nil -} - -// MustFromURLQuery generates a new Obj by parsing the specified -// query. -// -// For queries with multiple values, the first value is selected. -// -// Panics if it encounters an error -func MustFromURLQuery(query string) Map { - o, err := FromURLQuery(query) - if err != nil { - panic("objx: MustFromURLQuery failed with error: " + err.Error()) - } - return o -} diff --git a/vendor/github.com/stretchr/objx/mutations.go b/vendor/github.com/stretchr/objx/mutations.go deleted file mode 100644 index c3400a3f7..000000000 --- a/vendor/github.com/stretchr/objx/mutations.go +++ /dev/null @@ -1,77 +0,0 @@ -package objx - -// Exclude returns a new Map with the keys in the specified []string -// excluded. -func (m Map) Exclude(exclude []string) Map { - excluded := make(Map) - for k, v := range m { - if !contains(exclude, k) { - excluded[k] = v - } - } - return excluded -} - -// Copy creates a shallow copy of the Obj. -func (m Map) Copy() Map { - copied := Map{} - for k, v := range m { - copied[k] = v - } - return copied -} - -// Merge blends the specified map with a copy of this map and returns the result. -// -// Keys that appear in both will be selected from the specified map. -// This method requires that the wrapped object be a map[string]interface{} -func (m Map) Merge(merge Map) Map { - return m.Copy().MergeHere(merge) -} - -// MergeHere blends the specified map with this map and returns the current map. -// -// Keys that appear in both will be selected from the specified map. The original map -// will be modified. This method requires that -// the wrapped object be a map[string]interface{} -func (m Map) MergeHere(merge Map) Map { - for k, v := range merge { - m[k] = v - } - return m -} - -// Transform builds a new Obj giving the transformer a chance -// to change the keys and values as it goes. This method requires that -// the wrapped object be a map[string]interface{} -func (m Map) Transform(transformer func(key string, value interface{}) (string, interface{})) Map { - newMap := Map{} - for k, v := range m { - modifiedKey, modifiedVal := transformer(k, v) - newMap[modifiedKey] = modifiedVal - } - return newMap -} - -// TransformKeys builds a new map using the specified key mapping. -// -// Unspecified keys will be unaltered. -// This method requires that the wrapped object be a map[string]interface{} -func (m Map) TransformKeys(mapping map[string]string) Map { - return m.Transform(func(key string, value interface{}) (string, interface{}) { - if newKey, ok := mapping[key]; ok { - return newKey, value - } - return key, value - }) -} - -// Checks if a string slice contains a string -func contains(s []string, e string) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} diff --git a/vendor/github.com/stretchr/objx/security.go b/vendor/github.com/stretchr/objx/security.go deleted file mode 100644 index 692be8e2a..000000000 --- a/vendor/github.com/stretchr/objx/security.go +++ /dev/null @@ -1,12 +0,0 @@ -package objx - -import ( - "crypto/sha1" - "encoding/hex" -) - -// HashWithKey hashes the specified string using the security key -func HashWithKey(data, key string) string { - d := sha1.Sum([]byte(data + ":" + key)) - return hex.EncodeToString(d[:]) -} diff --git a/vendor/github.com/stretchr/objx/tests.go b/vendor/github.com/stretchr/objx/tests.go deleted file mode 100644 index d9e0b479a..000000000 --- a/vendor/github.com/stretchr/objx/tests.go +++ /dev/null @@ -1,17 +0,0 @@ -package objx - -// Has gets whether there is something at the specified selector -// or not. -// -// If m is nil, Has will always return false. -func (m Map) Has(selector string) bool { - if m == nil { - return false - } - return !m.Get(selector).IsNil() -} - -// IsNil gets whether the data is nil or not. -func (v *Value) IsNil() bool { - return v == nil || v.data == nil -} diff --git a/vendor/github.com/stretchr/objx/type_specific.go b/vendor/github.com/stretchr/objx/type_specific.go deleted file mode 100644 index 80f88d9fa..000000000 --- a/vendor/github.com/stretchr/objx/type_specific.go +++ /dev/null @@ -1,346 +0,0 @@ -package objx - -/* - MSI (map[string]interface{} and []map[string]interface{}) -*/ - -// MSI gets the value as a map[string]interface{}, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) MSI(optionalDefault ...map[string]interface{}) map[string]interface{} { - if s, ok := v.data.(map[string]interface{}); ok { - return s - } - if s, ok := v.data.(Map); ok { - return map[string]interface{}(s) - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustMSI gets the value as a map[string]interface{}. -// -// Panics if the object is not a map[string]interface{}. -func (v *Value) MustMSI() map[string]interface{} { - if s, ok := v.data.(Map); ok { - return map[string]interface{}(s) - } - return v.data.(map[string]interface{}) -} - -// MSISlice gets the value as a []map[string]interface{}, returns the optionalDefault -// value or nil if the value is not a []map[string]interface{}. -func (v *Value) MSISlice(optionalDefault ...[]map[string]interface{}) []map[string]interface{} { - if s, ok := v.data.([]map[string]interface{}); ok { - return s - } - - s := v.ObjxMapSlice() - if s == nil { - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil - } - - result := make([]map[string]interface{}, len(s)) - for i := range s { - result[i] = s[i].Value().MSI() - } - return result -} - -// MustMSISlice gets the value as a []map[string]interface{}. -// -// Panics if the object is not a []map[string]interface{}. -func (v *Value) MustMSISlice() []map[string]interface{} { - if s := v.MSISlice(); s != nil { - return s - } - - return v.data.([]map[string]interface{}) -} - -// IsMSI gets whether the object contained is a map[string]interface{} or not. -func (v *Value) IsMSI() bool { - _, ok := v.data.(map[string]interface{}) - if !ok { - _, ok = v.data.(Map) - } - return ok -} - -// IsMSISlice gets whether the object contained is a []map[string]interface{} or not. -func (v *Value) IsMSISlice() bool { - _, ok := v.data.([]map[string]interface{}) - if !ok { - _, ok = v.data.([]Map) - if !ok { - s, ok := v.data.([]interface{}) - if ok { - for i := range s { - switch s[i].(type) { - case Map: - case map[string]interface{}: - default: - return false - } - } - return true - } - } - } - return ok -} - -// EachMSI calls the specified callback for each object -// in the []map[string]interface{}. -// -// Panics if the object is the wrong type. -func (v *Value) EachMSI(callback func(int, map[string]interface{}) bool) *Value { - for index, val := range v.MustMSISlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereMSI uses the specified decider function to select items -// from the []map[string]interface{}. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereMSI(decider func(int, map[string]interface{}) bool) *Value { - var selected []map[string]interface{} - v.EachMSI(func(index int, val map[string]interface{}) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupMSI uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]map[string]interface{}. -func (v *Value) GroupMSI(grouper func(int, map[string]interface{}) string) *Value { - groups := make(map[string][]map[string]interface{}) - v.EachMSI(func(index int, val map[string]interface{}) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]map[string]interface{}, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceMSI uses the specified function to replace each map[string]interface{}s -// by iterating each item. The data in the returned result will be a -// []map[string]interface{} containing the replaced items. -func (v *Value) ReplaceMSI(replacer func(int, map[string]interface{}) map[string]interface{}) *Value { - arr := v.MustMSISlice() - replaced := make([]map[string]interface{}, len(arr)) - v.EachMSI(func(index int, val map[string]interface{}) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectMSI uses the specified collector function to collect a value -// for each of the map[string]interface{}s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectMSI(collector func(int, map[string]interface{}) interface{}) *Value { - arr := v.MustMSISlice() - collected := make([]interface{}, len(arr)) - v.EachMSI(func(index int, val map[string]interface{}) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - ObjxMap ((Map) and [](Map)) -*/ - -// ObjxMap gets the value as a (Map), returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) ObjxMap(optionalDefault ...(Map)) Map { - if s, ok := v.data.((Map)); ok { - return s - } - if s, ok := v.data.(map[string]interface{}); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return New(nil) -} - -// MustObjxMap gets the value as a (Map). -// -// Panics if the object is not a (Map). -func (v *Value) MustObjxMap() Map { - if s, ok := v.data.(map[string]interface{}); ok { - return s - } - return v.data.((Map)) -} - -// ObjxMapSlice gets the value as a [](Map), returns the optionalDefault -// value or nil if the value is not a [](Map). -func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) { - if s, ok := v.data.([]Map); ok { - return s - } - - if s, ok := v.data.([]map[string]interface{}); ok { - result := make([]Map, len(s)) - for i := range s { - result[i] = s[i] - } - return result - } - - s, ok := v.data.([]interface{}) - if !ok { - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil - } - - result := make([]Map, len(s)) - for i := range s { - switch s[i].(type) { - case Map: - result[i] = s[i].(Map) - case map[string]interface{}: - result[i] = New(s[i]) - default: - return nil - } - } - return result -} - -// MustObjxMapSlice gets the value as a [](Map). -// -// Panics if the object is not a [](Map). -func (v *Value) MustObjxMapSlice() [](Map) { - if s := v.ObjxMapSlice(); s != nil { - return s - } - return v.data.([](Map)) -} - -// IsObjxMap gets whether the object contained is a (Map) or not. -func (v *Value) IsObjxMap() bool { - _, ok := v.data.((Map)) - if !ok { - _, ok = v.data.(map[string]interface{}) - } - return ok -} - -// IsObjxMapSlice gets whether the object contained is a [](Map) or not. -func (v *Value) IsObjxMapSlice() bool { - _, ok := v.data.([](Map)) - if !ok { - _, ok = v.data.([]map[string]interface{}) - if !ok { - s, ok := v.data.([]interface{}) - if ok { - for i := range s { - switch s[i].(type) { - case Map: - case map[string]interface{}: - default: - return false - } - } - return true - } - } - } - - return ok -} - -// EachObjxMap calls the specified callback for each object -// in the [](Map). -// -// Panics if the object is the wrong type. -func (v *Value) EachObjxMap(callback func(int, Map) bool) *Value { - for index, val := range v.MustObjxMapSlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereObjxMap uses the specified decider function to select items -// from the [](Map). The object contained in the result will contain -// only the selected items. -func (v *Value) WhereObjxMap(decider func(int, Map) bool) *Value { - var selected [](Map) - v.EachObjxMap(func(index int, val Map) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupObjxMap uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][](Map). -func (v *Value) GroupObjxMap(grouper func(int, Map) string) *Value { - groups := make(map[string][](Map)) - v.EachObjxMap(func(index int, val Map) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([](Map), 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceObjxMap uses the specified function to replace each (Map)s -// by iterating each item. The data in the returned result will be a -// [](Map) containing the replaced items. -func (v *Value) ReplaceObjxMap(replacer func(int, Map) Map) *Value { - arr := v.MustObjxMapSlice() - replaced := make([](Map), len(arr)) - v.EachObjxMap(func(index int, val Map) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectObjxMap uses the specified collector function to collect a value -// for each of the (Map)s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectObjxMap(collector func(int, Map) interface{}) *Value { - arr := v.MustObjxMapSlice() - collected := make([]interface{}, len(arr)) - v.EachObjxMap(func(index int, val Map) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} diff --git a/vendor/github.com/stretchr/objx/type_specific_codegen.go b/vendor/github.com/stretchr/objx/type_specific_codegen.go deleted file mode 100644 index 45850456e..000000000 --- a/vendor/github.com/stretchr/objx/type_specific_codegen.go +++ /dev/null @@ -1,2261 +0,0 @@ -package objx - -/* - Inter (interface{} and []interface{}) -*/ - -// Inter gets the value as a interface{}, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Inter(optionalDefault ...interface{}) interface{} { - if s, ok := v.data.(interface{}); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustInter gets the value as a interface{}. -// -// Panics if the object is not a interface{}. -func (v *Value) MustInter() interface{} { - return v.data.(interface{}) -} - -// InterSlice gets the value as a []interface{}, returns the optionalDefault -// value or nil if the value is not a []interface{}. -func (v *Value) InterSlice(optionalDefault ...[]interface{}) []interface{} { - if s, ok := v.data.([]interface{}); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustInterSlice gets the value as a []interface{}. -// -// Panics if the object is not a []interface{}. -func (v *Value) MustInterSlice() []interface{} { - return v.data.([]interface{}) -} - -// IsInter gets whether the object contained is a interface{} or not. -func (v *Value) IsInter() bool { - _, ok := v.data.(interface{}) - return ok -} - -// IsInterSlice gets whether the object contained is a []interface{} or not. -func (v *Value) IsInterSlice() bool { - _, ok := v.data.([]interface{}) - return ok -} - -// EachInter calls the specified callback for each object -// in the []interface{}. -// -// Panics if the object is the wrong type. -func (v *Value) EachInter(callback func(int, interface{}) bool) *Value { - for index, val := range v.MustInterSlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereInter uses the specified decider function to select items -// from the []interface{}. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereInter(decider func(int, interface{}) bool) *Value { - var selected []interface{} - v.EachInter(func(index int, val interface{}) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupInter uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]interface{}. -func (v *Value) GroupInter(grouper func(int, interface{}) string) *Value { - groups := make(map[string][]interface{}) - v.EachInter(func(index int, val interface{}) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]interface{}, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceInter uses the specified function to replace each interface{}s -// by iterating each item. The data in the returned result will be a -// []interface{} containing the replaced items. -func (v *Value) ReplaceInter(replacer func(int, interface{}) interface{}) *Value { - arr := v.MustInterSlice() - replaced := make([]interface{}, len(arr)) - v.EachInter(func(index int, val interface{}) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectInter uses the specified collector function to collect a value -// for each of the interface{}s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectInter(collector func(int, interface{}) interface{}) *Value { - arr := v.MustInterSlice() - collected := make([]interface{}, len(arr)) - v.EachInter(func(index int, val interface{}) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Bool (bool and []bool) -*/ - -// Bool gets the value as a bool, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Bool(optionalDefault ...bool) bool { - if s, ok := v.data.(bool); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return false -} - -// MustBool gets the value as a bool. -// -// Panics if the object is not a bool. -func (v *Value) MustBool() bool { - return v.data.(bool) -} - -// BoolSlice gets the value as a []bool, returns the optionalDefault -// value or nil if the value is not a []bool. -func (v *Value) BoolSlice(optionalDefault ...[]bool) []bool { - if s, ok := v.data.([]bool); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustBoolSlice gets the value as a []bool. -// -// Panics if the object is not a []bool. -func (v *Value) MustBoolSlice() []bool { - return v.data.([]bool) -} - -// IsBool gets whether the object contained is a bool or not. -func (v *Value) IsBool() bool { - _, ok := v.data.(bool) - return ok -} - -// IsBoolSlice gets whether the object contained is a []bool or not. -func (v *Value) IsBoolSlice() bool { - _, ok := v.data.([]bool) - return ok -} - -// EachBool calls the specified callback for each object -// in the []bool. -// -// Panics if the object is the wrong type. -func (v *Value) EachBool(callback func(int, bool) bool) *Value { - for index, val := range v.MustBoolSlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereBool uses the specified decider function to select items -// from the []bool. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereBool(decider func(int, bool) bool) *Value { - var selected []bool - v.EachBool(func(index int, val bool) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupBool uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]bool. -func (v *Value) GroupBool(grouper func(int, bool) string) *Value { - groups := make(map[string][]bool) - v.EachBool(func(index int, val bool) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]bool, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceBool uses the specified function to replace each bools -// by iterating each item. The data in the returned result will be a -// []bool containing the replaced items. -func (v *Value) ReplaceBool(replacer func(int, bool) bool) *Value { - arr := v.MustBoolSlice() - replaced := make([]bool, len(arr)) - v.EachBool(func(index int, val bool) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectBool uses the specified collector function to collect a value -// for each of the bools in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectBool(collector func(int, bool) interface{}) *Value { - arr := v.MustBoolSlice() - collected := make([]interface{}, len(arr)) - v.EachBool(func(index int, val bool) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Str (string and []string) -*/ - -// Str gets the value as a string, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Str(optionalDefault ...string) string { - if s, ok := v.data.(string); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return "" -} - -// MustStr gets the value as a string. -// -// Panics if the object is not a string. -func (v *Value) MustStr() string { - return v.data.(string) -} - -// StrSlice gets the value as a []string, returns the optionalDefault -// value or nil if the value is not a []string. -func (v *Value) StrSlice(optionalDefault ...[]string) []string { - if s, ok := v.data.([]string); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustStrSlice gets the value as a []string. -// -// Panics if the object is not a []string. -func (v *Value) MustStrSlice() []string { - return v.data.([]string) -} - -// IsStr gets whether the object contained is a string or not. -func (v *Value) IsStr() bool { - _, ok := v.data.(string) - return ok -} - -// IsStrSlice gets whether the object contained is a []string or not. -func (v *Value) IsStrSlice() bool { - _, ok := v.data.([]string) - return ok -} - -// EachStr calls the specified callback for each object -// in the []string. -// -// Panics if the object is the wrong type. -func (v *Value) EachStr(callback func(int, string) bool) *Value { - for index, val := range v.MustStrSlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereStr uses the specified decider function to select items -// from the []string. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereStr(decider func(int, string) bool) *Value { - var selected []string - v.EachStr(func(index int, val string) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupStr uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]string. -func (v *Value) GroupStr(grouper func(int, string) string) *Value { - groups := make(map[string][]string) - v.EachStr(func(index int, val string) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]string, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceStr uses the specified function to replace each strings -// by iterating each item. The data in the returned result will be a -// []string containing the replaced items. -func (v *Value) ReplaceStr(replacer func(int, string) string) *Value { - arr := v.MustStrSlice() - replaced := make([]string, len(arr)) - v.EachStr(func(index int, val string) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectStr uses the specified collector function to collect a value -// for each of the strings in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectStr(collector func(int, string) interface{}) *Value { - arr := v.MustStrSlice() - collected := make([]interface{}, len(arr)) - v.EachStr(func(index int, val string) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Int (int and []int) -*/ - -// Int gets the value as a int, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Int(optionalDefault ...int) int { - if s, ok := v.data.(int); ok { - return s - } - if s, ok := v.data.(float64); ok { - if float64(int(s)) == s { - return int(s) - } - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustInt gets the value as a int. -// -// Panics if the object is not a int. -func (v *Value) MustInt() int { - if s, ok := v.data.(float64); ok { - if float64(int(s)) == s { - return int(s) - } - } - return v.data.(int) -} - -// IntSlice gets the value as a []int, returns the optionalDefault -// value or nil if the value is not a []int. -func (v *Value) IntSlice(optionalDefault ...[]int) []int { - if s, ok := v.data.([]int); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustIntSlice gets the value as a []int. -// -// Panics if the object is not a []int. -func (v *Value) MustIntSlice() []int { - return v.data.([]int) -} - -// IsInt gets whether the object contained is a int or not. -func (v *Value) IsInt() bool { - _, ok := v.data.(int) - return ok -} - -// IsIntSlice gets whether the object contained is a []int or not. -func (v *Value) IsIntSlice() bool { - _, ok := v.data.([]int) - return ok -} - -// EachInt calls the specified callback for each object -// in the []int. -// -// Panics if the object is the wrong type. -func (v *Value) EachInt(callback func(int, int) bool) *Value { - for index, val := range v.MustIntSlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereInt uses the specified decider function to select items -// from the []int. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereInt(decider func(int, int) bool) *Value { - var selected []int - v.EachInt(func(index int, val int) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupInt uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]int. -func (v *Value) GroupInt(grouper func(int, int) string) *Value { - groups := make(map[string][]int) - v.EachInt(func(index int, val int) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]int, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceInt uses the specified function to replace each ints -// by iterating each item. The data in the returned result will be a -// []int containing the replaced items. -func (v *Value) ReplaceInt(replacer func(int, int) int) *Value { - arr := v.MustIntSlice() - replaced := make([]int, len(arr)) - v.EachInt(func(index int, val int) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectInt uses the specified collector function to collect a value -// for each of the ints in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectInt(collector func(int, int) interface{}) *Value { - arr := v.MustIntSlice() - collected := make([]interface{}, len(arr)) - v.EachInt(func(index int, val int) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Int8 (int8 and []int8) -*/ - -// Int8 gets the value as a int8, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Int8(optionalDefault ...int8) int8 { - if s, ok := v.data.(int8); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustInt8 gets the value as a int8. -// -// Panics if the object is not a int8. -func (v *Value) MustInt8() int8 { - return v.data.(int8) -} - -// Int8Slice gets the value as a []int8, returns the optionalDefault -// value or nil if the value is not a []int8. -func (v *Value) Int8Slice(optionalDefault ...[]int8) []int8 { - if s, ok := v.data.([]int8); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustInt8Slice gets the value as a []int8. -// -// Panics if the object is not a []int8. -func (v *Value) MustInt8Slice() []int8 { - return v.data.([]int8) -} - -// IsInt8 gets whether the object contained is a int8 or not. -func (v *Value) IsInt8() bool { - _, ok := v.data.(int8) - return ok -} - -// IsInt8Slice gets whether the object contained is a []int8 or not. -func (v *Value) IsInt8Slice() bool { - _, ok := v.data.([]int8) - return ok -} - -// EachInt8 calls the specified callback for each object -// in the []int8. -// -// Panics if the object is the wrong type. -func (v *Value) EachInt8(callback func(int, int8) bool) *Value { - for index, val := range v.MustInt8Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereInt8 uses the specified decider function to select items -// from the []int8. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereInt8(decider func(int, int8) bool) *Value { - var selected []int8 - v.EachInt8(func(index int, val int8) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupInt8 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]int8. -func (v *Value) GroupInt8(grouper func(int, int8) string) *Value { - groups := make(map[string][]int8) - v.EachInt8(func(index int, val int8) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]int8, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceInt8 uses the specified function to replace each int8s -// by iterating each item. The data in the returned result will be a -// []int8 containing the replaced items. -func (v *Value) ReplaceInt8(replacer func(int, int8) int8) *Value { - arr := v.MustInt8Slice() - replaced := make([]int8, len(arr)) - v.EachInt8(func(index int, val int8) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectInt8 uses the specified collector function to collect a value -// for each of the int8s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectInt8(collector func(int, int8) interface{}) *Value { - arr := v.MustInt8Slice() - collected := make([]interface{}, len(arr)) - v.EachInt8(func(index int, val int8) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Int16 (int16 and []int16) -*/ - -// Int16 gets the value as a int16, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Int16(optionalDefault ...int16) int16 { - if s, ok := v.data.(int16); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustInt16 gets the value as a int16. -// -// Panics if the object is not a int16. -func (v *Value) MustInt16() int16 { - return v.data.(int16) -} - -// Int16Slice gets the value as a []int16, returns the optionalDefault -// value or nil if the value is not a []int16. -func (v *Value) Int16Slice(optionalDefault ...[]int16) []int16 { - if s, ok := v.data.([]int16); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustInt16Slice gets the value as a []int16. -// -// Panics if the object is not a []int16. -func (v *Value) MustInt16Slice() []int16 { - return v.data.([]int16) -} - -// IsInt16 gets whether the object contained is a int16 or not. -func (v *Value) IsInt16() bool { - _, ok := v.data.(int16) - return ok -} - -// IsInt16Slice gets whether the object contained is a []int16 or not. -func (v *Value) IsInt16Slice() bool { - _, ok := v.data.([]int16) - return ok -} - -// EachInt16 calls the specified callback for each object -// in the []int16. -// -// Panics if the object is the wrong type. -func (v *Value) EachInt16(callback func(int, int16) bool) *Value { - for index, val := range v.MustInt16Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereInt16 uses the specified decider function to select items -// from the []int16. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereInt16(decider func(int, int16) bool) *Value { - var selected []int16 - v.EachInt16(func(index int, val int16) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupInt16 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]int16. -func (v *Value) GroupInt16(grouper func(int, int16) string) *Value { - groups := make(map[string][]int16) - v.EachInt16(func(index int, val int16) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]int16, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceInt16 uses the specified function to replace each int16s -// by iterating each item. The data in the returned result will be a -// []int16 containing the replaced items. -func (v *Value) ReplaceInt16(replacer func(int, int16) int16) *Value { - arr := v.MustInt16Slice() - replaced := make([]int16, len(arr)) - v.EachInt16(func(index int, val int16) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectInt16 uses the specified collector function to collect a value -// for each of the int16s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectInt16(collector func(int, int16) interface{}) *Value { - arr := v.MustInt16Slice() - collected := make([]interface{}, len(arr)) - v.EachInt16(func(index int, val int16) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Int32 (int32 and []int32) -*/ - -// Int32 gets the value as a int32, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Int32(optionalDefault ...int32) int32 { - if s, ok := v.data.(int32); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustInt32 gets the value as a int32. -// -// Panics if the object is not a int32. -func (v *Value) MustInt32() int32 { - return v.data.(int32) -} - -// Int32Slice gets the value as a []int32, returns the optionalDefault -// value or nil if the value is not a []int32. -func (v *Value) Int32Slice(optionalDefault ...[]int32) []int32 { - if s, ok := v.data.([]int32); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustInt32Slice gets the value as a []int32. -// -// Panics if the object is not a []int32. -func (v *Value) MustInt32Slice() []int32 { - return v.data.([]int32) -} - -// IsInt32 gets whether the object contained is a int32 or not. -func (v *Value) IsInt32() bool { - _, ok := v.data.(int32) - return ok -} - -// IsInt32Slice gets whether the object contained is a []int32 or not. -func (v *Value) IsInt32Slice() bool { - _, ok := v.data.([]int32) - return ok -} - -// EachInt32 calls the specified callback for each object -// in the []int32. -// -// Panics if the object is the wrong type. -func (v *Value) EachInt32(callback func(int, int32) bool) *Value { - for index, val := range v.MustInt32Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereInt32 uses the specified decider function to select items -// from the []int32. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereInt32(decider func(int, int32) bool) *Value { - var selected []int32 - v.EachInt32(func(index int, val int32) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupInt32 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]int32. -func (v *Value) GroupInt32(grouper func(int, int32) string) *Value { - groups := make(map[string][]int32) - v.EachInt32(func(index int, val int32) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]int32, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceInt32 uses the specified function to replace each int32s -// by iterating each item. The data in the returned result will be a -// []int32 containing the replaced items. -func (v *Value) ReplaceInt32(replacer func(int, int32) int32) *Value { - arr := v.MustInt32Slice() - replaced := make([]int32, len(arr)) - v.EachInt32(func(index int, val int32) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectInt32 uses the specified collector function to collect a value -// for each of the int32s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectInt32(collector func(int, int32) interface{}) *Value { - arr := v.MustInt32Slice() - collected := make([]interface{}, len(arr)) - v.EachInt32(func(index int, val int32) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Int64 (int64 and []int64) -*/ - -// Int64 gets the value as a int64, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Int64(optionalDefault ...int64) int64 { - if s, ok := v.data.(int64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustInt64 gets the value as a int64. -// -// Panics if the object is not a int64. -func (v *Value) MustInt64() int64 { - return v.data.(int64) -} - -// Int64Slice gets the value as a []int64, returns the optionalDefault -// value or nil if the value is not a []int64. -func (v *Value) Int64Slice(optionalDefault ...[]int64) []int64 { - if s, ok := v.data.([]int64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustInt64Slice gets the value as a []int64. -// -// Panics if the object is not a []int64. -func (v *Value) MustInt64Slice() []int64 { - return v.data.([]int64) -} - -// IsInt64 gets whether the object contained is a int64 or not. -func (v *Value) IsInt64() bool { - _, ok := v.data.(int64) - return ok -} - -// IsInt64Slice gets whether the object contained is a []int64 or not. -func (v *Value) IsInt64Slice() bool { - _, ok := v.data.([]int64) - return ok -} - -// EachInt64 calls the specified callback for each object -// in the []int64. -// -// Panics if the object is the wrong type. -func (v *Value) EachInt64(callback func(int, int64) bool) *Value { - for index, val := range v.MustInt64Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereInt64 uses the specified decider function to select items -// from the []int64. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereInt64(decider func(int, int64) bool) *Value { - var selected []int64 - v.EachInt64(func(index int, val int64) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupInt64 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]int64. -func (v *Value) GroupInt64(grouper func(int, int64) string) *Value { - groups := make(map[string][]int64) - v.EachInt64(func(index int, val int64) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]int64, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceInt64 uses the specified function to replace each int64s -// by iterating each item. The data in the returned result will be a -// []int64 containing the replaced items. -func (v *Value) ReplaceInt64(replacer func(int, int64) int64) *Value { - arr := v.MustInt64Slice() - replaced := make([]int64, len(arr)) - v.EachInt64(func(index int, val int64) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectInt64 uses the specified collector function to collect a value -// for each of the int64s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectInt64(collector func(int, int64) interface{}) *Value { - arr := v.MustInt64Slice() - collected := make([]interface{}, len(arr)) - v.EachInt64(func(index int, val int64) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Uint (uint and []uint) -*/ - -// Uint gets the value as a uint, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Uint(optionalDefault ...uint) uint { - if s, ok := v.data.(uint); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustUint gets the value as a uint. -// -// Panics if the object is not a uint. -func (v *Value) MustUint() uint { - return v.data.(uint) -} - -// UintSlice gets the value as a []uint, returns the optionalDefault -// value or nil if the value is not a []uint. -func (v *Value) UintSlice(optionalDefault ...[]uint) []uint { - if s, ok := v.data.([]uint); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustUintSlice gets the value as a []uint. -// -// Panics if the object is not a []uint. -func (v *Value) MustUintSlice() []uint { - return v.data.([]uint) -} - -// IsUint gets whether the object contained is a uint or not. -func (v *Value) IsUint() bool { - _, ok := v.data.(uint) - return ok -} - -// IsUintSlice gets whether the object contained is a []uint or not. -func (v *Value) IsUintSlice() bool { - _, ok := v.data.([]uint) - return ok -} - -// EachUint calls the specified callback for each object -// in the []uint. -// -// Panics if the object is the wrong type. -func (v *Value) EachUint(callback func(int, uint) bool) *Value { - for index, val := range v.MustUintSlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereUint uses the specified decider function to select items -// from the []uint. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereUint(decider func(int, uint) bool) *Value { - var selected []uint - v.EachUint(func(index int, val uint) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupUint uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]uint. -func (v *Value) GroupUint(grouper func(int, uint) string) *Value { - groups := make(map[string][]uint) - v.EachUint(func(index int, val uint) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]uint, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceUint uses the specified function to replace each uints -// by iterating each item. The data in the returned result will be a -// []uint containing the replaced items. -func (v *Value) ReplaceUint(replacer func(int, uint) uint) *Value { - arr := v.MustUintSlice() - replaced := make([]uint, len(arr)) - v.EachUint(func(index int, val uint) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectUint uses the specified collector function to collect a value -// for each of the uints in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectUint(collector func(int, uint) interface{}) *Value { - arr := v.MustUintSlice() - collected := make([]interface{}, len(arr)) - v.EachUint(func(index int, val uint) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Uint8 (uint8 and []uint8) -*/ - -// Uint8 gets the value as a uint8, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Uint8(optionalDefault ...uint8) uint8 { - if s, ok := v.data.(uint8); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustUint8 gets the value as a uint8. -// -// Panics if the object is not a uint8. -func (v *Value) MustUint8() uint8 { - return v.data.(uint8) -} - -// Uint8Slice gets the value as a []uint8, returns the optionalDefault -// value or nil if the value is not a []uint8. -func (v *Value) Uint8Slice(optionalDefault ...[]uint8) []uint8 { - if s, ok := v.data.([]uint8); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustUint8Slice gets the value as a []uint8. -// -// Panics if the object is not a []uint8. -func (v *Value) MustUint8Slice() []uint8 { - return v.data.([]uint8) -} - -// IsUint8 gets whether the object contained is a uint8 or not. -func (v *Value) IsUint8() bool { - _, ok := v.data.(uint8) - return ok -} - -// IsUint8Slice gets whether the object contained is a []uint8 or not. -func (v *Value) IsUint8Slice() bool { - _, ok := v.data.([]uint8) - return ok -} - -// EachUint8 calls the specified callback for each object -// in the []uint8. -// -// Panics if the object is the wrong type. -func (v *Value) EachUint8(callback func(int, uint8) bool) *Value { - for index, val := range v.MustUint8Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereUint8 uses the specified decider function to select items -// from the []uint8. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereUint8(decider func(int, uint8) bool) *Value { - var selected []uint8 - v.EachUint8(func(index int, val uint8) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupUint8 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]uint8. -func (v *Value) GroupUint8(grouper func(int, uint8) string) *Value { - groups := make(map[string][]uint8) - v.EachUint8(func(index int, val uint8) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]uint8, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceUint8 uses the specified function to replace each uint8s -// by iterating each item. The data in the returned result will be a -// []uint8 containing the replaced items. -func (v *Value) ReplaceUint8(replacer func(int, uint8) uint8) *Value { - arr := v.MustUint8Slice() - replaced := make([]uint8, len(arr)) - v.EachUint8(func(index int, val uint8) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectUint8 uses the specified collector function to collect a value -// for each of the uint8s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectUint8(collector func(int, uint8) interface{}) *Value { - arr := v.MustUint8Slice() - collected := make([]interface{}, len(arr)) - v.EachUint8(func(index int, val uint8) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Uint16 (uint16 and []uint16) -*/ - -// Uint16 gets the value as a uint16, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Uint16(optionalDefault ...uint16) uint16 { - if s, ok := v.data.(uint16); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustUint16 gets the value as a uint16. -// -// Panics if the object is not a uint16. -func (v *Value) MustUint16() uint16 { - return v.data.(uint16) -} - -// Uint16Slice gets the value as a []uint16, returns the optionalDefault -// value or nil if the value is not a []uint16. -func (v *Value) Uint16Slice(optionalDefault ...[]uint16) []uint16 { - if s, ok := v.data.([]uint16); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustUint16Slice gets the value as a []uint16. -// -// Panics if the object is not a []uint16. -func (v *Value) MustUint16Slice() []uint16 { - return v.data.([]uint16) -} - -// IsUint16 gets whether the object contained is a uint16 or not. -func (v *Value) IsUint16() bool { - _, ok := v.data.(uint16) - return ok -} - -// IsUint16Slice gets whether the object contained is a []uint16 or not. -func (v *Value) IsUint16Slice() bool { - _, ok := v.data.([]uint16) - return ok -} - -// EachUint16 calls the specified callback for each object -// in the []uint16. -// -// Panics if the object is the wrong type. -func (v *Value) EachUint16(callback func(int, uint16) bool) *Value { - for index, val := range v.MustUint16Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereUint16 uses the specified decider function to select items -// from the []uint16. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereUint16(decider func(int, uint16) bool) *Value { - var selected []uint16 - v.EachUint16(func(index int, val uint16) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupUint16 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]uint16. -func (v *Value) GroupUint16(grouper func(int, uint16) string) *Value { - groups := make(map[string][]uint16) - v.EachUint16(func(index int, val uint16) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]uint16, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceUint16 uses the specified function to replace each uint16s -// by iterating each item. The data in the returned result will be a -// []uint16 containing the replaced items. -func (v *Value) ReplaceUint16(replacer func(int, uint16) uint16) *Value { - arr := v.MustUint16Slice() - replaced := make([]uint16, len(arr)) - v.EachUint16(func(index int, val uint16) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectUint16 uses the specified collector function to collect a value -// for each of the uint16s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectUint16(collector func(int, uint16) interface{}) *Value { - arr := v.MustUint16Slice() - collected := make([]interface{}, len(arr)) - v.EachUint16(func(index int, val uint16) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Uint32 (uint32 and []uint32) -*/ - -// Uint32 gets the value as a uint32, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Uint32(optionalDefault ...uint32) uint32 { - if s, ok := v.data.(uint32); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustUint32 gets the value as a uint32. -// -// Panics if the object is not a uint32. -func (v *Value) MustUint32() uint32 { - return v.data.(uint32) -} - -// Uint32Slice gets the value as a []uint32, returns the optionalDefault -// value or nil if the value is not a []uint32. -func (v *Value) Uint32Slice(optionalDefault ...[]uint32) []uint32 { - if s, ok := v.data.([]uint32); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustUint32Slice gets the value as a []uint32. -// -// Panics if the object is not a []uint32. -func (v *Value) MustUint32Slice() []uint32 { - return v.data.([]uint32) -} - -// IsUint32 gets whether the object contained is a uint32 or not. -func (v *Value) IsUint32() bool { - _, ok := v.data.(uint32) - return ok -} - -// IsUint32Slice gets whether the object contained is a []uint32 or not. -func (v *Value) IsUint32Slice() bool { - _, ok := v.data.([]uint32) - return ok -} - -// EachUint32 calls the specified callback for each object -// in the []uint32. -// -// Panics if the object is the wrong type. -func (v *Value) EachUint32(callback func(int, uint32) bool) *Value { - for index, val := range v.MustUint32Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereUint32 uses the specified decider function to select items -// from the []uint32. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereUint32(decider func(int, uint32) bool) *Value { - var selected []uint32 - v.EachUint32(func(index int, val uint32) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupUint32 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]uint32. -func (v *Value) GroupUint32(grouper func(int, uint32) string) *Value { - groups := make(map[string][]uint32) - v.EachUint32(func(index int, val uint32) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]uint32, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceUint32 uses the specified function to replace each uint32s -// by iterating each item. The data in the returned result will be a -// []uint32 containing the replaced items. -func (v *Value) ReplaceUint32(replacer func(int, uint32) uint32) *Value { - arr := v.MustUint32Slice() - replaced := make([]uint32, len(arr)) - v.EachUint32(func(index int, val uint32) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectUint32 uses the specified collector function to collect a value -// for each of the uint32s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectUint32(collector func(int, uint32) interface{}) *Value { - arr := v.MustUint32Slice() - collected := make([]interface{}, len(arr)) - v.EachUint32(func(index int, val uint32) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Uint64 (uint64 and []uint64) -*/ - -// Uint64 gets the value as a uint64, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Uint64(optionalDefault ...uint64) uint64 { - if s, ok := v.data.(uint64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustUint64 gets the value as a uint64. -// -// Panics if the object is not a uint64. -func (v *Value) MustUint64() uint64 { - return v.data.(uint64) -} - -// Uint64Slice gets the value as a []uint64, returns the optionalDefault -// value or nil if the value is not a []uint64. -func (v *Value) Uint64Slice(optionalDefault ...[]uint64) []uint64 { - if s, ok := v.data.([]uint64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustUint64Slice gets the value as a []uint64. -// -// Panics if the object is not a []uint64. -func (v *Value) MustUint64Slice() []uint64 { - return v.data.([]uint64) -} - -// IsUint64 gets whether the object contained is a uint64 or not. -func (v *Value) IsUint64() bool { - _, ok := v.data.(uint64) - return ok -} - -// IsUint64Slice gets whether the object contained is a []uint64 or not. -func (v *Value) IsUint64Slice() bool { - _, ok := v.data.([]uint64) - return ok -} - -// EachUint64 calls the specified callback for each object -// in the []uint64. -// -// Panics if the object is the wrong type. -func (v *Value) EachUint64(callback func(int, uint64) bool) *Value { - for index, val := range v.MustUint64Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereUint64 uses the specified decider function to select items -// from the []uint64. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereUint64(decider func(int, uint64) bool) *Value { - var selected []uint64 - v.EachUint64(func(index int, val uint64) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupUint64 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]uint64. -func (v *Value) GroupUint64(grouper func(int, uint64) string) *Value { - groups := make(map[string][]uint64) - v.EachUint64(func(index int, val uint64) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]uint64, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceUint64 uses the specified function to replace each uint64s -// by iterating each item. The data in the returned result will be a -// []uint64 containing the replaced items. -func (v *Value) ReplaceUint64(replacer func(int, uint64) uint64) *Value { - arr := v.MustUint64Slice() - replaced := make([]uint64, len(arr)) - v.EachUint64(func(index int, val uint64) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectUint64 uses the specified collector function to collect a value -// for each of the uint64s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectUint64(collector func(int, uint64) interface{}) *Value { - arr := v.MustUint64Slice() - collected := make([]interface{}, len(arr)) - v.EachUint64(func(index int, val uint64) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Uintptr (uintptr and []uintptr) -*/ - -// Uintptr gets the value as a uintptr, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Uintptr(optionalDefault ...uintptr) uintptr { - if s, ok := v.data.(uintptr); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustUintptr gets the value as a uintptr. -// -// Panics if the object is not a uintptr. -func (v *Value) MustUintptr() uintptr { - return v.data.(uintptr) -} - -// UintptrSlice gets the value as a []uintptr, returns the optionalDefault -// value or nil if the value is not a []uintptr. -func (v *Value) UintptrSlice(optionalDefault ...[]uintptr) []uintptr { - if s, ok := v.data.([]uintptr); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustUintptrSlice gets the value as a []uintptr. -// -// Panics if the object is not a []uintptr. -func (v *Value) MustUintptrSlice() []uintptr { - return v.data.([]uintptr) -} - -// IsUintptr gets whether the object contained is a uintptr or not. -func (v *Value) IsUintptr() bool { - _, ok := v.data.(uintptr) - return ok -} - -// IsUintptrSlice gets whether the object contained is a []uintptr or not. -func (v *Value) IsUintptrSlice() bool { - _, ok := v.data.([]uintptr) - return ok -} - -// EachUintptr calls the specified callback for each object -// in the []uintptr. -// -// Panics if the object is the wrong type. -func (v *Value) EachUintptr(callback func(int, uintptr) bool) *Value { - for index, val := range v.MustUintptrSlice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereUintptr uses the specified decider function to select items -// from the []uintptr. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereUintptr(decider func(int, uintptr) bool) *Value { - var selected []uintptr - v.EachUintptr(func(index int, val uintptr) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupUintptr uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]uintptr. -func (v *Value) GroupUintptr(grouper func(int, uintptr) string) *Value { - groups := make(map[string][]uintptr) - v.EachUintptr(func(index int, val uintptr) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]uintptr, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceUintptr uses the specified function to replace each uintptrs -// by iterating each item. The data in the returned result will be a -// []uintptr containing the replaced items. -func (v *Value) ReplaceUintptr(replacer func(int, uintptr) uintptr) *Value { - arr := v.MustUintptrSlice() - replaced := make([]uintptr, len(arr)) - v.EachUintptr(func(index int, val uintptr) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectUintptr uses the specified collector function to collect a value -// for each of the uintptrs in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectUintptr(collector func(int, uintptr) interface{}) *Value { - arr := v.MustUintptrSlice() - collected := make([]interface{}, len(arr)) - v.EachUintptr(func(index int, val uintptr) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Float32 (float32 and []float32) -*/ - -// Float32 gets the value as a float32, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Float32(optionalDefault ...float32) float32 { - if s, ok := v.data.(float32); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustFloat32 gets the value as a float32. -// -// Panics if the object is not a float32. -func (v *Value) MustFloat32() float32 { - return v.data.(float32) -} - -// Float32Slice gets the value as a []float32, returns the optionalDefault -// value or nil if the value is not a []float32. -func (v *Value) Float32Slice(optionalDefault ...[]float32) []float32 { - if s, ok := v.data.([]float32); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustFloat32Slice gets the value as a []float32. -// -// Panics if the object is not a []float32. -func (v *Value) MustFloat32Slice() []float32 { - return v.data.([]float32) -} - -// IsFloat32 gets whether the object contained is a float32 or not. -func (v *Value) IsFloat32() bool { - _, ok := v.data.(float32) - return ok -} - -// IsFloat32Slice gets whether the object contained is a []float32 or not. -func (v *Value) IsFloat32Slice() bool { - _, ok := v.data.([]float32) - return ok -} - -// EachFloat32 calls the specified callback for each object -// in the []float32. -// -// Panics if the object is the wrong type. -func (v *Value) EachFloat32(callback func(int, float32) bool) *Value { - for index, val := range v.MustFloat32Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereFloat32 uses the specified decider function to select items -// from the []float32. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereFloat32(decider func(int, float32) bool) *Value { - var selected []float32 - v.EachFloat32(func(index int, val float32) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupFloat32 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]float32. -func (v *Value) GroupFloat32(grouper func(int, float32) string) *Value { - groups := make(map[string][]float32) - v.EachFloat32(func(index int, val float32) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]float32, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceFloat32 uses the specified function to replace each float32s -// by iterating each item. The data in the returned result will be a -// []float32 containing the replaced items. -func (v *Value) ReplaceFloat32(replacer func(int, float32) float32) *Value { - arr := v.MustFloat32Slice() - replaced := make([]float32, len(arr)) - v.EachFloat32(func(index int, val float32) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectFloat32 uses the specified collector function to collect a value -// for each of the float32s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectFloat32(collector func(int, float32) interface{}) *Value { - arr := v.MustFloat32Slice() - collected := make([]interface{}, len(arr)) - v.EachFloat32(func(index int, val float32) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Float64 (float64 and []float64) -*/ - -// Float64 gets the value as a float64, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Float64(optionalDefault ...float64) float64 { - if s, ok := v.data.(float64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustFloat64 gets the value as a float64. -// -// Panics if the object is not a float64. -func (v *Value) MustFloat64() float64 { - return v.data.(float64) -} - -// Float64Slice gets the value as a []float64, returns the optionalDefault -// value or nil if the value is not a []float64. -func (v *Value) Float64Slice(optionalDefault ...[]float64) []float64 { - if s, ok := v.data.([]float64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustFloat64Slice gets the value as a []float64. -// -// Panics if the object is not a []float64. -func (v *Value) MustFloat64Slice() []float64 { - return v.data.([]float64) -} - -// IsFloat64 gets whether the object contained is a float64 or not. -func (v *Value) IsFloat64() bool { - _, ok := v.data.(float64) - return ok -} - -// IsFloat64Slice gets whether the object contained is a []float64 or not. -func (v *Value) IsFloat64Slice() bool { - _, ok := v.data.([]float64) - return ok -} - -// EachFloat64 calls the specified callback for each object -// in the []float64. -// -// Panics if the object is the wrong type. -func (v *Value) EachFloat64(callback func(int, float64) bool) *Value { - for index, val := range v.MustFloat64Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereFloat64 uses the specified decider function to select items -// from the []float64. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereFloat64(decider func(int, float64) bool) *Value { - var selected []float64 - v.EachFloat64(func(index int, val float64) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupFloat64 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]float64. -func (v *Value) GroupFloat64(grouper func(int, float64) string) *Value { - groups := make(map[string][]float64) - v.EachFloat64(func(index int, val float64) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]float64, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceFloat64 uses the specified function to replace each float64s -// by iterating each item. The data in the returned result will be a -// []float64 containing the replaced items. -func (v *Value) ReplaceFloat64(replacer func(int, float64) float64) *Value { - arr := v.MustFloat64Slice() - replaced := make([]float64, len(arr)) - v.EachFloat64(func(index int, val float64) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectFloat64 uses the specified collector function to collect a value -// for each of the float64s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectFloat64(collector func(int, float64) interface{}) *Value { - arr := v.MustFloat64Slice() - collected := make([]interface{}, len(arr)) - v.EachFloat64(func(index int, val float64) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Complex64 (complex64 and []complex64) -*/ - -// Complex64 gets the value as a complex64, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Complex64(optionalDefault ...complex64) complex64 { - if s, ok := v.data.(complex64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustComplex64 gets the value as a complex64. -// -// Panics if the object is not a complex64. -func (v *Value) MustComplex64() complex64 { - return v.data.(complex64) -} - -// Complex64Slice gets the value as a []complex64, returns the optionalDefault -// value or nil if the value is not a []complex64. -func (v *Value) Complex64Slice(optionalDefault ...[]complex64) []complex64 { - if s, ok := v.data.([]complex64); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustComplex64Slice gets the value as a []complex64. -// -// Panics if the object is not a []complex64. -func (v *Value) MustComplex64Slice() []complex64 { - return v.data.([]complex64) -} - -// IsComplex64 gets whether the object contained is a complex64 or not. -func (v *Value) IsComplex64() bool { - _, ok := v.data.(complex64) - return ok -} - -// IsComplex64Slice gets whether the object contained is a []complex64 or not. -func (v *Value) IsComplex64Slice() bool { - _, ok := v.data.([]complex64) - return ok -} - -// EachComplex64 calls the specified callback for each object -// in the []complex64. -// -// Panics if the object is the wrong type. -func (v *Value) EachComplex64(callback func(int, complex64) bool) *Value { - for index, val := range v.MustComplex64Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereComplex64 uses the specified decider function to select items -// from the []complex64. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereComplex64(decider func(int, complex64) bool) *Value { - var selected []complex64 - v.EachComplex64(func(index int, val complex64) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupComplex64 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]complex64. -func (v *Value) GroupComplex64(grouper func(int, complex64) string) *Value { - groups := make(map[string][]complex64) - v.EachComplex64(func(index int, val complex64) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]complex64, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceComplex64 uses the specified function to replace each complex64s -// by iterating each item. The data in the returned result will be a -// []complex64 containing the replaced items. -func (v *Value) ReplaceComplex64(replacer func(int, complex64) complex64) *Value { - arr := v.MustComplex64Slice() - replaced := make([]complex64, len(arr)) - v.EachComplex64(func(index int, val complex64) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectComplex64 uses the specified collector function to collect a value -// for each of the complex64s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectComplex64(collector func(int, complex64) interface{}) *Value { - arr := v.MustComplex64Slice() - collected := make([]interface{}, len(arr)) - v.EachComplex64(func(index int, val complex64) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} - -/* - Complex128 (complex128 and []complex128) -*/ - -// Complex128 gets the value as a complex128, returns the optionalDefault -// value or a system default object if the value is the wrong type. -func (v *Value) Complex128(optionalDefault ...complex128) complex128 { - if s, ok := v.data.(complex128); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return 0 -} - -// MustComplex128 gets the value as a complex128. -// -// Panics if the object is not a complex128. -func (v *Value) MustComplex128() complex128 { - return v.data.(complex128) -} - -// Complex128Slice gets the value as a []complex128, returns the optionalDefault -// value or nil if the value is not a []complex128. -func (v *Value) Complex128Slice(optionalDefault ...[]complex128) []complex128 { - if s, ok := v.data.([]complex128); ok { - return s - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - return nil -} - -// MustComplex128Slice gets the value as a []complex128. -// -// Panics if the object is not a []complex128. -func (v *Value) MustComplex128Slice() []complex128 { - return v.data.([]complex128) -} - -// IsComplex128 gets whether the object contained is a complex128 or not. -func (v *Value) IsComplex128() bool { - _, ok := v.data.(complex128) - return ok -} - -// IsComplex128Slice gets whether the object contained is a []complex128 or not. -func (v *Value) IsComplex128Slice() bool { - _, ok := v.data.([]complex128) - return ok -} - -// EachComplex128 calls the specified callback for each object -// in the []complex128. -// -// Panics if the object is the wrong type. -func (v *Value) EachComplex128(callback func(int, complex128) bool) *Value { - for index, val := range v.MustComplex128Slice() { - carryon := callback(index, val) - if !carryon { - break - } - } - return v -} - -// WhereComplex128 uses the specified decider function to select items -// from the []complex128. The object contained in the result will contain -// only the selected items. -func (v *Value) WhereComplex128(decider func(int, complex128) bool) *Value { - var selected []complex128 - v.EachComplex128(func(index int, val complex128) bool { - shouldSelect := decider(index, val) - if !shouldSelect { - selected = append(selected, val) - } - return true - }) - return &Value{data: selected} -} - -// GroupComplex128 uses the specified grouper function to group the items -// keyed by the return of the grouper. The object contained in the -// result will contain a map[string][]complex128. -func (v *Value) GroupComplex128(grouper func(int, complex128) string) *Value { - groups := make(map[string][]complex128) - v.EachComplex128(func(index int, val complex128) bool { - group := grouper(index, val) - if _, ok := groups[group]; !ok { - groups[group] = make([]complex128, 0) - } - groups[group] = append(groups[group], val) - return true - }) - return &Value{data: groups} -} - -// ReplaceComplex128 uses the specified function to replace each complex128s -// by iterating each item. The data in the returned result will be a -// []complex128 containing the replaced items. -func (v *Value) ReplaceComplex128(replacer func(int, complex128) complex128) *Value { - arr := v.MustComplex128Slice() - replaced := make([]complex128, len(arr)) - v.EachComplex128(func(index int, val complex128) bool { - replaced[index] = replacer(index, val) - return true - }) - return &Value{data: replaced} -} - -// CollectComplex128 uses the specified collector function to collect a value -// for each of the complex128s in the slice. The data returned will be a -// []interface{}. -func (v *Value) CollectComplex128(collector func(int, complex128) interface{}) *Value { - arr := v.MustComplex128Slice() - collected := make([]interface{}, len(arr)) - v.EachComplex128(func(index int, val complex128) bool { - collected[index] = collector(index, val) - return true - }) - return &Value{data: collected} -} diff --git a/vendor/github.com/stretchr/objx/value.go b/vendor/github.com/stretchr/objx/value.go deleted file mode 100644 index 4e5f9b77e..000000000 --- a/vendor/github.com/stretchr/objx/value.go +++ /dev/null @@ -1,159 +0,0 @@ -package objx - -import ( - "fmt" - "strconv" -) - -// Value provides methods for extracting interface{} data in various -// types. -type Value struct { - // data contains the raw data being managed by this Value - data interface{} -} - -// Data returns the raw data contained by this Value -func (v *Value) Data() interface{} { - return v.data -} - -// String returns the value always as a string -func (v *Value) String() string { - switch { - case v.IsNil(): - return "" - case v.IsStr(): - return v.Str() - case v.IsBool(): - return strconv.FormatBool(v.Bool()) - case v.IsFloat32(): - return strconv.FormatFloat(float64(v.Float32()), 'f', -1, 32) - case v.IsFloat64(): - return strconv.FormatFloat(v.Float64(), 'f', -1, 64) - case v.IsInt(): - return strconv.FormatInt(int64(v.Int()), 10) - case v.IsInt8(): - return strconv.FormatInt(int64(v.Int8()), 10) - case v.IsInt16(): - return strconv.FormatInt(int64(v.Int16()), 10) - case v.IsInt32(): - return strconv.FormatInt(int64(v.Int32()), 10) - case v.IsInt64(): - return strconv.FormatInt(v.Int64(), 10) - case v.IsUint(): - return strconv.FormatUint(uint64(v.Uint()), 10) - case v.IsUint8(): - return strconv.FormatUint(uint64(v.Uint8()), 10) - case v.IsUint16(): - return strconv.FormatUint(uint64(v.Uint16()), 10) - case v.IsUint32(): - return strconv.FormatUint(uint64(v.Uint32()), 10) - case v.IsUint64(): - return strconv.FormatUint(v.Uint64(), 10) - } - return fmt.Sprintf("%#v", v.Data()) -} - -// StringSlice returns the value always as a []string -func (v *Value) StringSlice(optionalDefault ...[]string) []string { - switch { - case v.IsStrSlice(): - return v.MustStrSlice() - case v.IsBoolSlice(): - slice := v.MustBoolSlice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatBool(iv) - } - return vals - case v.IsFloat32Slice(): - slice := v.MustFloat32Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatFloat(float64(iv), 'f', -1, 32) - } - return vals - case v.IsFloat64Slice(): - slice := v.MustFloat64Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatFloat(iv, 'f', -1, 64) - } - return vals - case v.IsIntSlice(): - slice := v.MustIntSlice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatInt(int64(iv), 10) - } - return vals - case v.IsInt8Slice(): - slice := v.MustInt8Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatInt(int64(iv), 10) - } - return vals - case v.IsInt16Slice(): - slice := v.MustInt16Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatInt(int64(iv), 10) - } - return vals - case v.IsInt32Slice(): - slice := v.MustInt32Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatInt(int64(iv), 10) - } - return vals - case v.IsInt64Slice(): - slice := v.MustInt64Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatInt(iv, 10) - } - return vals - case v.IsUintSlice(): - slice := v.MustUintSlice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatUint(uint64(iv), 10) - } - return vals - case v.IsUint8Slice(): - slice := v.MustUint8Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatUint(uint64(iv), 10) - } - return vals - case v.IsUint16Slice(): - slice := v.MustUint16Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatUint(uint64(iv), 10) - } - return vals - case v.IsUint32Slice(): - slice := v.MustUint32Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatUint(uint64(iv), 10) - } - return vals - case v.IsUint64Slice(): - slice := v.MustUint64Slice() - vals := make([]string, len(slice)) - for i, iv := range slice { - vals[i] = strconv.FormatUint(iv, 10) - } - return vals - } - if len(optionalDefault) == 1 { - return optionalDefault[0] - } - - return []string{} -} diff --git a/vendor/github.com/stretchr/testify/mock/doc.go b/vendor/github.com/stretchr/testify/mock/doc.go deleted file mode 100644 index d6b3c844c..000000000 --- a/vendor/github.com/stretchr/testify/mock/doc.go +++ /dev/null @@ -1,44 +0,0 @@ -// Package mock provides a system by which it is possible to mock your objects -// and verify calls are happening as expected. -// -// # Example Usage -// -// The mock package provides an object, Mock, that tracks activity on another object. It is usually -// embedded into a test object as shown below: -// -// type MyTestObject struct { -// // add a Mock object instance -// mock.Mock -// -// // other fields go here as normal -// } -// -// When implementing the methods of an interface, you wire your functions up -// to call the Mock.Called(args...) method, and return the appropriate values. -// -// For example, to mock a method that saves the name and age of a person and returns -// the year of their birth or an error, you might write this: -// -// func (o *MyTestObject) SavePersonDetails(firstname, lastname string, age int) (int, error) { -// args := o.Called(firstname, lastname, age) -// return args.Int(0), args.Error(1) -// } -// -// The Int, Error and Bool methods are examples of strongly typed getters that take the argument -// index position. Given this argument list: -// -// (12, true, "Something") -// -// You could read them out strongly typed like this: -// -// args.Int(0) -// args.Bool(1) -// args.String(2) -// -// For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion: -// -// return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine) -// -// This may cause a panic if the object you are getting is nil (the type assertion will fail), in those -// cases you should check for nil first. -package mock diff --git a/vendor/github.com/stretchr/testify/mock/mock.go b/vendor/github.com/stretchr/testify/mock/mock.go deleted file mode 100644 index 213bde2ea..000000000 --- a/vendor/github.com/stretchr/testify/mock/mock.go +++ /dev/null @@ -1,1241 +0,0 @@ -package mock - -import ( - "errors" - "fmt" - "path" - "reflect" - "regexp" - "runtime" - "strings" - "sync" - "time" - - "github.com/davecgh/go-spew/spew" - "github.com/pmezard/go-difflib/difflib" - "github.com/stretchr/objx" - - "github.com/stretchr/testify/assert" -) - -// regex for GCCGO functions -var gccgoRE = regexp.MustCompile(`\.pN\d+_`) - -// TestingT is an interface wrapper around *testing.T -type TestingT interface { - Logf(format string, args ...interface{}) - Errorf(format string, args ...interface{}) - FailNow() -} - -/* - Call -*/ - -// Call represents a method call and is used for setting expectations, -// as well as recording activity. -type Call struct { - Parent *Mock - - // The name of the method that was or will be called. - Method string - - // Holds the arguments of the method. - Arguments Arguments - - // Holds the arguments that should be returned when - // this method is called. - ReturnArguments Arguments - - // Holds the caller info for the On() call - callerInfo []string - - // The number of times to return the return arguments when setting - // expectations. 0 means to always return the value. - Repeatability int - - // Amount of times this call has been called - totalCalls int - - // Call to this method can be optional - optional bool - - // Holds a channel that will be used to block the Return until it either - // receives a message or is closed. nil means it returns immediately. - WaitFor <-chan time.Time - - waitTime time.Duration - - // Holds a handler used to manipulate arguments content that are passed by - // reference. It's useful when mocking methods such as unmarshalers or - // decoders. - RunFn func(Arguments) - - // PanicMsg holds msg to be used to mock panic on the function call - // if the PanicMsg is set to a non nil string the function call will panic - // irrespective of other settings - PanicMsg *string - - // Calls which must be satisfied before this call can be - requires []*Call -} - -func newCall(parent *Mock, methodName string, callerInfo []string, methodArguments ...interface{}) *Call { - return &Call{ - Parent: parent, - Method: methodName, - Arguments: methodArguments, - ReturnArguments: make([]interface{}, 0), - callerInfo: callerInfo, - Repeatability: 0, - WaitFor: nil, - RunFn: nil, - PanicMsg: nil, - } -} - -func (c *Call) lock() { - c.Parent.mutex.Lock() -} - -func (c *Call) unlock() { - c.Parent.mutex.Unlock() -} - -// Return specifies the return arguments for the expectation. -// -// Mock.On("DoSomething").Return(errors.New("failed")) -func (c *Call) Return(returnArguments ...interface{}) *Call { - c.lock() - defer c.unlock() - - c.ReturnArguments = returnArguments - - return c -} - -// Panic specifies if the function call should fail and the panic message -// -// Mock.On("DoSomething").Panic("test panic") -func (c *Call) Panic(msg string) *Call { - c.lock() - defer c.unlock() - - c.PanicMsg = &msg - - return c -} - -// Once indicates that the mock should only return the value once. -// -// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once() -func (c *Call) Once() *Call { - return c.Times(1) -} - -// Twice indicates that the mock should only return the value twice. -// -// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice() -func (c *Call) Twice() *Call { - return c.Times(2) -} - -// Times indicates that the mock should only return the indicated number -// of times. -// -// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5) -func (c *Call) Times(i int) *Call { - c.lock() - defer c.unlock() - c.Repeatability = i - return c -} - -// WaitUntil sets the channel that will block the mock's return until its closed -// or a message is received. -// -// Mock.On("MyMethod", arg1, arg2).WaitUntil(time.After(time.Second)) -func (c *Call) WaitUntil(w <-chan time.Time) *Call { - c.lock() - defer c.unlock() - c.WaitFor = w - return c -} - -// After sets how long to block until the call returns -// -// Mock.On("MyMethod", arg1, arg2).After(time.Second) -func (c *Call) After(d time.Duration) *Call { - c.lock() - defer c.unlock() - c.waitTime = d - return c -} - -// Run sets a handler to be called before returning. It can be used when -// mocking a method (such as an unmarshaler) that takes a pointer to a struct and -// sets properties in such struct -// -// Mock.On("Unmarshal", AnythingOfType("*map[string]interface{}")).Return().Run(func(args Arguments) { -// arg := args.Get(0).(*map[string]interface{}) -// arg["foo"] = "bar" -// }) -func (c *Call) Run(fn func(args Arguments)) *Call { - c.lock() - defer c.unlock() - c.RunFn = fn - return c -} - -// Maybe allows the method call to be optional. Not calling an optional method -// will not cause an error while asserting expectations -func (c *Call) Maybe() *Call { - c.lock() - defer c.unlock() - c.optional = true - return c -} - -// On chains a new expectation description onto the mocked interface. This -// allows syntax like. -// -// Mock. -// On("MyMethod", 1).Return(nil). -// On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error")) -// -//go:noinline -func (c *Call) On(methodName string, arguments ...interface{}) *Call { - return c.Parent.On(methodName, arguments...) -} - -// Unset removes a mock handler from being called. -// -// test.On("func", mock.Anything).Unset() -func (c *Call) Unset() *Call { - var unlockOnce sync.Once - - for _, arg := range c.Arguments { - if v := reflect.ValueOf(arg); v.Kind() == reflect.Func { - panic(fmt.Sprintf("cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")", arg)) - } - } - - c.lock() - defer unlockOnce.Do(c.unlock) - - foundMatchingCall := false - - // in-place filter slice for calls to be removed - iterate from 0'th to last skipping unnecessary ones - var index int // write index - for _, call := range c.Parent.ExpectedCalls { - if call.Method == c.Method { - _, diffCount := call.Arguments.Diff(c.Arguments) - if diffCount == 0 { - foundMatchingCall = true - // Remove from ExpectedCalls - just skip it - continue - } - } - c.Parent.ExpectedCalls[index] = call - index++ - } - // trim slice up to last copied index - c.Parent.ExpectedCalls = c.Parent.ExpectedCalls[:index] - - if !foundMatchingCall { - unlockOnce.Do(c.unlock) - c.Parent.fail("\n\nmock: Could not find expected call\n-----------------------------\n\n%s\n\n", - callString(c.Method, c.Arguments, true), - ) - } - - return c -} - -// NotBefore indicates that the mock should only be called after the referenced -// calls have been called as expected. The referenced calls may be from the -// same mock instance and/or other mock instances. -// -// Mock.On("Do").Return(nil).Notbefore( -// Mock.On("Init").Return(nil) -// ) -func (c *Call) NotBefore(calls ...*Call) *Call { - c.lock() - defer c.unlock() - - for _, call := range calls { - if call.Parent == nil { - panic("not before calls must be created with Mock.On()") - } - } - - c.requires = append(c.requires, calls...) - return c -} - -// Mock is the workhorse used to track activity on another object. -// For an example of its usage, refer to the "Example Usage" section at the top -// of this document. -type Mock struct { - // Represents the calls that are expected of - // an object. - ExpectedCalls []*Call - - // Holds the calls that were made to this mocked object. - Calls []Call - - // test is An optional variable that holds the test struct, to be used when an - // invalid mock call was made. - test TestingT - - // TestData holds any data that might be useful for testing. Testify ignores - // this data completely allowing you to do whatever you like with it. - testData objx.Map - - mutex sync.Mutex -} - -// String provides a %v format string for Mock. -// Note: this is used implicitly by Arguments.Diff if a Mock is passed. -// It exists because go's default %v formatting traverses the struct -// without acquiring the mutex, which is detected by go test -race. -func (m *Mock) String() string { - return fmt.Sprintf("%[1]T<%[1]p>", m) -} - -// TestData holds any data that might be useful for testing. Testify ignores -// this data completely allowing you to do whatever you like with it. -func (m *Mock) TestData() objx.Map { - if m.testData == nil { - m.testData = make(objx.Map) - } - - return m.testData -} - -/* - Setting expectations -*/ - -// Test sets the test struct variable of the mock object -func (m *Mock) Test(t TestingT) { - m.mutex.Lock() - defer m.mutex.Unlock() - m.test = t -} - -// fail fails the current test with the given formatted format and args. -// In case that a test was defined, it uses the test APIs for failing a test, -// otherwise it uses panic. -func (m *Mock) fail(format string, args ...interface{}) { - m.mutex.Lock() - defer m.mutex.Unlock() - - if m.test == nil { - panic(fmt.Sprintf(format, args...)) - } - m.test.Errorf(format, args...) - m.test.FailNow() -} - -// On starts a description of an expectation of the specified method -// being called. -// -// Mock.On("MyMethod", arg1, arg2) -func (m *Mock) On(methodName string, arguments ...interface{}) *Call { - for _, arg := range arguments { - if v := reflect.ValueOf(arg); v.Kind() == reflect.Func { - panic(fmt.Sprintf("cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")", arg)) - } - } - - m.mutex.Lock() - defer m.mutex.Unlock() - c := newCall(m, methodName, assert.CallerInfo(), arguments...) - m.ExpectedCalls = append(m.ExpectedCalls, c) - return c -} - -// /* -// Recording and responding to activity -// */ - -func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) { - var expectedCall *Call - - for i, call := range m.ExpectedCalls { - if call.Method == method { - _, diffCount := call.Arguments.Diff(arguments) - if diffCount == 0 { - expectedCall = call - if call.Repeatability > -1 { - return i, call - } - } - } - } - - return -1, expectedCall -} - -type matchCandidate struct { - call *Call - mismatch string - diffCount int -} - -func (c matchCandidate) isBetterMatchThan(other matchCandidate) bool { - if c.call == nil { - return false - } - if other.call == nil { - return true - } - - if c.diffCount > other.diffCount { - return false - } - if c.diffCount < other.diffCount { - return true - } - - if c.call.Repeatability > 0 && other.call.Repeatability <= 0 { - return true - } - return false -} - -func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call, string) { - var bestMatch matchCandidate - - for _, call := range m.expectedCalls() { - if call.Method == method { - - errInfo, tempDiffCount := call.Arguments.Diff(arguments) - tempCandidate := matchCandidate{ - call: call, - mismatch: errInfo, - diffCount: tempDiffCount, - } - if tempCandidate.isBetterMatchThan(bestMatch) { - bestMatch = tempCandidate - } - } - } - - return bestMatch.call, bestMatch.mismatch -} - -func callString(method string, arguments Arguments, includeArgumentValues bool) string { - var argValsString string - if includeArgumentValues { - var argVals []string - for argIndex, arg := range arguments { - if _, ok := arg.(*FunctionalOptionsArgument); ok { - argVals = append(argVals, fmt.Sprintf("%d: %s", argIndex, arg)) - continue - } - argVals = append(argVals, fmt.Sprintf("%d: %#v", argIndex, arg)) - } - argValsString = fmt.Sprintf("\n\t\t%s", strings.Join(argVals, "\n\t\t")) - } - - return fmt.Sprintf("%s(%s)%s", method, arguments.String(), argValsString) -} - -// Called tells the mock object that a method has been called, and gets an array -// of arguments to return. Panics if the call is unexpected (i.e. not preceded by -// appropriate .On .Return() calls) -// If Call.WaitFor is set, blocks until the channel is closed or receives a message. -func (m *Mock) Called(arguments ...interface{}) Arguments { - // get the calling function's name - pc, _, _, ok := runtime.Caller(1) - if !ok { - panic("Couldn't get the caller information") - } - functionPath := runtime.FuncForPC(pc).Name() - // Next four lines are required to use GCCGO function naming conventions. - // For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock - // uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree - // With GCCGO we need to remove interface information starting from pN
. - if gccgoRE.MatchString(functionPath) { - functionPath = gccgoRE.Split(functionPath, -1)[0] - } - parts := strings.Split(functionPath, ".") - functionName := parts[len(parts)-1] - return m.MethodCalled(functionName, arguments...) -} - -// MethodCalled tells the mock object that the given method has been called, and gets -// an array of arguments to return. Panics if the call is unexpected (i.e. not preceded -// by appropriate .On .Return() calls) -// If Call.WaitFor is set, blocks until the channel is closed or receives a message. -func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments { - m.mutex.Lock() - // TODO: could combine expected and closes in single loop - found, call := m.findExpectedCall(methodName, arguments...) - - if found < 0 { - // expected call found, but it has already been called with repeatable times - if call != nil { - m.mutex.Unlock() - m.fail("\nassert: mock: The method has been called over %d times.\n\tEither do one more Mock.On(\"%s\").Return(...), or remove extra call.\n\tThis call was unexpected:\n\t\t%s\n\tat: %s", call.totalCalls, methodName, callString(methodName, arguments, true), assert.CallerInfo()) - } - // we have to fail here - because we don't know what to do - // as the return arguments. This is because: - // - // a) this is a totally unexpected call to this method, - // b) the arguments are not what was expected, or - // c) the developer has forgotten to add an accompanying On...Return pair. - closestCall, mismatch := m.findClosestCall(methodName, arguments...) - m.mutex.Unlock() - - if closestCall != nil { - m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\nDiff: %s", - callString(methodName, arguments, true), - callString(methodName, closestCall.Arguments, true), - diffArguments(closestCall.Arguments, arguments), - strings.TrimSpace(mismatch), - ) - } else { - m.fail("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo()) - } - } - - for _, requirement := range call.requires { - if satisfied, _ := requirement.Parent.checkExpectation(requirement); !satisfied { - m.mutex.Unlock() - m.fail("mock: Unexpected Method Call\n-----------------------------\n\n%s\n\nMust not be called before%s:\n\n%s", - callString(call.Method, call.Arguments, true), - func() (s string) { - if requirement.totalCalls > 0 { - s = " another call of" - } - if call.Parent != requirement.Parent { - s += " method from another mock instance" - } - return - }(), - callString(requirement.Method, requirement.Arguments, true), - ) - } - } - - if call.Repeatability == 1 { - call.Repeatability = -1 - } else if call.Repeatability > 1 { - call.Repeatability-- - } - call.totalCalls++ - - // add the call - m.Calls = append(m.Calls, *newCall(m, methodName, assert.CallerInfo(), arguments...)) - m.mutex.Unlock() - - // block if specified - if call.WaitFor != nil { - <-call.WaitFor - } else { - time.Sleep(call.waitTime) - } - - m.mutex.Lock() - panicMsg := call.PanicMsg - m.mutex.Unlock() - if panicMsg != nil { - panic(*panicMsg) - } - - m.mutex.Lock() - runFn := call.RunFn - m.mutex.Unlock() - - if runFn != nil { - runFn(arguments) - } - - m.mutex.Lock() - returnArgs := call.ReturnArguments - m.mutex.Unlock() - - return returnArgs -} - -/* - Assertions -*/ - -type assertExpectationiser interface { - AssertExpectations(TestingT) bool -} - -// AssertExpectationsForObjects asserts that everything specified with On and Return -// of the specified objects was in fact called as expected. -// -// Calls may have occurred in any order. -func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool { - if h, ok := t.(tHelper); ok { - h.Helper() - } - for _, obj := range testObjects { - if m, ok := obj.(*Mock); ok { - t.Logf("Deprecated mock.AssertExpectationsForObjects(myMock.Mock) use mock.AssertExpectationsForObjects(myMock)") - obj = m - } - m := obj.(assertExpectationiser) - if !m.AssertExpectations(t) { - t.Logf("Expectations didn't match for Mock: %+v", reflect.TypeOf(m)) - return false - } - } - return true -} - -// AssertExpectations asserts that everything specified with On and Return was -// in fact called as expected. Calls may have occurred in any order. -func (m *Mock) AssertExpectations(t TestingT) bool { - if s, ok := t.(interface{ Skipped() bool }); ok && s.Skipped() { - return true - } - if h, ok := t.(tHelper); ok { - h.Helper() - } - - m.mutex.Lock() - defer m.mutex.Unlock() - var failedExpectations int - - // iterate through each expectation - expectedCalls := m.expectedCalls() - for _, expectedCall := range expectedCalls { - satisfied, reason := m.checkExpectation(expectedCall) - if !satisfied { - failedExpectations++ - t.Logf(reason) - } - } - - if failedExpectations != 0 { - t.Errorf("FAIL: %d out of %d expectation(s) were met.\n\tThe code you are testing needs to make %d more call(s).\n\tat: %s", len(expectedCalls)-failedExpectations, len(expectedCalls), failedExpectations, assert.CallerInfo()) - } - - return failedExpectations == 0 -} - -func (m *Mock) checkExpectation(call *Call) (bool, string) { - if !call.optional && !m.methodWasCalled(call.Method, call.Arguments) && call.totalCalls == 0 { - return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo) - } - if call.Repeatability > 0 { - return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo) - } - return true, fmt.Sprintf("PASS:\t%s(%s)", call.Method, call.Arguments.String()) -} - -// AssertNumberOfCalls asserts that the method was called expectedCalls times. -func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool { - if h, ok := t.(tHelper); ok { - h.Helper() - } - m.mutex.Lock() - defer m.mutex.Unlock() - var actualCalls int - for _, call := range m.calls() { - if call.Method == methodName { - actualCalls++ - } - } - return assert.Equal(t, expectedCalls, actualCalls, fmt.Sprintf("Expected number of calls (%d) does not match the actual number of calls (%d).", expectedCalls, actualCalls)) -} - -// AssertCalled asserts that the method was called. -// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method. -func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool { - if h, ok := t.(tHelper); ok { - h.Helper() - } - m.mutex.Lock() - defer m.mutex.Unlock() - if !m.methodWasCalled(methodName, arguments) { - var calledWithArgs []string - for _, call := range m.calls() { - calledWithArgs = append(calledWithArgs, fmt.Sprintf("%v", call.Arguments)) - } - if len(calledWithArgs) == 0 { - return assert.Fail(t, "Should have called with given arguments", - fmt.Sprintf("Expected %q to have been called with:\n%v\nbut no actual calls happened", methodName, arguments)) - } - return assert.Fail(t, "Should have called with given arguments", - fmt.Sprintf("Expected %q to have been called with:\n%v\nbut actual calls were:\n %v", methodName, arguments, strings.Join(calledWithArgs, "\n"))) - } - return true -} - -// AssertNotCalled asserts that the method was not called. -// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method. -func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool { - if h, ok := t.(tHelper); ok { - h.Helper() - } - m.mutex.Lock() - defer m.mutex.Unlock() - if m.methodWasCalled(methodName, arguments) { - return assert.Fail(t, "Should not have called with given arguments", - fmt.Sprintf("Expected %q to not have been called with:\n%v\nbut actually it was.", methodName, arguments)) - } - return true -} - -// IsMethodCallable checking that the method can be called -// If the method was called more than `Repeatability` return false -func (m *Mock) IsMethodCallable(t TestingT, methodName string, arguments ...interface{}) bool { - if h, ok := t.(tHelper); ok { - h.Helper() - } - m.mutex.Lock() - defer m.mutex.Unlock() - - for _, v := range m.ExpectedCalls { - if v.Method != methodName { - continue - } - if len(arguments) != len(v.Arguments) { - continue - } - if v.Repeatability < v.totalCalls { - continue - } - if isArgsEqual(v.Arguments, arguments) { - return true - } - } - return false -} - -// isArgsEqual compares arguments -func isArgsEqual(expected Arguments, args []interface{}) bool { - if len(expected) != len(args) { - return false - } - for i, v := range args { - if !reflect.DeepEqual(expected[i], v) { - return false - } - } - return true -} - -func (m *Mock) methodWasCalled(methodName string, expected []interface{}) bool { - for _, call := range m.calls() { - if call.Method == methodName { - - _, differences := Arguments(expected).Diff(call.Arguments) - - if differences == 0 { - // found the expected call - return true - } - - } - } - // we didn't find the expected call - return false -} - -func (m *Mock) expectedCalls() []*Call { - return append([]*Call{}, m.ExpectedCalls...) -} - -func (m *Mock) calls() []Call { - return append([]Call{}, m.Calls...) -} - -/* - Arguments -*/ - -// Arguments holds an array of method arguments or return values. -type Arguments []interface{} - -const ( - // Anything is used in Diff and Assert when the argument being tested - // shouldn't be taken into consideration. - Anything = "mock.Anything" -) - -// AnythingOfTypeArgument contains the type of an argument -// for use when type checking. Used in Diff and Assert. -// -// Deprecated: this is an implementation detail that must not be used. Use [AnythingOfType] instead. -type AnythingOfTypeArgument = anythingOfTypeArgument - -// anythingOfTypeArgument is a string that contains the type of an argument -// for use when type checking. Used in Diff and Assert. -type anythingOfTypeArgument string - -// AnythingOfType returns a special value containing the -// name of the type to check for. The type name will be matched against the type name returned by [reflect.Type.String]. -// -// Used in Diff and Assert. -// -// For example: -// -// Assert(t, AnythingOfType("string"), AnythingOfType("int")) -func AnythingOfType(t string) AnythingOfTypeArgument { - return anythingOfTypeArgument(t) -} - -// IsTypeArgument is a struct that contains the type of an argument -// for use when type checking. This is an alternative to AnythingOfType. -// Used in Diff and Assert. -type IsTypeArgument struct { - t reflect.Type -} - -// IsType returns an IsTypeArgument object containing the type to check for. -// You can provide a zero-value of the type to check. This is an -// alternative to AnythingOfType. Used in Diff and Assert. -// -// For example: -// Assert(t, IsType(""), IsType(0)) -func IsType(t interface{}) *IsTypeArgument { - return &IsTypeArgument{t: reflect.TypeOf(t)} -} - -// FunctionalOptionsArgument is a struct that contains the type and value of an functional option argument -// for use when type checking. -type FunctionalOptionsArgument struct { - value interface{} -} - -// String returns the string representation of FunctionalOptionsArgument -func (f *FunctionalOptionsArgument) String() string { - var name string - tValue := reflect.ValueOf(f.value) - if tValue.Len() > 0 { - name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String() - } - - return strings.Replace(fmt.Sprintf("%#v", f.value), "[]interface {}", name, 1) -} - -// FunctionalOptions returns an FunctionalOptionsArgument object containing the functional option type -// and the values to check of -// -// For example: -// Assert(t, FunctionalOptions("[]foo.FunctionalOption", foo.Opt1(), foo.Opt2())) -func FunctionalOptions(value ...interface{}) *FunctionalOptionsArgument { - return &FunctionalOptionsArgument{ - value: value, - } -} - -// argumentMatcher performs custom argument matching, returning whether or -// not the argument is matched by the expectation fixture function. -type argumentMatcher struct { - // fn is a function which accepts one argument, and returns a bool. - fn reflect.Value -} - -func (f argumentMatcher) Matches(argument interface{}) bool { - expectType := f.fn.Type().In(0) - expectTypeNilSupported := false - switch expectType.Kind() { - case reflect.Interface, reflect.Chan, reflect.Func, reflect.Map, reflect.Slice, reflect.Ptr: - expectTypeNilSupported = true - } - - argType := reflect.TypeOf(argument) - var arg reflect.Value - if argType == nil { - arg = reflect.New(expectType).Elem() - } else { - arg = reflect.ValueOf(argument) - } - - if argType == nil && !expectTypeNilSupported { - panic(errors.New("attempting to call matcher with nil for non-nil expected type")) - } - if argType == nil || argType.AssignableTo(expectType) { - result := f.fn.Call([]reflect.Value{arg}) - return result[0].Bool() - } - return false -} - -func (f argumentMatcher) String() string { - return fmt.Sprintf("func(%s) bool", f.fn.Type().In(0).String()) -} - -// MatchedBy can be used to match a mock call based on only certain properties -// from a complex struct or some calculation. It takes a function that will be -// evaluated with the called argument and will return true when there's a match -// and false otherwise. -// -// Example: -// m.On("Do", MatchedBy(func(req *http.Request) bool { return req.Host == "example.com" })) -// -// |fn|, must be a function accepting a single argument (of the expected type) -// which returns a bool. If |fn| doesn't match the required signature, -// MatchedBy() panics. -func MatchedBy(fn interface{}) argumentMatcher { - fnType := reflect.TypeOf(fn) - - if fnType.Kind() != reflect.Func { - panic(fmt.Sprintf("assert: arguments: %s is not a func", fn)) - } - if fnType.NumIn() != 1 { - panic(fmt.Sprintf("assert: arguments: %s does not take exactly one argument", fn)) - } - if fnType.NumOut() != 1 || fnType.Out(0).Kind() != reflect.Bool { - panic(fmt.Sprintf("assert: arguments: %s does not return a bool", fn)) - } - - return argumentMatcher{fn: reflect.ValueOf(fn)} -} - -// Get Returns the argument at the specified index. -func (args Arguments) Get(index int) interface{} { - if index+1 > len(args) { - panic(fmt.Sprintf("assert: arguments: Cannot call Get(%d) because there are %d argument(s).", index, len(args))) - } - return args[index] -} - -// Is gets whether the objects match the arguments specified. -func (args Arguments) Is(objects ...interface{}) bool { - for i, obj := range args { - if obj != objects[i] { - return false - } - } - return true -} - -// Diff gets a string describing the differences between the arguments -// and the specified objects. -// -// Returns the diff string and number of differences found. -func (args Arguments) Diff(objects []interface{}) (string, int) { - // TODO: could return string as error and nil for No difference - - output := "\n" - var differences int - - maxArgCount := len(args) - if len(objects) > maxArgCount { - maxArgCount = len(objects) - } - - for i := 0; i < maxArgCount; i++ { - var actual, expected interface{} - var actualFmt, expectedFmt string - - if len(objects) <= i { - actual = "(Missing)" - actualFmt = "(Missing)" - } else { - actual = objects[i] - actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual) - } - - if len(args) <= i { - expected = "(Missing)" - expectedFmt = "(Missing)" - } else { - expected = args[i] - expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected) - } - - if matcher, ok := expected.(argumentMatcher); ok { - var matches bool - func() { - defer func() { - if r := recover(); r != nil { - actualFmt = fmt.Sprintf("panic in argument matcher: %v", r) - } - }() - matches = matcher.Matches(actual) - }() - if matches { - output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher) - } else { - differences++ - output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher) - } - } else { - switch expected := expected.(type) { - case anythingOfTypeArgument: - // type checking - if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) { - // not match - differences++ - output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt) - } - case *IsTypeArgument: - actualT := reflect.TypeOf(actual) - if actualT != expected.t { - differences++ - output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt) - } - case *FunctionalOptionsArgument: - t := expected.value - - var name string - tValue := reflect.ValueOf(t) - if tValue.Len() > 0 { - name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String() - } - - tName := reflect.TypeOf(t).Name() - if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 { - differences++ - output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt) - } else { - if ef, af := assertOpts(t, actual); ef == "" && af == "" { - // match - output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, tName, tName) - } else { - // not match - differences++ - output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef) - } - } - - default: - if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { - // match - output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt) - } else { - // not match - differences++ - output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt) - } - } - } - - } - - if differences == 0 { - return "No differences.", differences - } - - return output, differences -} - -// Assert compares the arguments with the specified objects and fails if -// they do not exactly match. -func (args Arguments) Assert(t TestingT, objects ...interface{}) bool { - if h, ok := t.(tHelper); ok { - h.Helper() - } - - // get the differences - diff, diffCount := args.Diff(objects) - - if diffCount == 0 { - return true - } - - // there are differences... report them... - t.Logf(diff) - t.Errorf("%sArguments do not match.", assert.CallerInfo()) - - return false -} - -// String gets the argument at the specified index. Panics if there is no argument, or -// if the argument is of the wrong type. -// -// If no index is provided, String() returns a complete string representation -// of the arguments. -func (args Arguments) String(indexOrNil ...int) string { - if len(indexOrNil) == 0 { - // normal String() method - return a string representation of the args - var argsStr []string - for _, arg := range args { - argsStr = append(argsStr, fmt.Sprintf("%T", arg)) // handles nil nicely - } - return strings.Join(argsStr, ",") - } else if len(indexOrNil) == 1 { - // Index has been specified - get the argument at that index - index := indexOrNil[0] - var s string - var ok bool - if s, ok = args.Get(index).(string); !ok { - panic(fmt.Sprintf("assert: arguments: String(%d) failed because object wasn't correct type: %s", index, args.Get(index))) - } - return s - } - - panic(fmt.Sprintf("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d", len(indexOrNil))) -} - -// Int gets the argument at the specified index. Panics if there is no argument, or -// if the argument is of the wrong type. -func (args Arguments) Int(index int) int { - var s int - var ok bool - if s, ok = args.Get(index).(int); !ok { - panic(fmt.Sprintf("assert: arguments: Int(%d) failed because object wasn't correct type: %v", index, args.Get(index))) - } - return s -} - -// Error gets the argument at the specified index. Panics if there is no argument, or -// if the argument is of the wrong type. -func (args Arguments) Error(index int) error { - obj := args.Get(index) - var s error - var ok bool - if obj == nil { - return nil - } - if s, ok = obj.(error); !ok { - panic(fmt.Sprintf("assert: arguments: Error(%d) failed because object wasn't correct type: %v", index, args.Get(index))) - } - return s -} - -// Bool gets the argument at the specified index. Panics if there is no argument, or -// if the argument is of the wrong type. -func (args Arguments) Bool(index int) bool { - var s bool - var ok bool - if s, ok = args.Get(index).(bool); !ok { - panic(fmt.Sprintf("assert: arguments: Bool(%d) failed because object wasn't correct type: %v", index, args.Get(index))) - } - return s -} - -func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) { - t := reflect.TypeOf(v) - k := t.Kind() - - if k == reflect.Ptr { - t = t.Elem() - k = t.Kind() - } - return t, k -} - -func diffArguments(expected Arguments, actual Arguments) string { - if len(expected) != len(actual) { - return fmt.Sprintf("Provided %v arguments, mocked for %v arguments", len(expected), len(actual)) - } - - for x := range expected { - if diffString := diff(expected[x], actual[x]); diffString != "" { - return fmt.Sprintf("Difference found in argument %v:\n\n%s", x, diffString) - } - } - - return "" -} - -// diff returns a diff of both values as long as both are of the same type and -// are a struct, map, slice or array. Otherwise it returns an empty string. -func diff(expected interface{}, actual interface{}) string { - if expected == nil || actual == nil { - return "" - } - - et, ek := typeAndKind(expected) - at, _ := typeAndKind(actual) - - if et != at { - return "" - } - - if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array { - return "" - } - - e := spewConfig.Sdump(expected) - a := spewConfig.Sdump(actual) - - diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ - A: difflib.SplitLines(e), - B: difflib.SplitLines(a), - FromFile: "Expected", - FromDate: "", - ToFile: "Actual", - ToDate: "", - Context: 1, - }) - - return diff -} - -var spewConfig = spew.ConfigState{ - Indent: " ", - DisablePointerAddresses: true, - DisableCapacities: true, - SortKeys: true, -} - -type tHelper interface { - Helper() -} - -func assertOpts(expected, actual interface{}) (expectedFmt, actualFmt string) { - expectedOpts := reflect.ValueOf(expected) - actualOpts := reflect.ValueOf(actual) - var expectedNames []string - for i := 0; i < expectedOpts.Len(); i++ { - expectedNames = append(expectedNames, funcName(expectedOpts.Index(i).Interface())) - } - var actualNames []string - for i := 0; i < actualOpts.Len(); i++ { - actualNames = append(actualNames, funcName(actualOpts.Index(i).Interface())) - } - if !assert.ObjectsAreEqual(expectedNames, actualNames) { - expectedFmt = fmt.Sprintf("%v", expectedNames) - actualFmt = fmt.Sprintf("%v", actualNames) - return - } - - for i := 0; i < expectedOpts.Len(); i++ { - expectedOpt := expectedOpts.Index(i).Interface() - actualOpt := actualOpts.Index(i).Interface() - - expectedFunc := expectedNames[i] - actualFunc := actualNames[i] - if expectedFunc != actualFunc { - expectedFmt = expectedFunc - actualFmt = actualFunc - return - } - - ot := reflect.TypeOf(expectedOpt) - var expectedValues []reflect.Value - var actualValues []reflect.Value - if ot.NumIn() == 0 { - return - } - - for i := 0; i < ot.NumIn(); i++ { - vt := ot.In(i).Elem() - expectedValues = append(expectedValues, reflect.New(vt)) - actualValues = append(actualValues, reflect.New(vt)) - } - - reflect.ValueOf(expectedOpt).Call(expectedValues) - reflect.ValueOf(actualOpt).Call(actualValues) - - for i := 0; i < ot.NumIn(); i++ { - if !assert.ObjectsAreEqual(expectedValues[i].Interface(), actualValues[i].Interface()) { - expectedFmt = fmt.Sprintf("%s %+v", expectedNames[i], expectedValues[i].Interface()) - actualFmt = fmt.Sprintf("%s %+v", expectedNames[i], actualValues[i].Interface()) - return - } - } - } - - return "", "" -} - -func funcName(opt interface{}) string { - n := runtime.FuncForPC(reflect.ValueOf(opt).Pointer()).Name() - return strings.TrimSuffix(path.Base(n), path.Ext(n)) -} diff --git a/vendor/modules.txt b/vendor/modules.txt index da5cffd92..a45702bd3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -397,14 +397,7 @@ github.com/prometheus/procfs/internal/util # github.com/rs/cors v1.10.1 ## explicit; go 1.13 github.com/rs/cors -# github.com/shirou/gopsutil v3.21.11+incompatible -## explicit -github.com/shirou/gopsutil/cpu -github.com/shirou/gopsutil/internal/common -github.com/shirou/gopsutil/mem -github.com/shirou/gopsutil/net -github.com/shirou/gopsutil/process -# github.com/shirou/gopsutil/v3 v3.24.2 +# github.com/shirou/gopsutil/v3 v3.24.4 ## explicit; go 1.15 github.com/shirou/gopsutil/v3/common github.com/shirou/gopsutil/v3/cpu @@ -418,13 +411,9 @@ github.com/shoenig/go-m1cpu # github.com/spf13/pflag v1.0.5 ## explicit; go 1.12 github.com/spf13/pflag -# github.com/stretchr/objx v0.5.2 -## explicit; go 1.20 -github.com/stretchr/objx # github.com/stretchr/testify v1.9.0 ## explicit; go 1.17 github.com/stretchr/testify/assert -github.com/stretchr/testify/mock github.com/stretchr/testify/require # github.com/tklauser/go-sysconf v0.3.13 ## explicit; go 1.18