4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <sys/select.h>
27 #include "email-ipc-build.h"
28 #include "email-proxy-socket.h"
29 #include "email-ipc-socket.h"
31 #include "email-debug-log.h"
32 #include "email-internal-types.h"
33 #include "email-utilities.h"
37 #include <sys/types.h>
46 GList *socket_head = NULL;
47 pthread_mutex_t proxy_mutex = PTHREAD_MUTEX_INITIALIZER;
49 EXPORT_API bool emipc_start_proxy_socket()
51 EM_DEBUG_FUNC_BEGIN();
55 ret = emipc_init_email_socket(&socket_fd);
57 EM_DEBUG_EXCEPTION("emipc_init_email_socket failed");
61 ret = emipc_connect_email_socket(socket_fd);
63 EM_DEBUG_EXCEPTION("emipc_connect_email_socket failed");
67 thread_socket_t* cur = (thread_socket_t*) em_malloc(sizeof(thread_socket_t));
69 EM_DEBUG_EXCEPTION("em_malloc failed");
75 cur->tid = pthread_self();
76 cur->socket_fd = socket_fd;
78 ENTER_CRITICAL_SECTION(proxy_mutex);
79 socket_head = g_list_prepend(socket_head, cur);
80 LEAVE_CRITICAL_SECTION(proxy_mutex);
85 EXPORT_API bool emipc_end_proxy_socket()
87 EM_DEBUG_FUNC_BEGIN();
88 EM_DEBUG_LOG("[IPCLib] emipc_end_proxy_socket_fd");
92 ENTER_CRITICAL_SECTION(proxy_mutex);
93 GList *cur = socket_head;
95 thread_socket_t* cur_socket = g_list_nth_data(cur,0);
97 /* close all sockets of the pid */
98 if( pid == cur_socket->pid ) {
99 emipc_close_email_socket(&cur_socket->socket_fd);
100 EM_SAFE_FREE(cur_socket);
102 cur = g_list_next(cur);
103 socket_head = g_list_remove_link(socket_head, del);
107 cur = g_list_next(cur);
109 LEAVE_CRITICAL_SECTION(proxy_mutex);
114 /* return result of emipc_send_email_socket
115 * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection */
116 EXPORT_API int emipc_send_proxy_socket(unsigned char *data, int len)
118 EM_DEBUG_FUNC_BEGIN();
119 int socket_fd = emipc_get_proxy_socket_id();
121 /* if thread socket is not created */
123 int ret = emipc_start_proxy_socket();
125 EM_DEBUG_EXCEPTION("[IPCLib] emipc_send_proxy_socket not connected");
126 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
128 socket_fd = emipc_get_proxy_socket_id();
131 int send_len = emipc_send_email_socket(socket_fd, data, len);
133 EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", socket_fd);
134 emipc_end_proxy_socket();
140 EXPORT_API int emipc_get_proxy_socket_id()
142 EM_DEBUG_FUNC_BEGIN();
143 pthread_t tid = pthread_self();
146 ENTER_CRITICAL_SECTION(proxy_mutex);
147 GList *cur = socket_head;
148 /* need to acquire lock */
149 for( ; cur ; cur = g_list_next(cur) ) {
150 thread_socket_t* cur_socket = g_list_nth_data(cur,0);
151 if( pthread_equal(tid, cur_socket->tid) ) {
152 socket_fd = cur_socket->socket_fd;
156 LEAVE_CRITICAL_SECTION(proxy_mutex);
157 EM_DEBUG_LOG("tid %d, socket_fd %d", tid, socket_fd);
161 /* return true, when event occurred
162 * false, when select error
164 static bool wait_for_reply (int fd)
169 EM_DEBUG_EXCEPTION("Invalid file description : [%d]", fd);
176 if (select(fd + 1, &fds, NULL, NULL, NULL) == -1) {
177 EM_DEBUG_EXCEPTION("[IPCLib] select: %s", strerror(errno) );
181 if (FD_ISSET(fd, &fds)) return true;
187 /* return result of emipc_recv_email_socket
188 * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection, or wrong fd */
189 EXPORT_API int emipc_recv_proxy_socket(char **data)
191 EM_DEBUG_FUNC_BEGIN();
192 int socket_fd = emipc_get_proxy_socket_id();
194 EM_DEBUG_EXCEPTION("[IPCLib] proxy_socket_fd[%p] is not available or disconnected", socket_fd);
195 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
198 if( !wait_for_reply(socket_fd) ) {
199 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
202 int recv_len = emipc_recv_email_socket(socket_fd, data);
204 EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", socket_fd);
205 emipc_end_proxy_socket();