Home | History | Annotate | Download | only in libprocpartition
      1 /*
      2  * Copyright (C) 2018 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 <procpartition/procpartition.h>
     18 
     19 #include <android-base/file.h>
     20 
     21 namespace android {
     22 namespace procpartition {
     23 
     24 std::ostream& operator<<(std::ostream& os, Partition p) {
     25     switch (p) {
     26         case Partition::SYSTEM: return os << "system";
     27         case Partition::VENDOR: return os << "vendor";
     28         case Partition::ODM: return os << "odm";
     29         case Partition::UNKNOWN: // fallthrough
     30         default:
     31             return os << "(unknown)";
     32     }
     33 }
     34 
     35 std::string getExe(pid_t pid) {
     36     std::string exe;
     37     std::string real;
     38     if (!android::base::Readlink("/proc/" + std::to_string(pid) + "/exe", &exe)) {
     39         return "";
     40     }
     41     if (!android::base::Realpath(exe, &real)) {
     42         return "";
     43     }
     44     return real;
     45 }
     46 
     47 std::string getCmdline(pid_t pid) {
     48     std::string content;
     49     if (!android::base::ReadFileToString("/proc/" + std::to_string(pid) + "/cmdline", &content,
     50                                          false /* follow symlinks */)) {
     51         return "";
     52     }
     53     return content;
     54 }
     55 
     56 Partition parsePartition(const std::string& s) {
     57     if (s == "system") {
     58         return Partition::SYSTEM;
     59     }
     60     if (s == "vendor") {
     61         return Partition::VENDOR;
     62     }
     63     if (s == "odm") {
     64         return Partition::ODM;
     65     }
     66     return Partition::UNKNOWN;
     67 }
     68 
     69 Partition getPartitionFromRealpath(const std::string& path) {
     70     if (path == "/system/bin/app_process64" ||
     71         path == "/system/bin/app_process32") {
     72 
     73         return Partition::UNKNOWN; // cannot determine
     74     }
     75     size_t backslash = path.find_first_of('/', 1);
     76     std::string partition = (backslash != std::string::npos) ? path.substr(1, backslash - 1) : path;
     77 
     78     return parsePartition(partition);
     79 }
     80 
     81 Partition getPartitionFromCmdline(pid_t pid) {
     82     const auto& cmdline = getCmdline(pid);
     83     if (cmdline == "system_server") {
     84         return Partition::SYSTEM;
     85     }
     86     if (cmdline.empty() || cmdline.front() != '/') {
     87         return Partition::UNKNOWN;
     88     }
     89     return getPartitionFromRealpath(cmdline);
     90 }
     91 
     92 Partition getPartitionFromExe(pid_t pid) {
     93     const auto& real = getExe(pid);
     94     if (real.empty() || real.front() != '/') {
     95         return Partition::UNKNOWN;
     96     }
     97     return getPartitionFromRealpath(real);
     98 }
     99 
    100 
    101 Partition getPartition(pid_t pid) {
    102     Partition partition = getPartitionFromExe(pid);
    103     if (partition == Partition::UNKNOWN) {
    104         partition = getPartitionFromCmdline(pid);
    105     }
    106     return partition;
    107 }
    108 
    109 }  // namespace procpartition
    110 }  // namespace android
    111