Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new mach_inject demo project #3

Merged
merged 1 commit into from
Dec 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions mach_inject_example/Injector/DKInjector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Injector.h
// Dark
//
// Created by Erwan Barrier on 8/8/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import <Foundation/Foundation.h>

extern dispatch_source_t g_timer_source;

@interface DKInjector : NSObject

- (mach_error_t)inject:(pid_t)pid withBundle:(const char *)bundlePackageFileSystemRepresentation;

@end
29 changes: 29 additions & 0 deletions mach_inject_example/Injector/DKInjector.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Injector.m
// Dark
//
// Created by Erwan Barrier on 8/8/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import "mach_inject_bundle.h"
#import <mach/mach_error.h>

#import "DKInjector.h"

@implementation DKInjector

- (mach_error_t)inject:(pid_t)pid withBundle:(const char *)bundlePackageFileSystemRepresentation {
// Disarm timer while installing framework
dispatch_source_set_timer(g_timer_source, DISPATCH_TIME_FOREVER, 0llu, 0llu);

mach_error_t error = mach_inject_bundle_pid(bundlePackageFileSystemRepresentation, pid);

// Rearm timer
dispatch_time_t t0 = dispatch_time(DISPATCH_TIME_NOW, 5llu * NSEC_PER_SEC);
dispatch_source_set_timer(g_timer_source, t0, 0llu, 0llu);

return (error);
}

@end
18 changes: 18 additions & 0 deletions mach_inject_example/Injector/Injector-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>CFBundleIdentifier</key>
<string>com.erwanb.MachInjectSample.Injector</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Injector</string>
<key>CFBundleVersion</key>
<string>0.1</string>
<key>SMAuthorizedClients</key>
<array>
<string>identifier com.erwanb.MachInjectSample</string>
</array>
</dict>
</plist>
13 changes: 13 additions & 0 deletions mach_inject_example/Injector/Injector-Launchd.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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>Label</key>
<string>com.erwanb.MachInjectSample.Injector</string>
<key>MachServices</key>
<dict>
<key>com.erwanb.MachInjectSample.Injector.mach</key>
<true/>
</dict>
</dict>
</plist>
7 changes: 7 additions & 0 deletions mach_inject_example/Injector/Injector-Prefix.pch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'Injector' target in the 'Injector' project
//

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
62 changes: 62 additions & 0 deletions mach_inject_example/Injector/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// main.m
// Injector
//
// Created by Erwan Barrier on 8/7/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import <launch.h>

#import "DKInjector.h"

dispatch_source_t g_timer_source = NULL;

int main(int argc, char *argv[])
{
// Init idle-exit timer
dispatch_queue_t mq = dispatch_get_main_queue();
g_timer_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mq);
assert(g_timer_source != NULL);

/* When the idle-exit timer fires, we just call exit(2) with status 0. */
dispatch_set_context(g_timer_source, NULL);
dispatch_source_set_event_handler_f(g_timer_source, (void (*)(void *))exit);
/* We start off with our timer armed. This is for the simple reason that,
* upon kicking off the GCD state engine, the first thing we'll get to is
* a connection on our socket which will disarm the timer. Remember, handling
* new connections and the firing of the idle-exit timer are synchronized.
*/
dispatch_time_t t0 = dispatch_time(DISPATCH_TIME_NOW, 5llu * NSEC_PER_SEC);
dispatch_source_set_timer(g_timer_source, t0, 0llu, 0llu);
dispatch_resume(g_timer_source);


// Check in mach service
launch_data_t req = launch_data_new_string(LAUNCH_KEY_CHECKIN);
assert(req != NULL);

launch_data_t resp = launch_msg(req);
assert(resp != NULL);
assert(launch_data_get_type(resp) == LAUNCH_DATA_DICTIONARY);

launch_data_t machs = launch_data_dict_lookup(resp, LAUNCH_JOBKEY_MACHSERVICES);
assert(machs != NULL);
assert(launch_data_get_type(machs) == LAUNCH_DATA_DICTIONARY);

launch_data_t machPortData = launch_data_dict_lookup(machs, "com.erwanb.MachInjectSample.Injector.mach");

mach_port_t mp = launch_data_get_machport(machPortData);
launch_data_free(req);
launch_data_free(resp);

NSMachPort *rp = [[NSMachPort alloc] initWithMachPort:mp];
NSConnection *c = [NSConnection connectionWithReceivePort:rp sendPort:nil];

DKInjector *injector = [DKInjector new];
[c setRootObject:injector];

[[NSRunLoop currentRunLoop] run];

return (0);
}
21 changes: 21 additions & 0 deletions mach_inject_example/Installer/DKFrameworkInstaller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DKInstaller.h
// Dark
//
// Created by Erwan Barrier on 8/11/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import <Foundation/Foundation.h>

FOUNDATION_EXPORT NSString *const DKFrameworkDstPath;

extern dispatch_source_t g_timer_source;

@interface DKFrameworkInstaller : NSObject

@property (nonatomic, strong) NSError *error;

- (BOOL)installFramework:(NSString *)frameworkPath;

@end
43 changes: 43 additions & 0 deletions mach_inject_example/Installer/DKFrameworkInstaller.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// DKInstaller.m
// Dark
//
// Created by Erwan Barrier on 8/11/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import "DKFrameworkInstaller.h"

NSString *const DKFrameworkDstPath = @"/Library/Frameworks/mach_inject_bundle.framework";

@implementation DKFrameworkInstaller

@synthesize error = _error;

- (BOOL)installFramework:(NSString *)frameworkPath {
// Disarm timer while installing framework
dispatch_source_set_timer(g_timer_source, DISPATCH_TIME_FOREVER, 0llu, 0llu);

NSError *fileError;
BOOL result = YES;

if ([[NSFileManager defaultManager] fileExistsAtPath:DKFrameworkDstPath] == YES) {
result = [[NSFileManager defaultManager] removeItemAtPath:DKFrameworkDstPath error:&fileError];
}

if (result == YES) {
result = [[NSFileManager defaultManager] copyItemAtPath:frameworkPath toPath:DKFrameworkDstPath error:&fileError];
}

if (result == NO) {
_error = fileError;
}

// Rearm timer
dispatch_time_t t0 = dispatch_time(DISPATCH_TIME_NOW, 5llu * NSEC_PER_SEC);
dispatch_source_set_timer(g_timer_source, t0, 0llu, 0llu);

return result;
}

@end
18 changes: 18 additions & 0 deletions mach_inject_example/Installer/Installer-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>CFBundleIdentifier</key>
<string>com.erwanb.MachInjectSample.Installer</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Installer</string>
<key>CFBundleVersion</key>
<string>0.1</string>
<key>SMAuthorizedClients</key>
<array>
<string>identifier com.erwanb.MachInjectSample</string>
</array>
</dict>
</plist>
13 changes: 13 additions & 0 deletions mach_inject_example/Installer/Installer-Launchd.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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>Label</key>
<string>com.erwanb.MachInjectSample.Installer</string>
<key>MachServices</key>
<dict>
<key>com.erwanb.MachInjectSample.Installer.mach</key>
<true/>
</dict>
</dict>
</plist>
7 changes: 7 additions & 0 deletions mach_inject_example/Installer/Installer-Prefix.pch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'Installer' target in the 'Installer' project
//

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
66 changes: 66 additions & 0 deletions mach_inject_example/Installer/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// main.m
// Installer
//
// Created by Erwan Barrier on 8/11/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import <launch.h>
#import <Foundation/Foundation.h>

#import "DKFrameworkInstaller.h"

dispatch_source_t g_timer_source = NULL;

int main(int argc, const char * argv[])
{
@autoreleasepool {
// Init idle-exit timer
dispatch_queue_t mq = dispatch_get_main_queue();
g_timer_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mq);
assert(g_timer_source != NULL);

/* When the idle-exit timer fires, we just call exit(2) with status 0. */
dispatch_set_context(g_timer_source, NULL);
dispatch_source_set_event_handler_f(g_timer_source, (void (*)(void *))exit);
/* We start off with our timer armed. This is for the simple reason that,
* upon kicking off the GCD state engine, the first thing we'll get to is
* a connection on our socket which will disarm the timer. Remember, handling
* new connections and the firing of the idle-exit timer are synchronized.
*/
dispatch_time_t t0 = dispatch_time(DISPATCH_TIME_NOW, 5llu * NSEC_PER_SEC);
dispatch_source_set_timer(g_timer_source, t0, 0llu, 0llu);
dispatch_resume(g_timer_source);


// Check in mach service
launch_data_t req = launch_data_new_string(LAUNCH_KEY_CHECKIN);
assert(req != NULL);

launch_data_t resp = launch_msg(req);
assert(resp != NULL);
assert(launch_data_get_type(resp) == LAUNCH_DATA_DICTIONARY);

launch_data_t machs = launch_data_dict_lookup(resp, LAUNCH_JOBKEY_MACHSERVICES);
assert(machs != NULL);
assert(launch_data_get_type(machs) == LAUNCH_DATA_DICTIONARY);

launch_data_t machPortData = launch_data_dict_lookup(machs, "com.erwanb.MachInjectSample.Installer.mach");

mach_port_t mp = launch_data_get_machport(machPortData);
launch_data_free(req);
launch_data_free(resp);

NSMachPort *rp = [[NSMachPort alloc] initWithMachPort:mp];
NSConnection *c = [NSConnection connectionWithReceivePort:rp sendPort:nil];

DKFrameworkInstaller *installer = [DKFrameworkInstaller new];
[c setRootObject:installer];

[[NSRunLoop currentRunLoop] run];

return (0);
}
}

7 changes: 7 additions & 0 deletions mach_inject_example/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (C) 2012 Erwan Barrier

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading