2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 "ss_launch.h"
19 #include <sys/types.h>
25 #define PMON_DEV_NODE "/dev/pmon"
26 #define PMON_PERMANENT_DIR "/tmp/permanent"
28 static int pmon_fd = -1;
30 static int replace_char(int size, char *t)
41 #define PERNAMENT_DIR "/tmp/permanent"
43 static char *pmon_get_permanent_pname(int pid)
50 snprintf(buf, sizeof(buf), "%s/%d", PERNAMENT_DIR, pid);
51 fd = open(buf, O_RDONLY);
53 PRT_TRACE_ERR("file open error");
57 if (fstat(fd, &st) < 0) {
58 PRT_TRACE_ERR("fstat error");
62 PRT_TRACE("size = %d", (int)st.st_size);
64 cmdline = malloc(st.st_size + 1);
65 if (cmdline == NULL) {
66 PRT_TRACE_ERR("Not enough memory");
69 memset(cmdline, 0, st.st_size + 1);
71 read(fd, cmdline, st.st_size);
72 /* TODO - must change more smarter */
73 replace_char(st.st_size - 1, cmdline);
79 static void print_pmon_state(unsigned int dead_pid)
81 PRT_TRACE("[Process MON] %d killed", dead_pid);
84 static int pmon_process(unsigned int pid, void *ad)
88 char old_file[PATH_MAX];
91 if (sysconf_is_vip(pid)) {
92 PRT_TRACE_ERR("=======================================");
93 PRT_TRACE_ERR("[Process MON] VIP process dead.");
94 PRT_TRACE_ERR("=======================================");
96 /* If there is NOT a .hibernation_start file, run following codes
97 * On hibernation processing, just ignore relaunching */
98 else if (access("/tmp/.hibernation_start", R_OK) != 0) {
99 cmdline = pmon_get_permanent_pname(pid);
100 if (cmdline != NULL) {
101 PRT_TRACE("[Process MON] %s relaunch", cmdline);
102 new_pid = ss_launch_evenif_exist(cmdline, "");
107 char filepath[PATH_MAX];
110 if (access("/tmp/permanent", R_OK) < 0) {
112 ("no predefined matrix dir = /tmp/permanent, so created");
113 mkdir("/tmp/permanent", 0777);
116 snprintf(filepath, sizeof(filepath),
117 "/tmp/permanent/%d", pid);
118 fd = open(filepath, O_RDONLY);
120 PRT_TRACE("Failed to open");
124 cnt = read(fd, buf, PATH_MAX);
128 PRT_TRACE("Failed to read");
132 snprintf(filepath, sizeof(filepath), "%s/%d",
133 "/tmp/permanent", new_pid);
135 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
137 PRT_TRACE("Failed to open");
141 if (write(fd, buf, cnt) == -1) {
142 PRT_TRACE("Failed to write");
148 fd = open("/sys/class/pmon/mp_pnp", O_WRONLY);
151 PRT_TRACE("Failed to open");
155 write(fd, &new_pid, sizeof(int));
158 PRT_TRACE("[Process MON] %d ", new_pid);
163 ("[Process MON] OOMADJ_SET : pid %d, new_oomadj %d",
165 snprintf(buf, sizeof(buf), "/proc/%d/oom_adj",
167 fp = fopen(buf, "w");
170 fprintf(fp, "%d", (-17));
173 snprintf(old_file, sizeof(old_file), "%s/%d",
174 PMON_PERMANENT_DIR, pid);
177 PRT_TRACE_ERR("[Process MON] failed relaunching");
184 static unsigned int pmon_read(int fd)
187 read(fd, &pid, sizeof(pid));
192 static int pmon_cb(void *data, Ecore_Fd_Handler * fd_handler)
195 struct ss_main_data *ad = (struct ss_main_data *)data;
198 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
200 ("ecore_main_fd_handler_active_get error , return\n");
204 fd = ecore_main_fd_handler_fd_get(fd_handler);
206 if ((read(fd, &dead_pid, sizeof(dead_pid))) < 0) {
207 PRT_TRACE_ERR("Reading DEAD_PID failed, Return");
211 print_pmon_state(dead_pid);
212 pmon_process(dead_pid, ad);
217 int ss_pmon_init(struct ss_main_data *ad)
219 pmon_fd = open(PMON_DEV_NODE, O_RDONLY);
221 PRT_TRACE_ERR("ss_pmon_init fd open failed");
224 ecore_main_fd_handler_add(pmon_fd, ECORE_FD_READ, pmon_cb, ad, NULL,