2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
22 #include <sys/types.h>
25 #include "device-node.h"
27 #include "ss_launch.h"
28 #include "include/ss_data.h"
29 #include "ss_common.h"
31 #define PMON_PERMANENT_DIR "/tmp/permanent"
33 static Ecore_Fd_Handler *pmon_efd = NULL;
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)
48 static char *pmon_get_permanent_pname(int pid)
55 snprintf(buf, sizeof(buf), "%s/%d", PMON_PERMANENT_DIR, pid);
56 fd = open(buf, O_RDONLY);
58 PRT_TRACE_ERR("file open error");
62 if (fstat(fd, &st) < 0) {
63 PRT_TRACE_ERR("fstat error");
67 PRT_TRACE("size = %d", (int)st.st_size);
69 cmdline = malloc(st.st_size + 1);
70 if (cmdline == NULL) {
71 PRT_TRACE_ERR("Not enough memory");
75 memset(cmdline, 0, st.st_size + 1);
77 read(fd, cmdline, st.st_size);
78 /* TODO - must change more smarter */
79 replace_char(st.st_size - 1, cmdline);
85 static void print_pmon_state(unsigned int dead_pid)
87 PRT_TRACE("[Process MON] %d killed", dead_pid);
90 static int pmon_process(int pid, void *ad)
94 char old_file[PATH_MAX];
98 if (sysconf_is_vip(pid)) {
99 PRT_TRACE_ERR("=======================================");
100 PRT_TRACE_ERR("[Process MON] VIP process dead.");
101 PRT_TRACE_ERR("=======================================");
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, "");
114 char filepath[PATH_MAX];
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);
121 PRT_TRACE("Make Directory is failed");
126 snprintf(filepath, sizeof(filepath), "%s/%d", PMON_PERMANENT_DIR, pid);
127 fd = open(filepath, O_RDONLY);
129 PRT_TRACE("Failed to open");
132 cnt = read(fd, buf, PATH_MAX);
136 PRT_TRACE("Failed to read");
140 snprintf(filepath, sizeof(filepath), "%s/%d", PMON_PERMANENT_DIR, new_pid);
142 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
144 PRT_TRACE("Failed to open");
147 if (write(fd, buf, cnt) == -1) {
148 PRT_TRACE("Failed to write");
153 if ( device_set_property(DEVICE_TYPE_PROCESS, PROP_PROCESS_MP_PNP, new_pid) < 0) {
154 PRT_TRACE_ERR("Write new pid failed");
156 PRT_TRACE("[Process MON] %d ", new_pid);
161 ("[Process MON] OOMADJ_SET : pid %d, new_oomadj %d",
164 fp = open_proc_oom_adj_file(new_pid, "w");
167 fprintf(fp, "%d", (-17));
170 snprintf(old_file, sizeof(old_file), "%s/%d",
171 PMON_PERMANENT_DIR, pid);
174 PRT_TRACE_ERR("[Process MON] failed relaunching");
181 static unsigned int pmon_read(int fd)
184 read(fd, &pid, sizeof(pid));
189 static int pmon_cb(void *data, Ecore_Fd_Handler * fd_handler)
192 struct ss_main_data *ad = (struct ss_main_data *)data;
194 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
196 ("ecore_main_fd_handler_active_get error , return\n");
200 fd = ecore_main_fd_handler_fd_get(fd_handler);
203 PRT_TRACE_ERR("ecore_main_fd_handler_fd_get error , return");
206 if (read(fd, &dead_pid, sizeof(dead_pid)) < 0) {
208 PRT_TRACE_ERR("Reading DEAD_PID failed, restart ecore fd");
213 print_pmon_state(dead_pid);
214 pmon_process(dead_pid, ad);
219 int ss_pmon_init(struct ss_main_data *ad)
223 ecore_main_fd_handler_del(pmon_efd);
226 if (__pmon_start(ad) == -1) {
227 PRT_TRACE_ERR("fail pmon control fd init");
233 static int __pmon_start(struct ss_main_data *ad)
236 char pmon_dev_node[PATH_MAX];
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");
243 pmon_fd = open(pmon_dev_node, O_RDONLY);
245 PRT_TRACE_ERR("ss_pmon_init fd open failed");
248 pmon_efd = ecore_main_fd_handler_add(pmon_fd, ECORE_FD_READ, pmon_cb, ad, NULL, NULL);
250 PRT_TRACE_ERR("error ecore_main_fd_handler_add");
255 static int __pmon_stop(int fd)
258 ecore_main_fd_handler_del(pmon_efd);