Subversion Repositories public

Rev

Rev 69 | Rev 73 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 69 Rev 71
Line 24... Line 24...
24
#include <errno.h>
24
#include <errno.h>
25
#include <signal.h>
25
#include <signal.h>
26
26
27
#include "appinst.h"
27
#include "appinst.h"
28
28
29
#ifdef APPINST_LOCK_DIRECTORY
-
 
30
#define LOCKDIR APPINST_LOCK_DIRECTORY
-
 
31
#else
-
 
32
#define LOCKDIR "/tmp"
-
 
33
#endif // APPINST_LOCK_DIRECTORY
-
 
34
-
 
35
 
29
 
36
30
37
AppInstance::AppInstance(const std::string& rName)
31
AppInstance::AppInstance(const std::string& rName, const std::string& rBase)
38
: m_name(rName),
-
 
39
  m_fLocked(false)
32
: m_fLocked(false)
40
{
33
{
-
 
34
  std::string base(rBase);
-
 
35
  if (base.empty())
-
 
36
    base = APPLOCK_BASEDIR;
41
 
37
 
-
 
38
  if (base[base.length()-1] == '/')
-
 
39
    m_path = base + rName + ".pid";
-
 
40
  else
-
 
41
    m_path = base + "/" + rName + ".pid";
42
}
42
}
43
43
44
AppInstance::~AppInstance()
44
AppInstance::~AppInstance()
45
{
45
{
46
  try {
46
  try {
47
    Unlock();
47
    Unlock();
48
  } catch (AppInstException e) {}
48
  } catch (AppInstException e) {}
49
}
49
}
50
50
51
bool AppInstance::DoLock(const char* path)
51
bool AppInstance::DoLock()
52
{
52
{
53
  int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
53
  int fd = open(m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
54
  if (fd != -1) {
54
  if (fd != -1) {
55
    FILE* f = fdopen(fd, "w");
55
    FILE* f = fdopen(fd, "w");
56
    if (f == NULL) {
56
    if (f == NULL) {
57
      AppInstException e(errno);
57
      AppInstException e(errno);
58
      close(fd);
58
      close(fd);
Line 78... Line 78...
78
  return false;
78
  return false;
79
}
79
}
80
80
81
bool AppInstance::Lock()
81
bool AppInstance::Lock()
82
{
82
{
83
  std::string fn = GetLockfile();
-
 
84
 
-
 
85
  for (int i=0; i<100; i++) {
83
  for (int i=0; i<100; i++) {
86
    if (DoLock(fn.c_str()))
84
    if (DoLock())
87
      return true;
85
      return true;
88
   
86
   
89
    FILE* f = fopen(fn.c_str(), "r");
87
    FILE* f = fopen(m_path.c_str(), "r");
90
    if (f == NULL) {
88
    if (f == NULL) {
91
      if (errno != ENOENT)
89
      if (errno != ENOENT)
92
        throw AppInstException(errno);
90
        throw AppInstException(errno);
93
    }
91
    }
94
    else {
92
    else {
Line 112... Line 110...
112
        return false;
110
        return false;
113
       
111
       
114
      if (errno != ESRCH)
112
      if (errno != ESRCH)
115
        throw AppInstException(errno);
113
        throw AppInstException(errno);
116
       
114
       
117
      res = unlink(fn.c_str());
115
      res = unlink(m_path.c_str());
118
      if (res != 0 && errno != ENOENT)
116
      if (res != 0 && errno != ENOENT)
119
        throw AppInstException(errno);
117
        throw AppInstException(errno);
120
    }
118
    }
121
  }
119
  }
122
 
120
 
Line 126... Line 124...
126
void AppInstance::Unlock()
124
void AppInstance::Unlock()
127
{
125
{
128
  if (!m_fLocked)
126
  if (!m_fLocked)
129
    return;
127
    return;
130
   
128
   
131
  if (unlink(GetLockfile().c_str()) != 0 && errno != ENOENT)
129
  if (unlink(m_path.c_str()) != 0 && errno != ENOENT)
132
    throw AppInstException(errno);
130
    throw AppInstException(errno);
133
   
131
   
134
  m_fLocked = false;
132
  m_fLocked = false;
135
}
133
}
136
134
137
bool AppInstance::Exists() const
135
bool AppInstance::Exists() const
138
{
136
{
139
  if (m_fLocked)
137
  if (m_fLocked)
140
    return true;
138
    return true;
141
 
139
 
142
  FILE* f = fopen(GetLockfile().c_str(), "r");
140
  FILE* f = fopen(m_path.c_str(), "r");
143
  if (f == NULL) {
141
  if (f == NULL) {
144
    if (errno == ENOENT)
142
    if (errno == ENOENT)
145
      return false;
143
      return false;
146
    else
144
    else
147
      throw AppInstException(errno);
145
      throw AppInstException(errno);
Line 165... Line 163...
165
  return ok;
163
  return ok;
166
}
164
}
167
165
168
bool AppInstance::SendSignal(int iSigNo) const
166
bool AppInstance::SendSignal(int iSigNo) const
169
{
167
{
170
  FILE* f = fopen(GetLockfile().c_str(), "r");
168
  FILE* f = fopen(m_path.c_str(), "r");
171
  if (f == NULL) {
169
  if (f == NULL) {
172
    if (errno == ENOENT)
170
    if (errno == ENOENT)
173
      return false;
171
      return false;
174
    else
172
    else
175
      throw AppInstException(errno);
173
      throw AppInstException(errno);
Line 194... Line 192...
194
  fclose(f);
192
  fclose(f);
195
 
193
 
196
  return ok;
194
  return ok;
197
}
195
}
198
196
199
std::string AppInstance::GetLockfile() const
-
 
200
{
-
 
201
  return std::string(LOCKDIR) + "/" + m_name + ".pid";
-
 
202
}
-