Subversion Repositories MLServ

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 1
/* $Id: ipc.c 4 2024-09-24 18:35:15Z 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>
10
 
11
int cm_ipc_connect(void) {
12
	struct sockaddr_un sun;
13
	memset(&sun, 0, sizeof(sun));
14
	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
15
	if(sock == -1) {
16
		return -1;
17
	}
18
	sun.sun_family = AF_LOCAL;
19
	strcpy(sun.sun_path, SOCK_PATH);
20
	int ret = connect(sock, (const struct sockaddr*)&sun, sizeof(sun));
21
	if(ret == -1) {
22
		close(sock);
23
		return -1;
24
	}
25
	return sock;
26
}
27
 
28
int cm_ipc_create(void) {
29
	struct sockaddr_un sun;
30
	memset(&sun, 0, sizeof(sun));
31
	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
32
	if(sock == -1) {
33
		return -1;
34
	}
35
	remove(SOCK_PATH);
36
	sun.sun_family = AF_LOCAL;
37
	strcpy(sun.sun_path, SOCK_PATH);
38
	if(bind(sock, (const struct sockaddr*)&sun, sizeof(sun)) == -1) {
39
		close(sock);
40
		return -1;
41
	}
42
	if(listen(sock, 128) == -1) {
43
		close(sock);
44
		return -1;
45
	}
46
	return sock;
47
}