Pull request: all: openbsd support

Updates #2439.

Squashed commit of the following:

commit 3ff109e43751132d77500256c8869938680ac281
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jun 3 20:46:17 2021 +0300

    all: imp code, docs

commit 512ee6d78cfee511f429d09c8366bb7dd8019aa8
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jun 3 20:06:41 2021 +0300

    all: openbsd support
This commit is contained in:
Ainar Garipov
2021-06-03 21:04:13 +03:00
parent 084564e6b7
commit 3b87478470
13 changed files with 124 additions and 72 deletions

View File

@@ -5,18 +5,28 @@ import (
"fmt"
"os/exec"
"syscall"
"github.com/AdguardTeam/golibs/errors"
)
// ErrUnsupported is returned when the functionality is unsupported on the
// current operating system.
//
// TODO(a.garipov): Make a structured error and use it everywhere instead of
// a bunch of fmt.Errorf and all that.
const ErrUnsupported errors.Error = "unsupported"
// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
return canBindPrivilegedPorts()
}
// SetRlimit sets user-specified limit of how many fd's we can use
// https://github.com/AdguardTeam/AdGuardHome/internal/issues/659.
func SetRlimit(val uint) {
setRlimit(val)
// SetRlimit sets user-specified limit of how many fd's we can use.
//
// See https://github.com/AdguardTeam/AdGuardHome/internal/issues/659.
func SetRlimit(val uint64) (err error) {
return setRlimit(val)
}
// HaveAdminRights checks if the current user has root (administrator) rights.

View File

@@ -7,22 +7,18 @@ package aghos
import (
"os"
"syscall"
"github.com/AdguardTeam/golibs/log"
)
func canBindPrivilegedPorts() (can bool, err error) {
return HaveAdminRights()
}
func setRlimit(val uint) {
func setRlimit(val uint64) (err error) {
var rlim syscall.Rlimit
rlim.Max = uint64(val)
rlim.Cur = uint64(val)
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
if err != nil {
log.Error("Setrlimit() failed: %v", err)
}
rlim.Max = val
rlim.Cur = val
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
}
func haveAdminRights() (bool, error) {

View File

@@ -7,22 +7,18 @@ package aghos
import (
"os"
"syscall"
"github.com/AdguardTeam/golibs/log"
)
func canBindPrivilegedPorts() (can bool, err error) {
return HaveAdminRights()
}
func setRlimit(val uint) {
func setRlimit(val uint64) (err error) {
var rlim syscall.Rlimit
rlim.Max = int64(val)
rlim.Cur = int64(val)
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
if err != nil {
log.Error("Setrlimit() failed: %v", err)
}
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
}
func haveAdminRights() (bool, error) {

View File

@@ -11,7 +11,6 @@ import (
"strings"
"syscall"
"github.com/AdguardTeam/golibs/log"
"golang.org/x/sys/unix"
)
@@ -23,14 +22,12 @@ func canBindPrivilegedPorts() (can bool, err error) {
return cnbs == 1 || adm, err
}
func setRlimit(val uint) {
func setRlimit(val uint64) (err error) {
var rlim syscall.Rlimit
rlim.Max = uint64(val)
rlim.Cur = uint64(val)
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
if err != nil {
log.Error("Setrlimit() failed: %v", err)
}
rlim.Max = val
rlim.Cur = val
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
}
func haveAdminRights() (bool, error) {

View File

@@ -15,7 +15,8 @@ func canBindPrivilegedPorts() (can bool, err error) {
return HaveAdminRights()
}
func setRlimit(val uint) {
func setRlimit(val uint64) (err error) {
return ErrUnsupported
}
func haveAdminRights() (bool, error) {

View File

@@ -54,7 +54,7 @@ type configuration struct {
AuthBlockMin uint `yaml:"block_auth_min"`
ProxyURL string `yaml:"http_proxy"` // Proxy address for our HTTP client
Language string `yaml:"language"` // two-letter ISO 639-1 language code
RlimitNoFile uint `yaml:"rlimit_nofile"` // Maximum number of opened fd's per process (0: default)
RlimitNoFile uint64 `yaml:"rlimit_nofile"` // Maximum number of opened fd's per process (0: default)
DebugPProf bool `yaml:"debug_pprof"` // Enable pprof HTTP server on port 6060
// TTL for a web session (in hours)

View File

@@ -115,7 +115,15 @@ func Main(clientBuildFS fs.FS) {
}()
if args.serviceControlAction != "" {
// TODO(a.garipov): github.com/kardianos/service doesn't seem to
// support OpenBSD currently. Either patch it to do so or make
// our own implementation of the service.System interface.
if runtime.GOOS == "openbsd" {
log.Fatal("service actions are not supported on openbsd")
}
handleServiceControlAction(args, clientBuildFS)
return
}
@@ -175,7 +183,7 @@ func setupContext(args options) {
Context.mux = http.NewServeMux()
}
func setupConfig(args options) {
func setupConfig(args options) (err error) {
config.DHCP.WorkDir = Context.workDir
config.DHCP.HTTPRegister = httpRegister
config.DHCP.ConfigModified = onConfigModified
@@ -186,7 +194,7 @@ func setupConfig(args options) {
// now which assume that the DHCP server can be nil despite this
// condition. Inspect them and perhaps rewrite them to use
// Enabled() instead.
log.Fatalf("can't initialize dhcp module")
return fmt.Errorf("initing dhcp: %w", err)
}
Context.updater = updater.NewUpdater(&updater.Config{
@@ -208,9 +216,11 @@ func setupConfig(args options) {
Context.clients.Init(config.Clients, Context.dhcpServer, Context.etcHosts)
config.Clients = nil
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
config.RlimitNoFile != 0 {
aghos.SetRlimit(config.RlimitNoFile)
if config.RlimitNoFile != 0 {
err = aghos.SetRlimit(config.RlimitNoFile)
if err != nil && !errors.Is(err, aghos.ErrUnsupported) {
return fmt.Errorf("setting rlimit: %w", err)
}
}
// override bind host/port from the console
@@ -223,6 +233,8 @@ func setupConfig(args options) {
if len(args.pidFile) != 0 && writePIDFile(args.pidFile) {
Context.pidFileName = args.pidFile
}
return nil
}
func initWeb(args options, clientBuildFS fs.FS) (web *Web, err error) {
@@ -266,8 +278,16 @@ func initWeb(args options, clientBuildFS fs.FS) (web *Web, err error) {
return web, nil
}
func fatalOnError(err error) {
if err != nil {
log.Fatal(err)
}
}
// run performs configurating and starts AdGuard Home.
func run(args options, clientBuildFS fs.FS) {
var err error
// configure config filename
initConfigFilename(args)
@@ -294,14 +314,13 @@ func run(args options, clientBuildFS fs.FS) {
// but also avoid relying on automatic Go init() function
filtering.InitModule()
setupConfig(args)
err = setupConfig(args)
fatalOnError(err)
if !Context.firstRun {
// Save the updated config
err := config.write()
if err != nil {
log.Fatal(err)
}
err = config.write()
fatalOnError(err)
if config.DebugPProf {
mux := http.NewServeMux()
@@ -318,7 +337,7 @@ func run(args options, clientBuildFS fs.FS) {
}
}
err := os.MkdirAll(Context.getDataDir(), 0o755)
err = os.MkdirAll(Context.getDataDir(), 0o755)
if err != nil {
log.Fatalf("Cannot create DNS data dir at %s: %s", Context.getDataDir(), err)
}
@@ -352,20 +371,14 @@ func run(args options, clientBuildFS fs.FS) {
}
Context.web, err = initWeb(args, clientBuildFS)
if err != nil {
log.Fatal(err)
}
fatalOnError(err)
Context.subnetDetector, err = aghnet.NewSubnetDetector()
if err != nil {
log.Fatal(err)
}
fatalOnError(err)
if !Context.firstRun {
err = initDNSServer()
if err != nil {
log.Fatalf("%s", err)
}
fatalOnError(err)
Context.tls.Start()
Context.etcHosts.Start()
@@ -374,7 +387,7 @@ func run(args options, clientBuildFS fs.FS) {
serr := startDNSServer()
if serr != nil {
closeDNSServer()
log.Fatal(serr)
fatalOnError(serr)
}
}()