Skip to content

Commit

Permalink
fix #2639: getOwningProcessId sometimes return -1 on 64x linux (#2645)
Browse files Browse the repository at this point in the history
socklen_t is an unsigned (native) long which is 32-bit or 64-bit depending on OS bitness.
  • Loading branch information
yourancc committed May 25, 2024
1 parent 9bc64f4 commit cad9125
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 6.6.1 (in progress)

##### Bug fixes / Improvements
* [#2645](https://github.com/oshi/oshi/pull/2645): fix getOwningProcessId sometimes return -1 on 64x linux - [@yourancc](https://github.com/yourancc).

# 6.6.0 (2024-04-13)

##### New Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ public static File[] getPidFiles() {
*
* @return a map with socket as the key and pid as the value
*/
public static Map<Integer, Integer> querySocketToPidMap() {
Map<Integer, Integer> pidMap = new HashMap<>();
public static Map<Long, Integer> querySocketToPidMap() {
Map<Long, Integer> pidMap = new HashMap<>();
for (File f : getPidFiles()) {
int pid = ParseUtil.parseIntOrDefault(f.getName(), -1);
File[] fds = getFileDescriptorFiles(pid);
Expand All @@ -445,7 +445,7 @@ public static Map<Integer, Integer> querySocketToPidMap() {
if (symLink != null) {
Matcher m = SOCKET.matcher(symLink);
if (m.matches()) {
pidMap.put(ParseUtil.parseIntOrDefault(m.group(1), -1), pid);
pidMap.put(ParseUtil.parseLongOrDefault(m.group(1), -1L), pid);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public UdpStats getUDPv6Stats() {
@Override
public List<IPConnection> getConnections() {
List<IPConnection> conns = new ArrayList<>();
Map<Integer, Integer> pidMap = ProcessStat.querySocketToPidMap();
Map<Long, Integer> pidMap = ProcessStat.querySocketToPidMap();
conns.addAll(queryConnections("tcp", 4, pidMap));
conns.addAll(queryConnections("tcp", 6, pidMap));
conns.addAll(queryConnections("udp", 4, pidMap));
conns.addAll(queryConnections("udp", 6, pidMap));
return conns;
}

private static List<IPConnection> queryConnections(String protocol, int ipver, Map<Integer, Integer> pidMap) {
private static List<IPConnection> queryConnections(String protocol, int ipver, Map<Long, Integer> pidMap) {
List<IPConnection> conns = new ArrayList<>();
for (String s : FileUtil.readFile(ProcPath.NET + "/" + protocol + (ipver == 6 ? "6" : ""))) {
if (s.indexOf(':') >= 0) {
Expand All @@ -72,7 +72,7 @@ private static List<IPConnection> queryConnections(String protocol, int ipver, M
Pair<byte[], Integer> fAddr = parseIpAddr(split[2]);
TcpState state = stateLookup(ParseUtil.hexStringToInt(split[3], 0));
Pair<Integer, Integer> txQrxQ = parseHexColonHex(split[4]);
int inode = ParseUtil.parseIntOrDefault(split[9], 0);
long inode = ParseUtil.parseLongOrDefault(split[9], 0);
conns.add(new IPConnection(protocol + ipver, lAddr.getA(), lAddr.getB(), fAddr.getA(), fAddr.getB(),
state, txQrxQ.getA(), txQrxQ.getB(), pidMap.getOrDefault(inode, -1)));
}
Expand Down

0 comments on commit cad9125

Please sign in to comment.