|
1 | 1 | #import <Foundation/Foundation.h>
|
2 | 2 | #import <xpc/xpc.h>
|
3 | 3 |
|
4 |
| -#define ROUTINE_LOAD 800 |
| 4 | +#define ROUTINE_LOAD 800 |
5 | 5 | #define ROUTINE_UNLOAD 801
|
6 | 6 |
|
7 | 7 | struct _os_alloc_once_s {
|
8 |
| - long once; |
9 |
| - void *ptr; |
| 8 | + long once; |
| 9 | + void *ptr; |
10 | 10 | };
|
11 | 11 |
|
12 | 12 | struct xpc_global_data {
|
13 |
| - uint64_t a; |
14 |
| - uint64_t xpc_flags; |
15 |
| - mach_port_t task_bootstrap_port; /* 0x10 */ |
| 13 | + uint64_t a; |
| 14 | + uint64_t xpc_flags; |
| 15 | + mach_port_t task_bootstrap_port; /* 0x10 */ |
16 | 16 | #ifndef _64
|
17 |
| - uint32_t padding; |
| 17 | + uint32_t padding; |
18 | 18 | #endif
|
19 |
| - xpc_object_t xpc_bootstrap_pipe; /* 0x18 */ |
20 |
| - // and there's more, but you'll have to wait for MOXiI 2 for those... |
21 |
| - // ... |
| 19 | + xpc_object_t xpc_bootstrap_pipe; /* 0x18 */ |
| 20 | + // and there's more, but you'll have to wait for MOXiI 2 for those... |
| 21 | + // ... |
22 | 22 | };
|
23 | 23 |
|
24 | 24 | extern struct _os_alloc_once_s _os_alloc_once_table[];
|
25 |
| -extern void* _os_alloc_once(struct _os_alloc_once_s *slot, size_t sz, os_function_t init); |
| 25 | +extern void *_os_alloc_once(struct _os_alloc_once_s *slot, size_t sz, |
| 26 | + os_function_t init); |
26 | 27 |
|
27 |
| -xpc_object_t launchd_xpc_send_message(xpc_object_t xdict) |
28 |
| -{ |
29 |
| - void* pipePtr = NULL; |
30 |
| - |
31 |
| - if(_os_alloc_once_table[1].once == -1) |
32 |
| - { |
33 |
| - pipePtr = _os_alloc_once_table[1].ptr; |
34 |
| - } |
35 |
| - else |
36 |
| - { |
37 |
| - pipePtr = _os_alloc_once(&_os_alloc_once_table[1], 472, NULL); |
38 |
| - if (!pipePtr) _os_alloc_once_table[1].once = -1; |
39 |
| - } |
| 28 | +xpc_object_t launchd_xpc_send_message(xpc_object_t xdict) { |
| 29 | + void *pipePtr = NULL; |
40 | 30 |
|
41 |
| - xpc_object_t xreply = nil; |
42 |
| - if (pipePtr) { |
43 |
| - struct xpc_global_data* globalData = pipePtr; |
44 |
| - xpc_object_t pipe = globalData->xpc_bootstrap_pipe; |
45 |
| - if (pipe) { |
46 |
| - int err = xpc_pipe_routine_with_flags(pipe, xdict, &xreply, 0); |
47 |
| - if (err != 0) { |
48 |
| - return nil; |
49 |
| - } |
50 |
| - } |
| 31 | + if (_os_alloc_once_table[1].once == -1) { |
| 32 | + pipePtr = _os_alloc_once_table[1].ptr; |
| 33 | + } else { |
| 34 | + pipePtr = _os_alloc_once(&_os_alloc_once_table[1], 472, NULL); |
| 35 | + if (!pipePtr) |
| 36 | + _os_alloc_once_table[1].once = -1; |
| 37 | + } |
| 38 | + |
| 39 | + xpc_object_t xreply = nil; |
| 40 | + if (pipePtr) { |
| 41 | + struct xpc_global_data *globalData = pipePtr; |
| 42 | + xpc_object_t pipe = globalData->xpc_bootstrap_pipe; |
| 43 | + if (pipe) { |
| 44 | + int err = xpc_pipe_routine_with_flags(pipe, xdict, &xreply, 0); |
| 45 | + if (err != 0) { |
| 46 | + return nil; |
| 47 | + } |
51 | 48 | }
|
52 |
| - return xreply; |
| 49 | + } |
| 50 | + return xreply; |
53 | 51 | }
|
54 | 52 |
|
| 53 | +int64_t launchctl_load(const char *plistPath, bool unload) { |
| 54 | + xpc_object_t pathArray = xpc_array_create_empty(); |
| 55 | + xpc_array_set_string(pathArray, XPC_ARRAY_APPEND, plistPath); |
| 56 | + |
| 57 | + xpc_object_t msgDictionary = xpc_dictionary_create_empty(); |
| 58 | + xpc_dictionary_set_uint64(msgDictionary, "subsystem", 3); |
| 59 | + xpc_dictionary_set_uint64(msgDictionary, "handle", 0); |
| 60 | + xpc_dictionary_set_uint64(msgDictionary, "type", 1); |
| 61 | + xpc_dictionary_set_bool(msgDictionary, "legacy-load", true); |
| 62 | + xpc_dictionary_set_bool(msgDictionary, "enable", false); |
| 63 | + xpc_dictionary_set_uint64(msgDictionary, "routine", |
| 64 | + unload ? ROUTINE_UNLOAD : ROUTINE_LOAD); |
| 65 | + xpc_dictionary_set_value(msgDictionary, "paths", pathArray); |
| 66 | + |
| 67 | + xpc_object_t msgReply = launchd_xpc_send_message(msgDictionary); |
| 68 | + |
| 69 | + char *msgReplyDescription = xpc_copy_description(msgReply); |
| 70 | + NSLog(@"[jbinit] msgReply = %s\n", msgReplyDescription); |
| 71 | + free(msgReplyDescription); |
| 72 | + |
| 73 | + int64_t bootstrapError = |
| 74 | + xpc_dictionary_get_int64(msgReply, "bootstrap-error"); |
| 75 | + if (bootstrapError != 0) { |
| 76 | + NSLog(@"[jbinit] bootstrap-error = %s\n", |
| 77 | + xpc_strerror((int32_t)bootstrapError)); |
| 78 | + return bootstrapError; |
| 79 | + } |
55 | 80 |
|
56 |
| -int64_t launchctl_load(const char* plistPath, bool unload) |
57 |
| -{ |
58 |
| - xpc_object_t pathArray = xpc_array_create_empty(); |
59 |
| - xpc_array_set_string(pathArray, XPC_ARRAY_APPEND, plistPath); |
60 |
| - |
61 |
| - xpc_object_t msgDictionary = xpc_dictionary_create_empty(); |
62 |
| - xpc_dictionary_set_uint64(msgDictionary, "subsystem", 3); |
63 |
| - xpc_dictionary_set_uint64(msgDictionary, "handle", 0); |
64 |
| - xpc_dictionary_set_uint64(msgDictionary, "type", 1); |
65 |
| - xpc_dictionary_set_bool(msgDictionary, "legacy-load", true); |
66 |
| - xpc_dictionary_set_bool(msgDictionary, "enable", false); |
67 |
| - xpc_dictionary_set_uint64(msgDictionary, "routine", unload ? ROUTINE_UNLOAD : ROUTINE_LOAD); |
68 |
| - xpc_dictionary_set_value(msgDictionary, "paths", pathArray); |
69 |
| - |
70 |
| - xpc_object_t msgReply = launchd_xpc_send_message(msgDictionary); |
| 81 | + int64_t error = xpc_dictionary_get_int64(msgReply, "error"); |
| 82 | + if (error != 0) { |
| 83 | + NSLog(@"[jbinit]error = %s\n", xpc_strerror((int32_t)error)); |
| 84 | + return error; |
| 85 | + } |
71 | 86 |
|
72 |
| - char *msgReplyDescription = xpc_copy_description(msgReply); |
73 |
| - printf("msgReply = %s\n", msgReplyDescription); |
74 |
| - free(msgReplyDescription); |
75 |
| - |
76 |
| - int64_t bootstrapError = xpc_dictionary_get_int64(msgReply, "bootstrap-error"); |
77 |
| - if(bootstrapError != 0) |
78 |
| - { |
79 |
| - printf("bootstrap-error = %s\n", xpc_strerror((int32_t)bootstrapError)); |
80 |
| - return bootstrapError; |
81 |
| - } |
82 |
| - |
83 |
| - int64_t error = xpc_dictionary_get_int64(msgReply, "error"); |
84 |
| - if(error != 0) |
85 |
| - { |
86 |
| - printf("error = %s\n", xpc_strerror((int32_t)error)); |
87 |
| - return error; |
88 |
| - } |
89 |
| - |
90 |
| - // launchctl seems to do extra things here |
91 |
| - // like getting the audit token via xpc_dictionary_get_audit_token |
92 |
| - // or sometimes also getting msgReply["req_pid"] and msgReply["rec_execcnt"] |
93 |
| - // but we don't really care about that here |
| 87 | + // launchctl seems to do extra things here |
| 88 | + // like getting the audit token via xpc_dictionary_get_audit_token |
| 89 | + // or sometimes also getting msgReply["req_pid"] and msgReply["rec_execcnt"] |
| 90 | + // but we don't really care about that here |
94 | 91 |
|
95 |
| - return 0; |
| 92 | + return 0; |
96 | 93 | }
|
0 commit comments