blob: 1f8f1e8a9f4bbd8022ad10f169c0b710d45cb8ab [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>
Tom Cherry2e344f92017-04-04 17:53:45 -070024#include <string>
25#include <vector>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070026
Tom Cherryed506f72017-05-25 15:58:59 -070027#include <android-base/file.h>
28#include <selinux/label.h>
Tom Cherryfe062052017-04-24 16:59:05 -070029
Tom Cherryed506f72017-05-25 15:58:59 -070030#include "uevent.h"
Sandeep Patil957e4ab2017-02-07 18:11:37 -080031
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032namespace android {
33namespace init {
34
Tom Cherrycc054c92017-04-05 17:55:46 -070035class Permissions {
36 public:
37 Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid);
38
39 bool Match(const std::string& path) const;
40
41 mode_t perm() const { return perm_; }
42 uid_t uid() const { return uid_; }
43 gid_t gid() const { return gid_; }
44
45 protected:
46 const std::string& name() const { return name_; }
47
48 private:
49 std::string name_;
50 mode_t perm_;
51 uid_t uid_;
52 gid_t gid_;
53 bool prefix_;
54 bool wildcard_;
55};
56
57class SysfsPermissions : public Permissions {
58 public:
59 SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
60 gid_t gid)
61 : Permissions(name, perm, uid, gid), attribute_(attribute) {}
62
63 bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const;
64 void SetPermissions(const std::string& path) const;
65
66 private:
67 const std::string attribute_;
68};
69
Tom Cherryfe062052017-04-24 16:59:05 -070070class Subsystem {
71 public:
72 friend class SubsystemParser;
Tom Cherrycc054c92017-04-05 17:55:46 -070073
Tom Cherryfe062052017-04-24 16:59:05 -070074 Subsystem() {}
Tom Cherry9c8d6dd2017-08-17 09:38:01 -070075 Subsystem(std::string name) : name_(std::move(name)) {}
Tom Cherryfe062052017-04-24 16:59:05 -070076
77 // Returns the full path for a uevent of a device that is a member of this subsystem,
78 // according to the rules parsed from ueventd.rc
Tom Cherryed506f72017-05-25 15:58:59 -070079 std::string ParseDevPath(const Uevent& uevent) const {
80 std::string devname = devname_source_ == DevnameSource::DEVNAME_UEVENT_DEVNAME
81 ? uevent.device_name
82 : android::base::Basename(uevent.path);
Tom Cherryfe062052017-04-24 16:59:05 -070083
Tom Cherryed506f72017-05-25 15:58:59 -070084 return dir_name_ + "/" + devname;
85 }
86
87 bool operator==(const std::string& string_name) const { return name_ == string_name; }
Tom Cherryfe062052017-04-24 16:59:05 -070088
89 private:
90 enum class DevnameSource {
91 DEVNAME_UEVENT_DEVNAME,
92 DEVNAME_UEVENT_DEVPATH,
93 };
94
95 std::string name_;
96 std::string dir_name_ = "/dev";
97 DevnameSource devname_source_;
98};
99
Tom Cherryed506f72017-05-25 15:58:59 -0700100class DeviceHandler {
101 public:
102 friend class DeviceHandlerTester;
103
104 DeviceHandler();
105 DeviceHandler(std::vector<Permissions> dev_permissions,
106 std::vector<SysfsPermissions> sysfs_permissions,
Tom Cherryc5833052017-05-16 15:35:41 -0700107 std::vector<Subsystem> subsystems, bool skip_restorecon);
Tom Cherryed506f72017-05-25 15:58:59 -0700108 ~DeviceHandler(){};
109
110 void HandleDeviceEvent(const Uevent& uevent);
Tom Cherryc5833052017-05-16 15:35:41 -0700111
Tom Cherryed506f72017-05-25 15:58:59 -0700112 std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
Tom Cherryc5833052017-05-16 15:35:41 -0700113 void set_skip_restorecon(bool value) { skip_restorecon_ = value; }
Tom Cherryed506f72017-05-25 15:58:59 -0700114
115 private:
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700116 bool FindPlatformDevice(std::string path, std::string* platform_device_path) const;
Tom Cherryed506f72017-05-25 15:58:59 -0700117 std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
118 const std::string& path, const std::vector<std::string>& links) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700119 void MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700120 const std::vector<std::string>& links) const;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700121 void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
Tom Cherryed506f72017-05-25 15:58:59 -0700122 int minor, const std::vector<std::string>& links) const;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700123 void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
124
Tom Cherryed506f72017-05-25 15:58:59 -0700125 std::vector<Permissions> dev_permissions_;
126 std::vector<SysfsPermissions> sysfs_permissions_;
127 std::vector<Subsystem> subsystems_;
Tom Cherryc5833052017-05-16 15:35:41 -0700128 bool skip_restorecon_;
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700129 std::string sysfs_mount_point_;
Tom Cherryed506f72017-05-25 15:58:59 -0700130};
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800131
Tom Cherryc44f6a42017-04-05 15:58:31 -0700132// Exposed for testing
Tom Cherryed506f72017-05-25 15:58:59 -0700133void SanitizePartitionName(std::string* string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700134
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700135} // namespace init
136} // namespace android
137
Tom Cherryed506f72017-05-25 15:58:59 -0700138#endif