blob: 2acf661b7216f32ce7f5b655d13a70ddc00ee298 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG SYNC
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "file_sync_service.h"
21
Dan Albertb302d122015-02-24 15:51:19 -080022#include <dirent.h>
23#include <errno.h>
Elliott Hughes02c94742016-06-07 17:18:25 -070024#include <linux/xattr.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <stdio.h>
Dan Albertb302d122015-02-24 15:51:19 -080026#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027#include <string.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028#include <sys/stat.h>
29#include <sys/types.h>
Elliott Hughes02c94742016-06-07 17:18:25 -070030#include <sys/xattr.h>
Liang Cheng155c49a2014-01-02 18:27:51 -080031#include <unistd.h>
Dan Albertb302d122015-02-24 15:51:19 -080032#include <utime.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080033
Colin Crosse6794072017-02-23 21:23:05 -080034#include <android-base/file.h>
Mark Salyzyn5f17a902016-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 Salyzyn81a870e2016-10-05 08:13:56 -070038#include <private/android_logger.h>
Mark Salyzyn5f17a902016-09-28 15:54:45 -070039#include <selinux/android.h>
40
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080041#include "adb.h"
Dan Albert66a91b02015-02-24 21:26:58 -080042#include "adb_io.h"
Josh Gao14ed70a2016-11-22 14:33:08 -080043#include "adb_trace.h"
Josh Gaofe1a6122015-11-04 14:51:23 -080044#include "adb_utils.h"
Rubin Xu29a64f92016-01-11 10:23:47 +000045#include "security_log_tags.h"
Josh Gaoa2cf3752016-12-05 17:11:34 -080046#include "sysdeps/errno.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047
Josh Gao14ed70a2016-11-22 14:33:08 -080048using android::base::StringPrintf;
49
Elliott Hughesd189cfb2015-07-30 17:42:01 -070050static bool should_use_fs_config(const std::string& path) {
Elliott Hughesdedf7672015-03-16 21:58:32 +000051 // TODO: use fs_config to configure permissions on /data.
Elliott Hughesd189cfb2015-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 Rosenberge9a1c9c2014-06-30 20:29:40 -070055}
56
Elliott Hughes02c94742016-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 = {};
65 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
66 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ée28d1f8d2015-05-06 16:26:00 -040073static bool secure_mkdirs(const std::string& path) {
Nick Kralevich29e78132014-01-17 16:16:42 -080074 uid_t uid = -1;
75 gid_t gid = -1;
Liang Cheng155c49a2014-01-02 18:27:51 -080076 unsigned int mode = 0775;
Elliott Hughes02c94742016-06-07 17:18:25 -070077 uint64_t capabilities = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080078
Alex Vallée28d1f8d2015-05-06 16:26:00 -040079 if (path[0] != '/') return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080080
Elliott Hughesd189cfb2015-07-30 17:42:01 -070081 std::vector<std::string> path_components = android::base::Split(path, "/");
Elliott Hughesd189cfb2015-07-30 17:42:01 -070082 std::string partial_path;
Elliott Hughes85952832015-10-07 15:59:35 -070083 for (const auto& path_component : path_components) {
Elliott Hughesd189cfb2015-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 Hughes02c94742016-06-07 17:18:25 -070088 fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &capabilities);
Liang Cheng155c49a2014-01-02 18:27:51 -080089 }
Elliott Hughesd189cfb2015-07-30 17:42:01 -070090 if (adb_mkdir(partial_path.c_str(), mode) == -1) {
Alex Vallée28d1f8d2015-05-06 16:26:00 -040091 if (errno != EEXIST) {
92 return false;
Liang Cheng155c49a2014-01-02 18:27:51 -080093 }
Alex Vallée28d1f8d2015-05-06 16:26:00 -040094 } else {
Elliott Hughes02c94742016-06-07 17:18:25 -070095 if (chown(partial_path.c_str(), uid, gid) == -1) return false;
96
Elliott Hughesbfd89372015-08-25 16:33:50 -070097 // Not all filesystems support setting SELinux labels. http://b/23530370.
Elliott Hughesd189cfb2015-07-30 17:42:01 -070098 selinux_android_restorecon(partial_path.c_str(), 0);
Elliott Hughes02c94742016-06-07 17:18:25 -070099
100 if (!update_capabilities(partial_path.c_str(), capabilities)) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800102 }
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400103 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104}
105
Josh Gaoa2cf3752016-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 Project9ca14dc2009-03-03 19:32:55 -0800109
Elliott Hughes02c94742016-06-07 17:18:25 -0700110 struct stat st = {};
Elliott Hughesb628cb12015-08-03 10:38:08 -0700111 lstat(path, &st);
Josh Gaoa2cf3752016-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 Project9ca14dc2009-03-03 19:32:55 -0800117
Josh Gaoa2cf3752016-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 Project9ca14dc2009-03-03 19:32:55 -0800147}
148
Elliott Hughesb628cb12015-08-03 10:38:08 -0700149static bool do_list(int s, const char* path) {
150 dirent* de;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800151
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700152 syncmsg msg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800153 msg.dent.id = ID_DENT;
154
Elliott Hughesd189cfb2015-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 Project9ca14dc2009-03-03 19:32:55 -0800157
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700158 while ((de = readdir(d.get()))) {
Josh Gao14ed70a2016-11-22 14:33:08 -0800159 std::string filename(StringPrintf("%s/%s", path, de->d_name));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160
Elliott Hughesb628cb12015-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 Hughes9e8e3552015-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 Project9ca14dc2009-03-03 19:32:55 -0800168
Elliott Hughesb628cb12015-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 Project9ca14dc2009-03-03 19:32:55 -0800172 }
173 }
174 }
175
The Android Open Source Project9ca14dc2009-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 Hughesb628cb12015-08-03 10:38:08 -0700182 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183}
184
Josh Gaobd446be2016-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 Hughesd73ef202015-08-24 19:30:46 -0700188static bool SendSyncFail(int fd, const std::string& reason) {
Yabin Cui815ad882015-09-02 17:44:28 -0700189 D("sync: failure: %s", reason.c_str());
Elliott Hughesb628cb12015-08-03 10:38:08 -0700190
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191 syncmsg msg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192 msg.data.id = ID_FAIL;
Elliott Hughes9e8e3552015-08-24 14:27:03 -0700193 msg.data.size = reason.size();
Elliott Hughesd73ef202015-08-24 19:30:46 -0700194 return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800195}
196
Elliott Hughesd73ef202015-08-24 19:30:46 -0700197static bool SendSyncFailErrno(int fd, const std::string& reason) {
Josh Gao14ed70a2016-11-22 14:33:08 -0800198 return SendSyncFail(fd, StringPrintf("%s: %s", reason.c_str(), strerror(errno)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199}
200
Elliott Hughes02c94742016-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 Project9ca14dc2009-03-03 19:32:55 -0800203 syncmsg msg;
204 unsigned int timestamp = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205
Rubin Xu29a64f92016-01-11 10:23:47 +0000206 __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path);
207
Elliott Hughesb628cb12015-08-03 10:38:08 -0700208 int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Elliott Hughesd73ef202015-08-24 19:30:46 -0700209 if (fd < 0 && errno == ENOENT) {
Colin Crosse6794072017-02-23 21:23:05 -0800210 if (!secure_mkdirs(android::base::Dirname(path))) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700211 SendSyncFailErrno(s, "secure_mkdirs failed");
212 goto fail;
Liang Cheng155c49a2014-01-02 18:27:51 -0800213 }
Elliott Hughesd73ef202015-08-24 19:30:46 -0700214 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215 }
Elliott Hughesd73ef202015-08-24 19:30:46 -0700216 if (fd < 0 && errno == EEXIST) {
Nick Kralevich777523e2014-07-18 20:57:35 -0700217 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800218 }
Elliott Hughesd73ef202015-08-24 19:30:46 -0700219 if (fd < 0) {
220 SendSyncFailErrno(s, "couldn't create file");
221 goto fail;
Liang Cheng155c49a2014-01-02 18:27:51 -0800222 } else {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700223 if (fchown(fd, uid, gid) == -1) {
224 SendSyncFailErrno(s, "fchown failed");
225 goto fail;
Liang Cheng155c49a2014-01-02 18:27:51 -0800226 }
Nick Kralevich29e78132014-01-17 16:16:42 -0800227
Elliott Hughesbfd89372015-08-25 16:33:50 -0700228 // Not all filesystems support setting SELinux labels. http://b/23530370.
229 selinux_android_restorecon(path, 0);
Elliott Hughes4f97b612015-08-25 13:14:07 -0700230
231 // fchown clears the setuid bit - restore it if present.
232 // Ignore the result of calling fchmod. It's not supported
Elliott Hughes02c94742016-06-07 17:18:25 -0700233 // by all filesystems, so we don't check for success. b/12441485
Nick Kralevich29e78132014-01-17 16:16:42 -0800234 fchmod(fd, mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800235 }
236
Elliott Hughesb628cb12015-08-03 10:38:08 -0700237 while (true) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700238 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800239
Elliott Hughesd73ef202015-08-24 19:30:46 -0700240 if (msg.data.id != ID_DATA) {
241 if (msg.data.id == ID_DONE) {
Elliott Hughes9e8e3552015-08-24 14:27:03 -0700242 timestamp = msg.data.size;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243 break;
244 }
Elliott Hughesd73ef202015-08-24 19:30:46 -0700245 SendSyncFail(s, "invalid data message");
Josh Gao013a8eb2016-02-19 14:33:14 -0800246 goto abort;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247 }
Josh Gao013a8eb2016-02-19 14:33:14 -0800248
249 if (msg.data.size > buffer.size()) { // TODO: resize buffer?
Elliott Hughesd73ef202015-08-24 19:30:46 -0700250 SendSyncFail(s, "oversize data message");
Josh Gao013a8eb2016-02-19 14:33:14 -0800251 goto abort;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252 }
Elliott Hughesd73ef202015-08-24 19:30:46 -0700253
Josh Gao013a8eb2016-02-19 14:33:14 -0800254 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255
Josh Gao013a8eb2016-02-19 14:33:14 -0800256 if (!WriteFdExactly(fd, &buffer[0], msg.data.size)) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700257 SendSyncFailErrno(s, "write failed");
258 goto fail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800259 }
260 }
261
Elliott Hughesd73ef202015-08-24 19:30:46 -0700262 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800263
Jin Qian8833f142016-11-02 16:27:17 -0700264 if (!update_capabilities(path, capabilities)) {
265 SendSyncFailErrno(s, "update_capabilities failed");
266 goto fail;
267 }
268
Elliott Hughesd73ef202015-08-24 19:30:46 -0700269 utimbuf u;
270 u.actime = timestamp;
271 u.modtime = timestamp;
272 utime(path, &u);
273
274 msg.status.id = ID_OKAY;
275 msg.status.msglen = 0;
276 return WriteFdExactly(s, &msg.status, sizeof(msg.status));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277
278fail:
Josh Gao013a8eb2016-02-19 14:33:14 -0800279 // If there's a problem on the device, we'll send an ID_FAIL message and
280 // close the socket. Unfortunately the kernel will sometimes throw that
281 // data away if the other end keeps writing without reading (which is
282 // the case with old versions of adb). To maintain compatibility, keep
283 // reading and throwing away ID_DATA packets until the other side notices
284 // that we've reported an error.
285 while (true) {
286 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
287
288 if (msg.data.id == ID_DONE) {
289 goto abort;
290 } else if (msg.data.id != ID_DATA) {
291 char id[5];
292 memcpy(id, &msg.data.id, sizeof(msg.data.id));
293 id[4] = '\0';
294 D("handle_send_fail received unexpected id '%s' during failure", id);
295 goto abort;
296 }
297
298 if (msg.data.size > buffer.size()) {
299 D("handle_send_fail received oversized packet of length '%u' during failure",
300 msg.data.size);
301 goto abort;
302 }
303
304 if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort;
305 }
306
307abort:
Elliott Hughesb628cb12015-08-03 10:38:08 -0700308 if (fd >= 0) adb_close(fd);
JP Abgrallbb5f0d22014-03-07 17:31:25 -0800309 if (do_unlink) adb_unlink(path);
Elliott Hughesb628cb12015-08-03 10:38:08 -0700310 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311}
312
Elliott Hughes944e1d72015-01-12 14:26:36 -0800313#if defined(_WIN32)
Elliott Hughesd73ef202015-08-24 19:30:46 -0700314extern bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) __attribute__((error("no symlinks on Windows")));
Elliott Hughes944e1d72015-01-12 14:26:36 -0800315#else
Elliott Hughesd73ef202015-08-24 19:30:46 -0700316static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800317 syncmsg msg;
318 unsigned int len;
319 int ret;
320
Elliott Hughesb628cb12015-08-03 10:38:08 -0700321 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322
Elliott Hughesb628cb12015-08-03 10:38:08 -0700323 if (msg.data.id != ID_DATA) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700324 SendSyncFail(s, "invalid data message: expected ID_DATA");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700325 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800326 }
327
Elliott Hughes9e8e3552015-08-24 14:27:03 -0700328 len = msg.data.size;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700329 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesd73ef202015-08-24 19:30:46 -0700330 SendSyncFail(s, "oversize data message");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700331 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800332 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700333 if (!ReadFdExactly(s, &buffer[0], len)) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800334
Elliott Hughesd73ef202015-08-24 19:30:46 -0700335 ret = symlink(&buffer[0], path.c_str());
Elliott Hughesb628cb12015-08-03 10:38:08 -0700336 if (ret && errno == ENOENT) {
Colin Crosse6794072017-02-23 21:23:05 -0800337 if (!secure_mkdirs(android::base::Dirname(path))) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700338 SendSyncFailErrno(s, "secure_mkdirs failed");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700339 return false;
Liang Cheng155c49a2014-01-02 18:27:51 -0800340 }
Elliott Hughesd73ef202015-08-24 19:30:46 -0700341 ret = symlink(&buffer[0], path.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700343 if (ret) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700344 SendSyncFailErrno(s, "symlink failed");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700345 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346 }
347
Elliott Hughesb628cb12015-08-03 10:38:08 -0700348 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800349
Elliott Hughesb628cb12015-08-03 10:38:08 -0700350 if (msg.data.id == ID_DONE) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800351 msg.status.id = ID_OKAY;
352 msg.status.msglen = 0;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700353 if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800354 } else {
Josh Gaobd446be2016-01-28 16:21:12 -0800355 SendSyncFail(s, "invalid data message: expected ID_DONE");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700356 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800357 }
358
Elliott Hughesb628cb12015-08-03 10:38:08 -0700359 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800360}
Elliott Hughes944e1d72015-01-12 14:26:36 -0800361#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800362
Elliott Hughesd73ef202015-08-24 19:30:46 -0700363static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) {
364 // 'spec' is of the form "/some/path,0755". Break it up.
365 size_t comma = spec.find_last_of(',');
366 if (comma == std::string::npos) {
Josh Gaobd446be2016-01-28 16:21:12 -0800367 SendSyncFail(s, "missing , in ID_SEND");
Elliott Hughesd73ef202015-08-24 19:30:46 -0700368 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800369 }
370
Elliott Hughesd73ef202015-08-24 19:30:46 -0700371 std::string path = spec.substr(0, comma);
372
373 errno = 0;
374 mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0);
375 if (errno != 0) {
Josh Gaobd446be2016-01-28 16:21:12 -0800376 SendSyncFail(s, "bad mode");
Elliott Hughesd73ef202015-08-24 19:30:46 -0700377 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800378 }
379
Elliott Hughesd73ef202015-08-24 19:30:46 -0700380 // Don't delete files before copying if they are not "regular" or symlinks.
381 struct stat st;
382 bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
383 if (do_unlink) {
384 adb_unlink(path.c_str());
385 }
386
387 if (S_ISLNK(mode)) {
388 return handle_send_link(s, path.c_str(), buffer);
389 }
390
391 // Copy user permission bits to "group" and "other" permissions.
392 mode &= 0777;
393 mode |= ((mode >> 3) & 0070);
394 mode |= ((mode >> 3) & 0007);
395
Elliott Hughes944e1d72015-01-12 14:26:36 -0800396 uid_t uid = -1;
397 gid_t gid = -1;
Elliott Hughes02c94742016-06-07 17:18:25 -0700398 uint64_t capabilities = 0;
Elliott Hughesdedf7672015-03-16 21:58:32 +0000399 if (should_use_fs_config(path)) {
Elliott Hughes3dcb3bf2015-08-25 11:09:04 -0700400 unsigned int broken_api_hack = mode;
Elliott Hughes02c94742016-06-07 17:18:25 -0700401 fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &capabilities);
Elliott Hughes8979ed22015-08-25 11:01:39 -0700402 mode = broken_api_hack;
Elliott Hughes944e1d72015-01-12 14:26:36 -0800403 }
Elliott Hughes02c94742016-06-07 17:18:25 -0700404 return handle_send_file(s, path.c_str(), uid, gid, capabilities, mode, buffer, do_unlink);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800405}
406
Elliott Hughesb628cb12015-08-03 10:38:08 -0700407static bool do_recv(int s, const char* path, std::vector<char>& buffer) {
Rubin Xu29a64f92016-01-11 10:23:47 +0000408 __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path);
409
Elliott Hughesb628cb12015-08-03 10:38:08 -0700410 int fd = adb_open(path, O_RDONLY | O_CLOEXEC);
411 if (fd < 0) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700412 SendSyncFailErrno(s, "open failed");
413 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800414 }
415
Elliott Hughesb628cb12015-08-03 10:38:08 -0700416 syncmsg msg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417 msg.data.id = ID_DATA;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700418 while (true) {
419 int r = adb_read(fd, &buffer[0], buffer.size());
420 if (r <= 0) {
421 if (r == 0) break;
Elliott Hughesd73ef202015-08-24 19:30:46 -0700422 SendSyncFailErrno(s, "read failed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423 adb_close(fd);
Elliott Hughesd73ef202015-08-24 19:30:46 -0700424 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800425 }
Elliott Hughes9e8e3552015-08-24 14:27:03 -0700426 msg.data.size = r;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700427 if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800428 adb_close(fd);
Elliott Hughesb628cb12015-08-03 10:38:08 -0700429 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800430 }
431 }
432
433 adb_close(fd);
434
435 msg.data.id = ID_DONE;
436 msg.data.size = 0;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700437 return WriteFdExactly(s, &msg.data, sizeof(msg.data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800438}
439
Josh Gao14ed70a2016-11-22 14:33:08 -0800440static const char* sync_id_to_name(uint32_t id) {
441 switch (id) {
442 case ID_LSTAT_V1:
443 return "lstat_v1";
444 case ID_LSTAT_V2:
445 return "lstat_v2";
446 case ID_STAT_V2:
447 return "stat_v2";
448 case ID_LIST:
449 return "list";
450 case ID_SEND:
451 return "send";
452 case ID_RECV:
453 return "recv";
454 case ID_QUIT:
455 return "quit";
456 default:
457 return "???";
458 }
459}
460
Elliott Hughesb628cb12015-08-03 10:38:08 -0700461static bool handle_sync_command(int fd, std::vector<char>& buffer) {
Yabin Cui815ad882015-09-02 17:44:28 -0700462 D("sync: waiting for request");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700463
Josh Gao14ed70a2016-11-22 14:33:08 -0800464 ATRACE_CALL();
Elliott Hughesb628cb12015-08-03 10:38:08 -0700465 SyncRequest request;
466 if (!ReadFdExactly(fd, &request, sizeof(request))) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700467 SendSyncFail(fd, "command read failure");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700468 return false;
469 }
Elliott Hughes9e8e3552015-08-24 14:27:03 -0700470 size_t path_length = request.path_length;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700471 if (path_length > 1024) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700472 SendSyncFail(fd, "path too long");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700473 return false;
474 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800475 char name[1025];
Elliott Hughesb628cb12015-08-03 10:38:08 -0700476 if (!ReadFdExactly(fd, name, path_length)) {
Elliott Hughesd73ef202015-08-24 19:30:46 -0700477 SendSyncFail(fd, "filename read failure");
Elliott Hughesb628cb12015-08-03 10:38:08 -0700478 return false;
479 }
480 name[path_length] = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800481
Josh Gao14ed70a2016-11-22 14:33:08 -0800482 std::string id_name = sync_id_to_name(request.id);
483 std::string trace_name = StringPrintf("%s(%s)", id_name.c_str(), name);
484 ATRACE_NAME(trace_name.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800485
Josh Gao14ed70a2016-11-22 14:33:08 -0800486 D("sync: %s('%s')", id_name.c_str(), name);
Elliott Hughesb628cb12015-08-03 10:38:08 -0700487 switch (request.id) {
Josh Gaoa2cf3752016-12-05 17:11:34 -0800488 case ID_LSTAT_V1:
489 if (!do_lstat_v1(fd, name)) return false;
490 break;
491 case ID_LSTAT_V2:
492 case ID_STAT_V2:
493 if (!do_stat_v2(fd, request.id, name)) return false;
494 break;
495 case ID_LIST:
496 if (!do_list(fd, name)) return false;
497 break;
498 case ID_SEND:
499 if (!do_send(fd, name, buffer)) return false;
500 break;
501 case ID_RECV:
502 if (!do_recv(fd, name, buffer)) return false;
503 break;
504 case ID_QUIT:
505 return false;
506 default:
Josh Gao14ed70a2016-11-22 14:33:08 -0800507 SendSyncFail(fd, StringPrintf("unknown command %08x", request.id));
Josh Gaoa2cf3752016-12-05 17:11:34 -0800508 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800509 }
510
Elliott Hughesb628cb12015-08-03 10:38:08 -0700511 return true;
512}
513
Josh Gaoa2cf3752016-12-05 17:11:34 -0800514void file_sync_service(int fd, void*) {
Elliott Hughesb628cb12015-08-03 10:38:08 -0700515 std::vector<char> buffer(SYNC_DATA_MAX);
516
517 while (handle_sync_command(fd, buffer)) {
518 }
519
Yabin Cui815ad882015-09-02 17:44:28 -0700520 D("sync: done");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800521 adb_close(fd);
522}