about summary refs log tree commit diff
path: root/sami.c
blob: 64adf0a6dc1c744dba83e9f1ef7f01e5ef27f7c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "sami.h"

#include <errno.h>
#include <signal.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/socket.h>
#include <sys/wait.h>


int SAMI_make(SAMI *actor, SAMI_Handler *handler, void *arg)
{
	int pair[2];

	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) == -1)
		return 1;
	actor->fd = pair[0];

	if (!(actor->pid = fork()))
	{
		SAMI__fd = pair[1];
		handler(arg);
		exit(0);
	}

	return actor->pid == -1;
}

int SAMI_send(SAMI *actor, void *buf, size_t length)
{
	return send(actor->fd, buf, length, 0) == -1;
}

int SAMI_recv(void *buf, size_t length)
{
	return recvfrom(SAMI__fd, buf, length, 0, 0, 0) == -1;
}

int SAMI_kill(SAMI *actor)
{
	int res;

	if (kill(actor->pid, SIGKILL))
		return 1;

	for (;;)
		if (waitpid(actor->pid, 0, 0) == -1)
			if (errno == EINTR)
				continue;
			else
				return 1;
		else
			return 0;
}