diff --git a/providers/darwin/host_darwin.go b/providers/darwin/host_darwin.go index c87c7167..f40b7412 100644 --- a/providers/darwin/host_darwin.go +++ b/providers/darwin/host_darwin.go @@ -20,13 +20,11 @@ package darwin -import "C" import ( "errors" "fmt" "os" "time" - "unsafe" "github.com/joeshaw/multierror" @@ -217,7 +215,7 @@ func (r *reader) hostname(h *host) { } func (r *reader) fqdn(h *host) { - fqdn, err := fqdnC() + fqdn, err := shared.FQDN() if err != nil { r.addErr(fmt.Errorf("could not get linux FQDN: %w", err)) return @@ -226,34 +224,6 @@ func (r *reader) fqdn(h *host) { h.info.FQDN = fqdn } -func fqdnC() (string, error) { - // Another option could be reading: - // - /proc/sys/kernel/hostname - // - /proc/sys/kernel/domainname - const buffSize = 64 - buff := make([]byte, buffSize) - size := C.size_t(buffSize) - cString := C.CString(string(buff)) - defer C.free(unsafe.Pointer(cString)) - - _, errno := C.gethostname(cString, size) - if errno != nil { - return "", fmt.Errorf("syscall gethostname errored: %v", errno) - } - var hostname string = C.GoString(cString) - - _, errno = C.getdomainname(cString, size) - if errno != nil { - return "", fmt.Errorf("syscall getdomainname errored: %v", errno) - } - var domain string = C.GoString(cString) - if domain == "" || domain == "(none)" { // mimicking 'hostname -f' behaviour - domain = "lan" - } - - return fmt.Sprintf("%s.%s", hostname, domain), nil -} - func (r *reader) network(h *host) { ips, macs, err := shared.Network() if r.addErr(err) { diff --git a/providers/linux/host_linux.go b/providers/linux/host_linux.go index da11c599..f5b23346 100644 --- a/providers/linux/host_linux.go +++ b/providers/linux/host_linux.go @@ -17,10 +17,6 @@ package linux -// #include -// #include -import "C" - import ( "errors" "fmt" @@ -28,7 +24,6 @@ import ( "os" "path/filepath" "time" - "unsafe" "github.com/joeshaw/multierror" "github.com/prometheus/procfs" @@ -218,7 +213,7 @@ func (r *reader) hostname(h *host) { } func (r *reader) fqdn(h *host) { - fqdn, err := fqdnC() + fqdn, err := shared.FQDN() if err != nil { r.addErr(fmt.Errorf("could not get linux FQDN: %w", err)) return @@ -227,34 +222,6 @@ func (r *reader) fqdn(h *host) { h.info.FQDN = fqdn } -func fqdnC() (string, error) { - // Another option could be reading: - // - /proc/sys/kernel/hostname - // - /proc/sys/kernel/domainname - const buffSize = 64 - buff := make([]byte, buffSize) - size := C.size_t(buffSize) - cString := C.CString(string(buff)) - defer C.free(unsafe.Pointer(cString)) - - _, errno := C.gethostname(cString, size) - if errno != nil { - return "", fmt.Errorf("syscall gethostname errored: %v", errno) - } - var hostname string = C.GoString(cString) - - _, errno = C.getdomainname(cString, size) - if errno != nil { - return "", fmt.Errorf("syscall getdomainname errored: %v", errno) - } - var domain string = C.GoString(cString) - if domain == "" || domain == "(none)" { // mimicking 'hostname -f' behaviour - domain = "lan" - } - - return fmt.Sprintf("%s.%s", hostname, domain), nil -} - func (r *reader) network(h *host) { ips, macs, err := shared.Network() if r.addErr(err) { diff --git a/providers/shared/host_fqdn_cgo_unix.go b/providers/shared/host_fqdn_cgo_unix.go new file mode 100644 index 00000000..0ada8f8d --- /dev/null +++ b/providers/shared/host_fqdn_cgo_unix.go @@ -0,0 +1,58 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//go:build cgo + +package shared + +// #include +// #include +import "C" +import ( + "fmt" + "unsafe" +) + +func FQDN() (string, error) { + // Another option could be reading: + // - /proc/sys/kernel/hostname + // - /proc/sys/kernel/domainname + // perhaps not valid for darwin + + const buffSize = 64 + buff := make([]byte, buffSize) + size := C.size_t(buffSize) + cString := C.CString(string(buff)) + defer C.free(unsafe.Pointer(cString)) + + _, errno := C.gethostname(cString, size) + if errno != nil { + return "", fmt.Errorf("syscall gethostname errored: %v", errno) + } + var hostname string = C.GoString(cString) + + _, errno = C.getdomainname(cString, size) + if errno != nil { + return "", fmt.Errorf("syscall getdomainname errored: %v", errno) + } + var domain string = C.GoString(cString) + if domain == "" || domain == "(none)" { // mimicking 'hostname -f' behaviour + domain = "lan" + } + + return fmt.Sprintf("%s.%s", hostname, domain), nil +} diff --git a/providers/shared/host_fqdn_nocgo_linux.go b/providers/shared/host_fqdn_nocgo_linux.go new file mode 100644 index 00000000..caa68edf --- /dev/null +++ b/providers/shared/host_fqdn_nocgo_linux.go @@ -0,0 +1,29 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//go:build !cgo + +package shared + +import "C" +import ( + "os" +) + +func FQDN() (string, error) { + return os.Hostname() +}