aboutsummaryrefslogtreecommitdiffstats
path: root/sway/ipc-server.c
diff options
context:
space:
mode:
authorLibravatar M Stoeckl <code@mstoeckl.com>2019-01-20 13:51:12 -0500
committerLibravatar emersion <contact@emersion.fr>2019-01-21 12:59:42 +0100
commit1211a81aad18bbc4d9e8fb9973238ad8e7e1f688 (patch)
tree5c3f60e0219cb8b4a1b7cafb760a871661866e32 /sway/ipc-server.c
parentLog libinput_config_status errors (diff)
downloadsway-1211a81aad18bbc4d9e8fb9973238ad8e7e1f688.tar.gz
sway-1211a81aad18bbc4d9e8fb9973238ad8e7e1f688.tar.zst
sway-1211a81aad18bbc4d9e8fb9973238ad8e7e1f688.zip
Replace wlr_log with sway_log
This commit mostly duplicates the wlr_log functions, although with a sway_* prefix. (This is very similar to PR #2009.) However, the logging function no longer needs to be replaceable, so sway_log_init's second argument is used to set the exit callback for sway_abort. wlr_log_init is still invoked in sway/main.c This commit makes it easier to remove the wlroots dependency for the helper programs swaymsg, swaybg, swaybar, and swaynag.
Diffstat (limited to 'sway/ipc-server.c')
-rw-r--r--sway/ipc-server.c70
1 files changed, 35 insertions, 35 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 9a033a4b..82e144b7 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -147,32 +147,32 @@ struct sockaddr_un *ipc_user_sockaddr(void) {
147int ipc_handle_connection(int fd, uint32_t mask, void *data) { 147int ipc_handle_connection(int fd, uint32_t mask, void *data) {
148 (void) fd; 148 (void) fd;
149 struct sway_server *server = data; 149 struct sway_server *server = data;
150 wlr_log(WLR_DEBUG, "Event on IPC listening socket"); 150 sway_log(SWAY_DEBUG, "Event on IPC listening socket");
151 assert(mask == WL_EVENT_READABLE); 151 assert(mask == WL_EVENT_READABLE);
152 152
153 int client_fd = accept(ipc_socket, NULL, NULL); 153 int client_fd = accept(ipc_socket, NULL, NULL);
154 if (client_fd == -1) { 154 if (client_fd == -1) {
155 wlr_log_errno(WLR_ERROR, "Unable to accept IPC client connection"); 155 sway_log_errno(SWAY_ERROR, "Unable to accept IPC client connection");
156 return 0; 156 return 0;
157 } 157 }
158 158
159 int flags; 159 int flags;
160 if ((flags = fcntl(client_fd, F_GETFD)) == -1 160 if ((flags = fcntl(client_fd, F_GETFD)) == -1
161 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) { 161 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
162 wlr_log_errno(WLR_ERROR, "Unable to set CLOEXEC on IPC client socket"); 162 sway_log_errno(SWAY_ERROR, "Unable to set CLOEXEC on IPC client socket");
163 close(client_fd); 163 close(client_fd);
164 return 0; 164 return 0;
165 } 165 }
166 if ((flags = fcntl(client_fd, F_GETFL)) == -1 166 if ((flags = fcntl(client_fd, F_GETFL)) == -1
167 || fcntl(client_fd, F_SETFL, flags|O_NONBLOCK) == -1) { 167 || fcntl(client_fd, F_SETFL, flags|O_NONBLOCK) == -1) {
168 wlr_log_errno(WLR_ERROR, "Unable to set NONBLOCK on IPC client socket"); 168 sway_log_errno(SWAY_ERROR, "Unable to set NONBLOCK on IPC client socket");
169 close(client_fd); 169 close(client_fd);
170 return 0; 170 return 0;
171 } 171 }
172 172
173 struct ipc_client *client = malloc(sizeof(struct ipc_client)); 173 struct ipc_client *client = malloc(sizeof(struct ipc_client));
174 if (!client) { 174 if (!client) {
175 wlr_log(WLR_ERROR, "Unable to allocate ipc client"); 175 sway_log(SWAY_ERROR, "Unable to allocate ipc client");
176 close(client_fd); 176 close(client_fd);
177 return 0; 177 return 0;
178 } 178 }
@@ -188,12 +188,12 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
188 client->write_buffer_len = 0; 188 client->write_buffer_len = 0;
189 client->write_buffer = malloc(client->write_buffer_size); 189 client->write_buffer = malloc(client->write_buffer_size);
190 if (!client->write_buffer) { 190 if (!client->write_buffer) {
191 wlr_log(WLR_ERROR, "Unable to allocate ipc client write buffer"); 191 sway_log(SWAY_ERROR, "Unable to allocate ipc client write buffer");
192 close(client_fd); 192 close(client_fd);
193 return 0; 193 return 0;
194 } 194 }
195 195
196 wlr_log(WLR_DEBUG, "New client: fd %d", client_fd); 196 sway_log(SWAY_DEBUG, "New client: fd %d", client_fd);
197 list_add(ipc_client_list, client); 197 list_add(ipc_client_list, client);
198 return 0; 198 return 0;
199} 199}
@@ -202,22 +202,22 @@ int 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
204 if (mask & WL_EVENT_ERROR) { 204 if (mask & WL_EVENT_ERROR) {
205 wlr_log(WLR_ERROR, "IPC Client socket error, removing client"); 205 sway_log(SWAY_ERROR, "IPC Client socket error, removing client");
206 ipc_client_disconnect(client); 206 ipc_client_disconnect(client);
207 return 0; 207 return 0;
208 } 208 }
209 209
210 if (mask & WL_EVENT_HANGUP) { 210 if (mask & WL_EVENT_HANGUP) {
211 wlr_log(WLR_DEBUG, "Client %d hung up", client->fd); 211 sway_log(SWAY_DEBUG, "Client %d hung up", client->fd);
212 ipc_client_disconnect(client); 212 ipc_client_disconnect(client);
213 return 0; 213 return 0;
214 } 214 }
215 215
216 wlr_log(WLR_DEBUG, "Client %d readable", client->fd); 216 sway_log(SWAY_DEBUG, "Client %d readable", client->fd);
217 217
218 int read_available; 218 int read_available;
219 if (ioctl(client_fd, FIONREAD, &read_available) == -1) { 219 if (ioctl(client_fd, FIONREAD, &read_available) == -1) {
220 wlr_log_errno(WLR_INFO, "Unable to read IPC socket buffer size"); 220 sway_log_errno(SWAY_INFO, "Unable to read IPC socket buffer size");
221 ipc_client_disconnect(client); 221 ipc_client_disconnect(client);
222 return 0; 222 return 0;
223 } 223 }
@@ -239,13 +239,13 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
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 sway_log_errno(SWAY_INFO, "Unable to receive header from IPC client");
243 ipc_client_disconnect(client); 243 ipc_client_disconnect(client);
244 return 0; 244 return 0;
245 } 245 }
246 246
247 if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { 247 if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) {
248 wlr_log(WLR_DEBUG, "IPC header check failed"); 248 sway_log(SWAY_DEBUG, "IPC header check failed");
249 ipc_client_disconnect(client); 249 ipc_client_disconnect(client);
250 return 0; 250 return 0;
251 } 251 }
@@ -279,7 +279,7 @@ static void ipc_send_event(const char *json_string, enum ipc_command_type event)
279 } 279 }
280 client->current_command = event; 280 client->current_command = event;
281 if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) { 281 if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) {
282 wlr_log_errno(WLR_INFO, "Unable to send reply to IPC client"); 282 sway_log_errno(SWAY_INFO, "Unable to send reply to IPC client");
283 /* ipc_send_reply destroys client on error, which also 283 /* ipc_send_reply destroys client on error, which also
284 * removes it from the list, so we need to process 284 * removes it from the list, so we need to process
285 * current index again */ 285 * current index again */
@@ -293,7 +293,7 @@ void ipc_event_workspace(struct sway_workspace *old,
293 if (!ipc_has_event_listeners(IPC_EVENT_WORKSPACE)) { 293 if (!ipc_has_event_listeners(IPC_EVENT_WORKSPACE)) {
294 return; 294 return;
295 } 295 }
296 wlr_log(WLR_DEBUG, "Sending workspace::%s event", change); 296 sway_log(SWAY_DEBUG, "Sending workspace::%s event", change);
297 json_object *obj = json_object_new_object(); 297 json_object *obj = json_object_new_object();
298 json_object_object_add(obj, "change", json_object_new_string(change)); 298 json_object_object_add(obj, "change", json_object_new_string(change));
299 if (old) { 299 if (old) {
@@ -319,7 +319,7 @@ void ipc_event_window(struct sway_container *window, const char *change) {
319 if (!ipc_has_event_listeners(IPC_EVENT_WINDOW)) { 319 if (!ipc_has_event_listeners(IPC_EVENT_WINDOW)) {
320 return; 320 return;
321 } 321 }
322 wlr_log(WLR_DEBUG, "Sending window::%s event", change); 322 sway_log(SWAY_DEBUG, "Sending window::%s event", change);
323 json_object *obj = json_object_new_object(); 323 json_object *obj = json_object_new_object();
324 json_object_object_add(obj, "change", json_object_new_string(change)); 324 json_object_object_add(obj, "change", json_object_new_string(change));
325 json_object_object_add(obj, "container", 325 json_object_object_add(obj, "container",
@@ -334,7 +334,7 @@ void ipc_event_barconfig_update(struct bar_config *bar) {
334 if (!ipc_has_event_listeners(IPC_EVENT_BARCONFIG_UPDATE)) { 334 if (!ipc_has_event_listeners(IPC_EVENT_BARCONFIG_UPDATE)) {
335 return; 335 return;
336 } 336 }
337 wlr_log(WLR_DEBUG, "Sending barconfig_update event"); 337 sway_log(SWAY_DEBUG, "Sending barconfig_update event");
338 json_object *json = ipc_json_describe_bar_config(bar); 338 json_object *json = ipc_json_describe_bar_config(bar);
339 339
340 const char *json_string = json_object_to_json_string(json); 340 const char *json_string = json_object_to_json_string(json);
@@ -346,7 +346,7 @@ void ipc_event_bar_state_update(struct bar_config *bar) {
346 if (!ipc_has_event_listeners(IPC_EVENT_BAR_STATE_UPDATE)) { 346 if (!ipc_has_event_listeners(IPC_EVENT_BAR_STATE_UPDATE)) {
347 return; 347 return;
348 } 348 }
349 wlr_log(WLR_DEBUG, "Sending bar_state_update event"); 349 sway_log(SWAY_DEBUG, "Sending bar_state_update event");
350 350
351 json_object *json = json_object_new_object(); 351 json_object *json = json_object_new_object();
352 json_object_object_add(json, "id", json_object_new_string(bar->id)); 352 json_object_object_add(json, "id", json_object_new_string(bar->id));
@@ -362,7 +362,7 @@ void ipc_event_mode(const char *mode, bool pango) {
362 if (!ipc_has_event_listeners(IPC_EVENT_MODE)) { 362 if (!ipc_has_event_listeners(IPC_EVENT_MODE)) {
363 return; 363 return;
364 } 364 }
365 wlr_log(WLR_DEBUG, "Sending mode::%s event", mode); 365 sway_log(SWAY_DEBUG, "Sending mode::%s event", mode);
366 json_object *obj = json_object_new_object(); 366 json_object *obj = json_object_new_object();
367 json_object_object_add(obj, "change", json_object_new_string(mode)); 367 json_object_object_add(obj, "change", json_object_new_string(mode));
368 json_object_object_add(obj, "pango_markup", 368 json_object_object_add(obj, "pango_markup",
@@ -377,7 +377,7 @@ void ipc_event_shutdown(const char *reason) {
377 if (!ipc_has_event_listeners(IPC_EVENT_SHUTDOWN)) { 377 if (!ipc_has_event_listeners(IPC_EVENT_SHUTDOWN)) {
378 return; 378 return;
379 } 379 }
380 wlr_log(WLR_DEBUG, "Sending shutdown::%s event", reason); 380 sway_log(SWAY_DEBUG, "Sending shutdown::%s event", reason);
381 381
382 json_object *json = json_object_new_object(); 382 json_object *json = json_object_new_object();
383 json_object_object_add(json, "change", json_object_new_string(reason)); 383 json_object_object_add(json, "change", json_object_new_string(reason));
@@ -391,7 +391,7 @@ void ipc_event_binding(struct sway_binding *binding) {
391 if (!ipc_has_event_listeners(IPC_EVENT_BINDING)) { 391 if (!ipc_has_event_listeners(IPC_EVENT_BINDING)) {
392 return; 392 return;
393 } 393 }
394 wlr_log(WLR_DEBUG, "Sending binding event"); 394 sway_log(SWAY_DEBUG, "Sending binding event");
395 395
396 json_object *json_binding = json_object_new_object(); 396 json_object *json_binding = json_object_new_object();
397 json_object_object_add(json_binding, "command", json_object_new_string(binding->command)); 397 json_object_object_add(json_binding, "command", json_object_new_string(binding->command));
@@ -464,7 +464,7 @@ static void ipc_event_tick(const char *payload) {
464 if (!ipc_has_event_listeners(IPC_EVENT_TICK)) { 464 if (!ipc_has_event_listeners(IPC_EVENT_TICK)) {
465 return; 465 return;
466 } 466 }
467 wlr_log(WLR_DEBUG, "Sending tick event"); 467 sway_log(SWAY_DEBUG, "Sending tick event");
468 468
469 json_object *json = json_object_new_object(); 469 json_object *json = json_object_new_object();
470 json_object_object_add(json, "first", json_object_new_boolean(false)); 470 json_object_object_add(json, "first", json_object_new_boolean(false));
@@ -479,13 +479,13 @@ int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) {
479 struct ipc_client *client = data; 479 struct ipc_client *client = data;
480 480
481 if (mask & WL_EVENT_ERROR) { 481 if (mask & WL_EVENT_ERROR) {
482 wlr_log(WLR_ERROR, "IPC Client socket error, removing client"); 482 sway_log(SWAY_ERROR, "IPC Client socket error, removing client");
483 ipc_client_disconnect(client); 483 ipc_client_disconnect(client);
484 return 0; 484 return 0;
485 } 485 }
486 486
487 if (mask & WL_EVENT_HANGUP) { 487 if (mask & WL_EVENT_HANGUP) {
488 wlr_log(WLR_DEBUG, "Client %d hung up", client->fd); 488 sway_log(SWAY_DEBUG, "Client %d hung up", client->fd);
489 ipc_client_disconnect(client); 489 ipc_client_disconnect(client);
490 return 0; 490 return 0;
491 } 491 }
@@ -494,14 +494,14 @@ int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) {
494 return 0; 494 return 0;
495 } 495 }
496 496
497 wlr_log(WLR_DEBUG, "Client %d writable", client->fd); 497 sway_log(SWAY_DEBUG, "Client %d writable", client->fd);
498 498
499 ssize_t written = write(client->fd, client->write_buffer, client->write_buffer_len); 499 ssize_t written = write(client->fd, client->write_buffer, client->write_buffer_len);
500 500
501 if (written == -1 && errno == EAGAIN) { 501 if (written == -1 && errno == EAGAIN) {
502 return 0; 502 return 0;
503 } else if (written == -1) { 503 } else if (written == -1) {
504 wlr_log_errno(WLR_INFO, "Unable to send data from queue to IPC client"); 504 sway_log_errno(SWAY_INFO, "Unable to send data from queue to IPC client");
505 ipc_client_disconnect(client); 505 ipc_client_disconnect(client);
506 return 0; 506 return 0;
507 } 507 }
@@ -524,7 +524,7 @@ void ipc_client_disconnect(struct ipc_client *client) {
524 524
525 shutdown(client->fd, SHUT_RDWR); 525 shutdown(client->fd, SHUT_RDWR);
526 526
527 wlr_log(WLR_INFO, "IPC Client %d disconnected", client->fd); 527 sway_log(SWAY_INFO, "IPC Client %d disconnected", client->fd);
528 wl_event_source_remove(client->event_source); 528 wl_event_source_remove(client->event_source);
529 if (client->writable_event_source) { 529 if (client->writable_event_source) {
530 wl_event_source_remove(client->writable_event_source); 530 wl_event_source_remove(client->writable_event_source);
@@ -573,7 +573,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
573 573
574 char *buf = malloc(client->payload_length + 1); 574 char *buf = malloc(client->payload_length + 1);
575 if (!buf) { 575 if (!buf) {
576 wlr_log_errno(WLR_INFO, "Unable to allocate IPC payload"); 576 sway_log_errno(SWAY_INFO, "Unable to allocate IPC payload");
577 ipc_client_disconnect(client); 577 ipc_client_disconnect(client);
578 return; 578 return;
579 } 579 }
@@ -582,7 +582,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
582 ssize_t received = recv(client->fd, buf, client->payload_length, 0); 582 ssize_t received = recv(client->fd, buf, client->payload_length, 0);
583 if (received == -1) 583 if (received == -1)
584 { 584 {
585 wlr_log_errno(WLR_INFO, "Unable to receive payload from IPC client"); 585 sway_log_errno(SWAY_INFO, "Unable to receive payload from IPC client");
586 ipc_client_disconnect(client); 586 ipc_client_disconnect(client);
587 free(buf); 587 free(buf);
588 return; 588 return;
@@ -667,7 +667,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
667 if (request == NULL || !json_object_is_type(request, json_type_array)) { 667 if (request == NULL || !json_object_is_type(request, json_type_array)) {
668 const char msg[] = "{\"success\": false}"; 668 const char msg[] = "{\"success\": false}";
669 client_valid = ipc_send_reply(client, msg, strlen(msg)); 669 client_valid = ipc_send_reply(client, msg, strlen(msg));
670 wlr_log(WLR_INFO, "Failed to parse subscribe request"); 670 sway_log(SWAY_INFO, "Failed to parse subscribe request");
671 goto exit_cleanup; 671 goto exit_cleanup;
672 } 672 }
673 673
@@ -696,7 +696,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
696 const char msg[] = "{\"success\": false}"; 696 const char msg[] = "{\"success\": false}";
697 client_valid = ipc_send_reply(client, msg, strlen(msg)); 697 client_valid = ipc_send_reply(client, msg, strlen(msg));
698 json_object_put(request); 698 json_object_put(request);
699 wlr_log(WLR_INFO, "Unsupported event type in subscribe request"); 699 sway_log(SWAY_INFO, "Unsupported event type in subscribe request");
700 goto exit_cleanup; 700 goto exit_cleanup;
701 } 701 }
702 } 702 }
@@ -845,7 +845,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
845 } 845 }
846 846
847 default: 847 default:
848 wlr_log(WLR_INFO, "Unknown IPC command type %i", client->current_command); 848 sway_log(SWAY_INFO, "Unknown IPC command type %i", client->current_command);
849 goto exit_cleanup; 849 goto exit_cleanup;
850 } 850 }
851 851
@@ -873,14 +873,14 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay
873 } 873 }
874 874
875 if (client->write_buffer_size > 4e6) { // 4 MB 875 if (client->write_buffer_size > 4e6) { // 4 MB
876 wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client"); 876 sway_log(SWAY_ERROR, "Client write buffer too big, disconnecting client");
877 ipc_client_disconnect(client); 877 ipc_client_disconnect(client);
878 return false; 878 return false;
879 } 879 }
880 880
881 char *new_buffer = realloc(client->write_buffer, client->write_buffer_size); 881 char *new_buffer = realloc(client->write_buffer, client->write_buffer_size);
882 if (!new_buffer) { 882 if (!new_buffer) {
883 wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer"); 883 sway_log(SWAY_ERROR, "Unable to reallocate ipc client write buffer");
884 ipc_client_disconnect(client); 884 ipc_client_disconnect(client);
885 return false; 885 return false;
886 } 886 }
@@ -897,6 +897,6 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay
897 ipc_client_handle_writable, client); 897 ipc_client_handle_writable, client);
898 } 898 }
899 899
900 wlr_log(WLR_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload); 900 sway_log(SWAY_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload);
901 return true; 901 return true;
902} 902}