Home | History | Annotate | Download | only in adb
      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 <errno.h>
     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>
     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 static int vendor_ro = 1;
     33 
     34 /* Returns the device used to mount a directory in /proc/mounts */
     35 static char *find_mount(const char *dir)
     36 {
     37     int fd;
     38     int res;
     39     char *token = NULL;
     40     const char delims[] = "\n";
     41     char buf[4096];
     42 
     43     fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC);
     44     if (fd < 0)
     45         return NULL;
     46 
     47     buf[sizeof(buf) - 1] = '\0';
     48     adb_read(fd, buf, sizeof(buf) - 1);
     49     adb_close(fd);
     50 
     51     token = strtok(buf, delims);
     52 
     53     while (token) {
     54         char mount_dev[256];
     55         char mount_dir[256];
     56         int mount_freq;
     57         int mount_passno;
     58 
     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);
     65 
     66         token = strtok(NULL, delims);
     67     }
     68     return NULL;
     69 }
     70 
     71 static int hasVendorPartition()
     72 {
     73     struct stat info;
     74     if (!lstat("/vendor", &info))
     75         if ((info.st_mode & S_IFMT) == S_IFDIR)
     76           return true;
     77     return false;
     78 }
     79 
     80 /* Init mounts /system as read only, remount to enable writes. */
     81 static int remount(const char* dir, int* dir_ro)
     82 {
     83     char *dev;
     84     int fd;
     85     int OFF = 0;
     86 
     87     if (dir_ro == 0) {
     88         return 0;
     89     }
     90 
     91     dev = find_mount(dir);
     92 
     93     if (!dev)
     94         return -1;
     95 
     96     fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
     97     if (fd < 0)
     98         return -1;
     99 
    100     ioctl(fd, BLKROSET, &OFF);
    101     adb_close(fd);
    102 
    103     *dir_ro = mount(dev, dir, "none", MS_REMOUNT, NULL);
    104 
    105     free(dev);
    106 
    107     return *dir_ro;
    108 }
    109 
    110 static void write_string(int fd, const char* str)
    111 {
    112     writex(fd, str, strlen(str));
    113 }
    114 
    115 void remount_service(int fd, void *cookie)
    116 {
    117     char buffer[200];
    118     if (remount("/system", &system_ro)) {
    119         snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno));
    120         write_string(fd, buffer);
    121     }
    122 
    123     if (hasVendorPartition()) {
    124         if (remount("/vendor", &vendor_ro)) {
    125             snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno));
    126             write_string(fd, buffer);
    127         }
    128     }
    129 
    130     if (!system_ro && (!vendor_ro || !hasVendorPartition()))
    131         write_string(fd, "remount succeeded\n");
    132     else {
    133         write_string(fd, "remount failed\n");
    134     }
    135 
    136     adb_close(fd);
    137 }
    138 
    139