1 /*
2  * Copyright (C) 2020 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 <pthread.h>
19 #include <sys/inotify.h>
20 #include <sys/prctl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include <atomic>
26 #include <map>
27 #include <mutex>
28 #include <string>
29 #include <thread>
30 
31 #include <android-base/logging.h>
32 #include <android-base/unique_fd.h>
33 #include <packagelistparser/packagelistparser.h>
34 
35 #include "LogUtils.h"
36 
37 using android::base::unique_fd;
38 
39 #define PACKAGES_LIST_FILE "/data/system/packages.list"
40 
41 struct PkgIdMap {
42     bool active = false;
43     std::mutex lock;
44     std::atomic<bool> running = false;
45     std::map<uid_t, std::string> appid_to_package;
46     std::unique_ptr<std::thread> pklp_thread;
47 };
48 
49 static PkgIdMap* gPkgIdMap = new PkgIdMap;
50 
PackageParseCallback(pkg_info * info,void * userdata)51 static bool PackageParseCallback(pkg_info* info, void* userdata) {
52     struct PkgIdMap* global = static_cast<struct PkgIdMap*>(userdata);
53     if (info->name != NULL && info->name[0] != '\0') {
54         global->appid_to_package.emplace(info->uid, info->name);
55     }
56     packagelist_free(info);
57     return true;
58 }
59 
ReadPackageList(struct PkgIdMap * global)60 static bool ReadPackageList(struct PkgIdMap* global) {
61     std::lock_guard<std::mutex> guard(global->lock);
62     global->appid_to_package.clear();
63     bool rc = packagelist_parse(PackageParseCallback, global);
64     LOG(INFO) << "ReadPackageList, total packages: " << global->appid_to_package.size();
65     return rc;
66 }
67 
WatchPackageList(struct PkgIdMap * global)68 static void WatchPackageList(struct PkgIdMap* global) {
69     struct inotify_event* event;
70     char event_buf[512];
71 
72     unique_fd nfd(inotify_init1(IN_CLOEXEC));
73     if (nfd == -1) {
74         LOG(FATAL) << "inotify_init failed";
75         return;
76     }
77 
78     global->active = false;
79     while (1) {
80         if (!global->active) {
81             LOG(INFO) << "start watching " PACKAGES_LIST_FILE " ...";
82             int ret = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
83             if (ret == -1) {
84                 if (errno == ENOENT || errno == EACCES) {
85                     LOG(INFO) << "missing " PACKAGES_LIST_FILE "; retrying...";
86                     return;
87                 } else {
88                     PLOG(ERROR) << "inotify_add_watch failed";
89                     return;
90                 }
91             }
92 
93             if (ReadPackageList(global) == false) {
94                 LOG(ERROR) << "ReadPackageList failed";
95                 return;
96             }
97             global->active = true;
98         }
99 
100         int event_pos = 0;
101         ssize_t res = TEMP_FAILURE_RETRY(read(nfd, event_buf, sizeof(event_buf)));
102         if (res == -1 || static_cast<size_t>(res) < sizeof(*event)) {
103             LOG(ERROR) << "failed to read inotify event, res: " << res;
104             global->active = false;
105             continue;
106         }
107 
108         while (res >= static_cast<ssize_t>(sizeof(*event))) {
109             int event_size;
110             event = reinterpret_cast<struct inotify_event*>(event_buf + event_pos);
111 
112             if ((event->mask & IN_IGNORED) == IN_IGNORED) {
113                 global->active = false;
114             }
115 
116             event_size = sizeof(*event) + event->len;
117             res -= event_size;
118             event_pos += event_size;
119         }
120     }
121 }
122 
StartHandler(struct PkgIdMap * global)123 static void StartHandler(struct PkgIdMap* global) {
124     prctl(PR_SET_NAME, "logd.pkglist");
125     WatchPackageList(global);
126     global->running = false;
127 }
128 
StartPkgMonitor()129 void StartPkgMonitor() {
130     std::lock_guard<std::mutex> guard(gPkgIdMap->lock);
131     if (gPkgIdMap->pklp_thread.get() == nullptr) {
132         gPkgIdMap->running = true;
133         gPkgIdMap->pklp_thread = std::make_unique<std::thread>(StartHandler, gPkgIdMap);
134     } else if (!gPkgIdMap->running) {
135         gPkgIdMap->pklp_thread->join();
136         gPkgIdMap->running = true;
137         gPkgIdMap->pklp_thread.reset(new std::thread(StartHandler, gPkgIdMap));
138     }
139 }
140 
uidToName(uid_t u)141 char* android::uidToName(uid_t u) {
142     {
143         std::lock_guard<std::mutex> guard(gPkgIdMap->lock);
144         if (gPkgIdMap->active) {
145             const auto& iter = gPkgIdMap->appid_to_package.find(u);
146             if (iter != gPkgIdMap->appid_to_package.end()) {
147                 return strdup(iter->second.c_str());
148             }
149         }
150     }
151 
152     struct Userdata {
153         uid_t uid;
154         char* name;
155     } userdata = {
156             .uid = u,
157             .name = nullptr,
158     };
159 
160     packagelist_parse(
161             [](pkg_info* info, void* callback_parameter) {
162                 auto userdata = reinterpret_cast<Userdata*>(callback_parameter);
163                 bool result = true;
164                 if (info->uid == userdata->uid) {
165                     userdata->name = strdup(info->name);
166                     // false to stop processing
167                     result = false;
168                 }
169                 packagelist_free(info);
170                 return result;
171             },
172             &userdata);
173 
174     if (userdata.name != nullptr) {
175         StartPkgMonitor();
176     }
177     return userdata.name;
178 }
179