aboutsummaryrefslogtreecommitdiffstats
path: root/sway/ipc-server.c
diff options
context:
space:
mode:
authorLibravatar Connor E <38229097+c-edw@users.noreply.github.com>2019-01-16 09:57:51 +0000
committerLibravatar emersion <contact@emersion.fr>2019-01-16 13:02:26 +0100
commitde6f5b345380e80b4d59ebc569697683af064424 (patch)
tree2ba6738b30a3cdcbceb0d8307424217fca448252 /sway/ipc-server.c
parentRemove usage of VLAs. (diff)
downloadsway-de6f5b345380e80b4d59ebc569697683af064424.tar.gz
sway-de6f5b345380e80b4d59ebc569697683af064424.tar.zst
sway-de6f5b345380e80b4d59ebc569697683af064424.zip
Use static arrays where possible.
Diffstat (limited to 'sway/ipc-server.c')
-rw-r--r--sway/ipc-server.c28
1 files changed, 10 insertions, 18 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 73fa8ae5..9a033a4b 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -39,6 +39,8 @@ static struct wl_listener ipc_display_destroy;
39 39
40static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; 40static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
41 41
42#define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8)
43
42struct ipc_client { 44struct ipc_client {
43 struct wl_event_source *event_source; 45 struct wl_event_source *event_source;
44 struct wl_event_source *writable_event_source; 46 struct wl_event_source *writable_event_source;
@@ -196,8 +198,6 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
196 return 0; 198 return 0;
197} 199}
198 200
199static const int ipc_header_size = sizeof(ipc_magic) + 8;
200
201int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { 201int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
202 struct ipc_client *client = data; 202 struct ipc_client *client = data;
203 203
@@ -230,33 +230,29 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
230 return 0; 230 return 0;
231 } 231 }
232 232
233 if (read_available < ipc_header_size) { 233 if (read_available < (int) IPC_HEADER_SIZE) {
234 return 0; 234 return 0;
235 } 235 }
236 236
237 uint8_t *buf = malloc(sizeof(uint8_t) * ipc_header_size); 237 uint8_t buf[IPC_HEADER_SIZE];
238 uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic)); 238 uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic));
239 // Should be fully available, because read_available >= ipc_header_size 239 // Should be fully available, because read_available >= IPC_HEADER_SIZE
240 ssize_t received = recv(client_fd, buf, ipc_header_size, 0); 240 ssize_t received = recv(client_fd, buf, IPC_HEADER_SIZE, 0);
241 if (received == -1) { 241 if (received == -1) {
242 wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client"); 242 wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client");
243 ipc_client_disconnect(client); 243 ipc_client_disconnect(client);
244 free(buf);
245 return 0; 244 return 0;
246 } 245 }
247 246
248 if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { 247 if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) {
249 wlr_log(WLR_DEBUG, "IPC header check failed"); 248 wlr_log(WLR_DEBUG, "IPC header check failed");
250 ipc_client_disconnect(client); 249 ipc_client_disconnect(client);
251 free(buf);
252 return 0; 250 return 0;
253 } 251 }
254 252
255 memcpy(&client->payload_length, &buf32[0], sizeof(buf32[0])); 253 memcpy(&client->payload_length, &buf32[0], sizeof(buf32[0]));
256 memcpy(&client->current_command, &buf32[1], sizeof(buf32[1])); 254 memcpy(&client->current_command, &buf32[1], sizeof(buf32[1]));
257 255
258 free(buf);
259
260 if (read_available - received >= (long)client->payload_length) { 256 if (read_available - received >= (long)client->payload_length) {
261 ipc_client_handle_command(client); 257 ipc_client_handle_command(client);
262 } 258 }
@@ -864,14 +860,14 @@ exit_cleanup:
864bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) { 860bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) {
865 assert(payload); 861 assert(payload);
866 862
867 char *data = malloc(sizeof(char) * ipc_header_size); 863 char data[IPC_HEADER_SIZE];
868 uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic)); 864 uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic));
869 865
870 memcpy(data, ipc_magic, sizeof(ipc_magic)); 866 memcpy(data, ipc_magic, sizeof(ipc_magic));
871 memcpy(&data32[0], &payload_length, sizeof(payload_length)); 867 memcpy(&data32[0], &payload_length, sizeof(payload_length));
872 memcpy(&data32[1], &client->current_command, sizeof(client->current_command)); 868 memcpy(&data32[1], &client->current_command, sizeof(client->current_command));
873 869
874 while (client->write_buffer_len + ipc_header_size + payload_length >= 870 while (client->write_buffer_len + IPC_HEADER_SIZE + payload_length >=
875 client->write_buffer_size) { 871 client->write_buffer_size) {
876 client->write_buffer_size *= 2; 872 client->write_buffer_size *= 2;
877 } 873 }
@@ -879,7 +875,6 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay
879 if (client->write_buffer_size > 4e6) { // 4 MB 875 if (client->write_buffer_size > 4e6) { // 4 MB
880 wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client"); 876 wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client");
881 ipc_client_disconnect(client); 877 ipc_client_disconnect(client);
882 free(data);
883 return false; 878 return false;
884 } 879 }
885 880
@@ -887,18 +882,15 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay
887 if (!new_buffer) { 882 if (!new_buffer) {
888 wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer"); 883 wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer");
889 ipc_client_disconnect(client); 884 ipc_client_disconnect(client);
890 free(data);
891 return false; 885 return false;
892 } 886 }
893 client->write_buffer = new_buffer; 887 client->write_buffer = new_buffer;
894 888
895 memcpy(client->write_buffer + client->write_buffer_len, data, ipc_header_size); 889 memcpy(client->write_buffer + client->write_buffer_len, data, IPC_HEADER_SIZE);
896 client->write_buffer_len += ipc_header_size; 890 client->write_buffer_len += IPC_HEADER_SIZE;
897 memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length); 891 memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length);
898 client->write_buffer_len += payload_length; 892 client->write_buffer_len += payload_length;
899 893
900 free(data);
901
902 if (!client->writable_event_source) { 894 if (!client->writable_event_source) {
903 client->writable_event_source = wl_event_loop_add_fd( 895 client->writable_event_source = wl_event_loop_add_fd(
904 server.wl_event_loop, client->fd, WL_EVENT_WRITABLE, 896 server.wl_event_loop, client->fd, WL_EVENT_WRITABLE,