Skip to content

Commit

Permalink
Allow building for darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
iaguis committed Dec 8, 2016
1 parent 61b30fd commit a84a81e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
47 changes: 3 additions & 44 deletions probe/endpoint/ebpf.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package endpoint

import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
"syscall"
"unsafe"

log "github.com/Sirupsen/logrus"
bpflib "github.com/kinvolk/gobpf-elf-loader/bpf"
)

import "C"

var byteOrder binary.ByteOrder

type eventType uint32
Expand Down Expand Up @@ -116,43 +109,6 @@ type EbpfTracker struct {
closedConnections []ebpfConnection
}

func findBpfObjectFile() (string, error) {
var buf syscall.Utsname
err := syscall.Uname(&buf)
if err != nil {
return "", err
}

// parse "ID=" in /etc/host-os-release (this is a bind mount of
// /etc/os-release on the host)
hostDistroFile, err := os.Open("/etc/host-os-release")
if err != nil {
return "", err
}
defer hostDistroFile.Close()

scanner := bufio.NewScanner(hostDistroFile)
var distro string
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "ID=") {
distro = strings.TrimPrefix(scanner.Text(), "ID=")
break
}
}
if err = scanner.Err(); err != nil {
return "", err
}
if distro == "" {
return "", fmt.Errorf("distro ID not found")
}

arch := C.GoString((*C.char)(unsafe.Pointer(&buf.Machine[0])))
release := C.GoString((*C.char)(unsafe.Pointer(&buf.Release[0])))
fileName := fmt.Sprintf("/usr/libexec/scope/ebpf/%s/%s/%s/ebpf.o", distro, arch, release)

return fileName, nil
}

func newEbpfTracker(useEbpfConn bool) eventTracker {
if !useEbpfConn {
return &nilTracker{}
Expand All @@ -165,6 +121,9 @@ func newEbpfTracker(useEbpfConn bool) eventTracker {
}

bpfPerfEvent := bpflib.NewBpfPerfEvent(bpfObjectFile)
if bpfPerfEvent == nil {
return &nilTracker{}
}
err = bpfPerfEvent.Load()
if err != nil {
log.Errorf("Error loading BPF program: %v", err)
Expand Down
51 changes: 51 additions & 0 deletions probe/endpoint/ebpf_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//+build linux

package endpoint

import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"unsafe"
)

import "C"

func findBpfObjectFile() (string, error) {
var buf syscall.Utsname
err := syscall.Uname(&buf)
if err != nil {
return "", err
}

// parse "ID=" in /etc/host-os-release (this is a bind mount of
// /etc/os-release on the host)
hostDistroFile, err := os.Open("/etc/host-os-release")
if err != nil {
return "", err
}
defer hostDistroFile.Close()

scanner := bufio.NewScanner(hostDistroFile)
var distro string
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "ID=") {
distro = strings.TrimPrefix(scanner.Text(), "ID=")
break
}
}
if err = scanner.Err(); err != nil {
return "", err
}
if distro == "" {
return "", fmt.Errorf("distro ID not found")
}

arch := C.GoString((*C.char)(unsafe.Pointer(&buf.Machine[0])))
release := C.GoString((*C.char)(unsafe.Pointer(&buf.Release[0])))
fileName := fmt.Sprintf("/usr/libexec/scope/ebpf/%s/%s/%s/ebpf.o", distro, arch, release)

return fileName, nil
}
9 changes: 9 additions & 0 deletions probe/endpoint/ebpf_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//+build !linux

package endpoint

import "fmt"

func findBpfObjectFile() (string, error) {
return "", fmt.Errorf("not supported")
}

0 comments on commit a84a81e

Please sign in to comment.