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