blob: 1d80d26acf0aec873b5c0b1bd7cef49d96fbb95b [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
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <dirent.h>
24#include <utime.h>
Liang Cheng20d33f42014-01-02 18:27:51 -080025#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27#include <errno.h>
Liang Cheng20d33f42014-01-02 18:27:51 -080028#include <private/android_filesystem_config.h>
29#include <selinux/android.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include "sysdeps.h"
31
32#define TRACE_TAG TRACE_SYNC
33#include "adb.h"
34#include "file_sync_service.h"
35
Liang Cheng20d33f42014-01-02 18:27:51 -080036/* TODO: use fs_config to configure permissions on /data */
37static bool is_on_system(const char *name) {
38 const char *SYSTEM = "/system/";
39 return (strncmp(SYSTEM, name, strlen(SYSTEM)) == 0);
40}
41
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042static int mkdirs(char *name)
43{
44 int ret;
45 char *x = name + 1;
Nick Kralevich72917832014-01-17 16:16:42 -080046 uid_t uid = -1;
47 gid_t gid = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -080048 unsigned int mode = 0775;
49 uint64_t cap = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
51 if(name[0] != '/') return -1;
52
53 for(;;) {
54 x = adb_dirstart(x);
55 if(x == 0) return 0;
56 *x = 0;
Liang Cheng20d33f42014-01-02 18:27:51 -080057 if (is_on_system(name)) {
58 fs_config(name, 1, &uid, &gid, &mode, &cap);
59 }
60 ret = adb_mkdir(name, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 if((ret < 0) && (errno != EEXIST)) {
62 D("mkdir(\"%s\") -> %s\n", name, strerror(errno));
63 *x = '/';
64 return ret;
Liang Cheng20d33f42014-01-02 18:27:51 -080065 } else if(ret == 0) {
66 ret = chown(name, uid, gid);
67 if (ret < 0) {
68 *x = '/';
69 return ret;
70 }
Stephen Smalley27a93652014-02-07 09:14:13 -050071 selinux_android_restorecon(name, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 }
73 *x++ = '/';
74 }
75 return 0;
76}
77
78static int do_stat(int s, const char *path)
79{
80 syncmsg msg;
81 struct stat st;
82
83 msg.stat.id = ID_STAT;
84
85 if(lstat(path, &st)) {
86 msg.stat.mode = 0;
87 msg.stat.size = 0;
88 msg.stat.time = 0;
89 } else {
90 msg.stat.mode = htoll(st.st_mode);
91 msg.stat.size = htoll(st.st_size);
92 msg.stat.time = htoll(st.st_mtime);
93 }
94
95 return writex(s, &msg.stat, sizeof(msg.stat));
96}
97
98static int do_list(int s, const char *path)
99{
100 DIR *d;
101 struct dirent *de;
102 struct stat st;
103 syncmsg msg;
104 int len;
105
106 char tmp[1024 + 256 + 1];
107 char *fname;
108
109 len = strlen(path);
110 memcpy(tmp, path, len);
111 tmp[len] = '/';
112 fname = tmp + len + 1;
113
114 msg.dent.id = ID_DENT;
115
116 d = opendir(path);
117 if(d == 0) goto done;
118
119 while((de = readdir(d))) {
120 int len = strlen(de->d_name);
121
122 /* not supposed to be possible, but
123 if it does happen, let's not buffer overrun */
124 if(len > 256) continue;
125
126 strcpy(fname, de->d_name);
127 if(lstat(tmp, &st) == 0) {
128 msg.dent.mode = htoll(st.st_mode);
129 msg.dent.size = htoll(st.st_size);
130 msg.dent.time = htoll(st.st_mtime);
131 msg.dent.namelen = htoll(len);
132
133 if(writex(s, &msg.dent, sizeof(msg.dent)) ||
134 writex(s, de->d_name, len)) {
Elliott Hughes14e28d32013-10-29 14:12:46 -0700135 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 return -1;
137 }
138 }
139 }
140
141 closedir(d);
142
143done:
144 msg.dent.id = ID_DONE;
145 msg.dent.mode = 0;
146 msg.dent.size = 0;
147 msg.dent.time = 0;
148 msg.dent.namelen = 0;
149 return writex(s, &msg.dent, sizeof(msg.dent));
150}
151
152static int fail_message(int s, const char *reason)
153{
154 syncmsg msg;
155 int len = strlen(reason);
156
157 D("sync: failure: %s\n", reason);
158
159 msg.data.id = ID_FAIL;
160 msg.data.size = htoll(len);
161 if(writex(s, &msg.data, sizeof(msg.data)) ||
162 writex(s, reason, len)) {
163 return -1;
164 } else {
165 return 0;
166 }
167}
168
169static int fail_errno(int s)
170{
171 return fail_message(s, strerror(errno));
172}
173
Nick Kralevich72917832014-01-17 16:16:42 -0800174static int handle_send_file(int s, char *path, uid_t uid,
JP Abgrall55b6c842014-03-07 17:31:25 -0800175 gid_t gid, mode_t mode, char *buffer, bool do_unlink)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176{
177 syncmsg msg;
178 unsigned int timestamp = 0;
179 int fd;
180
181 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
182 if(fd < 0 && errno == ENOENT) {
Liang Cheng20d33f42014-01-02 18:27:51 -0800183 if(mkdirs(path) != 0) {
184 if(fail_errno(s))
185 return -1;
186 fd = -1;
187 } else {
188 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
189 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 }
191 if(fd < 0 && errno == EEXIST) {
192 fd = adb_open_mode(path, O_WRONLY, mode);
193 }
194 if(fd < 0) {
195 if(fail_errno(s))
196 return -1;
197 fd = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -0800198 } else {
199 if(fchown(fd, uid, gid) != 0) {
200 fail_errno(s);
201 errno = 0;
202 }
Nick Kralevich72917832014-01-17 16:16:42 -0800203
204 /*
205 * fchown clears the setuid bit - restore it if present.
206 * Ignore the result of calling fchmod. It's not supported
207 * by all filesystems. b/12441485
208 */
209 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 }
211
212 for(;;) {
213 unsigned int len;
214
215 if(readx(s, &msg.data, sizeof(msg.data)))
216 goto fail;
217
218 if(msg.data.id != ID_DATA) {
219 if(msg.data.id == ID_DONE) {
220 timestamp = ltohl(msg.data.size);
221 break;
222 }
223 fail_message(s, "invalid data message");
224 goto fail;
225 }
226 len = ltohl(msg.data.size);
227 if(len > SYNC_DATA_MAX) {
228 fail_message(s, "oversize data message");
229 goto fail;
230 }
231 if(readx(s, buffer, len))
232 goto fail;
233
234 if(fd < 0)
235 continue;
236 if(writex(fd, buffer, len)) {
JP Abgrall408fa572011-03-16 15:57:42 -0700237 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800239 if (do_unlink) adb_unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 fd = -1;
JP Abgrall408fa572011-03-16 15:57:42 -0700241 errno = saved_errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 if(fail_errno(s)) return -1;
243 }
244 }
245
246 if(fd >= 0) {
247 struct utimbuf u;
248 adb_close(fd);
Stephen Smalley27a93652014-02-07 09:14:13 -0500249 selinux_android_restorecon(path, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 u.actime = timestamp;
251 u.modtime = timestamp;
252 utime(path, &u);
253
254 msg.status.id = ID_OKAY;
255 msg.status.msglen = 0;
256 if(writex(s, &msg.status, sizeof(msg.status)))
257 return -1;
258 }
259 return 0;
260
261fail:
262 if(fd >= 0)
263 adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800264 if (do_unlink) adb_unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 return -1;
266}
267
268#ifdef HAVE_SYMLINKS
269static int handle_send_link(int s, char *path, char *buffer)
270{
271 syncmsg msg;
272 unsigned int len;
273 int ret;
274
275 if(readx(s, &msg.data, sizeof(msg.data)))
276 return -1;
277
278 if(msg.data.id != ID_DATA) {
279 fail_message(s, "invalid data message: expected ID_DATA");
280 return -1;
281 }
282
283 len = ltohl(msg.data.size);
284 if(len > SYNC_DATA_MAX) {
285 fail_message(s, "oversize data message");
286 return -1;
287 }
288 if(readx(s, buffer, len))
289 return -1;
290
291 ret = symlink(buffer, path);
292 if(ret && errno == ENOENT) {
Liang Cheng20d33f42014-01-02 18:27:51 -0800293 if(mkdirs(path) != 0) {
294 fail_errno(s);
295 return -1;
296 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 ret = symlink(buffer, path);
298 }
299 if(ret) {
300 fail_errno(s);
301 return -1;
302 }
303
304 if(readx(s, &msg.data, sizeof(msg.data)))
305 return -1;
306
307 if(msg.data.id == ID_DONE) {
308 msg.status.id = ID_OKAY;
309 msg.status.msglen = 0;
310 if(writex(s, &msg.status, sizeof(msg.status)))
311 return -1;
312 } else {
313 fail_message(s, "invalid data message: expected ID_DONE");
314 return -1;
315 }
316
317 return 0;
318}
319#endif /* HAVE_SYMLINKS */
320
321static int do_send(int s, char *path, char *buffer)
322{
323 char *tmp;
Liang Cheng20d33f42014-01-02 18:27:51 -0800324 unsigned int mode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 int is_link, ret;
JP Abgrall55b6c842014-03-07 17:31:25 -0800326 bool do_unlink;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327
328 tmp = strrchr(path,',');
329 if(tmp) {
330 *tmp = 0;
331 errno = 0;
332 mode = strtoul(tmp + 1, NULL, 0);
333#ifndef HAVE_SYMLINKS
334 is_link = 0;
335#else
Liang Cheng20d33f42014-01-02 18:27:51 -0800336 is_link = S_ISLNK((mode_t) mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337#endif
338 mode &= 0777;
339 }
340 if(!tmp || errno) {
341 mode = 0644;
342 is_link = 0;
JP Abgrall55b6c842014-03-07 17:31:25 -0800343 do_unlink = true;
JP Abgrall4ce2f832013-12-03 14:52:39 -0800344 } else {
345 struct stat st;
346 /* Don't delete files before copying if they are not "regular" */
JP Abgrall55b6c842014-03-07 17:31:25 -0800347 do_unlink = lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
348 if (do_unlink) {
JP Abgrall4ce2f832013-12-03 14:52:39 -0800349 adb_unlink(path);
350 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 }
352
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353#ifdef HAVE_SYMLINKS
354 if(is_link)
355 ret = handle_send_link(s, path, buffer);
356 else {
357#else
358 {
359#endif
Nick Kralevich72917832014-01-17 16:16:42 -0800360 uid_t uid = -1;
361 gid_t gid = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -0800362 uint64_t cap = 0;
Liang Cheng20d33f42014-01-02 18:27:51 -0800363
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 /* copy user permission bits to "group" and "other" permissions */
365 mode |= ((mode >> 3) & 0070);
366 mode |= ((mode >> 3) & 0007);
367
Liang Cheng20d33f42014-01-02 18:27:51 -0800368 tmp = path;
369 if(*tmp == '/') {
370 tmp++;
371 }
372 if (is_on_system(path)) {
373 fs_config(tmp, 0, &uid, &gid, &mode, &cap);
374 }
JP Abgrall55b6c842014-03-07 17:31:25 -0800375 ret = handle_send_file(s, path, uid, gid, mode, buffer, do_unlink);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 }
377
378 return ret;
379}
380
381static int do_recv(int s, const char *path, char *buffer)
382{
383 syncmsg msg;
384 int fd, r;
385
386 fd = adb_open(path, O_RDONLY);
387 if(fd < 0) {
388 if(fail_errno(s)) return -1;
389 return 0;
390 }
391
392 msg.data.id = ID_DATA;
393 for(;;) {
394 r = adb_read(fd, buffer, SYNC_DATA_MAX);
395 if(r <= 0) {
396 if(r == 0) break;
397 if(errno == EINTR) continue;
398 r = fail_errno(s);
399 adb_close(fd);
400 return r;
401 }
402 msg.data.size = htoll(r);
403 if(writex(s, &msg.data, sizeof(msg.data)) ||
404 writex(s, buffer, r)) {
405 adb_close(fd);
406 return -1;
407 }
408 }
409
410 adb_close(fd);
411
412 msg.data.id = ID_DONE;
413 msg.data.size = 0;
414 if(writex(s, &msg.data, sizeof(msg.data))) {
415 return -1;
416 }
417
418 return 0;
419}
420
421void file_sync_service(int fd, void *cookie)
422{
423 syncmsg msg;
424 char name[1025];
425 unsigned namelen;
426
427 char *buffer = malloc(SYNC_DATA_MAX);
428 if(buffer == 0) goto fail;
429
430 for(;;) {
431 D("sync: waiting for command\n");
432
433 if(readx(fd, &msg.req, sizeof(msg.req))) {
434 fail_message(fd, "command read failure");
435 break;
436 }
437 namelen = ltohl(msg.req.namelen);
438 if(namelen > 1024) {
439 fail_message(fd, "invalid namelen");
440 break;
441 }
442 if(readx(fd, name, namelen)) {
443 fail_message(fd, "filename read failure");
444 break;
445 }
446 name[namelen] = 0;
447
448 msg.req.namelen = 0;
449 D("sync: '%s' '%s'\n", (char*) &msg.req, name);
450
451 switch(msg.req.id) {
452 case ID_STAT:
453 if(do_stat(fd, name)) goto fail;
454 break;
455 case ID_LIST:
456 if(do_list(fd, name)) goto fail;
457 break;
458 case ID_SEND:
459 if(do_send(fd, name, buffer)) goto fail;
460 break;
461 case ID_RECV:
462 if(do_recv(fd, name, buffer)) goto fail;
463 break;
464 case ID_QUIT:
465 goto fail;
466 default:
467 fail_message(fd, "unknown command");
468 goto fail;
469 }
470 }
471
472fail:
473 if(buffer != 0) free(buffer);
474 D("sync: done\n");
475 adb_close(fd);
476}