Subversion Repositories public

Rev

Rev 71 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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