Home | History | Annotate | Download | only in recovery
      1 /*
      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 <sys/stat.h>
     18 #include "roots.h"
     19 #include "common.h"
     20 
     21 #define CANARY_FILE "/system/build.prop"
     22 #define CANARY_FILE_ROOT_PATH "SYSTEM:build.prop"
     23 
     24 int
     25 file_exists(const char *path)
     26 {
     27     struct stat st;
     28     int ret;
     29     ret = stat(path, &st);
     30     if (ret == 0) {
     31         return S_ISREG(st.st_mode);
     32     }
     33     return 0;
     34 }
     35 
     36 int
     37 test_roots()
     38 {
     39     int ret;
     40 
     41     /* Make sure that /system isn't mounted yet.
     42      */
     43     if (file_exists(CANARY_FILE)) return -__LINE__;
     44     if (is_root_path_mounted(CANARY_FILE_ROOT_PATH)) return -__LINE__;
     45 
     46     /* Try to mount the root.
     47      */
     48     ret = ensure_root_path_mounted(CANARY_FILE_ROOT_PATH);
     49     if (ret < 0) return -__LINE__;
     50 
     51     /* Make sure we can see the file now and that we know the root is mounted.
     52      */
     53     if (!file_exists(CANARY_FILE)) return -__LINE__;
     54     if (!is_root_path_mounted(CANARY_FILE_ROOT_PATH)) return -__LINE__;
     55 
     56     /* Make sure that the root path corresponds to the regular path.
     57      */
     58     struct stat st1, st2;
     59     char buf[128];
     60     const char *path = translate_root_path(CANARY_FILE_ROOT_PATH,
     61             buf, sizeof(buf));
     62     if (path == NULL) return -__LINE__;
     63     ret = stat(CANARY_FILE, &st1);
     64     if (ret != 0) return -__LINE__;
     65     ret = stat(path, &st2);
     66     if (ret != 0) return -__LINE__;
     67     if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) return -__LINE__;
     68 
     69     /* Try to unmount the root.
     70      */
     71     ret = ensure_root_path_unmounted(CANARY_FILE_ROOT_PATH);
     72     if (ret < 0) return -__LINE__;
     73 
     74     /* Make sure that we can't see the file anymore and that
     75      * we don't think the root is mounted.
     76      */
     77     if (file_exists(CANARY_FILE)) return -__LINE__;
     78     if (is_root_path_mounted(CANARY_FILE_ROOT_PATH)) return -__LINE__;
     79 
     80     return 0;
     81 }
     82