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> |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 24 | #include <set> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 27 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 28 | #include <android-base/file.h> |
| 29 | #include <selinux/label.h> |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 30 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 31 | #include "uevent.h" |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 32 | #include "uevent_handler.h" |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 33 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace init { |
| 36 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 37 | class Permissions { |
| 38 | public: |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 39 | friend void TestPermissions(const Permissions& expected, const Permissions& test); |
| 40 | |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 41 | Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid, bool no_fnm_pathname); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 42 | |
| 43 | bool Match(const std::string& path) const; |
| 44 | |
| 45 | mode_t perm() const { return perm_; } |
| 46 | uid_t uid() const { return uid_; } |
| 47 | gid_t gid() const { return gid_; } |
| 48 | |
| 49 | protected: |
| 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_; |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 59 | bool no_fnm_pathname_; |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | class SysfsPermissions : public Permissions { |
| 63 | public: |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 64 | friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test); |
| 65 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 66 | SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid, |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 67 | gid_t gid, bool no_fnm_pathname) |
| 68 | : Permissions(name, perm, uid, gid, no_fnm_pathname), attribute_(attribute) {} |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 69 | |
| 70 | bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const; |
| 71 | void SetPermissions(const std::string& path) const; |
| 72 | |
| 73 | private: |
| 74 | const std::string attribute_; |
| 75 | }; |
| 76 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 77 | class Subsystem { |
| 78 | public: |
| 79 | friend class SubsystemParser; |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 80 | friend void TestSubsystems(const Subsystem& expected, const Subsystem& test); |
| 81 | |
| 82 | enum DevnameSource { |
| 83 | DEVNAME_UEVENT_DEVNAME, |
| 84 | DEVNAME_UEVENT_DEVPATH, |
| 85 | }; |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 86 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 87 | Subsystem() {} |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 88 | Subsystem(std::string name) : name_(std::move(name)) {} |
| 89 | Subsystem(std::string name, DevnameSource source, std::string dir_name) |
| 90 | : name_(std::move(name)), devname_source_(source), dir_name_(std::move(dir_name)) {} |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 91 | |
| 92 | // Returns the full path for a uevent of a device that is a member of this subsystem, |
| 93 | // according to the rules parsed from ueventd.rc |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 94 | std::string ParseDevPath(const Uevent& uevent) const { |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 95 | std::string devname = devname_source_ == DEVNAME_UEVENT_DEVNAME |
| 96 | ? uevent.device_name |
| 97 | : android::base::Basename(uevent.path); |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 98 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 99 | return dir_name_ + "/" + devname; |
| 100 | } |
| 101 | |
| 102 | bool operator==(const std::string& string_name) const { return name_ == string_name; } |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 103 | |
| 104 | private: |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 105 | std::string name_; |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 106 | DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME; |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 107 | std::string dir_name_ = "/dev"; |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 110 | class DeviceHandler : public UeventHandler { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 111 | public: |
| 112 | friend class DeviceHandlerTester; |
| 113 | |
| 114 | DeviceHandler(); |
| 115 | DeviceHandler(std::vector<Permissions> dev_permissions, |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 116 | std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> subsystems, |
| 117 | std::set<std::string> boot_devices, bool skip_restorecon); |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 118 | virtual ~DeviceHandler() = default; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 119 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 120 | void HandleUevent(const Uevent& uevent) override; |
| 121 | void ColdbootDone() override; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 122 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 123 | std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const; |
| 124 | |
Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 125 | // `androidboot.partition_map` allows associating a partition name for a raw block device |
| 126 | // through a comma separated and semicolon deliminated list. For example, |
| 127 | // `androidboot.partition_map=vdb,metadata;vdc,userdata` maps `vdb` to `metadata` and `vdc` to |
| 128 | // `userdata`. |
| 129 | static std::string GetPartitionNameForDevice(const std::string& device); |
| 130 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 131 | private: |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 132 | bool FindPlatformDevice(std::string path, std::string* platform_device_path) const; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 133 | std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions( |
| 134 | const std::string& path, const std::vector<std::string>& links) const; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 135 | void MakeDevice(const std::string& path, bool block, int major, int minor, |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 136 | const std::vector<std::string>& links) const; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 137 | void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major, |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 138 | int minor, const std::vector<std::string>& links) const; |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 139 | void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const; |
Tri Vo | ff89b8d | 2019-09-24 13:00:43 -0700 | [diff] [blame] | 140 | void HandleAshmemUevent(const Uevent& uevent); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 141 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 142 | std::vector<Permissions> dev_permissions_; |
| 143 | std::vector<SysfsPermissions> sysfs_permissions_; |
| 144 | std::vector<Subsystem> subsystems_; |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 145 | std::set<std::string> boot_devices_; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 146 | bool skip_restorecon_; |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 147 | std::string sysfs_mount_point_; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 148 | }; |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 149 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 150 | // Exposed for testing |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 151 | void SanitizePartitionName(std::string* string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 152 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 153 | } // namespace init |
| 154 | } // namespace android |
| 155 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 156 | #endif |