1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-25 04:20:42 +01:00

Add helpers for initd socket

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-03-15 11:23:40 +01:00
parent c544fcc7a3
commit c78bbd2f73
5 changed files with 133 additions and 0 deletions

View file

@ -1,6 +1,8 @@
libinit_a_SOURCES = lib/init/delsvc.c lib/init/svcmap.c lib/init/rdsvc.c
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/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_CPPFLAGS = $(AM_CPPFLAGS)
libinit_a_CFLAGS = $(AM_CFLAGS)

23
lib/include/initsock.h Normal file
View file

@ -0,0 +1,23 @@
/* SPDX-License-Identifier: ISC */
#ifndef INITSOCK_H
#define INITSOCK_H
#include "config.h"
#define INIT_SOCK_PATH SOCKDIR "/init.sock"
typedef enum {
EIR_STATUS = 0x00,
} E_INIT_REQUEST;
typedef struct {
E_INIT_REQUEST rq;
} init_request_t;
int init_socket_create(void);
int init_socket_open(const char *tmppath);
int init_socket_send_request(int fd, E_INIT_REQUEST rq);
#endif /* INITSOCK_H */

View file

@ -0,0 +1,34 @@
/* SPDX-License-Identifier: ISC */
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
#include "initsock.h"
int init_socket_create(void)
{
struct sockaddr_un un;
int fd;
fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (fd < 0) {
perror("socket");
return -1;
}
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
strcpy(un.sun_path, INIT_SOCK_PATH);
unlink(INIT_SOCK_PATH);
if (bind(fd, (struct sockaddr *)&un, sizeof(un))) {
perror("bind: " INIT_SOCK_PATH);
close(fd);
unlink(INIT_SOCK_PATH);
return -1;
}
return fd;
}

View file

@ -0,0 +1,46 @@
/* SPDX-License-Identifier: ISC */
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "initsock.h"
int init_socket_open(const char *tmppath)
{
struct sockaddr_un un;
int fd;
fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (fd < 0) {
perror("socket");
return -1;
}
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
strcpy(un.sun_path, tmppath);
if (bind(fd, (struct sockaddr *)&un, sizeof(un))) {
fprintf(stderr, "bind: %s: %s", tmppath, strerror(errno));
close(fd);
unlink(tmppath);
return -1;
}
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
strcpy(un.sun_path, INIT_SOCK_PATH);
if (connect(fd, (struct sockaddr *)&un, sizeof(un))) {
perror("connect: " INIT_SOCK_PATH);
close(fd);
return -1;
}
return fd;
}

View file

@ -0,0 +1,28 @@
/* SPDX-License-Identifier: ISC */
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "initsock.h"
int init_socket_send_request(int fd, E_INIT_REQUEST rq)
{
init_request_t request;
ssize_t ret;
memset(&request, 0, sizeof(request));
request.rq = rq;
retry:
ret = write(fd, &request, sizeof(request));
if (ret < 0) {
if (errno == EINTR)
goto retry;
perror(INIT_SOCK_PATH);
return -1;
}
return 0;
}