mirror of
https://github.com/safing/portmaster
synced 2025-09-04 11:39:29 +00:00
Finish windows support
- Fix service creation - Singleton lock - Control error/logging - Revamp run/service interaction - Add option to turn off stdout/stderr output (for Windows Service) - Use log instead of fmt.Print*
This commit is contained in:
parent
d9ae6a140d
commit
c6083fc464
10 changed files with 486 additions and 246 deletions
|
@ -11,7 +11,7 @@ package main
|
||||||
// MS AttachConsole: https://docs.microsoft.com/en-us/windows/console/attachconsole
|
// MS AttachConsole: https://docs.microsoft.com/en-us/windows/console/attachconsole
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -116,11 +116,11 @@ func attachToParentConsole() (attached bool, err error) {
|
||||||
var st uint32
|
var st uint32
|
||||||
err := windows.GetConsoleMode(h, &st)
|
err := windows.GetConsoleMode(h, &st)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s failed to get console mode: %s\n", logPrefix, err)
|
log.Printf("failed to get console mode: %s\n", err)
|
||||||
} else {
|
} else {
|
||||||
err = windows.SetConsoleMode(h, st&^windows.DISABLE_NEWLINE_AUTO_RETURN)
|
err = windows.SetConsoleMode(h, st&^windows.DISABLE_NEWLINE_AUTO_RETURN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s failed to set console mode: %s\n", logPrefix, err)
|
log.Printf("failed to set console mode: %s\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,18 +128,18 @@ func attachToParentConsole() (attached bool, err error) {
|
||||||
// fix std handles to correct values (ie. redirects)
|
// fix std handles to correct values (ie. redirects)
|
||||||
if stdin != invalid {
|
if stdin != invalid {
|
||||||
os.Stdin = os.NewFile(uintptr(stdin), "stdin")
|
os.Stdin = os.NewFile(uintptr(stdin), "stdin")
|
||||||
fmt.Printf("%s fixed os.Stdin after attaching to parent console\n", logPrefix)
|
log.Printf("fixed os.Stdin after attaching to parent console\n")
|
||||||
}
|
}
|
||||||
if stdout != invalid {
|
if stdout != invalid {
|
||||||
os.Stdout = os.NewFile(uintptr(stdout), "stdout")
|
os.Stdout = os.NewFile(uintptr(stdout), "stdout")
|
||||||
fmt.Printf("%s fixed os.Stdout after attaching to parent console\n", logPrefix)
|
log.Printf("fixed os.Stdout after attaching to parent console\n")
|
||||||
}
|
}
|
||||||
if stderr != invalid {
|
if stderr != invalid {
|
||||||
os.Stderr = os.NewFile(uintptr(stderr), "stderr")
|
os.Stderr = os.NewFile(uintptr(stderr), "stderr")
|
||||||
fmt.Printf("%s fixed os.Stderr after attaching to parent console\n", logPrefix)
|
log.Printf("fixed os.Stderr after attaching to parent console\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s attached to parent console\n", logPrefix)
|
log.Println("attached to parent console")
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portmaster/updates"
|
"github.com/safing/portmaster/updates"
|
||||||
|
@ -22,7 +22,7 @@ func getFile(opts *Options) (*updates.File, error) {
|
||||||
|
|
||||||
// download
|
// download
|
||||||
if opts.AllowDownload {
|
if opts.AllowDownload {
|
||||||
fmt.Printf("%s downloading %s...\n", logPrefix, opts.Identifier)
|
log.Printf("downloading %s...\n", opts.Identifier)
|
||||||
|
|
||||||
// download indexes
|
// download indexes
|
||||||
err = updates.UpdateIndexes()
|
err = updates.UpdateIndexes()
|
||||||
|
@ -39,7 +39,7 @@ func getFile(opts *Options) (*updates.File, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for 30 seconds
|
// wait for 30 seconds
|
||||||
fmt.Printf("%s waiting for download of %s (by Portmaster Core) to complete...\n", logPrefix, opts.Identifier)
|
log.Printf("waiting for download of %s (by Portmaster Core) to complete...\n", opts.Identifier)
|
||||||
|
|
||||||
// try every 0.5 secs
|
// try every 0.5 secs
|
||||||
for tries := 0; tries < 60; tries++ {
|
for tries := 0; tries < 60; tries++ {
|
||||||
|
|
|
@ -7,8 +7,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -43,7 +45,11 @@ var installService = &cobra.Command{
|
||||||
var uninstallService = &cobra.Command{
|
var uninstallService = &cobra.Command{
|
||||||
Use: "core-service",
|
Use: "core-service",
|
||||||
Short: "Uninstall Portmaster Core Windows Service",
|
Short: "Uninstall Portmaster Core Windows Service",
|
||||||
RunE: uninstallWindowsService,
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
// non-nil dummy to override db flag requirement
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
RunE: uninstallWindowsService,
|
||||||
}
|
}
|
||||||
|
|
||||||
func getExePath() (string, error) {
|
func getExePath() (string, error) {
|
||||||
|
@ -75,12 +81,15 @@ func getExePath() (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServiceExecCommand(exePath string) string {
|
func getServiceExecCommand(exePath string) []string {
|
||||||
return fmt.Sprintf(
|
return []string{
|
||||||
"%s run core-service --db %s --input-signals",
|
|
||||||
windows.EscapeArg(exePath),
|
windows.EscapeArg(exePath),
|
||||||
|
"run",
|
||||||
|
"core-service",
|
||||||
|
"--db",
|
||||||
windows.EscapeArg(*databaseRootDir),
|
windows.EscapeArg(*databaseRootDir),
|
||||||
)
|
"--input-signals",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServiceConfig(exePath string) mgr.Config {
|
func getServiceConfig(exePath string) mgr.Config {
|
||||||
|
@ -88,7 +97,7 @@ func getServiceConfig(exePath string) mgr.Config {
|
||||||
ServiceType: windows.SERVICE_WIN32_OWN_PROCESS,
|
ServiceType: windows.SERVICE_WIN32_OWN_PROCESS,
|
||||||
StartType: mgr.StartAutomatic,
|
StartType: mgr.StartAutomatic,
|
||||||
ErrorControl: mgr.ErrorNormal,
|
ErrorControl: mgr.ErrorNormal,
|
||||||
BinaryPathName: getServiceExecCommand(exePath),
|
BinaryPathName: strings.Join(getServiceExecCommand(exePath), " "),
|
||||||
DisplayName: "Portmaster Core",
|
DisplayName: "Portmaster Core",
|
||||||
Description: "Portmaster Application Firewall - Core Service",
|
Description: "Portmaster Application Firewall - Core Service",
|
||||||
}
|
}
|
||||||
|
@ -130,7 +139,8 @@ func installWindowsService(cmd *cobra.Command, args []string) error {
|
||||||
s, err := m.OpenService(serviceName)
|
s, err := m.OpenService(serviceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// create service
|
// create service
|
||||||
s, err = m.CreateService(serviceName, getServiceExecCommand(exePath), getServiceConfig(exePath))
|
cmd := getServiceExecCommand(exePath)
|
||||||
|
s, err = m.CreateService(serviceName, cmd[0], getServiceConfig(exePath), cmd[1:]...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create service: %s", err)
|
return fmt.Errorf("failed to create service: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -152,9 +162,9 @@ func installWindowsService(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if created {
|
if created {
|
||||||
fmt.Printf("%s created service %s\n", logPrefix, serviceName)
|
log.Printf("created service %s\n", serviceName)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("%s updated service %s\n", logPrefix, serviceName)
|
log.Printf("updated service %s\n", serviceName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -181,6 +191,6 @@ func uninstallWindowsService(cmd *cobra.Command, args []string) error {
|
||||||
return fmt.Errorf("failed to delete service: %s", err)
|
return fmt.Errorf("failed to delete service: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s uninstalled service %s\n", logPrefix, serviceName)
|
log.Printf("uninstalled service %s\n", serviceName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
70
pmctl/lock.go
Normal file
70
pmctl/lock.go
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
processInfo "github.com/shirou/gopsutil/process"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkAndCreateInstanceLock(name string) (pid int32, err error) {
|
||||||
|
lockFilePath := filepath.Join(*databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
|
||||||
|
|
||||||
|
// read current pid file
|
||||||
|
data, err := ioutil.ReadFile(lockFilePath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// create new lock
|
||||||
|
return 0, createInstanceLock(lockFilePath)
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// file exists!
|
||||||
|
parsedPid, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to parse existing lock pid file (ignoring): %s\n", err)
|
||||||
|
return 0, createInstanceLock(lockFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("===== checking if PID %d exists\n", int32(parsedPid))
|
||||||
|
|
||||||
|
// check if process exists
|
||||||
|
p, err := processInfo.NewProcess(int32(parsedPid))
|
||||||
|
if err == nil {
|
||||||
|
_, err = p.Name()
|
||||||
|
if err == nil {
|
||||||
|
// process exists
|
||||||
|
return p.Pid, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// else create new lock
|
||||||
|
return 0, createInstanceLock(lockFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createInstanceLock(lockFilePath string) error {
|
||||||
|
// create database dir
|
||||||
|
err := os.MkdirAll(*databaseRootDir, 0777)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to create base folder: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// create lock file
|
||||||
|
err = ioutil.WriteFile(lockFilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0666)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteInstanceLock(name string) error {
|
||||||
|
lockFilePath := filepath.Join(*databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
|
||||||
|
return os.Remove(lockFilePath)
|
||||||
|
}
|
143
pmctl/logs.go
Normal file
143
pmctl/logs.go
Normal file
|
@ -0,0 +1,143 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/container"
|
||||||
|
"github.com/safing/portbase/database/record"
|
||||||
|
"github.com/safing/portbase/formats/dsd"
|
||||||
|
"github.com/safing/portbase/info"
|
||||||
|
"github.com/safing/portmaster/updates"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func initializeLogFile(logFilePath string, identifier string, updateFile *updates.File) *os.File {
|
||||||
|
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to create log file %s: %s\n", logFilePath, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// create header, so that the portmaster can view log files as a database
|
||||||
|
meta := record.Meta{}
|
||||||
|
meta.Update()
|
||||||
|
meta.SetAbsoluteExpiry(time.Now().Add(720 * time.Hour).Unix()) // one month
|
||||||
|
|
||||||
|
// manually marshal
|
||||||
|
// version
|
||||||
|
c := container.New([]byte{1})
|
||||||
|
// meta
|
||||||
|
metaSection, err := dsd.Dump(meta, dsd.JSON)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to serialize header for log file %s: %s\n", logFilePath, err)
|
||||||
|
finalizeLogFile(logFile, logFilePath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
c.AppendAsBlock(metaSection)
|
||||||
|
// log file data type (string) and newline for better manual viewing
|
||||||
|
c.Append([]byte("S\n"))
|
||||||
|
c.Append([]byte(fmt.Sprintf("executing %s version %s on %s %s\n", identifier, updateFile.Version(), runtime.GOOS, runtime.GOARCH)))
|
||||||
|
|
||||||
|
_, err = logFile.Write(c.CompileData())
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to write header for log file %s: %s\n", logFilePath, err)
|
||||||
|
finalizeLogFile(logFile, logFilePath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return logFile
|
||||||
|
}
|
||||||
|
|
||||||
|
func finalizeLogFile(logFile *os.File, logFilePath string) {
|
||||||
|
err := logFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to close log file %s: %s\n", logFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check file size
|
||||||
|
stat, err := os.Stat(logFilePath)
|
||||||
|
if err == nil {
|
||||||
|
// delete if file is smaller than
|
||||||
|
if stat.Size() < 200 { // header + info is about 150 bytes
|
||||||
|
err := os.Remove(logFilePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to delete empty log file %s: %s\n", logFilePath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initControlLogFile() *os.File {
|
||||||
|
// create logging dir
|
||||||
|
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", "control")
|
||||||
|
err := os.MkdirAll(logFileBasePath, 0777)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// open log file
|
||||||
|
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
|
||||||
|
return initializeLogFile(logFilePath, "control/portmaster-control", updates.NewFile("", info.Version(), false))
|
||||||
|
}
|
||||||
|
|
||||||
|
func logControlError(cErr error) {
|
||||||
|
// check if error present
|
||||||
|
if cErr == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create dir
|
||||||
|
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", "control")
|
||||||
|
err := os.MkdirAll(logFileBasePath, 0777)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// open log file
|
||||||
|
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.error.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
|
||||||
|
errorFile := initializeLogFile(logFilePath, "control/portmaster-control", updates.NewFile("", info.Version(), false))
|
||||||
|
if errorFile == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// write error and close
|
||||||
|
fmt.Fprintln(errorFile, cErr.Error())
|
||||||
|
errorFile.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func logControlStack() {
|
||||||
|
// create dir
|
||||||
|
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", "control")
|
||||||
|
err := os.MkdirAll(logFileBasePath, 0777)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// open log file
|
||||||
|
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.stack.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
|
||||||
|
errorFile := initializeLogFile(logFilePath, "control/portmaster-control", updates.NewFile("", info.Version(), false))
|
||||||
|
if errorFile == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// write error and close
|
||||||
|
pprof.Lookup("goroutine").WriteTo(errorFile, 1)
|
||||||
|
errorFile.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func runAndLogControlError(wrappedFunc func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
|
||||||
|
return func(cmd *cobra.Command, args []string) error {
|
||||||
|
err := wrappedFunc(cmd, args)
|
||||||
|
if err != nil {
|
||||||
|
logControlError(err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,31 +2,26 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/safing/portbase/info"
|
"github.com/safing/portbase/info"
|
||||||
"github.com/safing/portbase/log"
|
portlog "github.com/safing/portbase/log"
|
||||||
"github.com/safing/portmaster/updates"
|
"github.com/safing/portmaster/updates"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
logPrefix = "[control]"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
updateStoragePath string
|
databaseRootDir *string
|
||||||
databaseRootDir *string
|
|
||||||
|
|
||||||
rootCmd = &cobra.Command{
|
rootCmd = &cobra.Command{
|
||||||
Use: "portmaster-control",
|
Use: "portmaster-control",
|
||||||
Short: "contoller for all portmaster components",
|
Short: "contoller for all portmaster components",
|
||||||
PersistentPreRunE: initPmCtl,
|
PersistentPreRunE: cmdSetup,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cmd.Help()
|
return cmd.Help()
|
||||||
},
|
},
|
||||||
|
@ -47,20 +42,27 @@ func init() {
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// check if we are running in a console (try to attach to parent console if available)
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
runningInConsole, err = attachToParentConsole()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to attach to parent console: %s\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// set meta info
|
// set meta info
|
||||||
info.Set("Portmaster Control", "0.2.5", "AGPLv3", true)
|
info.Set("Portmaster Control", "0.2.5", "AGPLv3", true)
|
||||||
|
|
||||||
|
// check if we are running in a console (try to attach to parent console if available)
|
||||||
|
runningInConsole, err = attachToParentConsole()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to attach to parent console: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set up logging
|
||||||
|
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
|
||||||
|
log.SetPrefix("[control] ")
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
// check if meta info is ok
|
// check if meta info is ok
|
||||||
err = info.CheckVersion()
|
err = info.CheckVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s compile error: please compile using the provided build script\n", logPrefix)
|
log.Println("compile error: please compile using the provided build script")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,12 +73,12 @@ func main() {
|
||||||
|
|
||||||
// warn about CTRL-C on windows
|
// warn about CTRL-C on windows
|
||||||
if runningInConsole && runtime.GOOS == "windows" {
|
if runningInConsole && runtime.GOOS == "windows" {
|
||||||
fmt.Printf("%s WARNING: portmaster-control is marked as a GUI application in order to get rid of the console window.\n", logPrefix)
|
log.Println("WARNING: portmaster-control is marked as a GUI application in order to get rid of the console window.")
|
||||||
fmt.Printf("%s WARNING: CTRL-C will immediately kill without clean shutdown.\n", logPrefix)
|
log.Println("WARNING: CTRL-C will immediately kill without clean shutdown.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// not using portbase logger
|
// not using portbase logger
|
||||||
log.SetLogLevel(log.CriticalLevel)
|
portlog.SetLogLevel(portlog.CriticalLevel)
|
||||||
|
|
||||||
// for debugging
|
// for debugging
|
||||||
// log.Start()
|
// log.Start()
|
||||||
|
@ -107,22 +109,32 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// for debugging windows service (no stdout/err)
|
||||||
|
// go func() {
|
||||||
|
// time.Sleep(10 * time.Second)
|
||||||
|
// // initiateShutdown(nil)
|
||||||
|
// // logControlStack()
|
||||||
|
// }()
|
||||||
|
|
||||||
// wait for signals
|
// wait for signals
|
||||||
for sig := range signalCh {
|
for sig := range signalCh {
|
||||||
if childIsRunning.IsSet() {
|
if childIsRunning.IsSet() {
|
||||||
fmt.Printf("%s got %s signal (ignoring), waiting for child to exit...\n", logPrefix, sig)
|
log.Printf("got %s signal (ignoring), waiting for child to exit...\n", sig)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("%s got %s signal, exiting... (not executing anything)\n", logPrefix, sig)
|
log.Printf("got %s signal, exiting... (not executing anything)\n", sig)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initPmCtl(cmd *cobra.Command, args []string) (err error) {
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
||||||
|
// check for database root path
|
||||||
// transform from db base path to updates path
|
// transform from db base path to updates path
|
||||||
if *databaseRootDir != "" {
|
if *databaseRootDir != "" {
|
||||||
|
// remove redundant escape characters and quotes
|
||||||
|
*databaseRootDir = strings.Trim(*databaseRootDir, `\"`)
|
||||||
|
// set updates path
|
||||||
updates.SetDatabaseRoot(*databaseRootDir)
|
updates.SetDatabaseRoot(*databaseRootDir)
|
||||||
updateStoragePath = filepath.Join(*databaseRootDir, "updates")
|
|
||||||
} else {
|
} else {
|
||||||
return errors.New("please supply the database directory using the --db flag")
|
return errors.New("please supply the database directory using the --db flag")
|
||||||
}
|
}
|
||||||
|
|
240
pmctl/run.go
240
pmctl/run.go
|
@ -3,19 +3,16 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/container"
|
|
||||||
"github.com/safing/portbase/database/record"
|
|
||||||
"github.com/safing/portbase/formats/dsd"
|
|
||||||
"github.com/safing/portmaster/updates"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tevino/abool"
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
@ -24,17 +21,16 @@ var (
|
||||||
runningInConsole bool
|
runningInConsole bool
|
||||||
onWindows = runtime.GOOS == "windows"
|
onWindows = runtime.GOOS == "windows"
|
||||||
|
|
||||||
childIsRunning = abool.NewBool(false)
|
childIsRunning = abool.NewBool(false)
|
||||||
shuttingDown = make(chan struct{})
|
|
||||||
shutdownInitiated = abool.NewBool(false)
|
|
||||||
programEnded = make(chan struct{})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Options for starting component
|
// Options for starting component
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Identifier string
|
Identifier string // component identifier
|
||||||
AllowDownload bool
|
ShortIdentifier string // populated automatically
|
||||||
AllowHidingWindow bool
|
AllowDownload bool // allow download of component if it is not yet available
|
||||||
|
AllowHidingWindow bool // allow hiding the window of the subprocess
|
||||||
|
NoOutput bool // do not use stdout/err if logging to file is available (did not fail to open log file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -53,7 +49,7 @@ var runCore = &cobra.Command{
|
||||||
Use: "core",
|
Use: "core",
|
||||||
Short: "Run the Portmaster Core",
|
Short: "Run the Portmaster Core",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return run(cmd, &Options{
|
return handleRun(cmd, &Options{
|
||||||
Identifier: "core/portmaster-core",
|
Identifier: "core/portmaster-core",
|
||||||
AllowDownload: true,
|
AllowDownload: true,
|
||||||
AllowHidingWindow: true,
|
AllowHidingWindow: true,
|
||||||
|
@ -69,7 +65,7 @@ var runApp = &cobra.Command{
|
||||||
Use: "app",
|
Use: "app",
|
||||||
Short: "Run the Portmaster App",
|
Short: "Run the Portmaster App",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return run(cmd, &Options{
|
return handleRun(cmd, &Options{
|
||||||
Identifier: "app/portmaster-app",
|
Identifier: "app/portmaster-app",
|
||||||
AllowDownload: false,
|
AllowDownload: false,
|
||||||
AllowHidingWindow: false,
|
AllowHidingWindow: false,
|
||||||
|
@ -85,7 +81,7 @@ var runNotifier = &cobra.Command{
|
||||||
Use: "notifier",
|
Use: "notifier",
|
||||||
Short: "Run the Portmaster Notifier",
|
Short: "Run the Portmaster Notifier",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return run(cmd, &Options{
|
return handleRun(cmd, &Options{
|
||||||
Identifier: "notifier/portmaster-notifier",
|
Identifier: "notifier/portmaster-notifier",
|
||||||
AllowDownload: false,
|
AllowDownload: false,
|
||||||
AllowHidingWindow: true,
|
AllowHidingWindow: true,
|
||||||
|
@ -97,8 +93,40 @@ var runNotifier = &cobra.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(cmd *cobra.Command, opts *Options) error {
|
func handleRun(cmd *cobra.Command, opts *Options) (err error) {
|
||||||
defer close(programEnded)
|
err = run(cmd, opts)
|
||||||
|
initiateShutdown(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(cmd *cobra.Command, opts *Options) (err error) {
|
||||||
|
|
||||||
|
// parse identifier
|
||||||
|
opts.ShortIdentifier = path.Dir(opts.Identifier)
|
||||||
|
|
||||||
|
// check for concurrent error (eg. service)
|
||||||
|
shutdownLock.Lock()
|
||||||
|
alreadyDead := shutdownInitiated
|
||||||
|
shutdownLock.Unlock()
|
||||||
|
if alreadyDead {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for duplicate instances
|
||||||
|
if opts.ShortIdentifier == "core" {
|
||||||
|
pid, _ := checkAndCreateInstanceLock(opts.ShortIdentifier)
|
||||||
|
if pid != 0 {
|
||||||
|
return fmt.Errorf("another instance of Portmaster Core is already running: PID %d", pid)
|
||||||
|
}
|
||||||
|
defer deleteInstanceLock(opts.ShortIdentifier)
|
||||||
|
}
|
||||||
|
|
||||||
|
// notify service after some time
|
||||||
|
go func() {
|
||||||
|
// assume that after 5 seconds service has finished starting
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
startupComplete <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
// get original arguments
|
// get original arguments
|
||||||
var args []string
|
var args []string
|
||||||
|
@ -112,31 +140,47 @@ func run(cmd *cobra.Command, opts *Options) error {
|
||||||
opts.Identifier += ".exe"
|
opts.Identifier += ".exe"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setup logging
|
||||||
|
// init log file
|
||||||
|
logFile := initControlLogFile()
|
||||||
|
if logFile != nil {
|
||||||
|
// don't close logFile, will be closed by system
|
||||||
|
if opts.NoOutput {
|
||||||
|
log.Println("disabling log output to stdout... bye!")
|
||||||
|
log.SetOutput(logFile)
|
||||||
|
} else {
|
||||||
|
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// run
|
// run
|
||||||
tries := 0
|
tries := 0
|
||||||
for {
|
for {
|
||||||
// normal execution
|
// normal execution
|
||||||
tryAgain, err := execute(opts, args)
|
tryAgain := false
|
||||||
if tryAgain && err == nil {
|
tryAgain, err = execute(opts, args)
|
||||||
continue
|
switch {
|
||||||
}
|
case tryAgain && err != nil:
|
||||||
|
// temporary? execution error
|
||||||
if err != nil {
|
log.Printf("execution of %s failed: %s\n", opts.Identifier, err)
|
||||||
tries++
|
tries++
|
||||||
fmt.Printf("%s execution of %s failed: %s\n", logPrefix, opts.Identifier, err)
|
if tries >= 5 {
|
||||||
}
|
log.Println("error seems to be permanent, giving up...")
|
||||||
if !tryAgain {
|
return err
|
||||||
break
|
}
|
||||||
}
|
log.Println("trying again...")
|
||||||
if tries >= 5 {
|
case tryAgain && err == nil:
|
||||||
fmt.Printf("%s error seems to be permanent, giving up...\n", logPrefix)
|
// upgrade
|
||||||
|
log.Println("restarting by request...")
|
||||||
|
case !tryAgain && err != nil:
|
||||||
|
// fatal error
|
||||||
return err
|
return err
|
||||||
|
case !tryAgain && err == nil:
|
||||||
|
// clean exit
|
||||||
|
log.Printf("%s completed successfully\n", opts.Identifier)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
fmt.Printf("%s trying again...\n", logPrefix)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s %s completed successfully\n", logPrefix, opts.Identifier)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func execute(opts *Options, args []string) (cont bool, err error) {
|
func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
|
@ -159,23 +203,23 @@ func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s starting %s %s\n", logPrefix, file.Path(), strings.Join(args, " "))
|
log.Printf("starting %s %s\n", file.Path(), strings.Join(args, " "))
|
||||||
|
|
||||||
// log files
|
// log files
|
||||||
var logFile, errorFile *os.File
|
var logFile, errorFile *os.File
|
||||||
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", strings.SplitN(opts.Identifier, "/", 2)[0])
|
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", opts.ShortIdentifier)
|
||||||
err = os.MkdirAll(logFileBasePath, 0777)
|
err = os.MkdirAll(logFileBasePath, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s failed to create log file folder %s: %s\n", logPrefix, logFileBasePath, err)
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
} else {
|
} else {
|
||||||
// open log file
|
// open log file
|
||||||
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.log", time.Now().Format("2006-02-01-15-04-05")))
|
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
|
||||||
logFile = initializeLogFile(logFilePath, opts.Identifier, file)
|
logFile = initializeLogFile(logFilePath, opts.Identifier, file)
|
||||||
if logFile != nil {
|
if logFile != nil {
|
||||||
defer finalizeLogFile(logFile, logFilePath)
|
defer finalizeLogFile(logFile, logFilePath)
|
||||||
}
|
}
|
||||||
// open error log file
|
// open error log file
|
||||||
errorFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.error.log", time.Now().Format("2006-02-01-15-04-05")))
|
errorFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.error.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
|
||||||
errorFile = initializeLogFile(errorFilePath, opts.Identifier, file)
|
errorFile = initializeLogFile(errorFilePath, opts.Identifier, file)
|
||||||
if errorFile != nil {
|
if errorFile != nil {
|
||||||
defer finalizeLogFile(errorFile, errorFilePath)
|
defer finalizeLogFile(errorFile, errorFilePath)
|
||||||
|
@ -232,10 +276,14 @@ func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
if logFile == nil {
|
if logFile == nil {
|
||||||
_, logFileError = io.Copy(os.Stdout, stdout)
|
_, logFileError = io.Copy(os.Stdout, stdout)
|
||||||
} else {
|
} else {
|
||||||
_, logFileError = io.Copy(io.MultiWriter(os.Stdout, logFile), stdout)
|
if opts.NoOutput {
|
||||||
|
_, logFileError = io.Copy(logFile, stdout)
|
||||||
|
} else {
|
||||||
|
_, logFileError = io.Copy(io.MultiWriter(os.Stdout, logFile), stdout)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if logFileError != nil {
|
if logFileError != nil {
|
||||||
fmt.Printf("%s failed write logs: %s\n", logPrefix, logFileError)
|
log.Printf("failed write logs: %s\n", logFileError)
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
@ -244,23 +292,28 @@ func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
if logFile == nil {
|
if logFile == nil {
|
||||||
_, errorFileError = io.Copy(os.Stderr, stderr)
|
_, errorFileError = io.Copy(os.Stderr, stderr)
|
||||||
} else {
|
} else {
|
||||||
_, errorFileError = io.Copy(io.MultiWriter(os.Stderr, errorFile), stderr)
|
if opts.NoOutput {
|
||||||
|
_, errorFileError = io.Copy(errorFile, stderr)
|
||||||
|
} else {
|
||||||
|
_, errorFileError = io.Copy(io.MultiWriter(os.Stderr, errorFile), stderr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if errorFileError != nil {
|
if errorFileError != nil {
|
||||||
fmt.Printf("%s failed write error logs: %s\n", logPrefix, errorFileError)
|
log.Printf("failed write error logs: %s\n", errorFileError)
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
// give some time to finish log file writing
|
|
||||||
defer func() {
|
|
||||||
wg.Wait()
|
|
||||||
childIsRunning.UnSet()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// wait for completion
|
// wait for completion
|
||||||
finished := make(chan error)
|
finished := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
|
// wait for output writers to complete
|
||||||
|
wg.Wait()
|
||||||
|
// wait for process to return
|
||||||
finished <- exc.Wait()
|
finished <- exc.Wait()
|
||||||
|
// update status
|
||||||
|
childIsRunning.UnSet()
|
||||||
|
// notify manager
|
||||||
close(finished)
|
close(finished)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -276,16 +329,24 @@ func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
err = exc.Process.Signal(os.Interrupt)
|
err = exc.Process.Signal(os.Interrupt)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s failed to signal %s to shutdown: %s\n", logPrefix, opts.Identifier, err)
|
log.Printf("failed to signal %s to shutdown: %s\n", opts.Identifier, err)
|
||||||
err = exc.Process.Kill()
|
err = exc.Process.Kill()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s failed to kill %s: %s\n", logPrefix, opts.Identifier, err)
|
return false, fmt.Errorf("failed to kill %s: %s", opts.Identifier, err)
|
||||||
} else {
|
|
||||||
fmt.Printf("%s killed %s\n", logPrefix, opts.Identifier)
|
|
||||||
}
|
}
|
||||||
|
return false, fmt.Errorf("killed %s", opts.Identifier)
|
||||||
}
|
}
|
||||||
// wait until shut down
|
// wait until shut down
|
||||||
<-finished
|
select {
|
||||||
|
case <-finished:
|
||||||
|
case <-time.After(11 * time.Second): // portmaster core prints stack if not able to shutdown in 10 seconds
|
||||||
|
// kill
|
||||||
|
err = exc.Process.Kill()
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to kill %s: %s", opts.Identifier, err)
|
||||||
|
}
|
||||||
|
return false, fmt.Errorf("killed %s", opts.Identifier)
|
||||||
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
case err := <-finished:
|
case err := <-finished:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -300,7 +361,7 @@ func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
return true, fmt.Errorf("error during execution: %s", err)
|
return true, fmt.Errorf("error during execution: %s", err)
|
||||||
case 2357427: // Leet Speak for "restart"
|
case 2357427: // Leet Speak for "restart"
|
||||||
// restart request
|
// restart request
|
||||||
fmt.Printf("%s restarting %s\n", logPrefix, opts.Identifier)
|
log.Printf("restarting %s\n", opts.Identifier)
|
||||||
return true, nil
|
return true, nil
|
||||||
default:
|
default:
|
||||||
return true, fmt.Errorf("unexpected error during execution: %s", err)
|
return true, fmt.Errorf("unexpected error during execution: %s", err)
|
||||||
|
@ -314,74 +375,3 @@ func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initiateShutdown() {
|
|
||||||
if shutdownInitiated.SetToIf(false, true) {
|
|
||||||
close(shuttingDown)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func initializeLogFile(logFilePath string, identifier string, updateFile *updates.File) *os.File {
|
|
||||||
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0644)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%s failed to create log file %s: %s\n", logPrefix, logFilePath, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// create header, so that the portmaster can view log files as a database
|
|
||||||
meta := record.Meta{}
|
|
||||||
meta.Update()
|
|
||||||
meta.SetAbsoluteExpiry(time.Now().Add(720 * time.Hour).Unix()) // one month
|
|
||||||
|
|
||||||
// manually marshal
|
|
||||||
// version
|
|
||||||
c := container.New([]byte{1})
|
|
||||||
// meta
|
|
||||||
metaSection, err := dsd.Dump(meta, dsd.JSON)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%s failed to serialize header for log file %s: %s\n", logPrefix, logFilePath, err)
|
|
||||||
finalizeLogFile(logFile, logFilePath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
c.AppendAsBlock(metaSection)
|
|
||||||
// log file data type (string) and newline for better manual viewing
|
|
||||||
c.Append([]byte("S\n"))
|
|
||||||
c.Append([]byte(fmt.Sprintf("executing %s version %s on %s %s\n", identifier, updateFile.Version(), runtime.GOOS, runtime.GOARCH)))
|
|
||||||
|
|
||||||
_, err = logFile.Write(c.CompileData())
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%s failed to write header for log file %s: %s\n", logPrefix, logFilePath, err)
|
|
||||||
finalizeLogFile(logFile, logFilePath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return logFile
|
|
||||||
}
|
|
||||||
|
|
||||||
func finalizeLogFile(logFile *os.File, logFilePath string) {
|
|
||||||
err := logFile.Close()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%s failed to close log file %s: %s\n", logPrefix, logFilePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//keep := true
|
|
||||||
// check file size
|
|
||||||
stat, err := os.Stat(logFilePath)
|
|
||||||
if err == nil {
|
|
||||||
// delete again if file is smaller than
|
|
||||||
if stat.Size() < 200 { // header + info is about 150 bytes
|
|
||||||
// keep = false
|
|
||||||
err := os.Remove(logFilePath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%s failed to delete empty log file %s: %s\n", logPrefix, logFilePath, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//if !keep {
|
|
||||||
// err := os.Remove(logFilePath)
|
|
||||||
// if err != nil {
|
|
||||||
// fmt.Printf("%s failed to delete empty log file %s: %s\n", logPrefix, logFilePath, err)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
31
pmctl/service.go
Normal file
31
pmctl/service.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
startupComplete = make(chan struct{}) // signal that the start procedure completed (is never closed, just signaled once)
|
||||||
|
shuttingDown = make(chan struct{}) // signal that we are shutting down (will be closed, may not be closed directly, use initiateShutdown)
|
||||||
|
shutdownInitiated = false // not to be used directly
|
||||||
|
shutdownError error // may not be read or written to directly
|
||||||
|
shutdownLock sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func initiateShutdown(err error) {
|
||||||
|
shutdownLock.Lock()
|
||||||
|
defer shutdownLock.Unlock()
|
||||||
|
|
||||||
|
if !shutdownInitiated {
|
||||||
|
shutdownInitiated = true
|
||||||
|
shutdownError = err
|
||||||
|
close(shuttingDown)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getShutdownError() error {
|
||||||
|
shutdownLock.Lock()
|
||||||
|
defer shutdownLock.Unlock()
|
||||||
|
|
||||||
|
return shutdownError
|
||||||
|
}
|
|
@ -7,6 +7,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -19,25 +21,23 @@ var (
|
||||||
runCoreService = &cobra.Command{
|
runCoreService = &cobra.Command{
|
||||||
Use: "core-service",
|
Use: "core-service",
|
||||||
Short: "Run the Portmaster Core as a Windows Service",
|
Short: "Run the Portmaster Core as a Windows Service",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: runAndLogControlError(func(cmd *cobra.Command, args []string) error {
|
||||||
return runService(cmd, &Options{
|
return runService(cmd, &Options{
|
||||||
Identifier: "core/portmaster-core",
|
Identifier: "core/portmaster-core",
|
||||||
AllowDownload: true,
|
AllowDownload: true,
|
||||||
AllowHidingWindow: true,
|
AllowHidingWindow: false,
|
||||||
|
NoOutput: true,
|
||||||
})
|
})
|
||||||
},
|
}),
|
||||||
FParseErrWhitelist: cobra.FParseErrWhitelist{
|
FParseErrWhitelist: cobra.FParseErrWhitelist{
|
||||||
// UnknownFlags will ignore unknown flags errors and continue parsing rest of the flags
|
// UnknownFlags will ignore unknown flags errors and continue parsing rest of the flags
|
||||||
UnknownFlags: true,
|
UnknownFlags: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// helpers for execution
|
// wait groups
|
||||||
runError chan error
|
runWg sync.WaitGroup
|
||||||
runWrapper func() error
|
finishWg sync.WaitGroup
|
||||||
|
|
||||||
// eventlog
|
|
||||||
eventlogger *eventlog.Log
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -52,102 +52,87 @@ func (ws *windowsService) Execute(args []string, changeRequests <-chan svc.Chang
|
||||||
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
|
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
|
||||||
changes <- svc.Status{State: svc.StartPending}
|
changes <- svc.Status{State: svc.StartPending}
|
||||||
|
|
||||||
// start logic
|
service:
|
||||||
var runError chan error
|
|
||||||
go func() {
|
|
||||||
runError <- runWrapper()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// poll for start completion
|
|
||||||
started := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
|
||||||
if childIsRunning.IsSet() {
|
|
||||||
close(started)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// wait for start
|
|
||||||
select {
|
|
||||||
case err := <-runError:
|
|
||||||
// TODO: log error to windows
|
|
||||||
fmt.Printf("%s start error: %s", logPrefix, err)
|
|
||||||
eventlogger.Error(4, fmt.Sprintf("failed to start Portmaster Core: %s", err))
|
|
||||||
changes <- svc.Status{State: svc.Stopped}
|
|
||||||
return false, 1
|
|
||||||
case <-started:
|
|
||||||
// give some more time for enabling packet interception
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
|
||||||
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
||||||
fmt.Printf("%s startup complete, entered service running state\n", logPrefix)
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for change requests
|
|
||||||
serviceLoop:
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-startupComplete:
|
||||||
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
||||||
case <-shuttingDown:
|
case <-shuttingDown:
|
||||||
break serviceLoop
|
changes <- svc.Status{State: svc.StopPending}
|
||||||
|
break service
|
||||||
case c := <-changeRequests:
|
case c := <-changeRequests:
|
||||||
switch c.Cmd {
|
switch c.Cmd {
|
||||||
case svc.Interrogate:
|
case svc.Interrogate:
|
||||||
changes <- c.CurrentStatus
|
changes <- c.CurrentStatus
|
||||||
case svc.Stop, svc.Shutdown:
|
case svc.Stop, svc.Shutdown:
|
||||||
initiateShutdown()
|
initiateShutdown(nil)
|
||||||
break serviceLoop
|
|
||||||
default:
|
default:
|
||||||
fmt.Printf("%s unexpected control request: #%d\n", logPrefix, c)
|
log.Printf("unexpected control request: #%d\n", c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// signal that we are shutting down
|
// define return values
|
||||||
changes <- svc.Status{State: svc.StopPending}
|
if getShutdownError() != nil {
|
||||||
// wait for program to exit
|
ssec = true // this error is specific to this service (ie. custom)
|
||||||
<-programEnded
|
errno = 1 // generic error, check logs / windows events
|
||||||
// signal shutdown complete
|
}
|
||||||
|
|
||||||
|
// wait until everything else is finished
|
||||||
|
finishWg.Wait()
|
||||||
|
// send stopped status
|
||||||
changes <- svc.Status{State: svc.Stopped}
|
changes <- svc.Status{State: svc.Stopped}
|
||||||
|
// wait a little for the status to reach Windows
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func runService(cmd *cobra.Command, opts *Options) error {
|
func runService(cmd *cobra.Command, opts *Options) error {
|
||||||
// set run wrapper
|
|
||||||
runWrapper = func() error {
|
|
||||||
return run(cmd, opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if we are running interactively
|
// check if we are running interactively
|
||||||
isDebug, err := svc.IsAnInteractiveSession()
|
isDebug, err := svc.IsAnInteractiveSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not determine if running interactively: %s", err)
|
return fmt.Errorf("could not determine if running interactively: %s", err)
|
||||||
}
|
}
|
||||||
|
// select service run type
|
||||||
|
svcRun := svc.Run
|
||||||
|
if isDebug {
|
||||||
|
log.Printf("WARNING: running interactively, switching to debug execution (no real service).\n")
|
||||||
|
svcRun = debug.Run
|
||||||
|
}
|
||||||
|
|
||||||
// open eventlog
|
// open eventlog
|
||||||
// TODO: do something useful with eventlog
|
|
||||||
elog, err := eventlog.Open(serviceName)
|
elog, err := eventlog.Open(serviceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open eventlog: %s", err)
|
return fmt.Errorf("failed to open eventlog: %s", err)
|
||||||
}
|
}
|
||||||
defer elog.Close()
|
defer elog.Close()
|
||||||
eventlogger = elog
|
|
||||||
elog.Info(1, fmt.Sprintf("starting %s service", serviceName))
|
|
||||||
|
|
||||||
// select run method bas
|
runWg.Add(2)
|
||||||
run := svc.Run
|
finishWg.Add(1)
|
||||||
if isDebug {
|
|
||||||
fmt.Printf("%s WARNING: running interactively, switching to debug execution (no real service).\n", logPrefix)
|
// run service client
|
||||||
run = debug.Run
|
go func() {
|
||||||
}
|
sErr := svcRun(serviceName, &windowsService{})
|
||||||
// run
|
initiateShutdown(sErr)
|
||||||
err = run(serviceName, &windowsService{})
|
runWg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
|
// run service
|
||||||
|
go func() {
|
||||||
|
// run slightly delayed
|
||||||
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
handleRun(cmd, opts)
|
||||||
|
finishWg.Done()
|
||||||
|
runWg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
|
runWg.Wait()
|
||||||
|
|
||||||
|
err = getShutdownError()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
elog.Error(3, fmt.Sprintf("%s service failed: %v", serviceName, err))
|
log.Printf("%s service experienced an error: %s\n", serviceName, err)
|
||||||
return fmt.Errorf("failed to start service: %s", err)
|
elog.Error(1, fmt.Sprintf("%s experienced an error: %s", serviceName, err))
|
||||||
}
|
}
|
||||||
elog.Info(2, fmt.Sprintf("%s service stopped", serviceName))
|
|
||||||
|
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -63,8 +64,6 @@ var showNotifier = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func show(cmd *cobra.Command, opts *Options) error {
|
func show(cmd *cobra.Command, opts *Options) error {
|
||||||
defer close(programEnded)
|
|
||||||
|
|
||||||
// get original arguments
|
// get original arguments
|
||||||
var args []string
|
var args []string
|
||||||
if len(os.Args) < 4 {
|
if len(os.Args) < 4 {
|
||||||
|
@ -82,8 +81,8 @@ func show(cmd *cobra.Command, opts *Options) error {
|
||||||
return fmt.Errorf("could not get component: %s", err)
|
return fmt.Errorf("could not get component: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s command:\n", logPrefix)
|
log.Println("command:")
|
||||||
fmt.Printf("%s %s\n", file.Path(), strings.Join(args, " "))
|
log.Printf("%s %s\n", file.Path(), strings.Join(args, " "))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue