blob: 67a3d00b1d474f06c1ed9a843c988375c7d924bb [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>
Bowgo Tsai8eec38f2018-05-16 18:33:44 +080024#include <set>
Tom Cherry2e344f92017-04-04 17:53:45 -070025#include <string>
26#include <vector>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070027
Tom Cherryed506f72017-05-25 15:58:59 -070028#include <android-base/file.h>
29#include <selinux/label.h>
Tom Cherryfe062052017-04-24 16:59:05 -070030
Tom Cherryed506f72017-05-25 15:58:59 -070031#include "uevent.h"
Tom Cherry457e28f2018-08-01 13:12:20 -070032#include "uevent_handler.h"
Sandeep Patil957e4ab2017-02-07 18:11:37 -080033
Tom Cherry81f5d3e2017-06-22 12:53:17 -070034namespace android {
35namespace init {
36
Tom Cherrycc054c92017-04-05 17:55:46 -070037class Permissions {
38 public:
Tom Cherry5f0198b2018-07-17 15:28:16 -070039 friend void TestPermissions(const Permissions& expected, const Permissions& test);
40
Tom Cherry47031c82020-12-07 13:33:46 -080041 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 -070042
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 Cherry47031c82020-12-07 13:33:46 -080059 bool no_fnm_pathname_;
Tom Cherrycc054c92017-04-05 17:55:46 -070060};
61
62class SysfsPermissions : public Permissions {
63 public:
Tom Cherry5f0198b2018-07-17 15:28:16 -070064 friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test);
65
Tom Cherrycc054c92017-04-05 17:55:46 -070066 SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
Tom Cherry47031c82020-12-07 13:33:46 -080067 gid_t gid, bool no_fnm_pathname)
68 : Permissions(name, perm, uid, gid, no_fnm_pathname), attribute_(attribute) {}
Tom Cherrycc054c92017-04-05 17:55:46 -070069
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 Cherryfe062052017-04-24 16:59:05 -070077class Subsystem {
78 public:
79 friend class SubsystemParser;
Tom Cherry5f0198b2018-07-17 15:28:16 -070080 friend void TestSubsystems(const Subsystem& expected, const Subsystem& test);
81
82 enum DevnameSource {
83 DEVNAME_UEVENT_DEVNAME,
84 DEVNAME_UEVENT_DEVPATH,
A. Cody Schuffelenb4796662024-04-23 18:59:00 -070085 DEVNAME_SYS_NAME,
Tom Cherry5f0198b2018-07-17 15:28:16 -070086 };
Tom Cherrycc054c92017-04-05 17:55:46 -070087
Tom Cherryfe062052017-04-24 16:59:05 -070088 Subsystem() {}
Tom Cherry7c1d87e2019-07-10 11:18:24 -070089 Subsystem(std::string name) : name_(std::move(name)) {}
90 Subsystem(std::string name, DevnameSource source, std::string dir_name)
91 : name_(std::move(name)), devname_source_(source), dir_name_(std::move(dir_name)) {}
Tom Cherryfe062052017-04-24 16:59:05 -070092
93 // Returns the full path for a uevent of a device that is a member of this subsystem,
94 // according to the rules parsed from ueventd.rc
Tom Cherryed506f72017-05-25 15:58:59 -070095 std::string ParseDevPath(const Uevent& uevent) const {
A. Cody Schuffelenb4796662024-04-23 18:59:00 -070096 std::string devname;
97 if (devname_source_ == DEVNAME_UEVENT_DEVNAME) {
98 devname = uevent.device_name;
99 } else if (devname_source_ == DEVNAME_UEVENT_DEVPATH) {
100 devname = android::base::Basename(uevent.path);
101 } else if (devname_source_ == DEVNAME_SYS_NAME) {
102 if (android::base::ReadFileToString("/sys/" + uevent.path + "/name", &devname)) {
103 devname.pop_back(); // Remove terminating newline
104 } else {
105 devname = uevent.device_name;
106 }
107 }
Tom Cherryed506f72017-05-25 15:58:59 -0700108 return dir_name_ + "/" + devname;
109 }
110
111 bool operator==(const std::string& string_name) const { return name_ == string_name; }
Tom Cherryfe062052017-04-24 16:59:05 -0700112
113 private:
Tom Cherryfe062052017-04-24 16:59:05 -0700114 std::string name_;
Tom Cherry5f0198b2018-07-17 15:28:16 -0700115 DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME;
Tom Cherryfe062052017-04-24 16:59:05 -0700116 std::string dir_name_ = "/dev";
Tom Cherryfe062052017-04-24 16:59:05 -0700117};
118
Douglas Anderson9481f972024-11-06 09:28:07 -0800119struct BlockDeviceInfo {
120 std::string str;
121 std::string type;
122 bool is_boot_device;
123};
124
Tom Cherry457e28f2018-08-01 13:12:20 -0700125class DeviceHandler : public UeventHandler {
Tom Cherryed506f72017-05-25 15:58:59 -0700126 public:
127 friend class DeviceHandlerTester;
128
129 DeviceHandler();
130 DeviceHandler(std::vector<Permissions> dev_permissions,
Douglas Andersone9de3102024-10-22 14:34:30 -0700131 std::vector<SysfsPermissions> sysfs_permissions,
132 std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
133 std::string boot_part_uuid, bool skip_restorecon);
Tom Cherry457e28f2018-08-01 13:12:20 -0700134 virtual ~DeviceHandler() = default;
Tom Cherryed506f72017-05-25 15:58:59 -0700135
Douglas Andersone9de3102024-10-22 14:34:30 -0700136 bool CheckUeventForBootPartUuid(const Uevent& uevent);
Tom Cherry457e28f2018-08-01 13:12:20 -0700137 void HandleUevent(const Uevent& uevent) override;
Tom Cherryed506f72017-05-25 15:58:59 -0700138
Tom Cherry96e5f9b2021-07-30 09:54:36 -0700139 // `androidboot.partition_map` allows associating a partition name for a raw block device
140 // through a comma separated and semicolon deliminated list. For example,
141 // `androidboot.partition_map=vdb,metadata;vdc,userdata` maps `vdb` to `metadata` and `vdc` to
142 // `userdata`.
143 static std::string GetPartitionNameForDevice(const std::string& device);
Douglas Andersoneb3d2802024-11-05 09:15:25 -0800144 bool IsBootDeviceStrict() const;
145 bool IsBootDevice(const Uevent& uevent) const;
Tom Cherry96e5f9b2021-07-30 09:54:36 -0700146
Tom Cherryed506f72017-05-25 15:58:59 -0700147 private:
Bart Van Assche254436b2024-05-23 11:14:50 -0700148 void ColdbootDone() override;
Douglas Anderson9481f972024-11-06 09:28:07 -0800149 BlockDeviceInfo GetBlockDeviceInfo(const std::string& uevent_path) const;
Douglas Anderson6519e6d2024-10-22 14:09:26 -0700150 bool FindSubsystemDevice(std::string path, std::string* device_path,
151 const std::set<std::string>& subsystem_paths) const;
Douglas Anderson46afe222024-11-08 14:38:41 -0800152 bool FindPlatformDevice(const std::string& path, std::string* platform_device_path) const;
153 bool FindMmcDevice(const std::string& path, std::string* mmc_device_path) const;
154 bool FindScsiDevice(const std::string& path, std::string* scsi_device_path) const;
Tom Cherryed506f72017-05-25 15:58:59 -0700155 std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
156 const std::string& path, const std::vector<std::string>& links) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700157 void MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700158 const std::vector<std::string>& links) const;
Bart Van Assche254436b2024-05-23 11:14:50 -0700159 std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700160 void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
Tom Cherryed506f72017-05-25 15:58:59 -0700161 int minor, const std::vector<std::string>& links) const;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700162 void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
Tri Voff89b8d2019-09-24 13:00:43 -0700163 void HandleAshmemUevent(const Uevent& uevent);
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700164
Tom Cherryed506f72017-05-25 15:58:59 -0700165 std::vector<Permissions> dev_permissions_;
166 std::vector<SysfsPermissions> sysfs_permissions_;
167 std::vector<Subsystem> subsystems_;
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800168 std::set<std::string> boot_devices_;
Douglas Andersone9de3102024-10-22 14:34:30 -0700169 std::string boot_part_uuid_;
170 bool found_boot_part_uuid_;
Tom Cherryc5833052017-05-16 15:35:41 -0700171 bool skip_restorecon_;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700172 std::string sysfs_mount_point_;
Tom Cherryed506f72017-05-25 15:58:59 -0700173};
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800174
Tom Cherryc44f6a42017-04-05 15:58:31 -0700175// Exposed for testing
Tom Cherryed506f72017-05-25 15:58:59 -0700176void SanitizePartitionName(std::string* string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700177
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700178} // namespace init
179} // namespace android
180
Tom Cherryed506f72017-05-25 15:58:59 -0700181#endif