forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
271 lines (230 loc) · 6.95 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package main
import (
"fmt"
"os"
"os/signal"
"runtime"
"golang.org/x/net/context"
"github.com/keybase/client/go/client"
"github.com/keybase/client/go/install"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
keybase1 "github.com/keybase/client/go/protocol"
"github.com/keybase/client/go/service"
rpc "github.com/keybase/go-framed-msgpack-rpc"
)
// Keep this around to simplify things
var G = libkb.G
var cmd libcmdline.Command
type Canceler interface {
Cancel() error
}
type Stopper interface {
Stop(exitcode keybase1.ExitCode)
}
func main() {
g := G
g.Init()
go HandleSignals()
err := mainInner(g)
e2 := g.Shutdown()
if err == nil {
err = e2
}
if err != nil {
g.Log.Error(err.Error())
}
if g.ExitCode != keybase1.ExitCode_OK {
os.Exit(int(g.ExitCode))
}
}
func warnNonProd(log logger.Logger, e *libkb.Env) {
mode := e.GetRunMode()
if mode != libkb.ProductionRunMode {
log.Warning("Running in %s mode", mode)
}
}
func checkSystemUser(log logger.Logger) {
if isAdminUser, match, _ := libkb.IsSystemAdminUser(); isAdminUser {
log.Errorf("Oops, you are trying to run as an admin user (%s). This isn't supported.", match)
os.Exit(int(keybase1.ExitCode_NOTOK))
}
}
func mainInner(g *libkb.GlobalContext) error {
cl := libcmdline.NewCommandLine(true, client.GetExtraFlags())
cl.AddCommands(client.GetCommands(cl, g))
cl.AddCommands(service.GetCommands(cl, g))
cl.AddHelpTopics(client.GetHelpTopics())
var err error
cmd, err = cl.Parse(os.Args)
if err != nil {
err = fmt.Errorf("Error parsing command line arguments: %s\n", err)
return err
}
if cmd == nil {
return nil
}
checkSystemUser(g.Log)
if !cl.IsService() {
client.InitUI()
}
if err = g.ConfigureCommand(cl, cmd); err != nil {
return err
}
g.StartupMessage()
warnNonProd(g.Log, g.Env)
if err = configureProcesses(g, cl, &cmd); err != nil {
return err
}
return cmd.Run()
}
// AutoFork? Standalone? ClientServer? Brew service? This function deals with the
// various run configurations that we can run in.
func configureProcesses(g *libkb.GlobalContext, cl *libcmdline.CommandLine, cmd *libcmdline.Command) (err error) {
g.Log.Debug("+ configureProcesses")
defer func() {
g.Log.Debug("- configureProcesses -> %v", err)
}()
// On Linux, the service configures its own autostart file. Otherwise, no
// need to configure if we're a service.
if cl.IsService() {
g.Log.Debug("| in configureProcesses, is service")
if runtime.GOOS == "linux" {
g.Log.Debug("| calling AutoInstall")
_, err := install.AutoInstall(g, "", false)
if err != nil {
return err
}
}
return nil
}
// Start the server on the other end, possibly.
// There are two cases in which we do this: (1) we want
// a local loopback server in standalone mode; (2) we
// need to "autofork" it. Do at most one of these
// operations.
if g.Env.GetStandalone() {
if cl.IsNoStandalone() {
err = fmt.Errorf("Can't run command in standalone mode")
return err
}
err := service.NewService(g, false /* isDaemon */).StartLoopbackServer()
if err != nil {
if pflerr, ok := err.(libkb.PIDFileLockError); ok {
err = fmt.Errorf("Can't run in standalone mode with a service running (see %q)",
pflerr.Filename)
return err
}
}
return err
}
// After this point, we need to provide a remote logging story if necessary
// If this command specifically asks not to be forked, then we are done in this
// function. This sort of thing is true for the `ctl` commands and also the `version`
// command.
fc := cl.GetForkCmd()
if fc == libcmdline.NoFork {
return configureLogging(g, cl)
}
// If this command warrants an autofork, do it now.
var newProc bool
if fc == libcmdline.ForceFork || g.Env.GetAutoFork() {
newProc, err = client.AutoForkServer(g, cl)
if err != nil {
return err
}
} else if libkb.IsBrewBuild {
// If we're running in Brew mode, we might need to install ourselves as a persistent
// service for future invocations of the command.
newProc, err = install.AutoInstall(g, "", false)
if err != nil {
return err
}
}
g.Log.Debug("| After forks; newProc=%v", newProc)
if err = configureLogging(g, cl); err != nil {
return err
}
// If we have created a new proc, then there's no need to keep going to the
// final step, which is to check for a version clashes.
if newProc {
return nil
}
// Finally, we'll restart the service if we see that it's out of date.
if err = client.FixVersionClash(g, cl); err != nil {
return err
}
return nil
}
func configureLogging(g *libkb.GlobalContext, cl *libcmdline.CommandLine) error {
g.Log.Debug("+ configureLogging")
defer func() {
g.Log.Debug("- configureLogging")
}()
// Whether or not we autoforked, we're now running in client-server
// mode (as opposed to standalone). Register a global LogUI so that
// calls to G.Log() in the daemon can be copied to us. This is
// something of a hack on the daemon side.
if !g.Env.GetDoLogForward() || cl.GetLogForward() == libcmdline.LogForwardNone {
g.Log.Debug("Disabling log forwarding")
return nil
}
// TODO This triggers a connection to the RPC server before cmd.Run() is
// called, so the command has no way to deal with errors on its own.
// This should probably be moved into RegisterProtocols?
// Also rpc.RegisterProtocolsWithContext seems to automatically add the
// LogUIProtocol?
return registerGlobalLogUI(g)
}
func registerGlobalLogUI(g *libkb.GlobalContext) error {
protocols := []rpc.Protocol{client.NewLogUIProtocol()}
if err := client.RegisterProtocols(protocols); err != nil {
return err
}
// Send our current debugging state, so that the server can avoid
// sending us verbose logs when we don't want to read them.
logLevel := keybase1.LogLevel_INFO
if g.Env.GetDebug() {
logLevel = keybase1.LogLevel_DEBUG
}
ctlClient, err := client.GetCtlClient(g)
if err != nil {
return err
}
g.Log.Debug("Setting remote log level: %v", logLevel)
arg := keybase1.SetLogLevelArg{Level: logLevel}
ctlClient.SetLogLevel(context.TODO(), arg)
return nil
}
func HandleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
for {
s := <-c
if s != nil {
G.Log.Debug("trapped signal %v", s)
// if the current command has a Stop function, then call it.
// It will do its own stopping of the process and calling
// shutdown
if stop, ok := cmd.(Stopper); ok {
G.Log.Debug("Stopping command cleanly via stopper")
stop.Stop(keybase1.ExitCode_OK)
return
}
// if the current command has a Cancel function, then call it:
if canc, ok := cmd.(Canceler); ok {
G.Log.Debug("canceling running command")
if err := canc.Cancel(); err != nil {
G.Log.Warning("error canceling command: %s", err)
}
}
G.Log.Debug("calling shutdown")
G.Shutdown()
G.Log.Error("interrupted")
os.Exit(3)
}
}
}