1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <memory>
34 #include <string>
35 
36 #include "mongo/base/disallow_copying.h"
37 #include "mongo/base/status.h"
38 
39 namespace mongo {
40 
41 constexpr StringData kLockFileBasename = "mongod.lock"_sd;
42 
43 class StorageEngineLockFile {
44     MONGO_DISALLOW_COPYING(StorageEngineLockFile);
45 
46 public:
47     /**
48      * Checks existing lock file, if present, to see if it contains data from a previous
49      * unclean shutdown. A clean shutdown should have produced a zero length lock file.
50      * Uses open() to read existing lock file or create new file.
51      * Uses boost::filesystem to check lock file so may throw boost::exception.
52      */
53     StorageEngineLockFile(const std::string& dbpath, StringData fileName = kLockFileBasename);
54 
55     virtual ~StorageEngineLockFile();
56 
57     /**
58      * Returns the path to the lock file.
59      */
60     std::string getFilespec() const;
61 
62     /**
63      * Returns true if lock file was not zeroed out due to previous unclean shutdown.
64      * This state is evaluated at object initialization to allow storage engine
65      * to make decisions on recovery based on this information after open() has been called.
66      */
67     bool createdByUncleanShutdown() const;
68 
69     /**
70      * Opens and locks 'mongod.lock' in 'dbpath' directory.
71      */
72     Status open();
73 
74     /**
75      * Closes lock file handles.
76      */
77     void close();
78 
79     /**
80      * Writes current process ID to file.
81      * Fails if lock file has not been opened.
82      */
83     Status writePid();
84 
85     /**
86      * Truncates file contents and releases file locks.
87      */
88     void clearPidAndUnlock();
89 
90 private:
91     std::string _dbpath;
92     std::string _filespec;
93     bool _uncleanShutdown;
94 
95     class LockFileHandle;
96     std::unique_ptr<LockFileHandle> _lockFileHandle;
97 };
98 
99 }  // namespace mongo
100