Skip to content

Instantly share code, notes, and snippets.

@mach-kernel
Last active August 24, 2022 10:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mach-kernel/f25e11caf8b0601465c1215b01498292 to your computer and use it in GitHub Desktop.
Save mach-kernel/f25e11caf8b0601465c1215b01498292 to your computer and use it in GitHub Desktop.
launch.h API for getting launchd job by label
/**
./launch_key_getjob_test com.apple.Spotlight
LimitLoadToSessionType: Aqua
MachServices: (cba) 0x7fbfbb504700
Label: com.apple.Spotlight
OnDemand: (cba) 0x7fff9464d490
LastExitStatus: 0
PID: 562
Program: /System/Library/CoreServices/Spotlight.app/Contents/MacOS/Spotlight
ProgramArguments: (cba) 0x7fbfbb504b70
PerJobMachServices: (cba) 0x7fbfbb5049b0
*/
#include <stdio.h>
#include <launch.h>
#include <errno.h>
void print_job(launch_data_t j, const char *key, void *context)
{
printf("%s: ", key);
if (launch_data_get_type(j) == LAUNCH_DATA_STRING) {
printf("%s\n", launch_data_get_string(j));
}
else if (launch_data_get_type(j) == LAUNCH_DATA_INTEGER) {
printf("%lli\n", launch_data_get_integer(j));
}
else {
printf("(cba) %p\n", j);
}
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s [label]\n", argv[0]);
return 1;
}
launch_data_t msg, resp = NULL;
msg = launch_data_alloc(LAUNCH_DATA_DICTIONARY);
launch_data_dict_insert(msg, launch_data_new_string(argv[1]), LAUNCH_KEY_GETJOB);
resp = launch_msg(msg);
launch_data_free(msg);
if (launch_data_get_type(resp) == LAUNCH_DATA_DICTIONARY)
launch_data_dict_iterate(resp, print_job, NULL);
else
printf("%s not found\n", argv[1]);
launch_data_free(resp);
return 0;
}
@triste24
Copy link

it seems that 'launch_msg' can not get jobs of root?

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