Skip to content

Commit 12516b5

Browse files
committed
Do not daemonize on Windows
1 parent d3d4dac commit 12516b5

File tree

3 files changed

+67
-54
lines changed

3 files changed

+67
-54
lines changed

main.go

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

main_nowindows.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"strings"
2626
"sync"
2727
"syscall"
28+
2829
"github.com/kardianos/osext"
30+
daemon "github.com/sevlyar/go-daemon"
2931

3032
"github.com/yandex-cloud/geesefs/api/common"
3133
"github.com/yandex-cloud/geesefs/internal"
@@ -52,13 +54,15 @@ func kill(pid int, s os.Signal) (err error) {
5254
return
5355
}
5456

55-
type ParentNotifier struct {
57+
const canDaemonize = true
58+
59+
type Daemonizer struct {
5660
result os.Signal
5761
wg sync.WaitGroup
5862
}
5963

60-
func NewParentNotifier() *ParentNotifier {
61-
p := &ParentNotifier{}
64+
func NewDaemonizer() *Daemonizer {
65+
p := &Daemonizer{}
6266

6367
signalChan := make(chan os.Signal, 1)
6468
signal.Notify(signalChan, syscall.SIGUSR1, syscall.SIGUSR2)
@@ -72,18 +76,46 @@ func NewParentNotifier() *ParentNotifier {
7276
return p
7377
}
7478

75-
func (p *ParentNotifier) Cancel() {
79+
func (p *Daemonizer) Daemonize(logFile string) error {
80+
messageArg0()
81+
82+
ctx := new(daemon.Context)
83+
if logFile == "stderr" || logFile == "/dev/stderr" {
84+
ctx.LogFileName = "/dev/stderr"
85+
}
86+
child, err := ctx.Reborn()
87+
88+
if err != nil {
89+
panic(fmt.Sprintf("unable to daemonize: %v", err))
90+
}
91+
92+
if child != nil {
93+
// attempt to wait for child to notify parent
94+
if p.Wait() {
95+
os.Exit(0)
96+
} else {
97+
return syscall.EINVAL
98+
}
99+
} else {
100+
p.Cancel()
101+
defer ctx.Release()
102+
}
103+
104+
return nil
105+
}
106+
107+
func (p *Daemonizer) Cancel() {
76108
// kill our own waiting goroutine
77109
kill(os.Getpid(), syscall.SIGUSR1)
78110
p.wg.Wait()
79111
}
80112

81-
func (p *ParentNotifier) Wait() bool {
113+
func (p *Daemonizer) Wait() bool {
82114
p.wg.Wait()
83115
return p.result == syscall.SIGUSR1
84116
}
85117

86-
func (p *ParentNotifier) Notify(success bool) {
118+
func (p *Daemonizer) NotifySuccess(success bool) {
87119
sig := syscall.SIGUSR1
88120
if !success {
89121
sig = syscall.SIGUSR2

main_windows.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,27 @@ func isSigUsr1(s os.Signal) bool {
2929
return false
3030
}
3131

32-
type ParentNotifier struct {
32+
const canDaemonize = false
33+
34+
type Daemonizer struct {
35+
}
36+
37+
func NewDaemonizer() *Daemonizer {
38+
return &Daemonizer{}
3339
}
3440

35-
func NewParentNotifier() *ParentNotifier {
36-
return &ParentNotifier{}
41+
func (p *Daemonizer) Daemonize(logFile string) error {
42+
return nil
3743
}
3844

39-
func (p *ParentNotifier) Cancel() {
45+
func (p *Daemonizer) Cancel() {
4046
}
4147

42-
func (p *ParentNotifier) Wait() bool {
48+
func (p *Daemonizer) Wait() bool {
4349
return true
4450
}
4551

46-
func (p *ParentNotifier) Notify(success bool) {
52+
func (p *Daemonizer) NotifySuccess(success bool) {
4753
}
4854

4955
// Mount the file system based on the supplied arguments, returning a

0 commit comments

Comments
 (0)