1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-05-20 04:36:12 +02:00
init/lib/init/init_socket_create.c
David Oberhollenzer c78bbd2f73 Add helpers for initd socket
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 14:15:58 +01:00

35 lines
590 B
C

/* 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;
}