Skip to content

Commit

Permalink
all: add sysutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 30, 2020
1 parent 6e615c6 commit 778097c
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 30 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ and this project adheres to

### Changed

- Make the mobileconfig HTTP API more robust and predictable, add parameters and
- Updating and relaunching policy is now OS-dependent ([#2231]).
- Made the mobileconfig HTTP API more robust and predictable, add parameters and
improve error response ([#2358]).
- Improved HTTP requests handling and timeouts. ([#2343]).
- Our snap package now uses the `core20` image as its base [#2306].
- Various internal improvements ([#2271], [#2297]).

[#2231]: https://github.com/AdguardTeam/AdGuardHome/issues/2231
[#2271]: https://github.com/AdguardTeam/AdGuardHome/issues/2271
[#2297]: https://github.com/AdguardTeam/AdGuardHome/issues/2297
[#2306]: https://github.com/AdguardTeam/AdGuardHome/issues/2306
Expand Down
9 changes: 2 additions & 7 deletions internal/home/control_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
"syscall"

"github.com/AdguardTeam/AdGuardHome/internal/sysutil"
"github.com/AdguardTeam/AdGuardHome/internal/update"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/log"
)

Expand Down Expand Up @@ -104,12 +104,7 @@ func getVersionResp(info update.VersionInfo) []byte {
tlsConf.PortDNSOverQUIC < 1024)) ||
config.BindPort < 1024 ||
config.DNS.Port < 1024) {
// On UNIX, if we're running under a regular user,
// but with CAP_NET_BIND_SERVICE set on a binary file,
// and we're listening on ports <1024,
// we won't be able to restart after we replace the binary file,
// because we'll lose CAP_NET_BIND_SERVICE capability.
canUpdate, _ = util.HaveAdminRights()
canUpdate, _ = sysutil.CanBindPrivilegedPorts()
}
ret["can_autoupdate"] = canUpdate
}
Expand Down
7 changes: 4 additions & 3 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
"github.com/AdguardTeam/AdGuardHome/internal/stats"
"github.com/AdguardTeam/AdGuardHome/internal/sysutil"
"github.com/AdguardTeam/AdGuardHome/internal/update"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/log"
Expand Down Expand Up @@ -222,7 +223,7 @@ func setupConfig(args options) {

if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
config.RlimitNoFile != 0 {
util.SetRlimit(config.RlimitNoFile)
sysutil.SetRlimit(config.RlimitNoFile)
}

// override bind host/port from the console
Expand Down Expand Up @@ -376,7 +377,7 @@ func checkPermissions() {
if runtime.GOOS == "windows" {
// On Windows we need to have admin rights to run properly

admin, _ := util.HaveAdminRights()
admin, _ := sysutil.HaveAdminRights()
if admin {
return
}
Expand Down Expand Up @@ -493,7 +494,7 @@ func configureLogger(args options) {

if ls.LogFile == configSyslog {
// Use syslog where it is possible and eventlog on Windows
err := util.ConfigureSyslog(serviceName)
err := sysutil.ConfigureSyslog(serviceName)
if err != nil {
log.Fatalf("cannot initialize syslog: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/home/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"syscall"

"github.com/AdguardTeam/AdGuardHome/internal/sysutil"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/log"
"github.com/kardianos/service"
Expand Down Expand Up @@ -109,7 +110,7 @@ func sendSigReload() {
log.Error("Can't read PID file %s: %s", pidfile, err)
return
}
err = util.SendProcessSignal(pid, syscall.SIGHUP)
err = sysutil.SendProcessSignal(pid, syscall.SIGHUP)
if err != nil {
log.Error("Can't send signal to PID %d: %s", pid, err)
return
Expand Down
19 changes: 13 additions & 6 deletions internal/util/os_freebsd.go → internal/sysutil/os_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// +build freebsd
//+build freebsd

package util
// Package sysutil contains utilities for functions requiring system calls.
package sysutil

import (
"os"
Expand All @@ -9,8 +10,14 @@ import (
"github.com/AdguardTeam/golibs/log"
)

// Set user-specified limit of how many fd's we can use
// https://github.com/AdguardTeam/AdGuardHome/internal/issues/659
// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
return HaveAdminRights()
}

// 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) {
var rlim syscall.Rlimit
rlim.Max = int64(val)
Expand All @@ -21,12 +28,12 @@ func SetRlimit(val uint) {
}
}

// Check if the current user has root (administrator) rights
// HaveAdminRights checks if the current user has root (administrator) rights.
func HaveAdminRights() (bool, error) {
return os.Getuid() == 0, nil
}

// SendProcessSignal - send signal to a process
// SendProcessSignal sends signal to a process.
func SendProcessSignal(pid int, sig syscall.Signal) error {
return syscall.Kill(pid, sig)
}
41 changes: 41 additions & 0 deletions internal/sysutil/os_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//+build linux

// Package sysutil contains utilities for functions requiring system calls.
package sysutil

import (
"os"
"syscall"

"github.com/AdguardTeam/golibs/log"
"golang.org/x/sys/unix"
)

// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
cnbs, err := unix.PrctlRetInt(unix.PR_CAP_AMBIENT, unix.PR_CAP_AMBIENT_IS_SET, unix.CAP_NET_BIND_SERVICE, 0, 0)
return cnbs == 1, err
}

// 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) {
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)
}
}

// HaveAdminRights checks if the current user has root (administrator) rights.
func HaveAdminRights() (bool, error) {
return os.Getuid() == 0, nil
}

// SendProcessSignal sends signal to a process.
func SendProcessSignal(pid int, sig syscall.Signal) error {
return syscall.Kill(pid, sig)
}
19 changes: 13 additions & 6 deletions internal/util/os_unix.go → internal/sysutil/os_unix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// +build aix darwin dragonfly linux netbsd openbsd solaris
//+build aix darwin dragonfly netbsd openbsd solaris

package util
// Package sysutil contains utilities for functions requiring system calls.
package sysutil

import (
"os"
Expand All @@ -9,8 +10,14 @@ import (
"github.com/AdguardTeam/golibs/log"
)

// Set user-specified limit of how many fd's we can use
// https://github.com/AdguardTeam/AdGuardHome/internal/issues/659
// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
return HaveAdminRights()
}

// 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) {
var rlim syscall.Rlimit
rlim.Max = uint64(val)
Expand All @@ -21,12 +28,12 @@ func SetRlimit(val uint) {
}
}

// Check if the current user has root (administrator) rights
// HaveAdminRights checks if the current user has root (administrator) rights.
func HaveAdminRights() (bool, error) {
return os.Getuid() == 0, nil
}

// SendProcessSignal - send signal to a process
// SendProcessSignal sends signal to a process.
func SendProcessSignal(pid int, sig syscall.Signal) error {
return syscall.Kill(pid, sig)
}
16 changes: 14 additions & 2 deletions internal/util/os_windows.go → internal/sysutil/os_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package util
//+build windows

// Package sysutil contains utilities for functions requiring system calls.
package sysutil

import (
"fmt"
Expand All @@ -7,10 +10,18 @@ import (
"golang.org/x/sys/windows"
)

// Set user-specified limit of how many fd's we can use
// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
return HaveAdminRights()
}

// 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) {
}

// HaveAdminRights checks if the current user has root (administrator) rights.
func HaveAdminRights() (bool, error) {
var token windows.Token
h := windows.CurrentProcess()
Expand All @@ -32,6 +43,7 @@ func HaveAdminRights() (bool, error) {
return true, nil
}

// SendProcessSignal sends signal to a process.
func SendProcessSignal(pid int, sig syscall.Signal) error {
return fmt.Errorf("not supported on Windows")
}
7 changes: 4 additions & 3 deletions internal/util/syslog_others.go → internal/sysutil/syslog.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// +build !windows,!nacl,!plan9
//+build !windows,!nacl,!plan9

package util
// Package sysutil contains utilities for functions requiring system calls.
package sysutil

import (
"log"
"log/syslog"
)

// ConfigureSyslog reroutes standard logger output to syslog
// ConfigureSyslog reroutes standard logger output to syslog.
func ConfigureSyslog(serviceName string) error {
w, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, serviceName)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package util
//+build windows nacl plan9

// Package sysutil contains utilities for functions requiring system calls.
package sysutil

import (
"log"
Expand All @@ -17,6 +20,7 @@ func (w *eventLogWriter) Write(b []byte) (int, error) {
return len(b), w.el.Info(1, string(b))
}

// ConfigureSyslog reroutes standard logger output to syslog.
func ConfigureSyslog(serviceName string) error {
// Note that the eventlog src is the same as the service name
// Otherwise, we will get "the description for event id cannot be found" warning in every log record
Expand Down

0 comments on commit 778097c

Please sign in to comment.