|
| 1 | +#import "DeviceController.h" |
| 2 | +#include <spawn.h> |
| 3 | +#import <sys/sysctl.h> |
| 4 | +#import <Foundation/Foundation.h> |
| 5 | +//#import <FrontBoardServices/FBSSystemService.h> |
| 6 | + |
| 7 | +@implementation DeviceController |
| 8 | + |
| 9 | +#define POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE 1 |
| 10 | +extern int posix_spawnattr_set_persona_np(const posix_spawnattr_t* __restrict, uid_t, uint32_t); |
| 11 | +extern int posix_spawnattr_set_persona_uid_np(const posix_spawnattr_t* __restrict, uid_t); |
| 12 | +extern int posix_spawnattr_set_persona_gid_np(const posix_spawnattr_t* __restrict, uid_t); |
| 13 | + |
| 14 | +// @See https://github.com/opa334/TrollStore/blob/main/Shared/TSUtil.m#L297 |
| 15 | +- (void) Respring |
| 16 | +{ |
| 17 | + killall(@"SpringBoard", YES); |
| 18 | + exit(0); |
| 19 | +} |
| 20 | + |
| 21 | +- (Boolean) RebootCommCenter |
| 22 | +{ |
| 23 | +// killall(@"SpringBoard", YES); |
| 24 | +// exit(0); |
| 25 | + NSString *path = [[NSBundle mainBundle] pathForResource:@"MaintenanceHelper" ofType:@""]; |
| 26 | + |
| 27 | + NSArray *args = @[]; // 不需要任何额外参数 |
| 28 | + NSString *stdOut = nil; |
| 29 | + NSString *stdErr = nil; |
| 30 | + |
| 31 | + if (path == nil) { |
| 32 | + return NO; |
| 33 | + } |
| 34 | + |
| 35 | + int result = spawnRoot(path, args, &stdOut, &stdErr); |
| 36 | + if (result == 0) { |
| 37 | + return YES; |
| 38 | + } |
| 39 | + |
| 40 | + return NO; |
| 41 | +} |
| 42 | + |
| 43 | +// @See https://github.com/opa334/TrollStore/blob/main/Shared/TSUtil.m#L79 |
| 44 | +int spawnRoot(NSString* path, NSArray* args, NSString** stdOut, NSString** stdErr) |
| 45 | +{ |
| 46 | + NSMutableArray* argsM = args.mutableCopy ?: [NSMutableArray new]; |
| 47 | + [argsM insertObject:path atIndex:0]; |
| 48 | + |
| 49 | + NSUInteger argCount = [argsM count]; |
| 50 | + char **argsC = (char **)malloc((argCount + 1) * sizeof(char*)); |
| 51 | + |
| 52 | + for (NSUInteger i = 0; i < argCount; i++) |
| 53 | + { |
| 54 | + argsC[i] = strdup([[argsM objectAtIndex:i] UTF8String]); |
| 55 | + } |
| 56 | + argsC[argCount] = NULL; |
| 57 | + |
| 58 | + posix_spawnattr_t attr; |
| 59 | + posix_spawnattr_init(&attr); |
| 60 | + |
| 61 | + posix_spawnattr_set_persona_np(&attr, 99, POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE); |
| 62 | + posix_spawnattr_set_persona_uid_np(&attr, 0); |
| 63 | + posix_spawnattr_set_persona_gid_np(&attr, 0); |
| 64 | + |
| 65 | + posix_spawn_file_actions_t action; |
| 66 | + posix_spawn_file_actions_init(&action); |
| 67 | + |
| 68 | + int outErr[2]; |
| 69 | + if(stdErr) |
| 70 | + { |
| 71 | + pipe(outErr); |
| 72 | + posix_spawn_file_actions_adddup2(&action, outErr[1], STDERR_FILENO); |
| 73 | + posix_spawn_file_actions_addclose(&action, outErr[0]); |
| 74 | + } |
| 75 | + |
| 76 | + int out[2]; |
| 77 | + if(stdOut) |
| 78 | + { |
| 79 | + pipe(out); |
| 80 | + posix_spawn_file_actions_adddup2(&action, out[1], STDOUT_FILENO); |
| 81 | + posix_spawn_file_actions_addclose(&action, out[0]); |
| 82 | + } |
| 83 | + |
| 84 | + pid_t task_pid; |
| 85 | + int status = -200; |
| 86 | + int spawnError = posix_spawn(&task_pid, [path UTF8String], &action, &attr, (char* const*)argsC, NULL); |
| 87 | + posix_spawnattr_destroy(&attr); |
| 88 | + for (NSUInteger i = 0; i < argCount; i++) |
| 89 | + { |
| 90 | + free(argsC[i]); |
| 91 | + } |
| 92 | + free(argsC); |
| 93 | + |
| 94 | + if(spawnError != 0) |
| 95 | + { |
| 96 | + NSLog(@"posix_spawn error %d\n", spawnError); |
| 97 | + return spawnError; |
| 98 | + } |
| 99 | + |
| 100 | + __block volatile BOOL _isRunning = YES; |
| 101 | + NSMutableString* outString = [NSMutableString new]; |
| 102 | + NSMutableString* errString = [NSMutableString new]; |
| 103 | + dispatch_semaphore_t sema = 0; |
| 104 | + dispatch_queue_t logQueue; |
| 105 | + if(stdOut || stdErr) |
| 106 | + { |
| 107 | + logQueue = dispatch_queue_create("com.opa334.TrollStore.LogCollector", NULL); |
| 108 | + sema = dispatch_semaphore_create(0); |
| 109 | + |
| 110 | + int outPipe = out[0]; |
| 111 | + int outErrPipe = outErr[0]; |
| 112 | + |
| 113 | + __block BOOL outEnabled = (BOOL)stdOut; |
| 114 | + __block BOOL errEnabled = (BOOL)stdErr; |
| 115 | + dispatch_async(logQueue, ^ |
| 116 | + { |
| 117 | + while(_isRunning) |
| 118 | + { |
| 119 | + @autoreleasepool |
| 120 | + { |
| 121 | + if(outEnabled) |
| 122 | + { |
| 123 | + [outString appendString:getNSStringFromFile(outPipe)]; |
| 124 | + } |
| 125 | + if(errEnabled) |
| 126 | + { |
| 127 | + [errString appendString:getNSStringFromFile(outErrPipe)]; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + dispatch_semaphore_signal(sema); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + do |
| 136 | + { |
| 137 | + if (waitpid(task_pid, &status, 0) != -1) { |
| 138 | + NSLog(@"Child status %d", WEXITSTATUS(status)); |
| 139 | + } else |
| 140 | + { |
| 141 | + perror("waitpid"); |
| 142 | + _isRunning = NO; |
| 143 | + return -222; |
| 144 | + } |
| 145 | + } while (!WIFEXITED(status) && !WIFSIGNALED(status)); |
| 146 | + |
| 147 | + _isRunning = NO; |
| 148 | + if(stdOut || stdErr) |
| 149 | + { |
| 150 | + if(stdOut) |
| 151 | + { |
| 152 | + close(out[1]); |
| 153 | + } |
| 154 | + if(stdErr) |
| 155 | + { |
| 156 | + close(outErr[1]); |
| 157 | + } |
| 158 | + |
| 159 | + // wait for logging queue to finish |
| 160 | + dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); |
| 161 | + |
| 162 | + if(stdOut) |
| 163 | + { |
| 164 | + *stdOut = outString.copy; |
| 165 | + } |
| 166 | + if(stdErr) |
| 167 | + { |
| 168 | + *stdErr = errString.copy; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return WEXITSTATUS(status); |
| 173 | +} |
| 174 | + |
| 175 | +NSString* getNSStringFromFile(int fd) |
| 176 | +{ |
| 177 | + NSMutableString* ms = [NSMutableString new]; |
| 178 | + ssize_t num_read; |
| 179 | + char c; |
| 180 | + if(!fd_is_valid(fd)) return @""; |
| 181 | + while((num_read = read(fd, &c, sizeof(c)))) |
| 182 | + { |
| 183 | + [ms appendString:[NSString stringWithFormat:@"%c", c]]; |
| 184 | + if(c == '\n') break; |
| 185 | + } |
| 186 | + return ms.copy; |
| 187 | +} |
| 188 | + |
| 189 | +int fd_is_valid(int fd) |
| 190 | +{ |
| 191 | + return fcntl(fd, F_GETFD) != -1 || errno != EBADF; |
| 192 | +} |
| 193 | + |
| 194 | +// @See https://github.com/opa334/TrollStore/blob/main/Shared/TSUtil.m#L279 |
| 195 | +void killall(NSString* processName, BOOL softly) |
| 196 | +{ |
| 197 | + enumerateProcessesUsingBlock(^(pid_t pid, NSString* executablePath, BOOL* stop) |
| 198 | + { |
| 199 | + if([executablePath.lastPathComponent isEqualToString:processName]) |
| 200 | + { |
| 201 | + if(softly) |
| 202 | + { |
| 203 | + kill(pid, SIGTERM); |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + kill(pid, SIGKILL); |
| 208 | + } |
| 209 | + } |
| 210 | + }); |
| 211 | +} |
| 212 | + |
| 213 | +void enumerateProcessesUsingBlock(void (^enumerator)(pid_t pid, NSString* executablePath, BOOL* stop)) |
| 214 | +{ |
| 215 | + static int maxArgumentSize = 0; |
| 216 | + if (maxArgumentSize == 0) { |
| 217 | + size_t size = sizeof(maxArgumentSize); |
| 218 | + if (sysctl((int[]){ CTL_KERN, KERN_ARGMAX }, 2, &maxArgumentSize, &size, NULL, 0) == -1) { |
| 219 | + perror("sysctl argument size"); |
| 220 | + maxArgumentSize = 4096; // Default |
| 221 | + } |
| 222 | + } |
| 223 | + int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL}; |
| 224 | + struct kinfo_proc *info; |
| 225 | + size_t length; |
| 226 | + int count; |
| 227 | + |
| 228 | + if (sysctl(mib, 3, NULL, &length, NULL, 0) < 0) |
| 229 | + return; |
| 230 | + if (!(info = malloc(length))) |
| 231 | + return; |
| 232 | + if (sysctl(mib, 3, info, &length, NULL, 0) < 0) { |
| 233 | + free(info); |
| 234 | + return; |
| 235 | + } |
| 236 | + count = length / sizeof(struct kinfo_proc); |
| 237 | + for (int i = 0; i < count; i++) { |
| 238 | + @autoreleasepool { |
| 239 | + pid_t pid = info[i].kp_proc.p_pid; |
| 240 | + if (pid == 0) { |
| 241 | + continue; |
| 242 | + } |
| 243 | + size_t size = maxArgumentSize; |
| 244 | + char* buffer = (char *)malloc(length); |
| 245 | + if (sysctl((int[]){ CTL_KERN, KERN_PROCARGS2, pid }, 3, buffer, &size, NULL, 0) == 0) { |
| 246 | + NSString* executablePath = [NSString stringWithCString:(buffer+sizeof(int)) encoding:NSUTF8StringEncoding]; |
| 247 | + |
| 248 | + BOOL stop = NO; |
| 249 | + enumerator(pid, executablePath, &stop); |
| 250 | + if(stop) |
| 251 | + { |
| 252 | + free(buffer); |
| 253 | + break; |
| 254 | + } |
| 255 | + } |
| 256 | + free(buffer); |
| 257 | + } |
| 258 | + } |
| 259 | + free(info); |
| 260 | +} |
| 261 | + |
| 262 | +@end |
0 commit comments