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)
45 struct dirent *dentry;
48 struct dirent *dentry_child;
55 char cmdline[PATH_MAX];
56 char check_path[PATH_MAX];
58 int len = strlen(mount_point);
60 dp = opendir("/proc");
62 _E("FAIL: open /proc");
71 if (!isdigit(dentry->d_name[0]))
74 pid = atoi(dentry->d_name);
75 snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
77 fd = open(buf, O_RDONLY);
80 ret = read(fd, cmdline, PATH_MAX);
83 if (ret < 0 || ret >= PATH_MAX)
87 snprintf(buf, PATH_MAX, "/proc/%d/fd", pid);
88 dp_child = opendir(buf);
92 dentry_child = readdir(dp_child);
93 if (dentry_child == NULL)
96 snprintf(check_path, PATH_MAX, "%s/%s", buf, dentry_child->d_name);
98 if (readlink(check_path, buf2, PATH_MAX) < 0)
101 if (strncmp(buf2, mount_point, len) == 0)
102 _D("Process %s : Opened files - %s", cmdline, buf2);
111 int terminate_process(const char *partition, bool force)
113 const char *argv[7] = {"/usr/bin/fuser", "-m", "-k", "-s", NULL, NULL, NULL};
117 argv[4] = "-SIGKILL";
119 argv[4] = "-SIGTERM";
121 argc = sizeof(argv) / sizeof(argv[0]);
122 return run_child(argc, argv);
125 int mount_check(const char *path)
129 const char *table = "/etc/mtab";
133 fp = setmntent(table, "r");
137 len = strlen(path) + 1;
142 if (!strncmp(mnt->mnt_dir, path, len)) {
151 int umount_partition(const char *path, const bool force)
153 int retry = 0, ret = -1;
154 struct timespec time = {0,};
157 while (ret && retry < 2) {
160 /* Second, kill app with SIGTERM */
161 _I("Kill app with SIGTERM");
162 terminate_process(path, false);
163 time.tv_nsec = 500 * NANO_SECOND_MULTIPLIER;
164 nanosleep(&time, NULL);
165 ret = umount2(path, 0);
168 /* Last time, kill app with SIGKILL */
169 _I("Kill app with SIGKILL");
170 terminate_process(path, true);
171 time.tv_nsec = 200 * NANO_SECOND_MULTIPLIER;
172 nanosleep(&time, NULL);
173 ret = umount2(path, 0);
176 _I("ret %d retry %d", ret, retry);
180 ret = umount2(path, MNT_DETACH);
182 ret = umount2(path, 0);
185 _I("Failed to unmount %s", path);
187 _I("%s unmounted successfully", path);