-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodesign_parallel.c
More file actions
377 lines (299 loc) · 8.95 KB
/
codesign_parallel.c
File metadata and controls
377 lines (299 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* codesign_parallel - Parallel wrapper around macOS codesign binary
* Distributes code signing work across multiple CPU cores for improved throughput
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sysctl.h>
#include <spawn.h>
// Yeah, hardcoded, because it is hard to imagine it being anywhere else on macOS
#define CODESIGN_PATH "/usr/bin/codesign"
static pid_t *worker_pids = NULL;
static int worker_count = 0;
static volatile bool is_signal_received = false;
static bool should_fail_fast = false;
static bool should_be_verbose = false;
static int parallel_count;
// Arguments to pass to codesign
char **codesign_argv;
int codesign_argc;
// List of binaries to sign
char **binaries;
int binaries_count;
// Per-worker assignment structure
typedef struct {
int start_index;
int count;
} worker_assignment;
void help() {
fprintf(stderr, "Usage: codesign_parallel [codesign_parallel options] [codesign options] -- BINARY1 [BINARY2 ...]\n");
}
static int get_cpu_count(void) {
int mib[2] = { CTL_HW, HW_NCPU };
int ncpu;
size_t len = sizeof(ncpu);
if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) {
// sysctl failed?!
return 1;
}
return ncpu > 0 ? ncpu : 1;
}
// Check if verbose flag is present in arguments and set should_be_verbose
static void check_if_verbose_specified(int argc, char **argv) {
for (int i = 0; i < argc; i++) {
if (strncmp(argv[i], "-v", 2) == 0 || strncmp(argv[i], "--v", 3) == 0) {
should_be_verbose = true;
return;
}
}
}
static int parse_codesign_args(char **argv, int delimiter_index) {
codesign_argv = malloc(sizeof(char *) * (delimiter_index + 1));
if (!codesign_argv) {
fprintf(stderr, "codesign_parallel: error: memory allocation failed\n");
return -1;
}
// Parse arguments before delimiter
codesign_argc = 0;
for (int i = 1; i < delimiter_index; i++) {
if (strncmp(argv[i], "--parallel=", 11) == 0) {
parallel_count = atoi(argv[i] + 11);
if (parallel_count <= 0) {
fprintf(stderr, "codesign_parallel: error: invalid --parallel value\n");
free(codesign_argv);
return -1;
}
} else if (strcmp(argv[i], "--fail-fast") == 0) {
should_fail_fast = true;
} else {
// Pass through to codesign
codesign_argv[codesign_argc++] = argv[i];
}
}
codesign_argv[codesign_argc] = NULL;
return 0;
}
static int parse_args(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
help();
return -1;
}
}
// Find the delimiter
int delimiter_index = -1;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--") == 0) {
delimiter_index = i;
break;
}
}
if (delimiter_index == -1) {
fprintf(stderr, "codesign_parallel: error: missing '--' delimiter before binary list\n");
help();
return -1;
}
// Count binaries after delimiter
binaries_count = argc - delimiter_index - 1;
if (binaries_count == 0) {
fprintf(stderr, "codesign_parallel: warning: no binaries specified\n");
return 0;
}
// Set binaries pointer
binaries = &argv[delimiter_index + 1];
return parse_codesign_args(argv, delimiter_index);
}
static int validate_codesign_binary(void) {
if (access(CODESIGN_PATH, X_OK) != 0) {
fprintf(stderr, "codesign_parallel: error: %s does not exists or is not executable: %s\n", CODESIGN_PATH, strerror(errno));
return -1;
}
return 0;
}
static void send_signal_to_all_workers(int sig) {
for (int i = 0; i < worker_count; i++) {
if (worker_pids[i] > 0) {
kill(worker_pids[i], sig);
}
}
}
static void signal_handler(int sig) {
is_signal_received = true;
send_signal_to_all_workers(sig);
}
// Calculate contiguous chunk assignments for workers
static worker_assignment *calculate_assignments(int binaries_count, int cpu_count) {
worker_count = cpu_count < binaries_count ? cpu_count : binaries_count;
int count = binaries_count / worker_count;
int reminder = binaries_count % worker_count;
// Print summary if verbose
if (should_be_verbose) {
fprintf(stderr, "codesign_parallel: Signing %d binaries across %d workers (%d per worker", binaries_count, worker_count, count);
if (reminder > 0) {
fprintf(stderr, " + %d extra for the last worker)", reminder);
} else {
fprintf(stderr, ")");
}
fprintf(stderr, "\n");
}
worker_assignment *assignments = malloc(sizeof(worker_assignment) * worker_count);
if (!assignments) {
fprintf(stderr, "codesign_parallel: error: memory allocation failed\n");
return NULL;
}
int current_index = 0;
for (int i = 0; i < worker_count; i++) {
assignments[i].start_index = current_index;
assignments[i].count = count;
if (i == worker_count - 1) {
assignments[i].count += reminder;
}
if (current_index + assignments[i].count > binaries_count) {
assignments[i].count = binaries_count - current_index;
}
current_index += assignments[i].count;
}
return assignments;
}
// Spawn a single worker process
// The worker signs binaries from start_index to start_index + count - 1
static pid_t spawn_worker(int start_index, int count) {
extern char **environ;
// Build argv for codesign: codesign [args...] binary1 binary2 ... NULL
int total_argc = 1 + codesign_argc + count + 1;
char **child_argv = malloc(sizeof(char *) * total_argc);
if (!child_argv) {
fprintf(stderr, "codesign_parallel: error: memory allocation failed\n");
return -1;
}
int idx = 0;
child_argv[idx++] = CODESIGN_PATH;
// Add codesign arguments
for (int i = 0; i < codesign_argc; i++) {
child_argv[idx++] = codesign_argv[i];
}
// Add binaries for this worker
for (int i = 0; i < count; i++) {
child_argv[idx++] = binaries[start_index + i];
}
child_argv[idx] = NULL;
// Need to dump the argv for debugging?
// for (int i = 0; i < idx; i++) {
// printf("%s ", child_argv[i]);
// }
// printf("\n");
// Spawn!
pid_t pid;
int ret = posix_spawn(&pid, CODESIGN_PATH, NULL, NULL, child_argv, environ);
free(child_argv);
if (ret != 0) {
fprintf(stderr, "codesign_parallel: error: failed to spawn worker: %s\n", strerror(ret));
return -1;
}
return pid;
}
static int spawn_workers(const worker_assignment *assignments) {
worker_pids = calloc(worker_count, sizeof(pid_t));
if (!worker_pids) {
fprintf(stderr, "codesign_parallel: error: memory allocation failed\n");
return -1;
}
for (int i = 0; i < worker_count; i++) {
pid_t pid = spawn_worker(assignments[i].start_index, assignments[i].count);
if (pid < 0) {
// Kill already spawned workers
send_signal_to_all_workers(SIGTERM);
return -1;
}
worker_pids[i] = pid;
}
return 0;
}
static int wait_for_workers_to_finish() {
int failed_worker_count = 0;
int remaining_worker_count = worker_count;
while (remaining_worker_count > 0) {
int status;
pid_t pid = wait(&status);
if (pid < 0) {
if (errno == EINTR) {
// Interrupted by signal, continue waiting
continue;
}
// No more children
break;
}
remaining_worker_count--;
// Check exit status
bool is_worker_failed = false;
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0) {
is_worker_failed = true;
}
} else if (WIFSIGNALED(status)) {
is_worker_failed = true;
}
if (is_worker_failed) {
failed_worker_count = 1;
if (should_fail_fast && remaining_worker_count > 0) {
// Kill remaining workers
send_signal_to_all_workers(SIGTERM);
}
}
// Mark this PID as done
for (int i = 0; i < worker_count; i++) {
if (worker_pids[i] == pid) {
worker_pids[i] = 0;
break;
}
}
}
return failed_worker_count;
}
static void setup_signal_handlers(void) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
int main(int argc, char **argv) {
if (argc < 2) {
help();
return 1;
}
if (parse_args(argc, argv) != 0) {
return 1;
}
check_if_verbose_specified(codesign_argc, codesign_argv);
if (binaries_count == 0) {
return 0;
}
if (validate_codesign_binary() != 0) {
return 1;
}
int cpu_count = parallel_count > 0 ? parallel_count : get_cpu_count();
worker_assignment *assignments = calculate_assignments(binaries_count, cpu_count);
setup_signal_handlers();
if (spawn_workers(assignments) != 0) {
return 1;
}
// Not that we actually need to free this memory, given the fact that we shall exit soon anyway,
// but let'd be a good citizen.
free(assignments);
int ret = wait_for_workers_to_finish();
// Check if we received a signal
if (is_signal_received) {
return 1;
}
return ret;
}