1 /* 2 * Copyright (C) 2007 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 #ifndef _INIT_DEVICES_H 18 #define _INIT_DEVICES_H 19 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 23 #include <algorithm> 24 #include <set> 25 #include <string> 26 #include <vector> 27 28 #include <android-base/file.h> 29 #include <selinux/label.h> 30 31 #include "uevent.h" 32 #include "uevent_handler.h" 33 34 namespace android { 35 namespace init { 36 37 class Permissions { 38 public: 39 friend void TestPermissions(const Permissions& expected, const Permissions& test); 40 41 Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid); 42 43 bool Match(const std::string& path) const; 44 perm()45 mode_t perm() const { return perm_; } uid()46 uid_t uid() const { return uid_; } gid()47 gid_t gid() const { return gid_; } 48 49 protected: name()50 const std::string& name() const { return name_; } 51 52 private: 53 std::string name_; 54 mode_t perm_; 55 uid_t uid_; 56 gid_t gid_; 57 bool prefix_; 58 bool wildcard_; 59 }; 60 61 class SysfsPermissions : public Permissions { 62 public: 63 friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test); 64 SysfsPermissions(const std::string & name,const std::string & attribute,mode_t perm,uid_t uid,gid_t gid)65 SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid, 66 gid_t gid) 67 : Permissions(name, perm, uid, gid), attribute_(attribute) {} 68 69 bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const; 70 void SetPermissions(const std::string& path) const; 71 72 private: 73 const std::string attribute_; 74 }; 75 76 class Subsystem { 77 public: 78 friend class SubsystemParser; 79 friend void TestSubsystems(const Subsystem& expected, const Subsystem& test); 80 81 enum DevnameSource { 82 DEVNAME_UEVENT_DEVNAME, 83 DEVNAME_UEVENT_DEVPATH, 84 }; 85 Subsystem()86 Subsystem() {} Subsystem(const std::string & name)87 Subsystem(const std::string& name) : name_(name) {} Subsystem(const std::string & name,DevnameSource source,const std::string & dir_name)88 Subsystem(const std::string& name, DevnameSource source, const std::string& dir_name) 89 : name_(name), devname_source_(source), dir_name_(dir_name) {} 90 91 // Returns the full path for a uevent of a device that is a member of this subsystem, 92 // according to the rules parsed from ueventd.rc ParseDevPath(const Uevent & uevent)93 std::string ParseDevPath(const Uevent& uevent) const { 94 std::string devname = devname_source_ == DEVNAME_UEVENT_DEVNAME 95 ? uevent.device_name 96 : android::base::Basename(uevent.path); 97 98 return dir_name_ + "/" + devname; 99 } 100 101 bool operator==(const std::string& string_name) const { return name_ == string_name; } 102 103 private: 104 std::string name_; 105 DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME; 106 std::string dir_name_ = "/dev"; 107 }; 108 109 class DeviceHandler : public UeventHandler { 110 public: 111 friend class DeviceHandlerTester; 112 113 DeviceHandler(); 114 DeviceHandler(std::vector<Permissions> dev_permissions, 115 std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> subsystems, 116 std::set<std::string> boot_devices, bool skip_restorecon); 117 virtual ~DeviceHandler() = default; 118 119 void HandleUevent(const Uevent& uevent) override; 120 void ColdbootDone() override; 121 122 std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const; 123 124 private: 125 bool FindPlatformDevice(std::string path, std::string* platform_device_path) const; 126 std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions( 127 const std::string& path, const std::vector<std::string>& links) const; 128 void MakeDevice(const std::string& path, bool block, int major, int minor, 129 const std::vector<std::string>& links) const; 130 void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major, 131 int minor, const std::vector<std::string>& links) const; 132 void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const; 133 134 std::vector<Permissions> dev_permissions_; 135 std::vector<SysfsPermissions> sysfs_permissions_; 136 std::vector<Subsystem> subsystems_; 137 std::set<std::string> boot_devices_; 138 bool skip_restorecon_; 139 std::string sysfs_mount_point_; 140 }; 141 142 // Exposed for testing 143 void SanitizePartitionName(std::string* string); 144 145 } // namespace init 146 } // namespace android 147 148 #endif 149