1
0
mirror of https://github.com/DNSCrypt/dnscrypt-proxy.git synced 2024-12-29 00:30:30 +01:00
dnscrypt-proxy/vendor/github.com/kardianos/service/service_unix.go

143 lines
3.5 KiB
Go
Raw Normal View History

2018-01-17 11:28:43 +01:00
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.
2022-10-15 10:37:07 +02:00
//go:build linux || darwin || solaris || aix || freebsd
2020-06-08 19:22:36 +02:00
// +build linux darwin solaris aix freebsd
2018-01-17 11:28:43 +01:00
package service
import (
2019-11-17 22:54:56 +01:00
"bytes"
2018-01-17 11:28:43 +01:00
"fmt"
2018-08-23 00:23:59 +02:00
"io"
2018-01-17 11:28:43 +01:00
"io/ioutil"
"log/syslog"
"os/exec"
2018-08-23 00:23:59 +02:00
"syscall"
2018-01-17 11:28:43 +01:00
)
2022-10-15 10:37:07 +02:00
const defaultLogDirectory = "/var/log"
2018-01-17 11:28:43 +01:00
func newSysLogger(name string, errs chan<- error) (Logger, error) {
w, err := syslog.New(syslog.LOG_INFO, name)
if err != nil {
return nil, err
}
return sysLogger{w, errs}, nil
}
type sysLogger struct {
*syslog.Writer
errs chan<- error
}
func (s sysLogger) send(err error) error {
if err != nil && s.errs != nil {
s.errs <- err
}
return err
}
func (s sysLogger) Error(v ...interface{}) error {
return s.send(s.Writer.Err(fmt.Sprint(v...)))
}
func (s sysLogger) Warning(v ...interface{}) error {
return s.send(s.Writer.Warning(fmt.Sprint(v...)))
}
func (s sysLogger) Info(v ...interface{}) error {
return s.send(s.Writer.Info(fmt.Sprint(v...)))
}
func (s sysLogger) Errorf(format string, a ...interface{}) error {
return s.send(s.Writer.Err(fmt.Sprintf(format, a...)))
}
func (s sysLogger) Warningf(format string, a ...interface{}) error {
return s.send(s.Writer.Warning(fmt.Sprintf(format, a...)))
}
func (s sysLogger) Infof(format string, a ...interface{}) error {
return s.send(s.Writer.Info(fmt.Sprintf(format, a...)))
}
func run(command string, arguments ...string) error {
2018-08-23 00:23:59 +02:00
_, _, err := runCommand(command, false, arguments...)
return err
}
func runWithOutput(command string, arguments ...string) (int, string, error) {
return runCommand(command, true, arguments...)
}
func runCommand(command string, readStdout bool, arguments ...string) (int, string, error) {
2018-01-17 11:28:43 +01:00
cmd := exec.Command(command, arguments...)
2018-08-23 00:23:59 +02:00
var output string
var stdout io.ReadCloser
var err error
if readStdout {
// Connect pipe to read Stdout
stdout, err = cmd.StdoutPipe()
if err != nil {
// Failed to connect pipe
return 0, "", fmt.Errorf("%q failed to connect stdout pipe: %v", command, err)
}
}
2018-01-17 11:28:43 +01:00
// Connect pipe to read Stderr
stderr, err := cmd.StderrPipe()
if err != nil {
// Failed to connect pipe
2018-08-23 00:23:59 +02:00
return 0, "", fmt.Errorf("%q failed to connect stderr pipe: %v", command, err)
2018-01-17 11:28:43 +01:00
}
// Do not use cmd.Run()
if err := cmd.Start(); err != nil {
// Problem while copying stdin, stdout, or stderr
2018-08-23 00:23:59 +02:00
return 0, "", fmt.Errorf("%q failed: %v", command, err)
2018-01-17 11:28:43 +01:00
}
// Zero exit status
// Darwin: launchctl can fail with a zero exit status,
// so check for emtpy stderr
if command == "launchctl" {
slurp, _ := ioutil.ReadAll(stderr)
2019-11-17 22:54:56 +01:00
if len(slurp) > 0 && !bytes.HasSuffix(slurp, []byte("Operation now in progress\n")) {
2018-08-23 00:23:59 +02:00
return 0, "", fmt.Errorf("%q failed with stderr: %s", command, slurp)
}
}
if readStdout {
out, err := ioutil.ReadAll(stdout)
if err != nil {
return 0, "", fmt.Errorf("%q failed while attempting to read stdout: %v", command, err)
} else if len(out) > 0 {
output = string(out)
2018-01-17 11:28:43 +01:00
}
}
if err := cmd.Wait(); err != nil {
2018-08-23 00:23:59 +02:00
exitStatus, ok := isExitError(err)
if ok {
// Command didn't exit with a zero exit status.
return exitStatus, output, err
}
// An error occurred and there is no exit status.
return 0, output, fmt.Errorf("%q failed: %v", command, err)
}
return 0, output, nil
}
func isExitError(err error) (int, bool) {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), true
}
2018-01-17 11:28:43 +01:00
}
2018-08-23 00:23:59 +02:00
return 0, false
2018-01-17 11:28:43 +01:00
}