xref: /aosp_15_r20/external/libbrillo/brillo/namespaces/platform.cc (revision 1a96fba65179ea7d3f56207137718607415c5953)
1 // Copyright 2020 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Contains the implementation of class Platform for libbrillo.
6 
7 #include "brillo/namespaces/platform.h"
8 
9 #include <errno.h>
10 #include <linux/magic.h>
11 #include <stdio.h>
12 #include <sys/mount.h>
13 #include <sys/stat.h>
14 #include <sys/vfs.h>
15 #include <sys/wait.h>
16 
17 #include <memory>
18 
19 #include <base/files/file_path.h>
20 
21 using base::FilePath;
22 
23 namespace brillo {
24 
Platform()25 Platform::Platform() {}
26 
~Platform()27 Platform::~Platform() {}
28 
FileSystemIsNsfs(const FilePath & ns_path)29 bool Platform::FileSystemIsNsfs(const FilePath& ns_path) {
30   struct statfs buff;
31   if (statfs(ns_path.value().c_str(), &buff) < 0) {
32     PLOG(ERROR) << "Statfs() error for " << ns_path.value();
33     return false;
34   }
35   if ((uint64_t)buff.f_type == NSFS_MAGIC) {
36     return true;
37   }
38   return false;
39 }
40 
Unmount(const FilePath & path,bool lazy,bool * was_busy)41 bool Platform::Unmount(const FilePath& path, bool lazy, bool* was_busy) {
42   int flags = 0;
43   if (lazy) {
44     flags = MNT_DETACH;
45   }
46   if (umount2(path.value().c_str(), flags) != 0) {
47     if (was_busy) {
48       *was_busy = (errno == EBUSY);
49     }
50     return false;
51   }
52   if (was_busy) {
53     *was_busy = false;
54   }
55   return true;
56 }
57 
Mount(const std::string & source,const std::string & target,const std::string & fs_type,uint64_t mount_flags,const void * data)58 int Platform::Mount(const std::string& source,
59                     const std::string& target,
60                     const std::string& fs_type,
61                     uint64_t mount_flags,
62                     const void* data) {
63   return mount(source.c_str(), target.c_str(), fs_type.c_str(), mount_flags,
64                data);
65 }
66 
Fork()67 pid_t Platform::Fork() {
68   return fork();
69 }
70 
Waitpid(pid_t pid,int * status)71 pid_t Platform::Waitpid(pid_t pid, int* status) {
72   return waitpid(pid, status, 0);
73 }
74 
75 }  // namespace brillo
76