]> git.sur5r.net Git - i3/i3/blob - libi3/ipc_recv_message.c
0ef4fced4f27b1fd1bf415727efb6f1bbdd14796
[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 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             return -2;
45         }
46
47         read_bytes += n;
48     }
49
50     if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
51         ELOG("IPC: invalid magic in reply\n");
52         return -3;
53     }
54
55     walk += strlen(I3_IPC_MAGIC);
56     memcpy(reply_length, walk, sizeof(uint32_t));
57     walk += sizeof(uint32_t);
58     if (message_type != NULL)
59         memcpy(message_type, walk, sizeof(uint32_t));
60
61     *reply = smalloc(*reply_length);
62
63     read_bytes = 0;
64     int n;
65     while (read_bytes < *reply_length) {
66         if ((n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes)) == -1) {
67             if (errno == EINTR || errno == EAGAIN)
68                 continue;
69             return -1;
70         }
71
72         read_bytes += n;
73     }
74
75     return 0;
76 }