• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include "apexd_private.h"
18 
19 #include <sys/mount.h>
20 #include <sys/stat.h>
21 
22 #include <android-base/logging.h>
23 #include <android-base/macros.h>
24 
25 #include "string_log.h"
26 
27 using android::base::ErrnoError;
28 using android::base::Result;
29 
30 namespace android {
31 namespace apex {
32 namespace apexd_private {
33 
BindMount(const std::string & target,const std::string & source)34 Result<void> BindMount(const std::string& target, const std::string& source) {
35   LOG(VERBOSE) << "Creating bind-mount for " << target << " for " << source;
36   // Ensure the directory exists, try to unmount.
37   {
38     bool exists;
39     bool is_dir;
40     {
41       struct stat buf;
42       if (stat(target.c_str(), &buf) != 0) {
43         if (errno == ENOENT) {
44           exists = false;
45           is_dir = false;
46         } else {
47           PLOG(ERROR) << "Could not stat target directory " << target;
48           // Still attempt to bind-mount.
49           exists = true;
50           is_dir = true;
51         }
52       } else {
53         exists = true;
54         is_dir = S_ISDIR(buf.st_mode);
55       }
56     }
57 
58     // Ensure that it is a folder.
59     if (exists && !is_dir) {
60       LOG(WARNING) << target << " is not a directory, attempting to fix";
61       if (unlink(target.c_str()) != 0) {
62         PLOG(ERROR) << "Failed to unlink " << target;
63         // Try mkdir, anyways.
64       }
65       exists = false;
66     }
67     // And create it if necessary.
68     if (!exists) {
69       LOG(VERBOSE) << "Creating mountpoint " << target;
70       if (mkdir(target.c_str(), kMkdirMode) != 0) {
71         return ErrnoError() << "Could not create mountpoint " << target;
72       }
73     };
74     // Unmount any active bind-mount.
75     if (exists) {
76       int rc = umount2(target.c_str(), UMOUNT_NOFOLLOW);
77       if (rc != 0 && errno != EINVAL) {
78         // Log error but ignore.
79         PLOG(ERROR) << "Could not unmount " << target;
80       }
81     }
82   }
83 
84   LOG(VERBOSE) << "Bind-mounting " << source << " to " << target;
85   if (mount(source.c_str(), target.c_str(), nullptr, MS_BIND, nullptr) == 0) {
86     return {};
87   }
88   return ErrnoError() << "Could not bind-mount " << source << " to " << target;
89 }
90 
91 }  // namespace apexd_private
92 }  // namespace apex
93 }  // namespace android
94