• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
25 #include <vector>
26 
27 #include <android-base/file.h>
28 #include <selinux/label.h>
29 
30 #include "uevent.h"
31 
32 namespace android {
33 namespace init {
34 
35 class 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 
perm()41     mode_t perm() const { return perm_; }
uid()42     uid_t uid() const { return uid_; }
gid()43     gid_t gid() const { return gid_; }
44 
45   protected:
name()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 
57 class SysfsPermissions : public Permissions {
58   public:
SysfsPermissions(const std::string & name,const std::string & attribute,mode_t perm,uid_t uid,gid_t gid)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 
70 class Subsystem {
71   public:
72     friend class SubsystemParser;
73 
Subsystem()74     Subsystem() {}
75 
76     // Returns the full path for a uevent of a device that is a member of this subsystem,
77     // according to the rules parsed from ueventd.rc
ParseDevPath(const Uevent & uevent)78     std::string ParseDevPath(const Uevent& uevent) const {
79         std::string devname = devname_source_ == DevnameSource::DEVNAME_UEVENT_DEVNAME
80                                   ? uevent.device_name
81                                   : android::base::Basename(uevent.path);
82 
83         return dir_name_ + "/" + devname;
84     }
85 
86     bool operator==(const std::string& string_name) const { return name_ == string_name; }
87 
88   private:
89     enum class DevnameSource {
90         DEVNAME_UEVENT_DEVNAME,
91         DEVNAME_UEVENT_DEVPATH,
92     };
93 
94     std::string name_;
95     std::string dir_name_ = "/dev";
96     DevnameSource devname_source_;
97 };
98 
99 class DeviceHandler {
100   public:
101     friend class DeviceHandlerTester;
102 
103     DeviceHandler();
104     DeviceHandler(std::vector<Permissions> dev_permissions,
105                   std::vector<SysfsPermissions> sysfs_permissions,
106                   std::vector<Subsystem> subsystems, bool skip_restorecon);
~DeviceHandler()107     ~DeviceHandler(){};
108 
109     void HandleDeviceEvent(const Uevent& uevent);
110 
111     std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
set_skip_restorecon(bool value)112     void set_skip_restorecon(bool value) { skip_restorecon_ = value; }
113 
114   private:
115     bool FindPlatformDevice(std::string path, std::string* platform_device_path) const;
116     std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
117         const std::string& path, const std::vector<std::string>& links) const;
118     void MakeDevice(const std::string& path, bool block, int major, int minor,
119                     const std::vector<std::string>& links) const;
120     void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
121                       int minor, const std::vector<std::string>& links) const;
122     void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
123 
124     std::vector<Permissions> dev_permissions_;
125     std::vector<SysfsPermissions> sysfs_permissions_;
126     std::vector<Subsystem> subsystems_;
127     selabel_handle* sehandle_;
128     bool skip_restorecon_;
129     std::string sysfs_mount_point_;
130 };
131 
132 // Exposed for testing
133 void SanitizePartitionName(std::string* string);
134 
135 }  // namespace init
136 }  // namespace android
137 
138 #endif
139