1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROMECAST_CRASH_LINUX_SYNCHRONIZED_MINIDUMP_MANAGER_H_
6 #define CHROMECAST_CRASH_LINUX_SYNCHRONIZED_MINIDUMP_MANAGER_H_
12 #include "base/files/file_path.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/values.h"
16 #include "chromecast/crash/linux/dump_info.h"
18 namespace chromecast
{
20 // Abstract base class for mutually-exclusive minidump handling. Ensures
21 // synchronized access among instances of this class to the minidumps directory
22 // using a file lock. The "lockfile" also holds serialized metadata about each
23 // of the minidumps in the directory. Derived classes should not access the
24 // lockfile directly. Instead, use protected methods to query and modify the
25 // metadata, but only within the implementation of DoWork().
27 // This class holds an in memory representation of the lockfile and metadata
28 // file when the lockfile is held. Modifier methods work on this in memory
29 // representation. When the lockfile is released, the in memory representations
30 // are written to file
32 // The lockfile file is of the following format:
38 // Note that this isn't a valid json object. It is formatted in this way so
39 // that producers to this file do not need to understand json.
41 // Current external producers:
45 // The metadata file is a separate file containing a json dictionary.
47 class SynchronizedMinidumpManager
{
49 // Max number of dumps allowed in lockfile.
50 static const int kMaxLockfileDumps
;
52 // Length of a ratelimit period in seconds.
53 static const int kRatelimitPeriodSeconds
;
55 // Number of dumps allowed per period.
56 static const int kRatelimitPeriodMaxDumps
;
58 virtual ~SynchronizedMinidumpManager();
60 // Returns whether this object's file locking method is nonblocking or not.
61 bool non_blocking() { return non_blocking_
; }
63 // Sets the file locking mechansim to be nonblocking or not.
64 void set_non_blocking(bool non_blocking
) { non_blocking_
= non_blocking
; }
67 SynchronizedMinidumpManager();
69 // Acquires the lock, calls DoWork(), then releases the lock when DoWork()
70 // returns. Derived classes should expose a method which calls this. Returns
71 // the status of DoWork(), or -1 if the lock was not successfully acquired.
72 int AcquireLockAndDoWork();
74 // Derived classes must implement this method. It will be called from
75 // DoWorkLocked after the lock has been successfully acquired. The lockfile
76 // shall be accessed and mutated only through the methods below. All other
77 // files shall be managed as needed by the derived class.
78 virtual int DoWork() = 0;
80 // Get the current dumps in the lockfile.
81 ScopedVector
<DumpInfo
> GetDumps();
83 // Set |dumps| as the dumps in |lockfile_|, replacing current list of dumps.
84 int SetCurrentDumps(const ScopedVector
<DumpInfo
>& dumps
);
86 // Serialize |dump_info| and append it to the lockfile. Note that the child
87 // class must only call this inside DoWork(). This should be the only method
88 // used to write to the lockfile. Only call this if the minidump has been
89 // generated in the minidumps directory successfully. Returns 0 on success,
91 int AddEntryToLockFile(const DumpInfo
& dump_info
);
93 // Remove the lockfile entry at |index| in the current in memory
94 // representation of the lockfile. If the index is invalid returns -1.
95 // Otherwise returns 0.
96 int RemoveEntryFromLockFile(int index
);
98 // Get the number of un-uploaded dumps in the dump_path directory.
99 // If delete_all_dumps is true, also delete all these files, this is used to
100 // clean lingering dump files.
101 int GetNumDumps(bool delete_all_dumps
);
103 // If true, the flock on the lockfile will be nonblocking.
106 // Cached path for the minidumps directory.
107 const base::FilePath dump_path_
;
110 // Acquire the lock file. Blocks if another process holds it, or if called
111 // a second time by the same process. Returns the fd of the lockfile if
112 // successful, or -1 if failed.
113 int AcquireLockFile();
115 // Parse the lockfile and metadata file, populating |dumps_| and |metadata_|
116 // for modifier functions to use. Return -1 if an error occurred. Otherwise,
117 // return 0. This must not be called unless |this| has acquired the lock.
120 // Write deserialized |dumps| to |lockfile_path_| and the deserialized
121 // |metadata| to |metadata_path_|.
122 int WriteFiles(const base::ListValue
* dumps
, const base::Value
* metadata
);
124 // Creates an empty lock file and an initialized metadata file.
125 int InitializeFiles();
127 // Release the lock file with the associated *fd*.
128 void ReleaseLockFile();
130 // Returns true if |num_dumps| can be written to the lockfile. We can write
131 // the dumps if the number of dumps in the lockfile after |num_dumps| is added
132 // is less than or equal to |kMaxLockfileDumps| and the number of dumps in the
133 // current ratelimit period after |num_dumps| is added is less than or equal
134 // to |kRatelimitPeriodMaxDumps|.
135 bool CanWriteDumps(int num_dumps
);
137 const std::string lockfile_path_
;
138 const std::string metadata_path_
;
140 scoped_ptr
<base::Value
> metadata_
;
141 scoped_ptr
<base::ListValue
> dumps_
;
143 DISALLOW_COPY_AND_ASSIGN(SynchronizedMinidumpManager
);
146 } // namespace chromecast
148 #endif // CHROMECAST_CRASH_LINUX_SYNCHRONIZED_MINIDUMP_MANAGER_H_