File: PkgIds.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (178 lines) | stat: -rw-r--r-- 5,451 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <pthread.h>
#include <sys/inotify.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <atomic>
#include <map>
#include <mutex>
#include <string>
#include <thread>

#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <packagelistparser/packagelistparser.h>

#include "LogUtils.h"

using android::base::unique_fd;

#define PACKAGES_LIST_FILE "/data/system/packages.list"

struct PkgIdMap {
    bool active = false;
    std::mutex lock;
    std::atomic<bool> running = false;
    std::map<uid_t, std::string> appid_to_package;
    std::unique_ptr<std::thread> pklp_thread;
};

static PkgIdMap* gPkgIdMap = new PkgIdMap;

static bool PackageParseCallback(pkg_info* info, void* userdata) {
    struct PkgIdMap* global = static_cast<struct PkgIdMap*>(userdata);
    if (info->name != NULL && info->name[0] != '\0') {
        global->appid_to_package.emplace(info->uid, info->name);
    }
    packagelist_free(info);
    return true;
}

static bool ReadPackageList(struct PkgIdMap* global) {
    std::lock_guard<std::mutex> guard(global->lock);
    global->appid_to_package.clear();
    bool rc = packagelist_parse(PackageParseCallback, global);
    LOG(INFO) << "ReadPackageList, total packages: " << global->appid_to_package.size();
    return rc;
}

static void WatchPackageList(struct PkgIdMap* global) {
    struct inotify_event* event;
    char event_buf[512];

    unique_fd nfd(inotify_init1(IN_CLOEXEC));
    if (nfd == -1) {
        LOG(FATAL) << "inotify_init failed";
        return;
    }

    global->active = false;
    while (1) {
        if (!global->active) {
            LOG(INFO) << "start watching " PACKAGES_LIST_FILE " ...";
            int ret = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
            if (ret == -1) {
                if (errno == ENOENT || errno == EACCES) {
                    LOG(INFO) << "missing " PACKAGES_LIST_FILE "; retrying...";
                    return;
                } else {
                    PLOG(ERROR) << "inotify_add_watch failed";
                    return;
                }
            }

            if (ReadPackageList(global) == false) {
                LOG(ERROR) << "ReadPackageList failed";
                return;
            }
            global->active = true;
        }

        int event_pos = 0;
        ssize_t res = TEMP_FAILURE_RETRY(read(nfd, event_buf, sizeof(event_buf)));
        if (res == -1 || static_cast<size_t>(res) < sizeof(*event)) {
            LOG(ERROR) << "failed to read inotify event, res: " << res;
            global->active = false;
            continue;
        }

        while (res >= static_cast<ssize_t>(sizeof(*event))) {
            int event_size;
            event = reinterpret_cast<struct inotify_event*>(event_buf + event_pos);

            if ((event->mask & IN_IGNORED) == IN_IGNORED) {
                global->active = false;
            }

            event_size = sizeof(*event) + event->len;
            res -= event_size;
            event_pos += event_size;
        }
    }
}

static void StartHandler(struct PkgIdMap* global) {
    prctl(PR_SET_NAME, "logd.pkglist");
    WatchPackageList(global);
    global->running = false;
}

void StartPkgMonitor() {
    std::lock_guard<std::mutex> guard(gPkgIdMap->lock);
    if (gPkgIdMap->pklp_thread.get() == nullptr) {
        gPkgIdMap->running = true;
        gPkgIdMap->pklp_thread = std::make_unique<std::thread>(StartHandler, gPkgIdMap);
    } else if (!gPkgIdMap->running) {
        gPkgIdMap->pklp_thread->join();
        gPkgIdMap->running = true;
        gPkgIdMap->pklp_thread.reset(new std::thread(StartHandler, gPkgIdMap));
    }
}

char* android::uidToName(uid_t u) {
    {
        std::lock_guard<std::mutex> guard(gPkgIdMap->lock);
        if (gPkgIdMap->active) {
            const auto& iter = gPkgIdMap->appid_to_package.find(u);
            if (iter != gPkgIdMap->appid_to_package.end()) {
                return strdup(iter->second.c_str());
            }
        }
    }

    struct Userdata {
        uid_t uid;
        char* name;
    } userdata = {
            .uid = u,
            .name = nullptr,
    };

    packagelist_parse(
            [](pkg_info* info, void* callback_parameter) {
                auto userdata = reinterpret_cast<Userdata*>(callback_parameter);
                bool result = true;
                if (info->uid == userdata->uid) {
                    userdata->name = strdup(info->name);
                    // false to stop processing
                    result = false;
                }
                packagelist_free(info);
                return result;
            },
            &userdata);

    if (userdata.name != nullptr) {
        StartPkgMonitor();
    }
    return userdata.name;
}