fix races

This commit is contained in:
Nick Chan committed 2023-01-30 21:41:15 +08:00
1 parent 9f14da315b
commit 95b0b63a98
7 files changed
+129 -27

No files matched your search

+11 -2
View File
@@ -96,7 +96,7 @@ typedef int usb_ret_t;
typedef libusb_device_handle *usb_device_handle_t; typedef libusb_device_handle *usb_device_handle_t;
extern unsigned int verbose; extern unsigned int verbose;
extern int spin, demote; extern int demote;
extern char* pongo_path; extern char* pongo_path;
@@ -115,7 +115,9 @@ extern override_file_t override_ramdisk, override_kpf, override_overlay;
extern uint32_t checkrain_flags, palerain_flags, kpf_flags; extern uint32_t checkrain_flags, palerain_flags, kpf_flags;
extern pthread_mutex_t log_mutex; extern pthread_mutex_t log_mutex;
extern int enable_rootful, do_pongo_sleep, demote, found_pongo; extern pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex;
extern int enable_rootful, do_pongo_sleep, demote;
extern int pongo_thr_running, dfuhelper_thr_running; extern int pongo_thr_running, dfuhelper_thr_running;
extern bool ohio, start_from_pongo; extern bool ohio, start_from_pongo;
extern char xargs_cmd[0x270]; extern char xargs_cmd[0x270];
@@ -158,4 +160,11 @@ int issue_pongo_command();
int upload_pongo_file(); int upload_pongo_file();
int tui(); int tui();
int optparse(int argc, char* argv[]); int optparse(int argc, char* argv[]);
bool get_spin();
bool set_spin(bool val);
bool get_found_pongo();
bool set_found_pongo(bool val);
uint64_t get_ecid_wait_for_dfu();
uint64_t set_ecid_wait_for_dfu(uint64_t ecid);
#endif #endif
+1 -1
View File
@@ -3,7 +3,7 @@ all: palera1n
OBJECTS = main.o dfuhelper.o devhelper.o lockdown_helper.o exec_checkra1n.o OBJECTS = main.o dfuhelper.o devhelper.o lockdown_helper.o exec_checkra1n.o
OBJECTS += pongo_usb.o optparse.o override_file.o pongo_helper.o log.o OBJECTS += pongo_usb.o optparse.o override_file.o pongo_helper.o log.o
OBJECTS += xxd-checkra1n.o xxd-checkra1n-kpf-pongo.o xxd-ramdisk.dmg.o OBJECTS += xxd-checkra1n.o xxd-checkra1n-kpf-pongo.o xxd-ramdisk.dmg.o
OBJECTS += xxd-binpack.dmg.o OBJECTS += xxd-binpack.dmg.o lock_vars.o
ifeq ($(DEV_BUILD),1) ifeq ($(DEV_BUILD),1)
OBJECTS += tui_screen_enter_recovery.o tui_screen_enter_dfu.o tui_main.o OBJECTS += tui_screen_enter_recovery.o tui_screen_enter_dfu.o tui_main.o
OBJECTS += tui_screen_jailbreak.o tui_screen_main.o tui_screen_options.o OBJECTS += tui_screen_jailbreak.o tui_screen_main.o tui_screen_options.o
+12 -15
View File
@@ -32,9 +32,6 @@
#define NOHOME (cpid == 0x8015 || (cpid == 0x8010 && (bdid == 0x08 || bdid == 0x0a || bdid == 0x0c || bdid == 0x0e))) #define NOHOME (cpid == 0x8015 || (cpid == 0x8010 && (bdid == 0x08 || bdid == 0x0a || bdid == 0x0c || bdid == 0x0e)))
int spin = 1;
uint64_t ecid_wait_for_dfu = 0;
void step(int time, int time2, char *text, bool (*cond)(uint64_t), uint64_t cond_arg) { void step(int time, int time2, char *text, bool (*cond)(uint64_t), uint64_t cond_arg) {
for (int i = time2; i < time; i++) { for (int i = time2; i < time; i++) {
printf(CYN "\r\e[K%s (%d)" CRESET, text, time - i + time2); printf(CYN "\r\e[K%s (%d)" CRESET, text, time - i + time2);
@@ -82,7 +79,7 @@ int connected_normal_mode(const usbmuxd_device_info_t *usbmuxd_device) {
} }
static bool conditional(uint64_t ecid) { static bool conditional(uint64_t ecid) {
return ecid_wait_for_dfu != ecid; return get_ecid_wait_for_dfu() != ecid;
} }
void* connected_recovery_mode(struct irecv_device_info* info) { void* connected_recovery_mode(struct irecv_device_info* info) {
@@ -108,12 +105,12 @@ void* connected_recovery_mode(struct irecv_device_info* info) {
step(4, 2, "Hold Volume Down + Side button", NULL, 0); step(4, 2, "Hold Volume Down + Side button", NULL, 0);
else else
step(4, 2, "Hold Home + Power Button", NULL, 0); step(4, 2, "Hold Home + Power Button", NULL, 0);
ecid_wait_for_dfu = info->ecid; set_ecid_wait_for_dfu(info->ecid);
ret = exitrecv_cmd(info->ecid); ret = exitrecv_cmd(info->ecid);
info = NULL; info = NULL;
if (ret) { if (ret) {
LOG(LOG_ERROR, "Cannot exit recovery mode"); LOG(LOG_ERROR, "Cannot exit recovery mode");
ecid_wait_for_dfu = 0; set_ecid_wait_for_dfu(0);
return NULL; return NULL;
} }
printf("\r\e[K"); printf("\r\e[K");
@@ -126,25 +123,26 @@ void* connected_recovery_mode(struct irecv_device_info* info) {
step(10, 0, "Hold Volume Down button", conditional, ecid); step(10, 0, "Hold Volume Down button", conditional, ecid);
else else
step(10, 0, "Hold Home button", conditional, ecid); step(10, 0, "Hold Home button", conditional, ecid);
ecid_wait_for_dfu = 0; if (get_ecid_wait_for_dfu() != ecid) {
if (ecid_wait_for_dfu != ecid) {
} else { } else {
LOG(LOG_WARNING, "Whoops, device did not enter DFU mode"); LOG(LOG_WARNING, "Whoops, device did not enter DFU mode");
LOG(LOG_INFO, "Waiting for device to reconnect..."); LOG(LOG_INFO, "Waiting for device to reconnect...");
set_ecid_wait_for_dfu(0);
return NULL; return NULL;
} }
set_ecid_wait_for_dfu(0);
pthread_exit(NULL); pthread_exit(NULL);
return NULL; return NULL;
} }
void* connected_dfu_mode(struct irecv_device_info* info) { void* connected_dfu_mode(struct irecv_device_info* info) {
if (ecid_wait_for_dfu == info->ecid) { if (get_ecid_wait_for_dfu() == info->ecid) {
ecid_wait_for_dfu = 0; set_ecid_wait_for_dfu(0);
puts(""); puts("");
LOG(LOG_INFO, "Device entered DFU mode successfully"); LOG(LOG_INFO, "Device entered DFU mode successfully");
} }
unsubscribe_cmd(); unsubscribe_cmd();
spin = 0; set_spin(0);
pthread_exit(NULL); pthread_exit(NULL);
return NULL; return NULL;
} }
@@ -176,16 +174,15 @@ void irecv_device_event_cb(const irecv_device_event_t *event, void* userdata) {
} }
break; break;
case IRECV_DEVICE_REMOVE: case IRECV_DEVICE_REMOVE:
if (event->device_info->ecid == ecid_wait_for_dfu) { LOG(LOG_VERBOSE, "Recovery mode device disconnected");
LOG(LOG_VERBOSE, "Recovery mode device %ld disconnected", event->device_info->ecid);
}
break; break;
} }
} }
void *dfuhelper(void* ptr) { void *dfuhelper(void* ptr) {
subscribe_cmd(device_event_cb, irecv_device_event_cb); subscribe_cmd(device_event_cb, irecv_device_event_cb);
while (spin) { set_spin(1);
while (get_spin()) {
sleep(1); sleep(1);
}; };
return 0; return 0;
+89
View File
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <pthread.h>
#include <inttypes.h>
#include <getopt.h>
#include <errno.h>
#include <spawn.h>
#include <sys/mman.h>
#include <time.h>
#include <libimobiledevice/libimobiledevice.h>
#include <ANSI-color-codes.h>
#include <common.h>
#include <xxd-embedded.h>
#include <kerninfo.h>
pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex;
bool spin, found_pongo = 0;
uint64_t ecid_wait_for_dfu = 0;
bool get_spin() {
LOG(LOG_VERBOSE5, "locking spin mutex for get");
pthread_mutex_lock(&spin_mutex);
bool ret = spin;
LOG(LOG_VERBOSE5, "UN-locking spin mutex, spin value is %d", spin);
pthread_mutex_unlock(&spin_mutex);
return ret;
}
bool set_spin(bool val) {
LOG(LOG_VERBOSE5, "locking spin mutex for set spin = %d", val);
pthread_mutex_lock(&spin_mutex);
spin = val;
LOG(LOG_VERBOSE5, "UN-locking spin mutex after setting spin\n");
pthread_mutex_unlock(&spin_mutex);
return val;
}
bool get_found_pongo() {
LOG(LOG_VERBOSE5, "locking found pongo mutex for get");
pthread_mutex_lock(&found_pongo_mutex);
bool ret = found_pongo;
LOG(LOG_VERBOSE5, "UN-locking pongo mutex, found_pongo value is %d", found_pongo);
pthread_mutex_unlock(&found_pongo_mutex);
return ret;
}
bool set_found_pongo(bool val) {
LOG(LOG_VERBOSE5, "locking found pongo mutex for set found_pongo = %d", val);
pthread_mutex_lock(&found_pongo_mutex);
found_pongo = val;
LOG(LOG_VERBOSE5, "UN-locking pongo mutex after setting found_pongo");
pthread_mutex_unlock(&found_pongo_mutex);
return val;
}
uint64_t get_ecid_wait_for_dfu() {
LOG(LOG_VERBOSE5, "locking ecid wait for dfu mutex for get");
pthread_mutex_lock(&ecid_dfu_wait_mutex);
uint64_t ret = ecid_wait_for_dfu;
LOG(LOG_VERBOSE5, "UN-locking ecid wait for dfu mutex, ecid_wait_for_dfu value is %llu", ret);
pthread_mutex_unlock(&ecid_dfu_wait_mutex);
return ret;
}
uint64_t set_ecid_wait_for_dfu(uint64_t ecid) {
LOG(LOG_VERBOSE5, "locking ecid wait for dfu value for set ecid_wait_for_dfu = %llu", ecid);
pthread_mutex_lock(&ecid_dfu_wait_mutex);
ecid_wait_for_dfu = ecid;
LOG(LOG_VERBOSE5, "UN-locking ecid wait for dfu mutex afrer setting ecid_wait_for_dfu");
pthread_mutex_unlock(&ecid_dfu_wait_mutex);
return ecid;
}
+10 -2
View File
@@ -88,6 +88,9 @@ bool use_tui = false;
int palera1n(int argc, char *argv[]) { int palera1n(int argc, char *argv[]) {
int ret = 0; int ret = 0;
pthread_mutex_init(&log_mutex, NULL); pthread_mutex_init(&log_mutex, NULL);
pthread_mutex_init(&spin_mutex, NULL);
pthread_mutex_init(&found_pongo_mutex, NULL);
pthread_mutex_init(&ecid_dfu_wait_mutex, NULL);
if ((ret = build_checks())) return ret; if ((ret = build_checks())) return ret;
if ((ret = optparse(argc, argv))) goto cleanup; if ((ret = optparse(argc, argv))) goto cleanup;
if (palerain_version) goto normal_exit; if (palerain_version) goto normal_exit;
@@ -111,13 +114,13 @@ int palera1n(int argc, char *argv[]) {
if (pongo_exit || demote) if (pongo_exit || demote)
goto normal_exit; goto normal_exit;
pongo: pongo:
spin = true; set_spin(1);
if (do_pongo_sleep) if (do_pongo_sleep)
sleep(2); sleep(2);
else else
LOG(LOG_INFO, "Waiting for PongoOS devices..."); LOG(LOG_INFO, "Waiting for PongoOS devices...");
wait_for_pongo(); wait_for_pongo();
while (spin) while (get_spin())
{ {
sleep(1); sleep(1);
} }
@@ -156,9 +159,14 @@ cleanup:
munmap(override_overlay.ptr, (size_t)override_overlay.len); munmap(override_overlay.ptr, (size_t)override_overlay.len);
close(override_overlay.fd); close(override_overlay.fd);
} }
pthread_mutex_destroy(&log_mutex);
pthread_mutex_destroy(&spin_mutex);
pthread_mutex_destroy(&found_pongo_mutex);
pthread_mutex_destroy(&ecid_dfu_wait_mutex);
return ret; return ret;
} }
int main (int argc, char* argv[]) { int main (int argc, char* argv[]) {
return palera1n(argc, argv); return palera1n(argc, argv);
} }
+4 -5
View File
@@ -12,13 +12,12 @@
#include <common.h> #include <common.h>
int found_pongo = 0;
void* pongo_helper(void* ptr) { void* pongo_helper(void* ptr) {
pongo_thr_running = 1; pongo_thr_running = 1;
pthread_cleanup_push(thr_cleanup, &pongo_thr_running); pthread_cleanup_push(thr_cleanup, &pongo_thr_running);
wait_for_pongo(); wait_for_pongo();
while (spin) { while (get_spin()) {
sleep(1); sleep(1);
} }
pthread_cleanup_pop(1); pthread_cleanup_pop(1);
@@ -26,9 +25,9 @@ void* pongo_helper(void* ptr) {
} }
void *pongo_usb_callback(void *arg) { void *pongo_usb_callback(void *arg) {
if (found_pongo) if (get_found_pongo())
return NULL; return NULL;
found_pongo = 1; set_found_pongo(1);
strncat(xargs_cmd, " rootdev=md0", 0x270 - strlen(xargs_cmd) - 1); strncat(xargs_cmd, " rootdev=md0", 0x270 - strlen(xargs_cmd) - 1);
if (checkrain_option_enabled(palerain_flags, palerain_option_setup_rootful)) { if (checkrain_option_enabled(palerain_flags, palerain_option_setup_rootful)) {
strncat(xargs_cmd, " wdt=-1", 0x270 - strlen(xargs_cmd) - 1); strncat(xargs_cmd, " wdt=-1", 0x270 - strlen(xargs_cmd) - 1);
@@ -55,6 +54,6 @@ void *pongo_usb_callback(void *arg) {
issue_pongo_command(handle, "kpf"); issue_pongo_command(handle, "kpf");
issue_pongo_command(handle, "bootux"); issue_pongo_command(handle, "bootux");
LOG(LOG_INFO, "Booting Kernel..."); LOG(LOG_INFO, "Booting Kernel...");
spin = false; set_spin(0);
return NULL; return NULL;
} }
+2 -2
View File
@@ -267,7 +267,7 @@ int wait_for_pongo(void)
} }
libusb_free_device_list(list, 0); libusb_free_device_list(list, 0);
while(spin) while(get_spin())
{ {
r = libusb_handle_events(NULL); r = libusb_handle_events(NULL);
if(r != LIBUSB_SUCCESS) if(r != LIBUSB_SUCCESS)
@@ -290,7 +290,7 @@ static void io_start(stuff_t *stuff)
ERR("pthread_create: %s", strerror(r)); ERR("pthread_create: %s", strerror(r));
exit(-1); // TODO: ok with libusb? exit(-1); // TODO: ok with libusb?
} }
pongo_usb_callback(&stuff->handle); pthread_join(stuff->th, NULL);
} }
static void io_stop(stuff_t *stuff) static void io_stop(stuff_t *stuff)