-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge in DNS/adguard-home from 2231-autoupdate to master Updates #2231. Squashed commit of the following: commit 4ee9148 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 30 19:08:14 2020 +0300 sysutil: provide os-independent interface commit 778097c Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 30 16:40:33 2020 +0300 all: add sysutil package
- Loading branch information
1 parent
6e615c6
commit 641db73
Showing
13 changed files
with
158 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//+build freebsd | ||
|
||
package sysutil | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
|
||
"github.com/AdguardTeam/golibs/log" | ||
) | ||
|
||
func canBindPrivilegedPorts() (can bool, err error) { | ||
return HaveAdminRights() | ||
} | ||
|
||
func setRlimit(val uint) { | ||
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) | ||
} | ||
} | ||
|
||
func haveAdminRights() (bool, error) { | ||
return os.Getuid() == 0, nil | ||
} | ||
|
||
func sendProcessSignal(pid int, sig syscall.Signal) error { | ||
return syscall.Kill(pid, sig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//+build linux | ||
|
||
package sysutil | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
|
||
"github.com/AdguardTeam/golibs/log" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
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 | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
func haveAdminRights() (bool, error) { | ||
return os.Getuid() == 0, nil | ||
} | ||
|
||
func sendProcessSignal(pid int, sig syscall.Signal) error { | ||
return syscall.Kill(pid, sig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//+build aix darwin dragonfly netbsd openbsd solaris | ||
|
||
package sysutil | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
|
||
"github.com/AdguardTeam/golibs/log" | ||
) | ||
|
||
func canBindPrivilegedPorts() (can bool, err error) { | ||
return HaveAdminRights() | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
func haveAdminRights() (bool, error) { | ||
return os.Getuid() == 0, nil | ||
} | ||
|
||
func sendProcessSignal(pid int, sig syscall.Signal) error { | ||
return syscall.Kill(pid, sig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
internal/util/syslog_others.go → internal/sysutil/syslog_others.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.