2011-10-02 19:33:10 +02:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-10-02 19:33:10 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
#include "libi3.h"
|
|
|
|
|
2013-01-23 18:50:21 +01:00
|
|
|
#include <errno.h>
|
2011-10-02 19:33:10 +02:00
|
|
|
#include <i3/ipc.h>
|
2020-04-19 09:43:48 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-10-02 19:33:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads a message from the given socket file descriptor and stores its length
|
|
|
|
* (reply_length) as well as a pointer to its contents (reply).
|
|
|
|
*
|
|
|
|
* Returns -1 when read() fails, errno will remain.
|
2013-01-23 18:50:21 +01:00
|
|
|
* Returns -2 on EOF.
|
|
|
|
* Returns -3 when the IPC protocol is violated (invalid magic, unexpected
|
2011-10-02 19:33:10 +02:00
|
|
|
* message type, EOF instead of a message). Additionally, the error will be
|
|
|
|
* printed to stderr.
|
|
|
|
* Returns 0 on success.
|
|
|
|
*
|
|
|
|
*/
|
2013-01-23 18:50:21 +01:00
|
|
|
int ipc_recv_message(int sockfd, uint32_t *message_type,
|
2011-10-02 19:33:10 +02:00
|
|
|
uint32_t *reply_length, uint8_t **reply) {
|
|
|
|
/* Read the message header first */
|
2013-01-23 18:50:21 +01:00
|
|
|
const uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
|
2011-10-02 19:33:10 +02:00
|
|
|
char msg[to_read];
|
|
|
|
char *walk = msg;
|
|
|
|
|
|
|
|
uint32_t read_bytes = 0;
|
|
|
|
while (read_bytes < to_read) {
|
2013-01-23 18:50:21 +01:00
|
|
|
int n = read(sockfd, msg + read_bytes, to_read - read_bytes);
|
2011-10-02 19:33:10 +02:00
|
|
|
if (n == -1)
|
|
|
|
return -1;
|
|
|
|
if (n == 0) {
|
2017-11-09 20:18:23 +01:00
|
|
|
if (read_bytes == 0) {
|
|
|
|
return -2;
|
|
|
|
} else {
|
|
|
|
ELOG("IPC: unexpected EOF while reading header, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n",
|
|
|
|
read_bytes, to_read);
|
|
|
|
return -3;
|
|
|
|
}
|
2011-10-02 19:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
read_bytes += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
|
2017-11-09 20:18:23 +01:00
|
|
|
ELOG("IPC: invalid magic in header, got \"%.*s\", want \"%s\"\n",
|
|
|
|
(int)strlen(I3_IPC_MAGIC), walk, I3_IPC_MAGIC);
|
2013-01-23 18:50:21 +01:00
|
|
|
return -3;
|
2011-10-02 19:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
walk += strlen(I3_IPC_MAGIC);
|
2013-06-29 19:28:13 +02:00
|
|
|
memcpy(reply_length, walk, sizeof(uint32_t));
|
2011-10-02 19:33:10 +02:00
|
|
|
walk += sizeof(uint32_t);
|
2013-01-23 18:50:21 +01:00
|
|
|
if (message_type != NULL)
|
2013-06-29 19:28:13 +02:00
|
|
|
memcpy(message_type, walk, sizeof(uint32_t));
|
2011-10-02 19:33:10 +02:00
|
|
|
|
|
|
|
*reply = smalloc(*reply_length);
|
|
|
|
|
|
|
|
read_bytes = 0;
|
2013-01-23 18:50:21 +01:00
|
|
|
while (read_bytes < *reply_length) {
|
2017-11-09 20:18:23 +01:00
|
|
|
const int n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes);
|
|
|
|
if (n == -1) {
|
2013-01-23 18:50:21 +01:00
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
|
|
|
continue;
|
2011-10-02 19:33:10 +02:00
|
|
|
return -1;
|
2013-01-23 18:50:21 +01:00
|
|
|
}
|
2017-11-09 20:18:23 +01:00
|
|
|
if (n == 0) {
|
|
|
|
ELOG("IPC: unexpected EOF while reading payload, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n",
|
|
|
|
read_bytes, *reply_length);
|
|
|
|
return -3;
|
|
|
|
}
|
2011-10-02 19:33:10 +02:00
|
|
|
|
|
|
|
read_bytes += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|