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