-
Notifications
You must be signed in to change notification settings - Fork 17
/
littleboss.go
1032 lines (922 loc) · 25.7 KB
/
littleboss.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018 David Crawshaw <[email protected]>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// Package littleboss creates self-supervising Go binaries.
//
// A self-supervising binary starts itself as a child process.
// The parent process becomes a supervisor that is responsible for
// monitoring, managing I/O, restarting, and reloading the child.
//
// Make a program use littleboss by modifying the main function:
//
// func main() {
// lb := littleboss.New("service-name")
// lb.Run(func(ctx context.Context) {
// // main goes here, exit when <-ctx.Done()
// })
// }
//
//
// Usage
//
// By default the supervisor is bypassed and the program executes directly.
// A flag, -littleboss, is added to the binary.
// It can be used to start a supervised binary and manage it:
//
// $ mybin & # binary runs directly, no child process
// $ mybin -littleboss=start & # supervisor is created
// $ mybin2 -littleboss=reload # child is replaced by new mybin2 process
// $ mybin -littleboss=stop # supervisor and child are shut down
//
//
// Configuration
//
// Supervisor options are baked into the binary.
// The Littleboss struct type contains fields that can be set before calling
// the Run method to configure the supervisor.
package littleboss
// TODO version both protocols
// TODO document the parent<->child pipe protocol
// TODO reload changing flags
// TODO instrument supervisor
// TODO bring child up before shutting down old binary
import (
"bufio"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net"
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
type Littleboss struct {
FlagSet *flag.FlagSet // used for passing flags to child, default flag.CommandLine
Logf func(format string, args ...interface{}) // defaults to stderr
SupervisorInit func() // executed by supervisor on Run
Persist bool // if program exits, restart it
FallbackOnFailure bool // if program exits badly, use previous version
LameduckTimeout time.Duration // time to wait before forcing exit
// Fields set once in New.
name string
cmdname string
username string
reload chan string
reloadDone chan error
// Fields fixed by Run.
modeFlagName string
mode *string
running bool
lnFlags map[string]*ListenerFlag
// Fields used by runChild and "status"/"stop"/"reload" handlers.
mu sync.Mutex
childProcess *os.Process
childPiper *piper
status status
stopSignal chan int // exitCode
}
const usage = `instruct the littleboss:
start: start and manage this process using service name %q
stop: signal the littleboss to shutdown the process
status: print statistics about the running littleboss
reload: restart the managed process using the executed binary
bypass: disable littleboss, run the program directly`
// A ListenerFlag is a flag whose value is used as a net.Listener or net.PacketConn.
//
// An empty flag value ("") means no listener is created.
// For a listener on a random port, use ":0".
type ListenerFlag struct {
lb *Littleboss
net string
val string
ln net.Listener
pc net.PacketConn
f *os.File // listener fd in children
}
func (lnf *ListenerFlag) String() string {
if lnf.lb != nil && lnf.lb.mode != nil && *lnf.lb.mode == "child" {
if i := strings.LastIndex(lnf.val, ":fd:"); i >= 0 {
return lnf.val[:i]
}
}
return lnf.val
}
func (lnf *ListenerFlag) Network() string { return lnf.net }
func (lnf *ListenerFlag) Listener() net.Listener { return lnf.ln }
func (lnf *ListenerFlag) PacketConn() net.PacketConn { return lnf.pc }
func (lnf *ListenerFlag) Set(value string) error {
lnf.val = value
return nil
}
func New(serviceName string) *Littleboss {
if !isValidServiceName(serviceName) {
panic(fmt.Sprintf("littleboss: invalid service name %q, must be [a-zA-Z0-9_-]", serviceName))
}
uid := os.Geteuid()
u, err := user.LookupId(strconv.Itoa(uid))
if err != nil {
panic(fmt.Sprintf("littleboss: cannot determine user name: %v", err))
}
lb := &Littleboss{
FlagSet: flag.CommandLine,
FallbackOnFailure: true,
LameduckTimeout: 2 * time.Second,
name: serviceName,
cmdname: os.Args[0],
username: u.Username,
reload: make(chan string, 1),
reloadDone: make(chan error),
lnFlags: make(map[string]*ListenerFlag),
}
lb.Logf = func(format string, args ...interface{}) {
fmt.Fprintf(lb.stderr(), "littleboss: %s\n", fmt.Sprintf(format, args...))
}
return lb
}
func (lb *Littleboss) Listener(flagName, network, value, usage string) *ListenerFlag {
if lb.running {
panic("littleboss: cannot create Listener flag after calling Run")
}
lnf := &ListenerFlag{
lb: lb,
val: value,
net: network,
}
lb.lnFlags[flagName] = lnf
lb.FlagSet.Var(lnf, flagName, usage)
return lnf
}
// Command gives littleboss a custom command mode flag name and value.
//
// By default littleboss creates a flag named -littleboss when
// Run is called, and sets its default value to "bypass".
// If the FlagSet is going to be processed by a
// non-standard flag package or the littleboss command mode
// is not passed as a flag at all, then this Command method can
// be used instead to avoid the flag creation.
//
// This makes it possible for a program to invoke flag parsing
// itself, after calling New, Command, any calls to Listener,
// but before the call to Run.
//
// For example:
//
// lb := littleboss.New("myservice")
// lb.Command("-mylb", pflag.String("mylb", "start", "lb command mode"))
// pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// pflag.Parse()
// lb.Run(...)
//
// NOTE: All the flags passed to the child process are
// extracted from Littleboss.FlagSet. Make sure any flags defined
// by external flag packages have their value in the FlagSet.
func (lb *Littleboss) Command(modeFlagName string, mode *string) {
lb.modeFlagName = modeFlagName
lb.mode = mode
}
// Run starts the little boss, with mainFn as the entry point to the service.
// Run does not return.
func (lb *Littleboss) Run(mainFn func(ctx context.Context)) {
if lb.running {
panic("littleboss: Run called multiple times")
}
if lb.mode == nil {
if lb.FlagSet.Parsed() {
panic(fmt.Sprintf("littleboss: flags parsed before Run, but Command method not invoked"))
}
lb.modeFlagName = "littleboss"
lb.mode = lb.FlagSet.String(lb.modeFlagName, "bypass", fmt.Sprintf(usage, lb.name))
}
if !lb.FlagSet.Parsed() {
lb.FlagSet.Parse(os.Args[1:])
}
if lb.Logf == nil {
lb.Logf = func(format string, args ...interface{}) {} // do nothing
}
switch *lb.mode {
case "stop":
lb.issueStop()
case "reload":
lb.issueReload()
case "status":
lb.issueStatus()
case "child":
lb.child(mainFn)
}
for name, lnf := range lb.lnFlags {
if lnf.val == "" {
continue
}
var ln net.Listener
var pc net.PacketConn
var err error
if strings.HasPrefix(lnf.val, "fd:") {
fd, err := strconv.Atoi(lnf.val[7:])
if err != nil {
fmt.Fprintf(lb.stderr(), "%s: -%s: invalid fd number: %v\n", lb.cmdname, name, err)
os.Exit(1)
}
f := os.NewFile(uintptr(fd), name)
if strings.HasPrefix(lnf.val, "fd:udp:") {
pc, err = net.FilePacketConn(f)
} else if strings.HasPrefix(lnf.val, "fd:tcp:") {
ln, err = net.FileListener(f)
} else {
fmt.Fprintf(lb.stderr(), "%s: -%s: unknown fd type: %s\n", lb.cmdname, name, lnf.val)
os.Exit(1)
}
} else if strings.HasPrefix(lnf.net, "tcp") {
ln, err = net.Listen(lnf.net, lnf.val)
} else {
pc, err = net.ListenPacket(lnf.net, lnf.val)
}
if err != nil {
fmt.Fprintf(lb.stderr(), "%s: -%s: %v\n", lb.cmdname, name, err)
os.Exit(1)
}
if *lb.mode == "start" {
type fileSource interface {
File() (*os.File, error)
}
if src, _ := ln.(fileSource); ln != nil {
f, err := src.File()
if err != nil {
lb.fatalf("could not get TCP listener fd: %v", err)
}
lnf.f = f
} else if src, _ := pc.(fileSource); pc != nil {
f, err := src.File()
if err != nil {
lb.fatalf("could not get packet conn fd: %v", err)
}
lnf.f = f
} else {
lb.fatalf("unsupported listener type: %T/%T", ln, pc)
}
}
lnf.ln = ln
lnf.pc = pc
}
lb.running = true
switch *lb.mode {
case "bypass":
// Littleboss is not in use, run the program as if we weren't here.
mainFn(context.Background())
os.Exit(0)
case "start":
lb.startBoss()
default:
fmt.Fprintf(lb.stderr(), "%s: unknown littleboss command: %q\n\n", lb.cmdname, *lb.mode)
lb.FlagSet.Usage()
os.Exit(2)
}
panic("unexpected exit")
}
func (lb *Littleboss) startBoss() {
socketdir := lb.socketdir()
os.Mkdir(socketdir, 0700)
os.Chown(socketdir, os.Geteuid(), os.Getegid())
os.Chmod(socketdir, 0700)
fi, err := os.Stat(socketdir)
if err != nil {
lb.fatalf("cannot stat socket dir %v", err)
}
if !fi.IsDir() {
lb.fatalf("socket directory %s is not a directory", socketdir)
}
if fi.Mode().Perm() != 0700 {
lb.fatalf("socket directory has bad permissions, want 0700 got %v", fi.Mode().Perm())
}
socketpath := filepath.Join(socketdir, lb.name+".socket")
addr, err := net.ResolveUnixAddr("unix", socketpath) // "unix" == SOCK_STREAM
if err != nil {
lb.fatalf("resolve %v", err)
}
ln, err := net.ListenUnix("unix", addr)
if err != nil {
lb.fatalf("listen %v", err)
}
if f, _ := ln.File(); f != nil {
f.Chmod(0700) // TODO test this, or remove this?
}
childPath, err := os.Executable()
if err != nil {
lb.fatalf("cannot find oneself: %v", err)
}
if lb.SupervisorInit != nil {
lb.SupervisorInit()
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigCh
fmt.Fprintf(lb.stderr(), "littleboss: signal received: %s\n", sig)
stopSignal := make(chan int)
lb.handleStopBegin(stopSignal)
var exitCode int
stopped := false
select {
case exitCode = <-stopSignal:
stopped = true
case <-time.After(100 * time.Millisecond):
fmt.Fprintf(lb.stderr(), "littleboss: lameduck mode, exiting in %v\n", lb.LameduckTimeout)
}
if !stopped {
exitCode = <-stopSignal
}
lb.exit(exitCode)
}()
lb.mu.Lock()
lb.status = status{
BossStart: time.Now(),
BossPid: os.Getpid(),
Listeners: make(map[string]string),
}
lb.mu.Unlock()
go lb.runChild(childPath)
for {
conn, err := ln.AcceptUnix()
if err != nil {
ln.Close()
lb.Logf("%v", err) // TODO remove?
lb.exit(1)
}
go lb.handler(conn)
}
}
// handler handles socket connections to a littleboss supervisor.
// These can be commands to stop, reload, or provide status.
func (lb *Littleboss) handler(conn *net.UnixConn) {
defer conn.Close()
r := json.NewDecoder(conn)
w := json.NewEncoder(conn)
for {
var req ipcReq
if err := r.Decode(&req); err != nil {
return
}
switch req.Cmd {
case "stop":
lb.handleStop(w)
case "reload":
lb.handleReload(w, req)
case "status":
lb.handleStatus(w)
default:
lb.Logf("unknown ipc command: %q", req.Cmd)
return
}
}
}
func (lb *Littleboss) handleStatus(w *json.Encoder) {
lb.Logf("status requested")
lb.mu.Lock()
res := ipcRes{
Type: "status",
Status: &lb.status,
}
w.Encode(res)
lb.mu.Unlock()
}
func (lb *Littleboss) handleReload(w *json.Encoder, req ipcReq) {
lb.Logf("reload requested")
lb.reload <- req.ChildPath
go lb.handleStopBegin(nil)
err := <-lb.reloadDone
errStr := ""
if err != nil {
errStr = err.Error()
}
res := ipcRes{
Type: "reloaded",
Error: errStr,
}
w.Encode(res)
}
func (lb *Littleboss) handleStop(w *json.Encoder) {
lb.Logf("stop requested")
stopSignal := make(chan int)
lb.handleStopBegin(stopSignal)
var exitCode int
stopped := false
select {
case exitCode = <-stopSignal:
stopped = true
case <-time.After(100 * time.Millisecond):
}
if !stopped {
w.Encode(ipcRes{Type: "stopping"})
exitCode = <-stopSignal
}
res := ipcRes{
Type: "stopped",
ExitCode: exitCode,
}
w.Encode(res)
lb.exit(exitCode)
}
func (lb *Littleboss) handleStopBegin(stopSignal chan int) {
lb.mu.Lock()
lb.stopSignal = stopSignal
process := lb.childProcess
childPiper := lb.childPiper
lb.mu.Unlock()
if process != nil {
childPiper.Write("lameduck")
done := make(chan struct{}, 2)
go func() {
if childPiper.Read() == "finished" {
done <- struct{}{}
}
}()
go func() {
process.Wait()
done <- struct{}{}
}()
go func() {
select {
case <-done:
case <-time.After(lb.LameduckTimeout):
lb.Logf("timeout, forcing process exit")
process.Kill()
}
}()
}
}
func (lb *Littleboss) runChild(childPath string) {
var prevChildPath string
var flags, prevFlags []string
var extraFiles, prevExtraFiles []*os.File
flags, extraFiles = lb.collectFlags()
loadPrev := func() bool {
if !lb.FallbackOnFailure {
return false
}
if prevChildPath == "" {
return false
}
childPath = prevChildPath
flags = prevFlags
extraFiles = prevExtraFiles
prevChildPath = ""
prevFlags = nil
prevExtraFiles = nil
return true
}
for {
bossPipeR, childPipeW, err := os.Pipe()
if err != nil {
lb.fatalf("cannot create pipe: %v", err)
}
childPipeR, bossPipeW, err := os.Pipe()
if err != nil {
lb.fatalf("cannot create pipe: %v", err)
}
cmd := exec.Command(childPath, flags...)
cmd.ExtraFiles = append([]*os.File{bossPipeR, bossPipeW}, extraFiles...)
cmd.Stdin = os.Stdin // TODO: Stdio fields in *Littleboss
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true, // we propagate SIGINT ourselves
}
err = cmd.Start()
if err != nil {
lb.Logf("%s failed to start: %v", childPath, err)
}
// These fds have bene duped by the forked process.
bossPipeR.Close()
bossPipeW.Close()
var exitCode int
if err == nil {
childPiper := newPiper(childPipeR, childPipeW)
lb.mu.Lock()
stopSignal := lb.stopSignal
lb.childProcess = cmd.Process
lb.childPiper = childPiper
lb.status.Path = childPath
lb.status.Args = flags
lb.status.ChildStart = time.Now()
lb.status.ChildPid = cmd.Process.Pid
for name := range lb.status.Listeners {
delete(lb.status.Listeners, name)
}
for name, lnf := range lb.lnFlags {
if lnf.ln != nil {
lb.status.Listeners[name] = lnf.ln.Addr().String()
} else if lnf.pc != nil {
lb.status.Listeners[name] = lnf.pc.LocalAddr().String()
}
}
lb.mu.Unlock()
if stopSignal != nil {
// Oops. A signal to stop came in while we
// were starting the process.
// Now it is our job to do the work of killing it.
cmd.Process.Kill()
} else {
if res := childPiper.Read(); res != "ready" {
err = fmt.Errorf("child not ready: %q (%v)", res, childPiper.Error())
} else {
childPiper.Write("go")
err = childPiper.Error()
}
}
}
failure := make(chan error, 1)
go func(err error) {
if err == nil {
// We pause for a moment before reporting successful
// reloads in case the client fails early into its
// main function (which is common).
select {
case err = <-failure:
case <-time.After(250 * time.Millisecond):
}
}
lb.mu.Lock()
lb.status.Reloads++
lb.mu.Unlock()
select {
case lb.reloadDone <- err:
default:
}
}(err)
if err == nil {
err = cmd.Wait()
}
if err != nil {
failure <- err
if exErr, _ := err.(*exec.ExitError); exErr != nil {
exitCode = exErr.Sys().(syscall.WaitStatus).ExitStatus()
lb.Logf("exit code %d", exitCode)
} else {
exitCode = 1
lb.Logf("exit: %v", err)
}
}
childPipeR.Close()
childPipeW.Close()
lb.mu.Lock()
lb.childProcess = nil
lb.childPiper = nil
stopSignal := lb.stopSignal
lb.stopSignal = nil
if err != nil {
lb.status.Failures++
}
lb.mu.Unlock()
if stopSignal != nil {
stopSignal <- exitCode
return
}
// Handle reloads.
select {
case newChildPath := <-lb.reload:
// Reload was requested, so child exiting was expected.
// The particular error code does not matter: move forward!
prevChildPath = childPath
prevFlags = flags
prevExtraFiles = extraFiles
childPath = newChildPath
flags, extraFiles = lb.collectFlags()
continue
default:
}
if err == nil {
// Reload was not requested and child exited cleanly.
if lb.Persist {
lb.Logf("%s exited, waiting one second and restarting", childPath)
time.Sleep(1 * time.Second)
continue
}
lb.Logf("%s exited, shutting down", childPath)
lb.exit(0)
}
// Reload was not requested and process exited badly.
oldChildPath := childPath
if loadPrev() {
lb.Logf("%s failed: %v, restarting previous binary", oldChildPath, err)
continue
} else {
if lb.Persist {
time.Sleep(1 * time.Second)
lb.Logf("%s failed, waiting one second and restarting", childPath)
continue
}
// Child failed, no old binary, no persistence, so exit the boss.
lb.fatalf("%v", err)
}
}
}
func (lb *Littleboss) collectFlags() (flags []string, extraFDs []*os.File) {
addLn := func(f *flag.Flag, lnf *ListenerFlag) {
net := lnf.net[:3]
// 0 stdin, 1 stdout, 2 stderr, 3 bossPipeR, 4 bossPipeW, 5+ listeners
flags = append(flags, fmt.Sprintf("-%s=%s:fd:%s:%d", f.Name, f.Value, net, 5+len(extraFDs)))
extraFDs = append(extraFDs, lnf.f)
}
flags = []string{"-" + lb.modeFlagName + "=child"}
visitedLns := make(map[string]bool)
lb.FlagSet.Visit(func(f *flag.Flag) {
if lnf := lb.lnFlags[f.Name]; lnf != nil {
visitedLns[f.Name] = true
if lnf.ln == nil && lnf.pc == nil {
// Listener is disabled. We pass the flag,
// as the value is non-default, but do not
// pass an FD.
flags = append(flags, "-"+f.Name+"=")
} else {
addLn(f, lnf)
}
} else if f.Name == lb.modeFlagName {
return // rewritten first
} else {
flags = append(flags, "-"+f.Name+"="+f.Value.String())
}
})
// Any unset listener flag with a non-empty default value needs
// to be handled, even though it was not enumerated by Visit above.
var extraLnFlags []string
for name := range lb.lnFlags {
if !visitedLns[name] {
extraLnFlags = append(extraLnFlags, name)
}
}
sort.Strings(extraLnFlags)
for _, name := range extraLnFlags {
lnf := lb.lnFlags[name]
if lnf.ln == nil && lnf.pc == nil {
continue
}
addLn(lb.FlagSet.Lookup(name), lnf)
}
return flags, extraFDs
}
type ipcReq struct {
Cmd string // "stop", "status", "reload"
ChildPath string `json:",omitempty"`
}
type ipcRes struct {
Type string // "stopping", "stopped", "status", "reloading", "reloaded"
Error string `json:",omitempty"`
ExitCode int `json:",omitempty"`
Status *status `json:",omitempty"`
}
type status struct {
BossStart time.Time
BossPid int
ChildStart time.Time
ChildPid int
Reloads int
Failures int
Path string
Args []string
Listeners map[string]string // name -> addr
}
// issueStop sends a "stop" instruction to a running supervisor.
func (lb *Littleboss) issueStop() {
socketpath := filepath.Join(lb.socketdir(), lb.name+".socket")
conn, err := net.Dial("unix", socketpath)
if err != nil {
lb.fatalf("cannot connect to %s: %v", lb.name, err)
}
r := json.NewDecoder(conn)
w := json.NewEncoder(conn)
if err := w.Encode(ipcReq{Cmd: "stop"}); err != nil {
lb.fatalf("ipc write error: %v", err)
}
for {
var res ipcRes
if err := r.Decode(&res); err != nil {
lb.fatalf("ipc read error: %v", err)
}
switch res.Type {
case "stopping":
fmt.Fprintf(lb.stderr(), "stopping %s\n", lb.name)
case "stopped":
fmt.Fprintf(lb.stderr(), "stopped %s exit code %d\n", lb.name, res.ExitCode)
os.Exit(res.ExitCode)
}
}
}
// issueReload sends a "reload" instruction to a running supervisor.
func (lb *Littleboss) issueReload() {
socketpath := filepath.Join(lb.socketdir(), lb.name+".socket")
conn, err := net.Dial("unix", socketpath)
if err != nil {
lb.fatalf("cannot connect to %s: %v", lb.name, err)
}
r := json.NewDecoder(conn)
w := json.NewEncoder(conn)
childPath, err := os.Executable()
if err != nil {
lb.fatalf("cannot find oneself: %v", err)
}
req := ipcReq{
Cmd: "reload",
ChildPath: childPath,
}
if err := w.Encode(req); err != nil {
lb.fatalf("ipc write error: %v", err)
}
for {
var res ipcRes
if err := r.Decode(&res); err != nil {
lb.fatalf("ipc read error: %v", err)
}
switch res.Type {
case "reloading":
fmt.Fprintf(lb.stderr(), "reloading %s\n", lb.name)
case "reloaded":
if res.Error == "" {
fmt.Fprintf(lb.stderr(), "reloaded %s\n", lb.name)
os.Exit(0)
} else {
fmt.Fprintf(lb.stderr(), "reload of %s failed: %s\n", lb.name, res.Error)
os.Exit(1)
}
}
}
}
// issueStatus sends a "status" instruction to a running supervisor.
func (lb *Littleboss) issueStatus() {
socketpath := filepath.Join(lb.socketdir(), lb.name+".socket")
conn, err := net.Dial("unix", socketpath)
if err != nil {
lb.fatalf("cannot connect to %s: %v", lb.name, err)
}
r := json.NewDecoder(conn)
w := json.NewEncoder(conn)
if err := w.Encode(ipcReq{Cmd: "status"}); err != nil {
lb.fatalf("ipc write error: %v", err)
}
var res ipcRes
if err := r.Decode(&res); err != nil {
lb.fatalf("ipc read error: %v", err)
}
if res.Type != "status" {
lb.fatalf("expected status, got %q\n", res.Type)
}
status := res.Status
bossStart := status.BossStart.Format("2006-01-02 15:04:05")
childStart := status.ChildStart.Format("2006-01-02 15:04:05")
fmt.Fprintf(os.Stdout, "littleboss: %s\n\n", lb.name)
fmt.Fprintf(os.Stdout, "boss pid %d, started %s\n", status.BossPid, bossStart)
fmt.Fprintf(os.Stdout, "child pid %d, started %s, reloads %d, failures %d\n\n", status.ChildPid, childStart, status.Reloads, status.Failures)
fmt.Fprintf(os.Stdout, "command line:\n")
fmt.Fprintf(os.Stdout, "\t%s %s\n", status.Path, strings.Join(status.Args, " "))
if len(status.Listeners) > 0 {
fmt.Fprintf(os.Stdout, "\nlisteners:\n")
for name, addr := range status.Listeners {
fmt.Fprintf(os.Stdout, "\t%s:\t%s\n", name, addr)
}
}
os.Exit(0)
}
// child is executed in the child process.
// It prepares the listener flags, connects the
// root context to the supervisor and calls mainFn.
func (lb *Littleboss) child(mainFn func(ctx context.Context)) {
// We are in the child process.
lb.running = true
for name, lnf := range lb.lnFlags {
if lnf.val == "" {
continue
}
i := strings.LastIndex(lnf.val, ":fd:")
if i < 0 {
panic(fmt.Sprintf("littleboss: child listener flag %q value does not include fd: %q", name, lnf.val))
}
i += len(":fd:")
end := strings.LastIndexByte(lnf.val[i:], ':')
network := lnf.val[i : i+end]
fd, err := strconv.Atoi(lnf.val[i+end+1:])
if err != nil {
panic(fmt.Sprintf("littleboss: child listener flag %q bad value: %q: %v", name, lnf.val, err))
}
lnf.f = os.NewFile(uintptr(fd), name)
switch {
case strings.HasPrefix(network, "tcp"):
lnf.ln, err = net.FileListener(lnf.f)
case strings.HasPrefix(network, "udp"), strings.HasPrefix(network, "ip"):
lnf.pc, err = net.FilePacketConn(lnf.f)
default:
err = errors.New("unknown network type")
}
if err != nil {
panic(fmt.Sprintf("littleboss: child listener flag %q (%q) is no good: %v", name, lnf.val, err))
}
}
ctx, cancel := context.WithCancel(context.Background())
bossPiper := newPiper(os.NewFile(3, "bossPipeR"), os.NewFile(4, "bossPipeW"))
bossPiper.Write("ready")
if err := bossPiper.Error(); err != nil {
panic(fmt.Sprintf("littleboss: cannot signal boss: %v", err))
}
if res := bossPiper.Read(); res != "go" {
panic(fmt.Sprintf("littleboss: child did not get go: %s (%v)", res, bossPiper.Error()))
}
go func() {
switch cmd := bossPiper.Read(); cmd {
case "lameduck":
cancel()
default:
panic(fmt.Sprintf("littleboss: unknown boss pipe command: %s (%v)", cmd, bossPiper.Error()))
}
}()
mainFn(ctx)
bossPiper.Write("finished")
lb.exit(0)
}
func (lb *Littleboss) stderr() io.Writer {
return lb.FlagSet.Output()
}
func (lb *Littleboss) socketdir() string {
return filepath.Join(os.TempDir(), fmt.Sprintf("littleboss-%s-%s", lb.username, lb.name))
}
func (lb *Littleboss) fatalf(format string, args ...interface{}) {
fmt.Fprintf(lb.stderr(), "%s: littleboss: %s\n", lb.cmdname, fmt.Sprintf(format, args...))
lb.exit(1)
}
func (lb *Littleboss) isSupervisor() bool {
return lb.mode != nil && *lb.mode == "start"
}
func (lb *Littleboss) exit(code int) {
if lb.isSupervisor() {
os.RemoveAll(lb.socketdir())
}
os.Exit(code)
}
type piper struct {
r, w *os.File
bufr *bufio.Reader
err error
}
func newPiper(r, w *os.File) *piper {
return &piper{
r: r,
w: w,
bufr: bufio.NewReader(r),
}
}
func (p *piper) Error() error {
return p.err
}
func (p *piper) Read() string {
if p.err != nil {
return ""
}
str, err := p.bufr.ReadString('\n')
if err != nil {
p.r.Close()