The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/mount.h> |
| 23 | #include <errno.h> |
| 24 | |
| 25 | #include "sysdeps.h" |
| 26 | |
| 27 | #define TRACE_TAG TRACE_ADB |
| 28 | #include "adb.h" |
| 29 | |
| 30 | |
| 31 | static int system_ro = 1; |
| 32 | |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 33 | /* Returns the device used to mount a directory in /proc/mounts */ |
| 34 | static char *find_mount(const char *dir) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | { |
| 36 | int fd; |
| 37 | int res; |
| 38 | int size; |
| 39 | char *token = NULL; |
| 40 | const char delims[] = "\n"; |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 41 | char buf[4096]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 43 | fd = unix_open("/proc/mounts", O_RDONLY); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | if (fd < 0) |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 45 | return NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
| 47 | buf[sizeof(buf) - 1] = '\0'; |
| 48 | size = adb_read(fd, buf, sizeof(buf) - 1); |
| 49 | adb_close(fd); |
| 50 | |
| 51 | token = strtok(buf, delims); |
| 52 | |
| 53 | while (token) { |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 54 | char mount_dev[256]; |
| 55 | char mount_dir[256]; |
| 56 | int mount_freq; |
| 57 | int mount_passno; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 59 | res = sscanf(token, "%255s %255s %*s %*s %d %d\n", |
| 60 | mount_dev, mount_dir, &mount_freq, &mount_passno); |
| 61 | mount_dev[255] = 0; |
| 62 | mount_dir[255] = 0; |
| 63 | if (res == 4 && (strcmp(dir, mount_dir) == 0)) |
| 64 | return strdup(mount_dev); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
| 66 | token = strtok(NULL, delims); |
| 67 | } |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 68 | return NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* Init mounts /system as read only, remount to enable writes. */ |
| 72 | static int remount_system() |
| 73 | { |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 74 | char *dev; |
| 75 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | if (system_ro == 0) { |
| 77 | return 0; |
| 78 | } |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 79 | |
| 80 | dev = find_mount("/system"); |
| 81 | |
| 82 | if (!dev) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | return -1; |
| 84 | |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 85 | system_ro = mount(dev, "/system", "none", MS_REMOUNT, NULL); |
| 86 | |
| 87 | free(dev); |
| 88 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | return system_ro; |
| 90 | } |
| 91 | |
| 92 | static void write_string(int fd, const char* str) |
| 93 | { |
| 94 | writex(fd, str, strlen(str)); |
| 95 | } |
| 96 | |
| 97 | void remount_service(int fd, void *cookie) |
| 98 | { |
| 99 | int ret = remount_system(); |
| 100 | |
| 101 | if (!ret) |
| 102 | write_string(fd, "remount succeeded\n"); |
| 103 | else { |
| 104 | char buffer[200]; |
| 105 | snprintf(buffer, sizeof(buffer), "remount failed: %s\n", strerror(errno)); |
| 106 | write_string(fd, buffer); |
| 107 | } |
| 108 | |
| 109 | adb_close(fd); |
| 110 | } |
| 111 | |