Rev 4 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
/* $Id: ipc.c 5 2024-09-25 00:19:35Z nishi $ */
#include "cm_ipc.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
int cm_ipc_connect(void) {
struct sockaddr_un sun;
memset(&sun, 0, sizeof(sun));
int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
if(sock == -1) {
return -1;
}
sun.sun_family = AF_LOCAL;
strcpy(sun.sun_path, SOCK_PATH);
int ret = connect(sock, (const struct sockaddr*)&sun, sizeof(sun));
if(ret == -1) {
close(sock);
return -1;
}
return sock;
}
int cm_ipc_create(void) {
struct sockaddr_un sun;
memset(&sun, 0, sizeof(sun));
int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
if(sock == -1) {
return -1;
}
remove(SOCK_PATH);
sun.sun_family = AF_LOCAL;
strcpy(sun.sun_path, SOCK_PATH);
if(bind(sock, (const struct sockaddr*)&sun, sizeof(sun)) == -1) {
close(sock);
return -1;
}
if(listen(sock, 128) == -1) {
close(sock);
return -1;
}
chmod(sun.sun_path, 0660);
return sock;
}