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>
28 #include <sys/xattr.h>
33 #include "simple_util.h"
35 static int __connect_client_sock(int sockfd, const struct sockaddr *saptr, socklen_t salen,
39 static inline void __set_sock_option(int fd, int cli)
42 struct timeval tv = { 3, 200 * 1000 }; /* 3.2 sec */
44 size = AUL_SOCK_MAXBUFF;
45 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
46 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
48 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
51 int __create_server_sock(int pid)
53 struct sockaddr_un saddr;
54 struct sockaddr_un p_saddr;
58 /* Create basedir for our sockets */
60 mkdir(AUL_SOCK_PREFIX, S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX);
63 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
64 /* support above version 2.6.27*/
66 if (errno == EINVAL) {
67 fd = socket(AF_UNIX, SOCK_STREAM, 0);
69 _E("second chance - socket create error");
78 bzero(&saddr, sizeof(saddr));
79 saddr.sun_family = AF_UNIX;
80 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pid);
81 unlink(saddr.sun_path);
83 /* labeling to socket for SMACK */
84 if(getuid() == 0) { // this is meaningful iff current user is ROOT
85 if((fsetxattr(fd, "security.SMACK64IPOUT", "@", 2, 0)) < 0) {
86 /* in case of unsupported filesystem on 'socket' */
87 /* or permission error by using 'emulator', bypass*/
88 if((errno != EOPNOTSUPP) && (errno != EPERM)) {
89 _E("labeling to socket(IPOUT) error");
93 if((fsetxattr(fd, "security.SMACK64IPIN", "*", 2, 0)) < 0) {
94 /* in case of unsupported filesystem on 'socket' */
95 /* or permission error by using 'emulator', bypass*/
96 if((errno != EOPNOTSUPP) && (errno != EPERM)) {
97 _E("labeling to socket(IPIN) error");
103 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
108 if (chmod(saddr.sun_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
109 /* Flawfinder: ignore*/
110 _E("failed to change the socket permission");
114 __set_sock_option(fd, 0);
116 if (listen(fd, 10) == -1) {
121 /* support app launched by shell script */
122 if (pid != LAUNCHPAD_PID) {
126 snprintf(p_saddr.sun_path, UNIX_PATH_MAX, "%s/%d",
127 AUL_SOCK_PREFIX, pgid);
128 if (link(saddr.sun_path, p_saddr.sun_path) < 0) {
130 _D("pg path - already exists");
132 _E("pg path - unknown create error");
140 int __create_client_sock(int pid)
143 struct sockaddr_un saddr = { 0, };
147 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
148 /* support above version 2.6.27*/
150 if (errno == EINVAL) {
151 fd = socket(AF_UNIX, SOCK_STREAM, 0);
153 _E("second chance - socket create error");
162 saddr.sun_family = AF_UNIX;
163 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pid);
165 ret = __connect_client_sock(fd, (struct sockaddr *)&saddr, sizeof(saddr),
168 _E("maybe peer not launched or peer daed\n");
180 __set_sock_option(fd, 1);
185 static int __connect_client_sock(int fd, const struct sockaddr *saptr, socklen_t salen,
194 struct timeval timeout;
196 flags = fcntl(fd, F_GETFL, 0);
197 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
200 if ((ret = connect(fd, (struct sockaddr *)saptr, salen)) < 0) {
201 if (errno != EAGAIN && errno != EINPROGRESS) {
202 fcntl(fd, F_SETFL, flags);
207 /* Do whatever we want while the connect is taking place. */
209 goto done; /* connect completed immediately */
212 FD_SET(fd, &readfds);
215 timeout.tv_usec = nsec;
217 if ((ret = select(fd + 1, &readfds, &writefds, NULL,
218 nsec ? &timeout : NULL)) == 0) {
219 close(fd); /* timeout */
224 if (FD_ISSET(fd, &readfds) || FD_ISSET(fd, &writefds)) {
226 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
227 return (-1); /* Solaris pending error */
229 return (-1); /* select error: sockfd not set*/
232 fcntl(fd, F_SETFL, flags);
242 * @brief Send data (in raw) to the process with 'pid' via socket
244 int __app_send_raw(int pid, int cmd, unsigned char *kb_data, int datalen)
249 app_pkt_t *pkt = NULL;
251 if (kb_data == NULL || datalen > AUL_SOCK_MAXBUFF - 8) {
252 _E("keybundle error\n");
256 fd = __create_client_sock(pid);
260 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
262 _E("Malloc Failed!");
265 memset(pkt, 0, AUL_SOCK_MAXBUFF);
269 memcpy(pkt->data, kb_data, datalen);
271 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
272 _E("sendto() failed - %d %d (errno %d)", len, datalen + 8, errno);
273 if (errno == EPIPE) {
274 _E("pid:%d, fd:%d\n", pid, fd);
288 len = recv(fd, &res, sizeof(int), 0);
290 if (errno == EAGAIN) {
291 _E("recv timeout \n");
298 _D("recv result = %d (%d)", res, len);
304 app_pkt_t *__app_recv_raw(int fd, int *clifd, struct ucred *cr)
307 struct sockaddr_un aul_addr = { 0, };
309 app_pkt_t *pkt = NULL;
310 int cl = sizeof(struct ucred);
312 sun_size = sizeof(struct sockaddr_un);
314 if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr,
315 (socklen_t *) &sun_size)) == -1) {
321 if (getsockopt(*clifd, SOL_SOCKET, SO_PEERCRED, cr,
322 (socklen_t *) &cl) < 0) {
323 _E("peer information error");
328 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
333 memset(pkt, 0, AUL_SOCK_MAXBUFF);
335 __set_sock_option(*clifd, 1);
338 /* receive single packet from socket */
339 len = recv(*clifd, pkt, AUL_SOCK_MAXBUFF, 0);
344 if ((len < 8) || (len != (pkt->len + 8))) {
345 _E("recv error %d %d", len, pkt->len);
354 app_pkt_t *__app_send_cmd_with_result(int pid, int cmd)
358 app_pkt_t *pkt = NULL;
360 fd = __create_client_sock(pid);
364 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
366 _E("Malloc Failed!");
369 memset(pkt, 0, AUL_SOCK_MAXBUFF);
374 if ((len = send(fd, pkt, 8, 0)) != 8) {
375 _E("sendto() failed - %d", len);
376 if (errno == EPIPE) {
377 _E("pid:%d, fd:%d\n", pid, fd);
386 /* receive single packet from socket */
387 len = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
389 if (errno == EAGAIN) {
390 _E("recv timeout \n");
393 } else if (errno == EINTR) {
396 _E("recv error %s\n", strerror(errno));
401 _D("recv result = %d", len);