1 // Copyright (c) 2012 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <stdio.h>
31 
32 #include "client/linux/handler/minidump_descriptor.h"
33 
34 #include "common/linux/guid_creator.h"
35 
36 namespace google_breakpad {
37 
38 //static
39 const MinidumpDescriptor::MicrodumpOnConsole
40     MinidumpDescriptor::kMicrodumpOnConsole = {};
41 
42 MinidumpDescriptor::MinidumpDescriptor(const MinidumpDescriptor& descriptor)
43     : mode_(descriptor.mode_),
44       fd_(descriptor.fd_),
45       directory_(descriptor.directory_),
46       c_path_(NULL),
47       size_limit_(descriptor.size_limit_),
48       microdump_extra_info_(descriptor.microdump_extra_info_) {
49   // The copy constructor is not allowed to be called on a MinidumpDescriptor
50   // with a valid path_, as getting its c_path_ would require the heap which
51   // can cause problems in compromised environments.
52   assert(descriptor.path_.empty());
53 }
54 
55 MinidumpDescriptor& MinidumpDescriptor::operator=(
56     const MinidumpDescriptor& descriptor) {
57   assert(descriptor.path_.empty());
58 
59   mode_ = descriptor.mode_;
60   fd_ = descriptor.fd_;
61   directory_ = descriptor.directory_;
62   path_.clear();
63   if (c_path_) {
64     // This descriptor already had a path set, so generate a new one.
65     c_path_ = NULL;
66     UpdatePath();
67   }
68   size_limit_ = descriptor.size_limit_;
69   microdump_extra_info_ = descriptor.microdump_extra_info_;
70   return *this;
71 }
72 
73 void MinidumpDescriptor::UpdatePath() {
74   assert(mode_ == kWriteMinidumpToFile && !directory_.empty());
75 
76   GUID guid;
77   char guid_str[kGUIDStringLength + 1];
78   if (!CreateGUID(&guid) || !GUIDToString(&guid, guid_str, sizeof(guid_str))) {
79     assert(false);
80   }
81 
82   path_.clear();
83   path_ = directory_ + "/" + guid_str + ".dmp";
84   c_path_ = path_.c_str();
85 }
86 
87 }  // namespace google_breakpad
88