|
1 | 1 | #include "restart_frontboard.h" |
2 | 2 |
|
3 | | -#include <stdlib.h> |
| 3 | + |
4 | 4 | #include <string.h> |
5 | 5 | #include <unistd.h> |
6 | 6 | #include <signal.h> |
7 | 7 | #include <stdio.h> |
| 8 | +#include <spawn.h> |
| 9 | +#include <sys/wait.h> |
| 10 | +#include <errno.h> |
8 | 11 |
|
9 | 12 | // Simpler implementation for CI/iOS SDK: avoid using libproc APIs |
10 | 13 | // which may not be available in the iOS SDK on the build host. |
11 | 14 | // Use a best-effort `killall` invocation instead. |
12 | 15 |
|
| 16 | +static int spawn_and_wait(char *const argv[]) { |
| 17 | + pid_t pid = 0; |
| 18 | + int spawn_err = posix_spawnp(&pid, "killall", NULL, NULL, argv, NULL); |
| 19 | + if (spawn_err != 0) return -1; |
| 20 | + |
| 21 | + int status = 0; |
| 22 | + if (waitpid(pid, &status, 0) == -1) return -1; |
| 23 | + |
| 24 | + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) return 0; |
| 25 | + return -1; |
| 26 | +} |
| 27 | + |
13 | 28 | int restart_frontboard_kill(void) { |
14 | | - // Try a graceful terminate first |
15 | | - int r = system("killall -s TERM frontboardd 2>/dev/null"); |
16 | | - if (r == 0) return 0; |
| 29 | + // Attempt graceful shutdown, then force, then plain invocation. |
| 30 | + char *const args_term[] = {"killall", "-s", "TERM", "frontboardd", NULL}; |
| 31 | + if (spawn_and_wait(args_term) == 0) return 0; |
| 32 | + |
| 33 | + char *const args_kill[] = {"killall", "-s", "KILL", "frontboardd", NULL}; |
| 34 | + if (spawn_and_wait(args_kill) == 0) return 0; |
17 | 35 |
|
18 | | - // If graceful termination failed, try force kill |
19 | | - r = system("killall -s KILL frontboardd 2>/dev/null"); |
20 | | - if (r == 0) return 0; |
| 36 | + char *const args_plain[] = {"killall", "frontboardd", NULL}; |
| 37 | + if (spawn_and_wait(args_plain) == 0) return 0; |
21 | 38 |
|
22 | | - // As a last resort, try plain killall (older busybox-like syntax) |
23 | | - r = system("killall frontboardd 2>/dev/null"); |
24 | | - return (r == 0) ? 0 : -1; |
| 39 | + return -1; |
25 | 40 | } |
0 commit comments