The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 22 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 23 | #include <functional> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 26 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 27 | #include "init_parser.h" |
| 28 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 29 | enum coldboot_action_t { |
| 30 | // coldboot continues without creating the device for the uevent |
| 31 | COLDBOOT_CONTINUE = 0, |
| 32 | // coldboot continues after creating the device for the uevent |
| 33 | COLDBOOT_CREATE, |
| 34 | // coldboot stops after creating the device for uevent but doesn't |
| 35 | // create the COLDBOOT_DONE file |
| 36 | COLDBOOT_STOP, |
| 37 | // same as COLDBOOT_STOP, but creates the COLDBOOT_DONE file |
| 38 | COLDBOOT_FINISH |
| 39 | }; |
| 40 | |
| 41 | struct uevent { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 42 | std::string action; |
| 43 | std::string path; |
| 44 | std::string subsystem; |
| 45 | std::string firmware; |
| 46 | std::string partition_name; |
| 47 | std::string device_name; |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 48 | int partition_num; |
| 49 | int major; |
| 50 | int minor; |
| 51 | }; |
| 52 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 53 | class Permissions { |
| 54 | public: |
| 55 | Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid); |
| 56 | |
| 57 | bool Match(const std::string& path) const; |
| 58 | |
| 59 | mode_t perm() const { return perm_; } |
| 60 | uid_t uid() const { return uid_; } |
| 61 | gid_t gid() const { return gid_; } |
| 62 | |
| 63 | protected: |
| 64 | const std::string& name() const { return name_; } |
| 65 | |
| 66 | private: |
| 67 | std::string name_; |
| 68 | mode_t perm_; |
| 69 | uid_t uid_; |
| 70 | gid_t gid_; |
| 71 | bool prefix_; |
| 72 | bool wildcard_; |
| 73 | }; |
| 74 | |
| 75 | class SysfsPermissions : public Permissions { |
| 76 | public: |
| 77 | SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid, |
| 78 | gid_t gid) |
| 79 | : Permissions(name, perm, uid, gid), attribute_(attribute) {} |
| 80 | |
| 81 | bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const; |
| 82 | void SetPermissions(const std::string& path) const; |
| 83 | |
| 84 | private: |
| 85 | const std::string attribute_; |
| 86 | }; |
| 87 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 88 | class Subsystem { |
| 89 | public: |
| 90 | friend class SubsystemParser; |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 91 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 92 | Subsystem() {} |
| 93 | |
| 94 | // Returns the full path for a uevent of a device that is a member of this subsystem, |
| 95 | // according to the rules parsed from ueventd.rc |
| 96 | std::string ParseDevPath(uevent* uevent) const; |
| 97 | |
| 98 | bool operator==(const std::string& string_name) { return name_ == string_name; } |
| 99 | |
| 100 | private: |
| 101 | enum class DevnameSource { |
| 102 | DEVNAME_UEVENT_DEVNAME, |
| 103 | DEVNAME_UEVENT_DEVPATH, |
| 104 | }; |
| 105 | |
| 106 | std::string name_; |
| 107 | std::string dir_name_ = "/dev"; |
| 108 | DevnameSource devname_source_; |
| 109 | }; |
| 110 | |
| 111 | class SubsystemParser : public SectionParser { |
| 112 | public: |
| 113 | SubsystemParser() {} |
| 114 | bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line, |
| 115 | std::string* err) override; |
| 116 | bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override; |
| 117 | void EndSection() override; |
| 118 | |
| 119 | private: |
| 120 | bool ParseDevName(std::vector<std::string>&& args, std::string* err); |
| 121 | bool ParseDirName(std::vector<std::string>&& args, std::string* err); |
| 122 | |
| 123 | Subsystem subsystem_; |
| 124 | }; |
| 125 | |
| 126 | bool ParsePermissionsLine(std::vector<std::string>&& args, std::string* err, bool is_sysfs); |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 127 | typedef std::function<coldboot_action_t(struct uevent* uevent)> coldboot_callback; |
| 128 | extern coldboot_action_t handle_device_fd(coldboot_callback fn = nullptr); |
| 129 | extern void device_init(const char* path = nullptr, coldboot_callback fn = nullptr); |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 130 | extern void device_close(); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 131 | int get_device_fd(); |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 132 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 133 | // Exposed for testing |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 134 | extern std::vector<std::string> platform_devices; |
| 135 | bool find_platform_device(const std::string& path, std::string* out_path); |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 136 | std::vector<std::string> get_character_device_symlinks(uevent* uevent); |
| 137 | std::vector<std::string> get_block_device_symlinks(uevent* uevent); |
| 138 | void sanitize_partition_name(std::string* string); |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 139 | void handle_platform_device_event(uevent* uevent); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 140 | |
| 141 | #endif /* _INIT_DEVICES_H */ |