|
| 1 | +/* |
| 2 | + * The process-table scan behind the reader's system surface host check. It runs in the reader's own |
| 3 | + * process, so the check spawns nothing. |
| 4 | + */ |
| 5 | + |
| 6 | +#import "SnapshotBridgeSurfaceHosts.h" |
| 7 | + |
| 8 | +#import <errno.h> |
| 9 | +#import <stdlib.h> |
| 10 | +#import <string.h> |
| 11 | +#import <sys/sysctl.h> |
| 12 | + |
| 13 | +typedef NS_ENUM(NSInteger, SurfaceHostScan) { |
| 14 | + SurfaceHostAbsent, |
| 15 | + SurfaceHostPresent, |
| 16 | + SurfaceHostUnverified, |
| 17 | +}; |
| 18 | + |
| 19 | +static const char kSimulatorUdidPrefix[] = "SIMULATOR_UDID="; |
| 20 | + |
| 21 | +/* |
| 22 | + * Whether a process has exited: gone from the process table, or a zombie waiting to be reaped. The |
| 23 | + * kernel keeps no arguments for either, and neither presents anything, so a host that exited |
| 24 | + * between the table read and its argument read is absent, not unplaceable. |
| 25 | + */ |
| 26 | +static BOOL processExited(pid_t pid) |
| 27 | +{ |
| 28 | + int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; |
| 29 | + struct kinfo_proc process; |
| 30 | + size_t size = sizeof(process); |
| 31 | + if (sysctl(mib, 4, &process, &size, NULL, 0) != 0) return errno == ESRCH; |
| 32 | + return size == 0 || process.kp_proc.p_stat == SZOMB; |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | + * Whether one process runs this host's binary for this Simulator. KERN_PROCARGS2 holds argc, the |
| 37 | + * executable path, then argv and envp as NUL-terminated strings. Simulator processes carry |
| 38 | + * SIMULATOR_UDID, so only a scope naming another device is a negative. A live process whose |
| 39 | + * arguments cannot be read, or whose environment carries no scope, proves nothing. |
| 40 | + */ |
| 41 | +static SurfaceHostScan scanProcess(pid_t pid, const char *executable, const char *udidAssignment) |
| 42 | +{ |
| 43 | + int argumentMaximum = 0; |
| 44 | + size_t size = sizeof(argumentMaximum); |
| 45 | + int argumentMaximumMib[2] = {CTL_KERN, KERN_ARGMAX}; |
| 46 | + if (sysctl(argumentMaximumMib, 2, &argumentMaximum, &size, NULL, 0) != 0 || argumentMaximum <= 0) { |
| 47 | + return SurfaceHostUnverified; |
| 48 | + } |
| 49 | + char *buffer = malloc((size_t)argumentMaximum); |
| 50 | + if (!buffer) return SurfaceHostUnverified; |
| 51 | + int mib[3] = {CTL_KERN, KERN_PROCARGS2, pid}; |
| 52 | + size = (size_t)argumentMaximum; |
| 53 | + if (sysctl(mib, 3, buffer, &size, NULL, 0) != 0 || size <= sizeof(int)) { |
| 54 | + free(buffer); |
| 55 | + return processExited(pid) ? SurfaceHostAbsent : SurfaceHostUnverified; |
| 56 | + } |
| 57 | + int argc = 0; |
| 58 | + memcpy(&argc, buffer, sizeof(argc)); |
| 59 | + if (argc < 0) { |
| 60 | + free(buffer); |
| 61 | + return SurfaceHostUnverified; |
| 62 | + } |
| 63 | + const char *cursor = buffer + sizeof(int); |
| 64 | + const char *end = buffer + size; |
| 65 | + size_t pathLength = strnlen(cursor, (size_t)(end - cursor)); |
| 66 | + if (cursor + pathLength >= end) { |
| 67 | + free(buffer); |
| 68 | + return SurfaceHostUnverified; |
| 69 | + } |
| 70 | + // The fragment must end the executable path at a path component, so a sibling such as |
| 71 | + // `Name.app/NameHelper` or `OtherName.app/Name` is not the host. |
| 72 | + size_t fragmentLength = strlen(executable); |
| 73 | + const char *suffix = pathLength >= fragmentLength ? cursor + pathLength - fragmentLength : NULL; |
| 74 | + if (!suffix || strcmp(suffix, executable) != 0 || (suffix > cursor && suffix[-1] != '/')) { |
| 75 | + free(buffer); |
| 76 | + return SurfaceHostAbsent; |
| 77 | + } |
| 78 | + cursor += pathLength; |
| 79 | + while (cursor < end && *cursor == '\0') cursor += 1; |
| 80 | + for (int skipped = 0; skipped < argc && cursor < end; skipped += 1) { |
| 81 | + cursor += strnlen(cursor, (size_t)(end - cursor)) + 1; |
| 82 | + } |
| 83 | + if (cursor > end) cursor = end; |
| 84 | + // The kernel may pad argv before the environment. |
| 85 | + while (cursor < end && *cursor == '\0') cursor += 1; |
| 86 | + // A matching scope anywhere in the environment places the process here. Another scope proves |
| 87 | + // absence only when the walk reached the environment's end, so an entry order or a truncated |
| 88 | + // block cannot hide this Simulator's scope. |
| 89 | + BOOL sawOtherScope = NO; |
| 90 | + BOOL walkedToEnd = NO; |
| 91 | + while (cursor < end) { |
| 92 | + if (*cursor == '\0') { |
| 93 | + walkedToEnd = YES; |
| 94 | + break; |
| 95 | + } |
| 96 | + size_t length = strnlen(cursor, (size_t)(end - cursor)); |
| 97 | + if (cursor + length >= end) break; |
| 98 | + if (strncmp(cursor, kSimulatorUdidPrefix, sizeof(kSimulatorUdidPrefix) - 1) == 0) { |
| 99 | + if (strcmp(cursor, udidAssignment) == 0) { |
| 100 | + free(buffer); |
| 101 | + return SurfaceHostPresent; |
| 102 | + } |
| 103 | + sawOtherScope = YES; |
| 104 | + } |
| 105 | + cursor += length + 1; |
| 106 | + } |
| 107 | + // The last entry's terminator can be the block's last byte. |
| 108 | + if (cursor == end) walkedToEnd = YES; |
| 109 | + free(buffer); |
| 110 | + return sawOtherScope && walkedToEnd ? SurfaceHostAbsent : SurfaceHostUnverified; |
| 111 | +} |
| 112 | + |
| 113 | +/* |
| 114 | + * The first registered host running for this Simulator, or nil. The kernel keeps the executable's |
| 115 | + * basename cut to MAXCOMLEN as the process name, so the name selects the few candidates whose |
| 116 | + * arguments are read. `unverified` reports a candidate the scan could not place. |
| 117 | + */ |
| 118 | +NSString *_Nullable runningSurfaceHost(NSArray<NSString *> *executables, |
| 119 | + NSString *simulatorUdid, |
| 120 | + BOOL *unverified) |
| 121 | +{ |
| 122 | + *unverified = NO; |
| 123 | + int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; |
| 124 | + struct kinfo_proc *processes = NULL; |
| 125 | + size_t length = 0; |
| 126 | + for (int attempt = 0; attempt < 3 && !processes; attempt += 1) { |
| 127 | + if (sysctl(mib, 3, NULL, &length, NULL, 0) != 0) break; |
| 128 | + // Room for processes that start between the size read and the copy. |
| 129 | + length += 32 * sizeof(struct kinfo_proc); |
| 130 | + processes = malloc(length); |
| 131 | + if (!processes) break; |
| 132 | + if (sysctl(mib, 3, processes, &length, NULL, 0) == 0) break; |
| 133 | + const int error = errno; |
| 134 | + free(processes); |
| 135 | + processes = NULL; |
| 136 | + if (error != ENOMEM) break; |
| 137 | + } |
| 138 | + if (!processes) { |
| 139 | + *unverified = YES; |
| 140 | + return nil; |
| 141 | + } |
| 142 | + NSString *udidAssignment = [NSString stringWithFormat:@"%s%@", kSimulatorUdidPrefix, simulatorUdid]; |
| 143 | + NSString *running = nil; |
| 144 | + size_t count = length / sizeof(struct kinfo_proc); |
| 145 | + for (NSString *executable in executables) { |
| 146 | + const char *processName = executable.lastPathComponent.UTF8String; |
| 147 | + for (size_t index = 0; index < count && !running; index += 1) { |
| 148 | + if (strncmp(processes[index].kp_proc.p_comm, processName, MAXCOMLEN) != 0) continue; |
| 149 | + if (processes[index].kp_proc.p_stat == SZOMB) continue; |
| 150 | + SurfaceHostScan scan = scanProcess(processes[index].kp_proc.p_pid, executable.UTF8String, |
| 151 | + udidAssignment.UTF8String); |
| 152 | + if (scan == SurfaceHostPresent) running = executable; |
| 153 | + if (scan == SurfaceHostUnverified) *unverified = YES; |
| 154 | + } |
| 155 | + if (running) break; |
| 156 | + } |
| 157 | + free(processes); |
| 158 | + return running; |
| 159 | +} |
0 commit comments