michael@0: // Copyright (c) 2011, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Utility class that can persist a SimpleStringDictionary to disk. michael@0: michael@0: #import "client/mac/crash_generation/ConfigFile.h" michael@0: michael@0: #import michael@0: #include michael@0: #include michael@0: michael@0: #import "client/apple/Framework/BreakpadDefines.h" michael@0: #import "common/mac/SimpleStringDictionary.h" michael@0: #import "GTMDefines.h" michael@0: michael@0: #define VERBOSE 0 michael@0: michael@0: #if VERBOSE michael@0: bool gDebugLog = true; michael@0: #else michael@0: bool gDebugLog = false; michael@0: #endif michael@0: michael@0: #define DEBUGLOG if (gDebugLog) fprintf michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: //============================================================================= michael@0: BOOL EnsureDirectoryPathExists(NSString *dirPath) { michael@0: NSFileManager *mgr = [NSFileManager defaultManager]; michael@0: michael@0: NSDictionary *attrs = michael@0: [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0750] michael@0: forKey:NSFilePosixPermissions]; michael@0: michael@0: return [mgr createDirectoryAtPath:dirPath michael@0: withIntermediateDirectories:YES michael@0: attributes:attrs michael@0: error:nil]; michael@0: } michael@0: michael@0: //============================================================================= michael@0: BOOL ConfigFile::WriteData(const void *data, size_t length) { michael@0: size_t result = write(config_file_, data, length); michael@0: michael@0: return result == length; michael@0: } michael@0: michael@0: //============================================================================= michael@0: BOOL ConfigFile::AppendConfigData(const char *key, michael@0: const void *data, size_t length) { michael@0: assert(config_file_ != -1); michael@0: michael@0: if (!key) { michael@0: DEBUGLOG(stderr, "Breakpad: Missing Key\n"); michael@0: return NO; michael@0: } michael@0: michael@0: if (!data) { michael@0: DEBUGLOG(stderr, "Breakpad: Missing data for key: %s\n", key ? key : michael@0: ""); michael@0: return NO; michael@0: } michael@0: michael@0: // Write the key, \n, length of data (ascii integer), \n, data michael@0: char buffer[16]; michael@0: char nl = '\n'; michael@0: BOOL result = WriteData(key, strlen(key)); michael@0: michael@0: snprintf(buffer, sizeof(buffer) - 1, "\n%lu\n", length); michael@0: result &= WriteData(buffer, strlen(buffer)); michael@0: result &= WriteData(data, length); michael@0: result &= WriteData(&nl, 1); michael@0: return result; michael@0: } michael@0: michael@0: //============================================================================= michael@0: BOOL ConfigFile::AppendConfigString(const char *key, michael@0: const char *value) { michael@0: return AppendConfigData(key, value, strlen(value)); michael@0: } michael@0: michael@0: //============================================================================= michael@0: BOOL ConfigFile::AppendCrashTimeParameters(const char *processStartTimeString) { michael@0: // Set process uptime parameter michael@0: struct timeval tv; michael@0: gettimeofday(&tv, NULL); michael@0: michael@0: char processUptimeString[32], processCrashtimeString[32]; michael@0: // Set up time if we've received the start time. michael@0: if (processStartTimeString) { michael@0: time_t processStartTime = strtol(processStartTimeString, NULL, 10); michael@0: time_t processUptime = tv.tv_sec - processStartTime; michael@0: // Store the uptime in milliseconds. michael@0: sprintf(processUptimeString, "%llu", michael@0: static_cast(processUptime) * 1000); michael@0: if (!AppendConfigString(BREAKPAD_PROCESS_UP_TIME, processUptimeString)) michael@0: return false; michael@0: } michael@0: michael@0: sprintf(processCrashtimeString, "%zd", tv.tv_sec); michael@0: return AppendConfigString(BREAKPAD_PROCESS_CRASH_TIME, michael@0: processCrashtimeString); michael@0: } michael@0: michael@0: //============================================================================= michael@0: void ConfigFile::WriteFile(const char* directory, michael@0: const SimpleStringDictionary *configurationParameters, michael@0: const char *dump_dir, michael@0: const char *minidump_id) { michael@0: michael@0: assert(config_file_ == -1); michael@0: michael@0: // Open and write out configuration file preamble michael@0: if (directory) { michael@0: snprintf(config_file_path_, sizeof(config_file_path_), "%s/Config-XXXXXX", michael@0: directory); michael@0: } else { michael@0: strlcpy(config_file_path_, "/tmp/Config-XXXXXX", michael@0: sizeof(config_file_path_)); michael@0: } michael@0: config_file_ = mkstemp(config_file_path_); michael@0: michael@0: if (config_file_ == -1) { michael@0: DEBUGLOG(stderr, michael@0: "mkstemp(config_file_path_) == -1 (%s)\n", michael@0: strerror(errno)); michael@0: return; michael@0: } michael@0: else { michael@0: DEBUGLOG(stderr, "Writing config file to (%s)\n", config_file_path_); michael@0: } michael@0: michael@0: has_created_file_ = true; michael@0: michael@0: // Add the minidump dir michael@0: AppendConfigString(kReporterMinidumpDirectoryKey, dump_dir); michael@0: AppendConfigString(kReporterMinidumpIDKey, minidump_id); michael@0: michael@0: // Write out the configuration parameters michael@0: BOOL result = YES; michael@0: const SimpleStringDictionary &dictionary = *configurationParameters; michael@0: michael@0: const KeyValueEntry *entry = NULL; michael@0: SimpleStringDictionaryIterator iter(dictionary); michael@0: michael@0: while ((entry = iter.Next())) { michael@0: DEBUGLOG(stderr, michael@0: "config: (%s) -> (%s)\n", michael@0: entry->GetKey(), michael@0: entry->GetValue()); michael@0: result = AppendConfigString(entry->GetKey(), entry->GetValue()); michael@0: michael@0: if (!result) michael@0: break; michael@0: } michael@0: AppendCrashTimeParameters( michael@0: configurationParameters->GetValueForKey(BREAKPAD_PROCESS_START_TIME)); michael@0: michael@0: close(config_file_); michael@0: config_file_ = -1; michael@0: } michael@0: michael@0: } // namespace google_breakpad