From 8f3e5fa776a1b9aa5d4fd03c32a018883562aad0 Mon Sep 17 00:00:00 2001 From: Felix Kratz Date: Wed, 2 Mar 2022 19:50:22 +0100 Subject: [PATCH] Moving to XNU mach IPC (#172) * move to mach messages * cleanup * mach responses * finalize xnu mach ipc * clean up memory management --- src/bar_item.c | 18 ++--- src/bar_manager.c | 8 +-- src/event.c | 10 ++- src/event.h | 8 +-- src/manifest.m | 4 +- src/message.c | 22 ++++-- src/message.h | 3 +- src/misc/mach.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++ src/misc/mach.h | 36 ++++++++++ src/misc/socket.c | 112 ------------------------------- src/misc/socket.h | 37 ----------- src/sketchybar.c | 70 +++---------------- 12 files changed, 251 insertions(+), 243 deletions(-) create mode 100644 src/misc/mach.c create mode 100644 src/misc/mach.h delete mode 100644 src/misc/socket.c delete mode 100644 src/misc/socket.h diff --git a/src/bar_item.c b/src/bar_item.c index b7839ff7..15c3f247 100644 --- a/src/bar_item.c +++ b/src/bar_item.c @@ -47,8 +47,10 @@ void bar_item_inherit_from_item(struct bar_item* bar_item, struct bar_item* ance text_set_string(&bar_item->icon, string_copy(ancestor->icon.string), true); text_set_string(&bar_item->label, string_copy(ancestor->label.string), true); - bar_item_set_script(bar_item, string_copy(ancestor->script)); - bar_item_set_click_script(bar_item, string_copy(ancestor->click_script)); + if (ancestor->script) + bar_item_set_script(bar_item, string_copy(ancestor->script)); + if (ancestor->click_script) + bar_item_set_click_script(bar_item, string_copy(ancestor->click_script)); image_copy(&bar_item->background.image, ancestor->background.image.image_ref); image_copy(&bar_item->icon.background.image, ancestor->icon.background.image.image_ref); @@ -90,9 +92,9 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) { bar_item->has_alias = false; bar_item->has_graph = false; - bar_item->name = string_copy(""); - bar_item->script = string_copy(""); - bar_item->click_script = string_copy(""); + bar_item->name = NULL; + bar_item->script = NULL; + bar_item->click_script = NULL; text_init(&bar_item->icon); text_init(&bar_item->label); @@ -158,7 +160,7 @@ bool bar_item_update(struct bar_item* bar_item, char* sender, bool forced, struc bar_item->counter = 0; // Script Update - if (strlen(bar_item->script) > 0) { + if (bar_item->script && strlen(bar_item->script) > 0) { if (!env_vars) env_vars = &bar_item->signal_args.env_vars; else env_vars_set(env_vars, string_copy("NAME"), string_copy(bar_item->name)); @@ -196,7 +198,7 @@ void bar_item_set_type(struct bar_item* bar_item, char type) { bar_item->type = type; if (type == BAR_COMPONENT_SPACE) { - if (strlen(bar_item->script) == 0) { + if (!bar_item->script) { bar_item_set_script(bar_item, string_copy("sketchybar -m --set $NAME icon.highlight=$SELECTED")); } @@ -256,7 +258,7 @@ void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t modifi env_vars_set(&bar_item->signal_args.env_vars, string_copy("BUTTON"), string_copy(get_type_description(type))); env_vars_set(&bar_item->signal_args.env_vars, string_copy("MODIFIER"), string_copy(get_modifier_description(modifier))); - if (strlen(bar_item->click_script) > 0) + if (bar_item->click_script && strlen(bar_item->click_script) > 0) fork_exec(bar_item->click_script, &bar_item->signal_args.env_vars); if (bar_item->update_mask & UPDATE_MOUSE_CLICKED) bar_item_update(bar_item, COMMAND_SUBSCRIBE_MOUSE_CLICKED, true, NULL); diff --git a/src/bar_manager.c b/src/bar_manager.c index fe6ea3f8..51796194 100644 --- a/src/bar_manager.c +++ b/src/bar_manager.c @@ -336,15 +336,15 @@ void bar_manager_begin(struct bar_manager *bar_manager) { if (bar_manager->display == DISPLAY_MAIN) { uint32_t did = display_main_display_id(); bar_manager->bar_count = 1; - bar_manager->bars = (struct bar **) malloc(sizeof(struct bar *) * bar_manager->bar_count); - memset(bar_manager->bars,0, sizeof(struct bar*) * bar_manager->bar_count); + bar_manager->bars = (struct bar **) realloc(bar_manager->bars, sizeof(struct bar *) * bar_manager->bar_count); + memset(bar_manager->bars, 0, sizeof(struct bar*) * bar_manager->bar_count); bar_manager->bars[0] = bar_create(did); bar_manager->bars[0]->adid = 1; } else { bar_manager->bar_count = display_active_display_count(); - bar_manager->bars = (struct bar **) malloc(sizeof(struct bar *) * bar_manager->bar_count); - memset(bar_manager->bars,0, sizeof(struct bar*) * bar_manager->bar_count); + bar_manager->bars = (struct bar **) realloc(bar_manager->bars, sizeof(struct bar *) * bar_manager->bar_count); + memset(bar_manager->bars, 0, sizeof(struct bar*) * bar_manager->bar_count); for (uint32_t index=1; index <= bar_manager->bar_count; index++) { uint32_t did = display_arrangement_display_id(index); bar_manager->bars[index - 1] = bar_create(did); diff --git a/src/event.c b/src/event.c index 736b5deb..9bde9b52 100644 --- a/src/event.c +++ b/src/event.c @@ -1,4 +1,5 @@ #include "event.h" +#include extern struct event_loop g_event_loop; extern struct bar_manager g_bar_manager; @@ -95,14 +96,11 @@ static EVENT_CALLBACK(EVENT_HANDLER_SHELL_REFRESH) { return EVENT_SUCCESS; } -static EVENT_CALLBACK(EVENT_HANDLER_DAEMON_MESSAGE) { +static EVENT_CALLBACK(EVENT_HANDLER_MACH_MESSAGE) { debug("%s\n", __FUNCTION__); - int sockfd = *((int*)context); - int length; - char* message = socket_read(sockfd, &length); - if (message) handle_message(sockfd, message), free(message); - socket_close(sockfd); + if (context) handle_message_mach(context); + mach_msg_destroy(&((struct mach_buffer*) context)->message.header); free(context); return EVENT_SUCCESS; } diff --git a/src/event.h b/src/event.h index 900a5699..4ca2d35e 100644 --- a/src/event.h +++ b/src/event.h @@ -17,7 +17,7 @@ static EVENT_CALLBACK(EVENT_HANDLER_MENU_BAR_HIDDEN_CHANGED); static EVENT_CALLBACK(EVENT_HANDLER_SYSTEM_WOKE); static EVENT_CALLBACK(EVENT_HANDLER_SYSTEM_WILL_SLEEP); static EVENT_CALLBACK(EVENT_HANDLER_SHELL_REFRESH); -static EVENT_CALLBACK(EVENT_HANDLER_DAEMON_MESSAGE); +static EVENT_CALLBACK(EVENT_HANDLER_MACH_MESSAGE); static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP); static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_ENTERED); static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_EXITED); @@ -45,7 +45,7 @@ enum event_type { SYSTEM_WOKE, SYSTEM_WILL_SLEEP, SHELL_REFRESH, - DAEMON_MESSAGE, + MACH_MESSAGE, MOUSE_UP, MOUSE_ENTERED, MOUSE_EXITED, @@ -68,7 +68,7 @@ static const char *event_type_str[] = { [SYSTEM_WOKE] = "system_woke", [SYSTEM_WILL_SLEEP] = "system_will_sleep", [SHELL_REFRESH] = "shell_refresh", - [DAEMON_MESSAGE] = "daemon_message", + [MACH_MESSAGE] = "mach_message", [MOUSE_UP] = "mouse_up", [MOUSE_ENTERED] = "mouse_entered", [MOUSE_EXITED] = "mouse_exited", @@ -94,7 +94,7 @@ static event_callback *event_handler[] = { [SYSTEM_WOKE] = EVENT_HANDLER_SYSTEM_WOKE, [SYSTEM_WILL_SLEEP] = EVENT_HANDLER_SYSTEM_WILL_SLEEP, [SHELL_REFRESH] = EVENT_HANDLER_SHELL_REFRESH, - [DAEMON_MESSAGE] = EVENT_HANDLER_DAEMON_MESSAGE, + [MACH_MESSAGE] = EVENT_HANDLER_MACH_MESSAGE, }; struct event { diff --git a/src/manifest.m b/src/manifest.m index 5ea1d77f..2ae1f82a 100644 --- a/src/manifest.m +++ b/src/manifest.m @@ -16,8 +16,8 @@ #include "misc/env_vars.h" #include "misc/helpers.h" #include "misc/memory_pool.h" -#include "misc/socket.h" -#include "misc/socket.c" +#include "misc/mach.h" +#include "misc/mach.c" #include "event_loop.h" #include "mouse.h" diff --git a/src/message.c b/src/message.c index 8646e365..d070541f 100644 --- a/src/message.c +++ b/src/message.c @@ -484,6 +484,8 @@ static void handle_domain_remove(FILE* rsp, struct token domain, char* message) for (int i = 0; i < count; i++) { bar_manager_remove_item(&g_bar_manager, bar_items[i]); } + + if (bar_items) free(bar_items); } // Syntax: sketchybar -m --move @@ -528,8 +530,13 @@ static void handle_domain_order(FILE* rsp, struct token domain, char* message) { bar_manager_refresh(&g_bar_manager, false); } -void handle_message(int sockfd, char* message) { - FILE* rsp = fdopen(sockfd, "w"); +void handle_message_mach(struct mach_buffer* buffer) { + if (!buffer->message.descriptor.address) return; + char* message = buffer->message.descriptor.address; + char* response = NULL; + size_t length = 0; + FILE* rsp = open_memstream(&response, &length); + fprintf(rsp, ""); bar_manager_freeze(&g_bar_manager); struct token command = get_token(&message); @@ -686,11 +693,14 @@ void handle_message(int sockfd, char* message) { bar_manager_refresh(&g_bar_manager, false); } if (rsp) fclose(rsp); + + mach_send_message(buffer->message.header.msgh_remote_port, response, + length, + false ); + if (response) free(response); } -static SOCKET_DAEMON_HANDLER(message_handler) { - int* _sockfd = malloc(sizeof(int)); - memcpy(_sockfd, &sockfd, sizeof(int)); - struct event *event = event_create(&g_event_loop, DAEMON_MESSAGE, _sockfd); +static MACH_HANDLER(mach_message_handler) { + struct event *event = event_create(&g_event_loop, MACH_MESSAGE, message); event_loop_post(&g_event_loop, event); } diff --git a/src/message.h b/src/message.h index c44afd49..88f7b3e7 100644 --- a/src/message.h +++ b/src/message.h @@ -136,7 +136,6 @@ #define REGEX_DELIMITER '/' -static SOCKET_DAEMON_HANDLER(message_handler); -void handle_message(int sockfd, char *message); +void handle_message_mach(struct mach_buffer* buffer); #endif diff --git a/src/misc/mach.c b/src/misc/mach.c new file mode 100644 index 00000000..0f46325c --- /dev/null +++ b/src/misc/mach.c @@ -0,0 +1,166 @@ +#include "mach.h" +#include +#include + +mach_port_t mach_get_bs_port() { + mach_port_name_t task = mach_task_self(); + + mach_port_t bs_port; + if (task_get_special_port(task, + TASK_BOOTSTRAP_PORT, + &bs_port ) != KERN_SUCCESS) { + return 0; + } + + mach_port_t port; + if (bootstrap_look_up(bs_port, + "git.felix.sketchybar", + &port ) != KERN_SUCCESS) { + return 0; + } + + return port; +} + +void mach_receive_message(mach_port_t port, struct mach_buffer* buffer, + bool timeout ) { + *buffer = (struct mach_buffer) { 0 }; + mach_msg_return_t msg_return; + if (timeout) + msg_return = mach_msg(&buffer->message.header, + MACH_RCV_MSG | MACH_RCV_TIMEOUT, + 0, + sizeof(struct mach_buffer), + port, + 10, + MACH_PORT_NULL ); + else + msg_return = mach_msg(&buffer->message.header, + MACH_RCV_MSG, + 0, + sizeof(struct mach_buffer), + port, + MACH_MSG_TIMEOUT_NONE, + MACH_PORT_NULL ); + + if (msg_return != MACH_MSG_SUCCESS) { + buffer->message.descriptor.address = NULL; + } +} + +bool mach_send_message(mach_port_t port, char* message, uint32_t len, + bool await_response) { + if (!message || !port) { + if (message) free(message); + return false; + } + + mach_port_t response_port; + if (await_response) { + mach_port_name_t task = mach_task_self(); + if (mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, + &response_port ) != KERN_SUCCESS) { + return false; + } + + if (mach_port_insert_right(task, response_port, + response_port, + MACH_MSG_TYPE_MAKE_SEND)!= KERN_SUCCESS) { + return false; + } + } + + struct mach_message msg = { 0 }; + msg.header.msgh_remote_port = port; + if (await_response) { + msg.header.msgh_local_port = response_port; + msg.header.msgh_id = response_port; + msg.header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, + MACH_MSG_TYPE_MAKE_SEND_ONCE, + 0, + MACH_MSGH_BITS_COMPLEX ); + } else { + msg.header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE + & MACH_MSGH_BITS_REMOTE_MASK, + 0, + 0, + MACH_MSGH_BITS_COMPLEX ); + } + + msg.header.msgh_size = sizeof(struct mach_message); + + msg.msgh_descriptor_count = 1; + msg.descriptor.address = message; + msg.descriptor.size = len * sizeof(char); + msg.descriptor.copy = MACH_MSG_VIRTUAL_COPY; + msg.descriptor.deallocate = await_response; + msg.descriptor.type = MACH_MSG_OOL_DESCRIPTOR; + + mach_msg_return_t msg_return = mach_msg(&msg.header, + MACH_SEND_MSG, + sizeof(struct mach_message), + 0, + MACH_PORT_NULL, + MACH_MSG_TIMEOUT_NONE, + MACH_PORT_NULL ); + + if (await_response) { + struct mach_buffer buffer = { 0 }; + mach_receive_message(response_port, &buffer, true); + if (buffer.message.descriptor.address) + printf("%s", (char*)buffer.message.descriptor.address); + mach_msg_destroy(&buffer.message.header); + } + + return msg_return == MACH_MSG_SUCCESS; +} + +static void* mach_connection_handler(void *context) { + struct mach_server* server = context; + while (server->is_running) { + struct mach_buffer* buffer = malloc(sizeof(struct mach_buffer)); + mach_receive_message(server->port, buffer, false); + server->handler(buffer); + } + + return NULL; +} + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +bool mach_server_begin(struct mach_server* mach_server, mach_handler handler) { + mach_server->task = mach_task_self(); + + if (mach_port_allocate(mach_server->task, + MACH_PORT_RIGHT_RECEIVE, + &mach_server->port ) != KERN_SUCCESS) { + return false; + } + + if (mach_port_insert_right(mach_server->task, + mach_server->port, + mach_server->port, + MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) { + return false; + } + + if (task_get_special_port(mach_server->task, + TASK_BOOTSTRAP_PORT, + &mach_server->bs_port) != KERN_SUCCESS) { + return false; + } + + if (bootstrap_register(mach_server->bs_port, + "git.felix.sketchybar", + mach_server->port ) != KERN_SUCCESS) { + return false; + } + + mach_server->handler = handler; + mach_server->is_running = true; + pthread_create(&mach_server->thread, NULL, &mach_connection_handler, + mach_server ); + + return true; +} +#pragma clang diagnostic pop diff --git a/src/misc/mach.h b/src/misc/mach.h new file mode 100644 index 00000000..05f113b2 --- /dev/null +++ b/src/misc/mach.h @@ -0,0 +1,36 @@ +#ifndef MACH_H +#define MACH_H + +#include +#include +#include + + +struct mach_message { + mach_msg_header_t header; + mach_msg_size_t msgh_descriptor_count; + mach_msg_ool_descriptor_t descriptor; +}; + +struct mach_buffer { + struct mach_message message; + mach_msg_trailer_t trailer; +}; + +#define MACH_HANDLER(name) void name(struct mach_buffer* message) +typedef MACH_HANDLER(mach_handler); + +struct mach_server { + bool is_running; + mach_port_name_t task; + mach_port_t port; + mach_port_t bs_port; + + pthread_t thread; + mach_handler* handler; +}; + +bool mach_server_begin(struct mach_server* mach_server, mach_handler handler); +bool mach_send_message(mach_port_t port, char* message, uint32_t len, + bool await_response); +#endif // !MACH_H diff --git a/src/misc/socket.c b/src/misc/socket.c deleted file mode 100644 index e666368c..00000000 --- a/src/misc/socket.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "socket.h" - -char* socket_read(int sockfd, int* len) { - int cursor = 0; - int bytes_read = 0; - char *result = NULL; - char buffer[BUFSIZ]; - - while ((bytes_read = read(sockfd, buffer, sizeof(buffer)-1)) > 0) { - char *temp = realloc(result, cursor+bytes_read+1); - if (!temp) goto err; - - result = temp; - memcpy(result+cursor, buffer, bytes_read); - cursor += bytes_read; - - if (((result+cursor)[-1] == '\0') && - ((result+cursor)[-2] == '\0')) { - break; - } - } - - if (result && bytes_read != -1) { - result[cursor] = '\0'; - *len = cursor; - } else { -err: - if (result) free(result); - result = NULL; - *len = 0; - } - - return result; -} - -bool socket_write_bytes(int sockfd, char* message, int len) { - return send(sockfd, message, len, 0) != -1; -} - -bool socket_write(int sockfd, char *message) { - return send(sockfd, message, strlen(message), 0) != -1; -} - -bool socket_set_timeout(int sockfd) { - struct timeval timeout = {SOCKET_TIMEOUT, 0}; - int rcv = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout); - int snd = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout); - return rcv >= 0 && snd >= 0; -} - -bool socket_connect_un(int* sockfd, char* socket_path) { - struct sockaddr_un socket_address; - socket_address.sun_family = AF_UNIX; - - *sockfd = socket(AF_UNIX, SOCK_STREAM, 0); - if (*sockfd == -1) return false; - - if (!socket_set_timeout(*sockfd)) return false; - - snprintf(socket_address.sun_path, sizeof(socket_address.sun_path), "%s", socket_path); - return connect(*sockfd, (struct sockaddr *) &socket_address, sizeof(socket_address)) != -1; -} - -void socket_close(int sockfd) { - shutdown(sockfd, SHUT_RDWR); - close(sockfd); -} - -static void *socket_connection_handler(void *context) { - struct daemon *daemon = context; - while (daemon->is_running) { - int sockfd = accept(daemon->sockfd, NULL, 0); - if (sockfd != -1 && socket_set_timeout(sockfd)) daemon->handler(sockfd); - } - - return NULL; -} - -bool socket_daemon_begin_un(struct daemon *daemon, char *socket_path, socket_daemon_handler *handler) { - struct sockaddr_un socket_address; - socket_address.sun_family = AF_UNIX; - snprintf(socket_address.sun_path, sizeof(socket_address.sun_path), "%s", socket_path); - unlink(socket_path); - - if (((daemon->sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) || (fcntl(daemon->sockfd, F_SETFD, FD_CLOEXEC | fcntl(daemon->sockfd, F_GETFD)) == -1)) { - return false; - } - - if (bind(daemon->sockfd, (struct sockaddr *) &socket_address, sizeof(socket_address)) == -1) { - return false; - } - - if (chmod(socket_path, 0600) != 0) { - return false; - } - - if (listen(daemon->sockfd, SOMAXCONN) == -1) { - return false; - } - - daemon->handler = handler; - daemon->is_running = true; - pthread_create(&daemon->thread, NULL, &socket_connection_handler, daemon); - - return true; -} - -void socket_daemon_end(struct daemon *daemon) { - daemon->is_running = false; - pthread_join(daemon->thread, NULL); - socket_close(daemon->sockfd); -} diff --git a/src/misc/socket.h b/src/misc/socket.h deleted file mode 100644 index abd3c3f1..00000000 --- a/src/misc/socket.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef SOCKET_H -#define SOCKET_H - -#define SOCKET_DAEMON_HANDLER(name) void name(int sockfd) -typedef SOCKET_DAEMON_HANDLER(socket_daemon_handler); - -#define FAILURE_MESSAGE "\x07" -#define SOCKET_TIMEOUT 1 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct daemon { - int sockfd; - bool is_running; - pthread_t thread; - socket_daemon_handler *handler; -}; - -char *socket_read(int sockfd, int *len); -bool socket_write_bytes(int sockfd, char *message, int len); -bool socket_write(int sockfd, char *message); -bool socket_connect_un(int *sockfd, char *socket_path); -void socket_wait(int sockfd); -void socket_close(int sockfd); -bool socket_daemon_begin_un(struct daemon *daemon, char *socket_path, socket_daemon_handler *handler); -void socket_daemon_end(struct daemon *daemon); - -#endif diff --git a/src/sketchybar.c b/src/sketchybar.c index aa7f4584..845fbdfc 100644 --- a/src/sketchybar.c +++ b/src/sketchybar.c @@ -1,5 +1,3 @@ -#include "misc/socket.h" -#define SOCKET_PATH_FMT "/tmp/sketchybar_%s.socket" #define LCFILE_PATH_FMT "/tmp/sketchybar_%s.lock" #define CLIENT_OPT_LONG "--message" @@ -19,11 +17,10 @@ extern int RunApplicationEventLoop(void); struct event_loop g_event_loop; void *g_workspace_context; -struct daemon g_daemon; +struct mach_server g_mach_server; struct bar_manager g_bar_manager; int g_connection; -char g_socket_file[MAXLEN]; char g_config_file[4096]; char g_lock_file[MAXLEN]; bool g_verbose; @@ -38,30 +35,16 @@ static int client_send_message(int argc, char **argv) { error("sketchybar-msg: 'env USER' not set! abort..\n"); } - int sockfd; - char socket_file[MAXLEN]; - snprintf(socket_file, sizeof(socket_file), SOCKET_PATH_FMT, user); - - int count = 0; - while (!socket_connect_un(&sockfd, socket_file)) { - count++; - if (count > 100) { - shutdown(sockfd, SHUT_WR); - socket_close(sockfd); - error("sketchybar-msg: failed to connect to socket..\n"); - } - } - int message_length = argc; int argl[argc]; for (int i = 1; i < argc; ++i) { argl[i] = strlen(argv[i]); - message_length += argl[i]; + message_length += argl[i] + 1; } - char message[message_length]; - char *temp = message; + char* message = malloc((sizeof(char) * (message_length + 1))); + char* temp = message; for (int i = 1; i < argc; ++i) { memcpy(temp, argv[i], argl[i]); @@ -70,47 +53,11 @@ static int client_send_message(int argc, char **argv) { } *temp++ = '\0'; - count = 0; - while (!socket_write_bytes(sockfd, message, message_length)) { - count++; - if (count > 100) { - shutdown(sockfd, SHUT_WR); - socket_close(sockfd); - error("sketchybar-msg: failed to send data..\n"); - } - } - - shutdown(sockfd, SHUT_WR); - int result = EXIT_SUCCESS; - int byte_count = 0; - char rsp[BUFSIZ]; + if (!mach_send_message(mach_get_bs_port(), message, message_length, true)) + return EXIT_FAILURE; - struct pollfd fds[] = { - { sockfd, POLLIN, 0 } - }; - - int timeout = 100; - while (poll(fds, 1, timeout) > 0) { - if (fds[0].revents & POLLIN) { - if ((byte_count = recv(sockfd, rsp, sizeof(rsp)-1, 0)) <= 0) { - break; - } - - rsp[byte_count] = '\0'; - - if (rsp[0] == FAILURE_MESSAGE[0]) { - result = EXIT_FAILURE; - fprintf(stderr, "%s", rsp + 1); - fflush(stderr); - } else { - fprintf(stdout, "%s", rsp); - fflush(stdout); - } - } - } + return 0; - socket_close(sockfd); - return result; } static void acquire_lockfile(void) { @@ -179,7 +126,6 @@ static inline void init_misc_settings(void) { error("sketchybar: 'env USER' not set! abort..\n"); } - snprintf(g_socket_file, sizeof(g_socket_file), SOCKET_PATH_FMT, user); snprintf(g_lock_file, sizeof(g_lock_file), LCFILE_PATH_FMT, user); NSApplicationLoad(); @@ -226,7 +172,7 @@ int main(int argc, char **argv) { workspace_event_handler_begin(&g_workspace_context); bar_manager_begin(&g_bar_manager); - if (!socket_daemon_begin_un(&g_daemon, g_socket_file, message_handler)) + if (!mach_server_begin(&g_mach_server, mach_message_handler)) error("sketchybar: could not initialize daemon! abort..\n"); exec_config_file();