mirror of
https://github.com/pygos/init.git
synced 2024-11-22 11:19:45 +01:00
Add functions to transmit service status accross initd socket
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
parent
08f72865b2
commit
23b713c3b5
4 changed files with 175 additions and 0 deletions
|
@ -3,6 +3,8 @@ libinit_a_SOURCES += lib/init/svcscan.c lib/init/del_svc_list.c
|
||||||
libinit_a_SOURCES += lib/init/svc_tsort.c lib/include/service.h
|
libinit_a_SOURCES += lib/init/svc_tsort.c lib/include/service.h
|
||||||
libinit_a_SOURCES += lib/init/init_socket_open.c lib/init/init_socket_create.c
|
libinit_a_SOURCES += lib/init/init_socket_open.c lib/init/init_socket_create.c
|
||||||
libinit_a_SOURCES += lib/include/initsock.h lib/init/init_socket_send_request.c
|
libinit_a_SOURCES += lib/include/initsock.h lib/init/init_socket_send_request.c
|
||||||
|
libinit_a_SOURCES += lib/init/init_socket_send_status.c
|
||||||
|
libinit_a_SOURCES += lib/init/init_socket_recv_status.c
|
||||||
libinit_a_CPPFLAGS = $(AM_CPPFLAGS)
|
libinit_a_CPPFLAGS = $(AM_CPPFLAGS)
|
||||||
libinit_a_CFLAGS = $(AM_CFLAGS)
|
libinit_a_CFLAGS = $(AM_CFLAGS)
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#define INITSOCK_H
|
#define INITSOCK_H
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "service.h"
|
||||||
|
|
||||||
#define INIT_SOCK_PATH SOCKDIR "/init.sock"
|
#define INIT_SOCK_PATH SOCKDIR "/init.sock"
|
||||||
|
|
||||||
|
@ -10,14 +11,33 @@ typedef enum {
|
||||||
EIR_STATUS = 0x00,
|
EIR_STATUS = 0x00,
|
||||||
} E_INIT_REQUEST;
|
} E_INIT_REQUEST;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ESS_NONE = 0x00,
|
||||||
|
ESS_RUNNING = 0x01,
|
||||||
|
ESS_ENQUEUED = 0x02,
|
||||||
|
ESS_EXITED = 0x03,
|
||||||
|
} E_SERVICE_STATE;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
E_INIT_REQUEST rq;
|
E_INIT_REQUEST rq;
|
||||||
} init_request_t;
|
} init_request_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
E_SERVICE_STATE state;
|
||||||
|
int exit_status;
|
||||||
|
char *filename;
|
||||||
|
char *service_name;
|
||||||
|
} init_status_response_t;
|
||||||
|
|
||||||
int init_socket_create(void);
|
int init_socket_create(void);
|
||||||
|
|
||||||
int init_socket_open(const char *tmppath);
|
int init_socket_open(const char *tmppath);
|
||||||
|
|
||||||
int init_socket_send_request(int fd, E_INIT_REQUEST rq);
|
int init_socket_send_request(int fd, E_INIT_REQUEST rq);
|
||||||
|
|
||||||
|
int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
|
||||||
|
E_SERVICE_STATE state, service_t *svc);
|
||||||
|
|
||||||
|
int init_socket_recv_status(int fd, init_status_response_t *resp);
|
||||||
|
|
||||||
#endif /* INITSOCK_H */
|
#endif /* INITSOCK_H */
|
||||||
|
|
88
lib/init/init_socket_recv_status.c
Normal file
88
lib/init/init_socket_recv_status.c
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
/* SPDX-License-Identifier: ISC */
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "initsock.h"
|
||||||
|
|
||||||
|
static int read_retry(int fd, void *buffer, size_t size)
|
||||||
|
{
|
||||||
|
ssize_t ret;
|
||||||
|
retry:
|
||||||
|
ret = read(fd, buffer, size);
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
goto retry;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((size_t)ret < size) {
|
||||||
|
errno = EPROTO;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *read_string(int fd)
|
||||||
|
{
|
||||||
|
uint8_t size_raw[2];
|
||||||
|
char *buffer;
|
||||||
|
size_t len;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = read_retry(fd, size_raw, 2);
|
||||||
|
if (ret <= 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
len = (((size_t)size_raw[0]) << 8) | (size_t)size_raw[1];
|
||||||
|
|
||||||
|
buffer = malloc(len + 1);
|
||||||
|
if (buffer == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
ret = read_retry(fd, buffer, len);
|
||||||
|
|
||||||
|
if (ret <= 0) {
|
||||||
|
ret = errno;
|
||||||
|
free(buffer);
|
||||||
|
errno = ret;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[len] = '\0';
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int init_socket_recv_status(int fd, init_status_response_t *resp)
|
||||||
|
{
|
||||||
|
uint8_t info[2];
|
||||||
|
|
||||||
|
memset(resp, 0, sizeof(*resp));
|
||||||
|
|
||||||
|
if (read_retry(fd, info, 2) <= 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
resp->state = info[0];
|
||||||
|
resp->exit_status = info[1];
|
||||||
|
|
||||||
|
if (resp->state == ESS_NONE)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
resp->filename = read_string(fd);
|
||||||
|
if (resp->filename == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
resp->service_name = read_string(fd);
|
||||||
|
if (resp->service_name == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
65
lib/init/init_socket_send_status.c
Normal file
65
lib/init/init_socket_send_status.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/* SPDX-License-Identifier: ISC */
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "initsock.h"
|
||||||
|
|
||||||
|
|
||||||
|
static int send_retry(int fd, const void *dst, size_t addrlen,
|
||||||
|
const void *buffer, size_t size)
|
||||||
|
{
|
||||||
|
ssize_t ret;
|
||||||
|
retry:
|
||||||
|
ret = sendto(fd, buffer, size, MSG_NOSIGNAL, dst, addrlen);
|
||||||
|
if (ret < 0 && errno == EINTR)
|
||||||
|
goto retry;
|
||||||
|
|
||||||
|
if (ret < 0 || (size_t)ret < size)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int send_string(int fd, const void *dst, size_t addrlen,
|
||||||
|
const char *str)
|
||||||
|
{
|
||||||
|
size_t len = strlen(str);
|
||||||
|
uint8_t raw_len[2];
|
||||||
|
|
||||||
|
raw_len[0] = (len >> 8) & 0xFF;
|
||||||
|
raw_len[1] = len & 0xFF;
|
||||||
|
|
||||||
|
if (send_retry(fd, dst, addrlen, raw_len, 2))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
|
||||||
|
E_SERVICE_STATE state, service_t *svc)
|
||||||
|
{
|
||||||
|
uint8_t info[2];
|
||||||
|
|
||||||
|
if (svc == NULL || state == ESS_NONE) {
|
||||||
|
info[0] = ESS_NONE;
|
||||||
|
info[1] = 0;
|
||||||
|
} else {
|
||||||
|
info[0] = state;
|
||||||
|
info[1] = svc->status & 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (send_retry(fd, dest_addr, addrlen, info, 2))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (svc != NULL && state != ESS_NONE) {
|
||||||
|
if (send_string(fd, dest_addr, addrlen, svc->fname))
|
||||||
|
return -1;
|
||||||
|
if (send_string(fd, dest_addr, addrlen, svc->name))
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue