github.com/openimsdk/tools@v0.0.49/utils/mageutil/sys.go (about) 1 package mageutil 2 3 import ( 4 "fmt" 5 "github.com/shirou/gopsutil/net" 6 "github.com/shirou/gopsutil/process" 7 "os" 8 "path/filepath" 9 "runtime" 10 "strings" 11 ) 12 13 func OsArch() string { 14 os := runtime.GOOS 15 arch := runtime.GOARCH 16 if os == "windows" { 17 return fmt.Sprintf("%s\\%s", os, arch) 18 } 19 return fmt.Sprintf("%s/%s", os, arch) 20 } 21 22 func CheckProcessNames(processPath string, expectedCount int) error { 23 processes, err := process.Processes() 24 if err != nil { 25 return fmt.Errorf("failed to get processes: %v", err) 26 } 27 28 runningCount := 0 29 for _, p := range processes { 30 exePath, err := p.Exe() 31 if err != nil { 32 continue 33 } 34 35 if strings.EqualFold(exePath, processPath) { 36 runningCount++ 37 38 } 39 } 40 41 if runningCount == expectedCount { 42 return nil 43 } else { 44 return fmt.Errorf("%s Expected %d processes, but %d running", processPath, expectedCount, runningCount) 45 } 46 } 47 48 // CheckProcessNamesExist checks if there are any processes running that match the specified path. 49 func CheckProcessNamesExist(processPath string) bool { 50 processes, err := process.Processes() 51 if err != nil { 52 fmt.Printf("Failed to get processes: %v\n", err) 53 return false 54 } 55 56 for _, p := range processes { 57 exePath, err := p.Exe() 58 if err != nil { 59 continue 60 } 61 62 if exePath == processPath { 63 return true // Find at least one matching process 64 } 65 } 66 67 return false 68 } 69 70 // PrintBinaryPorts prints the ports listened by the process of a specified binary file along with its command line arguments. 71 func PrintBinaryPorts(binaryPath string) { 72 pids, err := FindPIDsByBinaryPath(binaryPath) 73 if err != nil { 74 fmt.Println("Error finding PIDs:", err) 75 return 76 } 77 78 if len(pids) == 0 { 79 fmt.Printf("No running processes found for binary: %s\n", binaryPath) 80 return 81 } 82 83 for _, pid := range pids { 84 85 proc, err := process.NewProcess(int32(pid)) 86 if err != nil { 87 fmt.Printf("Failed to create process object for PID %d: %v\n", pid, err) 88 continue 89 } 90 91 cmdline, err := proc.Cmdline() 92 if err != nil { 93 fmt.Printf("Failed to get command line for PID %d: %v\n", pid, err) 94 continue 95 } 96 97 connections, err := net.ConnectionsPid("all", int32(pid)) 98 if err != nil { 99 fmt.Printf("Error getting connections for PID %d: %v\n", pid, err) 100 continue 101 } 102 103 portsMap := make(map[string]struct{}) 104 for _, conn := range connections { 105 if conn.Status == "LISTEN" { 106 port := fmt.Sprintf("%d", conn.Laddr.Port) 107 portsMap[port] = struct{}{} 108 } 109 } 110 111 if len(portsMap) == 0 { 112 PrintGreen(fmt.Sprintf("Cmdline: %s, PID: %d is not listening on any ports.", cmdline, pid)) 113 } else { 114 ports := make([]string, 0, len(portsMap)) 115 for port := range portsMap { 116 ports = append(ports, port) 117 } 118 PrintGreen(fmt.Sprintf("Cmdline: %s, PID: %d is listening on ports: %s", cmdline, pid, strings.Join(ports, ", "))) 119 } 120 } 121 } 122 123 // FindPIDsByBinaryPath finds all matching process PIDs by binary path. 124 func FindPIDsByBinaryPath(binaryPath string) ([]int, error) { 125 var pids []int 126 processes, err := process.Processes() 127 if err != nil { 128 return nil, err 129 } 130 131 for _, proc := range processes { 132 exePath, err := proc.Exe() 133 if err != nil { 134 continue 135 } 136 137 if strings.EqualFold(exePath, binaryPath) { 138 pids = append(pids, int(proc.Pid)) 139 } 140 } 141 142 return pids, nil 143 } 144 145 // KillExistBinary kills all processes matching the given binary file path. 146 func KillExistBinary(binaryPath string) { 147 processes, err := process.Processes() 148 if err != nil { 149 fmt.Printf("Failed to get processes: %v\n", err) 150 return 151 } 152 153 for _, p := range processes { 154 exePath, err := p.Exe() 155 if err != nil { 156 continue 157 } 158 159 if strings.Contains(strings.ToLower(exePath), strings.ToLower(binaryPath)) { 160 161 //if strings.EqualFold(exePath, binaryPath) { 162 cmdline, err := p.Cmdline() 163 if err != nil { 164 fmt.Printf("Failed to get command line for process %d: %v\n", p.Pid, err) 165 continue 166 } 167 168 err = p.Terminate() 169 if err != nil { 170 171 err = p.Kill() 172 if err != nil { 173 fmt.Printf("Failed to kill process cmdline: %s, pid: %d, err: %v\n", cmdline, p.Pid, err) 174 } else { 175 fmt.Printf("Killed process cmdline: %s, pid: %d\n", cmdline, p.Pid) 176 } 177 } else { 178 fmt.Printf("Terminated process cmdline: %s, pid: %d\n", cmdline, p.Pid) 179 } 180 } 181 } 182 } 183 184 // DetectPlatform detects the operating system and architecture. 185 func DetectPlatform() string { 186 targetOS, targetArch := runtime.GOOS, runtime.GOARCH 187 switch targetArch { 188 case "amd64", "arm64": 189 default: 190 fmt.Printf("Unsupported architecture: %s\n", targetArch) 191 os.Exit(1) 192 } 193 return fmt.Sprintf("%s_%s", targetOS, targetArch) 194 } 195 196 // rootDir gets the absolute path of the current directory. 197 func rootDir() string { 198 dir, err := os.Getwd() 199 if err != nil { 200 fmt.Println("Failed to get current directory:", err) 201 os.Exit(1) 202 } 203 return dir 204 } 205 206 var rootDirPath = rootDir() 207 var platformsOutputBase = filepath.Join(rootDirPath, "_output/bin/platforms") 208 var toolsOutputBase = filepath.Join(rootDirPath, "_output/bin/tools")