Subversion Repositories MLServ

Rev

Rev 4 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 1
/* $Id: ipc.c 5 2024-09-25 00:19:35Z nishi $ */
2
 
3
#include "cm_ipc.h"
4
 
5
#include <sys/socket.h>
6
#include <sys/un.h>
7
#include <string.h>
8
#include <unistd.h>
9
#include <stdio.h>
5 nishi 10
#include <sys/stat.h>
4 nishi 11
 
12
int cm_ipc_connect(void) {
13
	struct sockaddr_un sun;
14
	memset(&sun, 0, sizeof(sun));
15
	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
16
	if(sock == -1) {
17
		return -1;
18
	}
19
	sun.sun_family = AF_LOCAL;
20
	strcpy(sun.sun_path, SOCK_PATH);
21
	int ret = connect(sock, (const struct sockaddr*)&sun, sizeof(sun));
22
	if(ret == -1) {
23
		close(sock);
24
		return -1;
25
	}
26
	return sock;
27
}
28
 
29
int cm_ipc_create(void) {
30
	struct sockaddr_un sun;
31
	memset(&sun, 0, sizeof(sun));
32
	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
33
	if(sock == -1) {
34
		return -1;
35
	}
36
	remove(SOCK_PATH);
37
	sun.sun_family = AF_LOCAL;
38
	strcpy(sun.sun_path, SOCK_PATH);
39
	if(bind(sock, (const struct sockaddr*)&sun, sizeof(sun)) == -1) {
40
		close(sock);
41
		return -1;
42
	}
43
	if(listen(sock, 128) == -1) {
44
		close(sock);
45
		return -1;
46
	}
5 nishi 47
	chmod(sun.sun_path, 0660);
4 nishi 48
	return sock;
49
}