mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 22:45:46 +03:00
5e71f5df6a
Merge in DNS/adguard-home from 2846-cover-aghnet-vol.3 to master Updates #2846. Squashed commit of the following: commit cb22987c43c17bbc8d098e65639cc84e2284bc7b Merge: cf995e9df31ffcc5
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 30 15:01:10 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit cf995e9dce635f16e10406a61e2ab12f06407f1f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:29:50 2022 +0300 aghnet: imp tests commit bc225fe8800633b29216840bc7d5b82d7c2d2bfb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:03:06 2022 +0300 aghnet: imp tests commit a82eb6045495b94a2e81ced9a3ef5bfe65788e56 Merge: f80812490d562a7b
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:39:13 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit f80812490c49f69655d409c6f015b069affa2f19 Merge: edccaa793603b1fc
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:29:20 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit edccaa79fca061ffeea1985c293eed123b16a09c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 13:53:40 2022 +0300 aghnet: imp tests commit 7c5028c92f0a6680516bda67c73e794182c9b825 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 18:01:28 2022 +0300 aghnet: imp code & docs commit 7897c6b13e9be340ae8a71947a8a0bab82c682eb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 17:11:46 2022 +0300 aghnet: imp coverage commit 1eef110af3bf721a0275c695bf27c31815abff04 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 23 21:10:29 2022 +0300 all: return byte slice
177 lines
4.5 KiB
Go
177 lines
4.5 KiB
Go
// Package aghos contains utilities for functions requiring system calls and
|
|
// other OS-specific APIs. OS-specific network handling should go to aghnet
|
|
// instead.
|
|
package aghos
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// UnsupportedError is returned by functions and methods when a particular
|
|
// operation Op cannot be performed on the current OS.
|
|
type UnsupportedError struct {
|
|
Op string
|
|
OS string
|
|
}
|
|
|
|
// Error implements the error interface for *UnsupportedError.
|
|
func (err *UnsupportedError) Error() (msg string) {
|
|
return fmt.Sprintf("%s is unsupported on %s", err.Op, err.OS)
|
|
}
|
|
|
|
// Unsupported is a helper that returns an *UnsupportedError with the Op field
|
|
// set to op and the OS field set to the current OS.
|
|
func Unsupported(op string) (err error) {
|
|
return &UnsupportedError{
|
|
Op: op,
|
|
OS: runtime.GOOS,
|
|
}
|
|
}
|
|
|
|
// 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.
|
|
func HaveAdminRights() (bool, error) {
|
|
return haveAdminRights()
|
|
}
|
|
|
|
// MaxCmdOutputSize is the maximum length of performed shell command output in
|
|
// bytes.
|
|
const MaxCmdOutputSize = 64 * 1024
|
|
|
|
// RunCommand runs shell command.
|
|
func RunCommand(command string, arguments ...string) (code int, output []byte, err error) {
|
|
cmd := exec.Command(command, arguments...)
|
|
out, err := cmd.Output()
|
|
if len(out) > MaxCmdOutputSize {
|
|
out = out[:MaxCmdOutputSize]
|
|
}
|
|
|
|
if err != nil {
|
|
if eerr := new(exec.ExitError); errors.As(err, &eerr) {
|
|
return eerr.ExitCode(), eerr.Stderr, nil
|
|
}
|
|
|
|
return 1, nil, fmt.Errorf("command %q failed: %w: %s", command, err, out)
|
|
}
|
|
|
|
return cmd.ProcessState.ExitCode(), out, nil
|
|
}
|
|
|
|
// PIDByCommand searches for process named command and returns its PID ignoring
|
|
// the PIDs from except. If no processes found, the error returned.
|
|
func PIDByCommand(command string, except ...int) (pid int, err error) {
|
|
// Don't use -C flag here since it's a feature of linux's ps
|
|
// implementation. Use POSIX-compatible flags instead.
|
|
//
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3457.
|
|
cmd := exec.Command("ps", "-A", "-o", "pid=", "-o", "comm=")
|
|
|
|
var stdout io.ReadCloser
|
|
if stdout, err = cmd.StdoutPipe(); err != nil {
|
|
return 0, fmt.Errorf("getting the command's stdout pipe: %w", err)
|
|
}
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
return 0, fmt.Errorf("start command executing: %w", err)
|
|
}
|
|
|
|
var instNum int
|
|
pid, instNum, err = parsePSOutput(stdout, command, except)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if err = cmd.Wait(); err != nil {
|
|
return 0, fmt.Errorf("executing the command: %w", err)
|
|
}
|
|
|
|
switch instNum {
|
|
case 0:
|
|
// TODO(e.burkov): Use constant error.
|
|
return 0, fmt.Errorf("no %s instances found", command)
|
|
case 1:
|
|
// Go on.
|
|
default:
|
|
log.Info("warning: %d %s instances found", instNum, command)
|
|
}
|
|
|
|
if code := cmd.ProcessState.ExitCode(); code != 0 {
|
|
return 0, fmt.Errorf("ps finished with code %d", code)
|
|
}
|
|
|
|
return pid, nil
|
|
}
|
|
|
|
// parsePSOutput scans the output of ps searching the largest PID of the process
|
|
// associated with cmdName ignoring PIDs from ignore. A valid line from
|
|
// r should look like these:
|
|
//
|
|
// 123 ./example-cmd
|
|
// 1230 some/base/path/example-cmd
|
|
// 3210 example-cmd
|
|
//
|
|
func parsePSOutput(r io.Reader, cmdName string, ignore []int) (largest, instNum int, err error) {
|
|
s := bufio.NewScanner(r)
|
|
for s.Scan() {
|
|
fields := strings.Fields(s.Text())
|
|
if len(fields) != 2 || path.Base(fields[1]) != cmdName {
|
|
continue
|
|
}
|
|
|
|
cur, aerr := strconv.Atoi(fields[0])
|
|
if aerr != nil || cur < 0 || intIn(cur, ignore) {
|
|
continue
|
|
}
|
|
|
|
instNum++
|
|
if cur > largest {
|
|
largest = cur
|
|
}
|
|
}
|
|
if err = s.Err(); err != nil {
|
|
return 0, 0, fmt.Errorf("scanning stdout: %w", err)
|
|
}
|
|
|
|
return largest, instNum, nil
|
|
}
|
|
|
|
// intIn returns true if nums contains n.
|
|
func intIn(n int, nums []int) (ok bool) {
|
|
for _, nn := range nums {
|
|
if n == nn {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsOpenWrt returns true if host OS is OpenWrt.
|
|
func IsOpenWrt() (ok bool) {
|
|
return isOpenWrt()
|
|
}
|
|
|
|
// RootDirFS returns the fs.FS rooted at the operating system's root.
|
|
func RootDirFS() (fsys fs.FS) {
|
|
// Use empty string since os.DirFS implicitly prepends a slash to it. This
|
|
// behavior is undocumented but it currently works.
|
|
return os.DirFS("")
|
|
}
|