2 * Copyright (c) 2016 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.
17 #include "stc-monitor.h"
18 #include "stc-monitor-proc.h"
19 #include "stc-default-connection.h"
20 #include "helper-net-cls.h"
22 static void __print_proc(gpointer key, gpointer value,
25 stc_proc_value_s *proc_value = (stc_proc_value_s *)value;
27 STC_LOGD("pid[\033[1;33m%d\033[0;m] ground[%d]",
28 proc_value->pid, proc_value->ground);
31 static void __print_proc_all(GHashTable *procs)
33 g_hash_table_foreach(procs, __print_proc, NULL);
36 static void __proc_remove_pid(gpointer key, gpointer value,
39 stc_app_value_s *app_value = (stc_app_value_s *)value;
40 remove_pid_context_s *context = (remove_pid_context_s *)data;
43 if (!g_hash_table_remove(app_value->processes, GUINT_TO_POINTER(context->pid)))
47 __print_proc_all(app_value->processes);
48 STC_LOGD("\033[1;31mProcess removed\033[0;m "
49 "[\033[1;33m%d\033[0;m]", context->pid);
52 context->entry_removed = TRUE;
53 context->app_value = app_value;
56 API stc_error_e stc_monitor_proc_update_ground(uint32_t classid,
58 const stc_proc_value_s value)
60 stc_error_e ret = STC_ERROR_NONE;
61 stc_app_value_s *app_lookup;
62 stc_proc_value_s *proc_lookup;
63 GHashTable *apps = stc_monitor_get_system_apps();
66 return STC_ERROR_NO_DATA;
68 if (classid == STC_UNKNOWN_CLASSID)
69 classid = get_classid_by_app_id(app_id, TRUE);
71 app_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
74 STC_LOGD("Application not found [\033[1;36m%d\033[0;m]", classid);
75 return STC_ERROR_NO_DATA;
78 proc_lookup = g_hash_table_lookup(app_lookup->processes,
79 GUINT_TO_POINTER(value.pid));
82 STC_LOGD("Process not found [\033[1;33m%d\033[0;m]", value.pid);
83 return STC_ERROR_NO_DATA;
86 if (proc_lookup->ground != value.ground)
87 proc_lookup->ground = value.ground;
89 place_pids_to_net_cgroup(value.pid, app_id);
94 API stc_error_e stc_monitor_proc_add(uint32_t classid,
96 const stc_proc_value_s value)
98 stc_error_e ret = STC_ERROR_NONE;
99 stc_app_value_s *app_lookup;
100 stc_proc_value_s *proc_lookup;
101 stc_proc_value_s *proc_value;
102 GHashTable *apps = stc_monitor_get_system_apps();
105 return STC_ERROR_NO_DATA;
107 if (classid == STC_UNKNOWN_CLASSID)
108 classid = get_classid_by_app_id(app_id, TRUE);
110 app_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
113 STC_LOGD("Application not found [\033[1;36m%d\033[0;m]", classid);
114 return STC_ERROR_NO_DATA;
117 proc_lookup = g_hash_table_lookup(app_lookup->processes,
118 GUINT_TO_POINTER(value.pid));
121 STC_LOGE("Already exists [\033[1;36m%d\033[0;m]", value.pid);
122 return STC_ERROR_NONE;
125 proc_value = MALLOC0(stc_proc_value_s, 1);
128 STC_LOGE("Value allocation failed");
129 return STC_ERROR_OUT_OF_MEMORY;
132 proc_value->pid = value.pid;
133 proc_value->ground = value.ground;
135 g_hash_table_insert(app_lookup->processes,
136 GUINT_TO_POINTER(proc_value->pid),
139 /* add pid to application cgroup */
140 place_pids_to_net_cgroup(proc_value->pid, app_id);
143 __print_proc_all(app_lookup->processes);
144 STC_LOGD("\033[1;32mProcess added\033[0;m "
145 "[\033[1;33m%d\033[0;m]", proc_value->pid);
151 API stc_error_e stc_monitor_proc_remove(uint32_t classid,
154 stc_error_e ret = STC_ERROR_NONE;
155 stc_app_value_s *app_lookup = NULL;
157 GHashTable *apps = stc_monitor_get_system_apps();
158 remove_pid_context_s context = {
161 .entry_removed = FALSE,
165 return STC_ERROR_NO_DATA;
167 if (classid == STC_UNKNOWN_CLASSID) {
168 g_hash_table_foreach(apps, __proc_remove_pid, &context);
170 if (context.entry_removed)
171 app_lookup = context.app_value;
175 STC_LOGD("Process not found [\033[1;33m%d\033[0;m]", pid);
176 return STC_ERROR_NO_DATA;
179 app_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
182 STC_LOGD("Application not found [\033[1;36m%d\033[0;m]",
184 return STC_ERROR_NO_DATA;
187 if (g_hash_table_remove(app_lookup->processes, GUINT_TO_POINTER(pid))) {
189 __print_proc_all(app_lookup->processes);
190 STC_LOGD("\033[1;31mProcess removed\033[0;m "
191 "[\033[1;33m%d\033[0;m]", pid);
194 STC_LOGD("Process not found [\033[1;33m%d\033[0;m]", pid);
198 pid_count = g_hash_table_size(app_lookup->processes);
201 /* remove nfacct rule for this classid */
202 stc_monitor_app_remove_monitor(GUINT_TO_POINTER(classid),
203 app_lookup, stc_get_default_connection());
204 stc_monitor_rstn_remove_for_app(classid);
206 g_hash_table_remove(apps, GUINT_TO_POINTER(classid));
209 STC_LOGD("\033[1;31mApplication removed\033[0;m "
210 "[\033[1;36m%d\033[0;m]", classid);