blob: 9a87931a34c52f327d937a2073bfe9ad85ee4513 [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 Hughesec7a6672015-03-16 21:58:32 +000051 // TODO: use fs_config to configure permissions on /data.
Elliott Hughes5c742702015-07-30 17:42:01 -070052 return android::base::StartsWith(path, "/system/") ||
53 android::base::StartsWith(path, "/vendor/") ||
54 android::base::StartsWith(path, "/oem/");
Daniel Rosenberg686bce62014-06-30 20:29:40 -070055}
56
Elliott Hughes32c60b42016-06-07 17:18:25 -070057static bool update_capabilities(const char* path, uint64_t capabilities) {
58 if (capabilities == 0) {
59 // Ensure we clean up in case the capabilities weren't 0 in the past.
60 removexattr(path, XATTR_NAME_CAPS);
61 return true;
62 }
63
64 vfs_cap_data cap_data = {};
Nick Kralevich65b8d742017-11-27 13:34:19 -080065 cap_data.magic_etc = VFS_CAP_REVISION_2 | VFS_CAP_FLAGS_EFFECTIVE;
Elliott Hughes32c60b42016-06-07 17:18:25 -070066 cap_data.data[0].permitted = (capabilities & 0xffffffff);
67 cap_data.data[0].inheritable = 0;
68 cap_data.data[1].permitted = (capabilities >> 32);
69 cap_data.data[1].inheritable = 0;
70 return setxattr(path, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) != -1;
71}
72
Alex Vallée47d67c92015-05-06 16:26:00 -040073static bool secure_mkdirs(const std::string& path) {
Nick Kralevich72917832014-01-17 16:16:42 -080074 uid_t uid = -1;
75 gid_t gid = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -080076 unsigned int mode = 0775;
Elliott Hughes32c60b42016-06-07 17:18:25 -070077 uint64_t capabilities = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Alex Vallée47d67c92015-05-06 16:26:00 -040079 if (path[0] != '/') return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Elliott Hughes5c742702015-07-30 17:42:01 -070081 std::vector<std::string> path_components = android::base::Split(path, "/");
Elliott Hughes5c742702015-07-30 17:42:01 -070082 std::string partial_path;
Elliott Hughes65fe2512015-10-07 15:59:35 -070083 for (const auto& path_component : path_components) {
Elliott Hughes5c742702015-07-30 17:42:01 -070084 if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR;
85 partial_path += path_component;
86
87 if (should_use_fs_config(partial_path)) {
Elliott Hughes32c60b42016-06-07 17:18:25 -070088 fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &capabilities);
Liang Cheng20d33f42014-01-02 18:27:51 -080089 }
Elliott Hughes5c742702015-07-30 17:42:01 -070090 if (adb_mkdir(partial_path.c_str(), mode) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040091 if (errno != EEXIST) {
92 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -080093 }
Alex Vallée47d67c92015-05-06 16:26:00 -040094 } else {
Elliott Hughes32c60b42016-06-07 17:18:25 -070095 if (chown(partial_path.c_str(), uid, gid) == -1) return false;
96
Elliott Hughes38344402015-08-25 16:33:50 -070097 // Not all filesystems support setting SELinux labels. http://b/23530370.
Elliott Hughes5c742702015-07-30 17:42:01 -070098 selinux_android_restorecon(partial_path.c_str(), 0);
Elliott Hughes32c60b42016-06-07 17:18:25 -070099
100 if (!update_capabilities(partial_path.c_str(), capabilities)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 }
Alex Vallée47d67c92015-05-06 16:26:00 -0400103 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104}
105
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800106static bool do_lstat_v1(int s, const char* path) {
107 syncmsg msg = {};
108 msg.stat_v1.id = ID_LSTAT_V1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109
Elliott Hughes32c60b42016-06-07 17:18:25 -0700110 struct stat st = {};
Elliott Hughesaa245492015-08-03 10:38:08 -0700111 lstat(path, &st);
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800112 msg.stat_v1.mode = st.st_mode;
113 msg.stat_v1.size = st.st_size;
114 msg.stat_v1.time = st.st_mtime;
115 return WriteFdExactly(s, &msg.stat_v1, sizeof(msg.stat_v1));
116}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800118static bool do_stat_v2(int s, uint32_t id, const char* path) {
119 syncmsg msg = {};
120 msg.stat_v2.id = id;
121
122 decltype(&stat) stat_fn;
123 if (id == ID_STAT_V2) {
124 stat_fn = stat;
125 } else {
126 stat_fn = lstat;
127 }
128
129 struct stat st = {};
130 int rc = stat_fn(path, &st);
131 if (rc == -1) {
132 msg.stat_v2.error = errno_to_wire(errno);
133 } else {
134 msg.stat_v2.dev = st.st_dev;
135 msg.stat_v2.ino = st.st_ino;
136 msg.stat_v2.mode = st.st_mode;
137 msg.stat_v2.nlink = st.st_nlink;
138 msg.stat_v2.uid = st.st_uid;
139 msg.stat_v2.gid = st.st_gid;
140 msg.stat_v2.size = st.st_size;
141 msg.stat_v2.atime = st.st_atime;
142 msg.stat_v2.mtime = st.st_mtime;
143 msg.stat_v2.ctime = st.st_ctime;
144 }
145
146 return WriteFdExactly(s, &msg.stat_v2, sizeof(msg.stat_v2));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147}
148
Elliott Hughesaa245492015-08-03 10:38:08 -0700149static bool do_list(int s, const char* path) {
150 dirent* de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151
Elliott Hughes5c742702015-07-30 17:42:01 -0700152 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 msg.dent.id = ID_DENT;
154
Elliott Hughes5c742702015-07-30 17:42:01 -0700155 std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir);
156 if (!d) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Elliott Hughes5c742702015-07-30 17:42:01 -0700158 while ((de = readdir(d.get()))) {
Josh Gaofd12aaa2016-11-22 14:33:08 -0800159 std::string filename(StringPrintf("%s/%s", path, de->d_name));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
Elliott Hughesaa245492015-08-03 10:38:08 -0700161 struct stat st;
162 if (lstat(filename.c_str(), &st) == 0) {
163 size_t d_name_length = strlen(de->d_name);
Elliott Hughesf4465202015-08-24 14:27:03 -0700164 msg.dent.mode = st.st_mode;
165 msg.dent.size = st.st_size;
166 msg.dent.time = st.st_mtime;
167 msg.dent.namelen = d_name_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Elliott Hughesaa245492015-08-03 10:38:08 -0700169 if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
170 !WriteFdExactly(s, de->d_name, d_name_length)) {
171 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 }
173 }
174 }
175
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176done:
177 msg.dent.id = ID_DONE;
178 msg.dent.mode = 0;
179 msg.dent.size = 0;
180 msg.dent.time = 0;
181 msg.dent.namelen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700182 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183}
184
Josh Gao6ad06b92016-01-28 16:21:12 -0800185// Make sure that SendFail from adb_io.cpp isn't accidentally used in this file.
186#pragma GCC poison SendFail
187
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700188static bool SendSyncFail(int fd, const std::string& reason) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700189 D("sync: failure: %s", reason.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700190
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 msg.data.id = ID_FAIL;
Elliott Hughesf4465202015-08-24 14:27:03 -0700193 msg.data.size = reason.size();
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700194 return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195}
196
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700197static bool SendSyncFailErrno(int fd, const std::string& reason) {
Josh Gaofd12aaa2016-11-22 14:33:08 -0800198 return SendSyncFail(fd, StringPrintf("%s: %s", reason.c_str(), strerror(errno)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199}
200
Elliott Hughes32c60b42016-06-07 17:18:25 -0700201static bool handle_send_file(int s, const char* path, uid_t uid, gid_t gid, uint64_t capabilities,
202 mode_t mode, std::vector<char>& buffer, bool do_unlink) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 syncmsg msg;
204 unsigned int timestamp = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
Rubin Xud61a25c2016-01-11 10:23:47 +0000206 __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path);
207
Elliott Hughesaa245492015-08-03 10:38:08 -0700208 int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Jerry Zhangecee4342017-07-18 14:07:57 -0700209
210 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED) <
211 0) {
212 D("[ Failed to fadvise: %d ]", errno);
213 }
214
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700215 if (fd < 0 && errno == ENOENT) {
Colin Cross58021d12017-02-23 21:23:05 -0800216 if (!secure_mkdirs(android::base::Dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700217 SendSyncFailErrno(s, "secure_mkdirs failed");
218 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800219 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700220 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700222 if (fd < 0 && errno == EEXIST) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700223 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700225 if (fd < 0) {
226 SendSyncFailErrno(s, "couldn't create file");
227 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800228 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700229 if (fchown(fd, uid, gid) == -1) {
230 SendSyncFailErrno(s, "fchown failed");
231 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800232 }
Nick Kralevich72917832014-01-17 16:16:42 -0800233
Elliott Hughes38344402015-08-25 16:33:50 -0700234 // Not all filesystems support setting SELinux labels. http://b/23530370.
235 selinux_android_restorecon(path, 0);
Elliott Hughes0adc0972015-08-25 13:14:07 -0700236
237 // fchown clears the setuid bit - restore it if present.
238 // Ignore the result of calling fchmod. It's not supported
Elliott Hughes32c60b42016-06-07 17:18:25 -0700239 // by all filesystems, so we don't check for success. b/12441485
Nick Kralevich72917832014-01-17 16:16:42 -0800240 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 }
242
Elliott Hughesaa245492015-08-03 10:38:08 -0700243 while (true) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700244 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700246 if (msg.data.id != ID_DATA) {
247 if (msg.data.id == ID_DONE) {
Elliott Hughesf4465202015-08-24 14:27:03 -0700248 timestamp = msg.data.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 break;
250 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700251 SendSyncFail(s, "invalid data message");
Josh Gao20a96c72016-02-19 14:33:14 -0800252 goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 }
Josh Gao20a96c72016-02-19 14:33:14 -0800254
255 if (msg.data.size > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700256 SendSyncFail(s, "oversize data message");
Josh Gao20a96c72016-02-19 14:33:14 -0800257 goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700259
Josh Gao20a96c72016-02-19 14:33:14 -0800260 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
Josh Gao20a96c72016-02-19 14:33:14 -0800262 if (!WriteFdExactly(fd, &buffer[0], msg.data.size)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700263 SendSyncFailErrno(s, "write failed");
264 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 }
266 }
267
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700268 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269
Jin Qiand0515e72016-11-02 16:27:17 -0700270 if (!update_capabilities(path, capabilities)) {
271 SendSyncFailErrno(s, "update_capabilities failed");
272 goto fail;
273 }
274
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700275 utimbuf u;
276 u.actime = timestamp;
277 u.modtime = timestamp;
278 utime(path, &u);
279
280 msg.status.id = ID_OKAY;
281 msg.status.msglen = 0;
282 return WriteFdExactly(s, &msg.status, sizeof(msg.status));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283
284fail:
Josh Gao20a96c72016-02-19 14:33:14 -0800285 // If there's a problem on the device, we'll send an ID_FAIL message and
286 // close the socket. Unfortunately the kernel will sometimes throw that
287 // data away if the other end keeps writing without reading (which is
288 // the case with old versions of adb). To maintain compatibility, keep
289 // reading and throwing away ID_DATA packets until the other side notices
290 // that we've reported an error.
291 while (true) {
Josh Gao1d6c01b2017-08-28 11:21:31 -0700292 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) break;
Josh Gao20a96c72016-02-19 14:33:14 -0800293
294 if (msg.data.id == ID_DONE) {
Josh Gao1d6c01b2017-08-28 11:21:31 -0700295 break;
Josh Gao20a96c72016-02-19 14:33:14 -0800296 } else if (msg.data.id != ID_DATA) {
297 char id[5];
298 memcpy(id, &msg.data.id, sizeof(msg.data.id));
299 id[4] = '\0';
300 D("handle_send_fail received unexpected id '%s' during failure", id);
Josh Gao1d6c01b2017-08-28 11:21:31 -0700301 break;
Josh Gao20a96c72016-02-19 14:33:14 -0800302 }
303
304 if (msg.data.size > buffer.size()) {
305 D("handle_send_fail received oversized packet of length '%u' during failure",
306 msg.data.size);
Josh Gao1d6c01b2017-08-28 11:21:31 -0700307 break;
Josh Gao20a96c72016-02-19 14:33:14 -0800308 }
309
Josh Gao1d6c01b2017-08-28 11:21:31 -0700310 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) break;
Josh Gao20a96c72016-02-19 14:33:14 -0800311 }
312
313abort:
Elliott Hughesaa245492015-08-03 10:38:08 -0700314 if (fd >= 0) adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800315 if (do_unlink) adb_unlink(path);
Elliott Hughesaa245492015-08-03 10:38:08 -0700316 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317}
318
Elliott Hughes0a049b12015-01-12 14:26:36 -0800319#if defined(_WIN32)
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700320extern 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 -0800321#else
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700322static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 syncmsg msg;
324 unsigned int len;
325 int ret;
326
Elliott Hughesaa245492015-08-03 10:38:08 -0700327 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328
Elliott Hughesaa245492015-08-03 10:38:08 -0700329 if (msg.data.id != ID_DATA) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700330 SendSyncFail(s, "invalid data message: expected ID_DATA");
Elliott Hughesaa245492015-08-03 10:38:08 -0700331 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 }
333
Elliott Hughesf4465202015-08-24 14:27:03 -0700334 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700335 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700336 SendSyncFail(s, "oversize data message");
Elliott Hughesaa245492015-08-03 10:38:08 -0700337 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700339 if (!ReadFdExactly(s, &buffer[0], len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700341 ret = symlink(&buffer[0], path.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700342 if (ret && errno == ENOENT) {
Colin Cross58021d12017-02-23 21:23:05 -0800343 if (!secure_mkdirs(android::base::Dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700344 SendSyncFailErrno(s, "secure_mkdirs failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700345 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -0800346 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700347 ret = symlink(&buffer[0], path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700349 if (ret) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700350 SendSyncFailErrno(s, "symlink failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700351 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 }
353
Elliott Hughesaa245492015-08-03 10:38:08 -0700354 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355
Elliott Hughesaa245492015-08-03 10:38:08 -0700356 if (msg.data.id == ID_DONE) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 msg.status.id = ID_OKAY;
358 msg.status.msglen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700359 if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 } else {
Josh Gao6ad06b92016-01-28 16:21:12 -0800361 SendSyncFail(s, "invalid data message: expected ID_DONE");
Elliott Hughesaa245492015-08-03 10:38:08 -0700362 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 }
364
Elliott Hughesaa245492015-08-03 10:38:08 -0700365 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366}
Elliott Hughes0a049b12015-01-12 14:26:36 -0800367#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700369static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) {
370 // 'spec' is of the form "/some/path,0755". Break it up.
371 size_t comma = spec.find_last_of(',');
372 if (comma == std::string::npos) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800373 SendSyncFail(s, "missing , in ID_SEND");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700374 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 }
376
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700377 std::string path = spec.substr(0, comma);
378
379 errno = 0;
380 mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0);
381 if (errno != 0) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800382 SendSyncFail(s, "bad mode");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700383 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 }
385
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700386 // Don't delete files before copying if they are not "regular" or symlinks.
387 struct stat st;
388 bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
389 if (do_unlink) {
390 adb_unlink(path.c_str());
391 }
392
393 if (S_ISLNK(mode)) {
394 return handle_send_link(s, path.c_str(), buffer);
395 }
396
397 // Copy user permission bits to "group" and "other" permissions.
398 mode &= 0777;
399 mode |= ((mode >> 3) & 0070);
400 mode |= ((mode >> 3) & 0007);
401
Elliott Hughes0a049b12015-01-12 14:26:36 -0800402 uid_t uid = -1;
403 gid_t gid = -1;
Elliott Hughes32c60b42016-06-07 17:18:25 -0700404 uint64_t capabilities = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000405 if (should_use_fs_config(path)) {
Elliott Hughes7baecbe2015-08-25 11:09:04 -0700406 unsigned int broken_api_hack = mode;
Elliott Hughes32c60b42016-06-07 17:18:25 -0700407 fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &capabilities);
Elliott Hughes56bf3092015-08-25 11:01:39 -0700408 mode = broken_api_hack;
Elliott Hughes0a049b12015-01-12 14:26:36 -0800409 }
Elliott Hughes32c60b42016-06-07 17:18:25 -0700410 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 -0800411}
412
Elliott Hughesaa245492015-08-03 10:38:08 -0700413static bool do_recv(int s, const char* path, std::vector<char>& buffer) {
Rubin Xud61a25c2016-01-11 10:23:47 +0000414 __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path);
415
Elliott Hughesaa245492015-08-03 10:38:08 -0700416 int fd = adb_open(path, O_RDONLY | O_CLOEXEC);
417 if (fd < 0) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700418 SendSyncFailErrno(s, "open failed");
419 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 }
421
Jerry Zhangecee4342017-07-18 14:07:57 -0700422 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE) < 0) {
423 D("[ Failed to fadvise: %d ]", errno);
424 }
425
Elliott Hughesaa245492015-08-03 10:38:08 -0700426 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 msg.data.id = ID_DATA;
Elliott Hughesaa245492015-08-03 10:38:08 -0700428 while (true) {
Jerry Zhangecee4342017-07-18 14:07:57 -0700429 int r = adb_read(fd, &buffer[0], buffer.size() - sizeof(msg.data));
Elliott Hughesaa245492015-08-03 10:38:08 -0700430 if (r <= 0) {
431 if (r == 0) break;
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700432 SendSyncFailErrno(s, "read failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 adb_close(fd);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700434 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700436 msg.data.size = r;
Elliott Hughesaa245492015-08-03 10:38:08 -0700437 if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 adb_close(fd);
Elliott Hughesaa245492015-08-03 10:38:08 -0700439 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 }
441 }
442
443 adb_close(fd);
444
445 msg.data.id = ID_DONE;
446 msg.data.size = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700447 return WriteFdExactly(s, &msg.data, sizeof(msg.data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448}
449
Josh Gaofd12aaa2016-11-22 14:33:08 -0800450static const char* sync_id_to_name(uint32_t id) {
451 switch (id) {
452 case ID_LSTAT_V1:
453 return "lstat_v1";
454 case ID_LSTAT_V2:
455 return "lstat_v2";
456 case ID_STAT_V2:
457 return "stat_v2";
458 case ID_LIST:
459 return "list";
460 case ID_SEND:
461 return "send";
462 case ID_RECV:
463 return "recv";
464 case ID_QUIT:
465 return "quit";
466 default:
467 return "???";
468 }
469}
470
Elliott Hughesaa245492015-08-03 10:38:08 -0700471static bool handle_sync_command(int fd, std::vector<char>& buffer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700472 D("sync: waiting for request");
Elliott Hughesaa245492015-08-03 10:38:08 -0700473
Josh Gaofd12aaa2016-11-22 14:33:08 -0800474 ATRACE_CALL();
Elliott Hughesaa245492015-08-03 10:38:08 -0700475 SyncRequest request;
476 if (!ReadFdExactly(fd, &request, sizeof(request))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700477 SendSyncFail(fd, "command read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700478 return false;
479 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700480 size_t path_length = request.path_length;
Elliott Hughesaa245492015-08-03 10:38:08 -0700481 if (path_length > 1024) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700482 SendSyncFail(fd, "path too long");
Elliott Hughesaa245492015-08-03 10:38:08 -0700483 return false;
484 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 char name[1025];
Elliott Hughesaa245492015-08-03 10:38:08 -0700486 if (!ReadFdExactly(fd, name, path_length)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700487 SendSyncFail(fd, "filename read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700488 return false;
489 }
490 name[path_length] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491
Josh Gaofd12aaa2016-11-22 14:33:08 -0800492 std::string id_name = sync_id_to_name(request.id);
493 std::string trace_name = StringPrintf("%s(%s)", id_name.c_str(), name);
494 ATRACE_NAME(trace_name.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495
Josh Gaofd12aaa2016-11-22 14:33:08 -0800496 D("sync: %s('%s')", id_name.c_str(), name);
Elliott Hughesaa245492015-08-03 10:38:08 -0700497 switch (request.id) {
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800498 case ID_LSTAT_V1:
499 if (!do_lstat_v1(fd, name)) return false;
500 break;
501 case ID_LSTAT_V2:
502 case ID_STAT_V2:
503 if (!do_stat_v2(fd, request.id, name)) return false;
504 break;
505 case ID_LIST:
506 if (!do_list(fd, name)) return false;
507 break;
508 case ID_SEND:
509 if (!do_send(fd, name, buffer)) return false;
510 break;
511 case ID_RECV:
512 if (!do_recv(fd, name, buffer)) return false;
513 break;
514 case ID_QUIT:
515 return false;
516 default:
Josh Gaofd12aaa2016-11-22 14:33:08 -0800517 SendSyncFail(fd, StringPrintf("unknown command %08x", request.id));
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800518 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 }
520
Elliott Hughesaa245492015-08-03 10:38:08 -0700521 return true;
522}
523
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800524void file_sync_service(int fd, void*) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700525 std::vector<char> buffer(SYNC_DATA_MAX);
526
527 while (handle_sync_command(fd, buffer)) {
528 }
529
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700530 D("sync: done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 adb_close(fd);
532}