blob: 7cbb2ce2749d27cdec051856d30f0a0a435d8d2f [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Vold"
18
19#include "sehandle.h"
20#include "Utils.h"
21#include "Process.h"
22
23#include <cutils/fs.h>
24#include <cutils/log.h>
25#include <utils/file.h>
26#include <utils/stringprintf.h>
27#include <private/android_filesystem_config.h>
28
29#include <fcntl.h>
30#include <linux/fs.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/wait.h>
36
37#ifndef UMOUNT_NOFOLLOW
38#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
39#endif
40
41namespace android {
42namespace vold {
43
44status_t CreateDeviceNode(const std::string& path, dev_t dev) {
45 const char* cpath = path.c_str();
46 status_t res = 0;
47
48 char* secontext = nullptr;
49 if (sehandle) {
50 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) {
51 setfscreatecon(secontext);
52 }
53 }
54
55 mode_t mode = 0660 | S_IFBLK;
56 if (mknod(cpath, mode, dev) < 0) {
57 if (errno != EEXIST) {
58 ALOGW("Failed to create device node for %ud:%ud at %s: %s",
59 major(dev), minor(dev), cpath, strerror(errno));
60 res = -errno;
61 }
62 }
63
64 if (secontext) {
65 setfscreatecon(nullptr);
66 freecon(secontext);
67 }
68
69 return res;
70}
71
72status_t DestroyDeviceNode(const std::string& path) {
73 const char* cpath = path.c_str();
74 if (TEMP_FAILURE_RETRY(unlink(cpath))) {
75 return -errno;
76 } else {
77 return OK;
78 }
79}
80
81status_t ForceUnmount(const std::string& path) {
82 const char* cpath = path.c_str();
83 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
84 return OK;
85 }
86 ALOGW("Failed to unmount %s (%s), sending SIGTERM", cpath, strerror(errno));
87 Process::killProcessesWithOpenFiles(cpath, SIGTERM);
88 sleep(1);
89
90 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
91 return OK;
92 }
93 ALOGW("Failed to unmount %s (%s), sending SIGKILL", cpath, strerror(errno));
94 Process::killProcessesWithOpenFiles(cpath, SIGKILL);
95 sleep(1);
96
97 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
98 return OK;
99 }
100 ALOGW("Failed to unmount %s (%s)", cpath, strerror(errno));
101 return -errno;
102}
103
104} // namespace vold
105} // namespace android