blob: 29c6629295c14d8170c8c7d02c9b2b03aa0965b9 [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>
Rubin Xud61a25c2016-01-11 10:23:47 +000024#include <log/log.h>
Dan Albert76649012015-02-24 15:51:19 -080025#include <selinux/android.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <stdio.h>
Dan Albert76649012015-02-24 15:51:19 -080027#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029#include <sys/stat.h>
30#include <sys/types.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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080035#include "adb_io.h"
Josh Gao45b6fc82015-11-04 14:51:23 -080036#include "adb_utils.h"
Dan Albert76649012015-02-24 15:51:19 -080037#include "private/android_filesystem_config.h"
Rubin Xud61a25c2016-01-11 10:23:47 +000038#include "security_log_tags.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Elliott Hughes4f713192015-12-04 22:00:26 -080040#include <android-base/stringprintf.h>
41#include <android-base/strings.h>
Elliott Hughes5c742702015-07-30 17:42:01 -070042
43static bool should_use_fs_config(const std::string& path) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000044 // TODO: use fs_config to configure permissions on /data.
Elliott Hughes5c742702015-07-30 17:42:01 -070045 return android::base::StartsWith(path, "/system/") ||
46 android::base::StartsWith(path, "/vendor/") ||
47 android::base::StartsWith(path, "/oem/");
Daniel Rosenberg686bce62014-06-30 20:29:40 -070048}
49
Alex Vallée47d67c92015-05-06 16:26:00 -040050static bool secure_mkdirs(const std::string& path) {
Nick Kralevich72917832014-01-17 16:16:42 -080051 uid_t uid = -1;
52 gid_t gid = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -080053 unsigned int mode = 0775;
54 uint64_t cap = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Alex Vallée47d67c92015-05-06 16:26:00 -040056 if (path[0] != '/') return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Elliott Hughes5c742702015-07-30 17:42:01 -070058 std::vector<std::string> path_components = android::base::Split(path, "/");
Elliott Hughes5c742702015-07-30 17:42:01 -070059 std::string partial_path;
Elliott Hughes65fe2512015-10-07 15:59:35 -070060 for (const auto& path_component : path_components) {
Elliott Hughes5c742702015-07-30 17:42:01 -070061 if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR;
62 partial_path += path_component;
63
64 if (should_use_fs_config(partial_path)) {
Elliott Hughese0b43a12015-07-30 20:07:12 -070065 fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &cap);
Liang Cheng20d33f42014-01-02 18:27:51 -080066 }
Elliott Hughes5c742702015-07-30 17:42:01 -070067 if (adb_mkdir(partial_path.c_str(), mode) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040068 if (errno != EEXIST) {
69 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -080070 }
Alex Vallée47d67c92015-05-06 16:26:00 -040071 } else {
Elliott Hughes5c742702015-07-30 17:42:01 -070072 if (chown(partial_path.c_str(), uid, gid) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040073 return false;
74 }
Elliott Hughes38344402015-08-25 16:33:50 -070075 // Not all filesystems support setting SELinux labels. http://b/23530370.
Elliott Hughes5c742702015-07-30 17:42:01 -070076 selinux_android_restorecon(partial_path.c_str(), 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078 }
Alex Vallée47d67c92015-05-06 16:26:00 -040079 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080}
81
Elliott Hughesaa245492015-08-03 10:38:08 -070082static bool do_stat(int s, const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084 msg.stat.id = ID_STAT;
85
Elliott Hughesaa245492015-08-03 10:38:08 -070086 struct stat st;
87 memset(&st, 0, sizeof(st));
88 // TODO: add a way to report that the stat failed!
89 lstat(path, &st);
Elliott Hughesf4465202015-08-24 14:27:03 -070090 msg.stat.mode = st.st_mode;
91 msg.stat.size = st.st_size;
92 msg.stat.time = st.st_mtime;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093
Elliott Hughesaa245492015-08-03 10:38:08 -070094 return WriteFdExactly(s, &msg.stat, sizeof(msg.stat));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095}
96
Elliott Hughesaa245492015-08-03 10:38:08 -070097static bool do_list(int s, const char* path) {
98 dirent* de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Elliott Hughes5c742702015-07-30 17:42:01 -0700100 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 msg.dent.id = ID_DENT;
102
Elliott Hughes5c742702015-07-30 17:42:01 -0700103 std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir);
104 if (!d) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105
Elliott Hughes5c742702015-07-30 17:42:01 -0700106 while ((de = readdir(d.get()))) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700107 std::string filename(android::base::StringPrintf("%s/%s", path, de->d_name));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108
Elliott Hughesaa245492015-08-03 10:38:08 -0700109 struct stat st;
110 if (lstat(filename.c_str(), &st) == 0) {
111 size_t d_name_length = strlen(de->d_name);
Elliott Hughesf4465202015-08-24 14:27:03 -0700112 msg.dent.mode = st.st_mode;
113 msg.dent.size = st.st_size;
114 msg.dent.time = st.st_mtime;
115 msg.dent.namelen = d_name_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116
Elliott Hughesaa245492015-08-03 10:38:08 -0700117 if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
118 !WriteFdExactly(s, de->d_name, d_name_length)) {
119 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 }
121 }
122 }
123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124done:
125 msg.dent.id = ID_DONE;
126 msg.dent.mode = 0;
127 msg.dent.size = 0;
128 msg.dent.time = 0;
129 msg.dent.namelen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700130 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131}
132
Josh Gao6ad06b92016-01-28 16:21:12 -0800133// Make sure that SendFail from adb_io.cpp isn't accidentally used in this file.
134#pragma GCC poison SendFail
135
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700136static bool SendSyncFail(int fd, const std::string& reason) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700137 D("sync: failure: %s", reason.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 msg.data.id = ID_FAIL;
Elliott Hughesf4465202015-08-24 14:27:03 -0700141 msg.data.size = reason.size();
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700142 return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143}
144
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700145static bool SendSyncFailErrno(int fd, const std::string& reason) {
146 return SendSyncFail(fd, android::base::StringPrintf("%s: %s", reason.c_str(), strerror(errno)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147}
148
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700149static bool handle_send_file(int s, const char* path, uid_t uid,
Elliott Hughesaa245492015-08-03 10:38:08 -0700150 gid_t gid, mode_t mode, std::vector<char>& buffer, bool do_unlink) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 syncmsg msg;
152 unsigned int timestamp = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153
Rubin Xud61a25c2016-01-11 10:23:47 +0000154 __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path);
155
Elliott Hughesaa245492015-08-03 10:38:08 -0700156 int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700157 if (fd < 0 && errno == ENOENT) {
Josh Gao45b6fc82015-11-04 14:51:23 -0800158 if (!secure_mkdirs(adb_dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700159 SendSyncFailErrno(s, "secure_mkdirs failed");
160 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800161 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700162 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700164 if (fd < 0 && errno == EEXIST) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700165 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700167 if (fd < 0) {
168 SendSyncFailErrno(s, "couldn't create file");
169 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800170 } else {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700171 if (fchown(fd, uid, gid) == -1) {
172 SendSyncFailErrno(s, "fchown failed");
173 goto fail;
Liang Cheng20d33f42014-01-02 18:27:51 -0800174 }
Nick Kralevich72917832014-01-17 16:16:42 -0800175
Elliott Hughes38344402015-08-25 16:33:50 -0700176 // Not all filesystems support setting SELinux labels. http://b/23530370.
177 selinux_android_restorecon(path, 0);
Elliott Hughes0adc0972015-08-25 13:14:07 -0700178
179 // fchown clears the setuid bit - restore it if present.
180 // Ignore the result of calling fchmod. It's not supported
181 // by all filesystems. b/12441485
Nick Kralevich72917832014-01-17 16:16:42 -0800182 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 }
184
Elliott Hughesaa245492015-08-03 10:38:08 -0700185 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 unsigned int len;
187
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700188 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700190 if (msg.data.id != ID_DATA) {
191 if (msg.data.id == ID_DONE) {
Elliott Hughesf4465202015-08-24 14:27:03 -0700192 timestamp = msg.data.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 break;
194 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700195 SendSyncFail(s, "invalid data message");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 goto fail;
197 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700198 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700199 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700200 SendSyncFail(s, "oversize data message");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 goto fail;
202 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700203
Elliott Hughesaa245492015-08-03 10:38:08 -0700204 if (!ReadFdExactly(s, &buffer[0], len)) goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
Elliott Hughesaa245492015-08-03 10:38:08 -0700206 if (!WriteFdExactly(fd, &buffer[0], len)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700207 SendSyncFailErrno(s, "write failed");
208 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
210 }
211
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700212 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700214 utimbuf u;
215 u.actime = timestamp;
216 u.modtime = timestamp;
217 utime(path, &u);
218
219 msg.status.id = ID_OKAY;
220 msg.status.msglen = 0;
221 return WriteFdExactly(s, &msg.status, sizeof(msg.status));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222
223fail:
Elliott Hughesaa245492015-08-03 10:38:08 -0700224 if (fd >= 0) adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800225 if (do_unlink) adb_unlink(path);
Elliott Hughesaa245492015-08-03 10:38:08 -0700226 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227}
228
Elliott Hughes0a049b12015-01-12 14:26:36 -0800229#if defined(_WIN32)
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700230extern 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 -0800231#else
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700232static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 syncmsg msg;
234 unsigned int len;
235 int ret;
236
Elliott Hughesaa245492015-08-03 10:38:08 -0700237 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
Elliott Hughesaa245492015-08-03 10:38:08 -0700239 if (msg.data.id != ID_DATA) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700240 SendSyncFail(s, "invalid data message: expected ID_DATA");
Elliott Hughesaa245492015-08-03 10:38:08 -0700241 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243
Elliott Hughesf4465202015-08-24 14:27:03 -0700244 len = msg.data.size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700245 if (len > buffer.size()) { // TODO: resize buffer?
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700246 SendSyncFail(s, "oversize data message");
Elliott Hughesaa245492015-08-03 10:38:08 -0700247 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700249 if (!ReadFdExactly(s, &buffer[0], len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700251 ret = symlink(&buffer[0], path.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700252 if (ret && errno == ENOENT) {
Josh Gao45b6fc82015-11-04 14:51:23 -0800253 if (!secure_mkdirs(adb_dirname(path))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700254 SendSyncFailErrno(s, "secure_mkdirs failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700255 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -0800256 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700257 ret = symlink(&buffer[0], path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700259 if (ret) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700260 SendSyncFailErrno(s, "symlink failed");
Elliott Hughesaa245492015-08-03 10:38:08 -0700261 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
263
Elliott Hughesaa245492015-08-03 10:38:08 -0700264 if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265
Elliott Hughesaa245492015-08-03 10:38:08 -0700266 if (msg.data.id == ID_DONE) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 msg.status.id = ID_OKAY;
268 msg.status.msglen = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700269 if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 } else {
Josh Gao6ad06b92016-01-28 16:21:12 -0800271 SendSyncFail(s, "invalid data message: expected ID_DONE");
Elliott Hughesaa245492015-08-03 10:38:08 -0700272 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273 }
274
Elliott Hughesaa245492015-08-03 10:38:08 -0700275 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276}
Elliott Hughes0a049b12015-01-12 14:26:36 -0800277#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700279static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) {
280 // 'spec' is of the form "/some/path,0755". Break it up.
281 size_t comma = spec.find_last_of(',');
282 if (comma == std::string::npos) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800283 SendSyncFail(s, "missing , in ID_SEND");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700284 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 }
286
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700287 std::string path = spec.substr(0, comma);
288
289 errno = 0;
290 mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0);
291 if (errno != 0) {
Josh Gao6ad06b92016-01-28 16:21:12 -0800292 SendSyncFail(s, "bad mode");
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700293 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 }
295
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700296 // Don't delete files before copying if they are not "regular" or symlinks.
297 struct stat st;
298 bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
299 if (do_unlink) {
300 adb_unlink(path.c_str());
301 }
302
303 if (S_ISLNK(mode)) {
304 return handle_send_link(s, path.c_str(), buffer);
305 }
306
307 // Copy user permission bits to "group" and "other" permissions.
308 mode &= 0777;
309 mode |= ((mode >> 3) & 0070);
310 mode |= ((mode >> 3) & 0007);
311
Elliott Hughes0a049b12015-01-12 14:26:36 -0800312 uid_t uid = -1;
313 gid_t gid = -1;
314 uint64_t cap = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000315 if (should_use_fs_config(path)) {
Elliott Hughes7baecbe2015-08-25 11:09:04 -0700316 unsigned int broken_api_hack = mode;
Elliott Hughes9bc80052015-08-25 11:21:58 -0700317 fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &cap);
Elliott Hughes56bf3092015-08-25 11:01:39 -0700318 mode = broken_api_hack;
Elliott Hughes0a049b12015-01-12 14:26:36 -0800319 }
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700320 return handle_send_file(s, path.c_str(), uid, gid, mode, buffer, do_unlink);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321}
322
Elliott Hughesaa245492015-08-03 10:38:08 -0700323static bool do_recv(int s, const char* path, std::vector<char>& buffer) {
Rubin Xud61a25c2016-01-11 10:23:47 +0000324 __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path);
325
Elliott Hughesaa245492015-08-03 10:38:08 -0700326 int fd = adb_open(path, O_RDONLY | O_CLOEXEC);
327 if (fd < 0) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700328 SendSyncFailErrno(s, "open failed");
329 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 }
331
Elliott Hughesaa245492015-08-03 10:38:08 -0700332 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 msg.data.id = ID_DATA;
Elliott Hughesaa245492015-08-03 10:38:08 -0700334 while (true) {
335 int r = adb_read(fd, &buffer[0], buffer.size());
336 if (r <= 0) {
337 if (r == 0) break;
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700338 SendSyncFailErrno(s, "read failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 adb_close(fd);
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700340 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700342 msg.data.size = r;
Elliott Hughesaa245492015-08-03 10:38:08 -0700343 if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 adb_close(fd);
Elliott Hughesaa245492015-08-03 10:38:08 -0700345 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 }
347 }
348
349 adb_close(fd);
350
351 msg.data.id = ID_DONE;
352 msg.data.size = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700353 return WriteFdExactly(s, &msg.data, sizeof(msg.data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354}
355
Elliott Hughesaa245492015-08-03 10:38:08 -0700356static bool handle_sync_command(int fd, std::vector<char>& buffer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700357 D("sync: waiting for request");
Elliott Hughesaa245492015-08-03 10:38:08 -0700358
359 SyncRequest request;
360 if (!ReadFdExactly(fd, &request, sizeof(request))) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700361 SendSyncFail(fd, "command read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700362 return false;
363 }
Elliott Hughesf4465202015-08-24 14:27:03 -0700364 size_t path_length = request.path_length;
Elliott Hughesaa245492015-08-03 10:38:08 -0700365 if (path_length > 1024) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700366 SendSyncFail(fd, "path too long");
Elliott Hughesaa245492015-08-03 10:38:08 -0700367 return false;
368 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 char name[1025];
Elliott Hughesaa245492015-08-03 10:38:08 -0700370 if (!ReadFdExactly(fd, name, path_length)) {
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700371 SendSyncFail(fd, "filename read failure");
Elliott Hughesaa245492015-08-03 10:38:08 -0700372 return false;
373 }
374 name[path_length] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375
Elliott Hughesaa245492015-08-03 10:38:08 -0700376 const char* id = reinterpret_cast<const char*>(&request.id);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700377 D("sync: '%.4s' '%s'", id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Elliott Hughesaa245492015-08-03 10:38:08 -0700379 switch (request.id) {
380 case ID_STAT:
381 if (!do_stat(fd, name)) return false;
382 break;
383 case ID_LIST:
384 if (!do_list(fd, name)) return false;
385 break;
386 case ID_SEND:
387 if (!do_send(fd, name, buffer)) return false;
388 break;
389 case ID_RECV:
390 if (!do_recv(fd, name, buffer)) return false;
391 break;
392 case ID_QUIT:
393 return false;
394 default:
Elliott Hughesfdd4e582015-08-24 19:30:46 -0700395 SendSyncFail(fd, android::base::StringPrintf("unknown command '%.4s' (%08x)",
Elliott Hughesaa245492015-08-03 10:38:08 -0700396 id, request.id));
397 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 }
399
Elliott Hughesaa245492015-08-03 10:38:08 -0700400 return true;
401}
402
403void file_sync_service(int fd, void* cookie) {
404 std::vector<char> buffer(SYNC_DATA_MAX);
405
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800406 // If there's a problem on the device, we'll send an ID_FAIL message and
407 // close the socket. Unfortunately the kernel will sometimes throw that
408 // data away if the other end keeps writing without reading (which is
409 // the normal case with our protocol --- they won't read until the end).
410 // So set SO_LINGER to give the client 20s to get around to reading our
411 // failure response. Without this, the other side's ability to report
412 // useful errors is reduced.
413 struct linger l;
414 l.l_onoff = 1;
415 l.l_linger = 20;
416 adb_setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
417
Elliott Hughesaa245492015-08-03 10:38:08 -0700418 while (handle_sync_command(fd, buffer)) {
419 }
420
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700421 D("sync: done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 adb_close(fd);
423}