blob: 647b4c4bcc0f0225eb2f30f15e935fc5998a49fa [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
Tom Cherryfe062052017-04-24 16:59:05 -070027#include "init_parser.h"
28
Sandeep Patil957e4ab2017-02-07 18:11:37 -080029enum 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
41struct uevent {
Tom Cherrye3e48212017-04-11 13:53:37 -070042 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 Patil957e4ab2017-02-07 18:11:37 -080048 int partition_num;
49 int major;
50 int minor;
51};
52
Tom Cherrycc054c92017-04-05 17:55:46 -070053class 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
75class 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 Cherryfe062052017-04-24 16:59:05 -070088class Subsystem {
89 public:
90 friend class SubsystemParser;
Tom Cherrycc054c92017-04-05 17:55:46 -070091
Tom Cherryfe062052017-04-24 16:59:05 -070092 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
111class 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
126bool ParsePermissionsLine(std::vector<std::string>&& args, std::string* err, bool is_sysfs);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800127typedef std::function<coldboot_action_t(struct uevent* uevent)> coldboot_callback;
128extern coldboot_action_t handle_device_fd(coldboot_callback fn = nullptr);
129extern void device_init(const char* path = nullptr, coldboot_callback fn = nullptr);
Sandeep Patil35403eb2017-02-08 20:27:12 -0800130extern void device_close();
Colin Cross0dd7ca62010-04-13 19:25:51 -0700131int get_device_fd();
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800132
Tom Cherryc44f6a42017-04-05 15:58:31 -0700133// Exposed for testing
Tom Cherry1ab8f552017-04-06 14:41:30 -0700134extern std::vector<std::string> platform_devices;
135bool find_platform_device(const std::string& path, std::string* out_path);
Tom Cherry2e344f92017-04-04 17:53:45 -0700136std::vector<std::string> get_character_device_symlinks(uevent* uevent);
137std::vector<std::string> get_block_device_symlinks(uevent* uevent);
138void sanitize_partition_name(std::string* string);
Tom Cherry1ab8f552017-04-06 14:41:30 -0700139void handle_platform_device_event(uevent* uevent);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700140
141#endif /* _INIT_DEVICES_H */