Advertisement
bigrushdog

disable_log_uid

Jul 9th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.64 KB | None | 0 0
  1. /* //device/libs/android_runtime/android_util_Log.cpp
  2. **
  3. ** Copyright 2006, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. **     http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17.  
  18. #define LOG_NAMESPACE "log.tag."
  19. #define LOG_TAG "Log_println"
  20.  
  21. #include <assert.h>
  22. #include <cutils/properties.h>
  23. #include <utils/Log.h>
  24. #include <utils/String8.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28.  
  29. #include "jni.h"
  30. #include "JNIHelp.h"
  31. #include "utils/misc.h"
  32. #include "android_runtime/AndroidRuntime.h"
  33. #include "android_util_Log.h"
  34.  
  35. #define MIN(a,b) ((a<b)?a:b)
  36.  
  37. namespace android {
  38.  
  39. static int loggingMode = -1;
  40. static int loggingUids [100];
  41. static int loggingUidsCount = 0;
  42.  
  43. struct levels_t {
  44.     jint verbose;
  45.     jint debug;
  46.     jint info;
  47.     jint warn;
  48.     jint error;
  49.     jint assert;
  50. };
  51. static levels_t levels;
  52.  
  53. static int toLevel(const char* value)
  54. {
  55.     switch (value[0]) {
  56.         case 'V': return levels.verbose;
  57.         case 'D': return levels.debug;
  58.         case 'I': return levels.info;
  59.         case 'W': return levels.warn;
  60.         case 'E': return levels.error;
  61.         case 'A': return levels.assert;
  62.         case 'S': return -1; // SUPPRESS
  63.     }
  64.     return levels.info;
  65. }
  66.  
  67. static int canUidLog() {
  68.     int uid = getuid();
  69.     int i = 0;
  70.     if (loggingMode == 2) { // Let everything log.
  71.         return 1;
  72.     } else if (loggingMode == -1) { // Default value and need to load settings.
  73.         FILE *file;
  74.         char line [50];
  75.  
  76.         /* Load the logging mode */
  77.         file = fopen("/eos/uids_mode", "r");
  78.         if (file == NULL) {
  79.             loggingMode = -2; // Unknown, let everything log.
  80.             return 1; // Let the application log.
  81.         } else {
  82.             if (fgets(line, sizeof(line), file) != NULL) {
  83.                 if (strncmp("whitelist", line, 9) == 0) {
  84.                     loggingMode = 1;
  85.                 } else if (strncmp("blacklist", line, 9) == 0) {
  86.                     loggingMode = 0;
  87.                 } else {
  88.                     loggingMode = 2;
  89.                 }
  90.             }
  91.  
  92.             fclose(file);
  93.             if (loggingMode == 2) {
  94.                 return 1;
  95.             }
  96.         }
  97.  
  98.         /* Load the user ids */
  99.         file = fopen("/eos/uids_list", "r");
  100.         if (file != NULL) {
  101.             i = 0;
  102.             while (fgets (line, sizeof(line), file) != NULL) {
  103.                 if (i < sizeof(loggingUids)) {
  104.                     loggingUids[i] = atoi(line);
  105.                     loggingUidsCount++;
  106.                 }
  107.             }
  108.             fclose(file);
  109.         }
  110.     }
  111.  
  112.     for (i = 0; i < loggingUidsCount && i < sizeof(loggingUids); i++) {
  113.         if (uid == loggingUids[i]) return loggingMode;
  114.     }
  115.  
  116.     return loggingMode == 1 ? 0 : 1;
  117. }
  118.  
  119. static jboolean isLoggable(const char* tag, jint level) {
  120.     String8 key;
  121.     key.append(LOG_NAMESPACE);
  122.     key.append(tag);
  123.  
  124.     char buf[PROPERTY_VALUE_MAX];
  125.     if (property_get(key.string(), buf, "") <= 0) {
  126.         buf[0] = '\0';
  127.     }
  128.  
  129.     int logLevel = toLevel(buf);
  130.     return logLevel >= 0 && level >= logLevel;
  131. }
  132.  
  133. static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
  134. {
  135.     if (tag == NULL) {
  136.         return false;
  137.     }
  138.  
  139.     const char* chars = env->GetStringUTFChars(tag, NULL);
  140.     if (!chars) {
  141.         return false;
  142.     }
  143.  
  144.     jboolean result = false;
  145.     if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) {
  146.         char buf2[200];
  147.         snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %d characters\n",
  148.                 chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE));
  149.  
  150.         jniThrowException(env, "java/lang/IllegalArgumentException", buf2);
  151.     } else {
  152.         result = isLoggable(chars, level);
  153.     }
  154.  
  155.     env->ReleaseStringUTFChars(tag, chars);
  156.     return result;
  157. }
  158.  
  159. bool android_util_Log_isVerboseLogEnabled(const char* tag) {
  160.     return isLoggable(tag, levels.verbose);
  161. }
  162.  
  163. /*
  164.  * In class android.util.Log:
  165.  *  public static native int println_native(int buffer, int priority, String tag, String msg)
  166.  */
  167. static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
  168.         jint bufID, jint priority, jstring tagObj, jstring msgObj)
  169. {
  170.     const char* tag = NULL;
  171.     const char* msg = NULL;
  172.  
  173.     if (canUidLog() == 0) {
  174.         return 0;
  175.     }
  176.  
  177.     if (msgObj == NULL) {
  178.         jniThrowNullPointerException(env, "println needs a message");
  179.         return -1;
  180.     }
  181.  
  182.     if (bufID < 0 || bufID >= LOG_ID_MAX) {
  183.         jniThrowNullPointerException(env, "bad bufID");
  184.         return -1;
  185.     }
  186.  
  187.     if (tagObj != NULL)
  188.         tag = env->GetStringUTFChars(tagObj, NULL);
  189.     msg = env->GetStringUTFChars(msgObj, NULL);
  190.  
  191.     int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
  192.  
  193.     if (tag != NULL)
  194.         env->ReleaseStringUTFChars(tagObj, tag);
  195.     env->ReleaseStringUTFChars(msgObj, msg);
  196.  
  197.     return res;
  198. }
  199.  
  200. /*
  201.  * JNI registration.
  202.  */
  203. static JNINativeMethod gMethods[] = {
  204.     /* name, signature, funcPtr */
  205.     { "isLoggable",      "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
  206.     { "println_native",  "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
  207. };
  208.  
  209. int register_android_util_Log(JNIEnv* env)
  210. {
  211.     jclass clazz = env->FindClass("android/util/Log");
  212.  
  213.     if (clazz == NULL) {
  214.         ALOGE("Can't find android/util/Log");
  215.         return -1;
  216.     }
  217.  
  218.     levels.verbose = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "VERBOSE", "I"));
  219.     levels.debug = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "DEBUG", "I"));
  220.     levels.info = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "INFO", "I"));
  221.     levels.warn = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "WARN", "I"));
  222.     levels.error = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "ERROR", "I"));
  223.     levels.assert = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "ASSERT", "I"));
  224.  
  225.     return AndroidRuntime::registerNativeMethods(env, "android/util/Log", gMethods, NELEM(gMethods));
  226. }
  227.  
  228. }; // namespace android
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement