Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <stdio.h> |
michael@0 | 6 | #include <string.h> |
michael@0 | 7 | #include <stdarg.h> |
michael@0 | 8 | |
michael@0 | 9 | #include "CSFLog.h" |
michael@0 | 10 | #include "base/basictypes.h" |
michael@0 | 11 | |
michael@0 | 12 | #include <map> |
michael@0 | 13 | #include "cpr_threads.h" |
michael@0 | 14 | #include "prrwlock.h" |
michael@0 | 15 | #include "prthread.h" |
michael@0 | 16 | #include "nsThreadUtils.h" |
michael@0 | 17 | #ifndef WIN32 |
michael@0 | 18 | #include <pthread.h> |
michael@0 | 19 | #endif |
michael@0 | 20 | #ifdef OS_MACOSX |
michael@0 | 21 | #include <dlfcn.h> |
michael@0 | 22 | #endif |
michael@0 | 23 | #ifdef OS_LINUX |
michael@0 | 24 | #include <sys/prctl.h> |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | static PRLogModuleInfo *gLogModuleInfo = nullptr; |
michael@0 | 28 | |
michael@0 | 29 | PRLogModuleInfo *GetSignalingLogInfo() |
michael@0 | 30 | { |
michael@0 | 31 | if (gLogModuleInfo == nullptr) |
michael@0 | 32 | gLogModuleInfo = PR_NewLogModule("signaling"); |
michael@0 | 33 | |
michael@0 | 34 | return gLogModuleInfo; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | static PRLogModuleInfo *gWebRTCLogModuleInfo = nullptr; |
michael@0 | 38 | int gWebrtcTraceLoggingOn = 0; |
michael@0 | 39 | |
michael@0 | 40 | PRLogModuleInfo *GetWebRTCLogInfo() |
michael@0 | 41 | { |
michael@0 | 42 | if (gWebRTCLogModuleInfo == nullptr) |
michael@0 | 43 | gWebRTCLogModuleInfo = PR_NewLogModule("webrtc_trace"); |
michael@0 | 44 | |
michael@0 | 45 | return gWebRTCLogModuleInfo; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | extern "C" { |
michael@0 | 49 | void CSFLogRegisterThread(const cprThread_t thread); |
michael@0 | 50 | void CSFLogUnregisterThread(const cprThread_t thread); |
michael@0 | 51 | #ifndef WIN32 |
michael@0 | 52 | pthread_t cprGetThreadId(cprThread_t thread); |
michael@0 | 53 | #endif |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | #ifdef WIN32 |
michael@0 | 57 | typedef unsigned int thread_key_t; |
michael@0 | 58 | #else |
michael@0 | 59 | typedef pthread_t thread_key_t; |
michael@0 | 60 | #endif |
michael@0 | 61 | static PRRWLock *maplock = PR_NewRWLock(0,"thread map"); |
michael@0 | 62 | typedef std::map<thread_key_t,const cpr_thread_t*> threadMap_t; |
michael@0 | 63 | static threadMap_t threadMap; |
michael@0 | 64 | |
michael@0 | 65 | void CSFLogRegisterThread(const cprThread_t thread) { |
michael@0 | 66 | const cpr_thread_t *t = reinterpret_cast<cpr_thread_t *>(thread); |
michael@0 | 67 | thread_key_t key; |
michael@0 | 68 | #ifdef WIN32 |
michael@0 | 69 | key = t->threadId; |
michael@0 | 70 | #else |
michael@0 | 71 | key = cprGetThreadId(thread); |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | CSFLog(CSF_LOG_DEBUG, __FILE__, __LINE__, "log", |
michael@0 | 75 | "Registering new thread with logging system: %s", t->name); |
michael@0 | 76 | PR_RWLock_Wlock(maplock); |
michael@0 | 77 | threadMap[key] = t; |
michael@0 | 78 | PR_RWLock_Unlock(maplock); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | void CSFLogUnregisterThread(const cprThread_t thread) { |
michael@0 | 82 | const cpr_thread_t *t = reinterpret_cast<cpr_thread_t *>(thread); |
michael@0 | 83 | thread_key_t key; |
michael@0 | 84 | #ifdef WIN32 |
michael@0 | 85 | key = t->threadId; |
michael@0 | 86 | #else |
michael@0 | 87 | key = cprGetThreadId(thread); |
michael@0 | 88 | #endif |
michael@0 | 89 | CSFLog(CSF_LOG_DEBUG, __FILE__, __LINE__, "log", |
michael@0 | 90 | "Unregistering thread from logging system: %s", t->name); |
michael@0 | 91 | PR_RWLock_Wlock(maplock); |
michael@0 | 92 | threadMap.erase(key); |
michael@0 | 93 | PR_RWLock_Unlock(maplock); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | const char *CSFCurrentThreadName() { |
michael@0 | 97 | const char *name = nullptr; |
michael@0 | 98 | #ifdef WIN32 |
michael@0 | 99 | thread_key_t key = GetCurrentThreadId(); |
michael@0 | 100 | #else |
michael@0 | 101 | thread_key_t key = pthread_self(); |
michael@0 | 102 | #endif |
michael@0 | 103 | PR_RWLock_Rlock(maplock); |
michael@0 | 104 | threadMap_t::iterator i = threadMap.find(key); |
michael@0 | 105 | if (i != threadMap.end()) { |
michael@0 | 106 | name = i->second->name; |
michael@0 | 107 | } |
michael@0 | 108 | PR_RWLock_Unlock(maplock); |
michael@0 | 109 | return name; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | #ifdef OS_MACOSX |
michael@0 | 113 | // pthread_getname_np isn't available on all versions of OS X, so |
michael@0 | 114 | // we need to load it in dynamically and check for its presence |
michael@0 | 115 | static int (*dynamic_pthread_getname_np)(pthread_t,char*,size_t); |
michael@0 | 116 | bool init_pthread_getname() { |
michael@0 | 117 | *reinterpret_cast<void**>(&dynamic_pthread_getname_np) = |
michael@0 | 118 | dlsym(RTLD_DEFAULT, "pthread_getname_np"); |
michael@0 | 119 | return dynamic_pthread_getname_np; |
michael@0 | 120 | } |
michael@0 | 121 | static bool have_pthread_getname_np = init_pthread_getname(); |
michael@0 | 122 | #endif |
michael@0 | 123 | |
michael@0 | 124 | void CSFLogV(CSFLogLevel priority, const char* sourceFile, int sourceLine, const char* tag , const char* format, va_list args) |
michael@0 | 125 | { |
michael@0 | 126 | #ifdef STDOUT_LOGGING |
michael@0 | 127 | printf("%s\n:",tag); |
michael@0 | 128 | vprintf(format, args); |
michael@0 | 129 | #else |
michael@0 | 130 | |
michael@0 | 131 | PRLogModuleLevel level = static_cast<PRLogModuleLevel>(priority); |
michael@0 | 132 | |
michael@0 | 133 | GetSignalingLogInfo(); |
michael@0 | 134 | |
michael@0 | 135 | // Skip doing any of this work if we're not logging the indicated level... |
michael@0 | 136 | if (!PR_LOG_TEST(gLogModuleInfo,level)) { |
michael@0 | 137 | return; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | // Trim the path component from the filename |
michael@0 | 141 | const char *lastSlash = sourceFile; |
michael@0 | 142 | while (*sourceFile) { |
michael@0 | 143 | if (*sourceFile == '/' || *sourceFile == '\\') { |
michael@0 | 144 | lastSlash = sourceFile; |
michael@0 | 145 | } |
michael@0 | 146 | sourceFile++; |
michael@0 | 147 | } |
michael@0 | 148 | sourceFile = lastSlash; |
michael@0 | 149 | if (*sourceFile == '/' || *sourceFile == '\\') { |
michael@0 | 150 | sourceFile++; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | #define MAX_MESSAGE_LENGTH 1024 |
michael@0 | 154 | char message[MAX_MESSAGE_LENGTH]; |
michael@0 | 155 | char buffer[64] = ""; |
michael@0 | 156 | |
michael@0 | 157 | const char *threadName = CSFCurrentThreadName(); |
michael@0 | 158 | |
michael@0 | 159 | // Check if we're the main thread... |
michael@0 | 160 | if (!threadName && NS_IsMainThread()) { |
michael@0 | 161 | threadName = "main"; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | // If null, the name wasn't set up by CPR -- try NSPR |
michael@0 | 165 | if (!threadName) { |
michael@0 | 166 | threadName = PR_GetThreadName(PR_GetCurrentThread()); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | // If not NSPR, it might be from some other imported library that uses |
michael@0 | 170 | // one of the variety of non-portable means of naming threads. |
michael@0 | 171 | #ifdef OS_LINUX |
michael@0 | 172 | if (!threadName && |
michael@0 | 173 | !prctl(PR_GET_NAME,reinterpret_cast<uintptr_t>(buffer),0,0,0)) { |
michael@0 | 174 | buffer[16]='\0'; |
michael@0 | 175 | if (buffer[0] != '\0') { |
michael@0 | 176 | threadName = buffer; |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | #endif |
michael@0 | 180 | #ifdef OS_MACOSX |
michael@0 | 181 | if (!threadName && have_pthread_getname_np) { |
michael@0 | 182 | dynamic_pthread_getname_np(pthread_self(), buffer, sizeof(buffer)); |
michael@0 | 183 | if (buffer[0] != '\0') { |
michael@0 | 184 | threadName = buffer; |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | #endif |
michael@0 | 188 | |
michael@0 | 189 | // If we can't find it anywhere, use a blank string |
michael@0 | 190 | if (!threadName) { |
michael@0 | 191 | threadName = ""; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | vsnprintf(message, MAX_MESSAGE_LENGTH, format, args); |
michael@0 | 195 | PR_LOG(gLogModuleInfo, level, ("[%s|%s] %s:%d: %s", |
michael@0 | 196 | threadName, tag, sourceFile, sourceLine, |
michael@0 | 197 | message)); |
michael@0 | 198 | #endif |
michael@0 | 199 | |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | void CSFLog( CSFLogLevel priority, const char* sourceFile, int sourceLine, const char* tag , const char* format, ...) |
michael@0 | 203 | { |
michael@0 | 204 | va_list ap; |
michael@0 | 205 | va_start(ap, format); |
michael@0 | 206 | |
michael@0 | 207 | CSFLogV(priority, sourceFile, sourceLine, tag, format, ap); |
michael@0 | 208 | va_end(ap); |
michael@0 | 209 | } |
michael@0 | 210 |