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 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");
88 if(smack_fsetlabel(fd, "*", SMACK_LABEL_IPIN) != 0) {
89 /* in case of unsupported filesystem on 'socket' */
90 /* or permission error by using 'emulator', bypass*/
91 if((errno != EOPNOTSUPP) && (errno != EPERM)) {
92 _E("labeling to socket(IPIN) error");
98 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
103 if (chmod(saddr.sun_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
104 /* Flawfinder: ignore*/
105 _E("failed to change the socket permission");
109 __set_sock_option(fd, 0);
111 if (listen(fd, 10) == -1) {
116 /* support app launched by shell script */
117 if (pid != WRT_LAUNCHPAD_PID) {
121 snprintf(p_saddr.sun_path, UNIX_PATH_MAX, "%s/%d",
122 AUL_SOCK_PREFIX, pgid);
123 if (link(saddr.sun_path, p_saddr.sun_path) < 0) {
125 _D("pg path - already exists");
127 _E("pg path - unknown create error");
135 int __create_client_sock(int pid)
138 struct sockaddr_un saddr = { 0, };
142 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
143 /* support above version 2.6.27*/
145 if (errno == EINVAL) {
146 fd = socket(AF_UNIX, SOCK_STREAM, 0);
148 _E("second chance - socket create error");
157 saddr.sun_family = AF_UNIX;
158 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pid);
160 ret = __connect_client_sock(fd, (struct sockaddr *)&saddr, sizeof(saddr),
163 _E("maybe peer not launched or peer daed\n");
175 __set_sock_option(fd, 1);
180 static int __connect_client_sock(int fd, const struct sockaddr *saptr, socklen_t salen,
189 struct timeval timeout;
191 flags = fcntl(fd, F_GETFL, 0);
192 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
195 if ((ret = connect(fd, (struct sockaddr *)saptr, salen)) < 0) {
196 if (errno != EAGAIN && errno != EINPROGRESS) {
197 fcntl(fd, F_SETFL, flags);
202 /* Do whatever we want while the connect is taking place. */
204 goto done; /* connect completed immediately */
207 FD_SET(fd, &readfds);
210 timeout.tv_usec = nsec;
212 if ((ret = select(fd + 1, &readfds, &writefds, NULL,
213 nsec ? &timeout : NULL)) == 0) {
214 close(fd); /* timeout */
219 if (FD_ISSET(fd, &readfds) || FD_ISSET(fd, &writefds)) {
221 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
222 return (-1); /* Solaris pending error */
224 return (-1); /* select error: sockfd not set*/
227 fcntl(fd, F_SETFL, flags);
237 * @brief Send data (in raw) to the process with 'pid' via socket
239 int __app_send_raw(int pid, int cmd, unsigned char *kb_data, int datalen)
244 app_pkt_t *pkt = NULL;
246 if (kb_data == NULL || datalen > AUL_SOCK_MAXBUFF - 8) {
247 _E("keybundle error\n");
251 fd = __create_client_sock(pid);
255 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
257 _E("Malloc Failed!");
260 memset(pkt, 0, AUL_SOCK_MAXBUFF);
264 memcpy(pkt->data, kb_data, datalen);
266 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
267 _E("sendto() failed - %d %d (errno %d)", len, datalen + 8, errno);
268 if (errno == EPIPE) {
269 _E("pid:%d, fd:%d\n", pid, fd);
283 len = recv(fd, &res, sizeof(int), 0);
285 if (errno == EAGAIN) {
286 _E("recv timeout \n");
298 app_pkt_t *__app_recv_raw(int fd, int *clifd, struct ucred *cr)
301 struct sockaddr_un aul_addr = { 0, };
303 app_pkt_t *pkt = NULL;
304 int cl = sizeof(struct ucred);
306 sun_size = sizeof(struct sockaddr_un);
308 if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr,
309 (socklen_t *) &sun_size)) == -1) {
315 if (getsockopt(*clifd, SOL_SOCKET, SO_PEERCRED, cr,
316 (socklen_t *) &cl) < 0) {
317 _E("peer information error");
322 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
327 memset(pkt, 0, AUL_SOCK_MAXBUFF);
329 __set_sock_option(*clifd, 1);
332 /* receive single packet from socket */
333 len = recv(*clifd, pkt, AUL_SOCK_MAXBUFF, 0);
338 if ((len < 8) || (len != (pkt->len + 8))) {
339 _E("recv error %d %d", len, pkt->len);
348 app_pkt_t *__app_send_cmd_with_result(int pid, int cmd)
352 app_pkt_t *pkt = NULL;
354 fd = __create_client_sock(pid);
358 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
360 _E("Malloc Failed!");
363 memset(pkt, 0, AUL_SOCK_MAXBUFF);
368 if ((len = send(fd, pkt, 8, 0)) != 8) {
369 _E("sendto() failed - %d", len);
370 if (errno == EPIPE) {
371 _E("pid:%d, fd:%d\n", pid, fd);
380 /* receive single packet from socket */
381 len = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
383 if (errno == EAGAIN) {
384 _E("recv timeout \n");
387 } else if (errno == EINTR) {
390 _E("recv error %s\n", strerror(errno));
395 _D("recv result = %d", len);