Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "sehandle.h" |
| 18 | #include "Utils.h" |
| 19 | #include "Process.h" |
| 20 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 22 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | #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 | |
| 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
| 40 | status_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 Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 54 | PLOG(ERROR) << "Failed to create device node for " << major(dev) |
| 55 | << ":" << minor(dev) << " at " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 56 | res = -errno; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (secontext) { |
| 61 | setfscreatecon(nullptr); |
| 62 | freecon(secontext); |
| 63 | } |
| 64 | |
| 65 | return res; |
| 66 | } |
| 67 | |
| 68 | status_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 | |
| 77 | status_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 Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 82 | PLOG(WARNING) << "Failed to unmount " << path << "; sending SIGTERM"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 83 | Process::killProcessesWithOpenFiles(cpath, SIGTERM); |
| 84 | sleep(1); |
| 85 | |
| 86 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 87 | return OK; |
| 88 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 89 | PLOG(WARNING) << "Failed to unmount " << path << "; sending SIGKILL"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 90 | Process::killProcessesWithOpenFiles(cpath, SIGKILL); |
| 91 | sleep(1); |
| 92 | |
| 93 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 94 | return OK; |
| 95 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 96 | PLOG(ERROR) << "Failed to unmount " << path << "; giving up"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 97 | return -errno; |
| 98 | } |
| 99 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 100 | status_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 Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 108 | } // namespace vold |
| 109 | } // namespace android |