Skip to content

Commit

Permalink
nocgo fallback and fix darwin types
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Jan 5, 2023
1 parent 94780c5 commit dc6f651
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 64 deletions.
32 changes: 1 addition & 31 deletions providers/darwin/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@

package darwin

import "C"
import (
"errors"
"fmt"
"os"
"time"
"unsafe"

"github.com/joeshaw/multierror"

Expand Down Expand Up @@ -217,7 +215,7 @@ func (r *reader) hostname(h *host) {
}

func (r *reader) fqdn(h *host) {
fqdn, err := fqdnC()
fqdn, err := fqdn()
if err != nil {
r.addErr(fmt.Errorf("could not get linux FQDN: %w", err))
return
Expand All @@ -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) {
Expand Down
53 changes: 53 additions & 0 deletions providers/darwin/host_fqdn_cgo_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 && (amd64 || arm64)

package darwin

// #include <unistd.h>
// #include <stdlib.h>
import "C"
import (
"fmt"
"unsafe"
)

func fqdn() (string, error) {
const buffSize = 64
buff := make([]byte, buffSize)
size := buffSize
cString := C.CString(string(buff))
defer C.free(unsafe.Pointer(cString))

_, errno := C.gethostname(cString, C.size_t(size))
if errno != nil {
return "", fmt.Errorf("syscall gethostname errored: %v", errno)
}
var hostname string = C.GoString(cString)

_, errno = C.getdomainname(cString, C.int(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
}
24 changes: 24 additions & 0 deletions providers/darwin/host_fqdn_nocgo_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 && (amd64 || arm64)

package darwin

import "os"

func fqdn() (string, error) { return os.Hostname() }
56 changes: 56 additions & 0 deletions providers/linux/host_fqdn_cgo_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 && (amd64 || arm64)

package linux

// #include <unistd.h>
// #include <stdlib.h>
import "C"
import (
"fmt"
"unsafe"
)

func fqdn() (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
}
24 changes: 24 additions & 0 deletions providers/linux/host_fqdn_nocgo_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 && (amd64 || arm64)

package linux

import "os"

func fqdn() (string, error) { return os.Hostname() }
34 changes: 1 addition & 33 deletions providers/linux/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@

package linux

// #include <unistd.h>
// #include <stdlib.h>
import "C"

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"unsafe"

"github.com/joeshaw/multierror"
"github.com/prometheus/procfs"
Expand Down Expand Up @@ -218,7 +214,7 @@ func (r *reader) hostname(h *host) {
}

func (r *reader) fqdn(h *host) {
fqdn, err := fqdnC()
fqdn, err := fqdn()
if err != nil {
r.addErr(fmt.Errorf("could not get linux FQDN: %w", err))
return
Expand All @@ -227,34 +223,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) {
Expand Down

0 comments on commit dc6f651

Please sign in to comment.