Switch to syslog when running in the background
This commit is contained in:
parent
17f0eb1339
commit
9bab220a1b
17
daemonize.go
17
daemonize.go
@ -2,10 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"log/syslog"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/rfjakob/gocryptfs/cryptfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The child sends us USR1 if the mount was successful
|
// The child sends us USR1 if the mount was successful
|
||||||
@ -22,8 +26,7 @@ func exitOnUsr1() {
|
|||||||
func forkChild() {
|
func forkChild() {
|
||||||
go exitOnUsr1()
|
go exitOnUsr1()
|
||||||
name := os.Args[0]
|
name := os.Args[0]
|
||||||
notifyArg := fmt.Sprintf("-notifypid=%d", os.Getpid())
|
newArgs := []string{"-f", fmt.Sprintf("-notifypid=%d", os.Getpid())}
|
||||||
newArgs := []string{"-f", notifyArg}
|
|
||||||
newArgs = append(newArgs, os.Args[1:]...)
|
newArgs = append(newArgs, os.Args[1:]...)
|
||||||
c := exec.Command(name, newArgs...)
|
c := exec.Command(name, newArgs...)
|
||||||
c.Stdout = os.Stdout
|
c.Stdout = os.Stdout
|
||||||
@ -47,3 +50,13 @@ func forkChild() {
|
|||||||
// The child exited with 0 - let's do the same.
|
// The child exited with 0 - let's do the same.
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Switch one Logger to syslog
|
||||||
|
func switchToSyslog(l *log.Logger, p syslog.Priority) {
|
||||||
|
w, err := syslog.New(p, PROGRAM_NAME)
|
||||||
|
if err != nil {
|
||||||
|
cryptfs.Warn.Printf("Cannot switch 0x%02x to syslog: %v", p, err)
|
||||||
|
} else {
|
||||||
|
l.SetOutput(w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11
main.go
11
main.go
@ -5,6 +5,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log/syslog"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -276,9 +277,17 @@ func main() {
|
|||||||
cryptfs.Debug.Printf("cli args: %v", args)
|
cryptfs.Debug.Printf("cli args: %v", args)
|
||||||
srv := pathfsFrontend(masterkey, args, confFile)
|
srv := pathfsFrontend(masterkey, args, confFile)
|
||||||
cryptfs.Info.Println(colorGreen + "Filesystem mounted and ready." + colorReset)
|
cryptfs.Info.Println(colorGreen + "Filesystem mounted and ready." + colorReset)
|
||||||
// We are ready - send USR1 signal to our parent
|
// We are ready - send USR1 signal to our parent and switch to syslog
|
||||||
if args.notifypid > 0 {
|
if args.notifypid > 0 {
|
||||||
sendUsr1(args.notifypid)
|
sendUsr1(args.notifypid)
|
||||||
|
|
||||||
|
if !args.quiet {
|
||||||
|
switchToSyslog(cryptfs.Info, syslog.LOG_USER|syslog.LOG_INFO)
|
||||||
|
}
|
||||||
|
if args.debug {
|
||||||
|
switchToSyslog(cryptfs.Debug, syslog.LOG_USER|syslog.LOG_DEBUG)
|
||||||
|
}
|
||||||
|
switchToSyslog(cryptfs.Warn, syslog.LOG_USER|syslog.LOG_WARNING)
|
||||||
}
|
}
|
||||||
// Wait for SIGINT in the background and unmount ourselves if we get it.
|
// Wait for SIGINT in the background and unmount ourselves if we get it.
|
||||||
// This prevents a dangling "Transport endpoint is not connected" mountpoint.
|
// This prevents a dangling "Transport endpoint is not connected" mountpoint.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user