Skip to content

Commit

Permalink
sysutil: provide os-independent interface
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 30, 2020
1 parent 778097c commit 4ee9148
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 52 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ and this project adheres to

### Changed

- Updating and relaunching policy is now OS-dependent ([#2231]).
- Post-updating relaunch possibility is now determined OS-dependently ([#2231]).
- Made the mobileconfig HTTP API more robust and predictable, add parameters and
improve error response ([#2358]).
- Improved HTTP requests handling and timeouts. ([#2343]).
Expand Down
15 changes: 4 additions & 11 deletions internal/sysutil/os_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//+build freebsd

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

import (
Expand All @@ -10,15 +9,11 @@ import (
"github.com/AdguardTeam/golibs/log"
)

// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
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) {
func setRlimit(val uint) {
var rlim syscall.Rlimit
rlim.Max = int64(val)
rlim.Cur = int64(val)
Expand All @@ -28,12 +23,10 @@ func SetRlimit(val uint) {
}
}

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

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

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

import (
Expand All @@ -11,16 +10,12 @@ import (
"golang.org/x/sys/unix"
)

// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
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) {
func setRlimit(val uint) {
var rlim syscall.Rlimit
rlim.Max = uint64(val)
rlim.Cur = uint64(val)
Expand All @@ -30,12 +25,10 @@ func SetRlimit(val uint) {
}
}

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

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

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

import (
Expand All @@ -10,15 +9,11 @@ import (
"github.com/AdguardTeam/golibs/log"
)

// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
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) {
func setRlimit(val uint) {
var rlim syscall.Rlimit
rlim.Max = uint64(val)
rlim.Cur = uint64(val)
Expand All @@ -28,12 +23,10 @@ func SetRlimit(val uint) {
}
}

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

// SendProcessSignal sends signal to a process.
func SendProcessSignal(pid int, sig syscall.Signal) error {
func sendProcessSignal(pid int, sig syscall.Signal) error {
return syscall.Kill(pid, sig)
}
15 changes: 4 additions & 11 deletions internal/sysutil/os_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//+build windows

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

import (
Expand All @@ -10,19 +9,14 @@ import (
"golang.org/x/sys/windows"
)

// CanBindPrivilegedPorts checks if current process can bind to privileged
// ports.
func CanBindPrivilegedPorts() (can bool, err error) {
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) {
func setRlimit(val uint) {
}

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

// SendProcessSignal sends signal to a process.
func SendProcessSignal(pid int, sig syscall.Signal) error {
func sendProcessSignal(pid int, sig syscall.Signal) error {
return fmt.Errorf("not supported on Windows")
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//+build !windows,!nacl,!plan9

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

import (
"log"
"log/syslog"
)

// ConfigureSyslog reroutes standard logger output to syslog.
func ConfigureSyslog(serviceName string) error {
func configureSyslog(serviceName string) error {
w, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, serviceName)
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions internal/sysutil/syslog_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//+build windows nacl plan9

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

import (
Expand All @@ -15,13 +14,12 @@ type eventLogWriter struct {
el *eventlog.Log
}

// Write sends a log message to the Event Log.
// Write implements io.Writer interface for eventLogWriter.
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 {
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
31 changes: 31 additions & 0 deletions internal/sysutil/sysutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Package sysutil contains utilities for functions requiring system calls.
package sysutil

import "syscall"

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

// HaveAdminRights checks if the current user has root (administrator) rights.
func HaveAdminRights() (bool, error) {
return haveAdminRights()
}

// SendProcessSignal sends signal to a process.
func SendProcessSignal(pid int, sig syscall.Signal) error {
return sendProcessSignal(pid, sig)
}

// ConfigureSyslog reroutes standard logger output to syslog.
func ConfigureSyslog(serviceName string) error {
return configureSyslog(serviceName)
}

0 comments on commit 4ee9148

Please sign in to comment.