Moved procfs for app lifecycle to plugin and separate plugins
[platform/core/connectivity/stc-manager.git] / src / helper / helper-procfs.c
old mode 100644 (file)
new mode 100755 (executable)
index 0974f2a..c984abb
 #include "stc-manager-util.h"
 #include "helper-procfs.h"
 
-int proc_get_cmdline(pid_t pid, char *cmdline)
+#define USRAPPS "/usr/apps/"
+
+API int proc_get_cmdline(pid_t pid, char *cmdline)
 {
        char buf[PROC_BUF_MAX];
        char cmdline_buf[PROC_NAME_MAX];
        char *filename;
        FILE *fp;
+       char *token = NULL;
+       char *saveptr = NULL;
 
        snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
        fp = fopen(buf, "r");
@@ -56,11 +60,24 @@ int proc_get_cmdline(pid_t pid, char *cmdline)
        }
        fclose(fp);
 
-       filename = strrchr(cmdline_buf, '/');
-       if (filename == NULL)
-               filename = cmdline_buf;
-       else
-               filename = filename + 1;
+       if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) {
+               /* Application */
+               filename = cmdline_buf + strlen(USRAPPS);
+               token = strtok_r(filename, "/", &saveptr);
+               if (token != NULL)
+                       filename = token;
+       } else {
+               token = strtok_r(cmdline_buf, " ", &saveptr);
+               if (token != NULL)
+                       filename = strrchr(token, '/');
+               else
+                       filename = strrchr(cmdline_buf, '/');
+
+               if (filename == NULL)
+                       filename = cmdline_buf;
+               else
+                       filename = filename + 1;
+       }
 
        strncpy(cmdline, filename, PROC_NAME_MAX-1);
 
@@ -157,9 +174,66 @@ int proc_get_raw_cmdline(pid_t pid, char *buf, int len)
        return proc_get_data(path, buf, len);
 }
 
-int proc_get_status(pid_t pid, char *buf, int len)
+API int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
 {
+       unsigned int i;
+       unsigned int index = 0;
        char path[PROC_BUF_MAX];
+       char status_buf[PROC_BUF_MAX];
+       bool updated[PROC_STATUS_CNT] = {FALSE, };
+       FILE *fp;
+
        snprintf(path, sizeof(path), "/proc/%d/status", pid);
-       return proc_get_data(path, buf, len);
+       fp = fopen(path, "r");
+       if (fp == NULL)
+               return STC_ERROR_FAIL;
+
+       for (i = 0; i < PROC_STATUS_CNT; ++i) {
+               char *token = NULL;
+               char *saveptr = NULL;
+
+               if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
+                       fclose(fp);
+                       return STC_ERROR_FAIL;
+               }
+
+               if (!updated[PROC_STATUS_NAME] && strstr(status_buf,
+                                                        PROC_STATUS_NAME_STR))
+                       index = PROC_STATUS_NAME;
+               else if (!updated[PROC_STATUS_STATE] && strstr(status_buf,
+                                                              PROC_STATUS_STATE_STR))
+                       index = PROC_STATUS_STATE;
+               else if (!updated[PROC_STATUS_TGID] && strstr(status_buf,
+                                                             PROC_STATUS_TGID_STR))
+                       index = PROC_STATUS_TGID;
+               else if (!updated[PROC_STATUS_NGID] && strstr(status_buf,
+                                                             PROC_STATUS_NGID_STR))
+                       index = PROC_STATUS_NGID;
+               else if (!updated[PROC_STATUS_PID] && strstr(status_buf,
+                                                            PROC_STATUS_PID_STR))
+                       index = PROC_STATUS_PID;
+               else if (!updated[PROC_STATUS_PPID] && strstr(status_buf,
+                                                             PROC_STATUS_PPID_STR))
+                       index = PROC_STATUS_PPID;
+               else if (!updated[PROC_STATUS_TRACERPID] && strstr(status_buf,
+                                                                  PROC_STATUS_TRACERPID_STR))
+                       index = PROC_STATUS_TRACERPID;
+               else
+                       continue;
+
+               token = strtok_r(status_buf, ":", &saveptr);
+               if (token != NULL) {
+                       token = strtok_r(NULL, "\n", &saveptr);
+                       if (token != NULL) {
+                               while (isspace((unsigned char)*token))
+                                       token++;
+                               strncpy(status[index], token,
+                                       sizeof(status[index]));
+                               updated[index] = TRUE;
+                       }
+               }
+       }
+       fclose(fp);
+
+       return STC_ERROR_NONE;
 }