blob: 0be660f80d08697b16f9814d6bcd03d53f15ae8e [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"
Sandeep Patil957e4ab2017-02-07 18:11:37 -080032
Tom Cherry81f5d3e2017-06-22 12:53:17 -070033namespace android {
34namespace init {
35
Tom Cherrycc054c92017-04-05 17:55:46 -070036class Permissions {
37 public:
Tom Cherry5f0198b2018-07-17 15:28:16 -070038 friend void TestPermissions(const Permissions& expected, const Permissions& test);
39
Tom Cherrycc054c92017-04-05 17:55:46 -070040 Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid);
41
42 bool Match(const std::string& path) const;
43
44 mode_t perm() const { return perm_; }
45 uid_t uid() const { return uid_; }
46 gid_t gid() const { return gid_; }
47
48 protected:
49 const std::string& name() const { return name_; }
50
51 private:
52 std::string name_;
53 mode_t perm_;
54 uid_t uid_;
55 gid_t gid_;
56 bool prefix_;
57 bool wildcard_;
58};
59
60class SysfsPermissions : public Permissions {
61 public:
Tom Cherry5f0198b2018-07-17 15:28:16 -070062 friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test);
63
Tom Cherrycc054c92017-04-05 17:55:46 -070064 SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
65 gid_t gid)
66 : Permissions(name, perm, uid, gid), attribute_(attribute) {}
67
68 bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const;
69 void SetPermissions(const std::string& path) const;
70
71 private:
72 const std::string attribute_;
73};
74
Tom Cherryfe062052017-04-24 16:59:05 -070075class Subsystem {
76 public:
77 friend class SubsystemParser;
Tom Cherry5f0198b2018-07-17 15:28:16 -070078 friend void TestSubsystems(const Subsystem& expected, const Subsystem& test);
79
80 enum DevnameSource {
81 DEVNAME_UEVENT_DEVNAME,
82 DEVNAME_UEVENT_DEVPATH,
83 };
Tom Cherrycc054c92017-04-05 17:55:46 -070084
Tom Cherryfe062052017-04-24 16:59:05 -070085 Subsystem() {}
Tom Cherry5f0198b2018-07-17 15:28:16 -070086 Subsystem(const std::string& name) : name_(name) {}
87 Subsystem(const std::string& name, DevnameSource source, const std::string& dir_name)
88 : name_(name), devname_source_(source), dir_name_(dir_name) {}
Tom Cherryfe062052017-04-24 16:59:05 -070089
90 // Returns the full path for a uevent of a device that is a member of this subsystem,
91 // according to the rules parsed from ueventd.rc
Tom Cherryed506f72017-05-25 15:58:59 -070092 std::string ParseDevPath(const Uevent& uevent) const {
Tom Cherry5f0198b2018-07-17 15:28:16 -070093 std::string devname = devname_source_ == DEVNAME_UEVENT_DEVNAME
94 ? uevent.device_name
95 : android::base::Basename(uevent.path);
Tom Cherryfe062052017-04-24 16:59:05 -070096
Tom Cherryed506f72017-05-25 15:58:59 -070097 return dir_name_ + "/" + devname;
98 }
99
100 bool operator==(const std::string& string_name) const { return name_ == string_name; }
Tom Cherryfe062052017-04-24 16:59:05 -0700101
102 private:
Tom Cherryfe062052017-04-24 16:59:05 -0700103 std::string name_;
Tom Cherry5f0198b2018-07-17 15:28:16 -0700104 DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME;
Tom Cherryfe062052017-04-24 16:59:05 -0700105 std::string dir_name_ = "/dev";
Tom Cherryfe062052017-04-24 16:59:05 -0700106};
107
Tom Cherryed506f72017-05-25 15:58:59 -0700108class DeviceHandler {
109 public:
110 friend class DeviceHandlerTester;
111
112 DeviceHandler();
113 DeviceHandler(std::vector<Permissions> dev_permissions,
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800114 std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> subsystems,
115 std::set<std::string> boot_devices, bool skip_restorecon);
Tom Cherryed506f72017-05-25 15:58:59 -0700116
117 void HandleDeviceEvent(const Uevent& uevent);
Tom Cherryc5833052017-05-16 15:35:41 -0700118
Tom Cherryed506f72017-05-25 15:58:59 -0700119 std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
Tom Cherryc5833052017-05-16 15:35:41 -0700120 void set_skip_restorecon(bool value) { skip_restorecon_ = value; }
Tom Cherryed506f72017-05-25 15:58:59 -0700121
122 private:
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700123 bool FindPlatformDevice(std::string path, std::string* platform_device_path) const;
Tom Cherryed506f72017-05-25 15:58:59 -0700124 std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
125 const std::string& path, const std::vector<std::string>& links) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700126 void MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700127 const std::vector<std::string>& links) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700128 void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
Tom Cherryed506f72017-05-25 15:58:59 -0700129 int minor, const std::vector<std::string>& links) const;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700130 void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
131
Tom Cherryed506f72017-05-25 15:58:59 -0700132 std::vector<Permissions> dev_permissions_;
133 std::vector<SysfsPermissions> sysfs_permissions_;
134 std::vector<Subsystem> subsystems_;
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800135 std::set<std::string> boot_devices_;
Tom Cherryc5833052017-05-16 15:35:41 -0700136 bool skip_restorecon_;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700137 std::string sysfs_mount_point_;
Tom Cherryed506f72017-05-25 15:58:59 -0700138};
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800139
Tom Cherryc44f6a42017-04-05 15:58:31 -0700140// Exposed for testing
Tom Cherryed506f72017-05-25 15:58:59 -0700141void SanitizePartitionName(std::string* string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700142
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700143} // namespace init
144} // namespace android
145
Tom Cherryed506f72017-05-25 15:58:59 -0700146#endif