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_
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_vector.h"
14 namespace chromecast
{
18 // Abstract base class for mutually-exclusive minidump handling. Ensures
19 // synchronized access among instances of this class to the minidumps directory
20 // using a file lock. The "lockfile" also holds serialized metadata about each
21 // of the minidumps in the directory. Derived classes should not access the
22 // lockfile directly. Instead, use protected methods to query and modify the
23 // metadata, but only within the implementation of DoWork().
24 class SynchronizedMinidumpManager
{
26 virtual ~SynchronizedMinidumpManager();
28 // Returns whether this object's file locking method is nonblocking or not.
29 bool non_blocking() { return non_blocking_
; }
31 // Sets the file locking mechansim to be nonblocking or not.
32 void set_non_blocking(bool non_blocking
) { non_blocking_
= non_blocking
; }
35 SynchronizedMinidumpManager();
37 // Acquires the lock, calls DoWork(), then releases the lock when DoWork()
38 // returns. Derived classes should expose a method which calls this. Returns
39 // the status of DoWork(), or -1 if the lock was not successfully acquired.
40 int AcquireLockAndDoWork();
42 // Derived classes must implement this method. It will be called from
43 // DoWorkLocked after the lock has been successfully acquired. The lockfile
44 // shall be accessed and mutated only through the methods below. All other
45 // files shall be managed as needed by the derived class.
46 virtual int DoWork() = 0;
48 // Access the container holding all the metadata for the dumps. Note that
49 // the child class must only call this inside DoWork(). This is lazy. If the
50 // lockfile has not been parsed yet, it will be parsed when this is called.
51 const ScopedVector
<DumpInfo
>& GetDumpMetadata();
53 // Serialize |dump_info| and append it to the lockfile. Note that the child
54 // class must only call this inside DoWork(). This should be the only method
55 // used to write to the lockfile. Only call this if the minidump has been
56 // generated in the minidumps directory successfully. Returns 0 on success,
58 int AddEntryToLockFile(const DumpInfo
& dump_info
);
60 // Remove the lockfile entry at |index| in the container returned by
61 // GetDumpMetadata(). If the index is invalid or an IO error occurred, returns
62 // -1. Otherwise returns 0. When this function returns, both the in-memory
63 // containter returned by GetDumpMetadata and the persistent lockfile will be
65 int RemoveEntryFromLockFile(int index
);
67 // Get the number of un-uploaded dumps in the dump_path directory.
68 // If delete_all_dumps is true, also delete all these files, this is used to
69 // clean lingering dump files.
70 int GetNumDumps(bool delete_all_dumps
);
72 // TODO(slan): Remove this accessor. All I/O on the lockfile in inherited
73 // classes should be done via GetDumpMetadata(), AddEntryToLockFile(), and
74 // RemoveEntryFromLockFile().
75 const std::string
& lockfile_path() { return lockfile_path_
; }
77 // If true, the flock on the lockfile will be nonblocking
80 // Cached path for the minidumps directory.
81 base::FilePath dump_path_
;
84 // Acquire the lock file. Blocks if another process holds it, or if called
85 // a second time by the same process. Returns the fd of the lockfile if
86 // successful, or -1 if failed.
87 int AcquireLockFile();
89 // Parse the lockfile, populating |dumps_| for descendants to use. Return -1
90 // if an error occurred. Otherwise, return 0. This must not be called unless
91 // |this| has acquired the lock.
94 // Release the lock file with the associated *fd*.
95 void ReleaseLockFile();
97 std::string lockfile_path_
;
99 scoped_ptr
<ScopedVector
<DumpInfo
> > dump_metadata_
;
101 DISALLOW_COPY_AND_ASSIGN(SynchronizedMinidumpManager
);
104 } // namespace chromecast
106 #endif // CHROMECAST_CRASH_LINUX_SYNCHRONIZED_MINIDUMP_MANAGER_H_