You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by pp...@apache.org on 2022/03/22 22:34:48 UTC

[maven-mvnd] 01/02: Make `DaemonRegistry.getProcessId0` more robust

This is an automated email from the ASF dual-hosted git repository.

ppalaga pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git

commit 86d1c3661b3e94f6fc8727195851984b2ad7bf1d
Author: Jesse Glick <jg...@apache.org>
AuthorDate: Tue Mar 22 09:30:46 2022 -0400

    Make `DaemonRegistry.getProcessId0` more robust
---
 .../org/mvndaemon/mvnd/common/DaemonRegistry.java  | 28 +++++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java b/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java
index ab414dc..7fcf21c 100644
--- a/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java
+++ b/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java
@@ -271,23 +271,33 @@ public class DaemonRegistry implements AutoCloseable {
     private static final int PROCESS_ID = getProcessId0();
 
     private static int getProcessId0() {
-        String pid = null;
         try {
             final Path self = Paths.get("/proc/self");
             if (Files.exists(self)) {
-                pid = self.toRealPath().getFileName().toString();
+                String pid = self.toRealPath().getFileName().toString();
+                if (pid.equals("self")) {
+                    LOGGER.debug("/proc/self symlink could not be followed");
+                } else {
+                    LOGGER.debug("loading own PID from /proc/self link: {}", pid);
+                    try {
+                        return Integer.parseInt(pid);
+                    } catch (NumberFormatException x) {
+                        LOGGER.warn("Unable to determine PID from malformed /proc/self link `" + pid + "`");
+                    }
+                }
             }
         } catch (IOException ignored) {
+            LOGGER.debug("could not load /proc/self", ignored);
         }
-        if (pid == null) {
-            pid = ManagementFactory.getRuntimeMXBean().getName().split("@", 0)[0];
-        }
-        if (pid == null) {
+        String vmname = ManagementFactory.getRuntimeMXBean().getName();
+        String pid = vmname.split("@", 0)[0];
+        LOGGER.debug("loading own PID from VM name: {}", pid);
+        try {
+            return Integer.parseInt(pid);
+        } catch (NumberFormatException x) {
             int rpid = new Random().nextInt(1 << 16);
-            LOGGER.warn("Unable to determine PID, picked a random number=" + rpid);
+            LOGGER.warn("Unable to determine PID from malformed VM name `" + vmname + "`, picked a random number=" + rpid);
             return rpid;
-        } else {
-            return Integer.parseInt(pid);
         }
     }