Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FreeBSD Compile Problems #1396

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmd/cloudflared/generic_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,38 @@
package main

import (
"fmt"
"os"

cli "github.com/urfave/cli/v2"

"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
)

func runApp(app *cli.App, graceShutdownC chan struct{}) {
app.Commands = append(app.Commands, &cli.Command{
Name: "service",
Usage: "Manages the cloudflared system service (not supported on this operating system)",
Subcommands: []*cli.Command{
{
Name: "install",
Usage: "Install cloudflared as a system service (not supported on this operating system)",
Action: cliutil.ConfiguredAction(installGenericService),
},
{
Name: "uninstall",
Usage: "Uninstall the cloudflared service (not supported on this operating system)",
Action: cliutil.ConfiguredAction(uninstallGenericService),
},
},
})
app.Run(os.Args)
}

func installGenericService(c *cli.Context) error {
return fmt.Errorf("service installation is not supported on this operating system")
}

func uninstallGenericService(c *cli.Context) error {
return fmt.Errorf("service uninstallation is not supported on this operating system")
}
2 changes: 1 addition & 1 deletion diagnostic/network/collector_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || linux
//go:build darwin || linux || freebsd || openbsd || netbsd

package diagnostic

Expand Down
2 changes: 1 addition & 1 deletion diagnostic/network/collector_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || linux
//go:build darwin || linux || freebsd || openbsd || netbsd

package diagnostic_test

Expand Down
168 changes: 168 additions & 0 deletions diagnostic/system_collector_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
//go:build !windows && !darwin && !linux

package diagnostic

import (
"context"
"fmt"
"os/exec"
"runtime"
"strconv"
"strings"
)

type SystemCollectorImpl struct {
version string
}

func NewSystemCollectorImpl(
version string,
) *SystemCollectorImpl {
return &SystemCollectorImpl{
version,
}
}

func (collector *SystemCollectorImpl) Collect(ctx context.Context) (*SystemInformation, error) {
memoryInfo, memoryInfoRaw, memoryInfoErr := collectMemoryInformation(ctx)
fdInfo, fdInfoRaw, fdInfoErr := collectFileDescriptorInformation(ctx)
disks, disksRaw, diskErr := collectDiskVolumeInformationUnix(ctx)
osInfo, osInfoRaw, osInfoErr := collectOSInformationUnix(ctx)

var memoryMaximum, memoryCurrent, fileDescriptorMaximum, fileDescriptorCurrent uint64
var osSystem, name, osVersion, osRelease, architecture string
gerror := SystemInformationGeneralError{}

if memoryInfoErr != nil {
gerror.MemoryInformationError = SystemInformationError{
Err: memoryInfoErr,
RawInfo: memoryInfoRaw,
}
} else {
memoryMaximum = memoryInfo.MemoryMaximum
memoryCurrent = memoryInfo.MemoryCurrent
}

if fdInfoErr != nil {
gerror.FileDescriptorsInformationError = SystemInformationError{
Err: fdInfoErr,
RawInfo: fdInfoRaw,
}
} else {
fileDescriptorMaximum = fdInfo.FileDescriptorMaximum
fileDescriptorCurrent = fdInfo.FileDescriptorCurrent
}

if diskErr != nil {
gerror.DiskVolumeInformationError = SystemInformationError{
Err: diskErr,
RawInfo: disksRaw,
}
}

if osInfoErr != nil {
gerror.OperatingSystemInformationError = SystemInformationError{
Err: osInfoErr,
RawInfo: osInfoRaw,
}
} else {
osSystem = osInfo.OsSystem
name = osInfo.Name
osVersion = osInfo.OsVersion
osRelease = osInfo.OsRelease
architecture = osInfo.Architecture
}

cloudflaredVersion := collector.version
info := NewSystemInformation(
memoryMaximum,
memoryCurrent,
fileDescriptorMaximum,
fileDescriptorCurrent,
osSystem,
name,
osVersion,
osRelease,
architecture,
cloudflaredVersion,
runtime.Version(),
runtime.GOARCH,
disks,
)

return info, gerror
}

func collectMemoryInformation(ctx context.Context) (*MemoryInformation, string, error) {
// FreeBSD uses `sysctl` to retrieve memory information.
const (
memTotalKey = "hw.physmem"
memAvailableKey = "vm.stats.vm.v_free_count"
)

command := exec.CommandContext(ctx, "sysctl", "-n", memTotalKey)
totalOutput, err := command.Output()
if err != nil {
return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err)
}

command = exec.CommandContext(ctx, "sysctl", "-n", memAvailableKey)
availableOutput, err := command.Output()
if err != nil {
return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err)
}

total, err := strconv.ParseUint(strings.TrimSpace(string(totalOutput)), 10, 64)
if err != nil {
return nil, string(totalOutput), fmt.Errorf("error parsing memory total: %w", err)
}

available, err := strconv.ParseUint(strings.TrimSpace(string(availableOutput)), 10, 64)
if err != nil {
return nil, string(availableOutput), fmt.Errorf("error parsing memory available: %w", err)
}

memoryInfo := &MemoryInformation{
MemoryMaximum: total,
MemoryCurrent: available * 4096, // FreeBSD reports pages; multiply by page size (4K).
}

return memoryInfo, fmt.Sprintf("Total: %s, Available: %s", totalOutput, availableOutput), nil
}

func collectFileDescriptorInformation(ctx context.Context) (*FileDescriptorInformation, string, error) {
// FreeBSD uses `sysctl` for file descriptor limits.
const (
fdMaxKey = "kern.maxfiles"
fdCurrentKey = "kern.openfiles"
)

command := exec.CommandContext(ctx, "sysctl", "-n", fdMaxKey)
maxOutput, err := command.Output()
if err != nil {
return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err)
}

command = exec.CommandContext(ctx, "sysctl", "-n", fdCurrentKey)
currentOutput, err := command.Output()
if err != nil {
return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err)
}

max, err := strconv.ParseUint(strings.TrimSpace(string(maxOutput)), 10, 64)
if err != nil {
return nil, string(maxOutput), fmt.Errorf("error parsing max file descriptors: %w", err)
}

current, err := strconv.ParseUint(strings.TrimSpace(string(currentOutput)), 10, 64)
if err != nil {
return nil, string(currentOutput), fmt.Errorf("error parsing current file descriptors: %w", err)
}

fdInfo := &FileDescriptorInformation{
FileDescriptorMaximum: max,
FileDescriptorCurrent: current,
}

return fdInfo, fmt.Sprintf("Max: %s, Current: %s", maxOutput, currentOutput), nil
}
3 changes: 1 addition & 2 deletions ingress/icmp_posix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build darwin || linux

//go:build darwin || linux || freebsd || openbsd || netbsd
package ingress

// This file extracts logic shared by Linux and Darwin implementation if ICMPProxy.
Expand Down
2 changes: 1 addition & 1 deletion ingress/icmp_posix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || linux
//go:build darwin || linux || freebsd || openbsd || netbsd

package ingress

Expand Down