4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@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 <sys/types.h>
35 static int __connect_client_sock(int sockfd, const struct sockaddr *saptr, socklen_t salen,
38 static inline void __set_sock_option(int fd, int cli)
42 struct timeval tv = { 5, 200 * 1000 }; /* 5.2 sec */
44 size = AC_SOCK_MAXBUFF;
45 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
47 _E("setsockopt error");
50 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
52 _E("setsockopt error");
56 ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
58 _E("setsockopt error");
63 int _create_server_sock()
65 struct sockaddr_un saddr;
68 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
69 /* support above version 2.6.27*/
72 fd = socket(AF_UNIX, SOCK_STREAM, 0);
74 _E("second chance - socket create error");
83 memset(&saddr, 0, sizeof(saddr));
84 saddr.sun_family = AF_UNIX;
85 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s",AC_SOCK_NAME);
86 unlink(saddr.sun_path);
88 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
94 if (chmod(saddr.sun_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
95 /* Flawfinder: ignore*/
96 _E("failed to change the socket permission");
101 __set_sock_option(fd, 0);
103 if (listen(fd, 10) == -1) {
112 int _create_client_sock()
115 struct sockaddr_un saddr = { 0, };
119 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
120 /* support above version 2.6.27*/
122 if (errno == EINVAL) {
123 fd = socket(AF_UNIX, SOCK_STREAM, 0);
125 _E("second chance - socket create error");
134 saddr.sun_family = AF_UNIX;
135 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s", AC_SOCK_NAME);
137 ret = __connect_client_sock(fd, (struct sockaddr *)&saddr, sizeof(saddr),
140 _E("maybe peer not launched or peer daed\n");
152 __set_sock_option(fd, 1);
157 static int __connect_client_sock(int fd, const struct sockaddr *saptr, socklen_t salen,
166 struct timeval timeout;
168 flags = fcntl(fd, F_GETFL, 0);
169 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
172 if ((ret = connect(fd, (struct sockaddr *)saptr, salen)) < 0) {
173 if (errno != EAGAIN && errno != EINPROGRESS) {
174 (void)fcntl(fd, F_SETFL, flags);
179 /* Do whatever we want while the connect is taking place. */
181 goto done; /* connect completed immediately */
184 FD_SET(fd, &readfds);
187 timeout.tv_usec = nsec;
189 if ((ret = select(fd + 1, &readfds, &writefds, NULL,
190 nsec ? &timeout : NULL)) == 0) {
191 close(fd); /* timeout */
196 if (FD_ISSET(fd, &readfds) || FD_ISSET(fd, &writefds)) {
198 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
199 return (-1); /* Solaris pending error */
201 return (-1); /* select error: sockfd not set*/
204 (void)fcntl(fd, F_SETFL, flags);
214 * @brief Send data (in raw) to the process with 'pid' via socket
216 int _app_send_raw(int cmd, unsigned char *data, int datalen)
221 ac_pkt_t *pkt = NULL;
223 if (data == NULL || datalen > AC_SOCK_MAXBUFF - 8) {
224 _E("keybundle error\n");
228 fd = _create_client_sock();
232 pkt = (ac_pkt_t *) malloc(sizeof(char) * AC_SOCK_MAXBUFF);
234 _E("Malloc Failed!");
237 memset(pkt, 0, AC_SOCK_MAXBUFF);
241 memcpy(pkt->data, data, datalen);
243 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
244 _E("sendto() failed - %d %d", len, datalen + 8);
245 if (errno == EPIPE) {
260 len = recv(fd, &res, sizeof(int), 0);
262 if (errno == EAGAIN) {
263 _E("recv timeout \n");
270 _D("recv result = %d (%d)", res, len);
276 ac_pkt_t *_app_recv_raw(int fd, int *clifd, struct ucred *cr)
279 struct sockaddr_un aul_addr = { 0, };
281 ac_pkt_t *pkt = NULL;
282 int cl = sizeof(struct ucred);
284 sun_size = sizeof(struct sockaddr_un);
286 if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr,
287 (socklen_t *) &sun_size)) == -1) {
293 if (getsockopt(*clifd, SOL_SOCKET, SO_PEERCRED, cr,
294 (socklen_t *) &cl) < 0) {
295 _E("peer information error");
300 pkt = (ac_pkt_t *) malloc(sizeof(char) * AC_SOCK_MAXBUFF);
305 memset(pkt, 0, AC_SOCK_MAXBUFF);
307 __set_sock_option(*clifd, 1);
310 /* receive single packet from socket */
311 len = recv(*clifd, pkt, AC_SOCK_MAXBUFF, 0);
316 if ((len < 8) || (len != (pkt->len + 8))) {
317 _E("recv error %d %d", len, pkt->len);
326 int _send_result_to_server(int fd, int res)
328 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
330 _E("send failed due to EPIPE.\n");
331 _E("send fail to client");