change device node control api from plugin to device-node
[framework/system/system-server.git] / ss_pmon_handler.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <sysman.h>
19 #include <fcntl.h>
20 #include <assert.h>
21 #include <limits.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #include "device-node.h"
26 #include "ss_log.h"
27 #include "ss_launch.h"
28 #include "include/ss_data.h"
29 #include "ss_common.h"
30
31 #define PMON_PERMANENT_DIR      "/tmp/permanent"
32
33 static Ecore_Fd_Handler *pmon_efd = NULL;
34
35 static int __pmon_start(struct ss_main_data *ad);
36 static int __pmon_stop(int fd);
37 static int replace_char(int size, char *t)
38 {
39         while (size > 0) {
40                 if (*t == 0)
41                         *t = ' ';
42                 size--;
43                 t++;
44         }
45         return 0;
46 }
47
48 static char *pmon_get_permanent_pname(int pid)
49 {
50         int fd;
51         char buf[PATH_MAX];
52         struct stat st;
53         char *cmdline = NULL;
54
55         snprintf(buf, sizeof(buf), "%s/%d", PMON_PERMANENT_DIR, pid);
56         fd = open(buf, O_RDONLY);
57         if (fd == -1) {
58                 PRT_TRACE_ERR("file open error");
59                 return NULL;
60         }
61
62         if (fstat(fd, &st) < 0) {
63                 PRT_TRACE_ERR("fstat error");
64                 close(fd);
65                 return NULL;
66         }
67         PRT_TRACE("size = %d", (int)st.st_size);
68
69         cmdline = malloc(st.st_size + 1);
70         if (cmdline == NULL) {
71                 PRT_TRACE_ERR("Not enough memory");
72                 close(fd);
73                 return NULL;
74         }
75         memset(cmdline, 0, st.st_size + 1);
76
77         read(fd, cmdline, st.st_size);
78         /* TODO - must change more smarter */
79         replace_char(st.st_size - 1, cmdline);
80         close(fd);
81
82         return cmdline;
83 }
84
85 static void print_pmon_state(unsigned int dead_pid)
86 {
87         PRT_TRACE("[Process MON] %d killed", dead_pid);
88 }
89
90 static int pmon_process(int pid, void *ad)
91 {
92         char *cmdline;
93         int new_pid;
94         char old_file[PATH_MAX];
95         int fd;
96         int r;
97
98         if (sysconf_is_vip(pid)) {
99                 PRT_TRACE_ERR("=======================================");
100                 PRT_TRACE_ERR("[Process MON] VIP process dead.");
101                 PRT_TRACE_ERR("=======================================");
102         }
103         /* If there is NOT a .hibernation_start file, run following codes 
104          * On hibernation processing, just ignore relaunching */
105         else if (access("/tmp/.hibernation_start", R_OK) != 0) {
106                 cmdline = pmon_get_permanent_pname(pid);
107                 if (cmdline != NULL) {
108                         PRT_TRACE("[Process MON] %s relaunch", cmdline);
109                         new_pid = ss_launch_evenif_exist(cmdline, "");
110                         free(cmdline);
111                         if (new_pid > 0) {
112                                 /* TODO - set oom */
113                                 char buf[PATH_MAX];
114                                 char filepath[PATH_MAX];
115                                 int cnt;
116
117                                 if (access(PMON_PERMANENT_DIR, R_OK) < 0) {
118                                         PRT_TRACE("no predefined matrix dir = %s, so created", PMON_PERMANENT_DIR);
119                                         r = mkdir(PMON_PERMANENT_DIR, 0777);
120                                         if(r < 0) {
121                                                 PRT_TRACE("Make Directory is failed");
122                                                 return -1;
123                                         }
124                                 }
125
126                                 snprintf(filepath, sizeof(filepath), "%s/%d", PMON_PERMANENT_DIR, pid);
127                                 fd = open(filepath, O_RDONLY);
128                                 if (fd == -1) {
129                                         PRT_TRACE("Failed to open");
130                                         return -1;
131                                 }
132                                 cnt = read(fd, buf, PATH_MAX);
133                                 close(fd);
134
135                                 if (cnt <= 0) {
136                                         PRT_TRACE("Failed to read");
137                                         return -1;
138                                 }
139
140                                 snprintf(filepath, sizeof(filepath), "%s/%d", PMON_PERMANENT_DIR, new_pid);
141
142                                 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
143                                 if (fd == -1) {
144                                         PRT_TRACE("Failed to open");
145                                         return -1;
146                                 }
147                                 if (write(fd, buf, cnt) == -1) {
148                                         PRT_TRACE("Failed to write");
149                                         close(fd);
150                                         return -1;
151                                 }
152                                 close(fd);
153                                 if ( device_set_property(DEVICE_TYPE_PROCESS, PROP_PROCESS_MP_PNP, new_pid) < 0) {
154                                         PRT_TRACE_ERR("Write new pid failed");
155                                 }
156                                 PRT_TRACE("[Process MON] %d ", new_pid);
157
158                                 FILE *fp;
159
160                                 PRT_TRACE
161                                     ("[Process MON] OOMADJ_SET : pid %d, new_oomadj %d",
162                                      new_pid, (-17));
163                                 
164                                 fp = open_proc_oom_adj_file(new_pid, "w");
165                                 if (fp == NULL)
166                                         return -1;
167                                 fprintf(fp, "%d", (-17));
168                                 fclose(fp);
169
170                                 snprintf(old_file, sizeof(old_file), "%s/%d",
171                                          PMON_PERMANENT_DIR, pid);
172                                 unlink(old_file);
173                         } else { 
174                                 PRT_TRACE_ERR("[Process MON] failed relaunching");
175                         }
176                 }
177         }
178         return 0;
179 }
180 /*
181 static unsigned int pmon_read(int fd)
182 {
183         unsigned int pid;
184         read(fd, &pid, sizeof(pid));
185         return pid;
186 }
187 */
188
189 static int pmon_cb(void *data, Ecore_Fd_Handler * fd_handler)
190 {
191         int fd;
192         struct ss_main_data *ad = (struct ss_main_data *)data;
193         int dead_pid;
194         if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
195                 PRT_TRACE_ERR
196                     ("ecore_main_fd_handler_active_get error , return\n");
197                 return -1;
198         }
199
200         fd = ecore_main_fd_handler_fd_get(fd_handler);
201
202         if (fd < 0) {
203                 PRT_TRACE_ERR("ecore_main_fd_handler_fd_get error , return");
204                 return -1;
205         }
206         if (read(fd, &dead_pid, sizeof(dead_pid)) < 0) {
207                 __pmon_stop(fd);
208                 PRT_TRACE_ERR("Reading DEAD_PID failed, restart ecore fd");
209                 __pmon_start(ad);
210                 return -1;
211         }
212                 
213         print_pmon_state(dead_pid);
214         pmon_process(dead_pid, ad);
215
216         return 1;
217 }
218
219 int ss_pmon_init(struct ss_main_data *ad)
220 {
221         int ret = -1;
222         if (pmon_efd) {
223                 ecore_main_fd_handler_del(pmon_efd);
224                 pmon_efd = NULL;
225         }
226         if (__pmon_start(ad) == -1) {
227                 PRT_TRACE_ERR("fail pmon control fd init");
228                 return -1;
229         }
230         return 0;
231 }
232
233 static int __pmon_start(struct ss_main_data *ad)
234 {
235         int pmon_fd = -1;
236         char pmon_dev_node[PATH_MAX];
237
238         if (device_get_property(DEVICE_TYPE_PROCESS, PROP_PROCESS_NODE, pmon_dev_node) < 0) {
239                 PRT_TRACE_ERR("ss_pmon_init get dev node path failed");
240                 return -1;
241         }
242
243         pmon_fd = open(pmon_dev_node, O_RDONLY);
244         if (pmon_fd < 0) {
245                 PRT_TRACE_ERR("ss_pmon_init fd open failed");
246                 return -1;
247         }
248         pmon_efd = ecore_main_fd_handler_add(pmon_fd, ECORE_FD_READ, pmon_cb, ad, NULL, NULL);
249         if (!pmon_efd) {
250                 PRT_TRACE_ERR("error ecore_main_fd_handler_add");
251                 return -1;
252         }
253         return 0;
254 }
255 static int __pmon_stop(int fd)
256 {
257         if (pmon_efd) {
258                 ecore_main_fd_handler_del(pmon_efd);
259                 pmon_efd = NULL;
260         }
261         if (fd >=0) {
262                 close(fd);
263                 fd = -1;
264         }
265         return 0;
266 }