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
|
//go:build !linux
// +build !linux
package preflight
import (
"context"
"fmt"
"net"
"runtime"
"strconv"
"time"
"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/daemonclient"
crcerrors "github.com/crc-org/crc/v2/pkg/crc/errors"
"github.com/crc-org/crc/v2/pkg/crc/logging"
"github.com/shirou/gopsutil/v3/process"
)
func killDaemonProcess() error {
crcProcessName := "crc"
if runtime.GOOS == "windows" {
crcProcessName = "crc.exe"
}
processes, err := process.Processes()
if err != nil {
return err
}
for _, process := range processes {
// Ignore the error from process.Name() because errors
// come from processes which are not owned by the current user
// on Mac - https://github.com/shirou/gopsutil/issues/1288
name, _ := process.Name()
if name == crcProcessName {
cmdLine, err := process.CmdlineSlice()
if err != nil {
return err
}
if isDaemonProcess(cmdLine) {
logging.Debugf("Got the pid for %s : %d", name, process.Pid)
if err := process.Kill(); err != nil {
return err
}
}
}
}
return nil
}
// isDaemonProcess return true if the cmdline contains the word daemon
// since only one instance of the daemon is allowed to run this should
// be enough to detect the daemon process
func isDaemonProcess(cmdLine []string) bool {
for _, arg := range cmdLine {
if arg == "daemon" {
return true
}
}
return false
}
func daemonRunning() bool {
if _, err := daemonclient.GetVersionFromDaemonAPI(); err != nil {
return false
}
return true
}
func waitForDaemonRunning() error {
return crcerrors.Retry(context.Background(), 15*time.Second, func() error {
if !daemonRunning() {
return &crcerrors.RetriableError{Err: fmt.Errorf("daemon is not running yet")}
}
return nil
}, 2*time.Second)
}
func sshPortCheck() Check {
return Check{
configKeySuffix: "check-ssh-port",
checkDescription: "Checking SSH port availability",
check: checkSSHPortFree(),
fixDescription: fmt.Sprintf("crc uses port %d to run SSH", constants.VsockSSHPort),
flags: NoFix,
labels: None,
}
}
func checkSSHPortFree() func() error {
return func() error {
host := net.JoinHostPort(constants.LocalIP, strconv.Itoa(constants.VsockSSHPort))
daemonClient := daemonclient.New()
exposed, err := daemonClient.NetworkClient.List()
if err == nil {
// if port already exported by vsock we could proceed
for _, e := range exposed {
if e.Local == host {
return nil
}
}
}
server, err := net.Listen("tcp", host)
// if it fails then the port is likely taken
if err != nil {
return fmt.Errorf("port %d already in use: %s", constants.VsockSSHPort, err)
}
// we successfully used and closed the port
return server.Close()
}
}
|