4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <sys/types.h>
36 #include <system_info.h>
37 #include <sys/mount.h>
42 int print_open_files(const char *mount_point)
49 struct dirent *dir_child;
56 char cmdline[PATH_MAX];
57 char check_path[PATH_MAX];
59 int len = strlen(mount_point);
61 dp = opendir("/proc");
63 _E("FAIL: open /proc");
68 ret = readdir_r(dp, &entry, &dir);
74 if (!isdigit(dir->d_name[0]))
77 pid = atoi(dir->d_name);
78 snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
80 fd = open(buf, O_RDONLY);
83 ret = read(fd, cmdline, PATH_MAX);
86 if (ret < 0 || ret >= PATH_MAX)
90 snprintf(buf, PATH_MAX, "/proc/%d/fd", pid);
91 dp_child = opendir(buf);
95 ret = readdir_r(dp, &entry, &dir_child);
96 if (dir_child == NULL)
101 snprintf(check_path, PATH_MAX, "%s/%s", buf, dir_child->d_name);
103 if (readlink(check_path, buf2, PATH_MAX) < 0)
106 if (strncmp(buf2, mount_point, len) == 0)
107 _D("Process %s : Opened files - %s", cmdline, buf2);
116 int terminate_process(const char *partition, bool force)
118 const char *argv[7] = {"/usr/bin/fuser", "-m", "-k", "-s", NULL, NULL, NULL};
122 argv[4] = "-SIGKILL";
124 argv[4] = "-SIGTERM";
126 argc = sizeof(argv) / sizeof(argv[0]);
127 return run_child(argc, argv);
130 int mount_check(const char *path)
134 const char *table = "/etc/mtab";
138 fp = setmntent(table, "r");
142 len = strlen(path) + 1;
147 if (!strncmp(mnt->mnt_dir, path, len)) {
156 int umount_partition(const char *path, const bool force)
158 int retry = 0, ret = -1;
159 struct timespec time = {0,};
162 while (ret && retry < 2) {
165 /* Second, kill app with SIGTERM */
166 _I("Kill app with SIGTERM");
167 terminate_process(path, false);
168 time.tv_nsec = 500 * NANO_SECOND_MULTIPLIER;
169 nanosleep(&time, NULL);
170 ret = umount2(path, 0);
173 /* Last time, kill app with SIGKILL */
174 _I("Kill app with SIGKILL");
175 terminate_process(path, true);
176 time.tv_nsec = 200 * NANO_SECOND_MULTIPLIER;
177 nanosleep(&time, NULL);
178 ret = umount2(path, 0);
181 _I("ret %d retry %d", ret, retry);
185 ret = umount2(path, MNT_DETACH);
187 ret = umount2(path, 0);
190 _I("Failed to unmount %s", path);
192 _I("%s unmounted successfully", path);