-
Notifications
You must be signed in to change notification settings - Fork 46
/
main_nowindows.go
165 lines (135 loc) · 3.32 KB
/
main_nowindows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// +build !windows
// Copyright 2015 - 2017 Ka-Hing Cheung
// Copyright 2021 Yandex LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/kardianos/osext"
daemon "github.com/sevlyar/go-daemon"
"github.com/yandex-cloud/geesefs/internal/cfg"
"github.com/yandex-cloud/geesefs/internal"
)
var signalsToHandle = []os.Signal{ os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1 }
func isSigUsr1(s os.Signal) bool {
return s == syscall.SIGUSR1
}
func kill(pid int, s os.Signal) (err error) {
p, err := os.FindProcess(pid)
if err != nil {
return err
}
defer p.Release()
err = p.Signal(s)
if err != nil {
return err
}
return
}
const canDaemonize = true
type Daemonizer struct {
result os.Signal
wg sync.WaitGroup
}
func NewDaemonizer() *Daemonizer {
p := &Daemonizer{}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGUSR1, syscall.SIGUSR2)
p.wg.Add(1)
go func() {
p.result = <-signalChan
p.wg.Done()
}()
return p
}
func (p *Daemonizer) Daemonize(logFile string) error {
messageArg0()
ctx := new(daemon.Context)
if logFile == "stderr" || logFile == "/dev/stderr" {
ctx.LogFileName = "/dev/stderr"
}
child, err := ctx.Reborn()
if err != nil {
panic(fmt.Sprintf("unable to daemonize: %v", err))
}
if child != nil {
// attempt to wait for child to notify parent
if p.Wait() {
os.Exit(0)
} else {
return syscall.EINVAL
}
} else {
p.Cancel()
defer ctx.Release()
}
return nil
}
func (p *Daemonizer) Cancel() {
// kill our own waiting goroutine
kill(os.Getpid(), syscall.SIGUSR1)
p.wg.Wait()
}
func (p *Daemonizer) Wait() bool {
p.wg.Wait()
return p.result == syscall.SIGUSR1
}
func (p *Daemonizer) NotifySuccess(success bool) {
sig := syscall.SIGUSR1
if !success {
sig = syscall.SIGUSR2
}
kill(os.Getppid(), sig)
}
// Mount the file system based on the supplied arguments, returning a
// MountedFS that can be joined to wait for unmounting.
func mount(
ctx context.Context,
bucketName string,
flags *cfg.FlagStorage) (fs *internal.Goofys, mfs internal.MountedFS, err error) {
if flags.ClusterMode {
return internal.MountCluster(ctx, bucketName, flags)
} else {
return internal.MountFuse(ctx, bucketName, flags)
}
}
func messagePath() {
for _, e := range os.Environ() {
if strings.HasPrefix(e, "PATH=") {
return
}
}
// mount -a seems to run goofys without PATH
// usually fusermount is in /bin
os.Setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
}
func messageArg0() {
var err error
os.Args[0], err = osext.Executable()
if err != nil {
panic(fmt.Sprintf("Unable to discover current executable: %v", err))
}
}
func setuid(uid int) error {
return syscall.Setuid(uid)
}
func setgid(gid int) error {
return syscall.Setgid(gid)
}