blob: b5d037a313a5b887fd28e38e80c5c5d36bcfa3c7 [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
Jeff Sharkeydeb24052015-03-02 21:01:40 -080017#include "sehandle.h"
18#include "Utils.h"
19#include "Process.h"
20
Jeff Sharkey36801cc2015-03-13 16:09:20 -070021#include <base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023#include <private/android_filesystem_config.h>
24
25#include <fcntl.h>
26#include <linux/fs.h>
27#include <stdlib.h>
28#include <sys/mount.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/wait.h>
32
33#ifndef UMOUNT_NOFOLLOW
34#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
35#endif
36
37namespace android {
38namespace vold {
39
40status_t CreateDeviceNode(const std::string& path, dev_t dev) {
41 const char* cpath = path.c_str();
42 status_t res = 0;
43
44 char* secontext = nullptr;
45 if (sehandle) {
46 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) {
47 setfscreatecon(secontext);
48 }
49 }
50
51 mode_t mode = 0660 | S_IFBLK;
52 if (mknod(cpath, mode, dev) < 0) {
53 if (errno != EEXIST) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070054 PLOG(ERROR) << "Failed to create device node for " << major(dev)
55 << ":" << minor(dev) << " at " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080056 res = -errno;
57 }
58 }
59
60 if (secontext) {
61 setfscreatecon(nullptr);
62 freecon(secontext);
63 }
64
65 return res;
66}
67
68status_t DestroyDeviceNode(const std::string& path) {
69 const char* cpath = path.c_str();
70 if (TEMP_FAILURE_RETRY(unlink(cpath))) {
71 return -errno;
72 } else {
73 return OK;
74 }
75}
76
77status_t ForceUnmount(const std::string& path) {
78 const char* cpath = path.c_str();
79 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
80 return OK;
81 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082 PLOG(WARNING) << "Failed to unmount " << path << "; sending SIGTERM";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 Process::killProcessesWithOpenFiles(cpath, SIGTERM);
84 sleep(1);
85
86 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
87 return OK;
88 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070089 PLOG(WARNING) << "Failed to unmount " << path << "; sending SIGKILL";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080090 Process::killProcessesWithOpenFiles(cpath, SIGKILL);
91 sleep(1);
92
93 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
94 return OK;
95 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096 PLOG(ERROR) << "Failed to unmount " << path << "; giving up";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080097 return -errno;
98}
99
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700100status_t BindMount(const std::string& source, const std::string& target) {
101 if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) {
102 PLOG(ERROR) << "Failed to bind mount " << source << " to " << target;
103 return -errno;
104 }
105 return OK;
106}
107
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800108} // namespace vold
109} // namespace android