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.
22 #include <sys/types.h>
25 #include "ss_device_plugin.h"
27 #include "ss_launch.h"
28 #include "include/ss_data.h"
30 #define PMON_PERMANENT_DIR "/tmp/permanent"
32 static int pmon_fd = -1;
34 static int replace_char(int size, char *t)
45 static char *pmon_get_permanent_pname(int pid)
52 snprintf(buf, sizeof(buf), "%s/%d", PMON_PERMANENT_DIR, pid);
53 fd = open(buf, O_RDONLY);
55 PRT_TRACE_ERR("file open error");
59 if (fstat(fd, &st) < 0) {
60 PRT_TRACE_ERR("fstat error");
64 PRT_TRACE("size = %d", (int)st.st_size);
66 cmdline = malloc(st.st_size + 1);
67 if (cmdline == NULL) {
68 PRT_TRACE_ERR("Not enough memory");
71 memset(cmdline, 0, st.st_size + 1);
73 read(fd, cmdline, st.st_size);
74 /* TODO - must change more smarter */
75 replace_char(st.st_size - 1, cmdline);
81 static void print_pmon_state(unsigned int dead_pid)
83 PRT_TRACE("[Process MON] %d killed", dead_pid);
86 static int pmon_process(unsigned int pid, void *ad)
90 char old_file[PATH_MAX];
93 if (sysconf_is_vip(pid)) {
94 PRT_TRACE_ERR("=======================================");
95 PRT_TRACE_ERR("[Process MON] VIP process dead.");
96 PRT_TRACE_ERR("=======================================");
98 /* If there is NOT a .hibernation_start file, run following codes
99 * On hibernation processing, just ignore relaunching */
100 else if (access("/tmp/.hibernation_start", R_OK) != 0) {
101 cmdline = pmon_get_permanent_pname(pid);
102 if (cmdline != NULL) {
103 PRT_TRACE("[Process MON] %s relaunch", cmdline);
104 new_pid = ss_launch_evenif_exist(cmdline, "");
109 char filepath[PATH_MAX];
112 if (access(PMON_PERMANENT_DIR, R_OK) < 0) {
113 PRT_TRACE("no predefined matrix dir = %s, so created", PMON_PERMANENT_DIR);
114 mkdir(PMON_PERMANENT_DIR, 0777);
117 snprintf(filepath, sizeof(filepath), "%s/%d", PMON_PERMANENT_DIR, 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", PMON_PERMANENT_DIR, new_pid);
134 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
136 PRT_TRACE("Failed to open");
140 if (write(fd, buf, cnt) == -1) {
141 PRT_TRACE("Failed to write");
147 if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_pnp(new_pid)) {
148 PRT_TRACE_ERR("Write new pid failed");
150 PRT_TRACE("[Process MON] %d ", new_pid);
155 ("[Process MON] OOMADJ_SET : pid %d, new_oomadj %d",
157 snprintf(buf, sizeof(buf), "/proc/%d/oom_adj",
159 fp = fopen(buf, "w");
162 fprintf(fp, "%d", (-17));
165 snprintf(old_file, sizeof(old_file), "%s/%d",
166 PMON_PERMANENT_DIR, pid);
169 PRT_TRACE_ERR("[Process MON] failed relaunching");
176 static unsigned int pmon_read(int fd)
179 read(fd, &pid, sizeof(pid));
184 static int pmon_cb(void *data, Ecore_Fd_Handler * fd_handler)
187 struct ss_main_data *ad = (struct ss_main_data *)data;
190 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
192 ("ecore_main_fd_handler_active_get error , return\n");
196 fd = ecore_main_fd_handler_fd_get(fd_handler);
198 if ((read(fd, &dead_pid, sizeof(dead_pid))) < 0) {
199 PRT_TRACE_ERR("Reading DEAD_PID failed, Return");
203 print_pmon_state(dead_pid);
204 pmon_process(dead_pid, ad);
209 int ss_pmon_init(struct ss_main_data *ad)
211 char pmon_dev_node[PATH_MAX];
213 if (0 > plugin_intf->OEM_sys_get_process_monitor_node(pmon_dev_node)) {
214 PRT_TRACE_ERR("ss_pmon_init get dev node path failed");
218 pmon_fd = open(pmon_dev_node, O_RDONLY);
220 PRT_TRACE_ERR("ss_pmon_init fd open failed");
223 ecore_main_fd_handler_add(pmon_fd, ECORE_FD_READ, pmon_cb, ad, NULL,