2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/types.h>
23 #include <sys/smack.h>
28 #include "simple_util.h"
30 static int __connect_client_sock(int sockfd, const struct sockaddr *saptr, socklen_t salen,
34 static inline void __set_sock_option(int fd, int cli)
37 struct timeval tv = { 3, 200 * 1000 }; /* 3.2 sec */
39 size = AUL_SOCK_MAXBUFF;
40 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
41 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
43 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
46 int __create_server_sock(int pid)
48 struct sockaddr_un saddr;
49 struct sockaddr_un p_saddr;
53 /* Create basedir for our sockets */
55 (void) mkdir(AUL_SOCK_PREFIX, S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX);
58 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
59 /* support above version 2.6.27*/
61 if (errno == EINVAL) {
62 fd = socket(AF_UNIX, SOCK_STREAM, 0);
64 _E("second chance - socket create error");
73 bzero(&saddr, sizeof(saddr));
74 saddr.sun_family = AF_UNIX;
75 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pid);
76 unlink(saddr.sun_path);
78 /* labeling to socket for SMACK */
79 if(getuid() == 0) { // this is meaningful iff current user is ROOT
80 if(smack_fsetlabel(fd, "@", SMACK_LABEL_IPOUT) != 0) {
81 /* in case of unsupported filesystem on 'socket' */
82 /* or permission error by using 'emulator', bypass*/
83 if((errno != EOPNOTSUPP) && (errno != EPERM)) {
84 _E("labeling to socket(IPOUT) error");
89 if(smack_fsetlabel(fd, "*", SMACK_LABEL_IPIN) != 0) {
90 /* in case of unsupported filesystem on 'socket' */
91 /* or permission error by using 'emulator', bypass*/
92 if((errno != EOPNOTSUPP) && (errno != EPERM)) {
93 _E("labeling to socket(IPIN) error");
100 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
106 if (chmod(saddr.sun_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
107 /* Flawfinder: ignore*/
108 _E("failed to change the socket permission");
113 __set_sock_option(fd, 0);
115 if (listen(fd, 10) == -1) {
121 /* support app launched by shell script */
122 if (pid != WRT_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 (void) 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");
303 app_pkt_t *__app_recv_raw(int fd, int *clifd, struct ucred *cr)
306 struct sockaddr_un aul_addr = { 0, };
308 app_pkt_t *pkt = NULL;
309 int cl = sizeof(struct ucred);
311 sun_size = sizeof(struct sockaddr_un);
313 if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr,
314 (socklen_t *) &sun_size)) == -1) {
320 if (getsockopt(*clifd, SOL_SOCKET, SO_PEERCRED, cr,
321 (socklen_t *) &cl) < 0) {
322 _E("peer information error");
327 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
332 memset(pkt, 0, AUL_SOCK_MAXBUFF);
334 __set_sock_option(*clifd, 1);
337 /* receive single packet from socket */
338 len = recv(*clifd, pkt, AUL_SOCK_MAXBUFF, 0);
343 if ((len < 8) || (len != (pkt->len + 8))) {
344 _E("recv error %d %d", len, pkt->len);
353 app_pkt_t *__app_send_cmd_with_result(int pid, int cmd)
357 app_pkt_t *pkt = NULL;
359 fd = __create_client_sock(pid);
363 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
365 _E("Malloc Failed!");
368 memset(pkt, 0, AUL_SOCK_MAXBUFF);
373 if ((len = send(fd, pkt, 8, 0)) != 8) {
374 _E("sendto() failed - %d", len);
375 if (errno == EPIPE) {
376 _E("pid:%d, fd:%d\n", pid, fd);
385 /* receive single packet from socket */
386 len = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
388 if (errno == EAGAIN) {
389 _E("recv timeout \n");
392 } else if (errno == EINTR) {
395 _E("recv error %s\n", strerror(errno));
400 _D("recv result = %d", len);