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");
142 struct timespec time = {
144 .tv_nsec = 1000 * 1000 * 100
147 nanosleep(&time, NULL);
157 __set_sock_option(fd, 1);
162 static int __connect_client_sock(int fd, const struct sockaddr *saptr, socklen_t salen,
171 struct timeval timeout;
173 flags = fcntl(fd, F_GETFL, 0);
174 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
177 if ((ret = connect(fd, (struct sockaddr *)saptr, salen)) < 0) {
178 if (errno != EAGAIN && errno != EINPROGRESS) {
179 (void)fcntl(fd, F_SETFL, flags);
184 /* Do whatever we want while the connect is taking place. */
186 goto done; /* connect completed immediately */
189 FD_SET(fd, &readfds);
192 timeout.tv_usec = nsec;
194 if ((ret = select(fd + 1, &readfds, &writefds, NULL,
195 nsec ? &timeout : NULL)) == 0) {
196 close(fd); /* timeout */
201 if (FD_ISSET(fd, &readfds) || FD_ISSET(fd, &writefds)) {
203 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
204 return (-1); /* Solaris pending error */
206 return (-1); /* select error: sockfd not set*/
209 (void)fcntl(fd, F_SETFL, flags);
219 * @brief Send data (in raw) to the process with 'pid' via socket
221 int _app_send_raw(int cmd, unsigned char *data, int datalen)
226 ac_pkt_t *pkt = NULL;
228 if (data == NULL || datalen > AC_SOCK_MAXBUFF - 8) {
229 _E("keybundle error\n");
233 fd = _create_client_sock();
237 pkt = (ac_pkt_t *) malloc(sizeof(char) * AC_SOCK_MAXBUFF);
239 _E("Malloc Failed!");
242 memset(pkt, 0, AC_SOCK_MAXBUFF);
246 memcpy(pkt->data, data, datalen);
248 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
249 _E("sendto() failed - %d %d", len, datalen + 8);
250 if (errno == EPIPE) {
265 len = recv(fd, &res, sizeof(int), 0);
267 if (errno == EAGAIN) {
268 _E("recv timeout \n");
275 _D("recv result = %d (%d)", res, len);
281 ac_pkt_t *_app_recv_raw(int fd, int *clifd, struct ucred *cr)
284 struct sockaddr_un aul_addr = { 0, };
286 ac_pkt_t *pkt = NULL;
287 int cl = sizeof(struct ucred);
289 sun_size = sizeof(struct sockaddr_un);
291 if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr,
292 (socklen_t *) &sun_size)) == -1) {
298 if (getsockopt(*clifd, SOL_SOCKET, SO_PEERCRED, cr,
299 (socklen_t *) &cl) < 0) {
300 _E("peer information error");
305 pkt = (ac_pkt_t *) malloc(sizeof(char) * AC_SOCK_MAXBUFF);
310 memset(pkt, 0, AC_SOCK_MAXBUFF);
312 __set_sock_option(*clifd, 1);
315 /* receive single packet from socket */
316 len = recv(*clifd, pkt, AC_SOCK_MAXBUFF, 0);
321 if ((len < 8) || (len != (pkt->len + 8))) {
322 _E("recv error %d %d", len, pkt->len);
331 int _send_result_to_server(int fd, int res)
333 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
335 _E("send failed due to EPIPE.\n");
336 _E("send fail to client");