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 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Mark Salyzyn | 63e39f2 | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/mount.h> |
| 23 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | char *token = NULL; |
| 39 | const char delims[] = "\n"; |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 40 | char buf[4096]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Nick Kralevich | 43f94fb | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 42 | fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | if (fd < 0) |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 44 | return NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
| 46 | buf[sizeof(buf) - 1] = '\0'; |
Mark Salyzyn | 63e39f2 | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 47 | adb_read(fd, buf, sizeof(buf) - 1); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | adb_close(fd); |
| 49 | |
| 50 | token = strtok(buf, delims); |
| 51 | |
| 52 | while (token) { |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 53 | char mount_dev[256]; |
| 54 | char mount_dir[256]; |
| 55 | int mount_freq; |
| 56 | int mount_passno; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 58 | res = sscanf(token, "%255s %255s %*s %*s %d %d\n", |
| 59 | mount_dev, mount_dir, &mount_freq, &mount_passno); |
| 60 | mount_dev[255] = 0; |
| 61 | mount_dir[255] = 0; |
| 62 | if (res == 4 && (strcmp(dir, mount_dir) == 0)) |
| 63 | return strdup(mount_dev); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
| 65 | token = strtok(NULL, delims); |
| 66 | } |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 67 | return NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* Init mounts /system as read only, remount to enable writes. */ |
| 71 | static int remount_system() |
| 72 | { |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 73 | char *dev; |
Nick Kralevich | 57eb352 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 74 | int fd; |
| 75 | int OFF = 0; |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 76 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | if (system_ro == 0) { |
| 78 | return 0; |
| 79 | } |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 80 | |
| 81 | dev = find_mount("/system"); |
| 82 | |
| 83 | if (!dev) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | return -1; |
| 85 | |
Nick Kralevich | 43f94fb | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 86 | fd = unix_open(dev, O_RDONLY | O_CLOEXEC); |
Nick Kralevich | 57eb352 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 87 | if (fd < 0) |
| 88 | return -1; |
| 89 | |
| 90 | ioctl(fd, BLKROSET, &OFF); |
| 91 | adb_close(fd); |
| 92 | |
Colin Cross | 0b57335 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 93 | system_ro = mount(dev, "/system", "none", MS_REMOUNT, NULL); |
| 94 | |
| 95 | free(dev); |
| 96 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | return system_ro; |
| 98 | } |
| 99 | |
| 100 | static void write_string(int fd, const char* str) |
| 101 | { |
| 102 | writex(fd, str, strlen(str)); |
| 103 | } |
| 104 | |
| 105 | void remount_service(int fd, void *cookie) |
| 106 | { |
| 107 | int ret = remount_system(); |
| 108 | |
| 109 | if (!ret) |
| 110 | write_string(fd, "remount succeeded\n"); |
| 111 | else { |
| 112 | char buffer[200]; |
| 113 | snprintf(buffer, sizeof(buffer), "remount failed: %s\n", strerror(errno)); |
| 114 | write_string(fd, buffer); |
| 115 | } |
| 116 | |
| 117 | adb_close(fd); |
| 118 | } |
| 119 | |