blob: c30f9fb262fbdc1d17e7e318a1fb44235972c665 [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;
Liang Cheng20d33f42014-01-02 18:27:51 -080046 unsigned int uid, gid;
47 unsigned int mode = 0775;
48 uint64_t cap = 0;
49 uid = getuid();
50 gid = getgid();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52 if(name[0] != '/') return -1;
53
54 for(;;) {
55 x = adb_dirstart(x);
56 if(x == 0) return 0;
57 *x = 0;
Liang Cheng20d33f42014-01-02 18:27:51 -080058 if (is_on_system(name)) {
59 fs_config(name, 1, &uid, &gid, &mode, &cap);
60 }
61 ret = adb_mkdir(name, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062 if((ret < 0) && (errno != EEXIST)) {
63 D("mkdir(\"%s\") -> %s\n", name, strerror(errno));
64 *x = '/';
65 return ret;
Liang Cheng20d33f42014-01-02 18:27:51 -080066 } else if(ret == 0) {
67 ret = chown(name, uid, gid);
68 if (ret < 0) {
69 *x = '/';
70 return ret;
71 }
72 selinux_android_restorecon(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 }
74 *x++ = '/';
75 }
76 return 0;
77}
78
79static int do_stat(int s, const char *path)
80{
81 syncmsg msg;
82 struct stat st;
83
84 msg.stat.id = ID_STAT;
85
86 if(lstat(path, &st)) {
87 msg.stat.mode = 0;
88 msg.stat.size = 0;
89 msg.stat.time = 0;
90 } else {
91 msg.stat.mode = htoll(st.st_mode);
92 msg.stat.size = htoll(st.st_size);
93 msg.stat.time = htoll(st.st_mtime);
94 }
95
96 return writex(s, &msg.stat, sizeof(msg.stat));
97}
98
99static int do_list(int s, const char *path)
100{
101 DIR *d;
102 struct dirent *de;
103 struct stat st;
104 syncmsg msg;
105 int len;
106
107 char tmp[1024 + 256 + 1];
108 char *fname;
109
110 len = strlen(path);
111 memcpy(tmp, path, len);
112 tmp[len] = '/';
113 fname = tmp + len + 1;
114
115 msg.dent.id = ID_DENT;
116
117 d = opendir(path);
118 if(d == 0) goto done;
119
120 while((de = readdir(d))) {
121 int len = strlen(de->d_name);
122
123 /* not supposed to be possible, but
124 if it does happen, let's not buffer overrun */
125 if(len > 256) continue;
126
127 strcpy(fname, de->d_name);
128 if(lstat(tmp, &st) == 0) {
129 msg.dent.mode = htoll(st.st_mode);
130 msg.dent.size = htoll(st.st_size);
131 msg.dent.time = htoll(st.st_mtime);
132 msg.dent.namelen = htoll(len);
133
134 if(writex(s, &msg.dent, sizeof(msg.dent)) ||
135 writex(s, de->d_name, len)) {
Elliott Hughes14e28d32013-10-29 14:12:46 -0700136 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 return -1;
138 }
139 }
140 }
141
142 closedir(d);
143
144done:
145 msg.dent.id = ID_DONE;
146 msg.dent.mode = 0;
147 msg.dent.size = 0;
148 msg.dent.time = 0;
149 msg.dent.namelen = 0;
150 return writex(s, &msg.dent, sizeof(msg.dent));
151}
152
153static int fail_message(int s, const char *reason)
154{
155 syncmsg msg;
156 int len = strlen(reason);
157
158 D("sync: failure: %s\n", reason);
159
160 msg.data.id = ID_FAIL;
161 msg.data.size = htoll(len);
162 if(writex(s, &msg.data, sizeof(msg.data)) ||
163 writex(s, reason, len)) {
164 return -1;
165 } else {
166 return 0;
167 }
168}
169
170static int fail_errno(int s)
171{
172 return fail_message(s, strerror(errno));
173}
174
Liang Cheng20d33f42014-01-02 18:27:51 -0800175static int handle_send_file(int s, char *path, unsigned int uid,
176 unsigned int gid, mode_t mode, char *buffer)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177{
178 syncmsg msg;
179 unsigned int timestamp = 0;
180 int fd;
181
182 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
183 if(fd < 0 && errno == ENOENT) {
Liang Cheng20d33f42014-01-02 18:27:51 -0800184 if(mkdirs(path) != 0) {
185 if(fail_errno(s))
186 return -1;
187 fd = -1;
188 } else {
189 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
190 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 }
192 if(fd < 0 && errno == EEXIST) {
193 fd = adb_open_mode(path, O_WRONLY, mode);
194 }
195 if(fd < 0) {
196 if(fail_errno(s))
197 return -1;
198 fd = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -0800199 } else {
200 if(fchown(fd, uid, gid) != 0) {
201 fail_errno(s);
202 errno = 0;
203 }
204 /* fchown clears the setuid bit - restore it if present */
205 if(fchmod(fd, mode) != 0) {
206 fail_errno(s);
207 errno = 0;
208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
210
211 for(;;) {
212 unsigned int len;
213
214 if(readx(s, &msg.data, sizeof(msg.data)))
215 goto fail;
216
217 if(msg.data.id != ID_DATA) {
218 if(msg.data.id == ID_DONE) {
219 timestamp = ltohl(msg.data.size);
220 break;
221 }
222 fail_message(s, "invalid data message");
223 goto fail;
224 }
225 len = ltohl(msg.data.size);
226 if(len > SYNC_DATA_MAX) {
227 fail_message(s, "oversize data message");
228 goto fail;
229 }
230 if(readx(s, buffer, len))
231 goto fail;
232
233 if(fd < 0)
234 continue;
235 if(writex(fd, buffer, len)) {
JP Abgrall408fa572011-03-16 15:57:42 -0700236 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 adb_close(fd);
238 adb_unlink(path);
239 fd = -1;
JP Abgrall408fa572011-03-16 15:57:42 -0700240 errno = saved_errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 if(fail_errno(s)) return -1;
242 }
243 }
244
245 if(fd >= 0) {
246 struct utimbuf u;
247 adb_close(fd);
Liang Cheng20d33f42014-01-02 18:27:51 -0800248 selinux_android_restorecon(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 u.actime = timestamp;
250 u.modtime = timestamp;
251 utime(path, &u);
252
253 msg.status.id = ID_OKAY;
254 msg.status.msglen = 0;
255 if(writex(s, &msg.status, sizeof(msg.status)))
256 return -1;
257 }
258 return 0;
259
260fail:
261 if(fd >= 0)
262 adb_close(fd);
263 adb_unlink(path);
264 return -1;
265}
266
267#ifdef HAVE_SYMLINKS
268static int handle_send_link(int s, char *path, char *buffer)
269{
270 syncmsg msg;
271 unsigned int len;
272 int ret;
273
274 if(readx(s, &msg.data, sizeof(msg.data)))
275 return -1;
276
277 if(msg.data.id != ID_DATA) {
278 fail_message(s, "invalid data message: expected ID_DATA");
279 return -1;
280 }
281
282 len = ltohl(msg.data.size);
283 if(len > SYNC_DATA_MAX) {
284 fail_message(s, "oversize data message");
285 return -1;
286 }
287 if(readx(s, buffer, len))
288 return -1;
289
290 ret = symlink(buffer, path);
291 if(ret && errno == ENOENT) {
Liang Cheng20d33f42014-01-02 18:27:51 -0800292 if(mkdirs(path) != 0) {
293 fail_errno(s);
294 return -1;
295 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 ret = symlink(buffer, path);
297 }
298 if(ret) {
299 fail_errno(s);
300 return -1;
301 }
302
303 if(readx(s, &msg.data, sizeof(msg.data)))
304 return -1;
305
306 if(msg.data.id == ID_DONE) {
307 msg.status.id = ID_OKAY;
308 msg.status.msglen = 0;
309 if(writex(s, &msg.status, sizeof(msg.status)))
310 return -1;
311 } else {
312 fail_message(s, "invalid data message: expected ID_DONE");
313 return -1;
314 }
315
316 return 0;
317}
318#endif /* HAVE_SYMLINKS */
319
320static int do_send(int s, char *path, char *buffer)
321{
322 char *tmp;
Liang Cheng20d33f42014-01-02 18:27:51 -0800323 unsigned int mode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 int is_link, ret;
325
326 tmp = strrchr(path,',');
327 if(tmp) {
328 *tmp = 0;
329 errno = 0;
330 mode = strtoul(tmp + 1, NULL, 0);
331#ifndef HAVE_SYMLINKS
332 is_link = 0;
333#else
Liang Cheng20d33f42014-01-02 18:27:51 -0800334 is_link = S_ISLNK((mode_t) mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335#endif
336 mode &= 0777;
337 }
338 if(!tmp || errno) {
339 mode = 0644;
340 is_link = 0;
341 }
342
343 adb_unlink(path);
344
345
346#ifdef HAVE_SYMLINKS
347 if(is_link)
348 ret = handle_send_link(s, path, buffer);
349 else {
350#else
351 {
352#endif
Liang Cheng20d33f42014-01-02 18:27:51 -0800353 unsigned int uid, gid;
354 uint64_t cap = 0;
355 uid = getuid();
356 gid = getgid();
357
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 /* copy user permission bits to "group" and "other" permissions */
359 mode |= ((mode >> 3) & 0070);
360 mode |= ((mode >> 3) & 0007);
361
Liang Cheng20d33f42014-01-02 18:27:51 -0800362 tmp = path;
363 if(*tmp == '/') {
364 tmp++;
365 }
366 if (is_on_system(path)) {
367 fs_config(tmp, 0, &uid, &gid, &mode, &cap);
368 }
369 ret = handle_send_file(s, path, uid, gid, mode, buffer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 }
371
372 return ret;
373}
374
375static int do_recv(int s, const char *path, char *buffer)
376{
377 syncmsg msg;
378 int fd, r;
379
380 fd = adb_open(path, O_RDONLY);
381 if(fd < 0) {
382 if(fail_errno(s)) return -1;
383 return 0;
384 }
385
386 msg.data.id = ID_DATA;
387 for(;;) {
388 r = adb_read(fd, buffer, SYNC_DATA_MAX);
389 if(r <= 0) {
390 if(r == 0) break;
391 if(errno == EINTR) continue;
392 r = fail_errno(s);
393 adb_close(fd);
394 return r;
395 }
396 msg.data.size = htoll(r);
397 if(writex(s, &msg.data, sizeof(msg.data)) ||
398 writex(s, buffer, r)) {
399 adb_close(fd);
400 return -1;
401 }
402 }
403
404 adb_close(fd);
405
406 msg.data.id = ID_DONE;
407 msg.data.size = 0;
408 if(writex(s, &msg.data, sizeof(msg.data))) {
409 return -1;
410 }
411
412 return 0;
413}
414
415void file_sync_service(int fd, void *cookie)
416{
417 syncmsg msg;
418 char name[1025];
419 unsigned namelen;
420
421 char *buffer = malloc(SYNC_DATA_MAX);
422 if(buffer == 0) goto fail;
423
424 for(;;) {
425 D("sync: waiting for command\n");
426
427 if(readx(fd, &msg.req, sizeof(msg.req))) {
428 fail_message(fd, "command read failure");
429 break;
430 }
431 namelen = ltohl(msg.req.namelen);
432 if(namelen > 1024) {
433 fail_message(fd, "invalid namelen");
434 break;
435 }
436 if(readx(fd, name, namelen)) {
437 fail_message(fd, "filename read failure");
438 break;
439 }
440 name[namelen] = 0;
441
442 msg.req.namelen = 0;
443 D("sync: '%s' '%s'\n", (char*) &msg.req, name);
444
445 switch(msg.req.id) {
446 case ID_STAT:
447 if(do_stat(fd, name)) goto fail;
448 break;
449 case ID_LIST:
450 if(do_list(fd, name)) goto fail;
451 break;
452 case ID_SEND:
453 if(do_send(fd, name, buffer)) goto fail;
454 break;
455 case ID_RECV:
456 if(do_recv(fd, name, buffer)) goto fail;
457 break;
458 case ID_QUIT:
459 goto fail;
460 default:
461 fail_message(fd, "unknown command");
462 goto fail;
463 }
464 }
465
466fail:
467 if(buffer != 0) free(buffer);
468 D("sync: done\n");
469 adb_close(fd);
470}