2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2011 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
22 * Reads a message from the given socket file descriptor and stores its length
23 * (reply_length) as well as a pointer to its contents (reply).
25 * Returns -1 when read() fails, errno will remain.
26 * Returns -2 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 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);
45 fprintf(stderr, "IPC: received EOF instead of reply\n");
53 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
54 fprintf(stderr, "IPC: invalid magic in reply\n");
58 walk += strlen(I3_IPC_MAGIC);
59 *reply_length = *((uint32_t*)walk);
60 walk += sizeof(uint32_t);
61 if (*((uint32_t*)walk) != message_type) {
62 fprintf(stderr, "IPC: unexpected reply type (got %d, expected %d)\n", *((uint32_t*)walk), message_type);
65 walk += sizeof(uint32_t);
67 *reply = smalloc(*reply_length);
69 to_read = *reply_length;
71 while (read_bytes < to_read) {
72 int n = read(sockfd, *reply + read_bytes, to_read);