4 * Copyright (c) 2000 - 2011 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.
23 #include "email-ipc-socket.h"
24 #include "email-ipc-build.h"
26 #include "email-debug-log.h"
27 #include "email-types.h"
31 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
42 EXPORT_API bool emipc_init_email_socket(int *fd)
46 *fd = socket(AF_UNIX, SOCK_STREAM, 0);
49 EM_DEBUG_EXCEPTION("socket creation fails!!!: %s", strerror(errno));
53 EM_DEBUG_LOG("Socket fd = %d", *fd);
59 EXPORT_API bool emipc_close_email_socket(int fd)
61 EM_DEBUG_FUNC_BEGIN("fd[%d] to be removed", fd);
64 EM_DEBUG_LOG("fd %d removal done", fd);
71 static int emipc_writen(int fd, const char *buf, int len)
77 passed_len = send(fd, (const void *)buf, length, MSG_NOSIGNAL);
78 if (passed_len == -1) {
79 EM_DEBUG_LOG("write : %s", EM_STRERROR(errno));
80 if (errno == EINTR) continue;
82 } else if (passed_len == 0)
87 return (len - length);
90 EXPORT_API int emipc_send_email_socket(int fd, unsigned char *buf, int len)
92 EM_DEBUG_FUNC_BEGIN("fd [%d], buffer [%p], buf_len [%d]", fd, buf, len);
95 EM_DEBUG_EXCEPTION("No data to send");
99 EM_DEBUG_LOG("Sending %dB data to [fd = %d]", len, fd);
101 int write_len = emipc_writen(fd, (char*) buf, len);
102 if ( write_len != len) {
103 EM_DEBUG_LOG("WARNING: buf_size [%d] != write_len[%d]", len, write_len);
104 return EMF_ERROR_IPC_SOCKET_FAILURE;
110 static int emipc_readn(int fd, char *buf, int len)
116 read_len = read(fd, (void *)buf, length);
118 EM_DEBUG_EXCEPTION("Read : %s", EM_STRERROR(errno));
119 if (errno == EINTR) continue;
121 } else if (read_len == 0)
131 EXPORT_API int emipc_recv_email_socket(int fd, char **buf)
133 EM_DEBUG_FUNC_BEGIN();
136 EM_DEBUG_LOG("Buffer must not null");
137 return EMF_ERROR_INVALID_PARAM;
141 if (ioctl(fd, FIONREAD, &read_len)) {
142 EM_DEBUG_EXCEPTION("ioctl: %s", strerror(errno));
143 return EMF_ERROR_IPC_SOCKET_FAILURE;
146 *buf = (char *) malloc(read_len);
148 EM_DEBUG_EXCEPTION("Malloc failed");
149 return EMF_ERROR_OUT_OF_MEMORY;
151 memset(*buf, 0x00, read_len);
153 EM_DEBUG_LOG("[IPC Socket] Receiving Body begins for [%d] bytes", read_len);
154 int len = emipc_readn(fd, *buf, read_len);
155 if (read_len != len) {
157 EM_DEBUG_LOG("WARNING: buf_size [%d] != read_len[%d]", read_len, len);
158 return EMF_ERROR_IPC_SOCKET_FAILURE;
161 EM_DEBUG_LOG("[IPC Socket] Receiving [%d] bytes Completed", len);
166 EXPORT_API int emipc_accept_email_socket(int fd)
168 EM_DEBUG_FUNC_BEGIN();
171 EM_DEBUG_LOG("Server_socket not init");
172 return EMF_ERROR_INVALID_PARAM;
175 struct sockaddr_un remote;
176 int remote_len = sizeof(remote);
177 int client_fd = accept(fd, (struct sockaddr *)&remote, (socklen_t*) &remote_len);
178 if (client_fd == -1) {
179 EM_DEBUG_LOG("accept: %s", EM_STRERROR(errno));
180 return EMF_ERROR_IPC_SOCKET_FAILURE;
183 EM_DEBUG_LOG("%d is added", client_fd);
189 EXPORT_API int emipc_open_email_socket(int fd, const char *path)
191 EM_DEBUG_FUNC_BEGIN("path [%s]", path);
193 if (!path || strlen(path) > 108) {
194 EM_DEBUG_LOG("Path is null");
195 return EMF_ERROR_IPC_SOCKET_FAILURE;
199 EM_DEBUG_LOG("Socket not created %d", fd);
200 return EMF_ERROR_IPC_SOCKET_FAILURE;
203 struct sockaddr_un local;
204 local.sun_family = AF_UNIX;
205 strcpy(local.sun_path, path);
206 unlink(local.sun_path);
208 int len = strlen(local.sun_path) + sizeof(local.sun_family);
210 if (bind(fd, (struct sockaddr *)&local, len) == -1) {
211 EM_DEBUG_LOG("bind: %s", EM_STRERROR(errno));
212 return EMF_ERROR_IPC_SOCKET_FAILURE;
216 * determine permission of socket file
218 * - S_IRWXU : for user, allow read and write and execute
219 * - S_IRWXG : for group, allow read and write and execute
220 * - S_IRWXO : for other, allow read and write and execute
222 * - S_IRUSR, S_IWUSR, S_IXUSR : for user, allow only read, write, execute respectively
223 * - S_IRGRP, S_IWGRP, S_IXGRP : for group, allow only read, write, execute respectively
224 * - S_IROTH, S_IWOTH, S_IXOTH : for other, allow only read, write, execute respectively
226 mode_t sock_mode = (S_IRWXU | S_IRWXG | S_IRWXO); /* has 777 permission */
228 if (chmod(path, sock_mode) == -1) {
229 EM_DEBUG_LOG("chmod: %s", EM_STRERROR(errno));
230 return EMF_ERROR_IPC_SOCKET_FAILURE;
233 if (listen(fd, 10) == -1) {
234 EM_DEBUG_LOG("listen: %s", EM_STRERROR(errno));
235 return EMF_ERROR_IPC_SOCKET_FAILURE;
242 EXPORT_API bool emipc_connect_email_socket(int fd)
244 EM_DEBUG_FUNC_BEGIN();
245 struct sockaddr_un server;
246 server.sun_family = AF_UNIX;
247 strcpy(server.sun_path, EM_SOCKET_PATH);
249 int len = strlen(server.sun_path) + sizeof(server.sun_family);
251 if (connect(fd, (struct sockaddr *)&server, len) == -1) {
252 EM_DEBUG_LOG("Cannot connect server %s", EM_STRERROR(errno));