1
0

Only short flags left. version() and printWatchedProcesses() funcs are removed and its body was moved in-place.

This commit is contained in:
Alexander Andreev 2022-10-10 00:18:17 +04:00
parent f16e747205
commit 4cf95a18c0
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

91
main.go
View File

@ -9,72 +9,62 @@ import (
"syscall" "syscall"
) )
var configPath *string = flag.String("c", "config.conf", "path to configuration file")
var showVersion *bool = flag.Bool("v", false, "show version")
var listProcesses *bool = flag.Bool("l", false, "list watched processes")
var addProcess *string = flag.String("a", "", "add process to list")
var removeProcess *string = flag.String("r", "", "remove process from list")
func main() { func main() {
var oConfigPath string
var oShowVersion bool
var oListProcesses bool
var oAddProcess string
var oRemoveProcess string
log.SetFlags(0) log.SetFlags(0)
flag.StringVar(&oConfigPath, "config", "config.conf", "path to configuration file")
flag.StringVar(&oConfigPath, "c", "config.conf", "path to configuration file (shorthand)")
flag.BoolVar(&oShowVersion, "version", false, "show version")
flag.BoolVar(&oShowVersion, "v", false, "show version (shorthand)")
flag.BoolVar(&oListProcesses, "list", false, "list watched processes")
flag.BoolVar(&oListProcesses, "l", false, "list watched processes (shorthand)")
flag.StringVar(&oAddProcess, "add", "", "add process to list")
flag.StringVar(&oAddProcess, "a", "", "add process to list (shorthand)")
flag.StringVar(&oRemoveProcess, "remove", "", "remove process from list")
flag.StringVar(&oRemoveProcess, "r", "", "remove process from list (shorthand)")
flag.Parse() flag.Parse()
if oShowVersion { if *showVersion {
version() fmt.Println("httpprocprobed ver. 2.0.0")
fmt.Println("Copyright (c) 2021,2022 Alexander \"Arav\" Andreev <me@arav.top>")
fmt.Println("This program is licensed under terms of MIT+NIGGER license a copy")
fmt.Println("of wich is located in /usr/share/licenses/httpprocprobed/LICENSE.")
os.Exit(0) os.Exit(0)
} }
conf, err := LoadConfiguration(oConfigPath) conf, err := LoadConfiguration(*configPath)
if err != nil { if err != nil {
log.Fatalf("Cannot load configuration file: %s\n", err) log.Fatalf("[ERR] Cannot load configuration file: %s\n", err)
} }
if oListProcesses { if *listProcesses {
printWatchedProcesses(&conf.Processes) for _, v := range conf.Processes {
fmt.Printf("%s, ", v)
}
fmt.Println()
os.Exit(0) os.Exit(0)
} }
if oAddProcess != "" { if *addProcess != "" {
err := conf.AddProcess(oAddProcess, oConfigPath) err := conf.AddProcess(*addProcess, *configPath)
if err != nil { if err != nil {
log.Fatalf("Cannot add process: %s\n", err) log.Fatalf("[ERR] Cannot add process: %s\n", err)
} }
} }
if oRemoveProcess != "" { if *removeProcess != "" {
err := conf.RemoveProcess(oRemoveProcess, oConfigPath) err := conf.RemoveProcess(*removeProcess, *configPath)
if err != nil { if err != nil {
log.Fatalf("Cannot remove process: %s\n", err) log.Fatalf("[ERR] Cannot remove process: %s\n", err)
} }
} }
// If we modified a list then let's look for a running program and // If we modified a list then let's look for a running program and
// send SIGHUP to it to reload a list. Here we assume that there // send SIGHUP to it to reload a list. Here we assume that there
// is only one process running, so we just filter our PID. // is only one process running, so we just filter our PID.
if oAddProcess != "" || oRemoveProcess != "" { if *addProcess != "" || *removeProcess != "" {
if err := conf.StoreConfiguration(oConfigPath); err != nil { if err := conf.StoreConfiguration(*configPath); err != nil {
log.Fatalf("Cannot write configuration. Error: %s\n", err) log.Fatalf("[ERR] Cannot write configuration. Error: %s\n", err)
} }
pids, _ := GetProcessPIDs("httpprocwatchd") pids, _ := GetProcessPIDs("httpprocprobed")
if pids != nil && len(pids) > 1 { if len(pids) > 1 {
var trgt_pid int var trgt_pid int
if pids[0] == os.Getpid() { if pids[0] == os.Getpid() {
trgt_pid = pids[1] trgt_pid = pids[1]
@ -95,19 +85,18 @@ func main() {
syssignal := make(chan os.Signal, 1) syssignal := make(chan os.Signal, 1)
signal.Notify(syssignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) signal.Notify(syssignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
log.Printf("httpprocwatchd is running on \"%s\".", conf.ListenAddress) log.Printf("httpprocprobed is running on \"%s\".", conf.ListenAddress)
for { for {
switch <-syssignal { switch <-syssignal {
case os.Interrupt: case os.Interrupt:
fallthrough fallthrough
case syscall.SIGINT | syscall.SIGTERM: case syscall.SIGINT | syscall.SIGTERM:
log.Println("Shutting down... ")
ShutdownHTTPServer(srv) ShutdownHTTPServer(srv)
log.Println("Server shutted down.") log.Println("Server shutted down.")
os.Exit(0) os.Exit(0)
case syscall.SIGHUP: case syscall.SIGHUP:
newconf, err := LoadConfiguration(oConfigPath) newconf, err := LoadConfiguration(*configPath)
if err != nil { if err != nil {
log.Fatalf("Failed to reload a list of processes from configuration: %s\n", err) log.Fatalf("Failed to reload a list of processes from configuration: %s\n", err)
} }
@ -116,19 +105,3 @@ func main() {
} }
} }
} }
// printWatchedProcesses prints a list of processes being watched.
func printWatchedProcesses(processes *[]string) {
for _, v := range *processes {
fmt.Printf("%s, ", v)
}
fmt.Println()
}
// version prints information about program.
func version() {
fmt.Println("httpprocwatchd ver. 1.3.2")
fmt.Println("Copyright (c) 2021,2022 Alexander \"Arav\" Andreev <me@arav.top>")
fmt.Println("This program is licensed under terms of MIT+NIGGER license a copy")
fmt.Println("of wich is located in /usr/share/licenses/httpprocwatchd/LICENSE.")
}