Skip to content

Commit

Permalink
moving cgo to shared and adding nocgo fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Jan 5, 2023
1 parent 94780c5 commit 295121c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 65 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 := shared.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
35 changes: 1 addition & 34 deletions providers/linux/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@

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 +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
Expand All @@ -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) {
Expand Down
58 changes: 58 additions & 0 deletions providers/shared/host_fqdn_cgo_unix.go
Original file line number Diff line number Diff line change
@@ -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 <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
// 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
}
29 changes: 29 additions & 0 deletions providers/shared/host_fqdn_nocgo_linux.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 295121c

Please sign in to comment.