Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2011, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Utility class that can persist a SimpleStringDictionary to disk. |
michael@0 | 31 | |
michael@0 | 32 | #import "client/mac/crash_generation/ConfigFile.h" |
michael@0 | 33 | |
michael@0 | 34 | #import <Foundation/Foundation.h> |
michael@0 | 35 | #include <stdio.h> |
michael@0 | 36 | #include <sys/time.h> |
michael@0 | 37 | |
michael@0 | 38 | #import "client/apple/Framework/BreakpadDefines.h" |
michael@0 | 39 | #import "common/mac/SimpleStringDictionary.h" |
michael@0 | 40 | #import "GTMDefines.h" |
michael@0 | 41 | |
michael@0 | 42 | #define VERBOSE 0 |
michael@0 | 43 | |
michael@0 | 44 | #if VERBOSE |
michael@0 | 45 | bool gDebugLog = true; |
michael@0 | 46 | #else |
michael@0 | 47 | bool gDebugLog = false; |
michael@0 | 48 | #endif |
michael@0 | 49 | |
michael@0 | 50 | #define DEBUGLOG if (gDebugLog) fprintf |
michael@0 | 51 | |
michael@0 | 52 | namespace google_breakpad { |
michael@0 | 53 | |
michael@0 | 54 | //============================================================================= |
michael@0 | 55 | BOOL EnsureDirectoryPathExists(NSString *dirPath) { |
michael@0 | 56 | NSFileManager *mgr = [NSFileManager defaultManager]; |
michael@0 | 57 | |
michael@0 | 58 | NSDictionary *attrs = |
michael@0 | 59 | [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0750] |
michael@0 | 60 | forKey:NSFilePosixPermissions]; |
michael@0 | 61 | |
michael@0 | 62 | return [mgr createDirectoryAtPath:dirPath |
michael@0 | 63 | withIntermediateDirectories:YES |
michael@0 | 64 | attributes:attrs |
michael@0 | 65 | error:nil]; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | //============================================================================= |
michael@0 | 69 | BOOL ConfigFile::WriteData(const void *data, size_t length) { |
michael@0 | 70 | size_t result = write(config_file_, data, length); |
michael@0 | 71 | |
michael@0 | 72 | return result == length; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | //============================================================================= |
michael@0 | 76 | BOOL ConfigFile::AppendConfigData(const char *key, |
michael@0 | 77 | const void *data, size_t length) { |
michael@0 | 78 | assert(config_file_ != -1); |
michael@0 | 79 | |
michael@0 | 80 | if (!key) { |
michael@0 | 81 | DEBUGLOG(stderr, "Breakpad: Missing Key\n"); |
michael@0 | 82 | return NO; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | if (!data) { |
michael@0 | 86 | DEBUGLOG(stderr, "Breakpad: Missing data for key: %s\n", key ? key : |
michael@0 | 87 | "<Unknown Key>"); |
michael@0 | 88 | return NO; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | // Write the key, \n, length of data (ascii integer), \n, data |
michael@0 | 92 | char buffer[16]; |
michael@0 | 93 | char nl = '\n'; |
michael@0 | 94 | BOOL result = WriteData(key, strlen(key)); |
michael@0 | 95 | |
michael@0 | 96 | snprintf(buffer, sizeof(buffer) - 1, "\n%lu\n", length); |
michael@0 | 97 | result &= WriteData(buffer, strlen(buffer)); |
michael@0 | 98 | result &= WriteData(data, length); |
michael@0 | 99 | result &= WriteData(&nl, 1); |
michael@0 | 100 | return result; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | //============================================================================= |
michael@0 | 104 | BOOL ConfigFile::AppendConfigString(const char *key, |
michael@0 | 105 | const char *value) { |
michael@0 | 106 | return AppendConfigData(key, value, strlen(value)); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | //============================================================================= |
michael@0 | 110 | BOOL ConfigFile::AppendCrashTimeParameters(const char *processStartTimeString) { |
michael@0 | 111 | // Set process uptime parameter |
michael@0 | 112 | struct timeval tv; |
michael@0 | 113 | gettimeofday(&tv, NULL); |
michael@0 | 114 | |
michael@0 | 115 | char processUptimeString[32], processCrashtimeString[32]; |
michael@0 | 116 | // Set up time if we've received the start time. |
michael@0 | 117 | if (processStartTimeString) { |
michael@0 | 118 | time_t processStartTime = strtol(processStartTimeString, NULL, 10); |
michael@0 | 119 | time_t processUptime = tv.tv_sec - processStartTime; |
michael@0 | 120 | // Store the uptime in milliseconds. |
michael@0 | 121 | sprintf(processUptimeString, "%llu", |
michael@0 | 122 | static_cast<unsigned long long int>(processUptime) * 1000); |
michael@0 | 123 | if (!AppendConfigString(BREAKPAD_PROCESS_UP_TIME, processUptimeString)) |
michael@0 | 124 | return false; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | sprintf(processCrashtimeString, "%zd", tv.tv_sec); |
michael@0 | 128 | return AppendConfigString(BREAKPAD_PROCESS_CRASH_TIME, |
michael@0 | 129 | processCrashtimeString); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | //============================================================================= |
michael@0 | 133 | void ConfigFile::WriteFile(const char* directory, |
michael@0 | 134 | const SimpleStringDictionary *configurationParameters, |
michael@0 | 135 | const char *dump_dir, |
michael@0 | 136 | const char *minidump_id) { |
michael@0 | 137 | |
michael@0 | 138 | assert(config_file_ == -1); |
michael@0 | 139 | |
michael@0 | 140 | // Open and write out configuration file preamble |
michael@0 | 141 | if (directory) { |
michael@0 | 142 | snprintf(config_file_path_, sizeof(config_file_path_), "%s/Config-XXXXXX", |
michael@0 | 143 | directory); |
michael@0 | 144 | } else { |
michael@0 | 145 | strlcpy(config_file_path_, "/tmp/Config-XXXXXX", |
michael@0 | 146 | sizeof(config_file_path_)); |
michael@0 | 147 | } |
michael@0 | 148 | config_file_ = mkstemp(config_file_path_); |
michael@0 | 149 | |
michael@0 | 150 | if (config_file_ == -1) { |
michael@0 | 151 | DEBUGLOG(stderr, |
michael@0 | 152 | "mkstemp(config_file_path_) == -1 (%s)\n", |
michael@0 | 153 | strerror(errno)); |
michael@0 | 154 | return; |
michael@0 | 155 | } |
michael@0 | 156 | else { |
michael@0 | 157 | DEBUGLOG(stderr, "Writing config file to (%s)\n", config_file_path_); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | has_created_file_ = true; |
michael@0 | 161 | |
michael@0 | 162 | // Add the minidump dir |
michael@0 | 163 | AppendConfigString(kReporterMinidumpDirectoryKey, dump_dir); |
michael@0 | 164 | AppendConfigString(kReporterMinidumpIDKey, minidump_id); |
michael@0 | 165 | |
michael@0 | 166 | // Write out the configuration parameters |
michael@0 | 167 | BOOL result = YES; |
michael@0 | 168 | const SimpleStringDictionary &dictionary = *configurationParameters; |
michael@0 | 169 | |
michael@0 | 170 | const KeyValueEntry *entry = NULL; |
michael@0 | 171 | SimpleStringDictionaryIterator iter(dictionary); |
michael@0 | 172 | |
michael@0 | 173 | while ((entry = iter.Next())) { |
michael@0 | 174 | DEBUGLOG(stderr, |
michael@0 | 175 | "config: (%s) -> (%s)\n", |
michael@0 | 176 | entry->GetKey(), |
michael@0 | 177 | entry->GetValue()); |
michael@0 | 178 | result = AppendConfigString(entry->GetKey(), entry->GetValue()); |
michael@0 | 179 | |
michael@0 | 180 | if (!result) |
michael@0 | 181 | break; |
michael@0 | 182 | } |
michael@0 | 183 | AppendCrashTimeParameters( |
michael@0 | 184 | configurationParameters->GetValueForKey(BREAKPAD_PROCESS_START_TIME)); |
michael@0 | 185 | |
michael@0 | 186 | close(config_file_); |
michael@0 | 187 | config_file_ = -1; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | } // namespace google_breakpad |