From bba2ea9266246280c2b0b87aaccf739cc0d2a0a9 Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Mon, 27 Feb 2023 22:48:26 -0500 Subject: [PATCH] Remove all __builtin_available and replace with ifdefs For some reason ld64 refused to weak link the symbols, so this is a cheap work around. As an added bonus, it removes the need for compiler-rt. --- bootstrap.c | 59 +++++++++++++++++++------------------ limit.c | 20 +++++++------ load.c | 6 ++-- print.c | 81 ++++++++++++++++++++++++++++----------------------- userswitch.c | 7 +++-- version.c | 16 +++++----- xpc_helper.c | 38 ++++++++++++------------ xpc_private.h | 23 ++++++++++----- 8 files changed, 137 insertions(+), 113 deletions(-) diff --git a/bootstrap.c b/bootstrap.c index efb0a94..01217fd 100644 --- a/bootstrap.c +++ b/bootstrap.c @@ -62,22 +62,22 @@ bootstrap_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **appl if (argc > 2) { paths = launchctl_parse_load_unload(0, argc - 2, argv + 2); xpc_dictionary_set_value(dict, "paths", paths); - if (__builtin_available(iOS 16, *)) { - if (xpc_dictionary_get_uint64(dict, "type") == 1 && xpc_user_sessions_enabled() != 0) { - xpc_array_apply(paths, ^bool (size_t index, xpc_object_t val) { - xpc_object_t plist = launchctl_xpc_from_plist(xpc_string_get_string_ptr(val)); - if (plist != NULL && xpc_get_type(plist) == XPC_TYPE_DICTIONARY) { - if (xpc_dictionary_get_value(plist, "LimitLoadToSessionType") != 0) { - xpc_release(plist); - return true; - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 160000 + if (xpc_dictionary_get_uint64(dict, "type") == 1 && xpc_user_sessions_enabled() != 0) { + xpc_array_apply(paths, ^bool (size_t index, xpc_object_t val) { + xpc_object_t plist = launchctl_xpc_from_plist(xpc_string_get_string_ptr(val)); + if (plist != NULL && xpc_get_type(plist) == XPC_TYPE_DICTIONARY) { + if (xpc_dictionary_get_value(plist, "LimitLoadToSessionType") != 0) { + xpc_release(plist); + return true; } - xpc_dictionary_set_uint64(dict, "type", 2); - xpc_dictionary_set_uint64(dict, "handle", xpc_user_sessions_get_foreground_uid(0)); - return false; - }); - } + } + xpc_dictionary_set_uint64(dict, "type", 2); + xpc_dictionary_set_uint64(dict, "handle", xpc_user_sessions_get_foreground_uid(0)); + return false; + }); } +#endif } ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_LOAD, dict, &reply); if (ret != ENODOMAIN) { @@ -129,26 +129,27 @@ bootout_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) if (argc > 2 && name == NULL) { paths = launchctl_parse_load_unload(0, argc - 2, argv + 2); xpc_dictionary_set_value(dict, "paths", paths); - if (__builtin_available(iOS 16, *)) { - if (xpc_dictionary_get_uint64(dict, "type") == 1 && xpc_user_sessions_enabled() != 0) { - xpc_array_apply(paths, ^bool (size_t index, xpc_object_t val) { - xpc_object_t plist = launchctl_xpc_from_plist(xpc_string_get_string_ptr(val)); - if (plist != NULL && xpc_get_type(plist) == XPC_TYPE_DICTIONARY) { - if (xpc_dictionary_get_value(plist, "LimitLoadToSessionType") != 0) { - xpc_release(plist); - return true; - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 160000 + if (xpc_dictionary_get_uint64(dict, "type") == 1 && xpc_user_sessions_enabled() != 0) { + xpc_array_apply(paths, ^bool (size_t index, xpc_object_t val) { + xpc_object_t plist = launchctl_xpc_from_plist(xpc_string_get_string_ptr(val)); + if (plist != NULL && xpc_get_type(plist) == XPC_TYPE_DICTIONARY) { + if (xpc_dictionary_get_value(plist, "LimitLoadToSessionType") != 0) { + xpc_release(plist); + return true; } - xpc_dictionary_set_uint64(dict, "type", 2); - xpc_dictionary_set_uint64(dict, "handle", xpc_user_sessions_get_foreground_uid(0)); - return false; - }); - } + } + xpc_dictionary_set_uint64(dict, "type", 2); + xpc_dictionary_set_uint64(dict, "handle", xpc_user_sessions_get_foreground_uid(0)); + return false; + }); } +#endif } - if (__builtin_available(iOS 15, *)) +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 xpc_dictionary_set_bool(dict, "no-einprogress", true); +#endif ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_UNLOAD, dict, &reply); if (ret != ENODOMAIN) { diff --git a/limit.c b/limit.c index c25f9cf..07e6b2c 100644 --- a/limit.c +++ b/limit.c @@ -83,8 +83,10 @@ limit_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) xpc_object_t dict, reply; int err = 0; +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 vm_address_t addr; vm_size_t sz = 0x100000; +#endif long long softlimit, hardlimit, idx; dict = xpc_dictionary_create(NULL, NULL, 0); @@ -96,21 +98,21 @@ limit_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) printlimits: xpc_dictionary_set_bool(dict, "print", true); - if (__builtin_available(iOS 15, *)) { - addr = launchctl_create_shmem(dict, sz); - } else { - xpc_dictionary_set_fd(dict, "file", STDOUT_FILENO); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + addr = launchctl_create_shmem(dict, sz); +#else + xpc_dictionary_set_fd(dict, "file", STDOUT_FILENO); +#endif err = launchctl_send_xpc_to_launchd(XPC_ROUTINE_LIMIT, dict, &reply); if (err != 0) { fprintf(stderr, "Could not print resource limits: %d: %s\n", err, xpc_strerror(err)); } - if (__builtin_available(iOS 15, *)) { - launchctl_print_shmem(reply, addr, sz, stdout); - vm_deallocate(mach_task_self(), addr, sz); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + launchctl_print_shmem(reply, addr, sz, stdout); + vm_deallocate(mach_task_self(), addr, sz); +#endif } else { if ((idx = limit_index(argv[1])) == -1) { fprintf(stderr, "%s is not a valid limit name.\n", argv[1]); diff --git a/load.c b/load.c index 91bd711..d5a226e 100644 --- a/load.c +++ b/load.c @@ -99,9 +99,9 @@ load_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) xpc_dictionary_set_bool(dict, "enable", wflag); } else { xpc_dictionary_set_bool(dict, "disable", wflag); - if (__builtin_available(iOS 15, *)) { - xpc_dictionary_set_bool(dict, "no-einprogress", true); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + xpc_dictionary_set_bool(dict, "no-einprogress", true); +#endif } xpc_dictionary_set_bool(dict, "legacy-load", true); if (force) diff --git a/print.c b/print.c index f9a981e..2ec19cf 100644 --- a/print.c +++ b/print.c @@ -45,8 +45,10 @@ print_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) int ret = EUSAGE; xpc_object_t reply; const char *name = NULL; +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 vm_address_t addr; vm_size_t sz = 0x100000; +#endif xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); *msg = dict; @@ -54,21 +56,22 @@ print_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) if ((ret = launchctl_setup_xpc_dict_for_service_name(argv[1], dict, &name)) != 0) return ret; - if (__builtin_available(iOS 15, *)) { - addr = launchctl_create_shmem(dict, sz); - } else { - xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + addr = launchctl_create_shmem(dict, sz); +#else + xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); +#endif + if (name != NULL) ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_PRINT_SERVICE, dict, &reply); else ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_PRINT, dict, &reply); if (ret == 0) { - if (__builtin_available(iOS 15, *)) { - launchctl_print_shmem(reply, addr, sz, stdout); - vm_deallocate(mach_task_self(), addr, sz); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + launchctl_print_shmem(reply, addr, sz, stdout); + vm_deallocate(mach_task_self(), addr, sz); +#endif } else if (ret < ENODOMAIN) { if (ret == EINVAL) fprintf(stderr, "Bad request.\n"); @@ -84,8 +87,10 @@ print_cache_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **ap { int ret; xpc_object_t reply; +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 vm_address_t addr; vm_size_t sz = 0x1400000; +#endif xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); *msg = dict; @@ -93,20 +98,20 @@ print_cache_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **ap xpc_dictionary_set_uint64(dict, "type", 1); xpc_dictionary_set_uint64(dict, "handle", 0); - if (__builtin_available(iOS 15, *)) { - addr = launchctl_create_shmem(dict, sz); - } else { - xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + addr = launchctl_create_shmem(dict, sz); +#else + xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); +#endif xpc_dictionary_set_bool(dict, "cache", true); ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_PRINT, dict, &reply); if (ret == 0) { - if (__builtin_available(iOS 15, *)) { - launchctl_print_shmem(reply, addr, sz, stdout); - vm_deallocate(mach_task_self(), addr, sz); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + launchctl_print_shmem(reply, addr, sz, stdout); + vm_deallocate(mach_task_self(), addr, sz); +#endif } else if (ret < ENODOMAIN) { if (ret == EINVAL) fprintf(stderr, "Bad request.\n"); @@ -122,8 +127,10 @@ print_disabled_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char * { int ret; xpc_object_t reply; +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 vm_address_t addr; vm_size_t sz = 0x100000; +#endif xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); *msg = dict; @@ -131,20 +138,20 @@ print_disabled_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char * xpc_dictionary_set_uint64(dict, "type", 1); xpc_dictionary_set_uint64(dict, "handle", 0); - if (__builtin_available(iOS 15, *)) { - addr = launchctl_create_shmem(dict, sz); - } else { - xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + addr = launchctl_create_shmem(dict, sz); +#else + xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); +#endif xpc_dictionary_set_bool(dict, "disabled", true); ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_PRINT, dict, &reply); if (ret == 0) { - if (__builtin_available(iOS 15, *)) { - launchctl_print_shmem(reply, addr, sz, stdout); - vm_deallocate(mach_task_self(), addr, sz); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + launchctl_print_shmem(reply, addr, sz, stdout); + vm_deallocate(mach_task_self(), addr, sz); +#endif } else if (ret < ENODOMAIN) { if (ret == EINVAL) fprintf(stderr, "Bad request.\n"); @@ -160,8 +167,10 @@ dumpstate_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **appl { int ret; xpc_object_t reply; +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 vm_address_t addr; vm_size_t sz = 0x1400000; +#endif xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); *msg = dict; @@ -169,19 +178,19 @@ dumpstate_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **appl xpc_dictionary_set_uint64(dict, "type", 1); xpc_dictionary_set_uint64(dict, "handle", 0); - if (__builtin_available(iOS 15, *)) { - addr = launchctl_create_shmem(dict, sz); - } else { - xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + addr = launchctl_create_shmem(dict, sz); +#else + xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); +#endif ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_DUMPSTATE, dict, &reply); if (ret == 0) { - if (__builtin_available(iOS 15, *)) { - launchctl_print_shmem(reply, addr, sz, stdout); - vm_deallocate(mach_task_self(), addr, sz); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + launchctl_print_shmem(reply, addr, sz, stdout); + vm_deallocate(mach_task_self(), addr, sz); +#endif } else if (ret == EBUSY) { fprintf(stderr, "State-dump already in progress; please try again later.\n"); } else if (ret == ENOTSUP) { diff --git a/userswitch.c b/userswitch.c index 15da80e..533eec1 100644 --- a/userswitch.c +++ b/userswitch.c @@ -41,14 +41,15 @@ userswitch_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **app return EUSAGE; int ret = ENOTSUP; + +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 long olduid = 0, newuid = 0; olduid = strtol(argv[1], NULL, 0); newuid = strtol(argv[1], NULL, 0); - if (__builtin_available(iOS 15, *)) { - ret = launch_active_user_switch(olduid, newuid); - } + ret = launch_active_user_switch(olduid, newuid); +#endif if (ret != 0) { fprintf(stderr, "Failed to perform a user switch: %d: %s\n", ret, xpc_strerror(ret)); diff --git a/version.c b/version.c index 8692ac4..105f6cb 100644 --- a/version.c +++ b/version.c @@ -41,14 +41,15 @@ version_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); *msg = dict; launchctl_setup_xpc_dict(dict); + +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 vm_address_t addr = 0; vm_size_t sz = 0x100000; - if (__builtin_available(iOS 15, *)) { - addr = launchctl_create_shmem(dict, sz); - } else { - xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); - } + addr = launchctl_create_shmem(dict, sz); +#else + xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO); +#endif if (strcmp(argv[0], "variant") == 0) xpc_dictionary_set_bool(dict, "variant", 1); @@ -63,11 +64,12 @@ version_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple) fprintf(stderr, "Could not print variant: %d: %s\n", ret, xpc_strerror(ret)); } - if (__builtin_available(iOS 15, *)) - launchctl_print_shmem(reply, addr, sz, stdout); +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + launchctl_print_shmem(reply, addr, sz, stdout); if (addr != 0) vm_deallocate(mach_task_self(), addr, sz); +#endif return ret; } diff --git a/xpc_helper.c b/xpc_helper.c index 1045750..295d896 100644 --- a/xpc_helper.c +++ b/xpc_helper.c @@ -122,11 +122,11 @@ launchctl_xpc_object_print(xpc_object_t in, const char *name, int level) void launchctl_setup_xpc_dict(xpc_object_t dict) { - if (__builtin_available(iOS 15, *)) { - xpc_dictionary_set_uint64(dict, "type", 7); - } else { - xpc_dictionary_set_uint64(dict, "type", 1); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 150000 + xpc_dictionary_set_uint64(dict, "type", 7); +#else + xpc_dictionary_set_uint64(dict, "type", 1); +#endif xpc_dictionary_set_uint64(dict, "handle", 0); return; } @@ -170,28 +170,28 @@ launchctl_setup_xpc_dict_for_service_name(char *servicetarget, xpc_object_t dict if (name != NULL) { *name = split[1]; } - if (__builtin_available(iOS 16, *)) { - if (xpc_user_sessions_enabled() && launchctl_test_xpc_send(1, handle, split[1]) == false) { - uint64_t fguid = xpc_user_sessions_get_foreground_uid(0); - if (launchctl_test_xpc_send(2, fguid, split[1])) { - fprintf(stderr, "Warning: Please switch to user/foreground/%s service identifier\n", split[1]); - xpc_dictionary_set_uint64(dict, "type", 2); - xpc_dictionary_set_uint64(dict, "handle", xpc_user_sessions_get_foreground_uid(0)); - } +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 160000 + if (xpc_user_sessions_enabled() && launchctl_test_xpc_send(1, handle, split[1]) == false) { + uint64_t fguid = xpc_user_sessions_get_foreground_uid(0); + if (launchctl_test_xpc_send(2, fguid, split[1])) { + fprintf(stderr, "Warning: Please switch to user/foreground/%s service identifier\n", split[1]); + xpc_dictionary_set_uint64(dict, "type", 2); + xpc_dictionary_set_uint64(dict, "handle", xpc_user_sessions_get_foreground_uid(0)); } } +#endif } return 0; } else if (strcmp(split[0], "user") == 0) { xpc_dictionary_set_uint64(dict, "type", 2); if (split[1] != NULL && strcmp(split[1], "foreground") == 0) { - if (__builtin_available(iOS 16, *)) { - if (xpc_user_sessions_enabled() == 0) { - fprintf(stderr, "user/foreground/ specifier is not supported on this platform\n"); - return ENOTSUP; - } - handle = xpc_user_sessions_get_foreground_uid(0); +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 160000 + if (xpc_user_sessions_enabled() == 0) { + fprintf(stderr, "user/foreground/ specifier is not supported on this platform\n"); + return ENOTSUP; } + handle = xpc_user_sessions_get_foreground_uid(0); +#endif } } else if (strcmp(split[0], "session") == 0) { xpc_dictionary_set_uint64(dict, "type", 4); diff --git a/xpc_private.h b/xpc_private.h index 92e22b7..2fb2e22 100644 --- a/xpc_private.h +++ b/xpc_private.h @@ -57,20 +57,28 @@ enum { XPC_ROUTINE_DUMPSTATE = 834, }; -typedef xpc_object_t xpc_pipe_t; +XPC_DECL(xpc_pipe); -int xpc_pipe_routine(xpc_pipe_t pipe, xpc_object_t message, xpc_object_t XPC_GIVES_REFERENCE *reply); -int _xpc_pipe_interface_routine(xpc_pipe_t pipe, uint64_t routine, xpc_object_t message, xpc_object_t XPC_GIVES_REFERENCE *reply, uint64_t flags); +XPC_EXPORT XPC_WARN_RESULT XPC_NONNULL1 XPC_NONNULL2 XPC_NONNULL3 +int +xpc_pipe_routine(xpc_pipe_t pipe, xpc_object_t message, + xpc_object_t XPC_GIVES_REFERENCE *reply); -const char *xpc_strerror(int); +XPC_EXPORT XPC_WARN_RESULT XPC_NONNULL1 XPC_NONNULL3 XPC_NONNULL4 +int +_xpc_pipe_interface_routine(xpc_pipe_t pipe, uint64_t routine, + xpc_object_t message, xpc_object_t XPC_GIVES_REFERENCE *reply, + uint64_t flags) __API_AVAILABLE(ios(15.0)); + +int launch_active_user_switch(long, long) __API_AVAILABLE(ios(15.0)); -int64_t xpc_user_sessions_enabled(void); +int64_t xpc_user_sessions_enabled(void) __API_AVAILABLE(ios(16.0)); +uint64_t xpc_user_sessions_get_foreground_uid(uint64_t) __API_AVAILABLE(ios(16.0)); XPC_EXPORT XPC_RETURNS_RETAINED XPC_WARN_RESULT XPC_NONNULL1 xpc_object_t xpc_create_from_plist(const void * data, size_t length); -uint64_t xpc_user_sessions_get_foreground_uid(uint64_t); -int launch_active_user_switch(long, long); +const char *xpc_strerror(int); #define XPC_TYPE_MACH_SEND (&_xpc_type_mach_send) XPC_EXPORT @@ -78,6 +86,7 @@ XPC_TYPE(_xpc_type_mach_send); typedef void (*xpc_dictionary_applier_f)(const char *key, xpc_object_t val, void *ctx); void xpc_dictionary_apply_f(xpc_object_t xdict, void *ctx, xpc_dictionary_applier_f applier); + typedef void (*xpc_array_applier_f)(size_t index, xpc_object_t value, void* context); void xpc_array_apply_f(xpc_object_t xarray, void *context, xpc_array_applier_f applier);