Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / common / proc-common.h
1 /*
2  * resourced
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /**
21  * @file proc-common.h
22  * @desc proc common process
23  **/
24
25 #ifndef __PROC_COMMON_H__
26 #define __PROC_COMMON_H__
27
28 #include <unistd.h>
29 #include <glib.h>
30 #include <string.h>
31
32 #include "resourced.h"
33 #include "const.h"
34 #include "memory-cgroup.h"
35 #include "cpu-sched-common.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif /* __cplusplus */
40
41 #define RUNTIME_APP_INFO_DIR RUNTIME_RESOURCED_DIR"/appinfo"
42
43 #define GPOINTER_TO_PID(x) (pid_t)GPOINTER_TO_INT((x))
44 #define PID_TO_GPOINTER(x) GINT_TO_POINTER((pid_t)(x))
45
46 typedef GSList *pid_list;
47
48 enum proc_action {
49         PROC_ACTION_KILL = 1,
50         PROC_ACTION_RECLAIM = 2,
51         PROC_ACTION_BROADCAST = 4,
52
53         PROC_ACTION_IGNORE = 8,
54
55         PROC_ACTION_REBOOT = 16,
56 };
57
58 enum proc_type {
59         APP_TYPE,
60         SERVICE_TYPE,
61 };
62
63 struct proc_conf_info {
64         char name[MAX_NAME_LENGTH];
65         enum cgroup_type mem_type;                      /* fixed memory cgroup */
66         enum cgroup_type cpu_type;                      /* fixed cpu cgroup */
67         enum proc_action watchdog_action;       /* watchdog action */
68         enum proc_action fail_action;           /* release action */
69         struct mem_action mem_action;
70         struct cpu_sched_info cpu_sched_info;
71         pid_t pid;
72 };
73
74 enum application_type {
75         PROC_TYPE_NONE,
76         PROC_TYPE_READY,
77         PROC_TYPE_GUI,
78         PROC_TYPE_SERVICE,
79         PROC_TYPE_GROUP,
80         PROC_TYPE_WATCH,
81         PROC_TYPE_WIDGET,
82         PROC_TYPE_MAX,
83 };
84
85 enum proc_state {
86         PROC_STATE_DEFAULT,
87         PROC_STATE_FOREGROUND,
88         PROC_STATE_BACKGROUND,
89         PROC_STATE_SUSPEND_READY,
90         PROC_STATE_SUSPEND,
91 };
92
93 struct proc_status {
94         pid_t pid;
95         union {
96                 struct proc_app_info *pai;
97                 struct proc_conf_info *pci;
98         };
99 };
100
101 struct proc_limit_status {
102         struct proc_status ps;
103         unsigned long limit_bytes;
104         enum proc_action action;
105 };
106
107 enum proc_exclude_type {
108         PROC_INCLUDE,
109         PROC_EXCLUDE,
110 };
111
112 enum {
113         LCD_STATE_ON,
114         LCD_STATE_OFF,
115 };
116
117 enum proc_prelaunch_flags {
118         PROC_NONE       = 0x00u,
119         PROC_LARGEMEMORY        = 0x01u,        /* for mark large memory */
120         PROC_SIGTERM    = 0x02u,        /* for make killer kill victim by SIGTERM */
121         PROC_WEBAPP     = 0x04u,        /* for checking webapp */
122         PROC_DOWNLOADAPP = 0x08u,       /* for monitoring disk usage about downloadable app */
123         PROC_SERVICEAPP = 0x10u,        /* for distinguishing service app and ui app */
124         PROC_CGROUP_HIGH_ATTRIBUTE = 0x20u, /* for providing High level about MDM app */
125         PROC_BGALLOW = 0x100u,  /* for allowing background application */
126         PROC_BGCTRL_PLATFORM = 0x200u,  /* for controlling background application by appfw */
127         PROC_BGCTRL_APP = 0x400u,       /* for checking old version application */
128 };
129
130 /*
131  * BG categories in TIZEN 2.4
132  * Media : Playing audio, recoding, and streaming  video in background
133  * Download : Downloading data with the Tizen download-manager API
134  * Background network : Processing general network operation
135  *         in background (Sync-manager, IM / VOIP)
136  * Location: Processing location data in background
137  * Sensor : Processing context data from the sensors
138  *         such as gesture, health (product)
139  * IOT communition & connectivity : Communicating between external
140  *        devices in background such as WIFI, BT, SAP (only product)
141  * System : hidden category for system applications like home
142  */
143 enum proc_background_category {
144         PROC_BG_NONE            = 0x0,
145         PROC_BG_MEDIA           = 0x1,
146         PROC_BG_DOWNLOAD        = 0x2,
147         PROC_BG_NETWORK         = 0x4,
148         PROC_BG_LOCATION        = 0x8,
149         PROC_BG_SENSOR          = 0x10,
150         PROC_BG_IOT             = 0x20,
151         PROC_BG_SYSTEM          = 0x40,
152 };
153
154 enum proc_lru_state {
155         PROC_FOREGROUND = -1,
156         PROC_ACTIVE = 0,
157         PROC_BACKGROUND = 1,
158         PROC_LRU_MAX    = 15,
159         PROC_FAVORITE = 16,
160         PROC_FAVORITE_LRU_MAX = 35,
161 };
162
163
164 enum cgroup_cmd_type { /** cgroup command type **/
165         PROC_CGROUP_SET_FOREGRD,
166         PROC_CGROUP_SET_ACTIVE,
167         PROC_CGROUP_SET_BACKGRD,
168         PROC_CGROUP_SET_INACTIVE,
169         PROC_CGROUP_SET_LAUNCH_REQUEST,
170         PROC_CGROUP_SET_RESUME_REQUEST,
171         PROC_CGROUP_SET_TERMINATE_REQUEST,
172         PROC_CGROUP_SET_SERVICE_REQUEST,
173         PROC_CGROUP_SET_NOTI_REQUEST,
174         PROC_CGROUP_SET_PROC_EXCLUDE_REQUEST,
175         PROC_CGROUP_GET_MEMSWEEP,
176         PROC_CGROUP_SET_TERMINATED,
177         PROC_CGROUP_SET_SYSTEM_SERVICE,
178         PROC_CGROUP_GET_CMDLINE,
179         PROC_CGROUP_GET_EXE,
180         PROC_CGROUP_GET_STAT,
181         PROC_CGROUP_GET_STATUS,
182         PROC_CGROUP_GET_OOMSCORE,
183         PROC_CGROUP_GET_PGID_CMDLINE,
184 };
185
186 struct proc_exclude {
187         pid_t pid;
188         enum proc_exclude_type type;
189 };
190
191 struct proc_program_info {
192         char *pkgname;
193         GSList *app_list;
194         GSList *svc_list;
195 };
196
197 struct proc_memory_state {
198         struct memcg_info *memcg_info;
199         int memcg_idx;
200         int oom_score_adj;
201         bool oom_killed;
202         bool use_mem_limit;
203         bool memlimit_update_exclude;
204 };
205
206 struct proc_app_info {
207         char *appid;
208         pid_t main_pid;
209         pid_list childs;
210         bool app_watchdog_exclude;
211         bool app_memcg_update_exclude;
212         bool app_cpucg_update_exclude;
213         bool app_cpu_nice_update_exclude;
214         int runtime_exclude;
215         int flags;
216         int lru_state;
217         int categories;
218         enum proc_state state;
219         enum application_type type;
220         struct resourced_appinfo *ai;
221         struct proc_program_info *program;
222         struct proc_memory_state memory;
223         unsigned long starttime;
224
225         unsigned long last_thaw_time; // in seconds since boot
226         bool dont_freeze;
227 };
228
229 //int proc_priority_set_fixed(void *data);
230 int proc_get_freezer_status(void);
231
232 struct proc_app_info *find_app_info(const pid_t pid);
233 struct proc_app_info *find_app_info_by_appid(const char *appid);
234
235 int proc_get_id_info(struct proc_status *ps, char **app_name, char **pkg_name);
236
237 /**
238  * @desc set memory field in proc_app_info sturcture of selected pai, the data
239  * are used by lowmem module.
240  * @return void
241  */
242 void proc_set_process_memory_state(struct proc_app_info *pai,
243                 int memcg_idx, struct memcg_info *memcg_info, int oom_score_adj);
244
245 int proc_get_appflag(const pid_t pid);
246
247 static inline int equal_name_info(const char *id_a, const char *id_b)
248 {
249         return !strncmp(id_a, id_b, strlen(id_b)+1);
250 }
251
252 int proc_get_svc_state(struct proc_program_info *ppi);
253
254 bool proc_check_lru_suspend(int val, int lru);
255
256 enum proc_state proc_check_suspend_state(struct proc_app_info *pai);
257
258 GSList *proc_app_list_open(void);
259 void proc_app_list_close(void);
260
261 #define PAL_INIT_VALUE (GSList*)-1
262
263 static inline void cleanup_proc_app_list_close_func(GSList **l)
264 {
265         if (*l != PAL_INIT_VALUE)
266                 proc_app_list_close();
267 }
268
269 #define _cleanup_app_list_close_ __attribute__((cleanup(cleanup_proc_app_list_close_func)))
270
271 /**
272  * @desc  Check whether this process is OOM-fixed app or not
273  * @param[in] pid  Process ID
274  * @return  1 if it is OOM-fixed app. Else, 0
275  */
276 //int proc_priority_is_oom_fixed_process(int pid);
277
278 /**
279  * @desc  Check whether this process is cpu cgroup fixed app or not
280  * @param[in] pid  Process ID
281  * @return  1 if it is cpu cgroup fixed app. Else, 0
282  */
283 //int proc_priority_is_cpu_cgroup_fixed_process(int pid);
284
285 void fixed_app_and_service_list_init(void);
286 void fixed_app_and_service_list_exit(void);
287 void fixed_app_list_insert(struct proc_conf_info *pci);
288 void fixed_service_list_insert(struct proc_conf_info *pci);
289 void fixed_limit_pid_list_insert(pid_t pid, void *value);
290 void fixed_limit_pid_list_remove(pid_t pid);
291 GHashTable* fixed_app_list_get(void);
292 GHashTable * fixed_service_list_get(void);
293 struct proc_conf_info* fixed_app_and_service_exist_check(const char *name, enum proc_type proc_type);
294 enum proc_action fixed_app_and_service_watchdog_action(const char *name, enum proc_type proc_type);
295
296 #ifdef __cplusplus
297 }
298 #endif /* __cplusplus */
299
300 #endif /* __PROC_COMMON_H__ */