-
Notifications
You must be signed in to change notification settings - Fork 525
/
main.go
248 lines (207 loc) · 5.44 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2015 - 2017 Ka-Hing Cheung
// Copyright 2015 - 2017 Google Inc. All Rights Reserved.
//
// 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 (
goofys "github.com/kahing/goofys/api"
. "github.com/kahing/goofys/api/common"
. "github.com/kahing/goofys/internal"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"context"
"github.com/jacobsa/fuse"
"github.com/kardianos/osext"
"github.com/urfave/cli"
daemon "github.com/sevlyar/go-daemon"
)
var log = GetLogger("main")
func registerSIGINTHandler(fs *Goofys, flags *FlagStorage) {
// Register for SIGINT.
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
// Start a goroutine that will unmount when the signal is received.
go func() {
for {
s := <-signalChan
if s == syscall.SIGUSR1 {
log.Infof("Received %v", s)
fs.SigUsr1()
continue
}
if len(flags.Cache) == 0 {
log.Infof("Received %v, attempting to unmount...", s)
err := TryUnmount(flags.MountPoint)
if err != nil {
log.Errorf("Failed to unmount in response to %v: %v", s, err)
} else {
log.Printf("Successfully unmounted %v in response to %v",
flags.MountPoint, s)
return
}
} else {
log.Infof("Received %v", s)
// wait for catfs to die and cleanup
}
}
}()
}
var waitedForSignal os.Signal
func waitForSignal(wg *sync.WaitGroup) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGUSR1, syscall.SIGUSR2)
wg.Add(1)
go func() {
waitedForSignal = <-signalChan
wg.Done()
}()
}
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
}
// Mount the file system based on the supplied arguments, returning a
// fuse.MountedFileSystem that can be joined to wait for unmounting.
func mount(
ctx context.Context,
bucketName string,
flags *FlagStorage) (fs *Goofys, mfs *fuse.MountedFileSystem, err error) {
return goofys.Mount(ctx, bucketName, flags)
}
func massagePath() {
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 massageArg0() {
var err error
os.Args[0], err = osext.Executable()
if err != nil {
panic(fmt.Sprintf("Unable to discover current executable: %v", err))
}
}
var Version = "use `make build' to fill version hash correctly"
func main() {
VersionNumber = "0.24.0"
VersionHash = Version
massagePath()
app := NewApp()
var flags *FlagStorage
var child *os.Process
app.Action = func(c *cli.Context) (err error) {
// We should get two arguments exactly. Otherwise error out.
if len(c.Args()) != 2 {
fmt.Fprintf(
os.Stderr,
"Error: %s takes exactly two arguments.\n\n",
app.Name)
cli.ShowAppHelp(c)
os.Exit(1)
}
// Populate and parse flags.
bucketName := c.Args()[0]
flags = PopulateFlags(c)
if flags == nil {
cli.ShowAppHelp(c)
err = fmt.Errorf("invalid arguments")
return
}
defer func() {
time.Sleep(time.Second)
flags.Cleanup()
}()
if !flags.Foreground {
var wg sync.WaitGroup
waitForSignal(&wg)
massageArg0()
ctx := new(daemon.Context)
child, err = ctx.Reborn()
if err != nil {
panic(fmt.Sprintf("unable to daemonize: %v", err))
}
InitLoggers(!flags.Foreground && child == nil)
if child != nil {
// attempt to wait for child to notify parent
wg.Wait()
if waitedForSignal == syscall.SIGUSR1 {
return
} else {
return fuse.EINVAL
}
} else {
// kill our own waiting goroutine
kill(os.Getpid(), syscall.SIGUSR1)
wg.Wait()
defer ctx.Release()
}
} else {
InitLoggers(!flags.Foreground)
}
// Mount the file system.
var mfs *fuse.MountedFileSystem
var fs *Goofys
fs, mfs, err = mount(
context.Background(),
bucketName,
flags)
if err != nil {
if !flags.Foreground {
kill(os.Getppid(), syscall.SIGUSR2)
}
log.Fatalf("Mounting file system: %v", err)
// fatal also terminates itself
} else {
if !flags.Foreground {
kill(os.Getppid(), syscall.SIGUSR1)
}
log.Println("File system has been successfully mounted.")
// Let the user unmount with Ctrl-C
// (SIGINT). But if cache is on, catfs will
// receive the signal and we would detect that exiting
registerSIGINTHandler(fs, flags)
// Wait for the file system to be unmounted.
err = mfs.Join(context.Background())
if err != nil {
err = fmt.Errorf("MountedFileSystem.Join: %v", err)
return
}
log.Println("Successfully exiting.")
}
return
}
err := app.Run(MassageMountFlags(os.Args))
if err != nil {
if flags != nil && !flags.Foreground && child != nil {
log.Fatalln("Unable to mount file system, see syslog for details")
}
os.Exit(1)
}
}