Tizen release 1.0
[pkgs/p/phone-lock.git] / phone-lock-common / src / phone-lock-util.c
1 /*
2  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved 
3  *
4  * This file is part of <phone-lock>
5  * Written by <Seungtaek Chung> <seungtaek.chung@samsung.com>, <Mi-Ju Lee> <miju52.lee@samsung.com>, <Xi Zhichan> <zhichan.xi@samsung.com>
6  *
7  * PROPRIETARY/CONFIDENTIAL
8  *
9  * This software is the confidential and proprietary information of SAMSUNG ELECTRONICS ("Confidential Information").
10  * You shall not disclose such Confidential Information and shall use it only in accordance
11  * with the terms of the license agreement you entered into with SAMSUNG ELECTRONICS.
12  * SAMSUNG make no representations or warranties about the suitability of the software,
13  * either express or implied, including but not limited to the implied warranties of merchantability,
14  * fitness for a particular purpose, or non-infringement.
15  * SAMSUNG shall not be liable for any damages suffered by licensee as a result of using,
16  * modifying or distributing this software or its derivatives.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <appcore-common.h>
23 #include <Ecore_X.h>
24 #include <Elementary.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <glib.h>
30 #include <poll.h>
31
32 #include "phone-lock-util.h"
33
34 #define LINEMAX 256
35 #define MAXFILELEN      1048576
36 #define PHONE_LOCK_SOCK_PREFIX "/tmp/phlock"
37 #define PHONE_LOCK_SOCK_MAXBUFF 65535
38
39 static void _phone_lock_win_del(void *data, Evas_Object * obj, void *event)
40 {
41         elm_exit();
42 }
43
44 inline static void _phone_lock_set_sock_option(int fd, int cli)
45 {
46         int size;
47         struct timeval tv = { 1, 200 * 1000 };
48
49         size = PHONE_LOCK_SOCK_MAXBUFF;
50         setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
51         setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
52         if (cli)
53                 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
54 }
55
56 static int _phone_lock_create_sock(void)
57 {
58         struct sockaddr_un saddr;
59         int fd;
60         int retry = 1;
61
62         _DBG("func=%s", __func__);
63
64         fd = socket(AF_UNIX, SOCK_STREAM, 0);
65         if (fd < 0) {
66                 _DBG("errno=%s", errno);
67                 if (errno == EINVAL) {
68                         fd = socket(AF_UNIX, SOCK_STREAM, 0);
69                         if (fd < 0) {
70                                 _DBG("second chance - socket create error");
71                                 return -1;
72                         }
73                 } else {
74                         _DBG("socket error");
75                         return -1;
76                 }
77         }
78
79         bzero(&saddr, sizeof(saddr));
80         saddr.sun_family = AF_UNIX;
81
82         strncpy(saddr.sun_path, PHONE_LOCK_SOCK_PREFIX,
83                 strlen(PHONE_LOCK_SOCK_PREFIX));
84         saddr.sun_path[strlen(PHONE_LOCK_SOCK_PREFIX)] = 0;
85
86         _DBG("saddr.sun_path = %s", saddr.sun_path);
87
88  retry_con:
89         if (connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
90                 _DBG("maybe peer not launched or peer daed\n");
91                 if (retry > 0) {
92                         usleep(100 * 1000);
93                         retry--;
94                         goto retry_con;
95                 }
96                 close(fd);
97                 return -1;
98         }
99         _phone_lock_set_sock_option(fd, 1);
100
101         return fd;
102 }
103
104 void phone_lock_log_t(char *fmt, ...)
105 {
106         va_list ap;
107         FILE *fd = 0;
108         char buf[LINEMAX] = { 0, };
109         char debugString[LINEMAX] = { 0, };
110
111         va_start(ap, fmt);
112         vsnprintf(buf, sizeof(buf), fmt, ap);
113         va_end(ap);
114         int fileLen = 0;
115         struct tm local_t;
116         time_t current_time = 0;
117         bzero((char *)&debugString, LINEMAX);
118         time(&current_time);
119         gmtime_r(&current_time, &local_t);
120         int len = snprintf(debugString, sizeof(debugString),
121                            "[%d-%02d-%02d, %02d:%02d:%02d]: ",
122                            local_t.tm_year + 1900, local_t.tm_mon + 1,
123                            local_t.tm_mday, local_t.tm_hour, local_t.tm_min,
124                            local_t.tm_sec);
125         if (len == -1) {
126                 return;
127         } else {
128                 debugString[len] = '\0';
129         }
130         len = g_strlcat(debugString, buf, LINEMAX);
131         if (len >= LINEMAX) {
132                 return;
133         } else {
134                 debugString[len] = '\n';
135         }
136         if ((fd = fopen(LOGFILE, "at+")) == NULL) {
137                 PHONE_LOCK_ERR
138                     ("File fopen fail for writing Pwlock information");
139         } else {
140                 int pid = -1;
141                 if (fwrite(debugString, strlen(debugString), 1, fd) < 1) {
142                         PHONE_LOCK_ERR
143                             ("File fwrite fail for writing Pwlock information");
144                         fclose(fd);
145                         if ((pid = fork()) < 0) {
146                         } else if (pid == 0) {
147                                 execl("/bin/rm", "rm", "-f", LOGFILE,
148                                       (char *)0);
149                         }
150                 } else {
151                         fseek(fd, 0l, SEEK_END);
152                         fileLen = ftell(fd);
153                         if (fileLen > MAXFILELEN) {
154                                 fclose(fd);
155                                 if ((pid = fork()) < 0) {
156                                         return;
157                                 } else if (pid == 0) {
158                                         execl("/bin/rm", "rm", "-f", LOGFILE,
159                                               (char *)0);
160                                 }
161                         } else
162                                 fclose(fd);
163                 }
164         }
165 }
166
167 Evas_Object *phone_lock_create_win(const char *name)
168 {
169         Evas_Object *eo;
170         int w, h;
171
172         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
173         if (eo) {
174                 elm_win_title_set(eo, name);
175                 elm_win_borderless_set(eo, EINA_TRUE);
176                 evas_object_smart_callback_add(eo, "delete,request",
177                                                _phone_lock_win_del, NULL);
178                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
179                                         &w, &h);
180                 evas_object_resize(eo, w, h);
181         }
182
183         return eo;
184 }
185
186 int phone_lock_send_cmd(const char *cmd)
187 {
188         int fd;
189         int len;
190         int datalen;
191
192         fd = _phone_lock_create_sock();
193         if (fd < 0) {
194                 _DBG("phonelock create sock failed..!!");
195                 return -1;
196         }
197
198         if (cmd != NULL) {
199                 datalen = strlen(cmd);
200                 if ((len = send(fd, cmd, datalen, 0)) != datalen) {
201                         _DBG("send() failed %d %d, errno:%d", len, datalen,
202                              errno);
203                         close(fd);
204                         return -1;
205                 }
206         }
207         return 0;
208 }