Skip to content

Add proc_info structs and methods to macOS SystemB#1723

Open
dbwiddis wants to merge 1 commit into
java-native-access:masterfrom
dbwiddis:add-resource-usage-and-proc-info
Open

Add proc_info structs and methods to macOS SystemB#1723
dbwiddis wants to merge 1 commit into
java-native-access:masterfrom
dbwiddis:add-resource-usage-and-proc-info

Conversation

@dbwiddis
Copy link
Copy Markdown
Contributor

@dbwiddis dbwiddis commented May 11, 2026

Adds macOS process/socket information structs and methods to c.s.j.p.mac.SystemB.

Most mappings have been in use by OSHI since 2020, finally catching up on my upstream contributions. vm_deallocate (yes I had a memory leak for a decade) and statfs64 are recent additions.

Changes

c.s.j.p.mac.SystemB:

  • ProcFdInfo
  • InSockInfo
  • TcpSockInfo
  • proc_pidfdinfo(), statfs64(), vm_deallocate() methods
  • Constants: PROC_PIDLISTFDS, PROC_PIDFDSOCKETINFO, PROX_FDTYPE_SOCKET, SOCKINFO_IN, SOCKINFO_TCP, TSI_T_NTIMERS, AF_INET, AF_INET6

Reference headers

  • sys/proc_info.h: struct proc_fdinfo, struct in_sockinfo, struct tcp_sockinfo, PROC_PIDLISTFDS, PROC_PIDFDSOCKETINFO, PROX_FDTYPE_SOCKET, TSI_T_NTIMERS

@dbwiddis dbwiddis force-pushed the add-resource-usage-and-proc-info branch from fd3e4e7 to 4cd4d97 Compare May 11, 2026 01:04
*/
@Structure.FieldOrder({ "insi_fport", "insi_lport", "insi_gencnt", "insi_flags", "insi_flow", "insi_vflag",
"insi_ip_ttl", "rfu_1", "insi_faddr", "insi_laddr", "insi_v4", "insi_v6" })
class InSockInfo extends Structure {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this: https://github.com/phracker/MacOSX-SDKs/blob/041600eda65c6a668f66cb7d56b7d1da3e8bcc93/MacOSX11.3.sdk/usr/include/sys/proc_info.h#L360-L388

The first members match. Are the latter members intentionally not mapped as struct but instead as byte arrays?

Copy link
Copy Markdown
Contributor Author

@dbwiddis dbwiddis May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int arrays to be precise.

I knew there was a reason I hadn't contributed these upstream for years... since I didn't really need them in my project I just made sure the byte counts matched as creating a union I would never need was annoying.

So this is four 32-bit IP addresses, the larger member of the first and second unions (thus int[4])

struct in4in6_addr {
	u_int32_t               i46a_pad32[3];
	struct in_addr          i46a_addr4;
};
struct in_addr {
    in_addr_t s_addr;
};
typedef __uint32_t      in_addr_t;      /* base type for internet address */

Similarly with the last insi_v6 structure.

Not sure I want to go to the trouble of mapping them. I can just close this PR. (I guess it's not that hard. I'm just lazy.)

Comment on lines +380 to +388
int bufferSize = SystemB.INSTANCE.proc_pidinfo(pid, SystemB.PROC_PIDLISTFDS, 0, null, 0);
assertTrue(bufferSize > 0);

int numFds = bufferSize / new ProcFdInfo().size();
assertTrue(numFds > 0);

ProcFdInfo[] fdInfoArray = (ProcFdInfo[]) new ProcFdInfo().toArray(numFds);
int ret = SystemB.INSTANCE.proc_pidinfo(pid, SystemB.PROC_PIDLISTFDS, 0, fdInfoArray[0],
bufferSize);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is fishy here. I'd expect proc_pidfdinfo not proc_pidinfo to be called here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbwiddis dbwiddis force-pushed the add-resource-usage-and-proc-info branch from 4cd4d97 to 4ba2dc0 Compare May 12, 2026 03:23
Add In4In6Addr, In6Addr, InsiAddr union for proper address mapping
in InSockInfo. Add ProcFileInfo, SockbufInfo, SocketInfo, Pri union,
and SocketFdInfo structures for proc_pidfdinfo support.

InSockInfo.read() and SocketInfo.read() automatically select the
correct union type based on insi_vflag and soi_kind respectively.

Add testProcPidFdSocketInfo test exercising proc_pidfdinfo with
PROC_PIDFDSOCKETINFO.
@dbwiddis dbwiddis force-pushed the add-resource-usage-and-proc-info branch from 4ba2dc0 to 70db423 Compare May 12, 2026 03:24
@dbwiddis
Copy link
Copy Markdown
Contributor Author

Properly mapped the unions. Made identical changes here: https://github.com/dbwiddis/oshi/tree/jna-union-mapping

And got sane socket info:

Internet Protocol statistics:
 TCPv4: TcpStats [connectionsEstablished=35, connectionsActive=0, connectionsPassive=0, connectionFailures=0, connectionsReset=0, segmentsSent=44072670, segmentsReceived=39606736, segmentsRetransmitted=0, inErrors=17, outResets=0]
 TCPv6: TcpStats [connectionsEstablished=28, connectionsActive=0, connectionsPassive=0, connectionFailures=0, connectionsReset=0, segmentsSent=11068788, segmentsReceived=23792192, segmentsRetransmitted=0, inErrors=0, outResets=0]
 UDPv4: UdpStats [datagramsSent=12698180, datagramsReceived=28394674, datagramsNoPort=0, datagramsReceivedErrors=16]
 UDPv6: UdpStats [datagramsSent=3296969, datagramsReceived=0, datagramsNoPort=0, datagramsReceivedErrors=0]
IPConnection [type=tcp4, localAddress=/127.0.0.1, localPort=54293, foreignAddress=/127.0.0.1, foreignPort=54292, state=ESTABLISHED, transmitQueue=0, receiveQueue=0, owningProcessId=21781]
IPConnection [type=tcp6, localAddress=/2601:601:d47c:3090:e8d5:53bc:e284:8cdb, localPort=53268, foreignAddress=/2607:f8b0:400e:c05:0:0:0:bc, foreignPort=5228, state=ESTABLISHED, transmitQueue=0, receiveQueue=0, owningProcessId=68912]
IPConnection [type=tcp4, localAddress=/10.0.0.143, localPort=53898, foreignAddress=/140.82.113.25, foreignPort=443, state=ESTABLISHED, transmitQueue=0, receiveQueue=0, owningProcessId=68912]
IPConnection [type=udp6, localAddress=/2601:601:d47c:3090:e8d5:53bc:e284:8cdb, localPort=63483, foreignAddress=/2607:f8b0:400a:809:0:0:0:200e, foreignPort=443, state=NONE, transmitQueue=0, receiveQueue=0, owningProcessId=68912]
IPConnection [type=tcp4, localAddress=/10.0.0.143, localPort=54198, foreignAddress=/143.204.160.50, foreignPort=443, state=ESTABLISHED, transmitQueue=0, receiveQueue=0, owningProcessId=68912]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants