2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
21 * Reads a message from the given socket file descriptor and stores its length
22 * (reply_length) as well as a pointer to its contents (reply).
24 * Returns -1 when read() fails, errno will remain.
26 * Returns -3 when the IPC protocol is violated (invalid magic, unexpected
27 * message type, EOF instead of a message). Additionally, the error will be
29 * Returns 0 on success.
32 int ipc_recv_message(int sockfd, uint32_t *message_type,
33 uint32_t *reply_length, uint8_t **reply) {
34 /* Read the message header first */
35 const uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
39 uint32_t read_bytes = 0;
40 while (read_bytes < to_read) {
41 int n = read(sockfd, msg + read_bytes, to_read - read_bytes);
45 if (read_bytes == 0) {
48 ELOG("IPC: unexpected EOF while reading header, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n",
57 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
58 ELOG("IPC: invalid magic in header, got \"%.*s\", want \"%s\"\n",
59 (int)strlen(I3_IPC_MAGIC), walk, I3_IPC_MAGIC);
63 walk += strlen(I3_IPC_MAGIC);
64 memcpy(reply_length, walk, sizeof(uint32_t));
65 walk += sizeof(uint32_t);
66 if (message_type != NULL)
67 memcpy(message_type, walk, sizeof(uint32_t));
69 *reply = smalloc(*reply_length);
72 while (read_bytes < *reply_length) {
73 const int n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes);
75 if (errno == EINTR || errno == EAGAIN)
80 ELOG("IPC: unexpected EOF while reading payload, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n",
81 read_bytes, *reply_length);