| 1 |
use std::io; |
| 2 |
use std::fmt; |
| 3 |
use std::ffi::CStr; |
| 4 |
use std::fs::File; |
| 5 |
use std::io::Read; |
| 6 |
use std::path::{Path, PathBuf}; |
| 7 |
use std::env::current_dir; |
| 8 |
use std::default::Default; |
| 9 |
|
| 10 |
use nix::mount::{MsFlags, mount}; |
| 11 |
|
| 12 |
use {OSError, Error}; |
| 13 |
use util::path_to_cstring; |
| 14 |
use explain::{Explainable, exists, user}; |
| 15 |
use mountinfo::{parse_mount_point}; |
| 16 |
|
| 17 |
/// A remount definition |
| 18 |
/// |
| 19 |
/// Usually it is used to change mount flags for a mounted filesystem. |
| 20 |
/// Especially to make a readonly filesystem writable or vice versa. |
| 21 |
#[derive(Debug, Clone)] |
| 22 |
pub struct Remount { |
| 23 |
path: PathBuf, |
| 24 |
flags: MountFlags, |
| 25 |
} |
| 26 |
|
| 27 |
#[derive(Debug, Clone, Default)] |
| 28 |
struct MountFlags { |
| 29 |
pub bind: Option<bool>, |
| 30 |
pub readonly: Option<bool>, |
| 31 |
pub nodev: Option<bool>, |
| 32 |
pub noexec: Option<bool>, |
| 33 |
pub nosuid: Option<bool>, |
| 34 |
pub noatime: Option<bool>, |
| 35 |
pub nodiratime: Option<bool>, |
| 36 |
pub relatime: Option<bool>, |
| 37 |
pub strictatime: Option<bool>, |
| 38 |
pub dirsync: Option<bool>, |
| 39 |
pub synchronous: Option<bool>, |
| 40 |
pub mandlock: Option<bool>, |
| 41 |
} |
| 42 |
|
| 43 |
impl MountFlags { |
| 44 |
fn apply_to_flags(&self, flags: MsFlags) -> MsFlags { |
| 45 |
let mut flags = flags; |
| 46 |
flags = apply_flag(flags, MsFlags::MS_BIND, self.bind); |
| 47 |
flags = apply_flag(flags, MsFlags::MS_RDONLY, self.readonly); |
| 48 |
flags = apply_flag(flags, MsFlags::MS_NODEV, self.nodev); |
| 49 |
flags = apply_flag(flags, MsFlags::MS_NOEXEC, self.noexec); |
| 50 |
flags = apply_flag(flags, MsFlags::MS_NOSUID, self.nosuid); |
| 51 |
flags = apply_flag(flags, MsFlags::MS_NOATIME, self.noatime); |
| 52 |
flags = apply_flag(flags, MsFlags::MS_NODIRATIME, self.nodiratime); |
| 53 |
flags = apply_flag(flags, MsFlags::MS_RELATIME, self.relatime); |
| 54 |
flags = apply_flag(flags, MsFlags::MS_STRICTATIME, self.strictatime); |
| 55 |
flags = apply_flag(flags, MsFlags::MS_DIRSYNC, self.dirsync); |
| 56 |
flags = apply_flag(flags, MsFlags::MS_SYNCHRONOUS, self.synchronous); |
| 57 |
flags = apply_flag(flags, MsFlags::MS_MANDLOCK, self.mandlock); |
| 58 |
flags |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
fn apply_flag(flags: MsFlags, flag: MsFlags, set: Option<bool>) -> MsFlags { |
| 63 |
match set { |
| 64 |
Some(true) => flags | flag, |
| 65 |
Some(false) => flags & !flag, |
| 66 |
None => flags, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
quick_error! { |
| 71 |
#[derive(Debug)] |
| 72 |
pub enum RemountError { |
| 73 |
Io(msg: String, err: io::Error) { |
| •74 |
source(err) |
| 75 |
display("{}: {}", msg, err) |
| 76 |
from(err: io::Error) -> (String::new(), err) |
| 77 |
} |
| 78 |
ParseMountInfo(err: String) { |
| 79 |
display("{}", err) |
| 80 |
from() |
| 81 |
} |
| 82 |
UnknownMountPoint(path: PathBuf) { |
| 83 |
display("Cannot find mount point: {:?}", path) |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl Remount { |
| 89 |
/// Create a new Remount operation |
| 90 |
/// |
| 91 |
/// By default it doesn't modify any flags. So is basically useless, you |
| 92 |
/// should set some flags to make it effective. |
| 93 |
pub fn new<A: AsRef<Path>>(path: A) -> Remount { |
| 94 |
Remount { |
| 95 |
path: path.as_ref().to_path_buf(), |
| 96 |
flags: Default::default(), |
| 97 |
} |
| 98 |
} |
| 99 |
/// Set bind flag |
| 100 |
/// Note: remount readonly doesn't work without MS_BIND flag |
| 101 |
/// inside unpriviledged user namespaces |
| 102 |
pub fn bind(mut self, flag: bool) -> Remount { |
| 103 |
self.flags.bind = Some(flag); |
| 104 |
self |
| 105 |
} |
| 106 |
/// Set readonly flag |
| 107 |
pub fn readonly(mut self, flag: bool) -> Remount { |
| 108 |
self.flags.readonly = Some(flag); |
| 109 |
self |
| 110 |
} |
| 111 |
/// Set nodev flag |
| 112 |
pub fn nodev(mut self, flag: bool) -> Remount { |
| 113 |
self.flags.nodev = Some(flag); |
| 114 |
self |
| 115 |
} |
| 116 |
/// Set noexec flag |
| 117 |
pub fn noexec(mut self, flag: bool) -> Remount { |
| 118 |
self.flags.noexec = Some(flag); |
| 119 |
self |
| 120 |
} |
| 121 |
/// Set nosuid flag |
| 122 |
pub fn nosuid(mut self, flag: bool) -> Remount { |
| 123 |
self.flags.nosuid = Some(flag); |
| 124 |
self |
| 125 |
} |
| 126 |
/// Set noatime flag |
| 127 |
pub fn noatime(mut self, flag: bool) -> Remount { |
| 128 |
self.flags.noatime = Some(flag); |
| 129 |
self |
| 130 |
} |
| 131 |
/// Set nodiratime flag |
| 132 |
pub fn nodiratime(mut self, flag: bool) -> Remount { |
| 133 |
self.flags.nodiratime = Some(flag); |
| 134 |
self |
| 135 |
} |
| 136 |
/// Set relatime flag |
| 137 |
pub fn relatime(mut self, flag: bool) -> Remount { |
| 138 |
self.flags.relatime = Some(flag); |
| 139 |
self |
| 140 |
} |
| 141 |
/// Set strictatime flag |
| 142 |
pub fn strictatime(mut self, flag: bool) -> Remount { |
| 143 |
self.flags.strictatime = Some(flag); |
| 144 |
self |
| 145 |
} |
| 146 |
/// Set dirsync flag |
| 147 |
pub fn dirsync(mut self, flag: bool) -> Remount { |
| 148 |
self.flags.dirsync = Some(flag); |
| 149 |
self |
| 150 |
} |
| 151 |
/// Set synchronous flag |
| 152 |
pub fn synchronous(mut self, flag: bool) -> Remount { |
| 153 |
self.flags.synchronous = Some(flag); |
| 154 |
self |
| 155 |
} |
| 156 |
/// Set mandlock flag |
| 157 |
pub fn mandlock(mut self, flag: bool) -> Remount { |
| 158 |
self.flags.mandlock = Some(flag); |
| 159 |
self |
| 160 |
} |
| 161 |
|
| 162 |
/// Execute a remount |
| 163 |
pub fn bare_remount(self) -> Result<(), OSError> { |
| 164 |
let mut flags = match get_mountpoint_flags(&self.path) { |
| 165 |
Ok(flags) => flags, |
| 166 |
Err(e) => { |
| 167 |
return Err(OSError::from_remount(e, Box::new(self))); |
| 168 |
}, |
| 169 |
}; |
| 170 |
flags = self.flags.apply_to_flags(flags) | MsFlags::MS_REMOUNT; |
| 171 |
mount( |
| 172 |
None::<&CStr>, |
| 173 |
&*path_to_cstring(&self.path), |
| 174 |
None::<&CStr>, |
| 175 |
flags, |
| 176 |
None::<&CStr>, |
| 177 |
).map_err(|err| OSError::from_nix(err, Box::new(self))) |
| 178 |
} |
| 179 |
|
| 180 |
/// Execute a remount and explain the error immediately |
| 181 |
pub fn remount(self) -> Result<(), Error> { |
| 182 |
self.bare_remount().map_err(OSError::explain) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
impl fmt::Display for MountFlags { |
| 187 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 188 |
let mut prefix = ""; |
| 189 |
if let Some(true) = self.bind { |
| 190 |
try!(write!(fmt, "{}bind", prefix)); |
| 191 |
prefix = ","; |
| 192 |
} |
| 193 |
if let Some(true) = self.readonly { |
| 194 |
try!(write!(fmt, "{}ro", prefix)); |
| 195 |
prefix = ","; |
| 196 |
} |
| 197 |
if let Some(true) = self.nodev { |
| 198 |
try!(write!(fmt, "{}nodev", prefix)); |
| 199 |
prefix = ","; |
| 200 |
} |
| 201 |
if let Some(true) = self.noexec { |
| 202 |
try!(write!(fmt, "{}noexec", prefix)); |
| 203 |
prefix = ","; |
| 204 |
} |
| 205 |
if let Some(true) = self.nosuid { |
| 206 |
try!(write!(fmt, "{}nosuid", prefix)); |
| 207 |
prefix = ","; |
| 208 |
} |
| 209 |
if let Some(true) = self.noatime { |
| 210 |
try!(write!(fmt, "{}noatime", prefix)); |
| 211 |
prefix = ","; |
| 212 |
} |
| 213 |
if let Some(true) = self.nodiratime { |
| 214 |
try!(write!(fmt, "{}nodiratime", prefix)); |
| 215 |
prefix = ","; |
| 216 |
} |
| 217 |
if let Some(true) = self.relatime { |
| 218 |
try!(write!(fmt, "{}relatime", prefix)); |
| 219 |
prefix = ","; |
| 220 |
} |
| 221 |
if let Some(true) = self.strictatime { |
| 222 |
try!(write!(fmt, "{}strictatime", prefix)); |
| 223 |
prefix = ","; |
| 224 |
} |
| 225 |
if let Some(true) = self.dirsync { |
| 226 |
try!(write!(fmt, "{}dirsync", prefix)); |
| 227 |
prefix = ","; |
| 228 |
} |
| 229 |
if let Some(true) = self.synchronous { |
| 230 |
try!(write!(fmt, "{}sync", prefix)); |
| 231 |
prefix = ","; |
| 232 |
} |
| 233 |
if let Some(true) = self.mandlock { |
| 234 |
try!(write!(fmt, "{}mand", prefix)); |
| 235 |
} |
| 236 |
Ok(()) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
impl fmt::Display for Remount { |
| 241 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 242 |
if !self.flags.apply_to_flags(MsFlags::empty()).is_empty() { |
| 243 |
try!(write!(fmt, "{} ", self.flags)); |
| 244 |
} |
| 245 |
write!(fmt, "remount {:?}", &self.path) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
impl Explainable for Remount { |
| 250 |
fn explain(&self) -> String { |
| 251 |
[ |
| 252 |
format!("path: {}", exists(&self.path)), |
| 253 |
format!("{}", user()), |
| 254 |
].join(", ") |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
fn get_mountpoint_flags(path: &Path) -> Result<MsFlags, RemountError> { |
| 259 |
let mount_path = if path.is_absolute() { |
| 260 |
path.to_path_buf() |
| 261 |
} else { |
| 262 |
let mut mpath = try!(current_dir()); |
| 263 |
mpath.push(path); |
| 264 |
mpath |
| 265 |
}; |
| 266 |
let mut mountinfo_content = Vec::with_capacity(4 * 1024); |
| 267 |
let mountinfo_path = Path::new("/proc/self/mountinfo"); |
| 268 |
let mut mountinfo_file = try!(File::open(mountinfo_path) |
| 269 |
.map_err(|e| RemountError::Io( |
| 270 |
format!("Cannot open file: {:?}", mountinfo_path), e))); |
| 271 |
try!(mountinfo_file.read_to_end(&mut mountinfo_content) |
| 272 |
.map_err(|e| RemountError::Io( |
| 273 |
format!("Cannot read file: {:?}", mountinfo_path), e))); |
| 274 |
match get_mountpoint_flags_from(&mountinfo_content, &mount_path) { |
| 275 |
Ok(Some(flags)) => Ok(flags), |
| 276 |
Ok(None) => Err(RemountError::UnknownMountPoint(mount_path)), |
| 277 |
Err(e) => Err(e), |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
fn get_mountpoint_flags_from(content: &[u8], path: &Path) |
| 282 |
-> Result<Option<MsFlags>, RemountError> |
| 283 |
{ |
| 284 |
// iterate from the end of the mountinfo file |
| 285 |
for line in content.split(|c| *c == b'\n').rev() { |
| 286 |
let entry = parse_mount_point(line) |
| 287 |
.map_err(|e| RemountError::ParseMountInfo(e.0))?; |
| 288 |
if let Some(mount_point) = entry { |
| 289 |
if mount_point.mount_point == path { |
| 290 |
return Ok(Some(mount_point.get_mount_flags())); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
Ok(None) |
| 295 |
} |
| 296 |
|
| 297 |
#[cfg(test)] |
| 298 |
mod test { |
| 299 |
use std::path::Path; |
| 300 |
use std::ffi::OsStr; |
| 301 |
use std::os::unix::ffi::OsStrExt; |
| 302 |
|
| 303 |
use nix::mount::MsFlags; |
| 304 |
|
| 305 |
use Error; |
| 306 |
use super::{Remount, RemountError, MountFlags}; |
| 307 |
use super::{get_mountpoint_flags, get_mountpoint_flags_from}; |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn test_mount_flags() { |
| 311 |
let flags = MountFlags { |
| 312 |
bind: Some(true), |
| 313 |
readonly: Some(true), |
| 314 |
nodev: Some(true), |
| 315 |
noexec: Some(true), |
| 316 |
nosuid: Some(true), |
| 317 |
noatime: Some(true), |
| 318 |
nodiratime: Some(true), |
| 319 |
relatime: Some(true), |
| 320 |
strictatime: Some(true), |
| 321 |
dirsync: Some(true), |
| 322 |
synchronous: Some(true), |
| 323 |
mandlock: Some(true), |
| 324 |
}; |
| 325 |
let bits = (MsFlags::MS_BIND | MsFlags::MS_RDONLY | MsFlags::MS_NODEV | MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | |
| 326 |
MsFlags::MS_NOATIME | MsFlags::MS_NODIRATIME | MsFlags::MS_RELATIME | MsFlags::MS_STRICTATIME | |
| 327 |
MsFlags::MS_DIRSYNC | MsFlags::MS_SYNCHRONOUS | MsFlags::MS_MANDLOCK).bits(); |
| 328 |
assert_eq!(flags.apply_to_flags(MsFlags::empty()).bits(), bits); |
| 329 |
|
| 330 |
let flags = MountFlags { |
| 331 |
bind: Some(false), |
| 332 |
readonly: Some(false), |
| 333 |
nodev: Some(false), |
| 334 |
noexec: Some(false), |
| 335 |
nosuid: Some(false), |
| 336 |
noatime: Some(false), |
| 337 |
nodiratime: Some(false), |
| 338 |
relatime: Some(false), |
| 339 |
strictatime: Some(false), |
| 340 |
dirsync: Some(false), |
| 341 |
synchronous: Some(false), |
| 342 |
mandlock: Some(false), |
| 343 |
}; |
| 344 |
assert_eq!(flags.apply_to_flags(MsFlags::from_bits_truncate(bits)).bits(), 0); |
| 345 |
|
| 346 |
let flags = MountFlags::default(); |
| 347 |
assert_eq!(flags.apply_to_flags(MsFlags::from_bits_truncate(0)).bits(), 0); |
| 348 |
assert_eq!(flags.apply_to_flags(MsFlags::from_bits_truncate(bits)).bits(), bits); |
| 349 |
} |
| 350 |
|
| 351 |
#[test] |
| 352 |
fn test_remount() { |
| 353 |
let remount = Remount::new("/"); |
| 354 |
assert_eq!(format!("{}", remount), "remount \"/\""); |
| 355 |
|
| 356 |
let remount = Remount::new("/").readonly(true).nodev(true); |
| 357 |
assert_eq!(format!("{}", remount), "ro,nodev remount \"/\""); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn test_get_mountpoint_flags_from() { |
| 362 |
let content = b"19 24 0:4 / /proc rw,nosuid,nodev,noexec,relatime shared:12 - proc proc rw"; |
| 363 |
let flags = get_mountpoint_flags_from(&content[..], Path::new("/proc")).unwrap().unwrap(); |
| 364 |
assert_eq!(flags, MsFlags::MS_NODEV | MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_RELATIME); |
| 365 |
} |
| 366 |
|
| 367 |
#[test] |
| 368 |
fn test_get_mountpoint_flags_from_dups() { |
| 369 |
let content = b"11 18 0:4 / /tmp rw shared:28 - tmpfs tmpfs rw\n\ |
| 370 |
12 18 0:6 / /tmp rw,nosuid,nodev shared:29 - tmpfs tmpfs rw\n"; |
| 371 |
let flags = get_mountpoint_flags_from(&content[..], Path::new("/tmp")).unwrap().unwrap(); |
| 372 |
assert_eq!(flags, MsFlags::MS_NOSUID | MsFlags::MS_NODEV); |
| 373 |
} |
| 374 |
|
| 375 |
#[test] |
| 376 |
fn test_get_mountpoint_flags() { |
| 377 |
let res = get_mountpoint_flags(Path::new("/")); |
| •378 |
match res { |
| 379 |
Ok(v) => {}, |
| 380 |
Err(RemountError::UnknownMountPoint(p)) => assert_eq!(p, Path::new("/")), // in some chroot environments mount point information on the / mountpoint may be unavailable. |
| 381 |
Err(e) => panic!("{}",e) |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
#[test] |
| 386 |
fn test_get_mountpoint_flags_unknown() { |
| 387 |
let mount_point = Path::new(OsStr::from_bytes(b"/\xff")); |
| 388 |
let error = get_mountpoint_flags(mount_point).unwrap_err(); |
| 389 |
match error { |
| 390 |
RemountError::UnknownMountPoint(p) => assert_eq!(p, mount_point), |
| 391 |
_ => panic!(), |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
#[test] |
| 396 |
fn test_remount_unknown_mountpoint() { |
| 397 |
let remount = Remount::new("/non-existent"); |
| 398 |
let error = remount.remount().unwrap_err(); |
| 399 |
let Error(_, e, msg) = error; |
| 400 |
match e.get_ref() { |
| 401 |
Some(e) => { |
| 402 |
assert_eq!( |
| 403 |
e.to_string(), |
| 404 |
"Cannot find mount point: \"/non-existent\""); |
| 405 |
}, |
| 406 |
_ => panic!(), |
| 407 |
} |
| 408 |
assert!(msg.starts_with( |
| 409 |
"Cannot find mount point: \"/non-existent\", path: missing, ")); |
| 410 |
} |
| 411 |
} |