summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2016-12-15 18:07:09 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2016-12-15 19:01:41 -0500
commit31b002b6d5ca13be76c1991e50457766556b3c55 (patch)
tree289be6b10aab631a8a1dd19272bd4c40e3d74d94
parentHandle some more memory allocation failures (diff)
downloadsway-31b002b6d5ca13be76c1991e50457766556b3c55.tar.gz
sway-31b002b6d5ca13be76c1991e50457766556b3c55.tar.zst
sway-31b002b6d5ca13be76c1991e50457766556b3c55.zip
Handle IPC server allocation failures
-rw-r--r--sway/ipc-server.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index ba0cb310..f5be9e37 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -106,7 +106,7 @@ void ipc_terminate(void) {
106struct sockaddr_un *ipc_user_sockaddr(void) { 106struct sockaddr_un *ipc_user_sockaddr(void) {
107 struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); 107 struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un));
108 if (ipc_sockaddr == NULL) { 108 if (ipc_sockaddr == NULL) {
109 sway_abort("can't malloc ipc_sockaddr"); 109 sway_abort("Can't allocate ipc_sockaddr");
110 } 110 }
111 111
112 ipc_sockaddr->sun_family = AF_UNIX; 112 ipc_sockaddr->sun_family = AF_UNIX;
@@ -119,7 +119,7 @@ struct sockaddr_un *ipc_user_sockaddr(void) {
119 } 119 }
120 if (path_size <= snprintf(ipc_sockaddr->sun_path, path_size, 120 if (path_size <= snprintf(ipc_sockaddr->sun_path, path_size,
121 "%s/sway-ipc.%i.%i.sock", dir, getuid(), getpid())) { 121 "%s/sway-ipc.%i.%i.sock", dir, getuid(), getpid())) {
122 sway_abort("socket path won't fit into ipc_sockaddr->sun_path"); 122 sway_abort("Socket path won't fit into ipc_sockaddr->sun_path");
123 } 123 }
124 124
125 return ipc_sockaddr; 125 return ipc_sockaddr;
@@ -148,13 +148,13 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
148 148
149 int client_fd = accept(ipc_socket, NULL, NULL); 149 int client_fd = accept(ipc_socket, NULL, NULL);
150 if (client_fd == -1) { 150 if (client_fd == -1) {
151 sway_log_errno(L_INFO, "Unable to accept IPC client connection"); 151 sway_log_errno(L_ERROR, "Unable to accept IPC client connection");
152 return 0; 152 return 0;
153 } 153 }
154 154
155 int flags; 155 int flags;
156 if ((flags=fcntl(client_fd, F_GETFD)) == -1 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) { 156 if ((flags=fcntl(client_fd, F_GETFD)) == -1 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
157 sway_log_errno(L_INFO, "Unable to set CLOEXEC on IPC client socket"); 157 sway_log_errno(L_ERROR, "Unable to set CLOEXEC on IPC client socket");
158 close(client_fd); 158 close(client_fd);
159 return 0; 159 return 0;
160 } 160 }
@@ -171,6 +171,11 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
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) {
175 sway_log(L_ERROR, "Unable to allocate ipc client");
176 close(client_fd);
177 return 0;
178 }
174 client->payload_length = 0; 179 client->payload_length = 0;
175 client->fd = client_fd; 180 client->fd = client_fd;
176 client->subscribed_events = 0; 181 client->subscribed_events = 0;
@@ -187,7 +192,7 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
187 struct ipc_client *client = data; 192 struct ipc_client *client = data;
188 193
189 if (mask & WLC_EVENT_ERROR) { 194 if (mask & WLC_EVENT_ERROR) {
190 sway_log(L_INFO, "IPC Client socket error, removing client"); 195 sway_log(L_ERROR, "IPC Client socket error, removing client");
191 client->fd = -1; 196 client->fd = -1;
192 ipc_client_disconnect(client); 197 ipc_client_disconnect(client);
193 return 0; 198 return 0;
@@ -291,6 +296,12 @@ void ipc_get_pixels(wlc_handle output) {
291 char response_header[9]; 296 char response_header[9];
292 memset(response_header, 0, sizeof(response_header)); 297 memset(response_header, 0, sizeof(response_header));
293 char *data = malloc(sizeof(response_header) + size->w * size->h * 4); 298 char *data = malloc(sizeof(response_header) + size->w * size->h * 4);
299 if (!data) {
300 sway_log(L_ERROR, "Unable to allocate pixels for get_pixels");
301 ipc_client_disconnect(client);
302 free(req);
303 continue;
304 }
294 wlc_pixels_read(WLC_RGBA8888, &req->geo, &g_out, data + sizeof(response_header)); 305 wlc_pixels_read(WLC_RGBA8888, &req->geo, &g_out, data + sizeof(response_header));
295 306
296 response_header[0] = 1; 307 response_header[0] = 1;
@@ -318,7 +329,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
318 329
319 char *buf = malloc(client->payload_length + 1); 330 char *buf = malloc(client->payload_length + 1);
320 if (!buf) { 331 if (!buf) {
321 sway_log_errno(L_INFO, "Out of memory"); 332 sway_log_errno(L_INFO, "Unable to allocate IPC payload");
322 ipc_client_disconnect(client); 333 ipc_client_disconnect(client);
323 return; 334 return;
324 } 335 }
@@ -497,6 +508,10 @@ void ipc_client_handle_command(struct ipc_client *client) {
497 goto exit_cleanup; 508 goto exit_cleanup;
498 } 509 }
499 struct get_pixels_request *req = malloc(sizeof(struct get_pixels_request)); 510 struct get_pixels_request *req = malloc(sizeof(struct get_pixels_request));
511 if (!req) {
512 sway_log(L_ERROR, "Unable to allocate get_pixels request");
513 goto exit_cleanup;
514 }
500 req->client = client; 515 req->client = client;
501 req->output = output->handle; 516 req->output = output->handle;
502 req->geo = g; 517 req->geo = g;