forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.go
542 lines (484 loc) · 16.8 KB
/
signals.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
Copyright 2017 Gravitational, Inc.
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 service
import (
"context"
"encoding/json"
"net"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)
// printShutdownStatus prints running services until shut down
func (process *TeleportProcess) printShutdownStatus(ctx context.Context) {
t := time.NewTicker(5 * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
log.Infof("Waiting for services: %v to finish.", process.Supervisor.Services())
}
}
}
// WaitForSignals waits for system signals and processes them.
// Should not be called twice by the process.
func (process *TeleportProcess) WaitForSignals(ctx context.Context) error {
sigC := make(chan os.Signal, 1024)
signal.Notify(sigC,
syscall.SIGQUIT, // graceful shutdown
syscall.SIGTERM, // fast shutdown
syscall.SIGINT, // fast shutdown
syscall.SIGKILL, // fast shutdown
syscall.SIGUSR1, // log process diagnostic info
syscall.SIGUSR2, // initiate process restart procedure
syscall.SIGHUP, // graceful restart procedure
syscall.SIGCHLD, // collect child status
)
doneContext, cancel := context.WithCancel(ctx)
defer cancel()
serviceErrorsC := make(chan Event, 10)
process.WaitForEvent(ctx, ServiceExitedWithErrorEvent, serviceErrorsC)
// Block until a signal is received or handler got an error.
// Notice how this handler is serialized - it will only receive
// signals in sequence and will not run in parallel.
for {
select {
case signal := <-sigC:
switch signal {
case syscall.SIGQUIT:
go process.printShutdownStatus(doneContext)
process.Shutdown(ctx)
process.Infof("All services stopped, exiting.")
return nil
case syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT:
process.Infof("Got signal %q, exiting immediately.", signal)
process.Close()
return nil
case syscall.SIGUSR1:
// All programs placed diagnostics on the standard output.
// This had always caused trouble when the output was redirected into a file, but became intolerable
// when the output was sent to an unsuspecting process.
// Nevertheless, unwilling to violate the simplicity of the standard-input-standard-output model,
// people tolerated this state of affairs through v6. Shortly thereafter Dennis Ritchie cut the Gordian
// knot by introducing the standard error file.
// That was not quite enough. With pipelines diagnostics could come from any of several programs running simultaneously.
// Diagnostics needed to identify themselves.
// - Doug McIllroy, "A Research UNIX Reader: Annotated Excerpts from the Programmer’s Manual, 1971-1986"
process.Infof("Got signal %q, logging diagostic info to stderr.", signal)
writeDebugInfo(os.Stderr)
case syscall.SIGUSR2:
if !process.backendSupportsForks() {
process.Warningf("Process is using backend that does not support multiple processes, switch to another backend to use USR2.")
continue
}
log.Infof("Got signal %q, forking a new process.", signal)
if err := process.forkChild(); err != nil {
process.Warningf("Failed to fork: %v", err)
} else {
process.Infof("Successfully started new process.")
}
case syscall.SIGHUP:
if !process.backendSupportsForks() {
process.Warningf("Process is using backend that does not support multiple processes, switch to another backend to use HUP.")
continue
}
process.Infof("Got signal %q, performing graceful restart.", signal)
if err := process.forkChild(); err != nil {
process.Warningf("Failed to fork: %v", err)
continue
}
process.Infof("Successfully started new process, shutting down gracefully.")
go process.printShutdownStatus(doneContext)
process.Shutdown(ctx)
log.Infof("All services stopped, exiting.")
return nil
case syscall.SIGCHLD:
process.collectStatuses()
default:
process.Infof("Ignoring %q.", signal)
}
case <-process.ReloadContext().Done():
process.Infof("Exiting signal handler: process has started internal reload.")
return ErrTeleportReloading
case <-process.ExitContext().Done():
process.Infof("Someone else has closed context, exiting.")
return nil
case <-ctx.Done():
process.Close()
process.Wait()
process.Info("Got request to shutdown, context is closing")
return nil
case event := <-serviceErrorsC:
se, ok := event.Payload.(ServiceExit)
if !ok {
process.Warningf("Failed to decode service exit event, %T", event.Payload)
continue
}
if se.Service.IsCritical() {
process.Errorf("Critical service %v has exited with error %v, aborting.", se.Service, se.Error)
if err := process.Close(); err != nil {
process.Errorf("Error when shutting down teleport %v.", err)
}
return trace.Wrap(se.Error)
} else {
process.Warningf("Non-critical service %v has exited with error %v, continuing to operate.", se.Service, se.Error)
}
}
}
}
// ErrTeleportReloading is returned when signal waiter exits
// because the teleport process has initiaded shutdown
var ErrTeleportReloading = &trace.CompareFailedError{Message: "teleport process is reloading"}
// ErrTeleportExited means that teleport has exited
var ErrTeleportExited = &trace.CompareFailedError{Message: "teleport process has shutdown"}
func (process *TeleportProcess) writeToSignalPipe(signalPipe *os.File, message string) error {
messageSignalled, cancel := context.WithCancel(context.Background())
// Below the cancel is called second time, but it's ok.
// After the first call, subsequent calls to a CancelFunc do nothing.
defer cancel()
go func() {
_, err := signalPipe.Write([]byte(message))
if err != nil {
process.Debugf("Failed to write to pipe: %v.", trace.DebugReport(err))
return
}
cancel()
}()
select {
case <-time.After(signalPipeTimeout):
return trace.BadParameter("Failed to write to parent process pipe.")
case <-messageSignalled.Done():
process.Infof("Signalled success to parent process.")
}
return nil
}
// closeImportedDescriptors closes imported but unused file descriptors,
// what could happen if service has updated configuration
func (process *TeleportProcess) closeImportedDescriptors(prefix string) error {
process.Lock()
defer process.Unlock()
var errors []error
for i := range process.importedDescriptors {
d := process.importedDescriptors[i]
if strings.HasPrefix(d.Type, prefix) {
process.Infof("Closing imported but unused descriptor %v %v.", d.Type, d.Address)
errors = append(errors, d.File.Close())
}
}
return trace.NewAggregate(errors...)
}
// importOrCreateListener imports listener passed by the parent process (happens during live reload)
// or creates a new listener if there was no listener registered
func (process *TeleportProcess) importOrCreateListener(listenerType, address string) (net.Listener, error) {
l, err := process.importListener(listenerType, address)
if err == nil {
process.Infof("Using file descriptor %v %v passed by the parent process.", listenerType, address)
return l, nil
}
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
process.Infof("Service %v is creating new listener on %v.", listenerType, address)
return process.createListener(listenerType, address)
}
func (process *TeleportProcess) importSignalPipe() (*os.File, error) {
process.Lock()
defer process.Unlock()
for i := range process.importedDescriptors {
d := process.importedDescriptors[i]
if d.Type == signalPipeName {
process.importedDescriptors = append(process.importedDescriptors[:i], process.importedDescriptors[i+1:]...)
return d.File, nil
}
}
return nil, trace.NotFound("no file descriptor %v was found", signalPipeName)
}
// importListener imports listener passed by the parent process, if no listener is found
// returns NotFound, otherwise removes the file from the list
func (process *TeleportProcess) importListener(listenerType, address string) (net.Listener, error) {
process.Lock()
defer process.Unlock()
for i := range process.importedDescriptors {
d := process.importedDescriptors[i]
if d.Type == listenerType && d.Address == address {
l, err := d.ToListener()
if err != nil {
return nil, trace.Wrap(err)
}
process.importedDescriptors = append(process.importedDescriptors[:i], process.importedDescriptors[i+1:]...)
process.registeredListeners = append(process.registeredListeners, RegisteredListener{Type: listenerType, Address: address, Listener: l})
return l, nil
}
}
return nil, trace.NotFound("no file descriptor for type %v and address %v has been imported", listenerType, address)
}
// createListener creates listener and adds to a list of tracked listeners
func (process *TeleportProcess) createListener(listenerType, address string) (net.Listener, error) {
listener, err := net.Listen("tcp", address)
if err != nil {
return nil, trace.Wrap(err)
}
process.Lock()
defer process.Unlock()
r := RegisteredListener{Type: listenerType, Address: address, Listener: listener}
process.registeredListeners = append(process.registeredListeners, r)
return listener, nil
}
// ExportFileDescriptors exports file descriptors to be passed to child process
func (process *TeleportProcess) ExportFileDescriptors() ([]FileDescriptor, error) {
var out []FileDescriptor
process.Lock()
defer process.Unlock()
for _, r := range process.registeredListeners {
file, err := utils.GetListenerFile(r.Listener)
if err != nil {
return nil, trace.Wrap(err)
}
out = append(out, FileDescriptor{File: file, Type: r.Type, Address: r.Address})
}
return out, nil
}
// importFileDescriptors imports file descriptors from environment if there are any
func importFileDescriptors() ([]FileDescriptor, error) {
// These files may be passed in by the parent process
filesString := os.Getenv(teleportFilesEnvVar)
if filesString == "" {
return nil, nil
}
files, err := filesFromString(filesString)
if err != nil {
return nil, trace.BadParameter("child process has failed to read files, error %q", err)
}
if len(files) != 0 {
log.Infof("Child has been passed files: %v", files)
}
return files, nil
}
// RegisteredListener is a listener registered
// within teleport process, can be passed to child process
type RegisteredListener struct {
// Type is a listener type, e.g. auth:ssh
Type string
// Address is an address listener is serving on, e.g. 127.0.0.1:3025
Address string
// Listener is a file listener object
Listener net.Listener
}
// FileDescriptor is a file descriptor associated
// with a listener
type FileDescriptor struct {
// Type is a listener type, e.g. auth:ssh
Type string
// Address is an addresss of the listener, e.g. 127.0.0.1:3025
Address string
// File is a file descriptor associated with the listener
File *os.File
}
func (fd *FileDescriptor) ToListener() (net.Listener, error) {
listener, err := net.FileListener(fd.File)
if err != nil {
return nil, err
}
fd.File.Close()
return listener, nil
}
type fileDescriptor struct {
Address string `json:"addr"`
Type string `json:"type"`
FileFD int `json:"fd"`
FileName string `json:"fileName"`
}
// filesToString serializes file descriptors as well as accompanying information (like socket host and port)
func filesToString(files []FileDescriptor) (string, error) {
out := make([]fileDescriptor, len(files))
for i, f := range files {
out[i] = fileDescriptor{
// Once files will be passed to the child process and their FDs will change.
// The first three passed files are stdin, stdout and stderr, every next file will have the index + 3
// That's why we rearrange the FDs for child processes to get the correct file descriptors.
FileFD: i + 3,
FileName: f.File.Name(),
Address: f.Address,
Type: f.Type,
}
}
bytes, err := json.Marshal(out)
if err != nil {
return "", err
}
return string(bytes), nil
}
const teleportFilesEnvVar = "TELEPORT_OS_FILES"
func execPath() (string, error) {
name, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
if _, err = os.Stat(name); nil != err {
return "", err
}
return name, err
}
// filesFromString de-serializes the file descriptors and turns them in the os.Files
func filesFromString(in string) ([]FileDescriptor, error) {
var out []fileDescriptor
if err := json.Unmarshal([]byte(in), &out); err != nil {
return nil, err
}
files := make([]FileDescriptor, len(out))
for i, o := range out {
files[i] = FileDescriptor{
File: os.NewFile(uintptr(o.FileFD), o.FileName),
Address: o.Address,
Type: o.Type,
}
}
return files, nil
}
const (
signalPipeName = "teleport-signal-pipe"
// signalPipeTimeout is a time parent process is expecting
// the child process to initialize and write back,
// or child process is blocked on write to the pipe
signalPipeTimeout = 2 * time.Minute
)
func (process *TeleportProcess) forkChild() error {
readPipe, writePipe, err := os.Pipe()
if err != nil {
return trace.ConvertSystemError(err)
}
defer readPipe.Close()
defer writePipe.Close()
path, err := execPath()
if err != nil {
return trace.Wrap(err)
}
workingDir, err := os.Getwd()
if nil != err {
return err
}
log := log.WithFields(logrus.Fields{"path": path, "workingDir": workingDir})
log.Info("Forking child.")
listenerFiles, err := process.ExportFileDescriptors()
if err != nil {
return trace.Wrap(err)
}
listenerFiles = append(listenerFiles, FileDescriptor{
File: writePipe,
Type: signalPipeName,
Address: "127.0.0.1:0",
})
// These files will be passed to the child process
files := []*os.File{os.Stdin, os.Stdout, os.Stderr}
for _, f := range listenerFiles {
files = append(files, f.File)
}
// Serialize files to JSON string representation
vals, err := filesToString(listenerFiles)
if err != nil {
return trace.Wrap(err)
}
log.Infof("Passing %s to child", vals)
os.Setenv(teleportFilesEnvVar, vals)
p, err := os.StartProcess(path, os.Args, &os.ProcAttr{
Dir: workingDir,
Env: os.Environ(),
Files: files,
Sys: &syscall.SysProcAttr{},
})
if err != nil {
return trace.ConvertSystemError(err)
}
process.pushForkedPID(p.Pid)
log.WithFields(logrus.Fields{"pid": p.Pid}).Infof("Forked new child process.")
messageReceived, cancel := context.WithCancel(context.TODO())
defer cancel()
go func() {
data := make([]byte, 1024)
len, err := readPipe.Read(data)
if err != nil {
log.Debugf("Failed to read from pipe")
return
}
log.Infof("Received message from pid %v: %v", p.Pid, string(data[:len]))
cancel()
}()
select {
case <-time.After(signalPipeTimeout):
return trace.BadParameter("Failed waiting from process")
case <-messageReceived.Done():
log.WithFields(logrus.Fields{"pid": p.Pid}).Infof("Child process signals success.")
}
return nil
}
// collectStatuses attempts to collect exit statuses from
// forked teleport child processes.
// If forked teleport process exited with an error during graceful
// restart, parent process has to collect the child process status
// otherwise the child process will become a zombie process.
// Call Wait4(-1) is trying to collect status of any child
// leads to warnings in logs, because other parts of the program could
// have tried to collect the status of this process.
// Instead this logic tries to collect statuses of the processes
// forked during restart procedure.
func (process *TeleportProcess) collectStatuses() {
pids := process.getForkedPIDs()
if len(pids) == 0 {
return
}
for _, pid := range pids {
var wait syscall.WaitStatus
rpid, err := syscall.Wait4(pid, &wait, syscall.WNOHANG, nil)
if err != nil {
process.Errorf("Wait call failed: %v.", err)
continue
}
if rpid == pid {
process.popForkedPID(pid)
process.Warningf("Forked teleport process %v has exited with status: %v.", pid, wait.ExitStatus())
}
}
}
func (process *TeleportProcess) pushForkedPID(pid int) {
process.Lock()
defer process.Unlock()
process.forkedPIDs = append(process.forkedPIDs, pid)
}
func (process *TeleportProcess) popForkedPID(pid int) {
process.Lock()
defer process.Unlock()
for i, p := range process.forkedPIDs {
if p == pid {
process.forkedPIDs = append(process.forkedPIDs[:i], process.forkedPIDs[i+1:]...)
return
}
}
}
func (process *TeleportProcess) getForkedPIDs() []int {
process.Lock()
defer process.Unlock()
if len(process.forkedPIDs) == 0 {
return nil
}
out := make([]int, len(process.forkedPIDs))
copy(out, process.forkedPIDs)
return out
}