Skip to content

Instantly share code, notes, and snippets.

@hafta
Last active November 7, 2023 23:08
Show Gist options
  • Save hafta/891921b8cd26567bb71a6a89d95f055b to your computer and use it in GitHub Desktop.
Save hafta/891921b8cd26567bb71a6a89d95f055b to your computer and use it in GitHub Desktop.
Small debug program for printing the ProcessInfoRec launching process' signature. When macOS starts a process automatically at login due to the "Reopen windows when logging back in" checkbox, the launching process' signature is "lgnw".
#import <Foundation/Foundation.h>
#include <ApplicationServices/ApplicationServices.h>
//
// Given a single PID on the command line, print the process'
// signature and the launching process' signature. The process'
// signature is a field in the ProcessInfoRec structure.
//
// For converting the process signature 4 byte field to a string.
// Copied from https://stackoverflow.com/questions/14222435/ios-c-convert-integer-into-four-character-string
#include <TargetConditionals.h>
#if TARGET_RT_BIG_ENDIAN
# define FourCC2Str(fourcc) (const char[]){*((char*)&fourcc), *(((char*)&fourcc)+1), *(((char*)&fourcc)+2), *(((char*)&fourcc)+3),0}
#else
# define FourCC2Str(fourcc) (const char[]){*(((char*)&fourcc)+3), *(((char*)&fourcc)+2), *(((char*)&fourcc)+1), *(((char*)&fourcc)+0),0}
#endif
int main(int argc, const char * argv[]) {
if (argc != 2) {
printf("Usage: %s <PID>\n", argv[0]);
exit (1);
}
int pid = atoi(argv[1]);
ProcessSerialNumber psn;
OSStatus error = GetProcessForPID(pid, &psn);
ProcessInfoRec procInfo = {};
if (GetProcessInformation(&psn, &procInfo) != noErr) {
NSLog(@"GetProcessInformation() for PID %d failed.", pid);
exit (1);
}
NSLog(@"This process: PID: [%d] signature: [%s] name: [%s]",
pid,
FourCC2Str(procInfo.processSignature),
procInfo.processName);
ProcessInfoRec parentInfo = {};
error = GetProcessInformation(&procInfo.processLauncher, &parentInfo);
if (error == procNotFound) {
NSLog(@"GetProcessInformation() for process launcher failed: procNotFound");
exit (1);
} else if (error != noErr) {
NSLog(@"GetProcessInformation() for process launcher failed.");
exit (1);
}
NSLog(@"Launching process: signature: [%s] name: [%s]",
FourCC2Str(parentInfo.processSignature),
parentInfo.processName);
exit (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment