blob: 43c877e27d9c153a3b8e5a5fb5a0cca286719bc4 [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
Mark Salyzynff2dcd92016-09-28 15:54:45 -070034#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
36#include <private/android_filesystem_config.h>
Mark Salyzyn6debf982016-10-05 08:13:56 -070037#include <private/android_logger.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070038#include <selinux/android.h>
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080041#include "adb_io.h"
Josh Gao45b6fc82015-11-04 14:51:23 -080042#include "adb_utils.h"
Rubin Xud61a25c2016-01-11 10:23:47 +000043#include "security_log_tags.h"
Josh Gao5a1e3fd2016-12-05 17:11:34 -080044#include "sysdeps/errno.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Elliott Hughes5c742702015-07-30 17:42:01 -070046static bool should_use_fs_config(const std::string& path) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000047 // TODO: use fs_config to configure permissions on /data.
Elliott Hughes5c742702015-07-30 17:42:01 -070048 return android::base::StartsWith(path, "/system/") ||
49 android::base::StartsWith(path, "/vendor/") ||
50 android::base::StartsWith(path, "/oem/");
Daniel Rosenberg686bce62014-06-30 20:29:40 -070051}
52
Elliott Hughes32c60b42016-06-07 17:18:25 -070053static bool update_capabilities(const char* path, uint64_t capabilities) {
54 if (capabilities == 0) {
55 // Ensure we clean up in case the capabilities weren't 0 in the past.
56 removexattr(path, XATTR_NAME_CAPS);
57 return true;
58 }
59
60 vfs_cap_data cap_data = {};
61 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
62 cap_data.data[0].permitted = (capabilities & 0xffffffff);
63 cap_data.data[0].inheritable = 0;
64 cap_data.data[1].permitted = (capabilities >> 32);
65 cap_data.data[1].inheritable = 0;
66 return setxattr(path, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) != -1;
67}
68
Alex Vallée47d67c92015-05-06 16:26:00 -040069static bool secure_mkdirs(const std::string& path) {
Nick Kralevich72917832014-01-17 16:16:42 -080070 uid_t uid = -1;
71 gid_t gid = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -080072 unsigned int mode = 0775;
Elliott Hughes32c60b42016-06-07 17:18:25 -070073 uint64_t capabilities = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Alex Vallée47d67c92015-05-06 16:26:00 -040075 if (path[0] != '/') return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Elliott Hughes5c742702015-07-30 17:42:01 -070077 std::vector<std::string> path_components = android::base::Split(path, "/");
Elliott Hughes5c742702015-07-30 17:42:01 -070078 std::string partial_path;
Elliott Hughes65fe2512015-10-07 15:59:35 -070079 for (const auto& path_component : path_components) {
Elliott Hughes5c742702015-07-30 17:42:01 -070080 if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR;
81 partial_path += path_component;
82
83 if (should_use_fs_config(partial_path)) {
Elliott Hughes32c60b42016-06-07 17:18:25 -070084 fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &capabilities);
Liang Cheng20d33f42014-01-02 18:27:51 -080085 }
Elliott Hughes5c742702015-07-30 17:42:01 -070086 if (adb_mkdir(partial_path.c_str(), mode) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040087 if (errno != EEXIST) {
88 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -080089 }
Alex Vallée47d67c92015-05-06 16:26:00 -040090 } else {
Elliott Hughes32c60b42016-06-07 17:18:25 -070091 if (chown(partial_path.c_str(), uid, gid) == -1) return false;
92
Elliott Hughes38344402015-08-25 16:33:50 -070093 // Not all filesystems support setting SELinux labels. http://b/23530370.
Elliott Hughes5c742702015-07-30 17:42:01 -070094 selinux_android_restorecon(partial_path.c_str(), 0);
Elliott Hughes32c60b42016-06-07 17:18:25 -070095
96 if (!update_capabilities(partial_path.c_str(), capabilities)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 }
Alex Vallée47d67c92015-05-06 16:26:00 -040099 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100}
101
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800102static bool do_lstat_v1(int s, const char* path) {
103 syncmsg msg = {};
104 msg.stat_v1.id = ID_LSTAT_V1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105
Elliott Hughes32c60b42016-06-07 17:18:25 -0700106 struct stat st = {};
Elliott Hughesaa245492015-08-03 10:38:08 -0700107 lstat(path, &st);
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800108 msg.stat_v1.mode = st.st_mode;
109 msg.stat_v1.size = st.st_size;
110 msg.stat_v1.time = st.st_mtime;
111 return WriteFdExactly(s, &msg.stat_v1, sizeof(msg.stat_v1));
112}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800114static bool do_stat_v2(int s, uint32_t id, const char* path) {
115 syncmsg msg = {};
116 msg.stat_v2.id = id;
117
118 decltype(&stat) stat_fn;
119 if (id == ID_STAT_V2) {
120 stat_fn = stat;
121 } else {
122 stat_fn = lstat;
123 }
124
125 struct stat st = {};
126 int rc = stat_fn(path, &st);
127 if (rc == -1) {
128 msg.stat_v2.error = errno_to_wire(errno);
129 } else {
130 msg.stat_v2.dev = st.st_dev;
131 msg.stat_v2.ino = st.st_ino;
132 msg.stat_v2.mode = st.st_mode;
133 msg.stat_v2.nlink = st.st_nlink;
134 msg.stat_v2.uid = st.st_uid;
135 msg.stat_v2.gid = st.st_gid;
136 msg.stat_v2.size = st.st_size;
137 msg.stat_v2.atime = st.st_atime;
138 msg.stat_v2.mtime = st.st_mtime;
139 msg.stat_v2.ctime = st.st_ctime;
140 }
141
142 return WriteFdExactly(s, &msg.stat_v2, sizeof(msg.stat_v2));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143}
144
Elliott Hughesaa245492015-08-03 10:38:08 -0700145static bool do_list(int s, const char* path) {
146 dirent* de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147
Elliott Hughes5c742702015-07-30 17:42:01 -0700148 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 msg.dent.id = ID_DENT;
150
Elliott Hughes5c742702015-07-30 17:42:01 -0700151 std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir);
152 if (!d) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153
Elliott Hughes5c742702015-07-30 17:42:01 -0700154 while ((de = readdir(d.get()))) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700155 std::string filename(android::base::StringPrintf("%s/%s", path, de->d_name));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
Elliott Hughesaa245492015-08-03 10:38:08 -0700157 struct stat st;
158 if (lstat(filename.c_str(), &st) == 0) {
159 size_t d_name_length = strlen(de->d_name);
Elliott Hughesf4465202015-08-24 14:27:03 -0700160 msg.dent.mode = st.st_mode;
161 msg.dent.size = st.st_size;
162 msg.dent.time = st.st_mtime;
163 msg.dent.namelen = d_name_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesaa245492015-08-03 10:38:08 -0700165 if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
166 !WriteFdExactly(s, de->d_name, d_name_length)) {
167 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 }
169 }
170 }
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172done:
173 msg.dent.id = ID_DONE;
174 msg.dent.mode = 0;
175 msg.dent.size = 0;
176 msg.dent.time = 0;
177 msg.dent.namelen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700178 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179}
180
Josh Gao6ad06b92016-01-28 16:21:12 -0800181// Make sure that SendFail from adb_io.cpp isn't accidentally used in this file.
182#pragma GCC poison SendFail
183
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700184static bool SendSyncFail(int fd, const std::string& reason) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700185 D("sync: failure: %s", reason.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700186
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 msg.data.id = ID_FAIL;
Elliott Hughesf4465202015-08-24 14:27:03 -0700189 msg.data.size = reason.size();
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700190 return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191}
192
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700193static bool SendSyncFailErrno(int fd, const std::string& reason) {
194 return SendSyncFail(fd, android::base::StringPrintf("%s: %s", reason.c_str(), strerror(errno)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195}
196
Elliott Hughes32c60b42016-06-07 17:18:25 -0700197static bool handle_send_file(int s, const char* path, uid_t uid, gid_t gid, uint64_t capabilities,
198 mode_t mode, std::vector<char>& buffer, bool do_unlink) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 syncmsg msg;
200 unsigned int timestamp = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Rubin Xud61a25c2016-01-11 10:23:47 +0000202 __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path);
203
Elliott Hughesaa245492015-08-03 10:38:08 -0700204 int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700205 if (fd < 0 && errno == ENOENT) {
Josh Gao45b6fc82015-11-04 14:51:23 -0800206 if (!secure_mkdirs(adb_dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700207 SendSyncFailErrno(s, "secure_mkdirs failed");
208 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800209 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700210 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700212 if (fd < 0 && errno == EEXIST) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700213 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700215 if (fd < 0) {
216 SendSyncFailErrno(s, "couldn't create file");
217 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800218 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700219 if (fchown(fd, uid, gid) == -1) {
220 SendSyncFailErrno(s, "fchown failed");
221 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800222 }
Nick Kralevich72917832014-01-17 16:16:42 -0800223
Elliott Hughes38344402015-08-25 16:33:50 -0700224 // Not all filesystems support setting SELinux labels. http://b/23530370.
225 selinux_android_restorecon(path, 0);
Elliott Hughes0adc0972015-08-25 13:14:07 -0700226
227 // fchown clears the setuid bit - restore it if present.
228 // Ignore the result of calling fchmod. It's not supported
Elliott Hughes32c60b42016-06-07 17:18:25 -0700229 // by all filesystems, so we don't check for success. b/12441485
Nick Kralevich72917832014-01-17 16:16:42 -0800230 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
232
Elliott Hughesaa245492015-08-03 10:38:08 -0700233 while (true) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700234 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700236 if (msg.data.id != ID_DATA) {
237 if (msg.data.id == ID_DONE) {
Elliott Hughesf4465202015-08-24 14:27:03 -0700238 timestamp = msg.data.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 break;
240 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700241 SendSyncFail(s, "invalid data message");
Josh Gao20a96c72016-02-19 14:33:14 -0800242 goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
Josh Gao20a96c72016-02-19 14:33:14 -0800244
245 if (msg.data.size > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700246 SendSyncFail(s, "oversize data message");
Josh Gao20a96c72016-02-19 14:33:14 -0800247 goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700249
Josh Gao20a96c72016-02-19 14:33:14 -0800250 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251
Josh Gao20a96c72016-02-19 14:33:14 -0800252 if (!WriteFdExactly(fd, &buffer[0], msg.data.size)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700253 SendSyncFailErrno(s, "write failed");
254 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 }
256 }
257
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700258 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259
Jin Qiand0515e72016-11-02 16:27:17 -0700260 if (!update_capabilities(path, capabilities)) {
261 SendSyncFailErrno(s, "update_capabilities failed");
262 goto fail;
263 }
264
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700265 utimbuf u;
266 u.actime = timestamp;
267 u.modtime = timestamp;
268 utime(path, &u);
269
270 msg.status.id = ID_OKAY;
271 msg.status.msglen = 0;
272 return WriteFdExactly(s, &msg.status, sizeof(msg.status));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
274fail:
Josh Gao20a96c72016-02-19 14:33:14 -0800275 // If there's a problem on the device, we'll send an ID_FAIL message and
276 // close the socket. Unfortunately the kernel will sometimes throw that
277 // data away if the other end keeps writing without reading (which is
278 // the case with old versions of adb). To maintain compatibility, keep
279 // reading and throwing away ID_DATA packets until the other side notices
280 // that we've reported an error.
281 while (true) {
282 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
283
284 if (msg.data.id == ID_DONE) {
285 goto abort;
286 } else if (msg.data.id != ID_DATA) {
287 char id[5];
288 memcpy(id, &msg.data.id, sizeof(msg.data.id));
289 id[4] = '\0';
290 D("handle_send_fail received unexpected id '%s' during failure", id);
291 goto abort;
292 }
293
294 if (msg.data.size > buffer.size()) {
295 D("handle_send_fail received oversized packet of length '%u' during failure",
296 msg.data.size);
297 goto abort;
298 }
299
300 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort;
301 }
302
303abort:
Elliott Hughesaa245492015-08-03 10:38:08 -0700304 if (fd >= 0) adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800305 if (do_unlink) adb_unlink(path);
Elliott Hughesaa245492015-08-03 10:38:08 -0700306 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307}
308
Elliott Hughes0a049b12015-01-12 14:26:36 -0800309#if defined(_WIN32)
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700310extern 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 -0800311#else
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700312static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 syncmsg msg;
314 unsigned int len;
315 int ret;
316
Elliott Hughesaa245492015-08-03 10:38:08 -0700317 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318
Elliott Hughesaa245492015-08-03 10:38:08 -0700319 if (msg.data.id != ID_DATA) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700320 SendSyncFail(s, "invalid data message: expected ID_DATA");
Elliott Hughesaa245492015-08-03 10:38:08 -0700321 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
323
Elliott Hughesf4465202015-08-24 14:27:03 -0700324 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700325 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700326 SendSyncFail(s, "oversize data message");
Elliott Hughesaa245492015-08-03 10:38:08 -0700327 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700329 if (!ReadFdExactly(s, &buffer[0], len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700331 ret = symlink(&buffer[0], path.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700332 if (ret && errno == ENOENT) {
Josh Gao45b6fc82015-11-04 14:51:23 -0800333 if (!secure_mkdirs(adb_dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700334 SendSyncFailErrno(s, "secure_mkdirs failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700335 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -0800336 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700337 ret = symlink(&buffer[0], path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700339 if (ret) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700340 SendSyncFailErrno(s, "symlink failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700341 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 }
343
Elliott Hughesaa245492015-08-03 10:38:08 -0700344 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345
Elliott Hughesaa245492015-08-03 10:38:08 -0700346 if (msg.data.id == ID_DONE) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 msg.status.id = ID_OKAY;
348 msg.status.msglen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700349 if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 } else {
Josh Gao6ad06b92016-01-28 16:21:12 -0800351 SendSyncFail(s, "invalid data message: expected ID_DONE");
Elliott Hughesaa245492015-08-03 10:38:08 -0700352 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 }
354
Elliott Hughesaa245492015-08-03 10:38:08 -0700355 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356}
Elliott Hughes0a049b12015-01-12 14:26:36 -0800357#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700359static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) {
360 // 'spec' is of the form "/some/path,0755". Break it up.
361 size_t comma = spec.find_last_of(',');
362 if (comma == std::string::npos) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800363 SendSyncFail(s, "missing , in ID_SEND");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700364 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 }
366
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700367 std::string path = spec.substr(0, comma);
368
369 errno = 0;
370 mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0);
371 if (errno != 0) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800372 SendSyncFail(s, "bad mode");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700373 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 }
375
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700376 // Don't delete files before copying if they are not "regular" or symlinks.
377 struct stat st;
378 bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
379 if (do_unlink) {
380 adb_unlink(path.c_str());
381 }
382
383 if (S_ISLNK(mode)) {
384 return handle_send_link(s, path.c_str(), buffer);
385 }
386
387 // Copy user permission bits to "group" and "other" permissions.
388 mode &= 0777;
389 mode |= ((mode >> 3) & 0070);
390 mode |= ((mode >> 3) & 0007);
391
Elliott Hughes0a049b12015-01-12 14:26:36 -0800392 uid_t uid = -1;
393 gid_t gid = -1;
Elliott Hughes32c60b42016-06-07 17:18:25 -0700394 uint64_t capabilities = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000395 if (should_use_fs_config(path)) {
Elliott Hughes7baecbe2015-08-25 11:09:04 -0700396 unsigned int broken_api_hack = mode;
Elliott Hughes32c60b42016-06-07 17:18:25 -0700397 fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &capabilities);
Elliott Hughes56bf3092015-08-25 11:01:39 -0700398 mode = broken_api_hack;
Elliott Hughes0a049b12015-01-12 14:26:36 -0800399 }
Elliott Hughes32c60b42016-06-07 17:18:25 -0700400 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 -0800401}
402
Elliott Hughesaa245492015-08-03 10:38:08 -0700403static bool do_recv(int s, const char* path, std::vector<char>& buffer) {
Rubin Xud61a25c2016-01-11 10:23:47 +0000404 __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path);
405
Elliott Hughesaa245492015-08-03 10:38:08 -0700406 int fd = adb_open(path, O_RDONLY | O_CLOEXEC);
407 if (fd < 0) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700408 SendSyncFailErrno(s, "open failed");
409 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 }
411
Elliott Hughesaa245492015-08-03 10:38:08 -0700412 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 msg.data.id = ID_DATA;
Elliott Hughesaa245492015-08-03 10:38:08 -0700414 while (true) {
415 int r = adb_read(fd, &buffer[0], buffer.size());
416 if (r <= 0) {
417 if (r == 0) break;
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700418 SendSyncFailErrno(s, "read failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 adb_close(fd);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700420 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700422 msg.data.size = r;
Elliott Hughesaa245492015-08-03 10:38:08 -0700423 if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 adb_close(fd);
Elliott Hughesaa245492015-08-03 10:38:08 -0700425 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 }
427 }
428
429 adb_close(fd);
430
431 msg.data.id = ID_DONE;
432 msg.data.size = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700433 return WriteFdExactly(s, &msg.data, sizeof(msg.data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434}
435
Elliott Hughesaa245492015-08-03 10:38:08 -0700436static bool handle_sync_command(int fd, std::vector<char>& buffer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700437 D("sync: waiting for request");
Elliott Hughesaa245492015-08-03 10:38:08 -0700438
439 SyncRequest request;
440 if (!ReadFdExactly(fd, &request, sizeof(request))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700441 SendSyncFail(fd, "command read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700442 return false;
443 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700444 size_t path_length = request.path_length;
Elliott Hughesaa245492015-08-03 10:38:08 -0700445 if (path_length > 1024) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700446 SendSyncFail(fd, "path too long");
Elliott Hughesaa245492015-08-03 10:38:08 -0700447 return false;
448 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 char name[1025];
Elliott Hughesaa245492015-08-03 10:38:08 -0700450 if (!ReadFdExactly(fd, name, path_length)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700451 SendSyncFail(fd, "filename read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700452 return false;
453 }
454 name[path_length] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455
Elliott Hughesaa245492015-08-03 10:38:08 -0700456 const char* id = reinterpret_cast<const char*>(&request.id);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700457 D("sync: '%.4s' '%s'", id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Elliott Hughesaa245492015-08-03 10:38:08 -0700459 switch (request.id) {
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800460 case ID_LSTAT_V1:
461 if (!do_lstat_v1(fd, name)) return false;
462 break;
463 case ID_LSTAT_V2:
464 case ID_STAT_V2:
465 if (!do_stat_v2(fd, request.id, name)) return false;
466 break;
467 case ID_LIST:
468 if (!do_list(fd, name)) return false;
469 break;
470 case ID_SEND:
471 if (!do_send(fd, name, buffer)) return false;
472 break;
473 case ID_RECV:
474 if (!do_recv(fd, name, buffer)) return false;
475 break;
476 case ID_QUIT:
477 return false;
478 default:
479 SendSyncFail(
480 fd, android::base::StringPrintf("unknown command '%.4s' (%08x)", id, request.id));
481 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 }
483
Elliott Hughesaa245492015-08-03 10:38:08 -0700484 return true;
485}
486
Josh Gao5a1e3fd2016-12-05 17:11:34 -0800487void file_sync_service(int fd, void*) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700488 std::vector<char> buffer(SYNC_DATA_MAX);
489
490 while (handle_sync_command(fd, buffer)) {
491 }
492
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700493 D("sync: done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 adb_close(fd);
495}