]> git.sur5r.net Git - i3/i3/blob - libi3/ipc_recv_message.c
Merge branch 'release-4.16.1'
[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 "libi3.h"
9
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <inttypes.h>
17
18 #include <i3/ipc.h>
19
20 /*
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).
23  *
24  * Returns -1 when read() fails, errno will remain.
25  * Returns -2 on EOF.
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
28  * printed to stderr.
29  * Returns 0 on success.
30  *
31  */
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);
36     char msg[to_read];
37     char *walk = msg;
38
39     uint32_t read_bytes = 0;
40     while (read_bytes < to_read) {
41         int n = read(sockfd, msg + read_bytes, to_read - read_bytes);
42         if (n == -1)
43             return -1;
44         if (n == 0) {
45             if (read_bytes == 0) {
46                 return -2;
47             } else {
48                 ELOG("IPC: unexpected EOF while reading header, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n",
49                      read_bytes, to_read);
50                 return -3;
51             }
52         }
53
54         read_bytes += n;
55     }
56
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);
60         return -3;
61     }
62
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));
68
69     *reply = smalloc(*reply_length);
70
71     read_bytes = 0;
72     while (read_bytes < *reply_length) {
73         const int n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes);
74         if (n == -1) {
75             if (errno == EINTR || errno == EAGAIN)
76                 continue;
77             return -1;
78         }
79         if (n == 0) {
80             ELOG("IPC: unexpected EOF while reading payload, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n",
81                  read_bytes, *reply_length);
82             return -3;
83         }
84
85         read_bytes += n;
86     }
87
88     return 0;
89 }