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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/// Application instance class implementation
/**
* \file appinst.cpp
*
* Copyright (C) 2007, 2008, 2012 Lukas Jelinek, <lukas@aiken.cz>
*
* 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.
*
* Credits:
* Christian Ruppert (new include to build with GCC 4.4+)
*
*/
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <cstdio>
#include "appinst.h"
/// Lockfile permissions (currently 0644)
#define APPLOCK_PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
AppInstance::AppInstance(const std::string& rName, const std::string& rBase)
: m_fLocked(false)
{
std::string base(rBase);
if (base.empty())
base = APPLOCK_BASEDIR;
if (base[base.length()-1] == '/')
m_path = base + rName + ".pid";
else
m_path = base + "/" + rName + ".pid";
}
AppInstance::~AppInstance()
{
try {
Unlock();
} catch (AppInstException e) {}
}
bool AppInstance::DoLock()
{
int fd = open(m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, APPLOCK_PERM);
if (fd != -1) {
FILE* f = fdopen(fd, "w");
if (f == NULL) {
AppInstException e(errno);
close(fd);
throw e;
}
if (fprintf(f, "%u", (unsigned) getpid()) <= 0) {
AppInstException e(errno);
fclose(f);
throw e;
}
if (fclose(f) != 0)
throw AppInstException(errno);
m_fLocked = true;
return true;
}
if (errno != EEXIST)
throw AppInstException(errno);
return false;
}
bool AppInstance::Lock()
{
for (int i=0; i<100; i++) {
if (DoLock())
return true;
FILE* f = fopen(m_path.c_str(), "r");
if (f == NULL) {
if (errno != ENOENT)
throw AppInstException(errno);
}
else {
unsigned pid;
ssize_t len = fscanf(f, "%u", &pid);
if (len == -1) {
AppInstException e(errno);
fclose(f);
throw e;
}
else if (len == 0) {
AppInstException e(EIO);
fclose(f);
throw e;
}
fclose(f);
int res = kill((pid_t) pid, 0);
if (res == 0)
return false;
if (errno != ESRCH)
throw AppInstException(errno);
res = unlink(m_path.c_str());
if (res != 0 && errno != ENOENT)
throw AppInstException(errno);
}
}
return false;
}
void AppInstance::Unlock()
{
if (!m_fLocked)
return;
if (unlink(m_path.c_str()) != 0 && errno != ENOENT)
throw AppInstException(errno);
m_fLocked = false;
}
bool AppInstance::Exists() const
{
if (m_fLocked)
return true;
FILE* f = fopen(m_path.c_str(), "r");
if (f == NULL) {
if (errno == ENOENT)
return false;
else
throw AppInstException(errno);
}
bool ok = false;
unsigned pid;
if (fscanf(f, "%u", &pid) == 1) {
if (kill((pid_t) pid, 0) == 0)
ok = true;
else if (errno != ESRCH) {
AppInstException e(errno);
fclose(f);
throw e;
}
}
fclose(f);
return ok;
}
bool AppInstance::SendSignal(int iSigNo) const
{
FILE* f = fopen(m_path.c_str(), "r");
if (f == NULL) {
if (errno == ENOENT)
return false;
else
throw AppInstException(errno);
}
bool ok = false;
unsigned pid;
if (fscanf(f, "%u", &pid) == 1) {
if (pid != (unsigned) getpid()) {
if (kill((pid_t) pid, iSigNo) == 0) {
ok = true;
}
else if (errno != ESRCH) {
AppInstException e(errno);
fclose(f);
throw e;
}
}
}
fclose(f);
return ok;
}
|