blob: 69a2449788007348eacb46af2489c3ee2b89012d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Crossf83d0b92010-04-21 12:04:20 -070020#include <sys/stat.h>
Tom Cherrycc054c92017-04-05 17:55:46 -070021#include <sys/types.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070022
Tom Cherryed506f72017-05-25 15:58:59 -070023#include <algorithm>
Eric Carusod17d5c52024-10-28 15:30:11 -040024#include <map>
Bowgo Tsai8eec38f2018-05-16 18:33:44 +080025#include <set>
Tom Cherry2e344f92017-04-04 17:53:45 -070026#include <string>
27#include <vector>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070028
Tom Cherryed506f72017-05-25 15:58:59 -070029#include <android-base/file.h>
30#include <selinux/label.h>
Tom Cherryfe062052017-04-24 16:59:05 -070031
Tom Cherryed506f72017-05-25 15:58:59 -070032#include "uevent.h"
Tom Cherry457e28f2018-08-01 13:12:20 -070033#include "uevent_handler.h"
Sandeep Patil957e4ab2017-02-07 18:11:37 -080034
Tom Cherry81f5d3e2017-06-22 12:53:17 -070035namespace android {
36namespace init {
37
Tom Cherrycc054c92017-04-05 17:55:46 -070038class Permissions {
39 public:
Tom Cherry5f0198b2018-07-17 15:28:16 -070040 friend void TestPermissions(const Permissions& expected, const Permissions& test);
41
Tom Cherry47031c82020-12-07 13:33:46 -080042 Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid, bool no_fnm_pathname);
Tom Cherrycc054c92017-04-05 17:55:46 -070043
44 bool Match(const std::string& path) const;
45
46 mode_t perm() const { return perm_; }
47 uid_t uid() const { return uid_; }
48 gid_t gid() const { return gid_; }
49
50 protected:
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_;
Tom Cherry47031c82020-12-07 13:33:46 -080060 bool no_fnm_pathname_;
Tom Cherrycc054c92017-04-05 17:55:46 -070061};
62
63class SysfsPermissions : public Permissions {
64 public:
Tom Cherry5f0198b2018-07-17 15:28:16 -070065 friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test);
66
Tom Cherrycc054c92017-04-05 17:55:46 -070067 SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
Tom Cherry47031c82020-12-07 13:33:46 -080068 gid_t gid, bool no_fnm_pathname)
69 : Permissions(name, perm, uid, gid, no_fnm_pathname), attribute_(attribute) {}
Tom Cherrycc054c92017-04-05 17:55:46 -070070
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
Tom Cherryfe062052017-04-24 16:59:05 -070078class Subsystem {
79 public:
80 friend class SubsystemParser;
Tom Cherry5f0198b2018-07-17 15:28:16 -070081 friend void TestSubsystems(const Subsystem& expected, const Subsystem& test);
82
83 enum DevnameSource {
84 DEVNAME_UEVENT_DEVNAME,
85 DEVNAME_UEVENT_DEVPATH,
A. Cody Schuffelenb4796662024-04-23 18:59:00 -070086 DEVNAME_SYS_NAME,
Tom Cherry5f0198b2018-07-17 15:28:16 -070087 };
Tom Cherrycc054c92017-04-05 17:55:46 -070088
Tom Cherryfe062052017-04-24 16:59:05 -070089 Subsystem() {}
Tom Cherry7c1d87e2019-07-10 11:18:24 -070090 Subsystem(std::string name) : name_(std::move(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)) {}
Tom Cherryfe062052017-04-24 16:59:05 -070093
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
Tom Cherryed506f72017-05-25 15:58:59 -070096 std::string ParseDevPath(const Uevent& uevent) const {
A. Cody Schuffelenb4796662024-04-23 18:59:00 -070097 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 }
Tom Cherryed506f72017-05-25 15:58:59 -0700109 return dir_name_ + "/" + devname;
110 }
111
112 bool operator==(const std::string& string_name) const { return name_ == string_name; }
Tom Cherryfe062052017-04-24 16:59:05 -0700113
114 private:
Tom Cherryfe062052017-04-24 16:59:05 -0700115 std::string name_;
Tom Cherry5f0198b2018-07-17 15:28:16 -0700116 DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME;
Tom Cherryfe062052017-04-24 16:59:05 -0700117 std::string dir_name_ = "/dev";
Tom Cherryfe062052017-04-24 16:59:05 -0700118};
119
Douglas Anderson9481f972024-11-06 09:28:07 -0800120struct BlockDeviceInfo {
121 std::string str;
122 std::string type;
123 bool is_boot_device;
124};
125
Tom Cherry457e28f2018-08-01 13:12:20 -0700126class DeviceHandler : public UeventHandler {
Tom Cherryed506f72017-05-25 15:58:59 -0700127 public:
128 friend class DeviceHandlerTester;
129
130 DeviceHandler();
131 DeviceHandler(std::vector<Permissions> dev_permissions,
Eric Carusod17d5c52024-10-28 15:30:11 -0400132 std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> drivers,
Douglas Andersone9de3102024-10-22 14:34:30 -0700133 std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
134 std::string boot_part_uuid, bool skip_restorecon);
Tom Cherry457e28f2018-08-01 13:12:20 -0700135 virtual ~DeviceHandler() = default;
Tom Cherryed506f72017-05-25 15:58:59 -0700136
Douglas Andersone9de3102024-10-22 14:34:30 -0700137 bool CheckUeventForBootPartUuid(const Uevent& uevent);
Tom Cherry457e28f2018-08-01 13:12:20 -0700138 void HandleUevent(const Uevent& uevent) override;
Tom Cherryed506f72017-05-25 15:58:59 -0700139
Tom Cherry96e5f9b2021-07-30 09:54:36 -0700140 // `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);
Douglas Andersoneb3d2802024-11-05 09:15:25 -0800145 bool IsBootDeviceStrict() const;
146 bool IsBootDevice(const Uevent& uevent) const;
Tom Cherry96e5f9b2021-07-30 09:54:36 -0700147
Tom Cherryed506f72017-05-25 15:58:59 -0700148 private:
Eric Carusod17d5c52024-10-28 15:30:11 -0400149 struct TrackedUevent {
150 Uevent uevent;
151 std::string canonical_device_path;
152 };
153
Bart Van Assche254436b2024-05-23 11:14:50 -0700154 void ColdbootDone() override;
Douglas Anderson9481f972024-11-06 09:28:07 -0800155 BlockDeviceInfo GetBlockDeviceInfo(const std::string& uevent_path) const;
Douglas Anderson6519e6d2024-10-22 14:09:26 -0700156 bool FindSubsystemDevice(std::string path, std::string* device_path,
157 const std::set<std::string>& subsystem_paths) const;
Douglas Anderson46afe222024-11-08 14:38:41 -0800158 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;
Douglas Andersondd8edea2024-11-14 13:53:54 -0800160 bool FindNvmeDevice(const std::string& path, std::string* nvme_device_path) const;
Douglas Anderson46afe222024-11-08 14:38:41 -0800161 bool FindScsiDevice(const std::string& path, std::string* scsi_device_path) const;
Tom Cherryed506f72017-05-25 15:58:59 -0700162 std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
163 const std::string& path, const std::vector<std::string>& links) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700164 void MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700165 const std::vector<std::string>& links) const;
Bart Van Assche254436b2024-05-23 11:14:50 -0700166 std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700167 void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
Tom Cherryed506f72017-05-25 15:58:59 -0700168 int minor, const std::vector<std::string>& links) const;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700169 void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
Tri Voff89b8d2019-09-24 13:00:43 -0700170 void HandleAshmemUevent(const Uevent& uevent);
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700171
Eric Carusod17d5c52024-10-28 15:30:11 -0400172 void TrackDeviceUevent(const Uevent& uevent);
173 void HandleBindInternal(std::string driver_name, std::string action, const Uevent& uevent);
174
Tom Cherryed506f72017-05-25 15:58:59 -0700175 std::vector<Permissions> dev_permissions_;
176 std::vector<SysfsPermissions> sysfs_permissions_;
Eric Carusod17d5c52024-10-28 15:30:11 -0400177 std::vector<Subsystem> drivers_;
Tom Cherryed506f72017-05-25 15:58:59 -0700178 std::vector<Subsystem> subsystems_;
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800179 std::set<std::string> boot_devices_;
Douglas Andersone9de3102024-10-22 14:34:30 -0700180 std::string boot_part_uuid_;
181 bool found_boot_part_uuid_;
Tom Cherryc5833052017-05-16 15:35:41 -0700182 bool skip_restorecon_;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700183 std::string sysfs_mount_point_;
Eric Carusod17d5c52024-10-28 15:30:11 -0400184
185 std::vector<TrackedUevent> tracked_uevents_;
186 std::map<std::string, std::string> bound_drivers_;
Tom Cherryed506f72017-05-25 15:58:59 -0700187};
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800188
Tom Cherryc44f6a42017-04-05 15:58:31 -0700189// Exposed for testing
Tom Cherryed506f72017-05-25 15:58:59 -0700190void SanitizePartitionName(std::string* string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700191
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700192} // namespace init
193} // namespace android
194
Tom Cherryed506f72017-05-25 15:58:59 -0700195#endif