2020-12-07 09:48:24 -07:00
|
|
|
// +build linux
|
2020-11-30 09:23:14 -07:00
|
|
|
|
2021-03-16 09:42:15 -07:00
|
|
|
package aghos
|
2020-11-30 09:23:14 -07:00
|
|
|
|
|
|
|
import (
|
2021-04-08 10:42:04 -07:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2020-11-30 09:23:14 -07:00
|
|
|
"os"
|
2021-04-08 10:42:04 -07:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-11-30 09:23:14 -07:00
|
|
|
"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)
|
2020-12-04 07:06:19 -07:00
|
|
|
// Don't check the error because it's always nil on Linux.
|
|
|
|
adm, _ := haveAdminRights()
|
|
|
|
|
|
|
|
return cnbs == 1 || adm, err
|
2020-11-30 09:23:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-12-04 07:06:19 -07:00
|
|
|
// The error is nil because the platform-independent function signature
|
|
|
|
// requires returning an error.
|
2020-11-30 09:23:14 -07:00
|
|
|
return os.Getuid() == 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendProcessSignal(pid int, sig syscall.Signal) error {
|
|
|
|
return syscall.Kill(pid, sig)
|
|
|
|
}
|
2021-04-08 10:42:04 -07:00
|
|
|
|
|
|
|
func isOpenWrt() (ok bool) {
|
|
|
|
const etcDir = "/etc"
|
|
|
|
|
|
|
|
// TODO(e.burkov): Take care of dealing with fs package after updating
|
|
|
|
// Go version to 1.16.
|
|
|
|
fileInfos, err := ioutil.ReadDir(etcDir)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// fNameSubstr is a part of a name of the desired file.
|
|
|
|
const fNameSubstr = "release"
|
|
|
|
osNameData := []byte("OpenWrt")
|
|
|
|
|
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := fileInfo.Name()
|
|
|
|
if !strings.Contains(fn, fNameSubstr) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var body []byte
|
|
|
|
body, err = ioutil.ReadFile(filepath.Join(etcDir, fn))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Contains(body, osNameData) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|