2 * Copyright (c) 2017 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.
24 #include <sys/socket.h>
25 #include <linux/netlink.h>
26 #include <linux/connector.h>
27 #include <linux/cn_proc.h>
30 #include "stc-plugin-procfs.h"
31 #include "stc-monitor.h"
32 #include "helper-net-cls.h"
33 #include "helper-procfs.h"
41 char cmdline[PROC_NAME_MAX];
42 char status[PROC_STATUS_CNT][PROC_BUF_MAX];
45 typedef struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
46 struct nlmsghdr nl_hdr;
47 struct __attribute__ ((__packed__)) {
49 enum proc_cn_mcast_op cn_mcast;
53 typedef struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
54 struct nlmsghdr nl_hdr;
55 struct __attribute__ ((__packed__)) {
57 struct proc_event proc_ev;
59 } nl_connector_proc_event_s;
61 static int nl_connector_sock = -1;
62 static guint nl_connector_gsource_id = 0;
63 static GTree *proc_tree;
66 static gboolean __process_nl_connector_message(GIOChannel *source,
67 GIOCondition condition,
70 static int __proc_tree_key_compare(gconstpointer a, gconstpointer b,
71 gpointer UNUSED user_data)
73 proc_key_s *key_a = (proc_key_s *)a;
74 proc_key_s *key_b = (proc_key_s *)b;
76 return key_a->pid - key_b->pid;
79 static void __proc_tree_value_free(gpointer data)
81 proc_value_s *value = (proc_value_s *)data;
86 static void __proc_tree_key_free(gpointer data)
88 proc_key_s *key = (proc_key_s *)data;
93 static proc_value_s * __proc_tree_lookup(const proc_key_s *key)
97 if (proc_tree == NULL) {
98 STC_LOGE("tree is null");
102 lookup = g_tree_lookup(proc_tree, key);
107 static gboolean __proc_tree_foreach_print(gpointer key, gpointer value,
110 proc_key_s *proc_key = (proc_key_s *)key;
111 proc_value_s *proc_value = (proc_value_s *)value;
113 STC_LOGD("Proc pid [\033[1;33m%d\033[0;m] ppid [%s] "
114 "cmdline [\033[0;34m%s\033[0;m]", proc_key->pid,
115 proc_value->status[PROC_STATUS_PPID], proc_value->cmdline);
120 static void __proc_tree_printall(void)
122 g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL);
126 static proc_value_s * __proc_tree_find_parent(proc_value_s *value)
128 proc_value_s *parent = NULL;
129 proc_value_s *lookup = value;
133 key.pid = atoi(lookup->status[PROC_STATUS_PPID]);
134 lookup = __proc_tree_lookup(&key);
141 STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] "
142 "pid[%s] ppid[%s] cmdline[\033[0;34m%s\033[0;m] name[%s]",
143 parent->status[PROC_STATUS_TGID], parent->status[PROC_STATUS_PID],
144 parent->status[PROC_STATUS_PPID], parent->cmdline,
145 parent->status[PROC_STATUS_NAME]);
151 static void __proc_tree_add(proc_key_s *key, proc_value_s *value)
153 proc_value_s *lookup;
154 proc_value_s *parent;
156 if (proc_tree == NULL) {
157 STC_LOGE("tree is null");
161 if (key == NULL || value == NULL) {
163 STC_LOGE("invalid parameters");
167 lookup = g_tree_lookup(proc_tree, key);
171 proc_key_s *proc_key = MALLOC0(proc_key_s, 1);
172 if (proc_key == NULL) {
173 STC_LOGE("memory allocation failed");
177 proc_value_s *proc_value = MALLOC0(proc_value_s, 1);
178 if (proc_value == NULL) {
179 STC_LOGE("memory allocation failed");
184 memcpy(proc_key, key, sizeof(proc_key_s));
185 memcpy(proc_value, value, sizeof(proc_value_s));
187 g_tree_insert(proc_tree, proc_key, proc_value);
190 __proc_tree_printall();
193 parent = __proc_tree_find_parent(proc_value);
195 stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, proc_key->pid,
196 parent->cmdline, parent->cmdline, STC_APP_TYPE_SERVICE);
198 stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, proc_key->pid,
199 proc_value->cmdline, proc_value->cmdline, STC_APP_TYPE_SERVICE);
202 static void __proc_tree_remove(const proc_key_s *key,
203 const proc_value_s *value)
205 if (proc_tree == NULL) {
206 STC_LOGE("tree is null");
210 stc_plugin_procfs_status_changed(STC_CMD_SET_TERMINATED, key->pid,
211 value->cmdline, value->cmdline, STC_APP_TYPE_NONE);
213 g_tree_remove(proc_tree, key);
216 __proc_tree_printall();
220 static gboolean __check_excn(char *cmdline)
222 stc_error_e ret = STC_ERROR_NONE;
224 if (cmdline[0] == '(')
227 ret = stc_monitor_check_excn_by_cmdline(cmdline);
228 if (ret == STC_ERROR_UNINITIALIZED ||
229 ret == STC_ERROR_NO_DATA)
235 static void __open_nl_connector_sock(void)
237 __STC_LOG_FUNC_ENTER__;
238 GIOChannel *gio = NULL;
240 if (nl_connector_sock != -1 &&
241 nl_connector_gsource_id != 0) {
242 STC_LOGE("Socket is already open");
243 __STC_LOG_FUNC_EXIT__;
247 if (nl_connector_sock != -1) {
248 close(nl_connector_sock);
249 nl_connector_sock = -1;
252 if (nl_connector_gsource_id != 0) {
253 g_source_remove(nl_connector_gsource_id);
254 nl_connector_gsource_id = 0;
257 nl_connector_sock = create_netlink(NETLINK_CONNECTOR, CN_IDX_PROC);
258 if (nl_connector_sock == -1) {
259 __STC_LOG_FUNC_EXIT__;
263 gio = g_io_channel_unix_new(nl_connector_sock);
264 nl_connector_gsource_id =
265 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
266 (GIOFunc) __process_nl_connector_message,
268 g_io_channel_unref(gio);
269 __STC_LOG_FUNC_EXIT__;
272 static void __close_nl_connector_sock(void)
274 __STC_LOG_FUNC_ENTER__;
275 if (nl_connector_sock != -1) {
276 close(nl_connector_sock);
277 nl_connector_sock = -1;
280 if (nl_connector_gsource_id != 0) {
281 g_source_remove(nl_connector_gsource_id);
282 nl_connector_gsource_id = 0;
284 __STC_LOG_FUNC_EXIT__;
287 static void __reopen_nl_connector_sock(void)
289 __close_nl_connector_sock();
290 __open_nl_connector_sock();
293 static void __process_event_fork(int tgid, int pid)
295 char cmdline[PROC_NAME_MAX] = {0, };
296 char status[PROC_STATUS_CNT][PROC_BUF_MAX];
298 /* TODO: Add newly created thread to the process tasks */
302 memset(status, 0x0, sizeof(status));
304 if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
305 STC_ERROR_NONE == proc_get_status(pid, status)) {
307 if (__check_excn(cmdline)) {
309 STC_LOGD("[%s] monitoring is excepted", cmdline);
317 memset(&key, 0x0, sizeof(proc_key_s));
318 memset(&value, 0x0, sizeof(proc_value_s));
321 for (i = 0; i < PROC_STATUS_CNT; ++i)
322 g_strlcpy(value.status[i], status[i], sizeof(value.status[i]));
323 g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline));
326 STC_LOGD("\033[1;32mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] "
327 "ppid=[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]",
328 tgid, status[PROC_STATUS_PPID], cmdline, pid);
330 __proc_tree_add(&key, &value);
334 static void __process_event_exec(int tgid, int pid)
336 char cmdline[PROC_NAME_MAX] = {0, };
337 char status[PROC_STATUS_CNT][PROC_BUF_MAX];
339 /* TODO: Add newly created thread to the process tasks */
343 memset(status, 0x0, sizeof(status));
345 if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
346 STC_ERROR_NONE == proc_get_status(pid, status)) {
348 if (__check_excn(cmdline)) {
350 STC_LOGD("[%s] monitoring is excepted", cmdline);
358 memset(&key, 0x0, sizeof(proc_key_s));
359 memset(&value, 0x0, sizeof(proc_value_s));
362 for (i = 0; i < PROC_STATUS_CNT; ++i)
363 g_strlcpy(value.status[i], status[i],
364 sizeof(value.status[i]));
365 g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline));
368 STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] "
369 "ppid[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]",
370 tgid, status[PROC_STATUS_PPID], cmdline, pid);
372 __proc_tree_add(&key, &value);
376 static void __process_event_exit(int tgid, int pid, int exit_code)
379 proc_value_s *lookup;
385 lookup = __proc_tree_lookup(&key);
386 if (lookup == NULL) /* unmonitored process */
390 STC_LOGD("\033[1;31mEXIT\033[0;m: tgid[\033[1;33m%d\033[0;m] "
391 "cmdline[\033[0;34m%s\033[0;m] pid[%d] exitcode[%d]",
392 tgid, lookup->cmdline, pid, exit_code);
394 __proc_tree_remove(&key, lookup);
397 static gboolean __process_nl_connector_message(GIOChannel *source,
398 GIOCondition condition,
402 int sock = g_io_channel_unix_get_fd(source);
403 nl_connector_proc_event_s msg;
405 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
406 (condition & G_IO_NVAL)) {
407 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
409 STC_LOGE("Netlink Connector socket received G_IO event, closing"
410 " socket. G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
411 (condition & G_IO_ERR), (condition & G_IO_HUP),
412 (condition & G_IO_NVAL));
413 __reopen_nl_connector_sock();
414 __STC_LOG_FUNC_EXIT__;
418 memset(&msg, 0, sizeof(nl_connector_proc_event_s));
420 ret = read(sock, &msg, sizeof(nl_connector_proc_event_s));
422 __STC_LOG_FUNC_EXIT__;
426 switch (msg.proc_ev.what) {
427 case PROC_EVENT_FORK:
428 __process_event_fork(msg.proc_ev.event_data.fork.child_tgid,
429 msg.proc_ev.event_data.fork.child_pid);
431 case PROC_EVENT_EXEC:
432 __process_event_exec(msg.proc_ev.event_data.exec.process_tgid,
433 msg.proc_ev.event_data.exec.process_pid);
435 case PROC_EVENT_EXIT:
436 __process_event_exit(msg.proc_ev.event_data.exit.process_tgid,
437 msg.proc_ev.event_data.exit.process_pid,
438 msg.proc_ev.event_data.exit.exit_code);
447 static int __subscribe_proc_events(void)
449 __STC_LOG_FUNC_ENTER__;
450 nl_connector_msg_s msg;
452 int sock = nl_connector_sock;
455 __STC_LOG_FUNC_EXIT__;
459 memset(&msg, 0, sizeof(nl_connector_msg_s));
461 msg.nl_hdr.nlmsg_len = sizeof(nl_connector_msg_s);
462 msg.nl_hdr.nlmsg_pid = getpid();
463 msg.nl_hdr.nlmsg_type = NLMSG_DONE;
465 msg.cn_msg.id.idx = CN_IDX_PROC;
466 msg.cn_msg.id.val = CN_VAL_PROC;
467 msg.cn_msg.len = sizeof(enum proc_cn_mcast_op);
469 msg.cn_mcast = PROC_CN_MCAST_LISTEN;
471 ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0);
473 STC_LOGE("Error sending netlink connector message");
474 __STC_LOG_FUNC_EXIT__;
478 __STC_LOG_FUNC_EXIT__;
482 static int __unsubscribe_proc_events(void)
484 __STC_LOG_FUNC_ENTER__;
485 nl_connector_msg_s msg;
487 int sock = nl_connector_sock;
490 __STC_LOG_FUNC_EXIT__;
494 memset(&msg, 0, sizeof(nl_connector_msg_s));
496 msg.nl_hdr.nlmsg_len = sizeof(nl_connector_msg_s);
497 msg.nl_hdr.nlmsg_pid = getpid();
498 msg.nl_hdr.nlmsg_type = NLMSG_DONE;
500 msg.cn_msg.id.idx = CN_IDX_PROC;
501 msg.cn_msg.id.val = CN_VAL_PROC;
502 msg.cn_msg.len = sizeof(enum proc_cn_mcast_op);
504 msg.cn_mcast = PROC_CN_MCAST_IGNORE;
506 ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0);
508 STC_LOGE("Error sending netlink connector message");
509 __STC_LOG_FUNC_EXIT__;
513 __STC_LOG_FUNC_EXIT__;
517 static bool __process_pid_cb(pid_t pid, void *user_data)
519 char cmdline[PROC_NAME_MAX] = {0, };
520 char status[PROC_STATUS_CNT][PROC_BUF_MAX];
522 memset(status, 0x0, sizeof(status));
524 if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
525 STC_ERROR_NONE == proc_get_status(pid, status)) {
527 if (__check_excn(cmdline))
534 memset(&key, 0x0, sizeof(proc_key_s));
535 memset(&value, 0x0, sizeof(proc_value_s));
538 for (i = 0; i < PROC_STATUS_CNT; ++i)
539 g_strlcpy(value.status[i], status[i], sizeof(value.status[i]));
540 g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline));
542 __proc_tree_add(&key, &value);
548 int stc_plugin_procfs_initialize(void)
550 __STC_LOG_FUNC_ENTER__;
552 proc_tree = g_tree_new_full(__proc_tree_key_compare, NULL,
553 __proc_tree_key_free,
554 __proc_tree_value_free);
556 /* TODO: Fill proc tree with current procfs state */
558 __open_nl_connector_sock();
559 __subscribe_proc_events();
560 __STC_LOG_FUNC_EXIT__;
561 return STC_ERROR_NONE;
564 int stc_plugin_procfs_deinitialize(void)
566 __STC_LOG_FUNC_ENTER__;
568 if (nl_connector_sock == -1) {
569 STC_LOGD("socket already closed");
570 return STC_ERROR_NONE;
573 __unsubscribe_proc_events();
574 __close_nl_connector_sock();
576 g_tree_destroy(proc_tree);
579 __STC_LOG_FUNC_EXIT__;
580 return STC_ERROR_NONE;
583 stc_error_e stc_plugin_procfs_load(void)
585 __STC_LOG_FUNC_ENTER__;
587 proc_foreach_pid(__process_pid_cb, NULL);
589 __STC_LOG_FUNC_EXIT__;
590 return STC_ERROR_NONE;
593 stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
597 stc_app_type_e app_type)
599 stc_error_e ret = STC_ERROR_NONE;
601 if ((pkg_id && app_id) && STC_DEBUG_LOG)
602 STC_LOGD("cmd[%d] pkgid[%s] appid[%s] pid[%d] type[%d]",
603 cmd, pkg_id, app_id, pid, app_type);
606 case STC_CMD_SET_FOREGRD:
610 stc_app_value_s app_value;
611 stc_proc_value_s proc_value;
613 memset(&app_value, 0, sizeof(stc_app_value_s));
614 memset(&proc_value, 0, sizeof(stc_proc_value_s));
616 bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
618 app_value.type = app_type;
619 app_value.processes = NULL;
621 proc_value.pid = pid;
622 proc_value.ground = STC_APP_STATE_FOREGROUND;
624 classid = get_classid_by_app_id(bg_app_id, FALSE);
625 stc_monitor_proc_remove(classid, pid);
627 classid = get_classid_by_app_id(app_id, TRUE);
629 stc_monitor_app_add(classid, app_id, pkg_id, app_value);
630 stc_monitor_proc_add(classid, app_id, proc_value);
631 stc_monitor_proc_update_ground(classid, app_id, proc_value);
636 case STC_CMD_SET_BACKGRD:
640 stc_app_value_s app_value;
641 stc_proc_value_s proc_value;
643 memset(&app_value, 0, sizeof(stc_app_value_s));
644 memset(&proc_value, 0, sizeof(stc_proc_value_s));
646 bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
648 app_value.type = app_type;
649 app_value.processes = NULL;
651 proc_value.pid = pid;
652 proc_value.ground = STC_APP_STATE_BACKGROUND;
654 classid = get_classid_by_app_id(app_id, FALSE);
655 stc_monitor_proc_remove(classid, pid);
657 classid = get_classid_by_app_id(bg_app_id, TRUE);
659 stc_monitor_app_add(classid, bg_app_id, pkg_id, app_value);
660 stc_monitor_proc_add(classid, bg_app_id, proc_value);
661 stc_monitor_proc_update_ground(classid, bg_app_id, proc_value);
666 case STC_CMD_SET_SERVICE_LAUNCHED:
670 stc_app_value_s app_value;
671 stc_proc_value_s proc_value;
673 memset(&app_value, 0, sizeof(stc_app_value_s));
674 memset(&proc_value, 0, sizeof(stc_proc_value_s));
676 bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
678 app_value.type = app_type;
679 app_value.processes = NULL;
681 proc_value.pid = pid;
682 proc_value.ground = STC_APP_STATE_BACKGROUND;
684 classid = get_classid_by_app_id(bg_app_id, TRUE);
686 stc_monitor_app_add(classid, bg_app_id, pkg_id, app_value);
687 stc_monitor_proc_add(classid, bg_app_id, proc_value);
692 case STC_CMD_SET_TERMINATED:
697 bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
698 classid = get_classid_by_app_id(bg_app_id, FALSE);
700 if (classid == STC_UNKNOWN_CLASSID)
701 classid = get_classid_by_app_id(app_id, FALSE);
703 stc_monitor_proc_remove(classid, pid);
709 STC_LOGE("Unhandled command");
710 ret = STC_ERROR_INVALID_PARAMETER;
716 API stc_plugin_procfs_s stc_plugin_procfs = {
718 stc_plugin_procfs_initialize,
719 .deinitialize_plugin =
720 stc_plugin_procfs_deinitialize,
722 stc_plugin_procfs_load,
723 .procfs_status_changed =
724 stc_plugin_procfs_status_changed