The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 22 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 26 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> |
| 28 | #include <selinux/label.h> |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 29 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 30 | #include "uevent.h" |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 31 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 32 | class Permissions { |
| 33 | public: |
| 34 | Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid); |
| 35 | |
| 36 | bool Match(const std::string& path) const; |
| 37 | |
| 38 | mode_t perm() const { return perm_; } |
| 39 | uid_t uid() const { return uid_; } |
| 40 | gid_t gid() const { return gid_; } |
| 41 | |
| 42 | protected: |
| 43 | const std::string& name() const { return name_; } |
| 44 | |
| 45 | private: |
| 46 | std::string name_; |
| 47 | mode_t perm_; |
| 48 | uid_t uid_; |
| 49 | gid_t gid_; |
| 50 | bool prefix_; |
| 51 | bool wildcard_; |
| 52 | }; |
| 53 | |
| 54 | class SysfsPermissions : public Permissions { |
| 55 | public: |
| 56 | SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid, |
| 57 | gid_t gid) |
| 58 | : Permissions(name, perm, uid, gid), attribute_(attribute) {} |
| 59 | |
| 60 | bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const; |
| 61 | void SetPermissions(const std::string& path) const; |
| 62 | |
| 63 | private: |
| 64 | const std::string attribute_; |
| 65 | }; |
| 66 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 67 | class Subsystem { |
| 68 | public: |
| 69 | friend class SubsystemParser; |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 70 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 71 | Subsystem() {} |
| 72 | |
| 73 | // Returns the full path for a uevent of a device that is a member of this subsystem, |
| 74 | // according to the rules parsed from ueventd.rc |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 75 | std::string ParseDevPath(const Uevent& uevent) const { |
| 76 | std::string devname = devname_source_ == DevnameSource::DEVNAME_UEVENT_DEVNAME |
| 77 | ? uevent.device_name |
| 78 | : android::base::Basename(uevent.path); |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 79 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 80 | return dir_name_ + "/" + devname; |
| 81 | } |
| 82 | |
| 83 | bool operator==(const std::string& string_name) const { return name_ == string_name; } |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | enum class DevnameSource { |
| 87 | DEVNAME_UEVENT_DEVNAME, |
| 88 | DEVNAME_UEVENT_DEVPATH, |
| 89 | }; |
| 90 | |
| 91 | std::string name_; |
| 92 | std::string dir_name_ = "/dev"; |
| 93 | DevnameSource devname_source_; |
| 94 | }; |
| 95 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 96 | class PlatformDeviceList { |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 97 | public: |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 98 | void Add(const std::string& path) { platform_devices_.emplace_back(path); } |
| 99 | void Remove(const std::string& path) { |
| 100 | auto it = std::find(platform_devices_.begin(), platform_devices_.end(), path); |
| 101 | if (it != platform_devices_.end()) platform_devices_.erase(it); |
| 102 | } |
| 103 | bool Find(const std::string& path, std::string* out_path) const; |
| 104 | auto size() const { return platform_devices_.size(); } |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 105 | |
| 106 | private: |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 107 | std::vector<std::string> platform_devices_; |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 110 | class DeviceHandler { |
| 111 | public: |
| 112 | friend class DeviceHandlerTester; |
| 113 | |
| 114 | DeviceHandler(); |
| 115 | DeviceHandler(std::vector<Permissions> dev_permissions, |
| 116 | std::vector<SysfsPermissions> sysfs_permissions, |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame^] | 117 | std::vector<Subsystem> subsystems, bool skip_restorecon); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 118 | ~DeviceHandler(){}; |
| 119 | |
| 120 | void HandleDeviceEvent(const Uevent& uevent); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame^] | 121 | |
| 122 | void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const; |
| 123 | |
| 124 | void HandlePlatformDeviceEvent(const Uevent& uevent); |
| 125 | void HandleBlockDeviceEvent(const Uevent& uevent) const; |
| 126 | void HandleGenericDeviceEvent(const Uevent& uevent) const; |
| 127 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 128 | std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame^] | 129 | void set_skip_restorecon(bool value) { skip_restorecon_ = value; } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 130 | |
| 131 | private: |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 132 | std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions( |
| 133 | const std::string& path, const std::vector<std::string>& links) const; |
| 134 | void MakeDevice(const std::string& path, int block, int major, int minor, |
| 135 | const std::vector<std::string>& links) const; |
| 136 | std::vector<std::string> GetCharacterDeviceSymlinks(const Uevent& uevent) const; |
| 137 | void HandleDevice(const std::string& action, const std::string& devpath, int block, int major, |
| 138 | int minor, const std::vector<std::string>& links) const; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 139 | |
| 140 | std::vector<Permissions> dev_permissions_; |
| 141 | std::vector<SysfsPermissions> sysfs_permissions_; |
| 142 | std::vector<Subsystem> subsystems_; |
| 143 | PlatformDeviceList platform_devices_; |
| 144 | selabel_handle* sehandle_; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame^] | 145 | bool skip_restorecon_; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 146 | }; |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 147 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 148 | // Exposed for testing |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 149 | void SanitizePartitionName(std::string* string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 150 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 151 | #endif |