ipc_send_message: use stack frame with fixed size
This commit is contained in:
parent
fc0e80ee7f
commit
7b0d75ee0a
|
@ -2,7 +2,7 @@
|
||||||
* vim:ts=4:sw=4:expandtab
|
* vim:ts=4:sw=4:expandtab
|
||||||
*
|
*
|
||||||
* i3 - an improved dynamic tiling window manager
|
* i3 - an improved dynamic tiling window manager
|
||||||
* © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
|
* © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
|
||||||
*
|
*
|
||||||
* This public header defines the different constants and message types to use
|
* This public header defines the different constants and message types to use
|
||||||
* for the IPC interface to i3 (see docs/ipc for more information).
|
* for the IPC interface to i3 (see docs/ipc for more information).
|
||||||
|
@ -11,6 +11,13 @@
|
||||||
#ifndef I3_I3_IPC_H
|
#ifndef I3_I3_IPC_H
|
||||||
#define I3_I3_IPC_H
|
#define I3_I3_IPC_H
|
||||||
|
|
||||||
|
typedef struct i3_ipc_header {
|
||||||
|
/* 6 = strlen(I3_IPC_MAGIC) */
|
||||||
|
char magic[6];
|
||||||
|
uint32_t size;
|
||||||
|
uint32_t type;
|
||||||
|
} __attribute__ ((packed)) i3_ipc_header_t;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Messages from clients to i3
|
* Messages from clients to i3
|
||||||
*
|
*
|
||||||
|
|
|
@ -202,8 +202,8 @@ int ipc_connect(const char *socket_path);
|
||||||
* Returns 0 on success.
|
* Returns 0 on success.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int ipc_send_message(int sockfd, uint32_t message_size,
|
int ipc_send_message(int sockfd, const uint32_t message_size,
|
||||||
uint32_t message_type, const uint8_t *payload);
|
const uint32_t message_type, const uint8_t *payload);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a message from the given socket file descriptor and stores its length
|
* Reads a message from the given socket file descriptor and stores its length
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* vim:ts=4:sw=4:expandtab
|
* vim:ts=4:sw=4:expandtab
|
||||||
*
|
*
|
||||||
* i3 - an improved dynamic tiling window manager
|
* i3 - an improved dynamic tiling window manager
|
||||||
* © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
|
* © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -24,24 +24,35 @@
|
||||||
* Returns 0 on success.
|
* Returns 0 on success.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int ipc_send_message(int sockfd, uint32_t message_size,
|
int ipc_send_message(int sockfd, const uint32_t message_size,
|
||||||
uint32_t message_type, const uint8_t *payload) {
|
const uint32_t message_type, const uint8_t *payload) {
|
||||||
int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
|
const i3_ipc_header_t header = {
|
||||||
char msg[buffer_size];
|
/* We don’t use I3_IPC_MAGIC because it’s a 0-terminated C string. */
|
||||||
char *walk = msg;
|
.magic = { 'i', '3', '-', 'i', 'p', 'c' },
|
||||||
|
.size = message_size,
|
||||||
strncpy(walk, I3_IPC_MAGIC, buffer_size - 1);
|
.type = message_type
|
||||||
walk += strlen(I3_IPC_MAGIC);
|
};
|
||||||
memcpy(walk, &message_size, sizeof(uint32_t));
|
|
||||||
walk += sizeof(uint32_t);
|
|
||||||
memcpy(walk, &message_type, sizeof(uint32_t));
|
|
||||||
walk += sizeof(uint32_t);
|
|
||||||
memcpy(walk, payload, message_size);
|
|
||||||
|
|
||||||
int sent_bytes = 0;
|
int sent_bytes = 0;
|
||||||
while (sent_bytes < buffer_size) {
|
int n = 0;
|
||||||
int n = write(sockfd, msg + sent_bytes, buffer_size - sent_bytes);
|
|
||||||
if (n == -1) {
|
/* This first loop is basically unnecessary. No operating system has
|
||||||
|
* buffers which cannot fit 14 bytes into them, so the write() will only be
|
||||||
|
* called once. */
|
||||||
|
while (sent_bytes < sizeof(i3_ipc_header_t)) {
|
||||||
|
if ((n = write(sockfd, ((void*)&header) + sent_bytes, sizeof(i3_ipc_header_t) - sent_bytes)) == -1) {
|
||||||
|
if (errno == EAGAIN)
|
||||||
|
continue;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sent_bytes += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
sent_bytes = 0;
|
||||||
|
|
||||||
|
while (sent_bytes < message_size) {
|
||||||
|
if ((n = write(sockfd, payload + sent_bytes, message_size - sent_bytes)) == -1) {
|
||||||
if (errno == EAGAIN)
|
if (errno == EAGAIN)
|
||||||
continue;
|
continue;
|
||||||
return -1;
|
return -1;
|
||||||
|
|
Loading…
Reference in New Issue