block: debug improvement for application using external storage 10/105210/3
authorKunhoon Baik <knhoon.baik@samsung.com>
Fri, 16 Dec 2016 04:17:07 +0000 (13:17 +0900)
committerTaeyoung Kim <ty317.kim@samsung.com>
Mon, 19 Dec 2016 01:56:33 +0000 (17:56 -0800)
print information who uses the storage unmounting

Change-Id: I364e4cf8214b43d9b7c07d5d810ac967a7895574

src/block/block.c
src/core/common.c

index 8468ddf..cccb69f 100644 (file)
@@ -1226,6 +1226,8 @@ static int block_unmount(struct block_device *bdev,
                        if (app2ext_disable_all_external_pkgs() < 0)
                                _E("app2ext_disable_all_external_pkgs() failed");
 
+               print_open_files(data->mount_point);
+
                r = mmc_check_and_unmount(data->mount_point);
                if (!r) {
                        _D("Success to unmount (%d)", data->mount_point);
index c05c48c..e26edd4 100644 (file)
@@ -58,6 +58,77 @@ FILE *open_proc_oom_score_adj_file(int pid, const char *mode)
        return fp;
 }
 
+int print_open_files(const char *mount_point)
+{
+       DIR *dp;
+       struct dirent entry;
+       struct dirent *dentry;
+
+       DIR *dp_child;
+       struct dirent entry_child;
+       struct dirent *dentry_child;
+
+       int pid = -1, fd;
+       int ret;
+       char buf[PATH_MAX];
+       char buf2[PATH_MAX];
+
+       char cmdline[PATH_MAX];
+       char check_path[PATH_MAX];
+
+       int len = strlen(mount_point);
+
+       dp = opendir("/proc");
+       if (!dp) {
+               _E("FAIL: open /proc");
+               return -1;
+       }
+
+       while (1) {
+               ret = readdir_r(dp, &entry, &dentry);
+               if (ret != 0 || dentry == NULL)
+                       break;
+
+               if (!isdigit(dentry->d_name[0]))
+                       continue;
+
+               pid = atoi(dentry->d_name);
+               snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
+
+               fd = open(buf, O_RDONLY);
+               if (fd < 0)
+                       continue;
+               ret = read(fd, cmdline, PATH_MAX);
+               close(fd);
+
+               if (ret < 0 || ret >= PATH_MAX)
+                       continue;
+               cmdline[ret] = '\0';
+
+               snprintf(buf, PATH_MAX, "/proc/%d/fd", pid);
+               dp_child = opendir(buf);
+               if (!dp_child)
+                       continue;
+               while (1){
+                       ret = readdir_r(dp_child, &entry_child, &dentry_child);
+                       if (ret != 0 || dentry_child == NULL)
+                               break;
+
+                       snprintf(check_path, PATH_MAX, "%s/%s",buf, dentry_child->d_name);
+
+                       if(readlink(check_path, buf2, PATH_MAX) < 0)
+                               continue;
+
+                       if(strncmp(buf2, mount_point, len) == 0)
+                               _D("Process %s : Opened files - %s", cmdline, buf2);
+               }
+               closedir(dp_child);
+       }
+
+       closedir(dp);
+       return 0;
+}
+
 int get_exec_pid(const char *execpath)
 {
        DIR *dp;