mirror of
https://github.com/safing/portmaster
synced 2025-09-04 19:49:15 +00:00
Fix startup procedure, flags
This commit is contained in:
parent
d70edcf184
commit
27bb5fbeb0
5 changed files with 62 additions and 63 deletions
|
@ -88,7 +88,7 @@ func getServiceExecCommand(exePath string) []string {
|
||||||
"run",
|
"run",
|
||||||
"core-service",
|
"core-service",
|
||||||
"--db",
|
"--db",
|
||||||
windows.EscapeArg(*databaseRootDir),
|
windows.EscapeArg(databaseRootDir),
|
||||||
"--input-signals",
|
"--input-signals",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkAndCreateInstanceLock(name string) (pid int32, err error) {
|
func checkAndCreateInstanceLock(name string) (pid int32, err error) {
|
||||||
lockFilePath := filepath.Join(*databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
|
lockFilePath := filepath.Join(databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
|
||||||
|
|
||||||
// read current pid file
|
// read current pid file
|
||||||
data, err := ioutil.ReadFile(lockFilePath)
|
data, err := ioutil.ReadFile(lockFilePath)
|
||||||
|
@ -32,8 +32,6 @@ func checkAndCreateInstanceLock(name string) (pid int32, err error) {
|
||||||
return 0, createInstanceLock(lockFilePath)
|
return 0, createInstanceLock(lockFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("===== checking if PID %d exists\n", int32(parsedPid))
|
|
||||||
|
|
||||||
// check if process exists
|
// check if process exists
|
||||||
p, err := processInfo.NewProcess(int32(parsedPid))
|
p, err := processInfo.NewProcess(int32(parsedPid))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -52,7 +50,7 @@ func checkAndCreateInstanceLock(name string) (pid int32, err error) {
|
||||||
|
|
||||||
func createInstanceLock(lockFilePath string) error {
|
func createInstanceLock(lockFilePath string) error {
|
||||||
// create database dir
|
// create database dir
|
||||||
err := os.MkdirAll(*databaseRootDir, 0777)
|
err := os.MkdirAll(databaseRootDir, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to create base folder: %s\n", err)
|
log.Printf("failed to create base folder: %s\n", err)
|
||||||
}
|
}
|
||||||
|
@ -67,6 +65,6 @@ func createInstanceLock(lockFilePath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteInstanceLock(name string) error {
|
func deleteInstanceLock(name string) error {
|
||||||
lockFilePath := filepath.Join(*databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
|
lockFilePath := filepath.Join(databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
|
||||||
return os.Remove(lockFilePath)
|
return os.Remove(lockFilePath)
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ func finalizeLogFile(logFile *os.File, logFilePath string) {
|
||||||
|
|
||||||
func initControlLogFile() *os.File {
|
func initControlLogFile() *os.File {
|
||||||
// create logging dir
|
// create logging dir
|
||||||
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", "control")
|
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", "control")
|
||||||
err := os.MkdirAll(logFileBasePath, 0777)
|
err := os.MkdirAll(logFileBasePath, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
|
@ -94,7 +94,7 @@ func logControlError(cErr error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create dir
|
// create dir
|
||||||
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", "control")
|
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", "control")
|
||||||
err := os.MkdirAll(logFileBasePath, 0777)
|
err := os.MkdirAll(logFileBasePath, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
|
@ -114,7 +114,7 @@ func logControlError(cErr error) {
|
||||||
|
|
||||||
func logControlStack() {
|
func logControlStack() {
|
||||||
// create dir
|
// create dir
|
||||||
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", "control")
|
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", "control")
|
||||||
err := os.MkdirAll(logFileBasePath, 0777)
|
err := os.MkdirAll(logFileBasePath, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
@ -16,13 +17,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
databaseRootDir *string
|
databaseRootDir string
|
||||||
|
showShortVersion bool
|
||||||
|
showFullVersion bool
|
||||||
|
|
||||||
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: cmdSetup,
|
PersistentPreRunE: cmdSetup,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if showShortVersion {
|
||||||
|
fmt.Println(info.Version())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if showFullVersion {
|
||||||
|
fmt.Println(info.FullVersion())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return cmd.Help()
|
return cmd.Help()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -32,54 +43,15 @@ func init() {
|
||||||
// Let cobra ignore if we are running as "GUI" or not
|
// Let cobra ignore if we are running as "GUI" or not
|
||||||
cobra.MousetrapHelpText = ""
|
cobra.MousetrapHelpText = ""
|
||||||
|
|
||||||
databaseRootDir = rootCmd.PersistentFlags().String("db", "", "set database directory")
|
rootCmd.PersistentFlags().StringVar(&databaseRootDir, "db", "", "set database directory")
|
||||||
err := rootCmd.MarkPersistentFlagRequired("db")
|
rootCmd.Flags().BoolVar(&showFullVersion, "version", false, "print version")
|
||||||
if err != nil {
|
rootCmd.Flags().BoolVar(&showShortVersion, "ver", false, "print version number only")
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
|
||||||
|
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
||||||
|
|
||||||
// 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
|
|
||||||
err = info.CheckVersion()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("compile error: please compile using the provided build script")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// react to version flag
|
|
||||||
if info.PrintVersion() {
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// warn about CTRL-C on windows
|
|
||||||
if runningInConsole && runtime.GOOS == "windows" {
|
|
||||||
log.Println("WARNING: portmaster-control is marked as a GUI application in order to get rid of the console window.")
|
|
||||||
log.Println("WARNING: CTRL-C will immediately kill without clean shutdown.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// not using portbase logger
|
|
||||||
portlog.SetLogLevel(portlog.CriticalLevel)
|
|
||||||
|
|
||||||
// for debugging
|
// for debugging
|
||||||
// log.Start()
|
// log.Start()
|
||||||
// log.SetLogLevel(log.TraceLevel)
|
// log.SetLogLevel(log.TraceLevel)
|
||||||
|
@ -103,7 +75,7 @@ func main() {
|
||||||
|
|
||||||
// start root command
|
// start root command
|
||||||
go func() {
|
go func() {
|
||||||
if err = rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -128,16 +100,45 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
||||||
// check for database root path
|
// check if we are running in a console (try to attach to parent console if available)
|
||||||
// transform from db base path to updates path
|
runningInConsole, err = attachToParentConsole()
|
||||||
if *databaseRootDir != "" {
|
if err != nil {
|
||||||
|
log.Printf("failed to attach to parent console: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if meta info is ok
|
||||||
|
err = info.CheckVersion()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("compile error: please compile using the provided build script")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set up logging
|
||||||
|
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
|
||||||
|
log.SetPrefix("[control] ")
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
|
// not using portbase logger
|
||||||
|
portlog.SetLogLevel(portlog.CriticalLevel)
|
||||||
|
|
||||||
|
if !showShortVersion && !showFullVersion {
|
||||||
|
// set database root
|
||||||
|
if databaseRootDir != "" {
|
||||||
// remove redundant escape characters and quotes
|
// remove redundant escape characters and quotes
|
||||||
*databaseRootDir = strings.Trim(*databaseRootDir, `\"`)
|
databaseRootDir = strings.Trim(databaseRootDir, `\"`)
|
||||||
// set updates path
|
// set updates path
|
||||||
updates.SetDatabaseRoot(*databaseRootDir)
|
updates.SetDatabaseRoot(databaseRootDir)
|
||||||
} 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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// warn about CTRL-C on windows
|
||||||
|
if runningInConsole && runtime.GOOS == "windows" {
|
||||||
|
log.Println("WARNING: portmaster-control is marked as a GUI application in order to get rid of the console window.")
|
||||||
|
log.Println("WARNING: CTRL-C will immediately kill without clean shutdown.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -207,7 +207,7 @@ func execute(opts *Options, args []string) (cont bool, err error) {
|
||||||
|
|
||||||
// log files
|
// log files
|
||||||
var logFile, errorFile *os.File
|
var logFile, errorFile *os.File
|
||||||
logFileBasePath := filepath.Join(*databaseRootDir, "logs", "fstree", opts.ShortIdentifier)
|
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", opts.ShortIdentifier)
|
||||||
err = os.MkdirAll(logFileBasePath, 0777)
|
err = os.MkdirAll(logFileBasePath, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue