blob: f7be8f94b0bf79d42f9153ec7c580110d4ae7eda [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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG SYNC
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "file_sync_service.h"
21
Dan Albert76649012015-02-24 15:51:19 -080022#include <dirent.h>
23#include <errno.h>
Elliott Hughes32c60b42016-06-07 17:18:25 -070024#include <linux/xattr.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <sys/stat.h>
29#include <sys/types.h>
Elliott Hughes32c60b42016-06-07 17:18:25 -070030#include <sys/xattr.h>
Liang Cheng20d33f42014-01-02 18:27:51 -080031#include <unistd.h>
Dan Albert76649012015-02-24 15:51:19 -080032#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Colin Cross58021d12017-02-23 21:23:05 -080034#include <android-base/file.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070035#include <android-base/stringprintf.h>
36#include <android-base/strings.h>
37#include <private/android_filesystem_config.h>
Mark Salyzyn6debf982016-10-05 08:13:56 -070038#include <private/android_logger.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070039#include <selinux/android.h>
40
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080042#include "adb_io.h"
Josh Gaofd12aaa2016-11-22 14:33:08 -080043#include "adb_trace.h"
Josh Gao45b6fc82015-11-04 14:51:23 -080044#include "adb_utils.h"
Rubin Xud61a25c2016-01-11 10:23:47 +000045#include "security_log_tags.h"
Josh Gao5a1e3fd2016-12-05 17:11:34 -080046#include "sysdeps/errno.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
Josh Gaofd12aaa2016-11-22 14:33:08 -080048using android::base::StringPrintf;
49
Elliott Hughes5c742702015-07-30 17:42:01 -070050static bool should_use_fs_config(const std::string& path) {
Elliott Hughes792581f2018-04-03 10:54:39 -070051 // TODO: use fs_config to configure permissions on /data too.
52 return !android::base::StartsWith(path, "/data/");
Daniel Rosenberg686bce62014-06-30 20:29:40 -070053}
54
Elliott Hughes32c60b42016-06-07 17:18:25 -070055static bool update_capabilities(const char* path, uint64_t capabilities) {
56 if (capabilities == 0) {
57 // Ensure we clean up in case the capabilities weren't 0 in the past.
58 removexattr(path, XATTR_NAME_CAPS);
59 return true;
60 }
61
62 vfs_cap_data cap_data = {};
Nick Kralevich65b8d742017-11-27 13:34:19 -080063 cap_data.magic_etc = VFS_CAP_REVISION_2 | VFS_CAP_FLAGS_EFFECTIVE;
Elliott Hughes32c60b42016-06-07 17:18:25 -070064 cap_data.data[0].permitted = (capabilities & 0xffffffff);
65 cap_data.data[0].inheritable = 0;
66 cap_data.data[1].permitted = (capabilities >> 32);
67 cap_data.data[1].inheritable = 0;
68 return setxattr(path, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) != -1;
69}
70
Alex Vallée47d67c92015-05-06 16:26:00 -040071static bool secure_mkdirs(const std::string& path) {
Alex Vallée47d67c92015-05-06 16:26:00 -040072 if (path[0] != '/') return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073
Elliott Hughes5c742702015-07-30 17:42:01 -070074 std::vector<std::string> path_components = android::base::Split(path, "/");
Elliott Hughes5c742702015-07-30 17:42:01 -070075 std::string partial_path;
Elliott Hughes65fe2512015-10-07 15:59:35 -070076 for (const auto& path_component : path_components) {
Josh Gaocfe40112018-06-28 18:43:19 -070077 uid_t uid = -1;
78 gid_t gid = -1;
79 unsigned int mode = 0775;
80 uint64_t capabilities = 0;
81
82 if (path_component.empty()) {
83 continue;
84 }
85
86 if (partial_path.empty() || partial_path.back() != OS_PATH_SEPARATOR) {
87 partial_path += OS_PATH_SEPARATOR;
88 }
Elliott Hughes5c742702015-07-30 17:42:01 -070089 partial_path += path_component;
90
91 if (should_use_fs_config(partial_path)) {
Elliott Hughes32c60b42016-06-07 17:18:25 -070092 fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &capabilities);
Liang Cheng20d33f42014-01-02 18:27:51 -080093 }
Elliott Hughes5c742702015-07-30 17:42:01 -070094 if (adb_mkdir(partial_path.c_str(), mode) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040095 if (errno != EEXIST) {
96 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -080097 }
Alex Vallée47d67c92015-05-06 16:26:00 -040098 } else {
Elliott Hughes32c60b42016-06-07 17:18:25 -070099 if (chown(partial_path.c_str(), uid, gid) == -1) return false;
100
Elliott Hughes38344402015-08-25 16:33:50 -0700101 // Not all filesystems support setting SELinux labels. http://b/23530370.
Elliott Hughes5c742702015-07-30 17:42:01 -0700102 selinux_android_restorecon(partial_path.c_str(), 0);
Elliott Hughes32c60b42016-06-07 17:18:25 -0700103
104 if (!update_capabilities(partial_path.c_str(), capabilities)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 }
Alex Vallée47d67c92015-05-06 16:26:00 -0400107 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108}
109
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800110static bool do_lstat_v1(int s, const char* path) {
111 syncmsg msg = {};
112 msg.stat_v1.id = ID_LSTAT_V1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
Elliott Hughes32c60b42016-06-07 17:18:25 -0700114 struct stat st = {};
Elliott Hughesaa245492015-08-03 10:38:08 -0700115 lstat(path, &st);
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800116 msg.stat_v1.mode = st.st_mode;
117 msg.stat_v1.size = st.st_size;
118 msg.stat_v1.time = st.st_mtime;
119 return WriteFdExactly(s, &msg.stat_v1, sizeof(msg.stat_v1));
120}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800122static bool do_stat_v2(int s, uint32_t id, const char* path) {
123 syncmsg msg = {};
124 msg.stat_v2.id = id;
125
126 decltype(&stat) stat_fn;
127 if (id == ID_STAT_V2) {
128 stat_fn = stat;
129 } else {
130 stat_fn = lstat;
131 }
132
133 struct stat st = {};
134 int rc = stat_fn(path, &st);
135 if (rc == -1) {
136 msg.stat_v2.error = errno_to_wire(errno);
137 } else {
138 msg.stat_v2.dev = st.st_dev;
139 msg.stat_v2.ino = st.st_ino;
140 msg.stat_v2.mode = st.st_mode;
141 msg.stat_v2.nlink = st.st_nlink;
142 msg.stat_v2.uid = st.st_uid;
143 msg.stat_v2.gid = st.st_gid;
144 msg.stat_v2.size = st.st_size;
145 msg.stat_v2.atime = st.st_atime;
146 msg.stat_v2.mtime = st.st_mtime;
147 msg.stat_v2.ctime = st.st_ctime;
148 }
149
150 return WriteFdExactly(s, &msg.stat_v2, sizeof(msg.stat_v2));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151}
152
Elliott Hughesaa245492015-08-03 10:38:08 -0700153static bool do_list(int s, const char* path) {
154 dirent* de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
Elliott Hughes5c742702015-07-30 17:42:01 -0700156 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 msg.dent.id = ID_DENT;
158
Elliott Hughes5c742702015-07-30 17:42:01 -0700159 std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir);
160 if (!d) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
Elliott Hughes5c742702015-07-30 17:42:01 -0700162 while ((de = readdir(d.get()))) {
Josh Gaofd12aaa2016-11-22 14:33:08 -0800163 std::string filename(StringPrintf("%s/%s", path, de->d_name));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesaa245492015-08-03 10:38:08 -0700165 struct stat st;
166 if (lstat(filename.c_str(), &st) == 0) {
167 size_t d_name_length = strlen(de->d_name);
Elliott Hughesf4465202015-08-24 14:27:03 -0700168 msg.dent.mode = st.st_mode;
169 msg.dent.size = st.st_size;
170 msg.dent.time = st.st_mtime;
171 msg.dent.namelen = d_name_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesaa245492015-08-03 10:38:08 -0700173 if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
174 !WriteFdExactly(s, de->d_name, d_name_length)) {
175 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 }
177 }
178 }
179
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180done:
181 msg.dent.id = ID_DONE;
182 msg.dent.mode = 0;
183 msg.dent.size = 0;
184 msg.dent.time = 0;
185 msg.dent.namelen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700186 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187}
188
Josh Gao6ad06b92016-01-28 16:21:12 -0800189// Make sure that SendFail from adb_io.cpp isn't accidentally used in this file.
190#pragma GCC poison SendFail
191
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700192static bool SendSyncFail(int fd, const std::string& reason) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700193 D("sync: failure: %s", reason.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700194
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 msg.data.id = ID_FAIL;
Elliott Hughesf4465202015-08-24 14:27:03 -0700197 msg.data.size = reason.size();
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700198 return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199}
200
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700201static bool SendSyncFailErrno(int fd, const std::string& reason) {
Josh Gaofd12aaa2016-11-22 14:33:08 -0800202 return SendSyncFail(fd, StringPrintf("%s: %s", reason.c_str(), strerror(errno)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203}
204
Elliott Hughes32c60b42016-06-07 17:18:25 -0700205static bool handle_send_file(int s, const char* path, uid_t uid, gid_t gid, uint64_t capabilities,
206 mode_t mode, std::vector<char>& buffer, bool do_unlink) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 syncmsg msg;
208 unsigned int timestamp = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Rubin Xud61a25c2016-01-11 10:23:47 +0000210 __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path);
211
Elliott Hughesaa245492015-08-03 10:38:08 -0700212 int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Jerry Zhangecee4342017-07-18 14:07:57 -0700213
214 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED) <
215 0) {
216 D("[ Failed to fadvise: %d ]", errno);
217 }
218
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700219 if (fd < 0 && errno == ENOENT) {
Colin Cross58021d12017-02-23 21:23:05 -0800220 if (!secure_mkdirs(android::base::Dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700221 SendSyncFailErrno(s, "secure_mkdirs failed");
222 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800223 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700224 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700226 if (fd < 0 && errno == EEXIST) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700227 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700229 if (fd < 0) {
230 SendSyncFailErrno(s, "couldn't create file");
231 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800232 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700233 if (fchown(fd, uid, gid) == -1) {
234 SendSyncFailErrno(s, "fchown failed");
235 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800236 }
Nick Kralevich72917832014-01-17 16:16:42 -0800237
Elliott Hughes38344402015-08-25 16:33:50 -0700238 // Not all filesystems support setting SELinux labels. http://b/23530370.
239 selinux_android_restorecon(path, 0);
Elliott Hughes0adc0972015-08-25 13:14:07 -0700240
241 // fchown clears the setuid bit - restore it if present.
242 // Ignore the result of calling fchmod. It's not supported
Elliott Hughes32c60b42016-06-07 17:18:25 -0700243 // by all filesystems, so we don't check for success. b/12441485
Nick Kralevich72917832014-01-17 16:16:42 -0800244 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 }
246
Elliott Hughesaa245492015-08-03 10:38:08 -0700247 while (true) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700248 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700250 if (msg.data.id != ID_DATA) {
251 if (msg.data.id == ID_DONE) {
Elliott Hughesf4465202015-08-24 14:27:03 -0700252 timestamp = msg.data.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 break;
254 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700255 SendSyncFail(s, "invalid data message");
Josh Gao20a96c72016-02-19 14:33:14 -0800256 goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 }
Josh Gao20a96c72016-02-19 14:33:14 -0800258
259 if (msg.data.size > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700260 SendSyncFail(s, "oversize data message");
Josh Gao20a96c72016-02-19 14:33:14 -0800261 goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700263
Josh Gao20a96c72016-02-19 14:33:14 -0800264 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265
Josh Gao20a96c72016-02-19 14:33:14 -0800266 if (!WriteFdExactly(fd, &buffer[0], msg.data.size)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700267 SendSyncFailErrno(s, "write failed");
268 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 }
270 }
271
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700272 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
Jin Qiand0515e72016-11-02 16:27:17 -0700274 if (!update_capabilities(path, capabilities)) {
275 SendSyncFailErrno(s, "update_capabilities failed");
276 goto fail;
277 }
278
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700279 utimbuf u;
280 u.actime = timestamp;
281 u.modtime = timestamp;
282 utime(path, &u);
283
284 msg.status.id = ID_OKAY;
285 msg.status.msglen = 0;
286 return WriteFdExactly(s, &msg.status, sizeof(msg.status));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287
288fail:
Josh Gao20a96c72016-02-19 14:33:14 -0800289 // If there's a problem on the device, we'll send an ID_FAIL message and
290 // close the socket. Unfortunately the kernel will sometimes throw that
291 // data away if the other end keeps writing without reading (which is
292 // the case with old versions of adb). To maintain compatibility, keep
293 // reading and throwing away ID_DATA packets until the other side notices
294 // that we've reported an error.
295 while (true) {
Josh Gao1d6c01b2017-08-28 11:21:31 -0700296 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) break;
Josh Gao20a96c72016-02-19 14:33:14 -0800297
298 if (msg.data.id == ID_DONE) {
Josh Gao1d6c01b2017-08-28 11:21:31 -0700299 break;
Josh Gao20a96c72016-02-19 14:33:14 -0800300 } else if (msg.data.id != ID_DATA) {
301 char id[5];
302 memcpy(id, &msg.data.id, sizeof(msg.data.id));
303 id[4] = '\0';
304 D("handle_send_fail received unexpected id '%s' during failure", id);
Josh Gao1d6c01b2017-08-28 11:21:31 -0700305 break;
Josh Gao20a96c72016-02-19 14:33:14 -0800306 }
307
308 if (msg.data.size > buffer.size()) {
309 D("handle_send_fail received oversized packet of length '%u' during failure",
310 msg.data.size);
Josh Gao1d6c01b2017-08-28 11:21:31 -0700311 break;
Josh Gao20a96c72016-02-19 14:33:14 -0800312 }
313
Josh Gao1d6c01b2017-08-28 11:21:31 -0700314 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) break;
Josh Gao20a96c72016-02-19 14:33:14 -0800315 }
316
317abort:
Elliott Hughesaa245492015-08-03 10:38:08 -0700318 if (fd >= 0) adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800319 if (do_unlink) adb_unlink(path);
Elliott Hughesaa245492015-08-03 10:38:08 -0700320 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321}
322
Elliott Hughes0a049b12015-01-12 14:26:36 -0800323#if defined(_WIN32)
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700324extern bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) __attribute__((error("no symlinks on Windows")));
Elliott Hughes0a049b12015-01-12 14:26:36 -0800325#else
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700326static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 syncmsg msg;
328 unsigned int len;
329 int ret;
330
Elliott Hughesaa245492015-08-03 10:38:08 -0700331 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332
Elliott Hughesaa245492015-08-03 10:38:08 -0700333 if (msg.data.id != ID_DATA) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700334 SendSyncFail(s, "invalid data message: expected ID_DATA");
Elliott Hughesaa245492015-08-03 10:38:08 -0700335 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 }
337
Elliott Hughesf4465202015-08-24 14:27:03 -0700338 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700339 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700340 SendSyncFail(s, "oversize data message");
Elliott Hughesaa245492015-08-03 10:38:08 -0700341 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700343 if (!ReadFdExactly(s, &buffer[0], len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700345 ret = symlink(&buffer[0], path.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700346 if (ret && errno == ENOENT) {
Colin Cross58021d12017-02-23 21:23:05 -0800347 if (!secure_mkdirs(android::base::Dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700348 SendSyncFailErrno(s, "secure_mkdirs failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700349 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -0800350 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700351 ret = symlink(&buffer[0], path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700353 if (ret) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700354 SendSyncFailErrno(s, "symlink failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700355 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 }
357
Elliott Hughesaa245492015-08-03 10:38:08 -0700358 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359
Elliott Hughesaa245492015-08-03 10:38:08 -0700360 if (msg.data.id == ID_DONE) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 msg.status.id = ID_OKAY;
362 msg.status.msglen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700363 if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 } else {
Josh Gao6ad06b92016-01-28 16:21:12 -0800365 SendSyncFail(s, "invalid data message: expected ID_DONE");
Elliott Hughesaa245492015-08-03 10:38:08 -0700366 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 }
368
Elliott Hughesaa245492015-08-03 10:38:08 -0700369 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370}
Elliott Hughes0a049b12015-01-12 14:26:36 -0800371#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700373static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) {
374 // 'spec' is of the form "/some/path,0755". Break it up.
375 size_t comma = spec.find_last_of(',');
376 if (comma == std::string::npos) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800377 SendSyncFail(s, "missing , in ID_SEND");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700378 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 }
380
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700381 std::string path = spec.substr(0, comma);
382
383 errno = 0;
384 mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0);
385 if (errno != 0) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800386 SendSyncFail(s, "bad mode");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700387 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 }
389
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700390 // Don't delete files before copying if they are not "regular" or symlinks.
391 struct stat st;
392 bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
393 if (do_unlink) {
394 adb_unlink(path.c_str());
395 }
396
397 if (S_ISLNK(mode)) {
398 return handle_send_link(s, path.c_str(), buffer);
399 }
400
401 // Copy user permission bits to "group" and "other" permissions.
402 mode &= 0777;
403 mode |= ((mode >> 3) & 0070);
404 mode |= ((mode >> 3) & 0007);
405
Elliott Hughes0a049b12015-01-12 14:26:36 -0800406 uid_t uid = -1;
407 gid_t gid = -1;
Elliott Hughes32c60b42016-06-07 17:18:25 -0700408 uint64_t capabilities = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000409 if (should_use_fs_config(path)) {
Elliott Hughes7baecbe2015-08-25 11:09:04 -0700410 unsigned int broken_api_hack = mode;
Elliott Hughes32c60b42016-06-07 17:18:25 -0700411 fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &capabilities);
Elliott Hughes56bf3092015-08-25 11:01:39 -0700412 mode = broken_api_hack;
Elliott Hughes0a049b12015-01-12 14:26:36 -0800413 }
Elliott Hughes32c60b42016-06-07 17:18:25 -0700414 return handle_send_file(s, path.c_str(), uid, gid, capabilities, mode, buffer, do_unlink);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415}
416
Elliott Hughesaa245492015-08-03 10:38:08 -0700417static bool do_recv(int s, const char* path, std::vector<char>& buffer) {
Rubin Xud61a25c2016-01-11 10:23:47 +0000418 __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path);
419
Elliott Hughesaa245492015-08-03 10:38:08 -0700420 int fd = adb_open(path, O_RDONLY | O_CLOEXEC);
421 if (fd < 0) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700422 SendSyncFailErrno(s, "open failed");
423 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 }
425
Jerry Zhangecee4342017-07-18 14:07:57 -0700426 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE) < 0) {
427 D("[ Failed to fadvise: %d ]", errno);
428 }
429
Elliott Hughesaa245492015-08-03 10:38:08 -0700430 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 msg.data.id = ID_DATA;
Elliott Hughesaa245492015-08-03 10:38:08 -0700432 while (true) {
Jerry Zhangecee4342017-07-18 14:07:57 -0700433 int r = adb_read(fd, &buffer[0], buffer.size() - sizeof(msg.data));
Elliott Hughesaa245492015-08-03 10:38:08 -0700434 if (r <= 0) {
435 if (r == 0) break;
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700436 SendSyncFailErrno(s, "read failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 adb_close(fd);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700438 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700440 msg.data.size = r;
Elliott Hughesaa245492015-08-03 10:38:08 -0700441 if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 adb_close(fd);
Elliott Hughesaa245492015-08-03 10:38:08 -0700443 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 }
445 }
446
447 adb_close(fd);
448
449 msg.data.id = ID_DONE;
450 msg.data.size = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700451 return WriteFdExactly(s, &msg.data, sizeof(msg.data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452}
453
Josh Gaofd12aaa2016-11-22 14:33:08 -0800454static const char* sync_id_to_name(uint32_t id) {
455 switch (id) {
456 case ID_LSTAT_V1:
457 return "lstat_v1";
458 case ID_LSTAT_V2:
459 return "lstat_v2";
460 case ID_STAT_V2:
461 return "stat_v2";
462 case ID_LIST:
463 return "list";
464 case ID_SEND:
465 return "send";
466 case ID_RECV:
467 return "recv";
468 case ID_QUIT:
469 return "quit";
470 default:
471 return "???";
472 }
473}
474
Elliott Hughesaa245492015-08-03 10:38:08 -0700475static bool handle_sync_command(int fd, std::vector<char>& buffer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700476 D("sync: waiting for request");
Elliott Hughesaa245492015-08-03 10:38:08 -0700477
Josh Gaofd12aaa2016-11-22 14:33:08 -0800478 ATRACE_CALL();
Elliott Hughesaa245492015-08-03 10:38:08 -0700479 SyncRequest request;
480 if (!ReadFdExactly(fd, &request, sizeof(request))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700481 SendSyncFail(fd, "command read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700482 return false;
483 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700484 size_t path_length = request.path_length;
Elliott Hughesaa245492015-08-03 10:38:08 -0700485 if (path_length > 1024) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700486 SendSyncFail(fd, "path too long");
Elliott Hughesaa245492015-08-03 10:38:08 -0700487 return false;
488 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 char name[1025];
Elliott Hughesaa245492015-08-03 10:38:08 -0700490 if (!ReadFdExactly(fd, name, path_length)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700491 SendSyncFail(fd, "filename read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700492 return false;
493 }
494 name[path_length] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495
Josh Gaofd12aaa2016-11-22 14:33:08 -0800496 std::string id_name = sync_id_to_name(request.id);
497 std::string trace_name = StringPrintf("%s(%s)", id_name.c_str(), name);
498 ATRACE_NAME(trace_name.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
Josh Gaofd12aaa2016-11-22 14:33:08 -0800500 D("sync: %s('%s')", id_name.c_str(), name);
Elliott Hughesaa245492015-08-03 10:38:08 -0700501 switch (request.id) {
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800502 case ID_LSTAT_V1:
503 if (!do_lstat_v1(fd, name)) return false;
504 break;
505 case ID_LSTAT_V2:
506 case ID_STAT_V2:
507 if (!do_stat_v2(fd, request.id, name)) return false;
508 break;
509 case ID_LIST:
510 if (!do_list(fd, name)) return false;
511 break;
512 case ID_SEND:
513 if (!do_send(fd, name, buffer)) return false;
514 break;
515 case ID_RECV:
516 if (!do_recv(fd, name, buffer)) return false;
517 break;
518 case ID_QUIT:
519 return false;
520 default:
Josh Gaofd12aaa2016-11-22 14:33:08 -0800521 SendSyncFail(fd, StringPrintf("unknown command %08x", request.id));
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800522 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 }
524
Elliott Hughesaa245492015-08-03 10:38:08 -0700525 return true;
526}
527
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800528void file_sync_service(int fd, void*) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700529 std::vector<char> buffer(SYNC_DATA_MAX);
530
531 while (handle_sync_command(fd, buffer)) {
532 }
533
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700534 D("sync: done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 adb_close(fd);
536}