OSDN Git Service

ext4_utils: Yet another MMC discard pain in the ass
[android-x86/system-extras.git] / su / binder / pm-wrapper.c
1 #include "../utils.h"
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <utils/Log.h>
7
8 #define PACKAGE_LIST_PATH "/data/system/packages.list"
9 #define PACKAGE_NAME_MAX_LEN (1<<16)
10
11 /* Tries to resolve a package name from a uid via the packages list file.
12  *
13  * If there is no matching uid, it will return an empty string which can
14  * be resolved by appops in some cases (i.e. apps with uid = 0, uid = AID_SHELL).
15  *
16  * Since packages may share UID, this function will return the first present
17  * in packages.list.
18  */
19 const char* resolve_package_name(int uid) {
20     char *packages = read_file(PACKAGE_LIST_PATH);
21
22     if (packages == NULL) {
23         goto notfound;
24     }
25
26     char *p = packages;
27     while (*p) {
28         char *line_end = strstr(p, "\n");
29         if (line_end == NULL)
30             break;
31
32         char *token;
33         char *pkgName = strtok_r(p, " ", &token);
34         if (pkgName != NULL) {
35             char *pkgUid = strtok_r(NULL, " ", &token);
36             if (pkgUid != NULL) {
37                 char *endptr;
38                 errno = 0;
39                 int pkgUidInt = strtoul(pkgUid, &endptr, 10);
40                 if ((errno == 0 && endptr != NULL && !(*endptr)) && pkgUidInt == uid)
41                     return strdup(pkgName);
42             }
43         }
44         p = ++line_end;
45     }
46     free(packages);
47
48 notfound:
49     return "";
50 }