Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromecast / crash / linux / synchronized_minidump_manager.h
blob95e0607ba882340100d1da45628e37ed78851b19
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_
8 #include <time.h>
10 #include <string>
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:
33 // { <dump_info1> }
34 // { <dump_info2> }
35 // ...
36 // { <dump_infoN> }
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:
42 // + watchdog
45 // The metadata file is a separate file containing a json dictionary.
47 class SynchronizedMinidumpManager {
48 public:
49 // Length of a ratelimit period in seconds.
50 static const int kRatelimitPeriodSeconds;
52 // Number of dumps allowed per period.
53 static const int kRatelimitPeriodMaxDumps;
55 virtual ~SynchronizedMinidumpManager();
57 // Returns whether this object's file locking method is nonblocking or not.
58 bool non_blocking() { return non_blocking_; }
60 // Sets the file locking mechansim to be nonblocking or not.
61 void set_non_blocking(bool non_blocking) { non_blocking_ = non_blocking; }
63 protected:
64 SynchronizedMinidumpManager();
66 // Acquires the lock, calls DoWork(), then releases the lock when DoWork()
67 // returns. Derived classes should expose a method which calls this. Returns
68 // the status of DoWork(), or -1 if the lock was not successfully acquired.
69 int AcquireLockAndDoWork();
71 // Derived classes must implement this method. It will be called from
72 // DoWorkLocked after the lock has been successfully acquired. The lockfile
73 // shall be accessed and mutated only through the methods below. All other
74 // files shall be managed as needed by the derived class.
75 virtual int DoWork() = 0;
77 // Get the current dumps in the lockfile.
78 ScopedVector<DumpInfo> GetDumps();
80 // Set |dumps| as the dumps in |lockfile_|, replacing current list of dumps.
81 int SetCurrentDumps(const ScopedVector<DumpInfo>& dumps);
83 // Serialize |dump_info| and append it to the lockfile. Note that the child
84 // class must only call this inside DoWork(). This should be the only method
85 // used to write to the lockfile. Only call this if the minidump has been
86 // generated in the minidumps directory successfully. Returns 0 on success,
87 // -1 otherwise.
88 int AddEntryToLockFile(const DumpInfo& dump_info);
90 // Remove the lockfile entry at |index| in the current in memory
91 // representation of the lockfile. If the index is invalid returns -1.
92 // Otherwise returns 0.
93 int RemoveEntryFromLockFile(int index);
95 // Get the number of un-uploaded dumps in the dump_path directory.
96 // If delete_all_dumps is true, also delete all these files, this is used to
97 // clean lingering dump files.
98 int GetNumDumps(bool delete_all_dumps);
100 // Increment the number of dumps in the current ratelimit period.
101 // Returns 0 on success, < 0 on error.
102 int IncrementNumDumpsInCurrentPeriod();
104 // Returns true when dumps uploaded in current rate limit period is less than
105 // |kRatelimitPeriodMaxDumps|. Resets rate limit period if period time has
106 // elapsed.
107 bool CanUploadDump();
109 // If true, the flock on the lockfile will be nonblocking.
110 bool non_blocking_;
112 // Cached path for the minidumps directory.
113 const base::FilePath dump_path_;
115 private:
116 // Acquire the lock file. Blocks if another process holds it, or if called
117 // a second time by the same process. Returns the fd of the lockfile if
118 // successful, or -1 if failed.
119 int AcquireLockFile();
121 // Parse the lockfile and metadata file, populating |dumps_| and |metadata_|
122 // for modifier functions to use. Return -1 if an error occurred. Otherwise,
123 // return 0. This must not be called unless |this| has acquired the lock.
124 int ParseFiles();
126 // Write deserialized |dumps| to |lockfile_path_| and the deserialized
127 // |metadata| to |metadata_path_|.
128 int WriteFiles(const base::ListValue* dumps, const base::Value* metadata);
130 // Creates an empty lock file and an initialized metadata file.
131 int InitializeFiles();
133 // Release the lock file with the associated *fd*.
134 void ReleaseLockFile();
136 const std::string lockfile_path_;
137 const std::string metadata_path_;
138 int lockfile_fd_;
139 scoped_ptr<base::Value> metadata_;
140 scoped_ptr<base::ListValue> dumps_;
142 DISALLOW_COPY_AND_ASSIGN(SynchronizedMinidumpManager);
145 } // namespace chromecast
147 #endif // CHROMECAST_CRASH_LINUX_SYNCHRONIZED_MINIDUMP_MANAGER_H_