summaryrefslogtreecommitdiff
path: root/email-ipc/email-proxy/email-proxy-socket.c
blob: d9ab750786ed4be416bdd8914b4aa30ac340b09b (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
*  email-service
*
* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/



#include <unistd.h>
#include <sys/select.h>

#include "email-ipc-build.h"
#include "email-proxy-socket.h"
#include "email-ipc-socket.h"

#include "email-debug-log.h"
#include "email-internal-types.h"
#include "email-utilities.h"
#include <errno.h>
#include <glib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <unistd.h>

typedef struct {
	pid_t pid;
	pthread_t tid;
	int socket_fd;
} thread_socket_t;

GList *socket_head = NULL;
pthread_mutex_t proxy_mutex = PTHREAD_MUTEX_INITIALIZER;

EXPORT_API int emipc_start_proxy_socket()
{
	EM_DEBUG_FUNC_BEGIN();
	int ret = true;
	int socket_fd = 0;

	ret = emipc_init_email_socket(&socket_fd);
	if (!ret) {
		EM_DEBUG_EXCEPTION("emipc_init_email_socket failed");
		return false;
	}

	ret = emipc_connect_email_socket(socket_fd);
	if (ret != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("emipc_connect_email_socket failed");
		return ret;
	}

	thread_socket_t* cur = (thread_socket_t*) em_malloc(sizeof(thread_socket_t));
	if (!cur) {
		EM_DEBUG_EXCEPTION("em_malloc failed");
		return false;
	}

	/* add a socket */
	cur->pid = getpid();
	cur->tid = pthread_self();
	cur->socket_fd = socket_fd;

	ENTER_CRITICAL_SECTION(proxy_mutex);
	socket_head = g_list_prepend(socket_head, cur);
	LEAVE_CRITICAL_SECTION(proxy_mutex);

	return true;
}

EXPORT_API bool emipc_end_proxy_socket()
{
	EM_DEBUG_FUNC_BEGIN();
	EM_DEBUG_LOG("[IPCLib] emipc_end_proxy_socket");
	pthread_t tid = pthread_self();

	ENTER_CRITICAL_SECTION(proxy_mutex);
	GList *cur = socket_head;
	while (cur) {
		thread_socket_t* cur_socket = g_list_nth_data(cur, 0);

		/* close the socket of current thread */
		if (tid == cur_socket->tid) {
			emipc_close_email_socket(&cur_socket->socket_fd);
			EM_SAFE_FREE(cur_socket);
			GList *del = cur;
			cur = g_list_next(cur);
			socket_head = g_list_delete_link(socket_head, del);
			break;
		}

		cur = g_list_next(cur);
	}
	LEAVE_CRITICAL_SECTION(proxy_mutex);

	return true;
}

EXPORT_API bool emipc_end_all_proxy_sockets()
{
	EM_DEBUG_FUNC_BEGIN();
	EM_DEBUG_LOG("[IPCLib] emipc_end_all_proxy_sockets");

	pid_t pid = getpid();

	ENTER_CRITICAL_SECTION(proxy_mutex);
	GList *cur = socket_head;
	while (cur) {
		thread_socket_t* cur_socket = g_list_nth_data(cur, 0);

		/* close all sockets of the pid */
		if (pid == cur_socket->pid) {
			emipc_close_email_socket(&cur_socket->socket_fd);
			EM_SAFE_FREE(cur_socket);
			GList *del = cur;
			cur = g_list_next(cur);
			socket_head = g_list_delete_link(socket_head, del);
			continue;
		}

		cur = g_list_next(cur);
	}
	g_list_free(socket_head);
	socket_head = NULL;
	LEAVE_CRITICAL_SECTION(proxy_mutex);

	return true;
}

/* return result of emipc_send_email_socket
 * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection */
EXPORT_API int emipc_send_proxy_socket(unsigned char *data, int len)
{
	EM_DEBUG_FUNC_BEGIN("data [%p] len [%d]", data, len);
	int socket_fd = emipc_get_proxy_socket_id();

	/* if thread socket is not created */
	if (!socket_fd) {
		int ret = emipc_start_proxy_socket();
		if (!ret) {
			EM_DEBUG_EXCEPTION("[IPCLib] emipc_send_proxy_socket not connected");
			return EMAIL_ERROR_IPC_SOCKET_FAILURE;
		}
		socket_fd = emipc_get_proxy_socket_id();
	}

	int send_len = emipc_send_email_socket(socket_fd, data, len);
	if (send_len == 0) {
		EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", socket_fd);
		emipc_end_proxy_socket();
	}

	EM_DEBUG_FUNC_END("send_len [%d]", send_len);
	return send_len;
}

EXPORT_API int emipc_get_proxy_socket_id()
{
	EM_DEBUG_FUNC_BEGIN();
	pthread_t tid = pthread_self();
	int socket_fd = 0;

	ENTER_CRITICAL_SECTION(proxy_mutex);
	GList *cur = socket_head;
	/* need to acquire lock */
	for ( ; cur ; cur = g_list_next(cur)) {
		thread_socket_t* cur_socket = g_list_nth_data(cur, 0);
		if (pthread_equal(tid, cur_socket->tid)) {
			socket_fd = cur_socket->socket_fd;
			break;
		}
	}
	LEAVE_CRITICAL_SECTION(proxy_mutex);
	EM_DEBUG_FUNC_END("tid %d, socket_fd %d", tid, socket_fd);
	return socket_fd;
}

/* return true, when event occurred
 * false, when select error
 */

#define MAX_PROXY_EPOLL_EVENT 100

static bool wait_for_reply(int fd)
{
	if (fd < 0) {
		EM_DEBUG_EXCEPTION("Invalid file description : [%d]", fd);
		return false;
	}
#if 0
	int err = -1;
	fd_set fds;
	struct timeval tv;

	FD_ZERO(&fds);
	FD_SET(fd, &fds);

	tv.tv_sec  = 20; /* should be tuned */
	tv.tv_usec = 0;

	EM_DEBUG_LOG_DEV("wait for response [%d]", fd);
	err = select(fd + 1, &fds, NULL, NULL, &tv);
	if (err == -1) {
		EM_DEBUG_EXCEPTION("[IPCLib] select error[%d] fd[%d]", errno, fd);
		return false;
	} else if (err == 0) {
		EM_DEBUG_EXCEPTION("[IPCLib] select timeout fd[%d]", fd);
		return false;
	}

	if (FD_ISSET(fd, &fds)) return true;
#endif

	int i = 0;
	int ret = false;
	int proxy_epfd = -1;
	int event_num = 0;
	int timeout = 20000; /* 20 seconds */
	char errno_buf[ERRNO_BUF_SIZE] = {0};
	struct epoll_event proxy_ev = {0};
	struct epoll_event proxy_ev_events[MAX_PROXY_EPOLL_EVENT] = {{0}, };

	proxy_epfd = epoll_create(MAX_PROXY_EPOLL_EVENT);
	if (proxy_epfd < 0) {
		EM_DEBUG_EXCEPTION("epoll_create failed : [%d][%s]", errno, EM_STRERROR(errno_buf));
		goto FINISH_OFF;
	}

	proxy_ev.data.fd = fd;
	proxy_ev.events = EPOLLIN | EPOLLONESHOT;

	if (epoll_ctl(proxy_epfd, EPOLL_CTL_ADD, fd, &proxy_ev) == -1) {
		EM_DEBUG_EXCEPTION("epoll_ctl wait : [%d][%s]", errno, EM_STRERROR(errno_buf));
		goto FINISH_OFF;
	}

	EM_DEBUG_LOG("Wait for response poll_fd:[%d], proxy_fd:[%d]", proxy_epfd, fd);
	event_num = epoll_wait(proxy_epfd, proxy_ev_events, MAX_PROXY_EPOLL_EVENT, timeout);
	if (event_num == -1) {
		EM_DEBUG_EXCEPTION("epoll_wait failed : [%d][%s]", errno, EM_STRERROR(errno_buf));
		goto FINISH_OFF;
	} else if (event_num == 0) {
		EM_DEBUG_EXCEPTION("Occured timeout proxy_fd[%d]", fd);
		goto FINISH_OFF;
	} else {
		for (i = 0; i < event_num; i++) {
			if (proxy_ev_events[i].events & EPOLLIN) {
				EM_DEBUG_LOG("Received event to stub");
				ret = true;
			}
		}
	}

FINISH_OFF:

	if (proxy_epfd >= 0)
		close(proxy_epfd);

	return ret;
}


/* return result of emipc_recv_email_socket
 * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection, or wrong fd */
EXPORT_API int emipc_recv_proxy_socket(char **data)
{
	EM_DEBUG_FUNC_BEGIN();
	int socket_fd = emipc_get_proxy_socket_id();
	if (!socket_fd) {
		EM_DEBUG_EXCEPTION("[IPCLib] proxy_socket_fd[%d] is not available or disconnected", socket_fd);
		return EMAIL_ERROR_IPC_SOCKET_FAILURE;
	}

	if (!wait_for_reply(socket_fd)) {
		return EMAIL_ERROR_IPC_SOCKET_FAILURE;
	}

	int recv_len = emipc_recv_email_socket(socket_fd, data);
	if (recv_len == 0) {
		EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", socket_fd);
		emipc_end_proxy_socket();
	}

	return recv_len;
}