X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libi3%2Fipc_recv_message.c;h=84da5aa36cb90d7b913ec7c0afc87568102a9d70;hb=a0105dd2f22a96fe1b99be7eee9f716d6ef071e4;hp=f5c4a62b69dc7ba8742ce1832b4e22422fcc05a3;hpb=1c0b542c07546532b6d09c5805db478a96af10b5;p=i3%2Fi3 diff --git a/libi3/ipc_recv_message.c b/libi3/ipc_recv_message.c index f5c4a62b..84da5aa3 100644 --- a/libi3/ipc_recv_message.c +++ b/libi3/ipc_recv_message.c @@ -2,75 +2,87 @@ * vim:ts=4:sw=4:expandtab * * i3 - an improved dynamic tiling window manager - * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE) + * © 2009 Michael Stapelberg and contributors (see also: LICENSE) * */ +#include "libi3.h" + #include #include #include #include #include +#include +#include #include -#include "libi3.h" - /* * Reads a message from the given socket file descriptor and stores its length * (reply_length) as well as a pointer to its contents (reply). * * Returns -1 when read() fails, errno will remain. - * Returns -2 when the IPC protocol is violated (invalid magic, unexpected + * Returns -2 on EOF. + * Returns -3 when the IPC protocol is violated (invalid magic, unexpected * message type, EOF instead of a message). Additionally, the error will be * printed to stderr. * Returns 0 on success. * */ -int ipc_recv_message(int sockfd, uint32_t message_type, +int ipc_recv_message(int sockfd, uint32_t *message_type, uint32_t *reply_length, uint8_t **reply) { /* Read the message header first */ - uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t); + const uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t); char msg[to_read]; char *walk = msg; uint32_t read_bytes = 0; while (read_bytes < to_read) { - int n = read(sockfd, msg + read_bytes, to_read); + int n = read(sockfd, msg + read_bytes, to_read - read_bytes); if (n == -1) return -1; if (n == 0) { - fprintf(stderr, "IPC: received EOF instead of reply\n"); - return -2; + if (read_bytes == 0) { + return -2; + } else { + ELOG("IPC: unexpected EOF while reading header, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n", + read_bytes, to_read); + return -3; + } } read_bytes += n; - to_read -= n; } if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) { - fprintf(stderr, "IPC: invalid magic in reply\n"); - return -2; + ELOG("IPC: invalid magic in header, got \"%.*s\", want \"%s\"\n", + (int)strlen(I3_IPC_MAGIC), walk, I3_IPC_MAGIC); + return -3; } walk += strlen(I3_IPC_MAGIC); - *reply_length = *((uint32_t*)walk); + memcpy(reply_length, walk, sizeof(uint32_t)); walk += sizeof(uint32_t); - if (*((uint32_t*)walk) != message_type) { - fprintf(stderr, "IPC: unexpected reply type (got %d, expected %d)\n", *((uint32_t*)walk), message_type); - return -2; - } + if (message_type != NULL) + memcpy(message_type, walk, sizeof(uint32_t)); *reply = smalloc(*reply_length); - to_read = *reply_length; read_bytes = 0; - while (read_bytes < to_read) { - int n = read(sockfd, *reply + read_bytes, to_read); - if (n == -1) + while (read_bytes < *reply_length) { + const int n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes); + if (n == -1) { + if (errno == EINTR || errno == EAGAIN) + continue; return -1; + } + if (n == 0) { + ELOG("IPC: unexpected EOF while reading payload, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n", + read_bytes, *reply_length); + return -3; + } read_bytes += n; - to_read -= n; } return 0;