Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 118 lines (96 sloc) 2.986 kb
ee99867 August "Gus" Mueller oh geeze
authored
1 /*******************************************************************************
2 mach_inject_bundle_stub.c
3 Copyright (c) 2005 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
4 Some rights reserved: <http://creativecommons.org/licenses/by/2.0/>
5
6 Design inspired by SCPatchLoader, by Jon Gotow of St. Clair Software:
7 <http://www.stclairsoft.com>
8
9 ***************************************************************************/
10
11 #include "mach_inject_bundle_stub.h"
12 #include "load_bundle.h"
13 #include "mach_inject.h" // for INJECT_ENTRY
14
15 #include <assert.h>
16 #include <mach/mach_init.h>
17 #include <mach/thread_act.h>
18 #include <pthread.h>
19 #include <Carbon/Carbon.h>
20
21 /**************************
22 *
23 * Funky Protos
24 *
25 **************************/
26 #pragma mark (Funky Protos)
27
28 void
29 INJECT_ENTRY(
30 ptrdiff_t codeOffset,
31 mach_inject_bundle_stub_param *param,
32 size_t paramSize,
33 char *dummy_pthread_struc );
34
35 void*
36 pthread_entry(
37 mach_inject_bundle_stub_param *param );
38
39 pascal
40 void
41 EventLoopTimerEntry(
42 EventLoopTimerRef inTimer,
43 mach_inject_bundle_stub_param *param );
44
45 /*******************************************************************************
46 *
47 * Implementation
48 *
49 *******************************************************************************/
50 #pragma mark -
51 #pragma mark (Implementation)
52
53 void
54 INJECT_ENTRY(
55 ptrdiff_t codeOffset,
56 mach_inject_bundle_stub_param *param,
57 size_t paramSize,
58 char *dummy_pthread_struct )
59 {
60 assert( param );
61
62 param->codeOffset = codeOffset;
63
64 #if defined (__i386__)
65 // On intel, per-pthread data is a zone of data that must be allocated.
66 // if not, all function trying to access per-pthread data (all mig functions for instance)
67 // will crash.
68 extern void __pthread_set_self(char*);
69 __pthread_set_self(dummy_pthread_struct);
70 #endif
71
72 pthread_attr_t attr;
73 pthread_attr_init(&attr);
74
75 int policy;
76 pthread_attr_getschedpolicy( &attr, &policy );
77 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
78 pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
79
80 struct sched_param sched;
81 sched.sched_priority = sched_get_priority_max( policy );
82 pthread_attr_setschedparam( &attr, &sched );
83
84 pthread_t thread;
85 pthread_create( &thread,
86 &attr,
87 (void* (*)(void*))((long)pthread_entry + codeOffset),
88 (void*) param );
89 pthread_attr_destroy(&attr);
90
91 thread_suspend(mach_thread_self());
92 }
93
94 void*
95 pthread_entry(
96 mach_inject_bundle_stub_param *param )
97 {
98 assert( param );
99
100 EventLoopTimerProcPtr proc = (EventLoopTimerProcPtr) EventLoopTimerEntry;
101 proc += param->codeOffset;
102 EventLoopTimerUPP upp = NewEventLoopTimerUPP( proc );
103
104 InstallEventLoopTimer( GetMainEventLoop(), 0, 0, upp, (void*)param, NULL );
105 return NULL;
106 }
107
108 pascal
109 void
110 EventLoopTimerEntry(
111 EventLoopTimerRef inTimer,
112 mach_inject_bundle_stub_param *param )
113 {
114 assert( inTimer );
115 assert( param );
116
117 load_bundle_package( param->bundlePackageFileSystemRepresentation );
118 }
Something went wrong with that request. Please try again.