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 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 27 | enum coldboot_action_t { |
| 28 | // coldboot continues without creating the device for the uevent |
| 29 | COLDBOOT_CONTINUE = 0, |
| 30 | // coldboot continues after creating the device for the uevent |
| 31 | COLDBOOT_CREATE, |
| 32 | // coldboot stops after creating the device for uevent but doesn't |
| 33 | // create the COLDBOOT_DONE file |
| 34 | COLDBOOT_STOP, |
| 35 | // same as COLDBOOT_STOP, but creates the COLDBOOT_DONE file |
| 36 | COLDBOOT_FINISH |
| 37 | }; |
| 38 | |
| 39 | struct uevent { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 40 | std::string action; |
| 41 | std::string path; |
| 42 | std::string subsystem; |
| 43 | std::string firmware; |
| 44 | std::string partition_name; |
| 45 | std::string device_name; |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 46 | int partition_num; |
| 47 | int major; |
| 48 | int minor; |
| 49 | }; |
| 50 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 51 | class Permissions { |
| 52 | public: |
| 53 | Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid); |
| 54 | |
| 55 | bool Match(const std::string& path) const; |
| 56 | |
| 57 | mode_t perm() const { return perm_; } |
| 58 | uid_t uid() const { return uid_; } |
| 59 | gid_t gid() const { return gid_; } |
| 60 | |
| 61 | protected: |
| 62 | const std::string& name() const { return name_; } |
| 63 | |
| 64 | private: |
| 65 | std::string name_; |
| 66 | mode_t perm_; |
| 67 | uid_t uid_; |
| 68 | gid_t gid_; |
| 69 | bool prefix_; |
| 70 | bool wildcard_; |
| 71 | }; |
| 72 | |
| 73 | class SysfsPermissions : public Permissions { |
| 74 | public: |
| 75 | SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid, |
| 76 | gid_t gid) |
| 77 | : Permissions(name, perm, uid, gid), attribute_(attribute) {} |
| 78 | |
| 79 | bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const; |
| 80 | void SetPermissions(const std::string& path) const; |
| 81 | |
| 82 | private: |
| 83 | const std::string attribute_; |
| 84 | }; |
| 85 | |
| 86 | extern std::vector<Permissions> dev_permissions; |
| 87 | extern std::vector<SysfsPermissions> sysfs_permissions; |
| 88 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 89 | typedef std::function<coldboot_action_t(struct uevent* uevent)> coldboot_callback; |
| 90 | extern coldboot_action_t handle_device_fd(coldboot_callback fn = nullptr); |
| 91 | extern void device_init(const char* path = nullptr, coldboot_callback fn = nullptr); |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 92 | extern void device_close(); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 93 | int get_device_fd(); |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 94 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 95 | // Exposed for testing |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 96 | extern std::vector<std::string> platform_devices; |
| 97 | bool find_platform_device(const std::string& path, std::string* out_path); |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 98 | std::vector<std::string> get_character_device_symlinks(uevent* uevent); |
| 99 | std::vector<std::string> get_block_device_symlinks(uevent* uevent); |
| 100 | void sanitize_partition_name(std::string* string); |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 101 | void handle_platform_device_event(uevent* uevent); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 102 | |
| 103 | #endif /* _INIT_DEVICES_H */ |