2021-06-18 17:55:01 +03:00
|
|
|
// Package aghos contains utilities for functions requiring system calls and
|
|
|
|
// other OS-specific APIs. OS-specific network handling should go to aghnet
|
|
|
|
// instead.
|
2021-03-16 19:42:15 +03:00
|
|
|
package aghos
|
2020-11-30 19:23:14 +03:00
|
|
|
|
2021-03-22 16:46:36 +03:00
|
|
|
import (
|
2021-08-18 13:20:56 +03:00
|
|
|
"bufio"
|
2021-03-22 16:46:36 +03:00
|
|
|
"fmt"
|
2021-08-18 13:20:56 +03:00
|
|
|
"io"
|
2021-10-14 19:39:21 +03:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2021-03-22 16:46:36 +03:00
|
|
|
"os/exec"
|
2021-08-18 13:20:56 +03:00
|
|
|
"path"
|
2021-06-04 16:35:34 +03:00
|
|
|
"runtime"
|
2024-02-08 20:39:18 +03:00
|
|
|
"slices"
|
2021-08-18 13:20:56 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-24 18:00:30 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-08-18 13:20:56 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-01-24 19:50:19 +03:00
|
|
|
"github.com/AdguardTeam/golibs/mathutil"
|
2021-03-22 16:46:36 +03:00
|
|
|
)
|
2020-11-30 19:23:14 +03:00
|
|
|
|
2021-06-04 16:35:34 +03:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 21:04:13 +03:00
|
|
|
|
|
|
|
// 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)
|
2020-11-30 19:23:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HaveAdminRights checks if the current user has root (administrator) rights.
|
|
|
|
func HaveAdminRights() (bool, error) {
|
|
|
|
return haveAdminRights()
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
// MaxCmdOutputSize is the maximum length of performed shell command output in
|
|
|
|
// bytes.
|
|
|
|
const MaxCmdOutputSize = 64 * 1024
|
2021-03-22 16:46:36 +03:00
|
|
|
|
|
|
|
// RunCommand runs shell command.
|
2022-03-30 15:11:57 +03:00
|
|
|
func RunCommand(command string, arguments ...string) (code int, output []byte, err error) {
|
2021-03-22 16:46:36 +03:00
|
|
|
cmd := exec.Command(command, arguments...)
|
|
|
|
out, err := cmd.Output()
|
2023-01-24 19:50:19 +03:00
|
|
|
|
|
|
|
out = out[:mathutil.Min(len(out), MaxCmdOutputSize)]
|
2021-08-24 18:00:30 +03:00
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
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)
|
2021-03-22 16:46:36 +03:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
return cmd.ProcessState.ExitCode(), out, nil
|
2021-03-22 16:46:36 +03:00
|
|
|
}
|
2021-04-08 20:42:04 +03:00
|
|
|
|
2021-08-18 13:20:56 +03:00
|
|
|
// 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
|
2021-12-09 18:26:56 +03:00
|
|
|
pid, instNum, err = parsePSOutput(stdout, command, except)
|
2021-08-18 13:20:56 +03:00
|
|
|
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
|
2022-08-31 18:57:02 +03:00
|
|
|
// associated with cmdName ignoring PIDs from ignore. A valid line from r
|
|
|
|
// should look like these:
|
2021-08-18 13:20:56 +03:00
|
|
|
//
|
2022-08-31 18:57:02 +03:00
|
|
|
// 123 ./example-cmd
|
|
|
|
// 1230 some/base/path/example-cmd
|
|
|
|
// 3210 example-cmd
|
2021-12-09 18:26:56 +03:00
|
|
|
func parsePSOutput(r io.Reader, cmdName string, ignore []int) (largest, instNum int, err error) {
|
2021-08-18 13:20:56 +03:00
|
|
|
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])
|
2023-01-24 19:50:19 +03:00
|
|
|
if aerr != nil || cur < 0 || slices.Contains(ignore, cur) {
|
2021-08-18 13:20:56 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
instNum++
|
2023-01-24 19:50:19 +03:00
|
|
|
largest = mathutil.Max(largest, cur)
|
2021-08-18 13:20:56 +03:00
|
|
|
}
|
|
|
|
if err = s.Err(); err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("scanning stdout: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return largest, instNum, nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 20:42:04 +03:00
|
|
|
// IsOpenWrt returns true if host OS is OpenWrt.
|
|
|
|
func IsOpenWrt() (ok bool) {
|
|
|
|
return isOpenWrt()
|
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2022-12-07 16:29:06 +03:00
|
|
|
// RootDirFS returns the [fs.FS] rooted at the operating system's root. On
|
|
|
|
// Windows it returns the fs.FS rooted at the volume of the system directory
|
|
|
|
// (usually, C:).
|
2021-10-14 19:39:21 +03:00
|
|
|
func RootDirFS() (fsys fs.FS) {
|
2022-12-07 16:29:06 +03:00
|
|
|
return rootDirFS()
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// NotifyReconfigureSignal notifies c on receiving reconfigure signals.
|
|
|
|
func NotifyReconfigureSignal(c chan<- os.Signal) {
|
|
|
|
notifyReconfigureSignal(c)
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
// NotifyShutdownSignal notifies c on receiving shutdown signals.
|
|
|
|
func NotifyShutdownSignal(c chan<- os.Signal) {
|
|
|
|
notifyShutdownSignal(c)
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// IsReconfigureSignal returns true if sig is a reconfigure signal.
|
|
|
|
func IsReconfigureSignal(sig os.Signal) (ok bool) {
|
|
|
|
return isReconfigureSignal(sig)
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
// IsShutdownSignal returns true if sig is a shutdown signal.
|
|
|
|
func IsShutdownSignal(sig os.Signal) (ok bool) {
|
|
|
|
return isShutdownSignal(sig)
|
|
|
|
}
|
2023-08-31 17:50:46 +03:00
|
|
|
|
|
|
|
// SendShutdownSignal sends the shutdown signal to the channel.
|
|
|
|
func SendShutdownSignal(c chan<- os.Signal) {
|
|
|
|
sendShutdownSignal(c)
|
|
|
|
}
|