Access to process unique id

Hi Everyone,

In libproc, there is a flavour for proc_pidinfo() PROC_PIDUNIQIDENTIFIERINFO, which as the name suggests, returns a struct of values that uniquely identify a process - more reliably than a pid.

In particular, it seems to have a 64 bit value that appears to be unique forever (or at least until system restart), thus shouldn't suffer from pid reuse races.

The problem is that this interface is gated behind #ifdef PRIVATE, and as such is pretty much the antithesis of 'published api'. So I guess my question is, is there a 'legit' way of accessing this value (or an equivalent) ? The call seems to work fine, and the number appears unique..

thanks, nick

Answered by DTS Engineer in 886279022

@neek78 Brilliant coding skills and nice finding for sure.

Unfortunately, I do not have an answer about that becoming a public api, but I think will be worth for you to request an enhancement and get an answer.

If you'd like us to consider adding the necessary functionality, please file an enhancement request using Feedback Assistant. Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

Good luck!

Albert
  Worldwide Developer Relations.

Thanks for the post, this is a great post and hope many developers jump into this thread and provide suggestions and opinions of the best way to accomplish that.

proc_pidinfo() PROC_PIDUNIQIDENTIFIERINFO looks something I haven't use to be honest. Using them in production code sounds risky because is subject to breakage in future macOS/iOS updates without warning? If your concern about PID reuse races you should not use PIDs at all.

If you are iterating over running processes or monitoring a specific process and want to ensure the PID hasn't been reused by a new process, the standard approach is to combine the PID with the process's start time, it is practically impossible for a PID to be reused in the exact same microsecond.

Albert
  Worldwide Developer Relations.

Hi Albert,

Thanks for the info. I am aware of the pid+ctime trick. Whist I agree the odds of a pid being re-used in the same microsecond are practically impossible, clearly unique id was created for a reason.

I guess I was taken by the unique id function. It's a more elegant solution to the problem. It has been in Darwin in an almost unaltered form since 2013 with a comment saying /* This structure is API */.

So I guess my question remains - is there any other way to access this, and if not, is it possible to request that this become public api? It's already implemented and has tests, it really just needs to be moved to a public header.

thanks nick

Accepted Answer

@neek78 Brilliant coding skills and nice finding for sure.

Unfortunately, I do not have an answer about that becoming a public api, but I think will be worth for you to request an enhancement and get an answer.

If you'd like us to consider adding the necessary functionality, please file an enhancement request using Feedback Assistant. Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

Good luck!

Albert
  Worldwide Developer Relations.

Thanks Albert - I opened FB22652617

@neek78 Thanks for doing that so quickly. Give the team sometime to look into it and I'm sure they'll give you an answer eventually.

For more details on when you'll see updates to your report, please see What to expect after submission.

Albert
  Worldwide Developer Relations.

Access to process unique id
 
 
Q