#include <notify.h>
#include <stdio.h>
+#include <unistd.h>
#include <CoreFoundation/CoreFoundation.h>
return propertyList;
}
-int main(int argc, const char *argv[]) {
- if (argc > 1) {
- fprintf(stderr, "usage: sbreload\n");
- return 1;
- }
+#define _assert(test, format, args...) do { \
+ if (test) break; \
+ fprintf(stderr, format "\n", ##args); \
+ return 1; \
+} while (false)
- notify_post("com.apple.mobile.springboard_teardown");
+void stop() {
+ sleep(1);
+}
- launch_data_t request = launch_data_alloc(LAUNCH_DATA_DICTIONARY);
+#define SpringBoard_plist "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist"
- CFDictionaryRef plist = CreateMyPropertyListFromFile("/System/Library/LaunchDaemons/com.apple.SpringBoard.plist");
- if (plist == NULL) {
- fprintf(stderr, "CreateMyPropertyListFromFile() == NULL\n");
- return 2;
- }
+int main(int argc, const char *argv[]) {
+ _assert(argc == 1, "usage: sbreload");
+
+ CFDictionaryRef plist = CreateMyPropertyListFromFile(SpringBoard_plist);
+ _assert(plist != NULL, "CreateMyPropertyListFromFile() == NULL");
launch_data_t job = CF2launch_data(plist);
- if (job == NULL) {
- fprintf(stderr, "CF2launch_data() == NULL\n");
- return 3;
- }
+ _assert(job != NULL, "CF2launch_data() == NULL");
- const char *label = launch_data_get_string(launch_data_dict_lookup(job, LAUNCH_JOBKEY_LABEL));
- launch_data_dict_insert(request, job, LAUNCH_KEY_SUBMITJOB);
+ launch_data_t data, request, response;
+
+ data = launch_data_dict_lookup(job, LAUNCH_JOBKEY_LABEL);
+ _assert(data != NULL, "launch_data_dict_lookup(LABEL) == NULL");
+ const char *label = launch_data_get_string(data);
+
+ request = launch_data_alloc(LAUNCH_DATA_DICTIONARY);
+ launch_data_dict_insert(request, launch_data_new_string(label), LAUNCH_KEY_GETJOB);
- launch_data_t response;
- launch_msg:
response = launch_msg(request);
+ _assert(response != NULL, "launch_msg(GetJob) == NULL");
+ launch_data_free(request);
+
+ pid_t pid;
+
+ if (launch_data_get_type(response) == LAUNCH_DATA_ERRNO) {
+ int error = launch_data_get_errno(response);
+ _assert(error == ESRCH, "GetJob(%s): %s", label, strerror(error));
+ pid = -1;
+ } else if (launch_data_get_type(response) == LAUNCH_DATA_DICTIONARY) {
+ data = launch_data_dict_lookup(response, LAUNCH_JOBKEY_PID);
+ _assert(data != NULL, "launch_data_dict_lookup(PID) == NULL");
+ pid = launch_data_get_integer(data);
+ } else _assert(false, "launch_data_get_type() not in (DICTIONARY, ERRNO)");
+
+ launch_data_free(response);
- if (response == NULL) {
- fprintf(stderr, "launch_msg() == NULL\n");
- return 4;
+ // 600 is being used to approximate 4.x/5.x boundary
+ if (kCFCoreFoundationVersionNumber < 600) {
+ fprintf(stderr, "notify_post(com.apple.mobile.springboard_teardown)\n");
+ notify_post("com.apple.mobile.springboard_teardown");
+ } else {
+ // XXX: this code is preferable to launchctl unoad but it requires libvproc? :(
+ //vproc_err_t *error = _vproc_send_signal_by_label(label, VPROC_MAGIC_UNLOAD_SIGNAL);
+ //_assert(error == NULL, "_vproc_send_signal_by_label(UNLOAD) != NULL");
+
+ fprintf(stderr, "launchctl unload SpringBoard.plist\n");
+ system("launchctl unload " SpringBoard_plist);
}
- if (launch_data_get_type(response) != LAUNCH_DATA_ERRNO) {
- fprintf(stderr, "launch_data_get_type() != ERRNO\n");
- return 5;
+ if (pid != -1) {
+ fprintf(stderr, "waiting for kill(%u) != 0...\n", pid);
+ while (kill(pid, 0) == 0)
+ stop();
+
+ int error = errno;
+ _assert(error == ESRCH, "kill(%u): %s", pid, strerror(error));
}
- int error = launch_data_get_errno(response);
- launch_data_free(response);
+ request = launch_data_alloc(LAUNCH_DATA_DICTIONARY);
+ launch_data_dict_insert(request, job, LAUNCH_KEY_SUBMITJOB);
+
+ for (;;) {
+ response = launch_msg(request);
+ _assert(response != NULL, "launch_msg(SubmitJob) == NULL");
+
+ _assert(launch_data_get_type(response) == LAUNCH_DATA_ERRNO, "launch_data_get_type() != ERRNO");
+ int error = launch_data_get_errno(response);
+ launch_data_free(response);
- const char *string = strerror(error);
+ const char *string = strerror(error);
- if (error == EEXIST) {
- fprintf(stderr, "%s: %s, retrying...\n", label, string);
- sleep(1);
- goto launch_msg;
- } else if (error != 0) {
- fprintf(stderr, "%s: %s\n", label, string);
- return 6;
+ if (error == EEXIST) {
+ fprintf(stderr, "SubmitJob(%s): %s, retrying...\n", label, string);
+ stop();
+ } else {
+ _assert(error == 0, "SubmitJob(%s): %s", label, string);
+ break;
+ }
}
launch_data_free(request);