Consider the small test project whose code is at the end [1]. If I run it normally it prints:
% ./Test748948
host_get_host_priv_port failed, kr: 4
But if I run it with privileges it prints:
% sudo ./Test748948
process count: 14
That seems pretty reasonable to me; you can’t get a processor control port from non-privileged code.
A sandboxed app is not able to escalate privileges, so it seems like your current path forward is blocked.
What’s you’re high-level goal here? Why do you intend to do with these processor control ports when you get them?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] My experience with Mach is that it’s run your tests with C before you start trying to get things to work with Swift. Also, it’s best to test with a simple command-line tool project rather than a full-blown app.
#include <stdio.h>
#include <stdlib.h>
#include <mach/mach.h>
int main(int argc, char **argv) {
mach_port_t hostPrivPort = MACH_PORT_NULL;
kern_return_t kr = host_get_host_priv_port(mach_host_self(), &hostPrivPort);
if (kr != KERN_SUCCESS) {
fprintf(stderr, "host_get_host_priv_port failed, kr: %d\n", (int) kr);
exit(1);
}
processor_array_t processors = NULL;
mach_msg_type_number_t processorCount = 0;
kr = host_processors(hostPrivPort, &processors, &processorCount);
if (kr != KERN_SUCCESS) {
fprintf(stderr, "host_processors failed, kr: %d\n", (int) kr);
exit(1);
}
fprintf(stderr, "process count: %d\n", (int) processorCount);
return 0;
}