|
1 | 1 | package io.dongtai.iast.agent.manager; |
2 | 2 |
|
3 | | -import io.dongtai.iast.agent.*; |
| 3 | +import io.dongtai.iast.agent.IastClassLoader; |
| 4 | +import io.dongtai.iast.agent.IastProperties; |
| 5 | +import io.dongtai.iast.agent.LogCollector; |
4 | 6 | import io.dongtai.iast.agent.fallback.FallbackManager; |
5 | 7 | import io.dongtai.iast.agent.report.AgentRegisterReport; |
6 | | -import io.dongtai.iast.agent.util.*; |
| 8 | +import io.dongtai.iast.agent.util.FileUtils; |
| 9 | +import io.dongtai.iast.agent.util.HttpClientUtils; |
| 10 | +import io.dongtai.iast.agent.util.ThreadUtils; |
7 | 11 | import io.dongtai.iast.common.state.AgentState; |
8 | 12 | import io.dongtai.log.DongTaiLog; |
9 | 13 | import io.dongtai.log.ErrorCode; |
|
13 | 17 | import java.lang.instrument.Instrumentation; |
14 | 18 | import java.lang.management.ManagementFactory; |
15 | 19 | import java.util.jar.JarFile; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
16 | 22 |
|
17 | 23 | /** |
18 | 24 | * 引擎管理类,负责engine模块的完整生命周期,包括:下载、安装、启动、停止、重启、卸载 |
@@ -248,11 +254,32 @@ public synchronized boolean uninstall() { |
248 | 254 |
|
249 | 255 | public static String getPID() { |
250 | 256 | if (PID == null) { |
251 | | - PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; |
| 257 | + String runtimeName = ManagementFactory.getRuntimeMXBean().getName(); |
| 258 | + PID = extractPID(runtimeName); |
252 | 259 | } |
253 | 260 | return PID; |
254 | 261 | } |
255 | 262 |
|
| 263 | + /** |
| 264 | + * 通过正则提取runtimeName的PID |
| 265 | + * 从开头开始匹配,遇到非数字字符串停止匹配 |
| 266 | + * @param runtimeName 运行名称通常为PID@HostName |
| 267 | + * @return PID |
| 268 | + */ |
| 269 | + public static String extractPID(String runtimeName){ |
| 270 | + Pattern pattern = Pattern.compile("^\\d+"); |
| 271 | + Matcher matcher = pattern.matcher(runtimeName); |
| 272 | + |
| 273 | + //防止极端情况未获取到PID ,设置默认值为0,防止服务端出现问题 |
| 274 | + String extractedNumber = "0"; |
| 275 | + if (matcher.find()) { |
| 276 | + extractedNumber = matcher.group(); // 提取匹配到的数字 |
| 277 | + }else { |
| 278 | + DongTaiLog.warn("Get PID parsing exception, PID raw data is {}",runtimeName); |
| 279 | + } |
| 280 | + return extractedNumber; |
| 281 | + } |
| 282 | + |
256 | 283 | public AgentState getAgentState() { |
257 | 284 | return this.agentState; |
258 | 285 | } |
|
0 commit comments