diff options
author | Mark Salyzyn <salyzyn@google.com> | 2015-03-16 08:26:05 -0700 |
---|---|---|
committer | Mark Salyzyn <salyzyn@google.com> | 2015-04-08 14:47:54 -0700 |
commit | 08739ba71fc1f2659149be760405d622e5b68f06 (patch) | |
tree | 3d2b7bd6f0f5780a8dfc0ec4a560ddf58fcafd87 /logd/main.cpp | |
parent | 720f6d1d55d936d98cc9752e96f479e03e6d5009 (diff) | |
download | system_core-08739ba71fc1f2659149be760405d622e5b68f06.zip system_core-08739ba71fc1f2659149be760405d622e5b68f06.tar.gz system_core-08739ba71fc1f2659149be760405d622e5b68f06.tar.bz2 |
logd: uidToName improvement
- read packages.list to acquire package names
- hijack the reinit handler as a privileged worker
Bug: 19608965
Change-Id: If7becb34354d6415e5c387ecea7d4109a15259c9
Diffstat (limited to 'logd/main.cpp')
-rw-r--r-- | logd/main.cpp | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/logd/main.cpp b/logd/main.cpp index a61beff..1ffb8b0 100644 --- a/logd/main.cpp +++ b/logd/main.cpp @@ -143,6 +143,10 @@ static bool property_get_bool(const char *key, bool def) { // write(fdDmesg, "I am here\n", 10); static int fdDmesg = -1; +static sem_t uidName; +static uid_t uid; +static char *name; + static sem_t reinit; static bool reinit_running = false; static LogBuffer *logBuf = NULL; @@ -151,10 +155,45 @@ static void *reinit_thread_start(void * /*obj*/) { prctl(PR_SET_NAME, "logd.daemon"); set_sched_policy(0, SP_BACKGROUND); - setgid(AID_LOGD); - setuid(AID_LOGD); + setgid(AID_SYSTEM); + setuid(AID_SYSTEM); while (reinit_running && !sem_wait(&reinit) && reinit_running) { + + // uidToName Privileged Worker + if (uid) { + name = NULL; + + FILE *fp = fopen("/data/system/packages.list", "r"); + if (fp) { + // This simple parser is sensitive to format changes in + // frameworks/base/services/core/java/com/android/server/pm/Settings.java + // A dependency note has been added to that file to correct + // this parser. + + char *buffer = NULL; + size_t len; + while (getline(&buffer, &len, fp) > 0) { + char *userId = strchr(buffer, ' '); + if (!userId) { + continue; + } + *userId = '\0'; + unsigned long value = strtoul(userId + 1, NULL, 10); + if (value != uid) { + continue; + } + name = strdup(buffer); + break; + } + free(buffer); + fclose(fp); + } + uid = 0; + sem_post(&uidName); + continue; + } + if (fdDmesg >= 0) { static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO), 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':', @@ -171,6 +210,20 @@ static void *reinit_thread_start(void * /*obj*/) { return NULL; } +char *android::uidToName(uid_t u) { + if (!u || !reinit_running) { + return NULL; + } + + // Not multi-thread safe, we know there is only one caller + uid = u; + + name = NULL; + sem_post(&reinit); + sem_wait(&uidName); + return name; +} + // Serves as a global method to trigger reinitialization // and as a function that can be provided to signal(). void reinit_signal_handler(int /*signal*/) { @@ -223,6 +276,7 @@ int main(int argc, char *argv[]) { // Reinit Thread sem_init(&reinit, 0, 0); + sem_init(&uidName, 0, 0); pthread_attr_t attr; if (!pthread_attr_init(&attr)) { struct sched_param param; |