blob: 2cbae66a6dd4699a64d665034abc5bc7be1930e4 [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 Cherry3f5eaae52017-04-06 16:30:22 -070023#include <functional>
Tom Cherry2e344f92017-04-04 17:53:45 -070024#include <string>
25#include <vector>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070026
Sandeep Patil957e4ab2017-02-07 18:11:37 -080027enum 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
39struct uevent {
Tom Cherrye3e48212017-04-11 13:53:37 -070040 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 Patil957e4ab2017-02-07 18:11:37 -080046 int partition_num;
47 int major;
48 int minor;
49};
50
Tom Cherrycc054c92017-04-05 17:55:46 -070051class 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
73class 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
86extern std::vector<Permissions> dev_permissions;
87extern std::vector<SysfsPermissions> sysfs_permissions;
88
Sandeep Patil957e4ab2017-02-07 18:11:37 -080089typedef std::function<coldboot_action_t(struct uevent* uevent)> coldboot_callback;
90extern coldboot_action_t handle_device_fd(coldboot_callback fn = nullptr);
91extern void device_init(const char* path = nullptr, coldboot_callback fn = nullptr);
Sandeep Patil35403eb2017-02-08 20:27:12 -080092extern void device_close();
Colin Cross0dd7ca62010-04-13 19:25:51 -070093int get_device_fd();
Elliott Hughesf3cf4382015-02-03 17:12:07 -080094
Tom Cherryc44f6a42017-04-05 15:58:31 -070095// Exposed for testing
Tom Cherry1ab8f552017-04-06 14:41:30 -070096extern std::vector<std::string> platform_devices;
97bool find_platform_device(const std::string& path, std::string* out_path);
Tom Cherry2e344f92017-04-04 17:53:45 -070098std::vector<std::string> get_character_device_symlinks(uevent* uevent);
99std::vector<std::string> get_block_device_symlinks(uevent* uevent);
100void sanitize_partition_name(std::string* string);
Tom Cherry1ab8f552017-04-06 14:41:30 -0700101void handle_platform_device_event(uevent* uevent);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700102
103#endif /* _INIT_DEVICES_H */