33de701263
* refactor(atomicx): move outside the engine package After merging probe-engine into probe-cli, my impression is that we have too much unnecessary nesting of packages in this repository. The idea of this commit and of a bunch of following commits will instead be to reduce the nesting and simplify the structure. While there, improve the documentation. * fix: always use the atomicx package For consistency, never use sync/atomic and always use ./internal/atomicx so we can just grep and make sure we're not risking to crash if we make a subtle mistake on a 32 bit platform. While there, mention in the contributing guidelines that we want to always prefer the ./internal/atomicx package over sync/atomic. * fix(atomicx): remove unnecessary constructor We don't need a constructor here. The default constructed `&Int64{}` instance is already usable and the constructor does not add anything to what we are doing, rather it just creates extra confusion. * cleanup(atomicx): we are not using Float64 Because atomicx.Float64 is unused, we can safely zap it. * cleanup(atomicx): simplify impl and improve tests We can simplify the implementation by using defer and by letting the Load() method call Add(0). We can improve tests by making many goroutines updated the atomic int64 value concurrently. * refactor(fsx): can live in the ./internal pkg Let us reduce the amount of nesting. While there, ensure that the package only exports the bare minimum, and improve the documentation of the tests, to ease reading the code. * refactor: move runtimex to ./internal * refactor: move shellx into the ./internal package While there, remove unnecessary dependency between packages. While there, specify in the contributing guidelines that one should use x/sys/execabs instead of os/exec. * refactor: move ooapi into the ./internal pkg * refactor(humanize): move to ./internal and better docs * refactor: move platform to ./internal * refactor(randx): move to ./internal * refactor(multierror): move into the ./internal pkg * refactor(kvstore): all kvstores in ./internal Rather than having part of the kvstore inside ./internal/engine/kvstore and part in ./internal/engine/kvstore.go, let us put every piece of code that is kvstore related into the ./internal/kvstore package. * fix(kvstore): always return ErrNoSuchKey on Get() error It should help to use the kvstore everywhere removing all the copies that are lingering around the tree. * sessionresolver: make KVStore mandatory Simplifies implementation. While there, use the ./internal/kvstore package rather than having our private implementation. * fix(ooapi): use the ./internal/kvstore package * fix(platform): better documentation
185 lines
4.4 KiB
Go
185 lines
4.4 KiB
Go
package autorun
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"text/template"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils"
|
|
"github.com/ooni/probe-cli/v3/internal/shellx"
|
|
"golang.org/x/sys/execabs"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
type managerDarwin struct{}
|
|
|
|
var (
|
|
plistDir = os.ExpandEnv("$HOME/Library/LaunchAgents/")
|
|
plistPath = filepath.Join(plistDir, "org.ooni.cli.plist")
|
|
domainTarget = fmt.Sprintf("gui/%d", os.Getuid())
|
|
serviceTarget = fmt.Sprintf("%s/org.ooni.cli", domainTarget)
|
|
)
|
|
|
|
var plistTemplate = `
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
<plist version="1.0">
|
|
<dict>
|
|
<key>Label</key>
|
|
<string>org.ooni.cli</string>
|
|
<key>KeepAlive</key>
|
|
<false/>
|
|
<key>RunAtLoad</key>
|
|
<true/>
|
|
<key>ProgramArguments</key>
|
|
<array>
|
|
<string>{{ .Executable }}</string>
|
|
<string>--log-handler=syslog</string>
|
|
<string>run</string>
|
|
<string>unattended</string>
|
|
</array>
|
|
<key>StartInterval</key>
|
|
<integer>3600</integer>
|
|
</dict>
|
|
</plist>
|
|
`
|
|
|
|
func runQuiteQuietly(name string, arg ...string) error {
|
|
log.Infof("exec: %s %s", name, strings.Join(arg, " "))
|
|
return shellx.RunQuiet(name, arg...)
|
|
}
|
|
|
|
func darwinVersionMajor() (int, error) {
|
|
out, err := execabs.Command("uname", "-r").Output()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
v := bytes.Split(out, []byte("."))
|
|
if len(v) != 3 {
|
|
return 0, errors.New("cannot split version")
|
|
}
|
|
major, err := strconv.Atoi(string(v[0]))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return major, nil
|
|
}
|
|
|
|
var errNotImplemented = errors.New(
|
|
"autorun: command not implemented in this version of macOS")
|
|
|
|
func (managerDarwin) LogShow() error {
|
|
major, _ := darwinVersionMajor()
|
|
if major < 20 /* macOS 11.0 Big Sur */ {
|
|
return errNotImplemented
|
|
}
|
|
return shellx.Run("log", "show", "--info", "--debug",
|
|
"--process", "ooniprobe", "--style", "compact")
|
|
}
|
|
|
|
func (managerDarwin) LogStream() error {
|
|
return shellx.Run("log", "stream", "--style", "compact", "--level",
|
|
"debug", "--process", "ooniprobe")
|
|
}
|
|
|
|
func (managerDarwin) mustNotHavePlist() error {
|
|
log.Infof("exec: test -f %s && already_registered()", plistPath)
|
|
if utils.FileExists(plistPath) {
|
|
// This is not atomic. Do we need atomicity here?
|
|
return errors.New("autorun: service already registered")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (managerDarwin) writePlist() error {
|
|
executable, err := os.Executable()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var out bytes.Buffer
|
|
t := template.Must(template.New("plist").Parse(plistTemplate))
|
|
in := struct{ Executable string }{Executable: executable}
|
|
if err := t.Execute(&out, in); err != nil {
|
|
return err
|
|
}
|
|
log.Infof("exec: mkdir -p %s", plistDir)
|
|
if err := os.MkdirAll(plistDir, 0755); err != nil {
|
|
return err
|
|
}
|
|
log.Infof("exec: writePlist(%s)", plistPath)
|
|
return ioutil.WriteFile(plistPath, out.Bytes(), 0644)
|
|
}
|
|
|
|
func (managerDarwin) start() error {
|
|
if err := runQuiteQuietly("launchctl", "enable", serviceTarget); err != nil {
|
|
return err
|
|
}
|
|
return runQuiteQuietly("launchctl", "bootstrap", domainTarget, plistPath)
|
|
}
|
|
|
|
func (m managerDarwin) Start() error {
|
|
operations := []func() error{m.mustNotHavePlist, m.writePlist, m.start}
|
|
for _, op := range operations {
|
|
if err := op(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (managerDarwin) stop() error {
|
|
var failure *execabs.ExitError
|
|
err := runQuiteQuietly("launchctl", "bootout", serviceTarget)
|
|
if errors.As(err, &failure) && failure.ExitCode() == int(unix.ESRCH) {
|
|
err = nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (managerDarwin) removeFile() error {
|
|
log.Infof("exec: rm -f %s", plistPath)
|
|
err := os.Remove(plistPath)
|
|
if errors.Is(err, unix.ENOENT) {
|
|
err = nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (m managerDarwin) Stop() error {
|
|
operations := []func() error{m.stop, m.removeFile}
|
|
for _, op := range operations {
|
|
if err := op(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m managerDarwin) Status() (string, error) {
|
|
err := runQuiteQuietly("launchctl", "kill", "SIGINFO", serviceTarget)
|
|
var failure *execabs.ExitError
|
|
if errors.As(err, &failure) {
|
|
switch failure.ExitCode() {
|
|
case int(unix.ESRCH):
|
|
return StatusScheduled, nil
|
|
case 113: // exit code when there's no plist
|
|
return StatusStopped, nil
|
|
}
|
|
}
|
|
if err != nil {
|
|
return "", fmt.Errorf("autorun: unexpected error: %w", err)
|
|
}
|
|
return StatusRunning, nil
|
|
}
|
|
|
|
func init() {
|
|
register("darwin", managerDarwin{})
|
|
}
|