github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cli/start_unix.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 // +build !windows 12 13 package cli 14 15 import ( 16 "context" 17 "os" 18 "os/exec" 19 "os/signal" 20 "strings" 21 22 "github.com/cockroachdb/cockroach/pkg/cli/cliflags" 23 "github.com/cockroachdb/cockroach/pkg/util/log" 24 "github.com/cockroachdb/cockroach/pkg/util/sdnotify" 25 "github.com/cockroachdb/cockroach/pkg/util/sysutil" 26 "golang.org/x/sys/unix" 27 ) 28 29 // drainSignals are the signals that will cause the server to drain and exit. 30 // 31 // If two drain signals are seen, the second drain signal will be reraised 32 // without a signal handler. The default action of any signal listed here thus 33 // must terminate the process. 34 var drainSignals = []os.Signal{unix.SIGINT, unix.SIGTERM, unix.SIGQUIT} 35 36 // quitSignal is the signal to recognize to dump Go stacks. 37 var quitSignal os.Signal = unix.SIGQUIT 38 39 func handleSignalDuringShutdown(sig os.Signal) { 40 // On Unix, a signal that was not handled gracefully by the application 41 // should be reraised so it is visible in the exit code. 42 43 // Reset signal to its original disposition. 44 signal.Reset(sig) 45 46 // Reraise the signal. os.Signal is always sysutil.Signal. 47 if err := unix.Kill(unix.Getpid(), sig.(sysutil.Signal)); err != nil { 48 // Sending a valid signal to ourselves should never fail. 49 // 50 // Unfortunately it appears (#34354) that some users 51 // run CockroachDB in containers that only support 52 // a subset of all syscalls. If this ever happens, we 53 // still need to quit immediately. 54 log.Fatalf(context.Background(), "unable to forward signal %v: %v", sig, err) 55 } 56 57 // Block while we wait for the signal to be delivered. 58 select {} 59 } 60 61 var startBackground bool 62 63 func init() { 64 for _, cmd := range StartCmds { 65 BoolFlag(cmd.Flags(), &startBackground, cliflags.Background, false) 66 } 67 } 68 69 func maybeRerunBackground() (bool, error) { 70 if startBackground { 71 args := make([]string, 0, len(os.Args)) 72 foundBackground := false 73 for _, arg := range os.Args { 74 if arg == "--background" || strings.HasPrefix(arg, "--background=") { 75 foundBackground = true 76 continue 77 } 78 args = append(args, arg) 79 } 80 if !foundBackground { 81 args = append(args, "--background=false") 82 } 83 cmd := exec.Command(args[0], args[1:]...) 84 cmd.Stdout = os.Stdout 85 cmd.Stderr = stderr 86 87 // Notify to ourselves that we're restarting. 88 _ = os.Setenv(backgroundEnvVar, "1") 89 90 return true, sdnotify.Exec(cmd) 91 } 92 return false, nil 93 } 94 95 func disableOtherPermissionBits() { 96 mask := unix.Umask(0000) 97 mask |= 00007 98 _ = unix.Umask(mask) 99 } 100 101 func useUnixSocketsInDemo() bool { 102 return true 103 }