xref: /aosp_15_r20/system/core/init/devices.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 <map>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 #include <android-base/file.h>
30 #include <selinux/label.h>
31 
32 #include "uevent.h"
33 #include "uevent_handler.h"
34 
35 namespace android {
36 namespace init {
37 
38 class Permissions {
39   public:
40     friend void TestPermissions(const Permissions& expected, const Permissions& test);
41 
42     Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid, bool no_fnm_pathname);
43 
44     bool Match(const std::string& path) const;
45 
perm()46     mode_t perm() const { return perm_; }
uid()47     uid_t uid() const { return uid_; }
gid()48     gid_t gid() const { return gid_; }
49 
50   protected:
name()51     const std::string& name() const { return name_; }
52 
53   private:
54     std::string name_;
55     mode_t perm_;
56     uid_t uid_;
57     gid_t gid_;
58     bool prefix_;
59     bool wildcard_;
60     bool no_fnm_pathname_;
61 };
62 
63 class SysfsPermissions : public Permissions {
64   public:
65     friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test);
66 
SysfsPermissions(const std::string & name,const std::string & attribute,mode_t perm,uid_t uid,gid_t gid,bool no_fnm_pathname)67     SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
68                      gid_t gid, bool no_fnm_pathname)
69         : Permissions(name, perm, uid, gid, no_fnm_pathname), attribute_(attribute) {}
70 
71     bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const;
72     void SetPermissions(const std::string& path) const;
73 
74   private:
75     const std::string attribute_;
76 };
77 
78 class Subsystem {
79   public:
80     friend class SubsystemParser;
81     friend void TestSubsystems(const Subsystem& expected, const Subsystem& test);
82 
83     enum DevnameSource {
84         DEVNAME_UEVENT_DEVNAME,
85         DEVNAME_UEVENT_DEVPATH,
86         DEVNAME_SYS_NAME,
87     };
88 
Subsystem()89     Subsystem() {}
Subsystem(std::string name)90     Subsystem(std::string name) : name_(std::move(name)) {}
Subsystem(std::string name,DevnameSource source,std::string dir_name)91     Subsystem(std::string name, DevnameSource source, std::string dir_name)
92         : name_(std::move(name)), devname_source_(source), dir_name_(std::move(dir_name)) {}
93 
94     // Returns the full path for a uevent of a device that is a member of this subsystem,
95     // according to the rules parsed from ueventd.rc
ParseDevPath(const Uevent & uevent)96     std::string ParseDevPath(const Uevent& uevent) const {
97         std::string devname;
98         if (devname_source_ == DEVNAME_UEVENT_DEVNAME) {
99             devname = uevent.device_name;
100         } else if (devname_source_ == DEVNAME_UEVENT_DEVPATH) {
101             devname = android::base::Basename(uevent.path);
102         } else if (devname_source_ == DEVNAME_SYS_NAME) {
103             if (android::base::ReadFileToString("/sys/" + uevent.path + "/name", &devname)) {
104                 devname.pop_back();  // Remove terminating newline
105             } else {
106                 devname = uevent.device_name;
107             }
108         }
109         return dir_name_ + "/" + devname;
110     }
111 
112     bool operator==(const std::string& string_name) const { return name_ == string_name; }
113 
114   private:
115     std::string name_;
116     DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME;
117     std::string dir_name_ = "/dev";
118 };
119 
120 struct BlockDeviceInfo {
121     std::string str;
122     std::string type;
123     bool is_boot_device;
124 };
125 
126 class DeviceHandler : public UeventHandler {
127   public:
128     friend class DeviceHandlerTester;
129 
130     DeviceHandler();
131     DeviceHandler(std::vector<Permissions> dev_permissions,
132                   std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> drivers,
133                   std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
134                   std::string boot_part_uuid, bool skip_restorecon);
135     virtual ~DeviceHandler() = default;
136 
137     bool CheckUeventForBootPartUuid(const Uevent& uevent);
138     void HandleUevent(const Uevent& uevent) override;
139 
140     // `androidboot.partition_map` allows associating a partition name for a raw block device
141     // through a comma separated and semicolon deliminated list. For example,
142     // `androidboot.partition_map=vdb,metadata;vdc,userdata` maps `vdb` to `metadata` and `vdc` to
143     // `userdata`.
144     static std::string GetPartitionNameForDevice(const std::string& device);
145     bool IsBootDeviceStrict() const;
146     bool IsBootDevice(const Uevent& uevent) const;
147 
148   private:
149     struct TrackedUevent {
150         Uevent uevent;
151         std::string canonical_device_path;
152     };
153 
154     void ColdbootDone() override;
155     BlockDeviceInfo GetBlockDeviceInfo(const std::string& uevent_path) const;
156     bool FindSubsystemDevice(std::string path, std::string* device_path,
157                              const std::set<std::string>& subsystem_paths) const;
158     bool FindPlatformDevice(const std::string& path, std::string* platform_device_path) const;
159     bool FindMmcDevice(const std::string& path, std::string* mmc_device_path) const;
160     bool FindNvmeDevice(const std::string& path, std::string* nvme_device_path) const;
161     bool FindScsiDevice(const std::string& path, std::string* scsi_device_path) const;
162     std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
163         const std::string& path, const std::vector<std::string>& links) const;
164     void MakeDevice(const std::string& path, bool block, int major, int minor,
165                     const std::vector<std::string>& links) const;
166     std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
167     void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
168                       int minor, const std::vector<std::string>& links) const;
169     void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
170     void HandleAshmemUevent(const Uevent& uevent);
171 
172     void TrackDeviceUevent(const Uevent& uevent);
173     void HandleBindInternal(std::string driver_name, std::string action, const Uevent& uevent);
174 
175     std::vector<Permissions> dev_permissions_;
176     std::vector<SysfsPermissions> sysfs_permissions_;
177     std::vector<Subsystem> drivers_;
178     std::vector<Subsystem> subsystems_;
179     std::set<std::string> boot_devices_;
180     std::string boot_part_uuid_;
181     bool found_boot_part_uuid_;
182     bool skip_restorecon_;
183     std::string sysfs_mount_point_;
184 
185     std::vector<TrackedUevent> tracked_uevents_;
186     std::map<std::string, std::string> bound_drivers_;
187 };
188 
189 // Exposed for testing
190 void SanitizePartitionName(std::string* string);
191 
192 }  // namespace init
193 }  // namespace android
194 
195 #endif
196