The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SYNC |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "file_sync_service.h" |
| 21 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame^] | 24 | #include <log/log.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 25 | #include <selinux/android.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 27 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 31 | #include <unistd.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 32 | #include <utime.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include "adb.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 35 | #include "adb_io.h" |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 36 | #include "adb_utils.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 37 | #include "private/android_filesystem_config.h" |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame^] | 38 | #include "security_log_tags.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 40 | #include <android-base/stringprintf.h> |
| 41 | #include <android-base/strings.h> |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 42 | |
| 43 | static bool should_use_fs_config(const std::string& path) { |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 44 | // TODO: use fs_config to configure permissions on /data. |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 45 | return android::base::StartsWith(path, "/system/") || |
| 46 | android::base::StartsWith(path, "/vendor/") || |
| 47 | android::base::StartsWith(path, "/oem/"); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 50 | static bool secure_mkdirs(const std::string& path) { |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 51 | uid_t uid = -1; |
| 52 | gid_t gid = -1; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 53 | unsigned int mode = 0775; |
| 54 | uint64_t cap = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 56 | if (path[0] != '/') return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 58 | std::vector<std::string> path_components = android::base::Split(path, "/"); |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 59 | std::string partial_path; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 60 | for (const auto& path_component : path_components) { |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 61 | 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 Hughes | e0b43a1 | 2015-07-30 20:07:12 -0700 | [diff] [blame] | 65 | fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &cap); |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 66 | } |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 67 | if (adb_mkdir(partial_path.c_str(), mode) == -1) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 68 | if (errno != EEXIST) { |
| 69 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 70 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 71 | } else { |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 72 | if (chown(partial_path.c_str(), uid, gid) == -1) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 73 | return false; |
| 74 | } |
Elliott Hughes | 3834440 | 2015-08-25 16:33:50 -0700 | [diff] [blame] | 75 | // Not all filesystems support setting SELinux labels. http://b/23530370. |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 76 | selinux_android_restorecon(partial_path.c_str(), 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 79 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 82 | static bool do_stat(int s, const char* path) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | msg.stat.id = ID_STAT; |
| 85 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 86 | 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 Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 90 | msg.stat.mode = st.st_mode; |
| 91 | msg.stat.size = st.st_size; |
| 92 | msg.stat.time = st.st_mtime; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 94 | return WriteFdExactly(s, &msg.stat, sizeof(msg.stat)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 97 | static bool do_list(int s, const char* path) { |
| 98 | dirent* de; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 100 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | msg.dent.id = ID_DENT; |
| 102 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 103 | std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir); |
| 104 | if (!d) goto done; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 106 | while ((de = readdir(d.get()))) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 107 | std::string filename(android::base::StringPrintf("%s/%s", path, de->d_name)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 109 | struct stat st; |
| 110 | if (lstat(filename.c_str(), &st) == 0) { |
| 111 | size_t d_name_length = strlen(de->d_name); |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 112 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 117 | if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) || |
| 118 | !WriteFdExactly(s, de->d_name, d_name_length)) { |
| 119 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 | done: |
| 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 Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 130 | return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 133 | static bool SendSyncFail(int fd, const std::string& reason) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 134 | D("sync: failure: %s", reason.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 135 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | msg.data.id = ID_FAIL; |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 138 | msg.data.size = reason.size(); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 139 | return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 142 | static bool SendSyncFailErrno(int fd, const std::string& reason) { |
| 143 | return SendSyncFail(fd, android::base::StringPrintf("%s: %s", reason.c_str(), strerror(errno))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 146 | static bool handle_send_file(int s, const char* path, uid_t uid, |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 147 | gid_t gid, mode_t mode, std::vector<char>& buffer, bool do_unlink) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | syncmsg msg; |
| 149 | unsigned int timestamp = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame^] | 151 | __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path); |
| 152 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 153 | int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 154 | if (fd < 0 && errno == ENOENT) { |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 155 | if (!secure_mkdirs(adb_dirname(path))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 156 | SendSyncFailErrno(s, "secure_mkdirs failed"); |
| 157 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 158 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 159 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 161 | if (fd < 0 && errno == EEXIST) { |
Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 162 | fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 164 | if (fd < 0) { |
| 165 | SendSyncFailErrno(s, "couldn't create file"); |
| 166 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 167 | } else { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 168 | if (fchown(fd, uid, gid) == -1) { |
| 169 | SendSyncFailErrno(s, "fchown failed"); |
| 170 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 171 | } |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 172 | |
Elliott Hughes | 3834440 | 2015-08-25 16:33:50 -0700 | [diff] [blame] | 173 | // Not all filesystems support setting SELinux labels. http://b/23530370. |
| 174 | selinux_android_restorecon(path, 0); |
Elliott Hughes | 0adc097 | 2015-08-25 13:14:07 -0700 | [diff] [blame] | 175 | |
| 176 | // fchown clears the setuid bit - restore it if present. |
| 177 | // Ignore the result of calling fchmod. It's not supported |
| 178 | // by all filesystems. b/12441485 |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 179 | fchmod(fd, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 182 | while (true) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | unsigned int len; |
| 184 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 185 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 187 | if (msg.data.id != ID_DATA) { |
| 188 | if (msg.data.id == ID_DONE) { |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 189 | timestamp = msg.data.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | break; |
| 191 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 192 | SendSyncFail(s, "invalid data message"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | goto fail; |
| 194 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 195 | len = msg.data.size; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 196 | if (len > buffer.size()) { // TODO: resize buffer? |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 197 | SendSyncFail(s, "oversize data message"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | goto fail; |
| 199 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 200 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 201 | if (!ReadFdExactly(s, &buffer[0], len)) goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 203 | if (!WriteFdExactly(fd, &buffer[0], len)) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 204 | SendSyncFailErrno(s, "write failed"); |
| 205 | goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 209 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 211 | utimbuf u; |
| 212 | u.actime = timestamp; |
| 213 | u.modtime = timestamp; |
| 214 | utime(path, &u); |
| 215 | |
| 216 | msg.status.id = ID_OKAY; |
| 217 | msg.status.msglen = 0; |
| 218 | return WriteFdExactly(s, &msg.status, sizeof(msg.status)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | |
| 220 | fail: |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 221 | if (fd >= 0) adb_close(fd); |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 222 | if (do_unlink) adb_unlink(path); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 223 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 226 | #if defined(_WIN32) |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 227 | extern bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) __attribute__((error("no symlinks on Windows"))); |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 228 | #else |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 229 | static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | syncmsg msg; |
| 231 | unsigned int len; |
| 232 | int ret; |
| 233 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 234 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 236 | if (msg.data.id != ID_DATA) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 237 | SendSyncFail(s, "invalid data message: expected ID_DATA"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 238 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 241 | len = msg.data.size; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 242 | if (len > buffer.size()) { // TODO: resize buffer? |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 243 | SendSyncFail(s, "oversize data message"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 244 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 245 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 246 | if (!ReadFdExactly(s, &buffer[0], len)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 248 | ret = symlink(&buffer[0], path.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 249 | if (ret && errno == ENOENT) { |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 250 | if (!secure_mkdirs(adb_dirname(path))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 251 | SendSyncFailErrno(s, "secure_mkdirs failed"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 252 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 253 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 254 | ret = symlink(&buffer[0], path.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 256 | if (ret) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 257 | SendSyncFailErrno(s, "symlink failed"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 258 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 261 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 263 | if (msg.data.id == ID_DONE) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | msg.status.id = ID_OKAY; |
| 265 | msg.status.msglen = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 266 | if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | } else { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 268 | SendFail(s, "invalid data message: expected ID_DONE"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 269 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 272 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | } |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 274 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 276 | static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) { |
| 277 | // 'spec' is of the form "/some/path,0755". Break it up. |
| 278 | size_t comma = spec.find_last_of(','); |
| 279 | if (comma == std::string::npos) { |
| 280 | SendFail(s, "missing , in ID_SEND"); |
| 281 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 284 | std::string path = spec.substr(0, comma); |
| 285 | |
| 286 | errno = 0; |
| 287 | mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0); |
| 288 | if (errno != 0) { |
| 289 | SendFail(s, "bad mode"); |
| 290 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 293 | // Don't delete files before copying if they are not "regular" or symlinks. |
| 294 | struct stat st; |
| 295 | bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode); |
| 296 | if (do_unlink) { |
| 297 | adb_unlink(path.c_str()); |
| 298 | } |
| 299 | |
| 300 | if (S_ISLNK(mode)) { |
| 301 | return handle_send_link(s, path.c_str(), buffer); |
| 302 | } |
| 303 | |
| 304 | // Copy user permission bits to "group" and "other" permissions. |
| 305 | mode &= 0777; |
| 306 | mode |= ((mode >> 3) & 0070); |
| 307 | mode |= ((mode >> 3) & 0007); |
| 308 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 309 | uid_t uid = -1; |
| 310 | gid_t gid = -1; |
| 311 | uint64_t cap = 0; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 312 | if (should_use_fs_config(path)) { |
Elliott Hughes | 7baecbe | 2015-08-25 11:09:04 -0700 | [diff] [blame] | 313 | unsigned int broken_api_hack = mode; |
Elliott Hughes | 9bc8005 | 2015-08-25 11:21:58 -0700 | [diff] [blame] | 314 | fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &cap); |
Elliott Hughes | 56bf309 | 2015-08-25 11:01:39 -0700 | [diff] [blame] | 315 | mode = broken_api_hack; |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 316 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 317 | return handle_send_file(s, path.c_str(), uid, gid, mode, buffer, do_unlink); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 320 | static bool do_recv(int s, const char* path, std::vector<char>& buffer) { |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame^] | 321 | __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path); |
| 322 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 323 | int fd = adb_open(path, O_RDONLY | O_CLOEXEC); |
| 324 | if (fd < 0) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 325 | SendSyncFailErrno(s, "open failed"); |
| 326 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 329 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | msg.data.id = ID_DATA; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 331 | while (true) { |
| 332 | int r = adb_read(fd, &buffer[0], buffer.size()); |
| 333 | if (r <= 0) { |
| 334 | if (r == 0) break; |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 335 | SendSyncFailErrno(s, "read failed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | adb_close(fd); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 337 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 339 | msg.data.size = r; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 340 | if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 341 | adb_close(fd); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 342 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | adb_close(fd); |
| 347 | |
| 348 | msg.data.id = ID_DONE; |
| 349 | msg.data.size = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 350 | return WriteFdExactly(s, &msg.data, sizeof(msg.data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 353 | static bool handle_sync_command(int fd, std::vector<char>& buffer) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 354 | D("sync: waiting for request"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 355 | |
| 356 | SyncRequest request; |
| 357 | if (!ReadFdExactly(fd, &request, sizeof(request))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 358 | SendSyncFail(fd, "command read failure"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 359 | return false; |
| 360 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 361 | size_t path_length = request.path_length; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 362 | if (path_length > 1024) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 363 | SendSyncFail(fd, "path too long"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 364 | return false; |
| 365 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | char name[1025]; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 367 | if (!ReadFdExactly(fd, name, path_length)) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 368 | SendSyncFail(fd, "filename read failure"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | name[path_length] = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 372 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 373 | const char* id = reinterpret_cast<const char*>(&request.id); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 374 | D("sync: '%.4s' '%s'", id, name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 375 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 376 | switch (request.id) { |
| 377 | case ID_STAT: |
| 378 | if (!do_stat(fd, name)) return false; |
| 379 | break; |
| 380 | case ID_LIST: |
| 381 | if (!do_list(fd, name)) return false; |
| 382 | break; |
| 383 | case ID_SEND: |
| 384 | if (!do_send(fd, name, buffer)) return false; |
| 385 | break; |
| 386 | case ID_RECV: |
| 387 | if (!do_recv(fd, name, buffer)) return false; |
| 388 | break; |
| 389 | case ID_QUIT: |
| 390 | return false; |
| 391 | default: |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 392 | SendSyncFail(fd, android::base::StringPrintf("unknown command '%.4s' (%08x)", |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 393 | id, request.id)); |
| 394 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
| 400 | void file_sync_service(int fd, void* cookie) { |
| 401 | std::vector<char> buffer(SYNC_DATA_MAX); |
| 402 | |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 403 | // If there's a problem on the device, we'll send an ID_FAIL message and |
| 404 | // close the socket. Unfortunately the kernel will sometimes throw that |
| 405 | // data away if the other end keeps writing without reading (which is |
| 406 | // the normal case with our protocol --- they won't read until the end). |
| 407 | // So set SO_LINGER to give the client 20s to get around to reading our |
| 408 | // failure response. Without this, the other side's ability to report |
| 409 | // useful errors is reduced. |
| 410 | struct linger l; |
| 411 | l.l_onoff = 1; |
| 412 | l.l_linger = 20; |
| 413 | adb_setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 414 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 415 | while (handle_sync_command(fd, buffer)) { |
| 416 | } |
| 417 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 418 | D("sync: done"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | adb_close(fd); |
| 420 | } |