Merge "Return errors to caller" into tizen_5.5
[platform/core/connectivity/stc-manager.git] / src / helper / helper-procfs.c
old mode 100755 (executable)
new mode 100644 (file)
index ca291ac..8cd0461
 #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");
        if (fp == NULL)
-               return STC_ERROR_FAIL;
+               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
 
        if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
-               fclose(fp);
-               return STC_ERROR_FAIL;
+               fclose(fp); //LCOV_EXCL_LINE
+               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
        }
        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);
 
        return STC_ERROR_NONE;
 }
 
+//LCOV_EXCL_START
 pid_t find_pid_from_cmdline(char *cmdline)
 {
        pid_t pid = -1, foundpid = -1;
@@ -100,6 +118,35 @@ pid_t find_pid_from_cmdline(char *cmdline)
        return foundpid;
 }
 
+API void proc_foreach_pid(proc_pid_cb cb, void *user_data)
+{
+       pid_t pid = -1;
+       int ret = 0;
+       DIR *dp;
+       struct dirent *dentry;
+
+       dp = opendir("/proc");
+       if (!dp) {
+               STC_LOGE("failed to open /proc");
+               return;
+       }
+
+       while ((dentry = readdir(dp)) != NULL) {
+               if (!isdigit(dentry->d_name[0]))
+                       continue;
+
+               pid = atoi(dentry->d_name);
+               if (!pid)
+                       continue;
+
+               ret = cb(pid, user_data);
+               if (ret == false)
+                       break;
+       }
+
+       closedir(dp);
+}
+
 int proc_get_label(pid_t pid, char *label)
 {
        char buf[PROC_BUF_MAX];
@@ -156,12 +203,15 @@ int proc_get_raw_cmdline(pid_t pid, char *buf, int len)
        snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
        return proc_get_data(path, buf, len);
 }
+//LCOV_EXCL_STOP
 
-int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
+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);
@@ -174,17 +224,43 @@ int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
                char *saveptr = NULL;
 
                if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
-                       fclose(fp);
-                       return STC_ERROR_FAIL;
+                       fclose(fp); //LCOV_EXCL_LINE
+                       return STC_ERROR_FAIL; //LCOV_EXCL_LINE
                }
 
+               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[i], token, sizeof(status[i]));
+                               strncpy(status[index], token,
+                                       sizeof(status[index]));
+                               updated[index] = TRUE;
                        }
                }
        }