remount_service.cpp revision 9aa4fda4e64c1882faf019cc2a483ee4917e0c85
1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/* 2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * 4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * you may not use this file except in compliance with the License. 6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * You may obtain a copy of the License at 7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * 8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * 10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * See the License for the specific language governing permissions and 14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * limitations under the License. 15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */ 16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project 173313426fad9eaaf53017cdbde889ebcec91358ecDan Albert#define TRACE_TAG TRACE_ADB 183313426fad9eaaf53017cdbde889ebcec91358ecDan Albert 193313426fad9eaaf53017cdbde889ebcec91358ecDan Albert#include "sysdeps.h" 203313426fad9eaaf53017cdbde889ebcec91358ecDan Albert 2160299dfd6a5dca059a079bc8e11d45a1fecf02d0Mark Salyzyn#include <errno.h> 2260299dfd6a5dca059a079bc8e11d45a1fecf02d0Mark Salyzyn#include <fcntl.h> 23d6bd9bf45968cb97ed88a4e06c40a127625897b1Yabin Cui#include <mntent.h> 24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h> 2560299dfd6a5dca059a079bc8e11d45a1fecf02d0Mark Salyzyn#include <stdlib.h> 26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h> 27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/mount.h> 2860299dfd6a5dca059a079bc8e11d45a1fecf02d0Mark Salyzyn#include <unistd.h> 29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project 30ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes#include <string> 31ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes 32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "adb.h" 33cc731cc76786b6bdc58764aad9924c0d0c8d645fDan Albert#include "adb_io.h" 345830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include "adb_utils.h" 357664901a355b959f312e9acff5a0fd31b7139623Dan Albert#include "cutils/properties.h" 36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project 375677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes// Returns the device used to mount a directory in /proc/mounts. 385677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughesstatic std::string find_mount(const char* dir) { 395677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent); 405677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes if (!fp) { 415677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes return ""; 42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project } 435677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes 445677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes mntent* e; 455677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes while ((e = getmntent(fp.get())) != nullptr) { 465677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes if (strcmp(dir, e->mnt_dir) == 0) { 475677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes return e->mnt_fsname; 48d6bd9bf45968cb97ed88a4e06c40a127625897b1Yabin Cui } 49d6bd9bf45968cb97ed88a4e06c40a127625897b1Yabin Cui } 505677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes return ""; 51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project} 52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project 539aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughesbool make_block_device_writable(const std::string& dev) { 54ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC); 55ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes if (fd == -1) { 569aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes return false; 57982089d83879c768eac3fd36f19665463a550b53Paul Lawrence } 58982089d83879c768eac3fd36f19665463a550b53Paul Lawrence 59ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes int OFF = 0; 609aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes bool result = (ioctl(fd, BLKROSET, &OFF) != -1); 61ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes adb_close(fd); 62ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes return result; 63ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes} 646084a0124f868c7ec43f6c415a27a168f27ff694Dan Albert 659aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughesstatic bool remount_partition(int fd, const char* dir) { 669aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes if (!directory_exists(dir)) { 679aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes return true; 6813449cd71464c3a644109c469a77bd7fd56c8af8Sami Tolvanen } 699aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes std::string dev = find_mount(dir); 709aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes if (dev.empty()) { 715677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes return true; 725677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes } 739aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes if (!make_block_device_writable(dev)) { 749aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n", 759aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes dir, dev.c_str(), strerror(errno)); 769aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes return false; 779aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes } 789aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) { 799aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes WriteFdFmt(fd, "remount of %s failed: %s\n", dir, strerror(errno)); 805677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes return false; 815677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes } 82ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes return true; 83ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes} 84ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes 85ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughesvoid remount_service(int fd, void* cookie) { 86268eb4f3846d551c73eb4fc5a505f9a70d47b638Nick Kralevich if (getuid() != 0) { 87e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n"); 88268eb4f3846d551c73eb4fc5a505f9a70d47b638Nick Kralevich adb_close(fd); 89268eb4f3846d551c73eb4fc5a505f9a70d47b638Nick Kralevich return; 90268eb4f3846d551c73eb4fc5a505f9a70d47b638Nick Kralevich } 91268eb4f3846d551c73eb4fc5a505f9a70d47b638Nick Kralevich 925677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes char prop_buf[PROPERTY_VALUE_MAX]; 93454742392f72079dbdb0d23ea24e01b5703c1aa5Sami Tolvanen property_get("partition.system.verified", prop_buf, ""); 945677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes bool system_verified = (strlen(prop_buf) > 0); 9534637555c25a6663658b1ff45e98272b10b449f4Paul Lawrence 96454742392f72079dbdb0d23ea24e01b5703c1aa5Sami Tolvanen property_get("partition.vendor.verified", prop_buf, ""); 975677c23e8d0c085be8d8429a5d125147d11e9bb2Elliott Hughes bool vendor_verified = (strlen(prop_buf) > 0); 9834637555c25a6663658b1ff45e98272b10b449f4Paul Lawrence 9934637555c25a6663658b1ff45e98272b10b449f4Paul Lawrence if (system_verified || vendor_verified) { 10034637555c25a6663658b1ff45e98272b10b449f4Paul Lawrence // Allow remount but warn of likely bad effects 10134637555c25a6663658b1ff45e98272b10b449f4Paul Lawrence bool both = system_verified && vendor_verified; 102ab52c181fa4c1c9891644635dc5653cda5b90e2bElliott Hughes WriteFdFmt(fd, 103ab52c181fa4c1c9891644635dc5653cda5b90e2bElliott Hughes "dm_verity is enabled on the %s%s%s partition%s.\n", 104ab52c181fa4c1c9891644635dc5653cda5b90e2bElliott Hughes system_verified ? "system" : "", 105ab52c181fa4c1c9891644635dc5653cda5b90e2bElliott Hughes both ? " and " : "", 106ab52c181fa4c1c9891644635dc5653cda5b90e2bElliott Hughes vendor_verified ? "vendor" : "", 107ab52c181fa4c1c9891644635dc5653cda5b90e2bElliott Hughes both ? "s" : ""); 108e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes WriteFdExactly(fd, 109e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes "Use \"adb disable-verity\" to disable verity.\n" 110e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes "If you do not, remount may succeed, however, you will still " 111e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes "not be able to write to these volumes.\n"); 11234637555c25a6663658b1ff45e98272b10b449f4Paul Lawrence } 11334637555c25a6663658b1ff45e98272b10b449f4Paul Lawrence 114ec7a66713144a16361724fe88332b28bbf3f4b3cElliott Hughes bool success = true; 1159aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes success &= remount_partition(fd, "/system"); 1169aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes success &= remount_partition(fd, "/vendor"); 1179aa4fda4e64c1882faf019cc2a483ee4917e0c85Elliott Hughes success &= remount_partition(fd, "/oem"); 118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project 119e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n"); 120dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project 121dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project adb_close(fd); 122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project} 123