Subversion Repositories MLServ

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/* $Id: ipc.c 4 2024-09-24 18:35:15Z nishi $ */

#include "cm_ipc.h"

#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <stdio.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;
        }
        return sock;
}