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