mirror of
https://github.com/pygos/init.git
synced 2024-11-22 11:19:45 +01:00
cleanup: init socket wire format
Replace array adhockery with structs and make use of the handy endianness conversion macros. Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
parent
c3d14cbfa8
commit
be06641904
3 changed files with 30 additions and 30 deletions
|
@ -20,12 +20,13 @@ static int send_string(int fd, const void *dst, size_t addrlen,
|
||||||
const char *str)
|
const char *str)
|
||||||
{
|
{
|
||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
uint8_t raw_len[2];
|
uint16_t raw;
|
||||||
|
|
||||||
raw_len[0] = (len >> 8) & 0xFF;
|
if (len > 0xFFFF)
|
||||||
raw_len[1] = len & 0xFF;
|
return -1;
|
||||||
|
|
||||||
if (send_retry(fd, dst, addrlen, raw_len, 2))
|
raw = htobe16(len);
|
||||||
|
if (send_retry(fd, dst, addrlen, &raw, 2))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0;
|
return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0;
|
||||||
|
@ -61,24 +62,20 @@ int init_socket_create(void)
|
||||||
int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
|
int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
|
||||||
E_SERVICE_STATE state, service_t *svc)
|
E_SERVICE_STATE state, service_t *svc)
|
||||||
{
|
{
|
||||||
uint8_t info[8];
|
init_response_status_t info;
|
||||||
|
|
||||||
|
memset(&info, 0, sizeof(info));
|
||||||
|
|
||||||
if (svc == NULL || state == ESS_NONE) {
|
if (svc == NULL || state == ESS_NONE) {
|
||||||
info[0] = ESS_NONE;
|
info.state = ESS_NONE;
|
||||||
info[1] = 0;
|
info.id = -1;
|
||||||
info[2] = info[3] = 0;
|
|
||||||
info[4] = info[5] = info[6] = info[7] = 0xFF;
|
|
||||||
} else {
|
} else {
|
||||||
info[0] = state;
|
info.state = state;
|
||||||
info[1] = svc->status & 0xFF;
|
info.exit_status = svc->status & 0xFF;
|
||||||
info[2] = info[3] = 0;
|
info.id = htobe32(svc->id);
|
||||||
info[4] = (svc->id >> 24) & 0xFF;
|
|
||||||
info[5] = (svc->id >> 16) & 0xFF;
|
|
||||||
info[6] = (svc->id >> 8) & 0xFF;
|
|
||||||
info[7] = svc->id & 0xFF;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (send_retry(fd, dest_addr, addrlen, info, sizeof(info)))
|
if (send_retry(fd, dest_addr, addrlen, &info, sizeof(info)))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (svc != NULL && state != ESS_NONE) {
|
if (svc != NULL && state != ESS_NONE) {
|
||||||
|
|
|
@ -39,6 +39,13 @@ typedef struct {
|
||||||
} arg;
|
} arg;
|
||||||
} init_request_t;
|
} init_request_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t state;
|
||||||
|
uint8_t exit_status;
|
||||||
|
uint8_t padd[2];
|
||||||
|
int32_t id;
|
||||||
|
} init_response_status_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
E_SERVICE_STATE state;
|
E_SERVICE_STATE state;
|
||||||
int exit_status;
|
int exit_status;
|
||||||
|
|
|
@ -31,18 +31,17 @@ retry:
|
||||||
|
|
||||||
static char *read_string(int fd)
|
static char *read_string(int fd)
|
||||||
{
|
{
|
||||||
uint8_t size_raw[2];
|
uint16_t len;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
size_t len;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = read_retry(fd, size_raw, 2);
|
ret = read_retry(fd, &len, sizeof(len));
|
||||||
if (ret <= 0)
|
if (ret <= 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
len = (((size_t)size_raw[0]) << 8) | (size_t)size_raw[1];
|
len = be16toh(len);
|
||||||
|
|
||||||
buffer = malloc(len + 1);
|
buffer = calloc(1, len + 1);
|
||||||
if (buffer == NULL)
|
if (buffer == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -57,24 +56,21 @@ static char *read_string(int fd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer[len] = '\0';
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_socket_recv_status(int fd, init_status_t *resp)
|
int init_socket_recv_status(int fd, init_status_t *resp)
|
||||||
{
|
{
|
||||||
uint8_t info[8];
|
init_response_status_t info;
|
||||||
|
|
||||||
memset(resp, 0, sizeof(*resp));
|
memset(resp, 0, sizeof(*resp));
|
||||||
|
|
||||||
if (read_retry(fd, info, sizeof(info)) <= 0)
|
if (read_retry(fd, &info, sizeof(info)) <= 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
resp->state = info[0];
|
resp->state = info.state;
|
||||||
resp->exit_status = info[1];
|
resp->exit_status = info.exit_status;
|
||||||
|
resp->id = be32toh(info.id);
|
||||||
resp->id = ((int)info[4] << 24) | ((int)info[5] << 16) |
|
|
||||||
((int)info[6] << 8) | (int)info[7];
|
|
||||||
|
|
||||||
if (resp->state == ESS_NONE)
|
if (resp->state == ESS_NONE)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue