]> git.sur5r.net Git - i3/i3/blob - libi3/ipc_recv_message.c
Merge branch 'master' into next
[i3/i3] / libi3 / ipc_recv_message.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <unistd.h>
13 #include <errno.h>
14
15 #include <i3/ipc.h>
16
17 #include "libi3.h"
18
19 /*
20  * Reads a message from the given socket file descriptor and stores its length
21  * (reply_length) as well as a pointer to its contents (reply).
22  *
23  * Returns -1 when read() fails, errno will remain.
24  * Returns -2 on EOF.
25  * Returns -3 when the IPC protocol is violated (invalid magic, unexpected
26  * message type, EOF instead of a message). Additionally, the error will be
27  * printed to stderr.
28  * Returns 0 on success.
29  *
30  */
31 int ipc_recv_message(int sockfd, uint32_t *message_type,
32                      uint32_t *reply_length, uint8_t **reply) {
33     /* Read the message header first */
34     const uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
35     char msg[to_read];
36     char *walk = msg;
37
38     uint32_t read_bytes = 0;
39     while (read_bytes < to_read) {
40         int n = read(sockfd, msg + read_bytes, to_read - read_bytes);
41         if (n == -1)
42             return -1;
43         if (n == 0) {
44             ELOG("IPC: received EOF instead of reply\n");
45             return -2;
46         }
47
48         read_bytes += n;
49     }
50
51     if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
52         ELOG("IPC: invalid magic in reply\n");
53         return -3;
54     }
55
56     walk += strlen(I3_IPC_MAGIC);
57     *reply_length = *((uint32_t*)walk);
58     walk += sizeof(uint32_t);
59     if (message_type != NULL)
60         *message_type = *((uint32_t*)walk);
61
62     *reply = smalloc(*reply_length);
63
64     read_bytes = 0;
65     int n;
66     while (read_bytes < *reply_length) {
67         if ((n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes)) == -1) {
68             if (errno == EINTR || errno == EAGAIN)
69                 continue;
70             return -1;
71         }
72
73         read_bytes += n;
74     }
75
76     return 0;
77 }