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 "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");
65 PRT_TRACE("size = %d", (int)st.st_size);
67 cmdline = malloc(st.st_size + 1);
68 if (cmdline == NULL) {
69 PRT_TRACE_ERR("Not enough memory");
73 memset(cmdline, 0, st.st_size + 1);
75 read(fd, cmdline, st.st_size);
76 /* TODO - must change more smarter */
77 replace_char(st.st_size - 1, cmdline);
83 static void print_pmon_state(unsigned int dead_pid)
85 PRT_TRACE("[Process MON] %d killed", dead_pid);
88 static int pmon_process(int pid, void *ad)
92 char old_file[PATH_MAX];
96 if (sysconf_is_vip(pid)) {
97 PRT_TRACE_ERR("=======================================");
98 PRT_TRACE_ERR("[Process MON] VIP process dead.");
99 PRT_TRACE_ERR("=======================================");
101 /* If there is NOT a .hibernation_start file, run following codes
102 * On hibernation processing, just ignore relaunching */
103 else if (access("/tmp/.hibernation_start", R_OK) != 0) {
104 cmdline = pmon_get_permanent_pname(pid);
105 if (cmdline != NULL) {
106 PRT_TRACE("[Process MON] %s relaunch", cmdline);
107 new_pid = ss_launch_evenif_exist(cmdline, "");
112 char filepath[PATH_MAX];
115 if (access(PMON_PERMANENT_DIR, R_OK) < 0) {
116 PRT_TRACE("no predefined matrix dir = %s, so created", PMON_PERMANENT_DIR);
117 r = mkdir(PMON_PERMANENT_DIR, 0777);
119 PRT_TRACE("Make Directory is failed");
124 snprintf(filepath, sizeof(filepath), "%s/%d", PMON_PERMANENT_DIR, pid);
125 fd = open(filepath, O_RDONLY);
127 PRT_TRACE("Failed to open");
131 cnt = read(fd, buf, PATH_MAX);
135 PRT_TRACE("Failed to read");
139 snprintf(filepath, sizeof(filepath), "%s/%d", PMON_PERMANENT_DIR, new_pid);
141 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
143 PRT_TRACE("Failed to open");
147 if (write(fd, buf, cnt) == -1) {
148 PRT_TRACE("Failed to write");
154 if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_pnp(new_pid)) {
155 PRT_TRACE_ERR("Write new pid failed");
157 PRT_TRACE("[Process MON] %d ", new_pid);
162 ("[Process MON] OOMADJ_SET : pid %d, new_oomadj %d",
164 snprintf(buf, sizeof(buf), "/proc/%d/oom_adj",
166 fp = fopen(buf, "w");
169 fprintf(fp, "%d", (-17));
172 snprintf(old_file, sizeof(old_file), "%s/%d",
173 PMON_PERMANENT_DIR, pid);
176 PRT_TRACE_ERR("[Process MON] failed relaunching");
183 static unsigned int pmon_read(int fd)
186 read(fd, &pid, sizeof(pid));
191 static int pmon_cb(void *data, Ecore_Fd_Handler * fd_handler)
194 struct ss_main_data *ad = (struct ss_main_data *)data;
197 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
199 ("ecore_main_fd_handler_active_get error , return\n");
203 fd = ecore_main_fd_handler_fd_get(fd_handler);
206 PRT_TRACE_ERR("ecore_main_fd_handler_fd_get error , return");
209 if ((read(fd, &dead_pid, sizeof(dead_pid))) < 0) {
210 PRT_TRACE_ERR("Reading DEAD_PID failed, Return");
214 print_pmon_state(dead_pid);
215 pmon_process(dead_pid, ad);
220 int ss_pmon_init(struct ss_main_data *ad)
222 char pmon_dev_node[PATH_MAX];
224 if (0 > plugin_intf->OEM_sys_get_process_monitor_node(pmon_dev_node)) {
225 PRT_TRACE_ERR("ss_pmon_init get dev node path failed");
229 pmon_fd = open(pmon_dev_node, O_RDONLY);
231 PRT_TRACE_ERR("ss_pmon_init fd open failed");
234 ecore_main_fd_handler_add(pmon_fd, ECORE_FD_READ, pmon_cb, ad, NULL,