2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2015-01-06 11:13:57 +00:00
|
|
|
|
2015-07-07 03:08:16 +00:00
|
|
|
// Package utils contains internal helper functions for go-ethereum commands.
|
2014-05-14 10:41:30 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2020-02-03 12:02:26 +00:00
|
|
|
"context"
|
2014-06-26 17:41:36 +00:00
|
|
|
"fmt"
|
2015-03-18 12:36:48 +00:00
|
|
|
"io"
|
2014-08-14 23:07:40 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2016-09-26 15:23:26 +00:00
|
|
|
"runtime"
|
2018-02-20 12:33:34 +00:00
|
|
|
"syscall"
|
2014-08-14 23:07:40 +00:00
|
|
|
|
2020-08-24 17:00:40 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2021-06-13 16:41:39 +00:00
|
|
|
_debug "github.com/ledgerwatch/erigon/common/debug"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/internal/debug"
|
|
|
|
"github.com/ledgerwatch/erigon/log"
|
|
|
|
"github.com/ledgerwatch/erigon/node"
|
2015-05-27 23:16:57 +00:00
|
|
|
)
|
|
|
|
|
2015-05-27 13:48:07 +00:00
|
|
|
// Fatalf formats a message to standard error and exits the program.
|
|
|
|
// The message is also printed to standard output if standard error
|
|
|
|
// is redirected to a different file.
|
2015-03-06 02:00:41 +00:00
|
|
|
func Fatalf(format string, args ...interface{}) {
|
2015-05-27 13:48:07 +00:00
|
|
|
w := io.MultiWriter(os.Stdout, os.Stderr)
|
2016-09-26 15:23:26 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// The SameFile check below doesn't work on Windows.
|
|
|
|
// stdout is unlikely to get redirected though, so just print there.
|
|
|
|
w = os.Stdout
|
|
|
|
} else {
|
|
|
|
outf, _ := os.Stdout.Stat()
|
|
|
|
errf, _ := os.Stderr.Stat()
|
|
|
|
if outf != nil && errf != nil && os.SameFile(outf, errf) {
|
|
|
|
w = os.Stderr
|
|
|
|
}
|
2015-05-27 13:48:07 +00:00
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
|
2015-03-06 02:00:41 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-03-12 17:26:06 +00:00
|
|
|
func StartNode(stack *node.Node) {
|
2015-11-17 16:33:25 +00:00
|
|
|
if err := stack.Start(); err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
Fatalf("Error starting protocol stack: %v", err)
|
2015-01-05 16:12:52 +00:00
|
|
|
}
|
2015-07-06 13:01:13 +00:00
|
|
|
go func() {
|
|
|
|
sigc := make(chan os.Signal, 1)
|
2018-02-20 12:33:34 +00:00
|
|
|
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
|
2021-06-13 16:41:39 +00:00
|
|
|
_debug.GetSigC(&sigc)
|
2015-07-06 13:01:13 +00:00
|
|
|
defer signal.Stop(sigc)
|
2021-01-19 08:26:42 +00:00
|
|
|
|
2015-07-06 13:01:13 +00:00
|
|
|
<-sigc
|
2017-03-02 13:06:16 +00:00
|
|
|
log.Info("Got interrupt, shutting down...")
|
2020-08-03 17:40:46 +00:00
|
|
|
go stack.Close()
|
2015-07-06 13:01:13 +00:00
|
|
|
for i := 10; i > 0; i-- {
|
|
|
|
<-sigc
|
|
|
|
if i > 1 {
|
2017-03-02 13:06:16 +00:00
|
|
|
log.Warn("Already shutting down, interrupt more to panic.", "times", i-1)
|
2015-07-06 13:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-06 09:04:52 +00:00
|
|
|
debug.Exit() // ensure trace and CPU profile data is flushed.
|
2016-03-11 23:39:45 +00:00
|
|
|
debug.LoudPanic("boom")
|
2015-07-06 13:01:13 +00:00
|
|
|
}()
|
2015-03-06 02:25:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
func SetupCobra(cmd *cobra.Command) error {
|
|
|
|
return debug.SetupCobra(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopDebug() {
|
|
|
|
debug.Exit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupUrfave(ctx *cli.Context) error {
|
|
|
|
return debug.Setup(ctx)
|
|
|
|
}
|
|
|
|
|
2021-04-26 12:39:34 +00:00
|
|
|
func RootContext() (context.Context, context.CancelFunc) {
|
2020-08-19 11:46:20 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
|
|
|
defer signal.Stop(ch)
|
|
|
|
|
|
|
|
select {
|
2020-10-10 06:06:54 +00:00
|
|
|
case sig := <-ch:
|
|
|
|
log.Info("Got interrupt, shutting down...", "sig", sig)
|
2020-08-19 11:46:20 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}()
|
2021-04-26 12:39:34 +00:00
|
|
|
return ctx, cancel
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|