rentzsch/mach_star — Issue #21: Mach_Error 4 Occurring When Attempting Injection
Author: Jnani
Created: 2011-10-08 15:52:44+00:00
State: open

Hello,
I am trying to inject a bundle into a certain process. I have created a new Xcode Project which solely handles the injection. I added the necessary frameworks and project files to the app. And have also added an "injectee" bundle to the project. I feel that everything that I am doing is right however I keep occurring with the messages in the log:

mach_inject failing.. (os/kern) successful
mach inject done? 4
MyApp[34807:c07] Failure code 4

Here is my relevant code:

- (void)injectBundleWithPath:(NSString *)bundlePath intoProcess:(pid_t)pid
{
if ([bundlePath isAbsolutePath] == 0) {
bundlePath = [[[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:bundlePath] stringByStandardizingPath];
}
mach_error_t err = mach_inject_bundle_pid([bundlePath fileSystemRepresentation], pid);
if (err != err_none)
NSLog(@"Failure code %x", err);
}

- (void)injectBundle:(id)sender {
NSString *bundlePath = [[NSBundle mainBundle]
pathForResource:@"Injectee" ofType:@"bundle"];
pid_t pid = GetPidByBundleIdentifier(@"com.bundle.identifier");
[self injectBundleWithPath:bundlePath intoProcess:pid];
}

I am not sure why this error is happening and was wondering if other users have experienced the same thing. Thanks.