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.
17 #include <sys/types.h>
22 #include <sys/smack.h>
27 #include "simple_util.h"
29 static int __connect_client_sock(int sockfd,
30 const struct sockaddr *saptr,
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));
47 int __create_server_sock(int pid)
49 struct sockaddr_un saddr;
50 struct sockaddr_un p_saddr;
54 /* Create basedir for our sockets */
56 (void) mkdir(AUL_SOCK_PREFIX, S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX);
59 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
60 /* support above version 2.6.27*/
62 if (errno == EINVAL) {
63 fd = socket(AF_UNIX, SOCK_STREAM, 0);
65 _E("second chance - socket create error");
74 bzero(&saddr, sizeof(saddr));
75 saddr.sun_family = AF_UNIX;
76 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pid);
77 unlink(saddr.sun_path);
79 /* labeling to socket for SMACK */
80 if (getuid() == 0) { // this is meaningful iff current user is ROOT
81 if (smack_fsetlabel(fd, "@", SMACK_LABEL_IPOUT) != 0) {
82 /* in case of unsupported filesystem on 'socket' */
83 /* or permission error by using 'emulator', bypass*/
84 if ((errno != EOPNOTSUPP) && (errno != EPERM)) {
85 _E("labeling to socket(IPOUT) error");
90 if (smack_fsetlabel(fd, "*", SMACK_LABEL_IPIN) != 0) {
91 /* in case of unsupported filesystem on 'socket' */
92 /* or permission error by using 'emulator', bypass*/
93 if ((errno != EOPNOTSUPP) && (errno != EPERM)) {
94 _E("labeling to socket(IPIN) error");
101 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
107 if (chmod(saddr.sun_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
108 /* Flawfinder: ignore*/
109 _E("failed to change the socket permission");
114 __set_sock_option(fd, 0);
116 if (listen(fd, 10) == -1) {
122 /* support app launched by shell script */
123 if (pid != WRT_LAUNCHPAD_PID) {
127 snprintf(p_saddr.sun_path, UNIX_PATH_MAX, "%s/%d",
128 AUL_SOCK_PREFIX, pgid);
129 if (link(saddr.sun_path, p_saddr.sun_path) < 0) {
130 if (errno == EEXIST) {
131 _D("pg path - already exists");
133 _E("pg path - unknown create error");
142 int __create_client_sock(int pid)
145 struct sockaddr_un saddr = { 0, };
149 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
150 /* support above version 2.6.27*/
152 if (errno == EINVAL) {
153 fd = socket(AF_UNIX, SOCK_STREAM, 0);
155 _E("second chance - socket create error");
164 saddr.sun_family = AF_UNIX;
165 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pid);
167 ret = __connect_client_sock(fd, (struct sockaddr *)&saddr, sizeof(saddr),
170 _E("maybe peer not launched or peer daed\n");
182 __set_sock_option(fd, 1);
187 static int __connect_client_sock(int fd,
188 const struct sockaddr *saptr,
198 struct timeval timeout;
200 flags = fcntl(fd, F_GETFL, 0);
201 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
204 if ((ret = connect(fd, (struct sockaddr *)saptr, salen)) < 0) {
205 if (errno != EAGAIN && errno != EINPROGRESS) {
206 fcntl(fd, F_SETFL, flags);
211 /* Do whatever we want while the connect is taking place. */
213 goto done; /* connect completed immediately */
216 FD_SET(fd, &readfds);
219 timeout.tv_usec = nsec;
221 if ((ret = select(fd + 1, &readfds, &writefds, NULL,
222 nsec ? &timeout : NULL)) == 0)
224 close(fd); /* timeout */
229 if (FD_ISSET(fd, &readfds) || FD_ISSET(fd, &writefds)) {
231 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
232 return (-1); /* Solaris pending error */
235 return (-1); /* select error: sockfd not set*/
238 (void) fcntl(fd, F_SETFL, flags);
248 * @brief Send data (in raw) to the process with 'pid' via socket
250 int __app_send_raw(int pid, int cmd, unsigned char *kb_data, int datalen)
255 app_pkt_t *pkt = NULL;
257 if (kb_data == NULL || datalen > AUL_SOCK_MAXBUFF - 8) {
258 _E("keybundle error\n");
262 fd = __create_client_sock(pid);
267 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
269 _E("Malloc Failed!");
272 memset(pkt, 0, AUL_SOCK_MAXBUFF);
276 memcpy(pkt->data, kb_data, datalen);
278 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
279 _E("sendto() failed - %d %d (errno %d)", len, datalen + 8, errno);
280 if (errno == EPIPE) {
281 _E("pid:%d, fd:%d\n", pid, fd);
295 len = recv(fd, &res, sizeof(int), 0);
297 if (errno == EAGAIN) {
298 _E("recv timeout \n");
310 app_pkt_t *__app_recv_raw(int fd, int *clifd, struct ucred *cr)
313 struct sockaddr_un aul_addr = { 0, };
315 app_pkt_t *pkt = NULL;
316 int cl = sizeof(struct ucred);
318 sun_size = sizeof(struct sockaddr_un);
320 if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr,
321 (socklen_t *) &sun_size)) == -1)
323 if (errno != EINTR) {
329 if (getsockopt(*clifd, SOL_SOCKET, SO_PEERCRED, cr,
330 (socklen_t *) &cl) < 0)
332 _E("peer information error");
337 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
342 memset(pkt, 0, AUL_SOCK_MAXBUFF);
344 __set_sock_option(*clifd, 1);
347 /* receive single packet from socket */
348 len = recv(*clifd, pkt, AUL_SOCK_MAXBUFF, 0);
350 if (errno == EINTR) {
355 if ((len < 8) || (len != (pkt->len + 8))) {
356 _E("recv error %d %d", len, pkt->len);
365 app_pkt_t *__app_send_cmd_with_result(int pid, int cmd)
369 app_pkt_t *pkt = NULL;
371 fd = __create_client_sock(pid);
376 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
378 _E("Malloc Failed!");
381 memset(pkt, 0, AUL_SOCK_MAXBUFF);
386 if ((len = send(fd, pkt, 8, 0)) != 8) {
387 _E("sendto() failed - %d", len);
388 if (errno == EPIPE) {
389 _E("pid:%d, fd:%d\n", pid, fd);
398 /* receive single packet from socket */
399 len = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
401 if (errno == EAGAIN) {
402 _E("recv timeout \n");
405 } else if (errno == EINTR) {
408 _E("recv error %s\n", strerror(errno));
413 _D("recv result = %d", len);