Skip to content

Instantly share code, notes, and snippets.

@attilathedud
Last active July 4, 2021 10:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save attilathedud/e58917c9fd095a84fd5bbfb31674be05 to your computer and use it in GitHub Desktop.
Save attilathedud/e58917c9fd095a84fd5bbfb31674be05 to your computer and use it in GitHub Desktop.
Mac OS X El Capitan (10.11) and task_for_pid()
/*
Full explanation is available here: http://attilathedud.me/mac-os-x-el-capitan-10-11-and-task_for_pid/
*/
/*
To compile, create a file called Info.plist with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SecTaskAccess</key>
<array>
<string>allowed</string>
</array>
</dict>
</plist>
When compiling, use -sectcreate to create a section for the plist:
gcc task_for_pid.c -sectcreate __TEXT __info_plist ./Info.plist -o task_for_pid
Run using sudo ./task_for_pid _some_pid
*/
/*!
* task_for_pid.c: Given a pid in argv[ 1 ], return the mach task port.
*/
#include <stdio.h>
#include <stdlib.h>
#include <mach/mach.h>
int main( int argc, char** argv )
{
kern_return_t kern_return = 0;
mach_port_t task = 0;
long int pid = 0;
char *endptr = NULL;
if( argc < 2 )
{
return 0;
}
pid = strtol( argv[ 1 ], &endptr, 10 );
kern_return = task_for_pid( mach_task_self(), pid, &task );
if( kern_return != KERN_SUCCESS )
{
printf( "task_for_pid failed: %s\n", mach_error_string( kern_return ) );
return 0;
}
printf( "%u\n", task );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment