@@ -23,15 +23,12 @@ import (
2323 "os"
2424 "os/signal"
2525 "strings"
26- "syscall"
2726 "time"
2827
2928 "context"
3029
3130 "github.com/urfave/cli"
3231
33- daemon "github.com/sevlyar/go-daemon"
34-
3532 "net/http"
3633 _ "net/http/pprof"
3734)
@@ -99,47 +96,25 @@ func main() {
9996 flags .Cleanup ()
10097 }()
10198
102- var notifier * ParentNotifier
103- if ! flags . Foreground {
104- notifier = NewParentNotifier ()
105-
106- messageArg0 ()
107-
108- ctx := new (daemon. Context )
109- if flags . LogFile == "stderr" || flags . LogFile == "/dev/stderr" {
110- ctx . LogFileName = "/dev/stderr "
99+ var daemonizer * Daemonizer
100+ if ! canDaemonize {
101+ flags . Foreground = true
102+ }
103+ if flags . LogFile == "" {
104+ if flags . Foreground {
105+ flags . LogFile = "stderr"
106+ } else {
107+ flags . LogFile = "syslog "
111108 }
112- child , err = ctx .Reborn ()
113-
109+ }
110+ if ! flags .Foreground {
111+ daemonizer = NewDaemonizer ()
112+ err := daemonizer .Daemonize (flags .LogFile )
114113 if err != nil {
115- panic ( fmt . Sprintf ( "unable to daemonize: %v" , err ))
114+ return err
116115 }
117-
118- if flags .LogFile == "" {
119- if flags .Foreground || child != nil {
120- flags .LogFile = "stderr"
121- } else {
122- flags .LogFile = "syslog"
123- }
124- }
125-
126- InitLoggers (flags .LogFile )
127-
128- if child != nil {
129- // attempt to wait for child to notify parent
130- if notifier .Wait () {
131- return
132- } else {
133- return syscall .EINVAL
134- }
135- } else {
136- notifier .Cancel ()
137- defer ctx .Release ()
138- }
139-
140- } else {
141- InitLoggers (flags .LogFile )
142116 }
117+ InitLoggers (flags .LogFile )
143118
144119 pprof := flags .PProf
145120 if pprof == "" && os .Getenv ("PPROF" ) != "" {
@@ -163,13 +138,13 @@ func main() {
163138
164139 if err != nil {
165140 if ! flags .Foreground {
166- notifier . Notify (false )
141+ daemonizer . NotifySuccess (false )
167142 }
168143 log .Fatalf ("Mounting file system: %v" , err )
169144 // fatal also terminates itself
170145 } else {
171146 if ! flags .Foreground {
172- notifier . Notify (true )
147+ daemonizer . NotifySuccess (true )
173148 }
174149 log .Println ("File system has been successfully mounted." )
175150 // Let the user unmount with Ctrl-C (SIGINT)
0 commit comments