system/core
修订版 | 5f4e8eac8a0db87a43ed7a930103f4241c22d479 (tree) |
---|---|
时间 | 2017-06-21 06:29:51 |
作者 | Tom Cherry <tomcherry@goog...> |
Commiter | Tom Cherry |
init: poll in first stage mount if required devices are not found
First stage mount in init currently attempts to regenerate uevents for
specific devices to create the corresponding dev nodes. However, this
is racy as first stage mount happens early in the boot process and
it's possible that some of these devices have not yet been created by
the kernel.
To fix this issue, init will poll on the uevent socket for up to 10
seconds waiting for the kernel to create the required device. It will
return false and panic if this 10 second timeout passes.
Note that the same uevent socket is used in the uevent regeneration
and the polling code, so there is no race if the device is created
after the uevent regeneration and before polling starts; the first
poll will pick up the device.
Bug: 62681642
Bug: 62682821
Test: Boot bullhead
Test: Boot sailfish
Test: Boot hikey + hotplug/unplug sdcard
Merged-In: I4a6ff043eb7115b729ca4954ebc6c9e000132993
Change-Id: I4a6ff043eb7115b729ca4954ebc6c9e000132993
@@ -19,6 +19,7 @@ | ||
19 | 19 | #include <fcntl.h> |
20 | 20 | #include <fnmatch.h> |
21 | 21 | #include <libgen.h> |
22 | +#include <poll.h> | |
22 | 23 | #include <stddef.h> |
23 | 24 | #include <stdio.h> |
24 | 25 | #include <stdlib.h> |
@@ -1028,6 +1029,37 @@ void device_close() { | ||
1028 | 1029 | selinux_status_close(); |
1029 | 1030 | } |
1030 | 1031 | |
1031 | -int get_device_fd() { | |
1032 | - return device_fd; | |
1032 | +void device_poll(const coldboot_callback& callback, | |
1033 | + const std::optional<std::chrono::milliseconds> relative_timeout) { | |
1034 | + using namespace std::chrono; | |
1035 | + | |
1036 | + pollfd ufd; | |
1037 | + ufd.events = POLLIN; | |
1038 | + ufd.fd = device_fd; | |
1039 | + | |
1040 | + auto start_time = steady_clock::now(); | |
1041 | + | |
1042 | + while (true) { | |
1043 | + ufd.revents = 0; | |
1044 | + | |
1045 | + int timeout_ms = -1; | |
1046 | + if (relative_timeout) { | |
1047 | + auto now = steady_clock::now(); | |
1048 | + auto time_elapsed = duration_cast<milliseconds>(now - start_time); | |
1049 | + if (time_elapsed > *relative_timeout) return; | |
1050 | + | |
1051 | + auto remaining_timeout = *relative_timeout - time_elapsed; | |
1052 | + timeout_ms = remaining_timeout.count(); | |
1053 | + } | |
1054 | + | |
1055 | + int nr = poll(&ufd, 1, timeout_ms); | |
1056 | + if (nr == 0) return; | |
1057 | + if (nr < 0) { | |
1058 | + continue; | |
1059 | + } | |
1060 | + if (ufd.revents & POLLIN) { | |
1061 | + auto ret = handle_device_fd(callback); | |
1062 | + if (should_stop_coldboot(ret)) return; | |
1063 | + } | |
1064 | + } | |
1033 | 1065 | } |
@@ -17,9 +17,12 @@ | ||
17 | 17 | #ifndef _INIT_DEVICES_H |
18 | 18 | #define _INIT_DEVICES_H |
19 | 19 | |
20 | -#include <functional> | |
21 | 20 | #include <sys/stat.h> |
22 | 21 | |
22 | +#include <chrono> | |
23 | +#include <functional> | |
24 | +#include <optional> | |
25 | + | |
23 | 26 | enum coldboot_action_t { |
24 | 27 | // coldboot continues without creating the device for the uevent |
25 | 28 | COLDBOOT_CONTINUE = 0, |
@@ -53,8 +56,10 @@ extern int add_dev_perms(const char *name, const char *attr, | ||
53 | 56 | mode_t perm, unsigned int uid, |
54 | 57 | unsigned int gid, unsigned short prefix, |
55 | 58 | unsigned short wildcard); |
56 | -int get_device_fd(); | |
57 | 59 | |
58 | 60 | char** get_block_device_symlinks(struct uevent* uevent); |
59 | 61 | |
62 | +void device_poll(const coldboot_callback& callback = nullptr, | |
63 | + const std::optional<std::chrono::milliseconds> relative_timeout = {}); | |
64 | + | |
60 | 65 | #endif /* _INIT_DEVICES_H */ |
@@ -19,6 +19,7 @@ | ||
19 | 19 | #include <stdlib.h> |
20 | 20 | #include <unistd.h> |
21 | 21 | |
22 | +#include <chrono> | |
22 | 23 | #include <memory> |
23 | 24 | #include <set> |
24 | 25 | #include <string> |
@@ -33,6 +34,8 @@ | ||
33 | 34 | #include "fs_mgr_avb.h" |
34 | 35 | #include "util.h" |
35 | 36 | |
37 | +using namespace std::chrono_literals; | |
38 | + | |
36 | 39 | // Class Declarations |
37 | 40 | // ------------------ |
38 | 41 | class FirstStageMount { |
@@ -47,8 +50,8 @@ class FirstStageMount { | ||
47 | 50 | bool InitDevices(); |
48 | 51 | |
49 | 52 | protected: |
50 | - void InitRequiredDevices(); | |
51 | - void InitVerityDevice(const std::string& verity_device); | |
53 | + bool InitRequiredDevices(); | |
54 | + bool InitVerityDevice(const std::string& verity_device); | |
52 | 55 | bool MountPartitions(); |
53 | 56 | |
54 | 57 | virtual coldboot_action_t ColdbootCallback(uevent* uevent); |
@@ -139,42 +142,53 @@ bool FirstStageMount::DoFirstStageMount() { | ||
139 | 142 | return true; |
140 | 143 | } |
141 | 144 | |
142 | -bool FirstStageMount::InitDevices() { | |
143 | - if (!GetRequiredDevices()) return false; | |
144 | - | |
145 | - InitRequiredDevices(); | |
146 | - | |
147 | - // InitRequiredDevices() will remove found partitions from required_devices_partition_names_. | |
148 | - // So if it isn't empty here, it means some partitions are not found. | |
149 | - if (!required_devices_partition_names_.empty()) { | |
150 | - LOG(ERROR) << __FUNCTION__ << "(): partition(s) not found: " | |
151 | - << android::base::Join(required_devices_partition_names_, ", "); | |
152 | - return false; | |
153 | - } else { | |
154 | - return true; | |
155 | - } | |
156 | -} | |
145 | +bool FirstStageMount::InitDevices() { return GetRequiredDevices() && InitRequiredDevices(); } | |
157 | 146 | |
158 | 147 | // Creates devices with uevent->partition_name matching one in the member variable |
159 | 148 | // required_devices_partition_names_. Found partitions will then be removed from it |
160 | 149 | // for the subsequent member function to check which devices are NOT created. |
161 | -void FirstStageMount::InitRequiredDevices() { | |
150 | +bool FirstStageMount::InitRequiredDevices() { | |
162 | 151 | if (required_devices_partition_names_.empty()) { |
163 | - return; | |
152 | + return true; | |
164 | 153 | } |
165 | 154 | |
166 | 155 | if (need_dm_verity_) { |
167 | 156 | const std::string dm_path = "/devices/virtual/misc/device-mapper"; |
168 | - device_init(("/sys" + dm_path).c_str(), [&dm_path](uevent* uevent) -> coldboot_action_t { | |
169 | - if (uevent->path && uevent->path == dm_path) return COLDBOOT_STOP; | |
157 | + bool found = false; | |
158 | + auto dm_callback = [&dm_path, &found](uevent* uevent) -> coldboot_action_t { | |
159 | + if (uevent->path && uevent->path == dm_path) { | |
160 | + found = true; | |
161 | + return COLDBOOT_STOP; | |
162 | + } | |
170 | 163 | return COLDBOOT_CONTINUE; // dm_path not found, continue to find it. |
171 | - }); | |
164 | + }; | |
165 | + device_init(("/sys" + dm_path).c_str(), dm_callback); | |
166 | + if (!found) { | |
167 | + device_poll(dm_callback, 10s); | |
168 | + } | |
169 | + if (!found) { | |
170 | + LOG(ERROR) << "device-mapper device not found"; | |
171 | + return false; | |
172 | + } | |
172 | 173 | } |
173 | 174 | |
174 | - device_init(nullptr, | |
175 | - [this](uevent* uevent) -> coldboot_action_t { return ColdbootCallback(uevent); }); | |
175 | + auto uevent_callback = [this](uevent* uevent) -> coldboot_action_t { | |
176 | + return ColdbootCallback(uevent); | |
177 | + }; | |
178 | + | |
179 | + device_init(nullptr, uevent_callback); | |
180 | + if (!required_devices_partition_names_.empty()) { | |
181 | + device_poll(uevent_callback, 10s); | |
182 | + } | |
183 | + | |
184 | + if (!required_devices_partition_names_.empty()) { | |
185 | + LOG(ERROR) << __PRETTY_FUNCTION__ << ": partition(s) not found: " | |
186 | + << android::base::Join(required_devices_partition_names_, ", "); | |
187 | + return false; | |
188 | + } | |
176 | 189 | |
177 | 190 | device_close(); |
191 | + return true; | |
178 | 192 | } |
179 | 193 | |
180 | 194 | coldboot_action_t FirstStageMount::ColdbootCallback(uevent* uevent) { |
@@ -203,18 +217,30 @@ coldboot_action_t FirstStageMount::ColdbootCallback(uevent* uevent) { | ||
203 | 217 | } |
204 | 218 | |
205 | 219 | // Creates "/dev/block/dm-XX" for dm-verity by running coldboot on /sys/block/dm-XX. |
206 | -void FirstStageMount::InitVerityDevice(const std::string& verity_device) { | |
220 | +bool FirstStageMount::InitVerityDevice(const std::string& verity_device) { | |
207 | 221 | const std::string device_name(basename(verity_device.c_str())); |
208 | 222 | const std::string syspath = "/sys/block/" + device_name; |
223 | + bool found = false; | |
209 | 224 | |
210 | - device_init(syspath.c_str(), [&](uevent* uevent) -> coldboot_action_t { | |
225 | + auto verity_callback = [&](uevent* uevent) -> coldboot_action_t { | |
211 | 226 | if (uevent->device_name && uevent->device_name == device_name) { |
212 | 227 | LOG(VERBOSE) << "Creating dm-verity device : " << verity_device; |
228 | + found = true; | |
213 | 229 | return COLDBOOT_STOP; |
214 | 230 | } |
215 | 231 | return COLDBOOT_CONTINUE; |
216 | - }); | |
232 | + }; | |
233 | + | |
234 | + device_init(syspath.c_str(), verity_callback); | |
235 | + if (!found) { | |
236 | + device_poll(verity_callback, 10s); | |
237 | + } | |
238 | + if (!found) { | |
239 | + LOG(ERROR) << "dm-verity device not found"; | |
240 | + return false; | |
241 | + } | |
217 | 242 | device_close(); |
243 | + return true; | |
218 | 244 | } |
219 | 245 | |
220 | 246 | bool FirstStageMount::MountPartitions() { |
@@ -280,7 +306,7 @@ bool FirstStageMountVBootV1::SetUpDmVerity(fstab_rec* fstab_rec) { | ||
280 | 306 | } else if (ret == FS_MGR_SETUP_VERITY_SUCCESS) { |
281 | 307 | // The exact block device name (fstab_rec->blk_device) is changed to "/dev/block/dm-XX". |
282 | 308 | // Needs to create it because ueventd isn't started in init first stage. |
283 | - InitVerityDevice(fstab_rec->blk_device); | |
309 | + return InitVerityDevice(fstab_rec->blk_device); | |
284 | 310 | } else { |
285 | 311 | return false; |
286 | 312 | } |
@@ -17,7 +17,6 @@ | ||
17 | 17 | #include <ctype.h> |
18 | 18 | #include <fcntl.h> |
19 | 19 | #include <grp.h> |
20 | -#include <poll.h> | |
21 | 20 | #include <pwd.h> |
22 | 21 | #include <signal.h> |
23 | 22 | #include <stdio.h> |
@@ -76,20 +75,7 @@ int ueventd_main(int argc, char **argv) | ||
76 | 75 | |
77 | 76 | device_init(); |
78 | 77 | |
79 | - pollfd ufd; | |
80 | - ufd.events = POLLIN; | |
81 | - ufd.fd = get_device_fd(); | |
82 | - | |
83 | - while (true) { | |
84 | - ufd.revents = 0; | |
85 | - int nr = poll(&ufd, 1, -1); | |
86 | - if (nr <= 0) { | |
87 | - continue; | |
88 | - } | |
89 | - if (ufd.revents & POLLIN) { | |
90 | - handle_device_fd(); | |
91 | - } | |
92 | - } | |
78 | + device_poll(); | |
93 | 79 | |
94 | 80 | return 0; |
95 | 81 | } |