2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
19 * Reads a message from the given socket file descriptor and stores its length
20 * (reply_length) as well as a pointer to its contents (reply).
22 * Returns -1 when read() fails, errno will remain.
23 * Returns -2 when the IPC protocol is violated (invalid magic, unexpected
24 * message type, EOF instead of a message). Additionally, the error will be
26 * Returns 0 on success.
29 int ipc_recv_message(int sockfd, uint32_t message_type,
30 uint32_t *reply_length, uint8_t **reply) {
31 /* Read the message header first */
32 uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
36 uint32_t read_bytes = 0;
37 while (read_bytes < to_read) {
38 int n = read(sockfd, msg + read_bytes, to_read);
42 fprintf(stderr, "IPC: received EOF instead of reply\n");
50 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
51 fprintf(stderr, "IPC: invalid magic in reply\n");
55 walk += strlen(I3_IPC_MAGIC);
56 *reply_length = *((uint32_t*)walk);
57 walk += sizeof(uint32_t);
58 if (*((uint32_t*)walk) != message_type) {
59 fprintf(stderr, "IPC: unexpected reply type (got %d, expected %d)\n", *((uint32_t*)walk), message_type);
63 *reply = smalloc(*reply_length);
65 to_read = *reply_length;
67 while (read_bytes < to_read) {
68 int n = read(sockfd, *reply + read_bytes, to_read);