4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jungmin Cho <chivalry.cho@samsung.com>, Gwangho Hwang <gwang.hwang@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/smack.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 (void) 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 memset(&saddr, 0, 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(smack_fsetlabel(fd, "@", SMACK_LABEL_IPOUT) != 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");
94 if(smack_fsetlabel(fd, "*", SMACK_LABEL_IPIN) != 0) {
95 /* in case of unsupported filesystem on 'socket' */
96 /* or permission error by using 'emulator', bypass*/
97 if((errno != EOPNOTSUPP) && (errno != EPERM)) {
98 _E("labeling to socket(IPIN) error");
105 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
111 if (chmod(saddr.sun_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
112 /* Flawfinder: ignore*/
113 _E("failed to change the socket permission");
118 __set_sock_option(fd, 0);
120 if (listen(fd, 128) == -1) {
126 /* support app launched by shell script */
127 /*if (pid != LAUNCHPAD_PID) {
131 snprintf(p_saddr.sun_path, UNIX_PATH_MAX, "%s/%d",
132 AUL_SOCK_PREFIX, pgid);
133 if (link(saddr.sun_path, p_saddr.sun_path) < 0) {
135 _D("pg path - already exists");
137 _E("pg path - unknown create error");
145 int __create_client_sock(int pid)
148 struct sockaddr_un saddr = { 0, };
152 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
153 /* support above version 2.6.27*/
155 if (errno == EINVAL) {
156 fd = socket(AF_UNIX, SOCK_STREAM, 0);
158 _E("second chance - socket create error");
167 saddr.sun_family = AF_UNIX;
168 snprintf(saddr.sun_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pid);
170 ret = __connect_client_sock(fd, (struct sockaddr *)&saddr, sizeof(saddr),
173 _E("maybe peer not launched or peer daed\n");
185 __set_sock_option(fd, 1);
190 static int __connect_client_sock(int fd, const struct sockaddr *saptr, socklen_t salen,
199 struct timeval timeout;
201 flags = fcntl(fd, F_GETFL, 0);
202 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
205 if ((ret = connect(fd, (struct sockaddr *)saptr, salen)) < 0) {
206 if (errno != EAGAIN && errno != EINPROGRESS) {
207 fcntl(fd, F_SETFL, flags);
212 /* Do whatever we want while the connect is taking place. */
214 goto done; /* connect completed immediately */
217 FD_SET(fd, &readfds);
220 timeout.tv_usec = nsec;
222 if ((ret = select(fd + 1, &readfds, &writefds, NULL,
223 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 */
234 return (-1); /* select error: sockfd not set*/
237 (void) fcntl(fd, F_SETFL, flags);
247 * @brief Send data (in raw) to the process with 'pid' via socket
249 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 _D("pid(%d) : cmd(%d)", pid, cmd);
264 fd = __create_client_sock(pid);
268 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
270 _E("Malloc Failed!");
273 memset(pkt, 0, AUL_SOCK_MAXBUFF);
277 memcpy(pkt->data, kb_data, datalen);
279 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
280 _E("sendto() failed - %d %d (errno %d)", len, datalen + 8, errno);
282 while (len != datalen + 8) {
283 ret = send(fd, &pkt->data[len-8], datalen + 8 - len, 0);
285 _E("second sendto() failed - %d %d (errno %d)", ret, datalen + 8, errno);
286 if (errno == EPIPE) {
287 _E("pid:%d, fd:%d\n", pid, fd);
297 _D("sendto() len - %d %d", len, datalen + 8);
300 if (errno == EPIPE) {
301 _E("pid:%d, fd:%d\n", pid, fd);
316 len = recv(fd, &res, sizeof(int), 0);
318 if (errno == EAGAIN) {
319 _E("recv timeout \n");
331 int __app_send_raw_with_noreply(int pid, int cmd, unsigned char *kb_data, int datalen)
337 app_pkt_t *pkt = NULL;
339 if (kb_data == NULL || datalen > AUL_SOCK_MAXBUFF - 8) {
340 _E("keybundle error\n");
344 _D("pid(%d) : cmd(%d)", pid, cmd);
346 fd = __create_client_sock(pid);
350 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
352 _E("Malloc Failed!");
355 memset(pkt, 0, AUL_SOCK_MAXBUFF);
359 memcpy(pkt->data, kb_data, datalen);
361 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
362 _E("sendto() failed - %d %d (errno %d)", len, datalen + 8, errno);
364 while (len != datalen + 8) {
365 ret = send(fd, &pkt->data[len-8], datalen + 8 - len, 0);
367 _E("second sendto() failed - %d %d (errno %d)", ret, datalen + 8, errno);
368 if (errno == EPIPE) {
369 _E("pid:%d, fd:%d\n", pid, fd);
379 _D("sendto() len - %d %d", len, datalen + 8);
382 if (errno == EPIPE) {
383 _E("pid:%d, fd:%d\n", pid, fd);
403 app_pkt_t *__app_recv_raw(int fd, int *clifd, struct ucred *cr)
407 struct sockaddr_un aul_addr = { 0, };
409 app_pkt_t *pkt = NULL;
410 int cl = sizeof(struct ucred);
412 sun_size = sizeof(struct sockaddr_un);
414 if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr,
415 (socklen_t *) &sun_size)) == -1) {
421 if (getsockopt(*clifd, SOL_SOCKET, SO_PEERCRED, cr,
422 (socklen_t *) &cl) < 0) {
423 _E("peer information error");
428 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
433 memset(pkt, 0, AUL_SOCK_MAXBUFF);
435 __set_sock_option(*clifd, 1);
438 /* receive single packet from socket */
439 len = recv(*clifd, pkt, AUL_SOCK_MAXBUFF, 0);
445 _E("recv error %d %d", len, pkt->len);
451 while( len != (pkt->len + 8) ) {
452 ret = recv(*clifd, &pkt->data[len-8], AUL_SOCK_MAXBUFF, 0);
454 _E("recv error %d %d", len, pkt->len);
460 _D("recv len %d %d", len, pkt->len);
466 app_pkt_t *__app_send_cmd_with_result(int pid, int cmd, unsigned char *kb_data, int datalen)
470 app_pkt_t *pkt = NULL;
472 fd = __create_client_sock(pid);
476 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
478 _E("Malloc Failed!");
481 memset(pkt, 0, AUL_SOCK_MAXBUFF);
486 memcpy(pkt->data, kb_data, datalen);
489 if ((len = send(fd, pkt, datalen + 8, 0)) != datalen + 8) {
490 _E("sendto() failed - %d", len);
491 if (errno == EPIPE) {
492 _E("pid:%d, fd:%d\n", pid, fd);
501 /* receive single packet from socket */
502 len = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
504 if (errno == EAGAIN) {
505 _E("recv timeout \n");
509 } else if (errno == EINTR) {
512 _E("recv error %s\n", strerror(errno));
518 _D("recv result = %d", len);