-
Notifications
You must be signed in to change notification settings - Fork 51
/
appinst.h
137 lines (115 loc) · 3.13 KB
/
appinst.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/// Application instance class header
/**
* \file appinst.h
*
* Copyright (C) 2007, 2008 Lukas Jelinek, <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of one of the following licenses:
*
* \li 1. X11-style license (see LICENSE-X11)
* \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL)
* \li 3. GNU General Public License, version 2 (see LICENSE-GPL)
*
* If you want to help with choosing the best license for you,
* please visit http://www.gnu.org/licenses/license-list.html.
*
*/
#ifndef APPINST_H_
#define APPINST_H_
#include <string>
#define APPLOCK_BASEDIR "/var/run"
/// Exception class.
/**
* This class provides information about occurred errors.
*/
class AppInstException
{
public:
/// Constructor.
/**
* \param[in] iErr error number
*/
AppInstException(int iErr) : m_iErr(iErr) {}
/// Returns the error number.
/**
* \return error number
*/
inline int GetErrorNumber() const
{
return m_iErr;
}
private:
int m_iErr; ///< error number
};
/// Application instance management class.
/**
* This class is intended for application which require to
* be running only once (one instance only). It provides some
* methods for simple locking, signaling etc.
*/
class AppInstance
{
public:
/// Constructor.
/**
* \param[in] rName application name
* \param[in] rBase lockfile base directory
*
* \attention If an empty base directory is given it is replaced by
* the default value.
*/
AppInstance(const std::string& rName, const std::string& rBase = APPLOCK_BASEDIR);
/// Destructor.
~AppInstance();
/// Attempts to lock the instance.
/**
* This method attempts to create a lockfile. If the file
* already exists it checks whether its owner is still living.
* If it does this method fails. Otherwise it unlinks this file
* and re-attempts to create it.
*
* \return true = instance locked, false = otherwise
*/
bool Lock();
/// Unlocks the instance.
/**
* This method removes (unlinks) the appropriate lockfile.
* If the instance hasn't been locked this method has no
* effect.
*/
void Unlock();
/// Checks whether an instance of this application exists.
/**
* If this instance has acquired the lockfile the call will
* be successful. Otherwise it checks for existence of
* another running instance.
*
* \return true = instance exists, false = otherwise
*/
bool Exists() const;
/// Sends a signal to an instance of this application.
/**
* This method doesn't signal the current instance.
*
* \param[in] iSigNo signal number
* \return true = success, false = otherwise
*/
bool SendSignal(int iSigNo) const;
/// Terminates an instance of this application.
/**
* This method doesn't terminate the current instance.
*
* \return true = success, false = otherwise
*/
inline bool Terminate() const
{
return SendSignal(SIGTERM);
}
protected:
bool DoLock();
private:
std::string m_path; ///< lock path
bool m_fLocked; ///< locked yes/no
};
#endif /*APPINST_H_*/