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 while it is
28 // held. Modifier methods work on this in memory representation. When the
29 // lockfile is released, the in memory representation is written to file.
30 class SynchronizedMinidumpManager
{
32 // Max number of dumps allowed in lockfile.
33 static const int kMaxLockfileDumps
;
35 // Length of a ratelimit period in seconds.
36 static const int kRatelimitPeriodSeconds
;
38 // Number of dumps allowed per period.
39 static const int kRatelimitPeriodMaxDumps
;
41 virtual ~SynchronizedMinidumpManager();
43 // Returns whether this object's file locking method is nonblocking or not.
44 bool non_blocking() { return non_blocking_
; }
46 // Sets the file locking mechansim to be nonblocking or not.
47 void set_non_blocking(bool non_blocking
) { non_blocking_
= non_blocking
; }
50 SynchronizedMinidumpManager();
52 // Acquires the lock, calls DoWork(), then releases the lock when DoWork()
53 // returns. Derived classes should expose a method which calls this. Returns
54 // the status of DoWork(), or -1 if the lock was not successfully acquired.
55 int AcquireLockAndDoWork();
57 // Derived classes must implement this method. It will be called from
58 // DoWorkLocked after the lock has been successfully acquired. The lockfile
59 // shall be accessed and mutated only through the methods below. All other
60 // files shall be managed as needed by the derived class.
61 virtual int DoWork() = 0;
63 // Get the current dumps in the lockfile.
64 ScopedVector
<DumpInfo
> GetDumps();
66 // Set |dumps| as the dumps in |lockfile_|, replacing current list of dumps.
67 int SetCurrentDumps(const ScopedVector
<DumpInfo
>& dumps
);
69 // Serialize |dump_info| and append it to the lockfile. Note that the child
70 // class must only call this inside DoWork(). This should be the only method
71 // used to write to the lockfile. Only call this if the minidump has been
72 // generated in the minidumps directory successfully. Returns 0 on success,
74 int AddEntryToLockFile(const DumpInfo
& dump_info
);
76 // Remove the lockfile entry at |index| in the current in memory
77 // representation of the lockfile. If the index is invalid returns -1.
78 // Otherwise returns 0.
79 int RemoveEntryFromLockFile(int index
);
81 // Get the number of un-uploaded dumps in the dump_path directory.
82 // If delete_all_dumps is true, also delete all these files, this is used to
83 // clean lingering dump files.
84 int GetNumDumps(bool delete_all_dumps
);
86 // If true, the flock on the lockfile will be nonblocking.
89 // Cached path for the minidumps directory.
90 base::FilePath dump_path_
;
93 // Acquire the lock file. Blocks if another process holds it, or if called
94 // a second time by the same process. Returns the fd of the lockfile if
95 // successful, or -1 if failed.
96 int AcquireLockFile();
98 // Parse the lockfile, populating |lockfile_contents_| for modifier functions
99 // to use. Return -1 if an error occurred. Otherwise, return 0. This must not
100 // be called unless |this| has acquired the lock.
103 // Write deserialized lockfile to |lockfile_path_|.
104 int WriteLockFile(const base::Value
& contents
);
106 // Creates an empty lock file.
107 int CreateEmptyLockFile();
109 // Release the lock file with the associated *fd*.
110 void ReleaseLockFile();
112 // Returns true if |num_dumps| can be written to the lockfile. We can write
113 // the dumps if the number of dumps in the lockfile after |num_dumps| is added
114 // is less than or equal to |kMaxLockfileDumps| and the number of dumps in the
115 // current ratelimit period after |num_dumps| is added is less than or equal
116 // to |kRatelimitPeriodMaxDumps|.
117 bool CanWriteDumps(int num_dumps
);
119 std::string lockfile_path_
;
121 scoped_ptr
<base::Value
> lockfile_contents_
;
123 DISALLOW_COPY_AND_ASSIGN(SynchronizedMinidumpManager
);
126 } // namespace chromecast
128 #endif // CHROMECAST_CRASH_LINUX_SYNCHRONIZED_MINIDUMP_MANAGER_H_