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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#include "Ecore.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
void ecore_ipc_init(char *path);
void ecore_ipc_cleanup(void);
static void ecore_ipc_connect_handler(int fd);
static void ecore_ipc_client_handler(int fd);
void ecore_ipc_get_data(int fd, void *buf);
void ecore_ipc_send_data(int fd, void *buf, int size);
void ecore_add_ipc_service(int service, void (*func) (int fd));
void ecore_del_ipc_service(int service);
Ecore_Event_Ipc_Service *ipc_services = NULL;
void
ecore_ev_ipc_init(char *path)
{
int fd, len;
struct sockaddr_un saun;
if (path == NULL)
return;
/* a UNIX domain, stream socket */
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
printf("Cannot create ipc socket... disabling ipc.\n");
return;
}
/* create the address we will be binding to */
saun.sun_family = AF_UNIX;
strcpy(saun.sun_path, path);
/* unlink the address so the bind won't fail */
unlink(path);
len = sizeof(saun.sun_family) + strlen(saun.sun_path);
if (bind(fd, (struct sockaddr *)&saun, len) < 0)
{
printf("Cannot bind ipc socket... disabling ipc.\n");
return;
}
/* listen on the socket */
if (listen(fd, 5) < 0)
{
printf("Cannot listen on ipc socket... disabling ipc.\n");
return;
}
/* add ipc listener */
ecore_add_event_ipc(fd, ecore_ipc_connect_handler);
}
void
ecore_ev_ipc_cleanup(void)
{
Ecore_Event_Ipc_Service *ipc_s;
/* cleanup services list */
for (ipc_s = ipc_services; ipc_s; ipc_s = ipc_s->next)
{
ecore_del_ipc_service(ipc_s->service);
}
}
static void
ecore_ipc_connect_handler(int fd)
{
struct sockaddr_un fsaun;
int fromlen, nfd;
/* accept ipc connection */
fromlen = sizeof(fsaun);
if ((nfd = accept(fd, (struct sockaddr *)&fsaun, &fromlen)) < 0)
{
printf("Cannot accept ipc connection... ignoring connection attempt.\n");
return;
}
/* add ipc client */
ecore_add_event_ipc(nfd, ecore_ipc_client_handler);
}
static void
ecore_ipc_client_handler(int fd)
{
int nread, service;
Ecore_Event_Ipc_Service *ipc_s;
if ((nread = read(fd, &service, sizeof(service))) == 0)
{
close(fd);
ecore_del_event_ipc(fd);
}
else if (nread > 0)
{
/* call the service function */
for (ipc_s = ipc_services; ipc_s; ipc_s = ipc_s->next)
{
if (ipc_s->service == service)
{
ipc_s->func(fd);
break;
}
}
}
else
{
printf("ipc error in read service.\n");
fflush(stdout);
}
}
void
ecore_ipc_get_data(int fd, void *buf)
{
int readn, nread;
/* read number of bytes being sent */
if ((nread = read(fd, &readn, sizeof(readn))) == -1)
{
printf("ipc error in get data.\n");
fflush(stdout);
return;
}
/* get data structure */
if ((nread = read(fd, buf, readn)) == -1)
{
printf("ipc error in get data.\n");
fflush(stdout);
return;
}
}
void
ecore_ipc_send_data(int fd, void *buf, int size)
{
int n;
/* send length of data being sent */
if ((n = write(fd, &size, sizeof(size))) == -1)
{
printf("ipc error in send data length.\n");
fflush(stdout);
return;
}
/* send data */
if ((n = write(fd, buf, size)) == -1)
{
printf("ipc error in send data.\n");
fflush(stdout);
return;
}
}
void
ecore_add_ipc_service(int service, void (*func) (int fd))
{
Ecore_Event_Ipc_Service *ipc_s;
/* delete the old service */
ecore_del_ipc_service(service);
/* new service struct */
ipc_s = NEW(Ecore_Event_Ipc_Service, 1);
ipc_s->next = ipc_services;
ipc_s->service = service;
ipc_s->func = func;
ipc_services = ipc_s;
}
void
ecore_del_ipc_service(int service)
{
START_LIST_DEL(Ecore_Event_Ipc_Service, ipc_services,
(_p->service == service));
FREE(_p);
END_LIST_DEL;
}
|